1 | <?php |
---|
2 | /* |
---|
3 | * Plugin Name: ConcoursPhoto |
---|
4 | * File : Concours.class.php |
---|
5 | */ |
---|
6 | |
---|
7 | global $user, $conf; |
---|
8 | |
---|
9 | class Concours |
---|
10 | { |
---|
11 | var $concours_id = null; |
---|
12 | var $concours_infos = array(); |
---|
13 | |
---|
14 | /* var $create_date = null; |
---|
15 | var $name = null; |
---|
16 | var $descr = null; |
---|
17 | var $begin_date = null; |
---|
18 | var $end_date = null; |
---|
19 | var $category = null; |
---|
20 | var $groups = null; |
---|
21 | */ |
---|
22 | |
---|
23 | // General configuration parameters |
---|
24 | var $my_config = array(); |
---|
25 | /* |
---|
26 | active_menubar |
---|
27 | nbconcours_menubar |
---|
28 | */ |
---|
29 | var $user_groups = array(); |
---|
30 | var $debug = false; |
---|
31 | |
---|
32 | // Class constructor with concours id. Return NULL if not existing or create it |
---|
33 | function Concours($concours_id = null, $force_creation = NULL) |
---|
34 | { |
---|
35 | // $this->get_user_groups(); |
---|
36 | if ($concours_id !== null) |
---|
37 | { |
---|
38 | $this->concours_id = (int)$concours_id; |
---|
39 | $this->get_concours($concours_id); |
---|
40 | } |
---|
41 | // Load general parameters |
---|
42 | $this->load_config(); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | // Load general configuration from config_database |
---|
47 | function load_config() |
---|
48 | { |
---|
49 | |
---|
50 | $query = ' |
---|
51 | SELECT value |
---|
52 | FROM '.CONFIG_TABLE.' |
---|
53 | WHERE param = \'concoursphoto\' |
---|
54 | ;'; |
---|
55 | |
---|
56 | $result = pwg_query($query); |
---|
57 | |
---|
58 | if($result) |
---|
59 | { |
---|
60 | $row = mysql_fetch_row($result); |
---|
61 | if(is_string($row[0])) |
---|
62 | { |
---|
63 | $this->my_config = unserialize(($row[0])); |
---|
64 | } |
---|
65 | } |
---|
66 | $this->load_default_config(); |
---|
67 | } |
---|
68 | |
---|
69 | // Initialize default values of params |
---|
70 | private function load_default_config() |
---|
71 | { |
---|
72 | include CONCOURS_INC_PATH.'default_values.inc.php'; |
---|
73 | foreach ($concours_default_values as $key => $value) |
---|
74 | { |
---|
75 | if (!isset($this->my_config[$key])) $this->my_config[$key] = $value; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | // Save general configuration to config_database |
---|
80 | function save_config() |
---|
81 | { |
---|
82 | $query = ' |
---|
83 | REPLACE INTO '.CONFIG_TABLE.' |
---|
84 | VALUES( |
---|
85 | \'concoursphoto\', |
---|
86 | \''.serialize($this->my_config).'\', |
---|
87 | \'Configuration Concours Photo\') |
---|
88 | ;'; |
---|
89 | |
---|
90 | $result = pwg_query($query); |
---|
91 | |
---|
92 | if($result) |
---|
93 | return true; |
---|
94 | else |
---|
95 | return false; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | // Retrieve user groups |
---|
100 | function get_user_groups() |
---|
101 | { |
---|
102 | global $user; |
---|
103 | if ($this->debug) |
---|
104 | foreach ($user as $id=>$val) |
---|
105 | { |
---|
106 | if (is_array($val)) |
---|
107 | foreach ($val as $id2=>$val2) |
---|
108 | echo "user[".$id."][".$id2."]=".$val2."\n"; |
---|
109 | else |
---|
110 | echo "user[".$id."]=".$val."\n"; |
---|
111 | } |
---|
112 | |
---|
113 | $query = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id = ' . $user['id'] . ';'; |
---|
114 | if ($this->debug) echo $query."\n"; |
---|
115 | $result = pwg_query($query); |
---|
116 | while ($row = mysql_fetch_assoc($result)) |
---|
117 | { |
---|
118 | array_push($this->user_groups, $row['group_id']); |
---|
119 | } |
---|
120 | |
---|
121 | if ($this->debug) |
---|
122 | foreach ($this->user_groups as $gr) |
---|
123 | echo "Group=".$gr."\n"; |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | // create a concours and store it to db. |
---|
128 | // return id for concours. |
---|
129 | function create_concours() |
---|
130 | {} |
---|
131 | |
---|
132 | // Get informations array for a concours id |
---|
133 | function get_concours($concours_id = null) |
---|
134 | { |
---|
135 | if ($concours_id!== null or $this->concours_id !== null) |
---|
136 | { |
---|
137 | $query = ' |
---|
138 | SELECT * |
---|
139 | FROM ' . CONCOURS_TABLE .' |
---|
140 | WHERE id =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
141 | LIMIT 1 |
---|
142 | '; |
---|
143 | |
---|
144 | $result = pwg_query($query); |
---|
145 | if ($result) |
---|
146 | $this->concours_infos = mysql_fetch_assoc($result); |
---|
147 | } |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | // save informations on database for a concours_id |
---|
152 | function save_concours($concours_id = null) |
---|
153 | { |
---|
154 | if ($concours_id!== null or $this->concours_id !== null) |
---|
155 | { |
---|
156 | |
---|
157 | $query = "INSERT INTO " . CONCOURS_TABLE . " |
---|
158 | ( `id`, |
---|
159 | `create_date`, |
---|
160 | `name`, |
---|
161 | `descr`, |
---|
162 | `begin_date`, |
---|
163 | `end_date`, |
---|
164 | `category`, |
---|
165 | `groups`, |
---|
166 | `method`, |
---|
167 | `guest` |
---|
168 | |
---|
169 | ) |
---|
170 | VALUES (".($concours_id !== NULL ? $concours_id : $this->concours_id).", now(), |
---|
171 | \"".$this->concours_infos['name']."\", \"".$this->concours_infos['descr']."\", |
---|
172 | \"".$this->concours_infos['begin_date']."\", \"".$this->concours_infos['end_date']."\", |
---|
173 | ".$this->concours_infos['category'].", ".$this->concours_infos['groups'].", |
---|
174 | ".$this->concours_infos['method'].", ".($this->concours_infos['guest'] ? "TRUE" : "FALSE")." |
---|
175 | );"; |
---|
176 | if (pwg_query($query) != null) |
---|
177 | return true; |
---|
178 | else |
---|
179 | return false; |
---|
180 | } |
---|
181 | else |
---|
182 | return false; |
---|
183 | } |
---|
184 | |
---|
185 | // update informations on database for a concours_id |
---|
186 | function update_concours($concours_id = null) |
---|
187 | { |
---|
188 | if ($concours_id!== null or $this->concours_id !== null) |
---|
189 | { |
---|
190 | |
---|
191 | $query = "UPDATE " . CONCOURS_TABLE . " |
---|
192 | SET |
---|
193 | create_date = now(), |
---|
194 | name = \"".$this->concours_infos['name']."\", |
---|
195 | descr = \"".$this->concours_infos['descr']."\", |
---|
196 | begin_date = \"".$this->concours_infos['begin_date']."\", |
---|
197 | end_date = \"".$this->concours_infos['end_date']."\", |
---|
198 | category = ".$this->concours_infos['category'].", |
---|
199 | groups = ".$this->concours_infos['groups'].", |
---|
200 | method = ".$this->concours_infos['method'].", |
---|
201 | guest = ".($this->concours_infos['guest'] ? "TRUE" : "FALSE")." |
---|
202 | |
---|
203 | WHERE id = ".($concours_id !== NULL ? $concours_id : $this->concours_id)." |
---|
204 | ;"; |
---|
205 | pwg_query($query); |
---|
206 | if (pwg_query($query) != null) |
---|
207 | return true; |
---|
208 | else |
---|
209 | return false; |
---|
210 | } |
---|
211 | else |
---|
212 | return false; |
---|
213 | |
---|
214 | } |
---|
215 | |
---|
216 | // update field on database for a concours_id |
---|
217 | function update_concoursfield($field_id, $concours_id = null) |
---|
218 | { |
---|
219 | if ($concours_id!== null or $this->concours_id !== null) |
---|
220 | { |
---|
221 | |
---|
222 | $query = "UPDATE " . CONCOURS_TABLE . " |
---|
223 | SET |
---|
224 | " . $field_id." = ". $this->concours_infos[$field_id] . " |
---|
225 | |
---|
226 | WHERE id = ".($concours_id !== NULL ? $concours_id : $this->concours_id)." |
---|
227 | ;"; |
---|
228 | pwg_query($query); |
---|
229 | if (pwg_query($query) != null) |
---|
230 | return true; |
---|
231 | else |
---|
232 | return false; |
---|
233 | } |
---|
234 | else |
---|
235 | return false; |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | // delete concours from db (and all sub informations such as details, vote and result |
---|
240 | function delete_concours($concours_id = null) |
---|
241 | { |
---|
242 | if ($concours_id!== null or $this->concours_id !== null) |
---|
243 | { |
---|
244 | |
---|
245 | $query = ' |
---|
246 | DELETE |
---|
247 | FROM ' . CONCOURS_TABLE .' |
---|
248 | WHERE id =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
249 | '; |
---|
250 | pwg_query($query); |
---|
251 | $query = ' |
---|
252 | DELETE |
---|
253 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
254 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
255 | '; |
---|
256 | pwg_query($query); |
---|
257 | $query = ' |
---|
258 | DELETE |
---|
259 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
260 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
261 | '; |
---|
262 | pwg_query($query); |
---|
263 | $query = ' |
---|
264 | DELETE |
---|
265 | FROM ' . CONCOURS_RESULT_TABLE .' |
---|
266 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
267 | '; |
---|
268 | pwg_query($query); |
---|
269 | } |
---|
270 | else |
---|
271 | return false; |
---|
272 | |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | // today's date is between open and close date of concours? |
---|
277 | function is_active($concours_id = NULL) |
---|
278 | { |
---|
279 | if ($concours_id!== null or $this->concours_id !== null) |
---|
280 | { |
---|
281 | $query = ' |
---|
282 | SELECT id |
---|
283 | FROM ' . CONCOURS_TABLE .' |
---|
284 | WHERE id =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
285 | AND time_to_sec(TIMEDIFF(begin_date,now())) < 0 |
---|
286 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
287 | '; |
---|
288 | |
---|
289 | $result = pwg_query($query); |
---|
290 | if ($this->debug) echo $query."\n"; |
---|
291 | if ($result != NULL) |
---|
292 | return true; |
---|
293 | else |
---|
294 | return false; |
---|
295 | } |
---|
296 | else |
---|
297 | return NULL; |
---|
298 | } |
---|
299 | |
---|
300 | // today's date is not between open and close date of concours? |
---|
301 | function is_closed($concours_id = NULL) |
---|
302 | { |
---|
303 | if ($concours_id!== null or $this->concours_id !== null) |
---|
304 | { |
---|
305 | $query = ' |
---|
306 | SELECT id |
---|
307 | FROM ' . CONCOURS_TABLE .' |
---|
308 | WHERE id =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
309 | AND time_to_sec(TIMEDIFF(now(), end_date)) > 0 |
---|
310 | '; |
---|
311 | |
---|
312 | $result = pwg_query($query); |
---|
313 | if ($this->debug) echo $query."\n"; |
---|
314 | if ($result != NULL) |
---|
315 | return true; |
---|
316 | else |
---|
317 | return false; |
---|
318 | } |
---|
319 | else |
---|
320 | return NULL; |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | // Get criterias that are stored on db for default mode (concours_id = 0) |
---|
325 | function get_default_criterias() |
---|
326 | { |
---|
327 | $criterias = $this->get_criterias_list(0); |
---|
328 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id |
---|
329 | FROM ' . CONCOURS_DETAIL_TABLE . ' ;'; |
---|
330 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
331 | |
---|
332 | foreach ($criterias as $criteria) |
---|
333 | { |
---|
334 | if ($this->debug) echo "NEXT =".$next_element_id."\n"; |
---|
335 | $query = 'INSERT INTO '.CONCOURS_DETAIL_TABLE.' |
---|
336 | (id, id_concours, criteria_id, name, descr, min_value, max_value, ponderation, uppercriteria)' |
---|
337 | .'VALUES ('.$next_element_id.', '.$this->concours_id.', ' |
---|
338 | .$criteria['criteria_id'].', "'.$criteria['name'].'", "' |
---|
339 | .$criteria['descr'].'", '.$criteria['min_value'].', ' |
---|
340 | .$criteria['max_value'].', '.$criteria['ponderation'].', ' |
---|
341 | .($criteria['uppercriteria'] ? $criteria['uppercriteria'] : 'NULL').') |
---|
342 | ;'; |
---|
343 | |
---|
344 | $result = pwg_query($query); |
---|
345 | if ($this->debug) echo $query; |
---|
346 | $next_element_id = $next_element_id +1; |
---|
347 | } |
---|
348 | } |
---|
349 | |
---|
350 | // Get criterias from a concours |
---|
351 | function get_criterias_list($concours_id = NULL) |
---|
352 | { |
---|
353 | $criteria_list = array(); |
---|
354 | if ($concours_id!== null or $this->concours_id !== null) |
---|
355 | { |
---|
356 | $query = ' |
---|
357 | SELECT * |
---|
358 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
359 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
360 | ORDER BY criteria_id |
---|
361 | '; |
---|
362 | $result = pwg_query($query); |
---|
363 | if ($this->debug) echo $query."\n"; |
---|
364 | while ($row = mysql_fetch_assoc($result)) |
---|
365 | { |
---|
366 | array_push($criteria_list, $row); |
---|
367 | } |
---|
368 | return $criteria_list; |
---|
369 | } |
---|
370 | else |
---|
371 | return $criteria_list; |
---|
372 | } |
---|
373 | |
---|
374 | // Get list of the fist level criterias |
---|
375 | function get_firstlevel_criterias($concours_id = NULL) |
---|
376 | { |
---|
377 | $criteria_list = array(); |
---|
378 | if ($concours_id!== null or $this->concours_id !== null) |
---|
379 | { |
---|
380 | $query = ' |
---|
381 | SELECT * |
---|
382 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
383 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
384 | AND uppercriteria IS NULL |
---|
385 | ORDER BY criteria_id |
---|
386 | '; |
---|
387 | $result = pwg_query($query); |
---|
388 | if ($this->debug) echo $query."\n"; |
---|
389 | while ($row = mysql_fetch_assoc($result)) |
---|
390 | { |
---|
391 | if ($this->debug) echo "criteria_id=".$row['criteria_id']."\n"; |
---|
392 | array_push($criteria_list, $row); |
---|
393 | } |
---|
394 | return $criteria_list; |
---|
395 | } |
---|
396 | else |
---|
397 | return $criteria_list; |
---|
398 | } |
---|
399 | |
---|
400 | // check if a criteria contains subcriterias |
---|
401 | function is_criterias_contains_sub($criteria_id, $concours_id = NULL) |
---|
402 | { |
---|
403 | if ($concours_id!== null or $this->concours_id !== null) |
---|
404 | { |
---|
405 | $query = ' |
---|
406 | SELECT criteria_id |
---|
407 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
408 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
409 | AND uppercriteria = '.$criteria_id.' |
---|
410 | '; |
---|
411 | $result = pwg_query($query); |
---|
412 | if ($this->debug) echo $query."\n"; |
---|
413 | // mini 1 line |
---|
414 | if(mysql_fetch_assoc($result)) |
---|
415 | return true; |
---|
416 | else |
---|
417 | return false; |
---|
418 | } |
---|
419 | return NULL; |
---|
420 | } |
---|
421 | |
---|
422 | |
---|
423 | // Get list of subcriterias from a criteria_id |
---|
424 | function get_subcriterias($criteria_id, $concours_id = NULL) |
---|
425 | { |
---|
426 | $criteria_list = array(); |
---|
427 | if ($concours_id!== null or $this->concours_id !== null) |
---|
428 | { |
---|
429 | $query = ' |
---|
430 | SELECT * |
---|
431 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
432 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
433 | AND uppercriteria = '.$criteria_id.' |
---|
434 | ORDER BY criteria_id |
---|
435 | '; |
---|
436 | $result = pwg_query($query); |
---|
437 | if ($this->debug) echo $query."\n"; |
---|
438 | while ($row = mysql_fetch_assoc($result)) |
---|
439 | { |
---|
440 | array_push($criteria_list, $row); |
---|
441 | } |
---|
442 | return $criteria_list; |
---|
443 | } |
---|
444 | else |
---|
445 | return $criteria_list; |
---|
446 | } |
---|
447 | |
---|
448 | // Get a detail from a criteria |
---|
449 | function get_criteria($criteria_id, $concours_id = NULL) |
---|
450 | { |
---|
451 | $criteria = array(); |
---|
452 | |
---|
453 | if ($concours_id!== null or $this->concours_id !== null) |
---|
454 | { |
---|
455 | $query = ' |
---|
456 | SELECT * |
---|
457 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
458 | WHERE id_concours = '. ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
459 | AND criteria_id =' . $criteria_id . ' |
---|
460 | '; |
---|
461 | $result = pwg_query($query); |
---|
462 | if ($this->debug) echo $query."\n"; |
---|
463 | $criteria = mysql_fetch_assoc($result); |
---|
464 | /* while ($row = mysql_fetch_assoc($result)) |
---|
465 | { |
---|
466 | array_push($criteria, $row); |
---|
467 | } |
---|
468 | */ |
---|
469 | } |
---|
470 | return $criteria; |
---|
471 | |
---|
472 | } |
---|
473 | |
---|
474 | // Get a detail from a criteria |
---|
475 | function get_criteria_by_id($id) |
---|
476 | { |
---|
477 | $criteria = array(); |
---|
478 | |
---|
479 | $query = ' |
---|
480 | SELECT * |
---|
481 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
482 | WHERE id =' . $id . ' |
---|
483 | '; |
---|
484 | $result = pwg_query($query); |
---|
485 | if ($this->debug) echo $query."\n"; |
---|
486 | $criteria = mysql_fetch_assoc($result); |
---|
487 | /* while ($row = mysql_fetch_assoc($result)) |
---|
488 | { |
---|
489 | array_push($criteria, $row); |
---|
490 | } |
---|
491 | */ |
---|
492 | return $criteria; |
---|
493 | |
---|
494 | } |
---|
495 | |
---|
496 | // Add a criteria to a concours |
---|
497 | // Datas is an array and contains all infos. |
---|
498 | // Return the criteria id |
---|
499 | function add_criteria($datas, $concours_id = NULL) |
---|
500 | { |
---|
501 | // determines the criteria_id |
---|
502 | if ($concours_id!== null or $this->concours_id !== null) |
---|
503 | { |
---|
504 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id |
---|
505 | FROM ' . CONCOURS_DETAIL_TABLE . ' ;'; |
---|
506 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
507 | $query = 'SELECT IF(MAX(criteria_id)+1 IS NULL, 1, MAX(criteria_id)+1) AS next_criteria_id |
---|
508 | FROM ' . CONCOURS_DETAIL_TABLE . ' |
---|
509 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) . ' ;'; |
---|
510 | list($next_criteria_id) = mysql_fetch_array(pwg_query($query)); |
---|
511 | |
---|
512 | $query = ' |
---|
513 | INSERT INTO ' . CONCOURS_DETAIL_TABLE .' |
---|
514 | (`id`, |
---|
515 | `id_concours`, |
---|
516 | `criteria_id`, |
---|
517 | `name`, |
---|
518 | `descr`, |
---|
519 | `min_value`, |
---|
520 | `max_value`, |
---|
521 | `ponderation`, |
---|
522 | `uppercriteria`) |
---|
523 | VALUES ( |
---|
524 | '.$next_element_id.', '.($concours_id !== null ? $concours_id : $this->concours_id ).', |
---|
525 | '.$next_criteria_id.', "'.$datas['name'].'", "'.$datas['descr'].'", '.$datas['min_value'].' |
---|
526 | , '.$datas['max_value'].', '.$datas['ponderation'].' |
---|
527 | , '.($datas['uppercriteria'] ? $datas['uppercriteria'] : 'NULL').' |
---|
528 | );'; |
---|
529 | |
---|
530 | pwg_query($query); |
---|
531 | if ($this->debug) echo $query."\n"; |
---|
532 | |
---|
533 | } |
---|
534 | } |
---|
535 | |
---|
536 | // Update a criteria to a concours |
---|
537 | // Datas is an array and contains all infos. |
---|
538 | function update_criteria($datas, $concours_id = NULL) |
---|
539 | { |
---|
540 | // determines the criteria_id |
---|
541 | if ($concours_id!== null or $this->concours_id !== null) |
---|
542 | { |
---|
543 | $query = ' |
---|
544 | UPDATE ' . CONCOURS_DETAIL_TABLE .' |
---|
545 | SET |
---|
546 | name = "'.$datas['name'].'", |
---|
547 | descr = "'.$datas['descr'].'", |
---|
548 | min_value = '.$datas['min_value'].', |
---|
549 | max_value = '.$datas['max_value'].', |
---|
550 | '.($datas['uppercriteria'] == false ? '' : 'uppercriteria = '.($datas['uppercriteria'] ? $datas['uppercriteria'].',' : 'NULL ,')).' |
---|
551 | ponderation = '.$datas['ponderation'].' |
---|
552 | WHERE id = '.$datas['id'].';'; |
---|
553 | |
---|
554 | pwg_query($query); |
---|
555 | |
---|
556 | if ($this->debug) echo $query."\n"; |
---|
557 | |
---|
558 | } |
---|
559 | } |
---|
560 | |
---|
561 | |
---|
562 | // Delete a criteria from a concours |
---|
563 | function delete_criteria($criteria_id, $concours_id = NULL) |
---|
564 | { |
---|
565 | if ($concours_id!== null or $this->concours_id !== null) |
---|
566 | { |
---|
567 | |
---|
568 | $query = ' |
---|
569 | DELETE |
---|
570 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
571 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
572 | AND criteria_id = '.$criteria_id.' |
---|
573 | '; |
---|
574 | $result = pwg_query($query); |
---|
575 | if ($this->debug) echo $query."\n"; |
---|
576 | if($result) |
---|
577 | return true; |
---|
578 | else |
---|
579 | return false; |
---|
580 | } |
---|
581 | else |
---|
582 | return null; |
---|
583 | |
---|
584 | } |
---|
585 | |
---|
586 | // Delete a criteria (with id) from a concours |
---|
587 | function delete_criteria_by_id($id) |
---|
588 | { |
---|
589 | |
---|
590 | $query = ' |
---|
591 | DELETE |
---|
592 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
593 | WHERE id =' . $id . ' |
---|
594 | '; |
---|
595 | $result = pwg_query($query); |
---|
596 | if ($this->debug) echo $query."\n"; |
---|
597 | |
---|
598 | } |
---|
599 | |
---|
600 | // check if a result is already present in the database |
---|
601 | function is_result_present($concours_id = null) |
---|
602 | { |
---|
603 | // recover all img_id from the category |
---|
604 | $query = 'SELECT DISTINCT(id_concours)' |
---|
605 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
606 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).';'; |
---|
607 | |
---|
608 | $result = pwg_query($query); |
---|
609 | // For each images |
---|
610 | if (mysql_fetch_assoc($result)) |
---|
611 | return true; |
---|
612 | else |
---|
613 | return false; |
---|
614 | |
---|
615 | } |
---|
616 | |
---|
617 | // function to return the number of votes for a concours by images |
---|
618 | function nb_votes_by_img($concours_id = NULL) |
---|
619 | { |
---|
620 | |
---|
621 | // nb of users who vote for each image (for a selected concours |
---|
622 | $query = 'SELECT img_id, COUNT(DISTINCT user_id) AS NBVOTE FROM ' . CONCOURS_DATA_TABLE |
---|
623 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
624 | .' AND ipguest IS NULL' |
---|
625 | .' GROUP BY img_id ' |
---|
626 | .';'; |
---|
627 | |
---|
628 | $result = pwg_query($query); |
---|
629 | $nbvotes = array(); |
---|
630 | while ($row = mysql_fetch_assoc($result)) |
---|
631 | { |
---|
632 | $nbvotes[$row['img_id']] = $row['NBVOTE']; |
---|
633 | } |
---|
634 | |
---|
635 | // Add guest infos |
---|
636 | $query = 'SELECT img_id, COUNT(DISTINCT user_id, ipguest) AS NBVOTE FROM ' . CONCOURS_DATA_TABLE |
---|
637 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
638 | .' AND ipguest IS NOT NULL' |
---|
639 | .' GROUP BY img_id ' |
---|
640 | .';'; |
---|
641 | |
---|
642 | $result = pwg_query($query); |
---|
643 | if ($this->debug) echo "NBVOTE = ".$query."\n"; |
---|
644 | while ($row = mysql_fetch_assoc($result)) |
---|
645 | if (!isset($nbvotes[$row['img_id']])) |
---|
646 | $nbvotes[$row['img_id']] = $row['NBVOTE']; |
---|
647 | else |
---|
648 | $nbvotes[$row['img_id']] += $row['NBVOTE']; |
---|
649 | |
---|
650 | return $nbvotes; |
---|
651 | } |
---|
652 | |
---|
653 | // Get All iaddr for guest who have vote (IP Stores in db) in a concours |
---|
654 | function get_guest_list($concours_id = NULL) |
---|
655 | { |
---|
656 | $ipguest = array(); |
---|
657 | if ($concours_id!== null or $this->concours_id !== null) |
---|
658 | { |
---|
659 | $query = ' |
---|
660 | SELECT distinct(ipguest) |
---|
661 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
662 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
663 | AND ipguest IS NOT NULL |
---|
664 | ORDER BY ipguest |
---|
665 | '; |
---|
666 | $result = pwg_query($query); |
---|
667 | if ($this->debug) echo $query."\n"; |
---|
668 | while ($row = mysql_fetch_assoc($result)) |
---|
669 | { |
---|
670 | array_push($ipguest, $row['ipguest']); |
---|
671 | } |
---|
672 | if ($this->debug) |
---|
673 | { |
---|
674 | foreach ($ipguest as $ip) |
---|
675 | echo "IP=".$ip."\n"; |
---|
676 | } |
---|
677 | |
---|
678 | return $ipguest; |
---|
679 | } |
---|
680 | else |
---|
681 | return $ipguest; |
---|
682 | } |
---|
683 | |
---|
684 | |
---|
685 | // After concours is completed (closed date is passed), generate the result and store it to DB |
---|
686 | function create_result($concours_id = NULL) |
---|
687 | { |
---|
688 | global $conf; |
---|
689 | // var which contains id=img_id and val =global note |
---|
690 | $global_note = array(); |
---|
691 | if ($concours_id!== null or $this->concours_id !== null) |
---|
692 | { |
---|
693 | // only for closed concours and not already result generated |
---|
694 | if ($this->is_closed($concours_id) AND ! $this->is_result_present($concours_id)) |
---|
695 | { |
---|
696 | |
---|
697 | // array with the number of vote by image for the concours. Needed for the moyenne |
---|
698 | $nbvotes = $this->nb_votes_by_img($concours_id); |
---|
699 | |
---|
700 | $user_id = array(); |
---|
701 | // vars not initialized |
---|
702 | if ($this->concours_infos == array()) |
---|
703 | $this->get_concours(); |
---|
704 | $category = $this->concours_infos['category']; |
---|
705 | |
---|
706 | if ($this->debug) |
---|
707 | echo "CAT=".$category; |
---|
708 | // Get all user_id from a concours |
---|
709 | $query = 'SELECT DISTINCT user_id' |
---|
710 | .' FROM ' .CONCOURS_DATA_TABLE |
---|
711 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
712 | .' AND user_id <> '.$conf['guest_id'] |
---|
713 | .';'; |
---|
714 | $result = pwg_query($query); |
---|
715 | if ($this->debug) |
---|
716 | echo $query; |
---|
717 | while ($row = mysql_fetch_assoc($result)) |
---|
718 | array_push($user_id, $row['user_id']); |
---|
719 | |
---|
720 | // Add guest info is present |
---|
721 | $ipguest = array(); |
---|
722 | if ($concours_id!== null) |
---|
723 | $ipguest = $this->get_guest_list(($concours_id !== null ? $concours_id : $this->concours_id )); |
---|
724 | |
---|
725 | if ($this->debug) |
---|
726 | { |
---|
727 | foreach ($user_id as $userid) |
---|
728 | echo "USER = ".$userid; |
---|
729 | foreach ($ipguest as $userid) |
---|
730 | echo "IPGUEST = ".$userid; |
---|
731 | } |
---|
732 | |
---|
733 | // recover all img_id from the category |
---|
734 | $query = 'SELECT DISTINCT(image_id)' |
---|
735 | .' FROM ' .IMAGE_CATEGORY_TABLE |
---|
736 | .' WHERE category_id = '.$category.';'; |
---|
737 | |
---|
738 | $result = pwg_query($query); |
---|
739 | // For each images |
---|
740 | while ($row = mysql_fetch_assoc($result)) |
---|
741 | { |
---|
742 | if ($this->debug) echo "IMG_ID=".$row['image_id']."\n"; |
---|
743 | foreach ($user_id as $i => $userid) |
---|
744 | { |
---|
745 | if (!isset($global_note[$row['image_id']])) |
---|
746 | $global_note[$row['image_id']] = 0; |
---|
747 | $global_note[$row['image_id']] += $this->get_img_globalnote_user($row['image_id'], null, $userid); |
---|
748 | } |
---|
749 | // Add guest scores if present |
---|
750 | foreach ($ipguest as $i => $ipguestt) |
---|
751 | { |
---|
752 | if (!isset($global_note[$row['image_id']])) |
---|
753 | $global_note[$row['image_id']] = 0; |
---|
754 | $global_note[$row['image_id']] += $this->get_img_globalnote_user($row['image_id'], null, null, $ipguestt); |
---|
755 | } |
---|
756 | } |
---|
757 | if ($this->debug) |
---|
758 | { |
---|
759 | foreach ($global_note as $id => $val) |
---|
760 | echo "Note img ( ".$id.") = ".$val."\n"; |
---|
761 | } |
---|
762 | |
---|
763 | // update database and store result into RESULT table |
---|
764 | // note = global note |
---|
765 | $IMGmoyenne = 0; |
---|
766 | foreach ($global_note as $id => $val) |
---|
767 | // id contains the image id |
---|
768 | { |
---|
769 | if ($this->debug) echo $nbvotes[$id]; |
---|
770 | $IMGmoyenne = (isset($nbvotes[$id]) && $nbvotes[$id] != 0 ? $val/$nbvotes[$id] : 0); |
---|
771 | |
---|
772 | $query = 'INSERT INTO ' . CONCOURS_RESULT_TABLE .'(`id_concours`, `img_id`, `date` , `note`, `moyenne`, `nbvotant` ) |
---|
773 | VALUES ('.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
774 | .', '.$id |
---|
775 | .', now() ' |
---|
776 | .', '.$val |
---|
777 | .', '.$IMGmoyenne |
---|
778 | .', '.(isset($nbvotes[$id]) ? $nbvotes[$id] : 0) |
---|
779 | .');'; |
---|
780 | $result = pwg_query($query); |
---|
781 | |
---|
782 | |
---|
783 | } |
---|
784 | |
---|
785 | |
---|
786 | |
---|
787 | |
---|
788 | } |
---|
789 | else |
---|
790 | return false; |
---|
791 | } |
---|
792 | else |
---|
793 | return false; |
---|
794 | } |
---|
795 | |
---|
796 | |
---|
797 | // Check if an image's author is the same as the actual user |
---|
798 | function check_img_user($img_id) |
---|
799 | { |
---|
800 | global $user; |
---|
801 | $query = ' |
---|
802 | SELECT author |
---|
803 | FROM ' . IMAGES_TABLE .' |
---|
804 | WHERE id =' . $img_id . ' |
---|
805 | '; |
---|
806 | |
---|
807 | $result = pwg_query($query); |
---|
808 | if ($this->debug) echo $query."\n"; |
---|
809 | if ($result) |
---|
810 | { |
---|
811 | $row = mysql_fetch_assoc($result); |
---|
812 | $authorid = get_userid($row['author']); |
---|
813 | if ($authorid and ($authorid == $user['id'])) |
---|
814 | return true; |
---|
815 | else |
---|
816 | return false; |
---|
817 | } |
---|
818 | return false; |
---|
819 | |
---|
820 | } |
---|
821 | |
---|
822 | |
---|
823 | // Add tpl to picture.php page to display concours informations and vote |
---|
824 | function display_concours_to_picture() |
---|
825 | { |
---|
826 | // Step0 : if user = guest, check the concours param and store ip address to user name. |
---|
827 | // Step1 : concours is defined to this category AND concours is open AND user is authorized to access to this category (thru group) |
---|
828 | // Step1 bis : dont show concours if img_author = user_id |
---|
829 | // Step 2 : Recover stored informations in db for this user |
---|
830 | // Step 3 : Complete tpl information |
---|
831 | // Step 4 : concat tpl |
---|
832 | |
---|
833 | global $page, $user, $template, $conf; |
---|
834 | |
---|
835 | // Get user group. |
---|
836 | $this->get_user_groups(); |
---|
837 | |
---|
838 | $concours = array(); |
---|
839 | |
---|
840 | // STEP 0 |
---|
841 | if ($this->debug) echo "STEP0\n"; |
---|
842 | |
---|
843 | $user['ipguest'] = null; |
---|
844 | |
---|
845 | echo "user id=".$user['id']; |
---|
846 | // Check if the user is a guest only |
---|
847 | if (is_a_guest()) |
---|
848 | { |
---|
849 | if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) |
---|
850 | $IP = $_SERVER['HTTP_X_FORWARDED_FOR']; |
---|
851 | elseif(isset($_SERVER['HTTP_CLIENT_IP'])) |
---|
852 | $IP = $_SERVER['HTTP_CLIENT_IP']; |
---|
853 | else |
---|
854 | $IP = $_SERVER['REMOTE_ADDR']; |
---|
855 | |
---|
856 | // store ip |
---|
857 | $user['ipguest'] = $IP; |
---|
858 | if ($this->debug) |
---|
859 | echo "user ip guest=".$user['ipguest']; |
---|
860 | |
---|
861 | } |
---|
862 | |
---|
863 | |
---|
864 | |
---|
865 | // DEBUG |
---|
866 | if ($this->debug) |
---|
867 | { |
---|
868 | foreach ($user as $id=>$val) |
---|
869 | { |
---|
870 | if (is_array($val)) |
---|
871 | foreach ($val as $id2=>$val2) |
---|
872 | echo "user[".$id."][".$id2."]=".$val2."\n"; |
---|
873 | else |
---|
874 | echo "user[".$id."]=".$val."\n"; |
---|
875 | } |
---|
876 | } |
---|
877 | |
---|
878 | // END DEBUG |
---|
879 | |
---|
880 | |
---|
881 | // DEBUG |
---|
882 | if ($this->debug) |
---|
883 | foreach ($page as $id=>$val) |
---|
884 | { |
---|
885 | if (is_array($val)) |
---|
886 | foreach ($val as $id2=>$val2) |
---|
887 | echo "page[".$id."][".$id2."]=".$val2."\n"; |
---|
888 | else |
---|
889 | echo "page[".$id."]=".$val."\n"; |
---|
890 | } |
---|
891 | // END DEBUG |
---|
892 | |
---|
893 | // disable author name on image which are present in a concours. |
---|
894 | // check the categories where the current image is present and disable the author name |
---|
895 | if (isset($this->my_config['mask_author']) && $this->my_config['mask_author'] == true) |
---|
896 | { |
---|
897 | // Get all categories where the current image is present |
---|
898 | $query = ' |
---|
899 | SELECT category_id,uppercats,commentable,global_rank |
---|
900 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
901 | INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id |
---|
902 | WHERE image_id = '.$page['image_id'].' |
---|
903 | ;'; |
---|
904 | // echo $query; |
---|
905 | $result = pwg_query($query); |
---|
906 | $related_categories = array(); |
---|
907 | while ($row = mysql_fetch_array($result)) |
---|
908 | { |
---|
909 | array_push($related_categories, $row['category_id']); |
---|
910 | } |
---|
911 | // |
---|
912 | //echo related_categories; |
---|
913 | if (count($related_categories)) |
---|
914 | { |
---|
915 | // Request for all concours prepared & actived on each categories |
---|
916 | $query = ' |
---|
917 | SELECT * |
---|
918 | FROM ' . CONCOURS_TABLE .' |
---|
919 | WHERE category IN ('.implode(',', $related_categories).') |
---|
920 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
921 | '; |
---|
922 | // echo $query; |
---|
923 | $result = pwg_query($query); |
---|
924 | // If one or more concours are found, the author name is masked |
---|
925 | if ($result && mysql_fetch_assoc($result)) |
---|
926 | { |
---|
927 | $template->assign('INFO_AUTHOR',l10n('concours_img_author')); |
---|
928 | } |
---|
929 | } |
---|
930 | } |
---|
931 | |
---|
932 | // End disable author name |
---|
933 | |
---|
934 | |
---|
935 | if (($page['section']) == 'categories' AND !empty($page['category'])) |
---|
936 | { |
---|
937 | if ($this->debug) echo "STEP1\n"; |
---|
938 | //------------ |
---|
939 | // Step 1 |
---|
940 | |
---|
941 | /* |
---|
942 | if ($this->my_config['mask_author'] && $this->my_config['mask_author'] == true) |
---|
943 | { |
---|
944 | // Request for all concours prepared & actived |
---|
945 | $query = ' |
---|
946 | SELECT * |
---|
947 | FROM ' . CONCOURS_TABLE .' |
---|
948 | WHERE category =' . $page['category']['id'] . ' |
---|
949 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
950 | '; |
---|
951 | // echo $query; |
---|
952 | $result = pwg_query($query); |
---|
953 | if ($result && mysql_fetch_assoc($result)) |
---|
954 | $template->assign('INFO_AUTHOR',l10n('concours_img_author')); |
---|
955 | } |
---|
956 | */ |
---|
957 | |
---|
958 | $query = ' |
---|
959 | SELECT * |
---|
960 | FROM ' . CONCOURS_TABLE .' |
---|
961 | WHERE category =' . $page['category']['id'] . ' |
---|
962 | AND time_to_sec(TIMEDIFF(begin_date,now())) < 0 |
---|
963 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
964 | '; |
---|
965 | |
---|
966 | $result = pwg_query($query); |
---|
967 | if ($this->debug) echo $query."\n"; |
---|
968 | while ($row = mysql_fetch_assoc($result)) |
---|
969 | { |
---|
970 | if (!empty($row['groups'])) |
---|
971 | { |
---|
972 | if ($this->debug) echo "GROUPS=".$row['groups']; |
---|
973 | $authorized_groups = explode(',', $row['groups']); |
---|
974 | if (array_intersect($this->user_groups, $authorized_groups) == array()) |
---|
975 | { |
---|
976 | if ($this->debug) echo "ERROR GROUPS"; |
---|
977 | continue; |
---|
978 | } |
---|
979 | // If no group is parameter for that concours ==> available for all registered users |
---|
980 | $concours = $row; |
---|
981 | } |
---|
982 | else |
---|
983 | $concours = $row; |
---|
984 | |
---|
985 | |
---|
986 | } |
---|
987 | |
---|
988 | if ($this->debug) echo "STEP1 bis\n"; |
---|
989 | //------------ |
---|
990 | // Step 1 bis |
---|
991 | // Actual user is the author of the picture ==>end |
---|
992 | if ( !$this->my_config['author_vote_available'] |
---|
993 | AND $this->check_img_user($page['current_item'])) |
---|
994 | return; |
---|
995 | |
---|
996 | |
---|
997 | if ($this->debug) echo "STEP2\n"; |
---|
998 | //------------ |
---|
999 | // Step 2 |
---|
1000 | if ($concours != array()) |
---|
1001 | { |
---|
1002 | // If user validate the notation |
---|
1003 | if (isset($_POST['concours_submit'])) |
---|
1004 | { |
---|
1005 | |
---|
1006 | // array_push($page['infos'] , l10n('concours_vote_saved')); |
---|
1007 | |
---|
1008 | $user_note = ""; |
---|
1009 | if ($this->debug) echo "SUBMIT"; |
---|
1010 | // concat all the notes to save on db |
---|
1011 | |
---|
1012 | $firstcriterias = $this->get_firstlevel_criterias($concours['id']); |
---|
1013 | foreach ($firstcriterias as $criteria) |
---|
1014 | { |
---|
1015 | |
---|
1016 | // First without sub criterias |
---|
1017 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] )) |
---|
1018 | { |
---|
1019 | // Check value |
---|
1020 | $value = str_replace(",",".",$_POST[$criteria['criteria_id']]); |
---|
1021 | $value = str_replace(" ","",$value); |
---|
1022 | $value = floatval($value); |
---|
1023 | if ($value < floatval($criteria['min_value'])) |
---|
1024 | $value = floatval($criteria['min_value']); |
---|
1025 | if ($value > floatval($criteria['max_value'])) |
---|
1026 | $value = floatval($criteria['max_value']); |
---|
1027 | |
---|
1028 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$criteria['criteria_id']."=".$value; |
---|
1029 | } |
---|
1030 | else |
---|
1031 | { |
---|
1032 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours['id'] ); |
---|
1033 | foreach ($secondcriterias as $subcriteria) |
---|
1034 | { |
---|
1035 | // Check value |
---|
1036 | $value = str_replace(",",".",$_POST[$subcriteria['criteria_id']]); |
---|
1037 | $value = str_replace(" ","",$value); |
---|
1038 | $value = floatval($value); |
---|
1039 | if ($value < floatval($subcriteria['min_value'])) |
---|
1040 | $value = floatval($subcriteria['min_value']); |
---|
1041 | if ($value > floatval($subcriteria['max_value'])) |
---|
1042 | $value = floatval($subcriteria['max_value']); |
---|
1043 | |
---|
1044 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$subcriteria['criteria_id']."=".$value; |
---|
1045 | } |
---|
1046 | } |
---|
1047 | } |
---|
1048 | |
---|
1049 | // $datas = "0=0"; |
---|
1050 | $this->store_img_note_user($page['current_item'], $user_note, $concours['id'], $user['ipguest']); |
---|
1051 | if ($this->debug) echo "COMMENT=".$_POST['CONCOURS_COMMENT']; |
---|
1052 | $this->store_img_comment_user($page['current_item'], $_POST['concours_comment'], $concours['id'], $user['ipguest']); |
---|
1053 | } |
---|
1054 | |
---|
1055 | // If user want to erase notes |
---|
1056 | if (isset($_POST['concours_raz'])) |
---|
1057 | { |
---|
1058 | if ($this->debug) echo "RAZ"; |
---|
1059 | $this->RAZ_img_note_user($page['current_item'], $concours['id'], $user['ipguest']); |
---|
1060 | } |
---|
1061 | else |
---|
1062 | { |
---|
1063 | // Recover previous note in DB (if exists) |
---|
1064 | $user_notes = $this->get_img_note_user($page['current_item'], $concours['id'], $user['id'], $user['ipguest']); |
---|
1065 | } |
---|
1066 | |
---|
1067 | // Comment is not RAZ, always restore |
---|
1068 | $comment = $this->get_img_comment_user($page['current_item'], $concours['id'], $user['id'], $user['ipguest']); |
---|
1069 | |
---|
1070 | if ($this->debug) echo "STEP3\n"; |
---|
1071 | //------------ |
---|
1072 | // Step 3 |
---|
1073 | // Recover all 1st level criterias |
---|
1074 | $firstcriterias = $this->get_firstlevel_criterias($concours['id']); |
---|
1075 | foreach ($firstcriterias as $criteria) |
---|
1076 | { |
---|
1077 | if ($this->debug) echo "criteriaID=".$criteria['criteria_id']."\n"; |
---|
1078 | // First without sub criterias |
---|
1079 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] )) |
---|
1080 | { |
---|
1081 | $template->append( 'concours_criteria', array( |
---|
1082 | 'nosub' => true, |
---|
1083 | 'level' => 1, |
---|
1084 | 'id' => $criteria['criteria_id'], // id du critere |
---|
1085 | 'name' => $criteria['name'], // id du critere |
---|
1086 | 'lib' => $criteria['descr'], //.'(min='$criteria['min_value'].';max='.$criteria['min_value'].')', // libelle du critrer |
---|
1087 | 'val' => (isset($user_notes[$criteria['criteria_id']])?$user_notes[$criteria['criteria_id']] : $criteria['min_value']), // valeur du critere |
---|
1088 | 'min' => $criteria['min_value'], // min |
---|
1089 | 'max' => $criteria['max_value'] // max |
---|
1090 | )); |
---|
1091 | } |
---|
1092 | else |
---|
1093 | { |
---|
1094 | $template->append( 'concours_criteria', array( |
---|
1095 | 'nosub' => false, |
---|
1096 | 'level' => 1, |
---|
1097 | 'id' => $criteria['criteria_id'], // id du critere |
---|
1098 | 'name' => $criteria['name'], // id du critere |
---|
1099 | 'lib' => $criteria['descr'] |
---|
1100 | )); |
---|
1101 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours['id'] ); |
---|
1102 | foreach ($secondcriterias as $subcriteria) |
---|
1103 | { |
---|
1104 | if ($this->debug) echo "subcriteriaID=".$criteria['criteria_id']."\n"; |
---|
1105 | $template->append( 'concours_criteria', array( |
---|
1106 | 'nosub' => true, |
---|
1107 | 'level' => 2, |
---|
1108 | 'id' => $subcriteria['criteria_id'], // id du critere |
---|
1109 | 'name' => $subcriteria['name'], // id du critere |
---|
1110 | 'lib' => $subcriteria['descr'], //.'(min='$criteria['min_value'].';max='.$criteria['min_value'].')', // libelle du critrer |
---|
1111 | 'val' => (isset($user_notes[$subcriteria['criteria_id']])?$user_notes[$subcriteria['criteria_id']] : $subcriteria['min_value']), |
---|
1112 | 'min' => $subcriteria['min_value'], // min |
---|
1113 | 'max' => $subcriteria['max_value'] // max |
---|
1114 | )); |
---|
1115 | |
---|
1116 | } |
---|
1117 | } |
---|
1118 | |
---|
1119 | } |
---|
1120 | // Add the comment |
---|
1121 | $template->assign( 'CONCOURS_COMMENT', $comment); |
---|
1122 | |
---|
1123 | // if a user has already vote for this photo, check if the score change is allowed |
---|
1124 | |
---|
1125 | if ($this->debug) echo "change=".($this->my_config['concours_change_score'] ? "OK" : "KO"); |
---|
1126 | if ($user_notes == array() OR $this->my_config['concours_change_score']) // Allow user to change his vote after a validation |
---|
1127 | $template->assign( 'CONCOURS_CHANGE_SCORE', true); |
---|
1128 | else |
---|
1129 | $template->assign( 'CONCOURS_CHANGE_SCORE', false); |
---|
1130 | |
---|
1131 | |
---|
1132 | // Add concours informations on template |
---|
1133 | $template->assign( 'concours_infos', array( |
---|
1134 | 'name' => $concours['name'], |
---|
1135 | 'descr' => $concours['descr'], |
---|
1136 | 'note' => $this->get_img_globalnote_user($page['current_item'], $concours['id'], null, $user['ipguest']) |
---|
1137 | )); |
---|
1138 | |
---|
1139 | |
---|
1140 | $template->set_filenames(array('concours' => CONCOURS_ROOT.'/template/concours.tpl')); |
---|
1141 | // $template->concat('PLUGIN_PICTURE_AFTER', $template->parse('concours', true)); |
---|
1142 | $template->concat('COMMENT_IMG', $template->parse('concours', true)); |
---|
1143 | |
---|
1144 | // |
---|
1145 | $template->assign('INFO_AUTHOR',l10n('concours_img_author')); |
---|
1146 | } |
---|
1147 | } |
---|
1148 | } |
---|
1149 | |
---|
1150 | // RAZ notation from user to db |
---|
1151 | // fill criterias notes to 0 in DB |
---|
1152 | // return false if error |
---|
1153 | function RAZ_img_note_user($img_id, $concours_id = NULL, $ipguest = null) |
---|
1154 | { |
---|
1155 | $user_note = ""; |
---|
1156 | if ($concours_id === null) |
---|
1157 | if ($this->concours_id !== null) |
---|
1158 | $concours_id = $this->concours_id; |
---|
1159 | else |
---|
1160 | return false; |
---|
1161 | |
---|
1162 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1163 | foreach ($firstcriterias as $criteria) |
---|
1164 | { |
---|
1165 | if ($this->debug) echo "ID=".$criteria['criteria_id']."\n"; |
---|
1166 | // First without sub criterias |
---|
1167 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1168 | { |
---|
1169 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$criteria['criteria_id']."=".$criteria['min_value']; |
---|
1170 | } |
---|
1171 | else |
---|
1172 | { |
---|
1173 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1174 | foreach ($secondcriterias as $subcriteria) |
---|
1175 | { |
---|
1176 | if ($this->debug) echo "ID=".$criteria['criteria_id']."\n"; |
---|
1177 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$subcriteria['criteria_id']."=".$subcriteria['min_value']; |
---|
1178 | } |
---|
1179 | } |
---|
1180 | } |
---|
1181 | if ($this->debug) echo "RAZ=>".$user_note."\n"; |
---|
1182 | if (strlen($user_note) != 0) |
---|
1183 | { |
---|
1184 | $this->store_img_note_user($img_id, $user_note, $concours_id, $ipguest); |
---|
1185 | return true; |
---|
1186 | } |
---|
1187 | else |
---|
1188 | return false; |
---|
1189 | |
---|
1190 | |
---|
1191 | } |
---|
1192 | |
---|
1193 | // Store notation from user to db |
---|
1194 | // save with format "criteria_id=note;criteria_id=note..." for data |
---|
1195 | function store_img_note_user($img_id, $datas, $concours_id = NULL, $ipguest = null) |
---|
1196 | { |
---|
1197 | global $user; |
---|
1198 | if ($this->get_img_note_user( $img_id, $concours_id, $user['id'], $ipguest) == array() ) |
---|
1199 | { |
---|
1200 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id FROM ' . CONCOURS_DATA_TABLE . ' ;'; |
---|
1201 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
1202 | if ($this->debug) echo "Next_element=".$next_element_id."\n"; |
---|
1203 | $query = ' |
---|
1204 | INSERT INTO ' . CONCOURS_DATA_TABLE .' |
---|
1205 | (`id`, |
---|
1206 | `id_concours` , |
---|
1207 | `user_id` , |
---|
1208 | `create_date` , |
---|
1209 | `img_id` , |
---|
1210 | `datas`'. |
---|
1211 | ($ipguest ? ',`ipguest` ' : '') |
---|
1212 | |
---|
1213 | .') |
---|
1214 | VALUES ( '.$next_element_id.',' |
---|
1215 | .($concours_id !== null ? $concours_id : $this->concours_id ) .' |
---|
1216 | , '. $user['id'] .', now(), '.$img_id.', "'.$datas.'"' |
---|
1217 | .($ipguest ? ',"'.$user['ipguest'].'"' : '') |
---|
1218 | .');'; |
---|
1219 | } |
---|
1220 | else |
---|
1221 | { |
---|
1222 | $query = ' |
---|
1223 | UPDATE ' . CONCOURS_DATA_TABLE .' |
---|
1224 | SET datas = "'.$datas.'", create_date = now() |
---|
1225 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).' |
---|
1226 | AND user_id = '.$user['id'].' |
---|
1227 | AND img_id = '.$img_id. |
---|
1228 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1229 | .';'; |
---|
1230 | } |
---|
1231 | if ($this->debug) echo $query."\n"; |
---|
1232 | pwg_query($query); |
---|
1233 | |
---|
1234 | } |
---|
1235 | |
---|
1236 | // get notation's user from db |
---|
1237 | // Return an array with : |
---|
1238 | // Criteria_id ==> value |
---|
1239 | // Note : criterias with subcriterias are not saved and not restored. They are calculated for global result |
---|
1240 | function get_img_note_user($img_id, $concours_id = NULL, $user_id = null, $ipguest = null) |
---|
1241 | { |
---|
1242 | global $user; |
---|
1243 | $img_note = array(); |
---|
1244 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1245 | { |
---|
1246 | $query = ' |
---|
1247 | SELECT datas |
---|
1248 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1249 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
1250 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1251 | AND img_id = '. $img_id . |
---|
1252 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1253 | .';'; |
---|
1254 | if ($this->debug) echo $query."\n"; |
---|
1255 | $result = pwg_query($query); |
---|
1256 | |
---|
1257 | while ($row = mysql_fetch_assoc($result)) |
---|
1258 | { |
---|
1259 | if ($this->debug) echo "row[data]=".$row['datas']."\n"; |
---|
1260 | $datas = explode(";",$row['datas']); |
---|
1261 | foreach ($datas as $val) |
---|
1262 | { |
---|
1263 | if (strpos($val, '=') !== FALSE) |
---|
1264 | $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1); |
---|
1265 | } |
---|
1266 | } |
---|
1267 | // DEBUG |
---|
1268 | if ($this->debug) |
---|
1269 | foreach ($img_note as $id=>$val) |
---|
1270 | { |
---|
1271 | if (is_array($val)) |
---|
1272 | foreach ($val as $id2=>$val2) |
---|
1273 | echo "img_note[".$id."][".$id2."]=".$val2."\n"; |
---|
1274 | else |
---|
1275 | echo "img_note[".$id."]=".$val."\n"; |
---|
1276 | } |
---|
1277 | // END DEBUG |
---|
1278 | |
---|
1279 | return $img_note; |
---|
1280 | } |
---|
1281 | else |
---|
1282 | return array(); |
---|
1283 | |
---|
1284 | |
---|
1285 | } |
---|
1286 | |
---|
1287 | |
---|
1288 | // get notation's user from db |
---|
1289 | // Return the global note for a picture id from a user |
---|
1290 | function get_img_globalnote_user($img_id, $concours_id = NULL, $user_id = null, $ipguest = null) |
---|
1291 | { |
---|
1292 | global $user; |
---|
1293 | $img_note = array(); |
---|
1294 | $global_user_note = 0; |
---|
1295 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1296 | { |
---|
1297 | $query = ' |
---|
1298 | SELECT datas |
---|
1299 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1300 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
1301 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1302 | AND img_id = '. $img_id . |
---|
1303 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1304 | .';'; |
---|
1305 | $result = pwg_query($query); |
---|
1306 | |
---|
1307 | while ($row = mysql_fetch_assoc($result)) |
---|
1308 | { |
---|
1309 | $datas = explode(";",$row['datas']); |
---|
1310 | foreach ($datas as $val) |
---|
1311 | { |
---|
1312 | if (strpos($val, '=') !== FALSE) |
---|
1313 | $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1); |
---|
1314 | } |
---|
1315 | } |
---|
1316 | |
---|
1317 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1318 | foreach ($firstcriterias as $criteria) |
---|
1319 | { |
---|
1320 | if ($this->debug) echo "criteriaID=".$criteria['criteria_id']."\n"; |
---|
1321 | // First without sub criterias |
---|
1322 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1323 | { |
---|
1324 | if (isset($img_note[$criteria['criteria_id']])) |
---|
1325 | $global_user_note += (int)$criteria['ponderation'] * (float)$img_note[$criteria['criteria_id']]; |
---|
1326 | } |
---|
1327 | else |
---|
1328 | { |
---|
1329 | |
---|
1330 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1331 | foreach ($secondcriterias as $subcriteria) |
---|
1332 | { |
---|
1333 | if ($this->debug) echo "subcriteriaID=".$subcriteria['criteria_id']."\n"; |
---|
1334 | if (isset($img_note[$subcriteria['criteria_id']])) |
---|
1335 | $global_user_note += (int)$subcriteria['ponderation'] * (float)$img_note[$subcriteria['criteria_id']]; |
---|
1336 | } |
---|
1337 | } |
---|
1338 | |
---|
1339 | } |
---|
1340 | |
---|
1341 | return $global_user_note; |
---|
1342 | } |
---|
1343 | else |
---|
1344 | return array(); |
---|
1345 | |
---|
1346 | |
---|
1347 | } |
---|
1348 | |
---|
1349 | // Store comment from user to db |
---|
1350 | function store_img_comment_user($img_id, $datas, $concours_id = NULL, $ipguest = null) |
---|
1351 | { |
---|
1352 | global $user; |
---|
1353 | if (($comment = $this->get_img_note_user( $img_id, $concours_id, null, $ipguest)) == false) |
---|
1354 | { |
---|
1355 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id FROM ' . CONCOURS_DATA_TABLE . ' ;'; |
---|
1356 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
1357 | if ($this->debug) echo "Next_element=".$next_element_id."\n"; |
---|
1358 | $query = ' |
---|
1359 | INSERT INTO ' . CONCOURS_DATA_TABLE .' |
---|
1360 | (`id`, |
---|
1361 | `id_concours` , |
---|
1362 | `user_id` , |
---|
1363 | `create_date` , |
---|
1364 | `img_id` , |
---|
1365 | `comment`' |
---|
1366 | .($ipguest ? ' `ipguest`' : ''). |
---|
1367 | ') |
---|
1368 | VALUES ( '.$next_element_id.',' |
---|
1369 | .($concours_id !== null ? $concours_id : $this->concours_id ) .' |
---|
1370 | , '. $user['id'] .', now(), '.$img_id.', "'.$datas.'");'; |
---|
1371 | } |
---|
1372 | else |
---|
1373 | { |
---|
1374 | $query = ' |
---|
1375 | UPDATE ' . CONCOURS_DATA_TABLE .' |
---|
1376 | SET comment = "'.$datas.'", create_date = now() |
---|
1377 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).' |
---|
1378 | AND user_id = '.$user['id'].' |
---|
1379 | AND img_id = '.$img_id. |
---|
1380 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1381 | .';'; |
---|
1382 | } |
---|
1383 | if ($this->debug) echo $query."\n"; |
---|
1384 | pwg_query($query); |
---|
1385 | |
---|
1386 | } |
---|
1387 | |
---|
1388 | |
---|
1389 | // Get comment on an image for a user |
---|
1390 | function get_img_comment_user($img_id, $concours_id = NULL, $user_id = null, $ipguest = null) |
---|
1391 | { |
---|
1392 | global $user; |
---|
1393 | $datas = false; |
---|
1394 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1395 | { |
---|
1396 | $query = ' |
---|
1397 | SELECT comment |
---|
1398 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1399 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
1400 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1401 | AND img_id = '. $img_id . |
---|
1402 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1403 | .';'; |
---|
1404 | if ($this->debug) echo $query."\n"; |
---|
1405 | $result = pwg_query($query); |
---|
1406 | |
---|
1407 | while ($row = mysql_fetch_assoc($result)) |
---|
1408 | { |
---|
1409 | if ($this->debug) echo "row[comment]=".$row['comment']."\n"; |
---|
1410 | $datas = $row['comment']; |
---|
1411 | } |
---|
1412 | } |
---|
1413 | return $datas; |
---|
1414 | } |
---|
1415 | |
---|
1416 | |
---|
1417 | // Generate csv file for a closed concours with result generated |
---|
1418 | function generate_csv($concours_id = NULL) |
---|
1419 | { |
---|
1420 | global $conf; |
---|
1421 | $file = "rang, id_concours,method, img_id, img_name, img_file, date, note, moyenne, nbvotant, datas\r\n"; |
---|
1422 | |
---|
1423 | if (!(isset($this->concours_infos['method']))) |
---|
1424 | $this->concours_infos['method'] = 1; |
---|
1425 | |
---|
1426 | // recover all img_id from the category |
---|
1427 | $query = 'SELECT id_concours,method, img_id, IMG.name, IMG.file, date, note, moyenne, nbvotant, datas' |
---|
1428 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1429 | .' INNER JOIN ' . CONCOURS_TABLE.' AS CONC on CONC.id = id_concours' |
---|
1430 | .' INNER JOIN ' . IMAGES_TABLE.' AS IMG on IMG.id = img_id' |
---|
1431 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ); |
---|
1432 | |
---|
1433 | if ($this->concours_infos['method'] == 1) // total |
---|
1434 | $query .= ' ORDER BY note DESC'; |
---|
1435 | elseif ($this->concours_infos['method'] == 2) // moyenne |
---|
1436 | $query .= ' ORDER BY moyenne DESC'; |
---|
1437 | |
---|
1438 | $query .=';'; |
---|
1439 | |
---|
1440 | $result = pwg_query($query); |
---|
1441 | // For each images |
---|
1442 | $rang = 1; |
---|
1443 | while ($row = mysql_fetch_assoc($result)) |
---|
1444 | { |
---|
1445 | $file .= ($row['nbvotant'] != 0 ? $rang : 'N/A').', ' |
---|
1446 | .$row['id_concours'].', ' |
---|
1447 | .$row['method'].', ' |
---|
1448 | .$row['img_id'].', ' |
---|
1449 | .$row['name'].', ' |
---|
1450 | .$row['file'].', ' |
---|
1451 | .$row['date'].', ' |
---|
1452 | .$row['note'].', ' |
---|
1453 | .$row['moyenne'].', ' |
---|
1454 | .$row['nbvotant'].', ' |
---|
1455 | .($row['datas'] != null ? $row['datas'] : '') |
---|
1456 | ."\r\n"; |
---|
1457 | $rang ++; |
---|
1458 | } |
---|
1459 | |
---|
1460 | return $file; |
---|
1461 | |
---|
1462 | |
---|
1463 | } |
---|
1464 | |
---|
1465 | // Generate csv file for a closed concours with result generated and all users informations |
---|
1466 | function generate_detail_csv($concours_id = NULL) |
---|
1467 | { |
---|
1468 | global $conf; |
---|
1469 | if ($concours_id === null) |
---|
1470 | $concours_id = $this->concours_id; |
---|
1471 | // Prepare the list of criteria |
---|
1472 | $criteria_list = ""; |
---|
1473 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1474 | $ident1 = 1; |
---|
1475 | foreach ($firstcriterias as $criteria) |
---|
1476 | { |
---|
1477 | // format (id:name) |
---|
1478 | $criteria_list .= (strlen($criteria_list) ? "," : "") |
---|
1479 | .$ident1.":".$criteria['name'] |
---|
1480 | ."(id=".$criteria['criteria_id'].")"; |
---|
1481 | // First wit sub criterias |
---|
1482 | if ($this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1483 | { |
---|
1484 | $ident2 = 1; |
---|
1485 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1486 | foreach ($secondcriterias as $subcriteria) |
---|
1487 | { |
---|
1488 | // format (id:name) |
---|
1489 | $criteria_list .= (strlen($criteria_list) ? "," : "") |
---|
1490 | .$ident1.".".$ident2 |
---|
1491 | .":".$subcriteria['name']."(id=".$subcriteria['criteria_id'].")"; |
---|
1492 | $ident2 ++; |
---|
1493 | } |
---|
1494 | } |
---|
1495 | $ident1++; |
---|
1496 | } |
---|
1497 | if ($this->debug) echo "CRITERIA_LIST=".$criteria_list."\n"; |
---|
1498 | $user_list = array(); |
---|
1499 | $users_criteria = ""; |
---|
1500 | |
---|
1501 | |
---|
1502 | // Get all the users who have notes for the concours |
---|
1503 | $query = 'SELECT distinct(user_id), USER.username' |
---|
1504 | .' FROM ' .CONCOURS_DATA_TABLE |
---|
1505 | .' INNER JOIN ' . USERS_TABLE.' AS USER on USER.id = user_id' |
---|
1506 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1507 | // Dont take the guest informations because |
---|
1508 | .' AND user_id <> '.$conf['guest_id'] |
---|
1509 | .' ORDER BY username ASC' |
---|
1510 | .';'; |
---|
1511 | $result = pwg_query($query); |
---|
1512 | // For each user |
---|
1513 | while ($row = mysql_fetch_assoc($result)) |
---|
1514 | { |
---|
1515 | array_push($user_list, $row); |
---|
1516 | $users_criteria .= (strlen($users_criteria) ? "," : "")."user, user_global_note, comment, ".$criteria_list; |
---|
1517 | |
---|
1518 | } |
---|
1519 | |
---|
1520 | $ipguest = array(); |
---|
1521 | // Get guest info (if available) |
---|
1522 | if ($this->concours_infos['guest']) |
---|
1523 | { |
---|
1524 | $ipguest = $this->get_guest_list(); |
---|
1525 | // For each guest |
---|
1526 | foreach ( $ipguest as $i => $userid ) //on parcours le tableau |
---|
1527 | { |
---|
1528 | $users_criteria .= (strlen($users_criteria) ? "," : "")."user, user_global_note, comment, ".$criteria_list; |
---|
1529 | } |
---|
1530 | } |
---|
1531 | |
---|
1532 | if ($this->debug) echo "users_criteria=".$users_criteria."\n"; |
---|
1533 | |
---|
1534 | // All informations in csv format |
---|
1535 | $file = "rang, id_concours, method, nom_concours, date_debut, date_fin, date_export, img_id, img_name, img_file, img_author, note, moyenne, nbvotant, datas," |
---|
1536 | .$users_criteria |
---|
1537 | ."\r\n"; |
---|
1538 | |
---|
1539 | // nb of users who vote for each image |
---|
1540 | |
---|
1541 | // NOT NEEDED, INFO STORE IN DATABASE 'RESULT' |
---|
1542 | $nbvotes = $this->nb_votes_by_img($concours_id); |
---|
1543 | |
---|
1544 | |
---|
1545 | // recover all img_id from the category |
---|
1546 | if (!(isset($this->concours_infos['method']))) |
---|
1547 | $this->concours_infos['method'] = 1; |
---|
1548 | $query = 'SELECT id_concours, method, img_id, IMG.name, IMG.file, IMG.author, date, note, moyenne, nbvotant, datas, comment' |
---|
1549 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1550 | .' INNER JOIN ' . CONCOURS_TABLE.' AS CONC on CONC.id = id_concours' |
---|
1551 | .' INNER JOIN ' . IMAGES_TABLE.' AS IMG on IMG.id = img_id' |
---|
1552 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ); |
---|
1553 | |
---|
1554 | if ($this->concours_infos['method'] == 1) // total |
---|
1555 | $query .= ' ORDER BY note DESC'; |
---|
1556 | elseif ($this->concours_infos['method'] == 2) // moyenne |
---|
1557 | $query .= ' ORDER BY moyenne DESC'; |
---|
1558 | |
---|
1559 | $query .=';'; |
---|
1560 | |
---|
1561 | if ($this->debug) echo $query; |
---|
1562 | $result = pwg_query($query); |
---|
1563 | // For each images |
---|
1564 | $rang = 1; |
---|
1565 | $previousNote = $previousMoy = 0; |
---|
1566 | |
---|
1567 | while ($row = mysql_fetch_assoc($result)) |
---|
1568 | { |
---|
1569 | // Check and verify for exaequo |
---|
1570 | if ($this->my_config['check_exaequo']) |
---|
1571 | { |
---|
1572 | if ( ($this->concours_infos['method'] == 1 // total |
---|
1573 | AND ($row['note'] == $previousNote)) |
---|
1574 | OR ($this->concours_infos['method'] == 2 // moyenne |
---|
1575 | AND ($row['moyenne'] == $previousMoy))) |
---|
1576 | { |
---|
1577 | $rang --; |
---|
1578 | |
---|
1579 | } |
---|
1580 | } |
---|
1581 | |
---|
1582 | $file .= ($row['nbvotant'] != 0 ? $rang : 'N/A').', ' |
---|
1583 | .$row['id_concours'].', ' |
---|
1584 | .$row['method'].', ' |
---|
1585 | .str_replace(",", "",$this->concours_infos['name']).', ' |
---|
1586 | .$this->concours_infos['begin_date'].', ' |
---|
1587 | .$this->concours_infos['end_date'].', ' |
---|
1588 | .$row['date'].', ' |
---|
1589 | |
---|
1590 | .$row['img_id'].', ' |
---|
1591 | .str_replace(",", "",$row['name']).', ' |
---|
1592 | .$row['file'].', ' |
---|
1593 | .$row['author'].', ' |
---|
1594 | .$row['note'].', ' |
---|
1595 | .$row['moyenne'].', ' |
---|
1596 | .$row['nbvotant'].', ' |
---|
1597 | // .(isset($nbvotes[$row['img_id']]) ? $nbvotes[$row['img_id']] : 0).', ' |
---|
1598 | .($row['datas'] != null ? $row['datas'] : '') |
---|
1599 | ; |
---|
1600 | foreach ($user_list as $uuser) |
---|
1601 | { |
---|
1602 | |
---|
1603 | $comment = $this->get_img_comment_user($row['img_id'], $concours_id, $uuser['user_id']); |
---|
1604 | |
---|
1605 | $user_note = $this->get_img_note_user($row['img_id'], $concours_id, $uuser['user_id']); |
---|
1606 | |
---|
1607 | $file .= ', '.$uuser['username'].', '.$this->get_img_globalnote_user($row['img_id'], $concours_id, $uuser['user_id']) |
---|
1608 | // .', '.($row['comment'] != null ? str_replace(",", "",$row['comment']) : '') |
---|
1609 | .', '.($comment != false ? str_replace(array(",","\r\n", "\n", "\r"), " ",$comment) : '') |
---|
1610 | .', ' |
---|
1611 | ; |
---|
1612 | |
---|
1613 | $user_note_by_crit = ""; |
---|
1614 | foreach ($firstcriterias as $criteria) |
---|
1615 | { |
---|
1616 | // First without sub criterias |
---|
1617 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1618 | { |
---|
1619 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").(isset($user_note[$criteria['criteria_id']]) ? $user_note[$criteria['criteria_id']] : ' '); |
---|
1620 | } |
---|
1621 | else |
---|
1622 | { |
---|
1623 | $user_note_by_subcrit = ""; |
---|
1624 | $subcrit_note = 0; |
---|
1625 | $user_has_vote = false; |
---|
1626 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1627 | foreach ($secondcriterias as $subcriteria) |
---|
1628 | { |
---|
1629 | if (isset($user_note[$subcriteria['criteria_id']])) $user_has_vote = true; |
---|
1630 | $user_note_by_subcrit .= (strlen($user_note_by_subcrit) ? "," : "").(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : ' '); |
---|
1631 | $subcrit_note += (int)$subcriteria['ponderation'] * (float)(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : 0); |
---|
1632 | |
---|
1633 | } |
---|
1634 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").($user_has_vote ? $subcrit_note : ' ').",".$user_note_by_subcrit; |
---|
1635 | } |
---|
1636 | } |
---|
1637 | $file .= $user_note_by_crit; |
---|
1638 | |
---|
1639 | } |
---|
1640 | |
---|
1641 | // Add guest infos (if present) |
---|
1642 | foreach ($ipguest as $ipguestt) |
---|
1643 | { |
---|
1644 | // echo "IP=".$ipguestt."\n"; |
---|
1645 | $comment = $this->get_img_comment_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt); |
---|
1646 | |
---|
1647 | $user_note = $this->get_img_note_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt); |
---|
1648 | |
---|
1649 | $file .= ', Guest('.$ipguestt.'), '.$this->get_img_globalnote_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt) |
---|
1650 | // .', '.($row['comment'] != null ? str_replace(",", "",$row['comment']) : '') |
---|
1651 | .', '.($comment != false ? str_replace(array(",","\r\n", "\n", "\r"), " ",$comment) : '') |
---|
1652 | .', ' |
---|
1653 | ; |
---|
1654 | |
---|
1655 | $user_note_by_crit = ""; |
---|
1656 | foreach ($firstcriterias as $criteria) |
---|
1657 | { |
---|
1658 | // First without sub criterias |
---|
1659 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1660 | { |
---|
1661 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").(isset($user_note[$criteria['criteria_id']]) ? $user_note[$criteria['criteria_id']] : ' '); |
---|
1662 | } |
---|
1663 | else |
---|
1664 | { |
---|
1665 | $user_note_by_subcrit = ""; |
---|
1666 | $subcrit_note = 0; |
---|
1667 | $user_has_vote = false; |
---|
1668 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1669 | foreach ($secondcriterias as $subcriteria) |
---|
1670 | { |
---|
1671 | if (isset($user_note[$subcriteria['criteria_id']])) $user_has_vote = true; |
---|
1672 | $user_note_by_subcrit .= (strlen($user_note_by_subcrit) ? "," : "").(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : ' '); |
---|
1673 | $subcrit_note += (int)$subcriteria['ponderation'] * (float)(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : 0); |
---|
1674 | |
---|
1675 | } |
---|
1676 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").($user_has_vote ? $subcrit_note : ' ').",".$user_note_by_subcrit; |
---|
1677 | } |
---|
1678 | } |
---|
1679 | $file .= $user_note_by_crit; |
---|
1680 | |
---|
1681 | } |
---|
1682 | |
---|
1683 | $file .="\r\n"; |
---|
1684 | $rang ++; |
---|
1685 | $previousNote = $row['note']; |
---|
1686 | $previousMoy = $row['moyenne']; |
---|
1687 | |
---|
1688 | } |
---|
1689 | if ($this->debug) echo $file; |
---|
1690 | return utf8_decode($file); |
---|
1691 | |
---|
1692 | |
---|
1693 | } |
---|
1694 | |
---|
1695 | |
---|
1696 | |
---|
1697 | // Save csv datas to physical file |
---|
1698 | function save_file($file,$concours_id = NULL) |
---|
1699 | { |
---|
1700 | // check if already saved file |
---|
1701 | if ($filename = $this->get_file_name($concours_id)) |
---|
1702 | return $filename; |
---|
1703 | else |
---|
1704 | { |
---|
1705 | $dirpath = CONCOURS_RESULT_FOLDER; |
---|
1706 | |
---|
1707 | $filename = date('Ymd')."_concours".($concours_id !== null ? $concours_id : $this->concours_id ).".csv"; |
---|
1708 | if (!is_dir($dirpath)) |
---|
1709 | @mkdir($dirpath); |
---|
1710 | if (file_exists($dirpath. $filename)) |
---|
1711 | unlink($dirpath. $filename); |
---|
1712 | |
---|
1713 | $fh = fopen($dirpath. $filename, 'w') or die("can't open file"); |
---|
1714 | fwrite($fh, $file); |
---|
1715 | fclose($fh); |
---|
1716 | |
---|
1717 | //update result database with filename |
---|
1718 | $query = "UPDATE " . CONCOURS_RESULT_TABLE . " |
---|
1719 | SET |
---|
1720 | file_name = \"".$filename."\" |
---|
1721 | WHERE id_concours= ".($concours_id !== NULL ? $concours_id : $this->concours_id)." |
---|
1722 | ;"; |
---|
1723 | pwg_query($query); |
---|
1724 | if ($this->debug) echo $query; |
---|
1725 | |
---|
1726 | return $filename; |
---|
1727 | } |
---|
1728 | } |
---|
1729 | |
---|
1730 | // Get result file name from db |
---|
1731 | function get_file_name($concours_id = NULL) |
---|
1732 | { |
---|
1733 | $query = 'SELECT distinct(file_name)' |
---|
1734 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1735 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1736 | .';'; |
---|
1737 | if ($this->debug) echo $query; |
---|
1738 | $result = pwg_query($query); |
---|
1739 | if ($result) |
---|
1740 | { |
---|
1741 | $row = mysql_fetch_assoc($result); |
---|
1742 | if ($row) |
---|
1743 | return $row['file_name']; |
---|
1744 | else |
---|
1745 | return false; |
---|
1746 | } |
---|
1747 | } |
---|
1748 | |
---|
1749 | // Get csv datas from a saved file |
---|
1750 | function get_file($concours_id = NULL) |
---|
1751 | { |
---|
1752 | $filename = CONCOURS_RESULT_FOLDER. |
---|
1753 | $size = filesize($filename); |
---|
1754 | header('Content-Type: application/octet-stream'); |
---|
1755 | header('Content-Length: '.$size); |
---|
1756 | header('Content-Disposition: attachment; filename='.$filename); |
---|
1757 | header('Content-Transfer-Encoding: binary'); |
---|
1758 | |
---|
1759 | $file = @fopen($filename, ‘rb’); |
---|
1760 | if ($file) { |
---|
1761 | // stream the file and exit the script when complete |
---|
1762 | fpassthru($file); |
---|
1763 | } |
---|
1764 | |
---|
1765 | } |
---|
1766 | |
---|
1767 | // Activation page administration |
---|
1768 | function concours_admin_menu($menu) |
---|
1769 | { |
---|
1770 | array_push($menu, |
---|
1771 | array( |
---|
1772 | 'NAME' => CONCOURS_NAME, |
---|
1773 | 'URL' => get_admin_plugin_menu_link(CONCOURS_ADMIN_PATH.'admin.php') |
---|
1774 | ) |
---|
1775 | ); |
---|
1776 | return $menu; |
---|
1777 | } |
---|
1778 | |
---|
1779 | // register Concours Menubar |
---|
1780 | public function register_blocks( $menu_ref_arr ) |
---|
1781 | { |
---|
1782 | $menu = & $menu_ref_arr[0]; |
---|
1783 | if ($menu->get_id() != 'menubar' OR $this->my_config['active_menubar'] == false) |
---|
1784 | return; |
---|
1785 | $menu->register_block( new RegisteredBlock( 'CONCOURS_menu', 'concours', 'ConcoursPhoto')); |
---|
1786 | } |
---|
1787 | |
---|
1788 | // initialise menubar's menu. called by menubar object when making menu |
---|
1789 | public function blockmanager_apply( $menu_ref_arr ) |
---|
1790 | { |
---|
1791 | |
---|
1792 | global $user, $template; |
---|
1793 | $menu = & $menu_ref_arr[0]; |
---|
1794 | |
---|
1795 | if ($this->user_groups == array()) |
---|
1796 | $this->get_user_groups(); |
---|
1797 | |
---|
1798 | if ( ($block = $menu->get_block( 'CONCOURS_menu' ) ) != null ) |
---|
1799 | { |
---|
1800 | |
---|
1801 | $menu_list=array(); |
---|
1802 | |
---|
1803 | if(is_admin()) |
---|
1804 | { |
---|
1805 | array_push($menu_list, |
---|
1806 | array( |
---|
1807 | 'nfo' => "", |
---|
1808 | 'text' => l10n('concours_admin_add'), |
---|
1809 | 'link' => PHPWG_ROOT_PATH . 'admin.php?page=plugin&section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&action=new', |
---|
1810 | 'edit' => "" |
---|
1811 | ) |
---|
1812 | ); |
---|
1813 | } |
---|
1814 | |
---|
1815 | // recover all img_id from the category |
---|
1816 | $query = 'SELECT distinct(id_concours), groups, guest, CONC.name' |
---|
1817 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1818 | .' INNER JOIN '.CONCOURS_TABLE. ' AS CONC ON CONC.id = id_concours' |
---|
1819 | .' ORDER BY id DESC' |
---|
1820 | .';'; |
---|
1821 | |
---|
1822 | $result = pwg_query($query); |
---|
1823 | $nb_concours = 1; |
---|
1824 | // For each concours |
---|
1825 | while ($row = mysql_fetch_assoc($result)) |
---|
1826 | { |
---|
1827 | |
---|
1828 | if ($nb_concours > $this->my_config['nbconcours_menubar']) |
---|
1829 | break; |
---|
1830 | |
---|
1831 | $conc_to_show = false; // Default ==> dont show |
---|
1832 | |
---|
1833 | // Guest |
---|
1834 | if (is_a_guest() AND $row['guest']) |
---|
1835 | $conc_to_show = true; |
---|
1836 | // Group present |
---|
1837 | elseif (!empty($row['groups'])) |
---|
1838 | { |
---|
1839 | $authorized_groups = explode(',', $row['groups']); |
---|
1840 | if (array_intersect($this->user_groups, $authorized_groups) != array()) |
---|
1841 | $conc_to_show = true; |
---|
1842 | } |
---|
1843 | // Allowed for any user (logged) |
---|
1844 | elseif (empty($row['groups'])) |
---|
1845 | $conc_to_show = true; |
---|
1846 | |
---|
1847 | if ($conc_to_show) |
---|
1848 | { |
---|
1849 | $nb_concours ++; |
---|
1850 | array_push($menu_list, |
---|
1851 | array( |
---|
1852 | 'nfo' => "", |
---|
1853 | 'text' => $row['name'], |
---|
1854 | 'link' => './index.php?/concours/'.$row['id_concours'], |
---|
1855 | 'edit' => (is_admin()? PHPWG_ROOT_PATH . 'admin.php?page=plugin&section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&concours=' . $row['id_concours'].'&action=modify':"") |
---|
1856 | ) |
---|
1857 | ); |
---|
1858 | } |
---|
1859 | } |
---|
1860 | |
---|
1861 | if (count($menu_list)) |
---|
1862 | { |
---|
1863 | |
---|
1864 | $block->set_title(l10n('concoursphoto')); |
---|
1865 | $block->template = CONCOURS_ROOT.'/template/concours_menu.tpl'; |
---|
1866 | $block->data = $menu_list; |
---|
1867 | } |
---|
1868 | } |
---|
1869 | } |
---|
1870 | |
---|
1871 | |
---|
1872 | function section_init_concours() |
---|
1873 | { |
---|
1874 | global $tokens, $page; |
---|
1875 | if ($tokens[0] == 'concours') |
---|
1876 | $page['section'] = 'concours'; |
---|
1877 | } |
---|
1878 | |
---|
1879 | function index_concours() |
---|
1880 | { |
---|
1881 | global $page; |
---|
1882 | |
---|
1883 | |
---|
1884 | if (isset($page['section']) and $page['section'] == 'concours') |
---|
1885 | { |
---|
1886 | include(CONCOURS_PATH . 'publish.php'); |
---|
1887 | } |
---|
1888 | } |
---|
1889 | |
---|
1890 | |
---|
1891 | // Show the global note under the thumbnail |
---|
1892 | function thumbnail_note($tpl_var) |
---|
1893 | { |
---|
1894 | |
---|
1895 | global $page, $user; |
---|
1896 | $this->get_user_groups(); |
---|
1897 | |
---|
1898 | $user['ipguest'] = null; |
---|
1899 | if (is_a_guest()) |
---|
1900 | { |
---|
1901 | if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) |
---|
1902 | $IP = $_SERVER['HTTP_X_FORWARDED_FOR']; |
---|
1903 | elseif(isset($_SERVER['HTTP_CLIENT_IP'])) |
---|
1904 | $IP = $_SERVER['HTTP_CLIENT_IP']; |
---|
1905 | else |
---|
1906 | $IP = $_SERVER['REMOTE_ADDR']; |
---|
1907 | |
---|
1908 | // store ip |
---|
1909 | $user['ipguest'] = $IP; |
---|
1910 | } |
---|
1911 | |
---|
1912 | if ($this->my_config['thumb_note'] && $this->my_config['thumb_note'] == true) |
---|
1913 | { |
---|
1914 | $concours = array(); |
---|
1915 | if (($page['section']) == 'categories' AND !empty($page['category'])) |
---|
1916 | { |
---|
1917 | $query = ' |
---|
1918 | SELECT * |
---|
1919 | FROM ' . CONCOURS_TABLE .' |
---|
1920 | WHERE category =' . $page['category']['id'] . ' |
---|
1921 | AND time_to_sec(TIMEDIFF(begin_date,now())) < 0 |
---|
1922 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
1923 | '; |
---|
1924 | |
---|
1925 | $result = pwg_query($query); |
---|
1926 | if ($this->debug) echo $query."\n"; |
---|
1927 | while ($row = mysql_fetch_assoc($result)) |
---|
1928 | { |
---|
1929 | |
---|
1930 | if (!empty($row['groups'])) |
---|
1931 | { |
---|
1932 | if ($this->debug) echo "GROUPS=".$row['groups']; |
---|
1933 | $authorized_groups = explode(',', $row['groups']); |
---|
1934 | if (array_intersect($this->user_groups, $authorized_groups) == array()) |
---|
1935 | { |
---|
1936 | if ($this->debug) echo "ERROR GROUPS"; |
---|
1937 | continue; |
---|
1938 | } |
---|
1939 | $concours = $row; |
---|
1940 | } |
---|
1941 | |
---|
1942 | // If no group is parameter for that concours ==> available for all registered users |
---|
1943 | else |
---|
1944 | $concours = $row; |
---|
1945 | |
---|
1946 | } |
---|
1947 | if (count($concours)) |
---|
1948 | { |
---|
1949 | // Check if user is guest and if guest is allowed |
---|
1950 | if ((is_a_guest() AND $concours['guest']) |
---|
1951 | OR !is_a_guest()) |
---|
1952 | { |
---|
1953 | foreach($tpl_var as $cle=>$valeur) |
---|
1954 | { |
---|
1955 | // show only if the author is not the active user |
---|
1956 | if ($tpl_var[$cle]['FILE_AUTHOR'] != $user['username'] |
---|
1957 | OR $this->my_config['author_vote_available'] // Allow author to vote (and also show his score) |
---|
1958 | ) |
---|
1959 | { |
---|
1960 | $tpl_var[$cle]['NAME'] = $tpl_var[$cle]['NAME'].'<br> '.l10n('thumbnail_global_note').' : '.$this->get_img_globalnote_user($tpl_var[$cle]['ID'], $concours['id'], $user['id'], $user['ipguest']); |
---|
1961 | } |
---|
1962 | } |
---|
1963 | } |
---|
1964 | } |
---|
1965 | } |
---|
1966 | } |
---|
1967 | return $tpl_var; |
---|
1968 | } |
---|
1969 | |
---|
1970 | |
---|
1971 | } |
---|
1972 | |
---|
1973 | ?> |
---|