1 | <?php |
---|
2 | /* |
---|
3 | * Plugin Name: ConcoursPhoto |
---|
4 | * File : Concours.class.php |
---|
5 | */ |
---|
6 | |
---|
7 | global $user; |
---|
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 | VALUES (".($concours_id !== NULL ? $concours_id : $this->concours_id).", now(), |
---|
167 | \"".$this->concours_infos['name']."\", \"".$this->concours_infos['descr']."\", |
---|
168 | \"".$this->concours_infos['begin_date']."\", \"".$this->concours_infos['end_date']."\", |
---|
169 | ".$this->concours_infos['category'].", ".$this->concours_infos['groups'].");"; |
---|
170 | if (pwg_query($query) != null) |
---|
171 | return true; |
---|
172 | else |
---|
173 | return false; |
---|
174 | } |
---|
175 | else |
---|
176 | return false; |
---|
177 | } |
---|
178 | |
---|
179 | // update informations on database for a concours_id |
---|
180 | function update_concours($concours_id = null) |
---|
181 | { |
---|
182 | if ($concours_id!== null or $this->concours_id !== null) |
---|
183 | { |
---|
184 | |
---|
185 | $query = "UPDATE " . CONCOURS_TABLE . " |
---|
186 | SET |
---|
187 | create_date = now(), |
---|
188 | name = \"".$this->concours_infos['name']."\", |
---|
189 | descr = \"".$this->concours_infos['descr']."\", |
---|
190 | begin_date = \"".$this->concours_infos['begin_date']."\", |
---|
191 | end_date = \"".$this->concours_infos['end_date']."\", |
---|
192 | category = ".$this->concours_infos['category'].", |
---|
193 | groups = ".$this->concours_infos['groups']." |
---|
194 | WHERE id = ".($concours_id !== NULL ? $concours_id : $this->concours_id)." |
---|
195 | ;"; |
---|
196 | pwg_query($query); |
---|
197 | if (pwg_query($query) != null) |
---|
198 | return true; |
---|
199 | else |
---|
200 | return false; |
---|
201 | } |
---|
202 | else |
---|
203 | return false; |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | // delete concours from db (and all sub informations such as details, vote and result |
---|
208 | function delete_concours($concours_id = null) |
---|
209 | { |
---|
210 | if ($concours_id!== null or $this->concours_id !== null) |
---|
211 | { |
---|
212 | |
---|
213 | $query = ' |
---|
214 | DELETE |
---|
215 | FROM ' . CONCOURS_TABLE .' |
---|
216 | WHERE id =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
217 | '; |
---|
218 | pwg_query($query); |
---|
219 | $query = ' |
---|
220 | DELETE |
---|
221 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
222 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
223 | '; |
---|
224 | pwg_query($query); |
---|
225 | $query = ' |
---|
226 | DELETE |
---|
227 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
228 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
229 | '; |
---|
230 | pwg_query($query); |
---|
231 | $query = ' |
---|
232 | DELETE |
---|
233 | FROM ' . CONCOURS_RESULT_TABLE .' |
---|
234 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
235 | '; |
---|
236 | pwg_query($query); |
---|
237 | } |
---|
238 | else |
---|
239 | return false; |
---|
240 | |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | // today's date is between open and close date of concours? |
---|
245 | function is_active($concours_id = NULL) |
---|
246 | { |
---|
247 | if ($concours_id!== null or $this->concours_id !== null) |
---|
248 | { |
---|
249 | $query = ' |
---|
250 | SELECT id |
---|
251 | FROM ' . CONCOURS_TABLE .' |
---|
252 | WHERE id =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
253 | AND time_to_sec(TIMEDIFF(begin_date,now())) < 0 |
---|
254 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
255 | '; |
---|
256 | |
---|
257 | $result = pwg_query($query); |
---|
258 | if ($this->debug) echo $query."\n"; |
---|
259 | if ($result != NULL) |
---|
260 | return true; |
---|
261 | else |
---|
262 | return false; |
---|
263 | } |
---|
264 | else |
---|
265 | return NULL; |
---|
266 | } |
---|
267 | |
---|
268 | // today's date is not between open and close date of concours? |
---|
269 | function is_closed($concours_id = NULL) |
---|
270 | { |
---|
271 | if ($concours_id!== null or $this->concours_id !== null) |
---|
272 | { |
---|
273 | $query = ' |
---|
274 | SELECT id |
---|
275 | FROM ' . CONCOURS_TABLE .' |
---|
276 | WHERE id =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
277 | AND time_to_sec(TIMEDIFF(now(), end_date)) > 0 |
---|
278 | '; |
---|
279 | |
---|
280 | $result = pwg_query($query); |
---|
281 | if ($this->debug) echo $query."\n"; |
---|
282 | if ($result != NULL) |
---|
283 | return true; |
---|
284 | else |
---|
285 | return false; |
---|
286 | } |
---|
287 | else |
---|
288 | return NULL; |
---|
289 | } |
---|
290 | |
---|
291 | |
---|
292 | // Get criterias that are stored on db for default mode (concours_id = 0) |
---|
293 | function get_default_criterias() |
---|
294 | { |
---|
295 | $criterias = $this->get_criterias_list(0); |
---|
296 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id |
---|
297 | FROM ' . CONCOURS_DETAIL_TABLE . ' ;'; |
---|
298 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
299 | |
---|
300 | foreach ($criterias as $criteria) |
---|
301 | { |
---|
302 | if ($this->debug) echo "NEXT =".$next_element_id."\n"; |
---|
303 | $query = 'INSERT INTO '.CONCOURS_DETAIL_TABLE.' |
---|
304 | (id, id_concours, criteria_id, name, descr, min_value, max_value, ponderation, uppercriteria)' |
---|
305 | .'VALUES ('.$next_element_id.', '.$this->concours_id.', ' |
---|
306 | .$criteria['criteria_id'].', "'.$criteria['name'].'", "' |
---|
307 | .$criteria['descr'].'", '.$criteria['min_value'].', ' |
---|
308 | .$criteria['max_value'].', '.$criteria['ponderation'].', ' |
---|
309 | .($criteria['uppercriteria'] ? $criteria['uppercriteria'] : 'NULL').') |
---|
310 | ;'; |
---|
311 | |
---|
312 | $result = pwg_query($query); |
---|
313 | if ($this->debug) echo $query; |
---|
314 | $next_element_id = $next_element_id +1; |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | // Get criterias from a concours |
---|
319 | function get_criterias_list($concours_id = NULL) |
---|
320 | { |
---|
321 | $criteria_list = array(); |
---|
322 | if ($concours_id!== null or $this->concours_id !== null) |
---|
323 | { |
---|
324 | $query = ' |
---|
325 | SELECT * |
---|
326 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
327 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
328 | ORDER BY criteria_id |
---|
329 | '; |
---|
330 | $result = pwg_query($query); |
---|
331 | if ($this->debug) echo $query."\n"; |
---|
332 | while ($row = mysql_fetch_assoc($result)) |
---|
333 | { |
---|
334 | array_push($criteria_list, $row); |
---|
335 | } |
---|
336 | return $criteria_list; |
---|
337 | } |
---|
338 | else |
---|
339 | return $criteria_list; |
---|
340 | } |
---|
341 | |
---|
342 | // Get list of the fist level criterias |
---|
343 | function get_firstlevel_criterias($concours_id = NULL) |
---|
344 | { |
---|
345 | $criteria_list = array(); |
---|
346 | if ($concours_id!== null or $this->concours_id !== null) |
---|
347 | { |
---|
348 | $query = ' |
---|
349 | SELECT * |
---|
350 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
351 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
352 | AND uppercriteria IS NULL |
---|
353 | ORDER BY criteria_id |
---|
354 | '; |
---|
355 | $result = pwg_query($query); |
---|
356 | if ($this->debug) echo $query."\n"; |
---|
357 | while ($row = mysql_fetch_assoc($result)) |
---|
358 | { |
---|
359 | if ($this->debug) echo "criteria_id=".$row['criteria_id']."\n"; |
---|
360 | array_push($criteria_list, $row); |
---|
361 | } |
---|
362 | return $criteria_list; |
---|
363 | } |
---|
364 | else |
---|
365 | return $criteria_list; |
---|
366 | } |
---|
367 | |
---|
368 | // check if a criteria contains subcriterias |
---|
369 | function is_criterias_contains_sub($criteria_id, $concours_id = NULL) |
---|
370 | { |
---|
371 | if ($concours_id!== null or $this->concours_id !== null) |
---|
372 | { |
---|
373 | $query = ' |
---|
374 | SELECT criteria_id |
---|
375 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
376 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
377 | AND uppercriteria = '.$criteria_id.' |
---|
378 | '; |
---|
379 | $result = pwg_query($query); |
---|
380 | if ($this->debug) echo $query."\n"; |
---|
381 | // mini 1 line |
---|
382 | if(mysql_fetch_assoc($result)) |
---|
383 | return true; |
---|
384 | else |
---|
385 | return false; |
---|
386 | } |
---|
387 | return NULL; |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | // Get list of subcriterias from a criteria_id |
---|
392 | function get_subcriterias($criteria_id, $concours_id = NULL) |
---|
393 | { |
---|
394 | $criteria_list = array(); |
---|
395 | if ($concours_id!== null or $this->concours_id !== null) |
---|
396 | { |
---|
397 | $query = ' |
---|
398 | SELECT * |
---|
399 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
400 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
401 | AND uppercriteria = '.$criteria_id.' |
---|
402 | ORDER BY criteria_id |
---|
403 | '; |
---|
404 | $result = pwg_query($query); |
---|
405 | if ($this->debug) echo $query."\n"; |
---|
406 | while ($row = mysql_fetch_assoc($result)) |
---|
407 | { |
---|
408 | array_push($criteria_list, $row); |
---|
409 | } |
---|
410 | return $criteria_list; |
---|
411 | } |
---|
412 | else |
---|
413 | return $criteria_list; |
---|
414 | } |
---|
415 | |
---|
416 | // Get a detail from a criteria |
---|
417 | function get_criteria($criteria_id, $concours_id = NULL) |
---|
418 | { |
---|
419 | $criteria = array(); |
---|
420 | |
---|
421 | if ($concours_id!== null or $this->concours_id !== null) |
---|
422 | { |
---|
423 | $query = ' |
---|
424 | SELECT * |
---|
425 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
426 | WHERE id_concours = '. ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
427 | AND criteria_id =' . $criteria_id . ' |
---|
428 | '; |
---|
429 | $result = pwg_query($query); |
---|
430 | if ($this->debug) echo $query."\n"; |
---|
431 | $criteria = mysql_fetch_assoc($result); |
---|
432 | /* while ($row = mysql_fetch_assoc($result)) |
---|
433 | { |
---|
434 | array_push($criteria, $row); |
---|
435 | } |
---|
436 | */ |
---|
437 | } |
---|
438 | return $criteria; |
---|
439 | |
---|
440 | } |
---|
441 | |
---|
442 | // Get a detail from a criteria |
---|
443 | function get_criteria_by_id($id) |
---|
444 | { |
---|
445 | $criteria = array(); |
---|
446 | |
---|
447 | $query = ' |
---|
448 | SELECT * |
---|
449 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
450 | WHERE id =' . $id . ' |
---|
451 | '; |
---|
452 | $result = pwg_query($query); |
---|
453 | if ($this->debug) echo $query."\n"; |
---|
454 | $criteria = mysql_fetch_assoc($result); |
---|
455 | /* while ($row = mysql_fetch_assoc($result)) |
---|
456 | { |
---|
457 | array_push($criteria, $row); |
---|
458 | } |
---|
459 | */ |
---|
460 | return $criteria; |
---|
461 | |
---|
462 | } |
---|
463 | |
---|
464 | // Add a criteria to a concours |
---|
465 | // Datas is an array and contains all infos. |
---|
466 | // Return the criteria id |
---|
467 | function add_criteria($datas, $concours_id = NULL) |
---|
468 | { |
---|
469 | // determines the criteria_id |
---|
470 | if ($concours_id!== null or $this->concours_id !== null) |
---|
471 | { |
---|
472 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id |
---|
473 | FROM ' . CONCOURS_DETAIL_TABLE . ' ;'; |
---|
474 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
475 | $query = 'SELECT IF(MAX(criteria_id)+1 IS NULL, 1, MAX(criteria_id)+1) AS next_criteria_id |
---|
476 | FROM ' . CONCOURS_DETAIL_TABLE . ' |
---|
477 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) . ' ;'; |
---|
478 | list($next_criteria_id) = mysql_fetch_array(pwg_query($query)); |
---|
479 | |
---|
480 | $query = ' |
---|
481 | INSERT INTO ' . CONCOURS_DETAIL_TABLE .' |
---|
482 | (`id`, |
---|
483 | `id_concours`, |
---|
484 | `criteria_id`, |
---|
485 | `name`, |
---|
486 | `descr`, |
---|
487 | `min_value`, |
---|
488 | `max_value`, |
---|
489 | `ponderation`, |
---|
490 | `uppercriteria`) |
---|
491 | VALUES ( |
---|
492 | '.$next_element_id.', '.($concours_id !== null ? $concours_id : $this->concours_id ).', |
---|
493 | '.$next_criteria_id.', "'.$datas['name'].'", "'.$datas['descr'].'", '.$datas['min_value'].' |
---|
494 | , '.$datas['max_value'].', '.$datas['ponderation'].' |
---|
495 | , '.($datas['uppercriteria'] ? $datas['uppercriteria'] : 'NULL').' |
---|
496 | );'; |
---|
497 | |
---|
498 | pwg_query($query); |
---|
499 | if ($this->debug) echo $query."\n"; |
---|
500 | |
---|
501 | } |
---|
502 | } |
---|
503 | |
---|
504 | // Update a criteria to a concours |
---|
505 | // Datas is an array and contains all infos. |
---|
506 | function update_criteria($datas, $concours_id = NULL) |
---|
507 | { |
---|
508 | // determines the criteria_id |
---|
509 | if ($concours_id!== null or $this->concours_id !== null) |
---|
510 | { |
---|
511 | $query = ' |
---|
512 | UPDATE ' . CONCOURS_DETAIL_TABLE .' |
---|
513 | SET |
---|
514 | name = "'.$datas['name'].'", |
---|
515 | descr = "'.$datas['descr'].'", |
---|
516 | min_value = '.$datas['min_value'].', |
---|
517 | max_value = '.$datas['max_value'].', |
---|
518 | ponderation = '.$datas['ponderation'].', |
---|
519 | uppercriteria = '.($datas['uppercriteria'] ? $datas['uppercriteria'] : 'NULL').' |
---|
520 | WHERE id = '.$datas['id'].';'; |
---|
521 | |
---|
522 | pwg_query($query); |
---|
523 | |
---|
524 | if ($this->debug) echo $query."\n"; |
---|
525 | |
---|
526 | } |
---|
527 | } |
---|
528 | |
---|
529 | |
---|
530 | // Delete a criteria from a concours |
---|
531 | function delete_criteria($criteria_id, $concours_id = NULL) |
---|
532 | { |
---|
533 | if ($concours_id!== null or $this->concours_id !== null) |
---|
534 | { |
---|
535 | |
---|
536 | $query = ' |
---|
537 | DELETE |
---|
538 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
539 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
540 | AND criteria_id = '.$criteria_id.' |
---|
541 | '; |
---|
542 | $result = pwg_query($query); |
---|
543 | if ($this->debug) echo $query."\n"; |
---|
544 | if($result) |
---|
545 | return true; |
---|
546 | else |
---|
547 | return false; |
---|
548 | } |
---|
549 | else |
---|
550 | return null; |
---|
551 | |
---|
552 | } |
---|
553 | |
---|
554 | // Delete a criteria (with id) from a concours |
---|
555 | function delete_criteria_by_id($id) |
---|
556 | { |
---|
557 | |
---|
558 | $query = ' |
---|
559 | DELETE |
---|
560 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
561 | WHERE id =' . $id . ' |
---|
562 | '; |
---|
563 | $result = pwg_query($query); |
---|
564 | if ($this->debug) echo $query."\n"; |
---|
565 | |
---|
566 | } |
---|
567 | |
---|
568 | // check if a result is already present in the database |
---|
569 | function is_result_present($concours_id = null) |
---|
570 | { |
---|
571 | // recover all img_id from the category |
---|
572 | $query = 'SELECT DISTINCT(id_concours)' |
---|
573 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
574 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).';'; |
---|
575 | |
---|
576 | $result = pwg_query($query); |
---|
577 | // For each images |
---|
578 | if (mysql_fetch_assoc($result)) |
---|
579 | return true; |
---|
580 | else |
---|
581 | return false; |
---|
582 | |
---|
583 | } |
---|
584 | |
---|
585 | |
---|
586 | // After concours is completed (closed date is passed), generate the result and store it to DB |
---|
587 | function create_result($concours_id = NULL) |
---|
588 | { |
---|
589 | // var which contains id=img_id and val =global note |
---|
590 | $global_note = array(); |
---|
591 | if ($concours_id!== null or $this->concours_id !== null) |
---|
592 | { |
---|
593 | // only for closed concours and not already result generated |
---|
594 | if ($this->is_closed($concours_id) AND ! $this->is_result_present($concours_id)) |
---|
595 | { |
---|
596 | $user_id = array(); |
---|
597 | // vars not initialized |
---|
598 | if ($this->concours_infos == array()) |
---|
599 | $this->get_concours(); |
---|
600 | $category = $this->concours_infos['category']; |
---|
601 | |
---|
602 | if ($this->debug) |
---|
603 | echo "CAT=".$category; |
---|
604 | // Get all user_id from a concours |
---|
605 | $query = 'SELECT DISTINCT(user_id)' |
---|
606 | .' FROM ' .CONCOURS_DATA_TABLE |
---|
607 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
608 | .';'; |
---|
609 | $result = pwg_query($query); |
---|
610 | if ($this->debug) |
---|
611 | echo $query; |
---|
612 | while ($row = mysql_fetch_assoc($result)) |
---|
613 | { |
---|
614 | array_push($user_id, $row['user_id']); |
---|
615 | } |
---|
616 | |
---|
617 | if ($this->debug) |
---|
618 | foreach ($user_id as $userid) |
---|
619 | echo "USER = ".$userid; |
---|
620 | |
---|
621 | |
---|
622 | |
---|
623 | // recover all img_id from the category |
---|
624 | $query = 'SELECT DISTINCT(image_id)' |
---|
625 | .' FROM ' .IMAGE_CATEGORY_TABLE |
---|
626 | .' WHERE category_id = '.$category.';'; |
---|
627 | |
---|
628 | $result = pwg_query($query); |
---|
629 | // For each images |
---|
630 | while ($row = mysql_fetch_assoc($result)) |
---|
631 | { |
---|
632 | if ($this->debug) echo "IMG_ID=".$row['image_id']."\n"; |
---|
633 | foreach ($user_id as $i => $userid) |
---|
634 | { |
---|
635 | if (!isset($global_note[$row['image_id']])) |
---|
636 | $global_note[$row['image_id']] = 0; |
---|
637 | $global_note[$row['image_id']] += $this->get_img_globalnote_user($row['image_id'], null, $userid ); |
---|
638 | } |
---|
639 | } |
---|
640 | if ($this->debug) |
---|
641 | { |
---|
642 | foreach ($global_note as $id => $val) |
---|
643 | echo "Note img ( ".$id.") = ".$val."\n"; |
---|
644 | } |
---|
645 | |
---|
646 | // update database and store result into RESULT table |
---|
647 | // note = global note |
---|
648 | foreach ($global_note as $id => $val) |
---|
649 | { |
---|
650 | $query = 'INSERT INTO ' . CONCOURS_RESULT_TABLE .'(`id_concours`, `img_id`, `date` , `note` ) |
---|
651 | VALUES ('.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
652 | .', '.$id |
---|
653 | .', now() ' |
---|
654 | .', '.$val |
---|
655 | .');'; |
---|
656 | $result = pwg_query($query); |
---|
657 | |
---|
658 | |
---|
659 | } |
---|
660 | |
---|
661 | |
---|
662 | |
---|
663 | } |
---|
664 | else |
---|
665 | return false; |
---|
666 | } |
---|
667 | else |
---|
668 | return false; |
---|
669 | } |
---|
670 | |
---|
671 | |
---|
672 | // Check if an image's author is the same as the actual user |
---|
673 | function check_img_user($img_id) |
---|
674 | { |
---|
675 | global $user; |
---|
676 | $query = ' |
---|
677 | SELECT author |
---|
678 | FROM ' . IMAGES_TABLE .' |
---|
679 | WHERE id =' . $img_id . ' |
---|
680 | '; |
---|
681 | |
---|
682 | $result = pwg_query($query); |
---|
683 | if ($this->debug) echo $query."\n"; |
---|
684 | if ($result) |
---|
685 | { |
---|
686 | $row = mysql_fetch_assoc($result); |
---|
687 | $authorid = get_userid($row['author']); |
---|
688 | if ($authorid and ($authorid == $user['id'])) |
---|
689 | return true; |
---|
690 | else |
---|
691 | return false; |
---|
692 | } |
---|
693 | return false; |
---|
694 | |
---|
695 | } |
---|
696 | |
---|
697 | |
---|
698 | // Add tpl to picture.php page to display concours informations and vote |
---|
699 | function display_concours_to_picture() |
---|
700 | { |
---|
701 | // Step1 : concours is defined to this category AND concours is open AND user is authorized to access to this category (thru group) |
---|
702 | // Step1 bis : dont show concours if img_author = user_id |
---|
703 | // Step 2 : Recover stored informations in db for this user |
---|
704 | // Step 3 : Complete tpl information |
---|
705 | // Step 4 : concat tpl |
---|
706 | |
---|
707 | global $page, $user, $template; |
---|
708 | |
---|
709 | // Get user group. |
---|
710 | $this->get_user_groups(); |
---|
711 | |
---|
712 | $concours = array(); |
---|
713 | |
---|
714 | |
---|
715 | // DEBUG |
---|
716 | if ($this->debug) |
---|
717 | foreach ($page as $id=>$val) |
---|
718 | { |
---|
719 | if (is_array($val)) |
---|
720 | foreach ($val as $id2=>$val2) |
---|
721 | echo "page[".$id."][".$id2."]=".$val2."\n"; |
---|
722 | else |
---|
723 | echo "page[".$id."]=".$val."\n"; |
---|
724 | } |
---|
725 | // END DEBUG |
---|
726 | if (($page['section']) == 'categories' AND !empty($page['category'])) |
---|
727 | { |
---|
728 | if ($this->debug) echo "STEP1\n"; |
---|
729 | //------------ |
---|
730 | // Step 1 |
---|
731 | $query = ' |
---|
732 | SELECT * |
---|
733 | FROM ' . CONCOURS_TABLE .' |
---|
734 | WHERE category =' . $page['category']['id'] . ' |
---|
735 | AND time_to_sec(TIMEDIFF(begin_date,now())) < 0 |
---|
736 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
737 | '; |
---|
738 | |
---|
739 | $result = pwg_query($query); |
---|
740 | if ($this->debug) echo $query."\n"; |
---|
741 | while ($row = mysql_fetch_assoc($result)) |
---|
742 | { |
---|
743 | |
---|
744 | if (!empty($row['groups'])) |
---|
745 | { |
---|
746 | if ($this->debug) echo "GROUPS=".$row['groups']; |
---|
747 | $authorized_groups = explode(',', $row['groups']); |
---|
748 | if (array_intersect($this->user_groups, $authorized_groups) == array()) |
---|
749 | { |
---|
750 | if ($this->debug) echo "ERROR GROUPS"; |
---|
751 | continue; |
---|
752 | } |
---|
753 | $concours = $row; |
---|
754 | } |
---|
755 | |
---|
756 | } |
---|
757 | |
---|
758 | if ($this->debug) echo "STEP1 bis\n"; |
---|
759 | //------------ |
---|
760 | // Step 1 bis |
---|
761 | // Actual user is the author of the picture ==>end |
---|
762 | if ($this->check_img_user($page['current_item'])) |
---|
763 | return; |
---|
764 | |
---|
765 | |
---|
766 | if ($this->debug) echo "STEP2\n"; |
---|
767 | //------------ |
---|
768 | // Step 2 |
---|
769 | if ($concours != array()) |
---|
770 | { |
---|
771 | // If user validate the notation |
---|
772 | if (isset($_POST['concours_submit'])) |
---|
773 | { |
---|
774 | $user_note = ""; |
---|
775 | if ($this->debug) echo "SUBMIT"; |
---|
776 | // concat all the notes to save on db |
---|
777 | |
---|
778 | $firstcriterias = $this->get_firstlevel_criterias($concours['id']); |
---|
779 | foreach ($firstcriterias as $criteria) |
---|
780 | { |
---|
781 | |
---|
782 | // First without sub criterias |
---|
783 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] )) |
---|
784 | { |
---|
785 | // Check value |
---|
786 | $value = str_replace(",",".",$_POST[$criteria['criteria_id']]); |
---|
787 | $value = str_replace(" ","",$value); |
---|
788 | $value = floatval($value); |
---|
789 | if ($value < floatval($criteria['min_value'])) |
---|
790 | $value = floatval($criteria['min_value']); |
---|
791 | if ($value > floatval($criteria['max_value'])) |
---|
792 | $value = floatval($criteria['max_value']); |
---|
793 | |
---|
794 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$criteria['criteria_id']."=".$value; |
---|
795 | } |
---|
796 | else |
---|
797 | { |
---|
798 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours['id'] ); |
---|
799 | foreach ($secondcriterias as $subcriteria) |
---|
800 | { |
---|
801 | // Check value |
---|
802 | $value = str_replace(",",".",$_POST[$subcriteria['criteria_id']]); |
---|
803 | $value = str_replace(" ","",$value); |
---|
804 | $value = floatval($value); |
---|
805 | if ($value < floatval($subcriteria['min_value'])) |
---|
806 | $value = floatval($subcriteria['min_value']); |
---|
807 | if ($value > floatval($subcriteria['max_value'])) |
---|
808 | $value = floatval($subcriteria['max_value']); |
---|
809 | |
---|
810 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$subcriteria['criteria_id']."=".$value; |
---|
811 | } |
---|
812 | } |
---|
813 | } |
---|
814 | |
---|
815 | // $datas = "0=0"; |
---|
816 | $this->store_img_note_user($page['current_item'], $user_note, $concours['id']); |
---|
817 | if ($this->debug) echo "COMMENT=".$_POST['CONCOURS_COMMENT']; |
---|
818 | $this->store_img_comment_user($page['current_item'], $_POST['concours_comment'], $concours['id']); |
---|
819 | } |
---|
820 | |
---|
821 | // If user want to erase notes |
---|
822 | if (isset($_POST['concours_raz'])) |
---|
823 | { |
---|
824 | if ($this->debug) echo "RAZ"; |
---|
825 | $this->RAZ_img_note_user($page['current_item'], $concours['id']); |
---|
826 | } |
---|
827 | else |
---|
828 | { |
---|
829 | // Recover previous note in DB (if exists) |
---|
830 | $user_notes = $this->get_img_note_user($page['current_item'], $concours['id']); |
---|
831 | } |
---|
832 | |
---|
833 | // Comment is not RAZ, always restore |
---|
834 | $comment = $this->get_img_comment_user($page['current_item'], $concours['id']); |
---|
835 | |
---|
836 | if ($this->debug) echo "STEP3\n"; |
---|
837 | //------------ |
---|
838 | // Step 3 |
---|
839 | // Recover all 1st level criterias |
---|
840 | $firstcriterias = $this->get_firstlevel_criterias($concours['id']); |
---|
841 | foreach ($firstcriterias as $criteria) |
---|
842 | { |
---|
843 | if ($this->debug) echo "criteriaID=".$criteria['criteria_id']."\n"; |
---|
844 | // First without sub criterias |
---|
845 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] )) |
---|
846 | { |
---|
847 | $template->append( 'concours_criteria', array( |
---|
848 | 'nosub' => true, |
---|
849 | 'level' => 1, |
---|
850 | 'id' => $criteria['criteria_id'], // id du critere |
---|
851 | 'name' => $criteria['name'], // id du critere |
---|
852 | 'lib' => $criteria['descr'], //.'(min='$criteria['min_value'].';max='.$criteria['min_value'].')', // libelle du critrer |
---|
853 | 'val' => (isset($user_notes[$criteria['criteria_id']])?$user_notes[$criteria['criteria_id']] : $criteria['min_value']), // valeur du critere |
---|
854 | 'min' => $criteria['min_value'], // min |
---|
855 | 'max' => $criteria['max_value'] // max |
---|
856 | )); |
---|
857 | } |
---|
858 | else |
---|
859 | { |
---|
860 | $template->append( 'concours_criteria', array( |
---|
861 | 'nosub' => false, |
---|
862 | 'level' => 1, |
---|
863 | 'id' => $criteria['criteria_id'], // id du critere |
---|
864 | 'name' => $criteria['name'], // id du critere |
---|
865 | 'lib' => $criteria['descr'] |
---|
866 | )); |
---|
867 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours['id'] ); |
---|
868 | foreach ($secondcriterias as $subcriteria) |
---|
869 | { |
---|
870 | if ($this->debug) echo "subcriteriaID=".$criteria['criteria_id']."\n"; |
---|
871 | $template->append( 'concours_criteria', array( |
---|
872 | 'nosub' => true, |
---|
873 | 'level' => 2, |
---|
874 | 'id' => $subcriteria['criteria_id'], // id du critere |
---|
875 | 'name' => $subcriteria['name'], // id du critere |
---|
876 | 'lib' => $subcriteria['descr'], //.'(min='$criteria['min_value'].';max='.$criteria['min_value'].')', // libelle du critrer |
---|
877 | 'val' => (isset($user_notes[$subcriteria['criteria_id']])?$user_notes[$subcriteria['criteria_id']] : $subcriteria['min_value']), |
---|
878 | 'min' => $subcriteria['min_value'], // min |
---|
879 | 'max' => $subcriteria['max_value'] // max |
---|
880 | )); |
---|
881 | |
---|
882 | } |
---|
883 | } |
---|
884 | |
---|
885 | } |
---|
886 | // Add the comment |
---|
887 | $template->assign( 'CONCOURS_COMMENT', $comment); |
---|
888 | |
---|
889 | // Add concours informations on template |
---|
890 | $template->assign( 'concours_infos', array( |
---|
891 | 'name' => $concours['name'], |
---|
892 | 'descr' => $concours['descr'] |
---|
893 | )); |
---|
894 | |
---|
895 | |
---|
896 | $template->set_filenames(array('concours' => CONCOURS_ROOT.'/template/concours.tpl')); |
---|
897 | // $template->concat('PLUGIN_PICTURE_AFTER', $template->parse('concours', true)); |
---|
898 | $template->concat('COMMENT_IMG', $template->parse('concours', true)); |
---|
899 | |
---|
900 | // |
---|
901 | $template->assign('INFO_AUTHOR',l10n('concours_img_author')); |
---|
902 | } |
---|
903 | } |
---|
904 | } |
---|
905 | |
---|
906 | // RAZ notation from user to db |
---|
907 | // fill criterias notes to 0 in DB |
---|
908 | // return false if error |
---|
909 | function RAZ_img_note_user($img_id, $concours_id = NULL) |
---|
910 | { |
---|
911 | $user_note = ""; |
---|
912 | if ($concours_id === null) |
---|
913 | if ($this->concours_id !== null) |
---|
914 | $concours_id = $this->concours_id; |
---|
915 | else |
---|
916 | return false; |
---|
917 | |
---|
918 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
919 | foreach ($firstcriterias as $criteria) |
---|
920 | { |
---|
921 | if ($this->debug) echo "ID=".$criteria['criteria_id']."\n"; |
---|
922 | // First without sub criterias |
---|
923 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
924 | { |
---|
925 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$criteria['criteria_id']."=".$criteria['min_value']; |
---|
926 | } |
---|
927 | else |
---|
928 | { |
---|
929 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
930 | foreach ($secondcriterias as $subcriteria) |
---|
931 | { |
---|
932 | if ($this->debug) echo "ID=".$criteria['criteria_id']."\n"; |
---|
933 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$subcriteria['criteria_id']."=".$subcriteria['min_value']; |
---|
934 | } |
---|
935 | } |
---|
936 | } |
---|
937 | if ($this->debug) echo "RAZ=>".$user_note."\n"; |
---|
938 | if (strlen($user_note) != 0) |
---|
939 | { |
---|
940 | $this->store_img_note_user($img_id, $user_note, $concours_id); |
---|
941 | return true; |
---|
942 | } |
---|
943 | else |
---|
944 | return false; |
---|
945 | |
---|
946 | |
---|
947 | } |
---|
948 | |
---|
949 | // Store notation from user to db |
---|
950 | // save with format "criteria_id=note;criteria_id=note..." for data |
---|
951 | function store_img_note_user($img_id, $datas, $concours_id = NULL) |
---|
952 | { |
---|
953 | global $user; |
---|
954 | if ($this->get_img_note_user( $img_id, $concours_id) == array()) |
---|
955 | { |
---|
956 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id FROM ' . CONCOURS_DATA_TABLE . ' ;'; |
---|
957 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
958 | if ($this->debug) echo "Next_element=".$next_element_id."\n"; |
---|
959 | $query = ' |
---|
960 | INSERT INTO ' . CONCOURS_DATA_TABLE .' |
---|
961 | (`id`, |
---|
962 | `id_concours` , |
---|
963 | `user_id` , |
---|
964 | `create_date` , |
---|
965 | `img_id` , |
---|
966 | `datas` ) |
---|
967 | VALUES ( '.$next_element_id.',' |
---|
968 | .($concours_id !== null ? $concours_id : $this->concours_id ) .' |
---|
969 | , '. $user['id'] .', now(), '.$img_id.', "'.$datas.'");'; |
---|
970 | } |
---|
971 | else |
---|
972 | { |
---|
973 | $query = ' |
---|
974 | UPDATE ' . CONCOURS_DATA_TABLE .' |
---|
975 | SET datas = "'.$datas.'", create_date = now() |
---|
976 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).' |
---|
977 | AND user_id = '.$user['id'].' |
---|
978 | AND img_id = '.$img_id.';'; |
---|
979 | } |
---|
980 | if ($this->debug) echo $query."\n"; |
---|
981 | pwg_query($query); |
---|
982 | |
---|
983 | } |
---|
984 | |
---|
985 | // get notation's user from db |
---|
986 | // Return an array with : |
---|
987 | // Criteria_id ==> value |
---|
988 | // Note : criterias with subcriterias are not saved and not restored. They are calculated for global result |
---|
989 | function get_img_note_user($img_id, $concours_id = NULL, $user_id = null) |
---|
990 | { |
---|
991 | global $user; |
---|
992 | $img_note = array(); |
---|
993 | if ($concours_id!== null or $this->concours_id !== null) |
---|
994 | { |
---|
995 | $query = ' |
---|
996 | SELECT datas |
---|
997 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
998 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
999 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1000 | AND img_id = '. $img_id .' |
---|
1001 | '; |
---|
1002 | if ($this->debug) echo $query."\n"; |
---|
1003 | $result = pwg_query($query); |
---|
1004 | |
---|
1005 | while ($row = mysql_fetch_assoc($result)) |
---|
1006 | { |
---|
1007 | if ($this->debug) echo "row[data]=".$row['datas']."\n"; |
---|
1008 | $datas = explode(";",$row['datas']); |
---|
1009 | foreach ($datas as $val) |
---|
1010 | { |
---|
1011 | if (strpos($val, '=') !== FALSE) |
---|
1012 | $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1); |
---|
1013 | } |
---|
1014 | } |
---|
1015 | // DEBUG |
---|
1016 | if ($this->debug) |
---|
1017 | foreach ($img_note as $id=>$val) |
---|
1018 | { |
---|
1019 | if (is_array($val)) |
---|
1020 | foreach ($val as $id2=>$val2) |
---|
1021 | echo "img_note[".$id."][".$id2."]=".$val2."\n"; |
---|
1022 | else |
---|
1023 | echo "img_note[".$id."]=".$val."\n"; |
---|
1024 | } |
---|
1025 | // END DEBUG |
---|
1026 | |
---|
1027 | return $img_note; |
---|
1028 | } |
---|
1029 | else |
---|
1030 | return array(); |
---|
1031 | |
---|
1032 | |
---|
1033 | } |
---|
1034 | |
---|
1035 | |
---|
1036 | // get notation's user from db |
---|
1037 | // Return the global note for a picture id from a user |
---|
1038 | function get_img_globalnote_user($img_id, $concours_id = NULL, $user_id = null) |
---|
1039 | { |
---|
1040 | global $user; |
---|
1041 | $img_note = array(); |
---|
1042 | $global_user_note = 0; |
---|
1043 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1044 | { |
---|
1045 | $query = ' |
---|
1046 | SELECT datas |
---|
1047 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1048 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
1049 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1050 | AND img_id = '. $img_id .' |
---|
1051 | '; |
---|
1052 | $result = pwg_query($query); |
---|
1053 | |
---|
1054 | while ($row = mysql_fetch_assoc($result)) |
---|
1055 | { |
---|
1056 | $datas = explode(";",$row['datas']); |
---|
1057 | foreach ($datas as $val) |
---|
1058 | { |
---|
1059 | if (strpos($val, '=') !== FALSE) |
---|
1060 | $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1); |
---|
1061 | } |
---|
1062 | } |
---|
1063 | |
---|
1064 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1065 | foreach ($firstcriterias as $criteria) |
---|
1066 | { |
---|
1067 | if ($this->debug) echo "criteriaID=".$criteria['criteria_id']."\n"; |
---|
1068 | // First without sub criterias |
---|
1069 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1070 | { |
---|
1071 | if (isset($img_note[$criteria['criteria_id']])) |
---|
1072 | $global_user_note += (int)$criteria['ponderation'] * (float)$img_note[$criteria['criteria_id']]; |
---|
1073 | } |
---|
1074 | else |
---|
1075 | { |
---|
1076 | |
---|
1077 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1078 | foreach ($secondcriterias as $subcriteria) |
---|
1079 | { |
---|
1080 | if ($this->debug) echo "subcriteriaID=".$subcriteria['criteria_id']."\n"; |
---|
1081 | if (isset($img_note[$subcriteria['criteria_id']])) |
---|
1082 | $global_user_note += (int)$subcriteria['ponderation'] * (float)$img_note[$subcriteria['criteria_id']]; |
---|
1083 | } |
---|
1084 | } |
---|
1085 | |
---|
1086 | } |
---|
1087 | |
---|
1088 | return $global_user_note; |
---|
1089 | } |
---|
1090 | else |
---|
1091 | return array(); |
---|
1092 | |
---|
1093 | |
---|
1094 | } |
---|
1095 | |
---|
1096 | // Store comment from user to db |
---|
1097 | function store_img_comment_user($img_id, $datas, $concours_id = NULL) |
---|
1098 | { |
---|
1099 | global $user; |
---|
1100 | if (($comment = $this->get_img_note_user( $img_id, $concours_id)) == false) |
---|
1101 | { |
---|
1102 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id FROM ' . CONCOURS_DATA_TABLE . ' ;'; |
---|
1103 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
1104 | if ($this->debug) echo "Next_element=".$next_element_id."\n"; |
---|
1105 | $query = ' |
---|
1106 | INSERT INTO ' . CONCOURS_DATA_TABLE .' |
---|
1107 | (`id`, |
---|
1108 | `id_concours` , |
---|
1109 | `user_id` , |
---|
1110 | `create_date` , |
---|
1111 | `img_id` , |
---|
1112 | `comment`) |
---|
1113 | VALUES ( '.$next_element_id.',' |
---|
1114 | .($concours_id !== null ? $concours_id : $this->concours_id ) .' |
---|
1115 | , '. $user['id'] .', now(), '.$img_id.', "'.$datas.'");'; |
---|
1116 | } |
---|
1117 | else |
---|
1118 | { |
---|
1119 | $query = ' |
---|
1120 | UPDATE ' . CONCOURS_DATA_TABLE .' |
---|
1121 | SET comment = "'.$datas.'", create_date = now() |
---|
1122 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).' |
---|
1123 | AND user_id = '.$user['id'].' |
---|
1124 | AND img_id = '.$img_id.';'; |
---|
1125 | } |
---|
1126 | if ($this->debug) echo $query."\n"; |
---|
1127 | pwg_query($query); |
---|
1128 | |
---|
1129 | } |
---|
1130 | |
---|
1131 | |
---|
1132 | // Get comment on an image for a user |
---|
1133 | function get_img_comment_user($img_id, $concours_id = NULL, $user_id = null) |
---|
1134 | { |
---|
1135 | global $user; |
---|
1136 | $datas = false; |
---|
1137 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1138 | { |
---|
1139 | $query = ' |
---|
1140 | SELECT comment |
---|
1141 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1142 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
1143 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1144 | AND img_id = '. $img_id .' |
---|
1145 | '; |
---|
1146 | if ($this->debug) echo $query."\n"; |
---|
1147 | $result = pwg_query($query); |
---|
1148 | |
---|
1149 | while ($row = mysql_fetch_assoc($result)) |
---|
1150 | { |
---|
1151 | if ($this->debug) echo "row[comment]=".$row['comment']."\n"; |
---|
1152 | $datas = $row['comment']; |
---|
1153 | } |
---|
1154 | } |
---|
1155 | return $datas; |
---|
1156 | } |
---|
1157 | |
---|
1158 | |
---|
1159 | // Generate csv file for a closed concours with result generated |
---|
1160 | function generate_csv($concours_id = NULL) |
---|
1161 | { |
---|
1162 | $file = "rang, id_concours, img_id, img_name, img_file, date, note, datas\r\n"; |
---|
1163 | |
---|
1164 | // recover all img_id from the category |
---|
1165 | $query = 'SELECT id_concours, img_id, IMG.name, IMG.file, date, note, datas' |
---|
1166 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1167 | .' INNER JOIN ' . IMAGES_TABLE.' AS IMG on IMG.id = img_id' |
---|
1168 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1169 | .' ORDER BY note DESC' |
---|
1170 | .';'; |
---|
1171 | |
---|
1172 | $result = pwg_query($query); |
---|
1173 | // For each images |
---|
1174 | $rang = 1; |
---|
1175 | while ($row = mysql_fetch_assoc($result)) |
---|
1176 | { |
---|
1177 | $file .= $rang.', ' |
---|
1178 | .$row['id_concours'].', ' |
---|
1179 | .$row['img_id'].', ' |
---|
1180 | .$row['name'].', ' |
---|
1181 | .$row['file'].', ' |
---|
1182 | .$row['date'].', ' |
---|
1183 | .$row['note'].', ' |
---|
1184 | .($row['datas'] != null ? $row['datas'] : '') |
---|
1185 | ."\r\n"; |
---|
1186 | $rang ++; |
---|
1187 | } |
---|
1188 | |
---|
1189 | return $file; |
---|
1190 | |
---|
1191 | |
---|
1192 | } |
---|
1193 | |
---|
1194 | // Generate csv file for a closed concours with result generated and all users informations |
---|
1195 | function generate_detail_csv($concours_id = NULL) |
---|
1196 | { |
---|
1197 | if ($concours_id === null) |
---|
1198 | $concours_id = $this->concours_id; |
---|
1199 | // Prepare the list of criteria |
---|
1200 | $criteria_list = ""; |
---|
1201 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1202 | $ident1 = 1; |
---|
1203 | foreach ($firstcriterias as $criteria) |
---|
1204 | { |
---|
1205 | // format (id:name) |
---|
1206 | $criteria_list .= (strlen($criteria_list) ? "," : "") |
---|
1207 | .$ident1.":".$criteria['name'] |
---|
1208 | ."(id=".$criteria['criteria_id'].")"; |
---|
1209 | // First wit sub criterias |
---|
1210 | if ($this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1211 | { |
---|
1212 | $ident2 = 1; |
---|
1213 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1214 | foreach ($secondcriterias as $subcriteria) |
---|
1215 | { |
---|
1216 | // format (id:name) |
---|
1217 | $criteria_list .= (strlen($criteria_list) ? "," : "") |
---|
1218 | .$ident1.".".$ident2 |
---|
1219 | .":".$subcriteria['name']."(id=".$subcriteria['criteria_id'].")"; |
---|
1220 | $ident2 ++; |
---|
1221 | } |
---|
1222 | } |
---|
1223 | $ident1++; |
---|
1224 | } |
---|
1225 | if ($this->debug) echo "CRITERIA_LIST=".$criteria_list."\n"; |
---|
1226 | $user_list = array(); |
---|
1227 | $users_criteria = ""; |
---|
1228 | // Get all the users who have notes for the concours |
---|
1229 | $query = 'SELECT distinct(user_id), USER.username' |
---|
1230 | .' FROM ' .CONCOURS_DATA_TABLE |
---|
1231 | .' INNER JOIN ' . USERS_TABLE.' AS USER on USER.id = user_id' |
---|
1232 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1233 | .' ORDER BY username ASC' |
---|
1234 | .';'; |
---|
1235 | $result = pwg_query($query); |
---|
1236 | // For each user |
---|
1237 | while ($row = mysql_fetch_assoc($result)) |
---|
1238 | { |
---|
1239 | array_push($user_list, $row); |
---|
1240 | $users_criteria .= (strlen($users_criteria) ? "," : "")."user, user_global_note, comment, ".$criteria_list; |
---|
1241 | |
---|
1242 | } |
---|
1243 | |
---|
1244 | if ($this->debug) echo "users_criteria=".$users_criteria."\n"; |
---|
1245 | |
---|
1246 | // All informations in csv format |
---|
1247 | $file = "rang, id_concours, nom_concours, date_debut, date_fin, date_export, img_id, img_name, img_file, img_author, note, nbvotant, datas," |
---|
1248 | .$users_criteria |
---|
1249 | ."\r\n"; |
---|
1250 | |
---|
1251 | // nb of users who vote for each image |
---|
1252 | $query = 'SELECT img_id, COUNT(DISTINCT user_id) AS NBVOTE FROM ' . CONCOURS_DATA_TABLE |
---|
1253 | .' GROUP BY img_id ' |
---|
1254 | .';'; |
---|
1255 | |
---|
1256 | $result = pwg_query($query); |
---|
1257 | // echo $query; |
---|
1258 | $nbvotes = array(); |
---|
1259 | while ($row = mysql_fetch_assoc($result)) |
---|
1260 | { |
---|
1261 | $nbvotes[$row['img_id']] = $row['NBVOTE']; |
---|
1262 | } |
---|
1263 | /* |
---|
1264 | foreach ($nbvotes as $id=>$val) |
---|
1265 | { |
---|
1266 | echo "Vote img(".$id.")=".$val."\n"; |
---|
1267 | } |
---|
1268 | */ |
---|
1269 | // recover all img_id from the category |
---|
1270 | $query = 'SELECT id_concours, img_id, IMG.name, IMG.file, IMG.author, date, note, datas, comment' |
---|
1271 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1272 | .' INNER JOIN ' . IMAGES_TABLE.' AS IMG on IMG.id = img_id' |
---|
1273 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1274 | .' ORDER BY note DESC' |
---|
1275 | .';'; |
---|
1276 | |
---|
1277 | $result = pwg_query($query); |
---|
1278 | // echo $query; |
---|
1279 | // For each images |
---|
1280 | $rang = 1; |
---|
1281 | while ($row = mysql_fetch_assoc($result)) |
---|
1282 | { |
---|
1283 | $file .= $rang.', ' |
---|
1284 | .$row['id_concours'].', ' |
---|
1285 | .str_replace(",", "",$this->concours_infos['name']).', ' |
---|
1286 | .$this->concours_infos['begin_date'].', ' |
---|
1287 | .$this->concours_infos['end_date'].', ' |
---|
1288 | .$row['date'].', ' |
---|
1289 | |
---|
1290 | .$row['img_id'].', ' |
---|
1291 | .str_replace(",", "",$row['name']).', ' |
---|
1292 | .$row['file'].', ' |
---|
1293 | .$row['author'].', ' |
---|
1294 | .$row['note'].', ' |
---|
1295 | .(isset($nbvotes[$row['img_id']]) ? $nbvotes[$row['img_id']] : 0).', ' |
---|
1296 | .($row['datas'] != null ? $row['datas'] : '') |
---|
1297 | ; |
---|
1298 | foreach ($user_list as $uuser) |
---|
1299 | { |
---|
1300 | |
---|
1301 | $user_note = $this->get_img_note_user($row['img_id'], $concours_id, $uuser['user_id']); |
---|
1302 | |
---|
1303 | $file .= ', '.$uuser['username'].', '.$this->get_img_globalnote_user($row['img_id'], $concours_id, $uuser['user_id']) |
---|
1304 | .', '.($row['comment'] != null ? str_replace(",", "",$row['comment']) : '') |
---|
1305 | .', ' |
---|
1306 | ; |
---|
1307 | |
---|
1308 | $user_note_by_crit = ""; |
---|
1309 | foreach ($firstcriterias as $criteria) |
---|
1310 | { |
---|
1311 | // First without sub criterias |
---|
1312 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1313 | { |
---|
1314 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").(isset($user_note[$criteria['criteria_id']]) ? $user_note[$criteria['criteria_id']] : ' '); |
---|
1315 | } |
---|
1316 | else |
---|
1317 | { |
---|
1318 | $user_note_by_subcrit = ""; |
---|
1319 | $subcrit_note = 0; |
---|
1320 | $user_has_vote = false; |
---|
1321 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1322 | foreach ($secondcriterias as $subcriteria) |
---|
1323 | { |
---|
1324 | if (isset($user_note[$subcriteria['criteria_id']])) $user_has_vote = true; |
---|
1325 | $user_note_by_subcrit .= (strlen($user_note_by_subcrit) ? "," : "").(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : ' '); |
---|
1326 | $subcrit_note += (int)$subcriteria['ponderation'] * (float)(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : 0); |
---|
1327 | |
---|
1328 | } |
---|
1329 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").($user_has_vote ? $subcrit_note : ' ').",".$user_note_by_subcrit; |
---|
1330 | } |
---|
1331 | } |
---|
1332 | $file .= $user_note_by_crit; |
---|
1333 | |
---|
1334 | } |
---|
1335 | |
---|
1336 | $file .="\r\n"; |
---|
1337 | $rang ++; |
---|
1338 | } |
---|
1339 | // echo $file; |
---|
1340 | return $file; |
---|
1341 | |
---|
1342 | |
---|
1343 | } |
---|
1344 | |
---|
1345 | |
---|
1346 | |
---|
1347 | // Save csv datas to physical file |
---|
1348 | function save_file($file,$concours_id = NULL) |
---|
1349 | { |
---|
1350 | // check if already saved file |
---|
1351 | if ($filename = $this->get_file_name($concours_id)) |
---|
1352 | return $filename; |
---|
1353 | else |
---|
1354 | { |
---|
1355 | $dirpath = CONCOURS_RESULT_FOLDER; |
---|
1356 | |
---|
1357 | $filename = date('Ymd')."_concours".($concours_id !== null ? $concours_id : $this->concours_id ).".csv"; |
---|
1358 | if (!is_dir($dirpath)) |
---|
1359 | @mkdir($dirpath); |
---|
1360 | if (file_exists($dirpath. $filename)) |
---|
1361 | unlink($dirpath. $filename); |
---|
1362 | |
---|
1363 | $fh = fopen($dirpath. $filename, 'w') or die("can't open file"); |
---|
1364 | fwrite($fh, $file); |
---|
1365 | fclose($fh); |
---|
1366 | |
---|
1367 | //update result database with filename |
---|
1368 | $query = "UPDATE " . CONCOURS_RESULT_TABLE . " |
---|
1369 | SET |
---|
1370 | file_name = \"".$filename."\" |
---|
1371 | WHERE id_concours= ".($concours_id !== NULL ? $concours_id : $this->concours_id)." |
---|
1372 | ;"; |
---|
1373 | pwg_query($query); |
---|
1374 | // echo $query; |
---|
1375 | |
---|
1376 | return $filename; |
---|
1377 | } |
---|
1378 | } |
---|
1379 | |
---|
1380 | // Get result file name from db |
---|
1381 | function get_file_name($concours_id = NULL) |
---|
1382 | { |
---|
1383 | $query = 'SELECT distinct(file_name)' |
---|
1384 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1385 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1386 | .';'; |
---|
1387 | $row = mysql_fetch_assoc(pwg_query($query)); |
---|
1388 | if ($row) |
---|
1389 | return $row['file_name']; |
---|
1390 | else |
---|
1391 | return false; |
---|
1392 | |
---|
1393 | } |
---|
1394 | |
---|
1395 | // Get csv datas from a saved file |
---|
1396 | function get_file($concours_id = NULL) |
---|
1397 | { |
---|
1398 | $filename = CONCOURS_RESULT_FOLDER. |
---|
1399 | $size = filesize($filename); |
---|
1400 | header('Content-Type: application/octet-stream'); |
---|
1401 | header('Content-Length: '.$size); |
---|
1402 | header('Content-Disposition: attachment; filename='.$filename); |
---|
1403 | header('Content-Transfer-Encoding: binary'); |
---|
1404 | |
---|
1405 | $file = @fopen($filename, ‘rb’); |
---|
1406 | if ($file) { |
---|
1407 | // stream the file and exit the script when complete |
---|
1408 | fpassthru($file); |
---|
1409 | } |
---|
1410 | |
---|
1411 | } |
---|
1412 | |
---|
1413 | // Activation page administration |
---|
1414 | function concours_admin_menu($menu) |
---|
1415 | { |
---|
1416 | array_push($menu, |
---|
1417 | array( |
---|
1418 | 'NAME' => CONCOURS_NAME, |
---|
1419 | 'URL' => get_admin_plugin_menu_link(CONCOURS_ADMIN_PATH.'admin.php') |
---|
1420 | ) |
---|
1421 | ); |
---|
1422 | return $menu; |
---|
1423 | } |
---|
1424 | |
---|
1425 | // register Concours Menubar |
---|
1426 | public function register_blocks( $menu_ref_arr ) |
---|
1427 | { |
---|
1428 | $menu = & $menu_ref_arr[0]; |
---|
1429 | if ($menu->get_id() != 'menubar' OR $this->my_config['active_menubar'] == false) |
---|
1430 | return; |
---|
1431 | $menu->register_block( new RegisteredBlock( 'CONCOURS_menu', 'Polls', 'MYPOLLS')); |
---|
1432 | } |
---|
1433 | |
---|
1434 | /* |
---|
1435 | initialise menubar's menu |
---|
1436 | called by menubar object when making menu |
---|
1437 | */ |
---|
1438 | public function blockmanager_apply( $menu_ref_arr ) |
---|
1439 | { |
---|
1440 | |
---|
1441 | global $user, $template; |
---|
1442 | $menu = & $menu_ref_arr[0]; |
---|
1443 | |
---|
1444 | if ($this->user_groups == array()) |
---|
1445 | $this->get_user_groups(); |
---|
1446 | |
---|
1447 | if ( ($block = $menu->get_block( 'CONCOURS_menu' ) ) != null ) |
---|
1448 | { |
---|
1449 | if($user['id']==2) |
---|
1450 | { |
---|
1451 | $vote_id = $_SERVER['REMOTE_ADDR']; |
---|
1452 | } |
---|
1453 | else |
---|
1454 | { |
---|
1455 | $vote_id = $user['id']; |
---|
1456 | } |
---|
1457 | |
---|
1458 | // $polls_list=$this->get_polls_list($vote_id, is_admin(), $user['language']); |
---|
1459 | $menu_list=array(); |
---|
1460 | |
---|
1461 | if(is_admin()) |
---|
1462 | { |
---|
1463 | array_push($menu_list, |
---|
1464 | array( |
---|
1465 | 'nfo' => "", |
---|
1466 | 'text' => l10n('concours_admin_add'), |
---|
1467 | 'link' => PHPWG_ROOT_PATH . 'admin.php?page=plugin&section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&action=new', |
---|
1468 | 'edit' => "" |
---|
1469 | ) |
---|
1470 | ); |
---|
1471 | } |
---|
1472 | |
---|
1473 | // recover all img_id from the category |
---|
1474 | $query = 'SELECT distinct(id_concours), groups, CONC.name' |
---|
1475 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1476 | .' INNER JOIN '.CONCOURS_TABLE. ' AS CONC ON CONC.id = id_concours' |
---|
1477 | .' ORDER BY date DESC' |
---|
1478 | .';'; |
---|
1479 | |
---|
1480 | $result = pwg_query($query); |
---|
1481 | $nb_concours = 1; |
---|
1482 | // For each concours |
---|
1483 | while ($row = mysql_fetch_assoc($result)) |
---|
1484 | { |
---|
1485 | |
---|
1486 | if ($nb_concours > $this->my_config['nbconcours_menubar']) |
---|
1487 | break; |
---|
1488 | |
---|
1489 | if (!empty($row['groups'])) |
---|
1490 | { |
---|
1491 | $authorized_groups = explode(',', $row['groups']); |
---|
1492 | if (array_intersect($this->user_groups, $authorized_groups) == array()) |
---|
1493 | { |
---|
1494 | continue; |
---|
1495 | } |
---|
1496 | $nb_concours ++; |
---|
1497 | array_push($menu_list, |
---|
1498 | array( |
---|
1499 | 'nfo' => "", |
---|
1500 | 'text' => $row['name'], |
---|
1501 | // 'link' => CONCOURS_PATH.'publish.php?id='.$row['id_concours'], |
---|
1502 | 'link' => './index.php?/concours/'.$row['id_concours'], |
---|
1503 | 'edit' => (is_admin()? PHPWG_ROOT_PATH . 'admin.php?page=plugin&section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&concours=' . $row['id_concours'].'&action=modify':"") |
---|
1504 | ) |
---|
1505 | ); |
---|
1506 | } |
---|
1507 | } |
---|
1508 | |
---|
1509 | if (count($menu_list)) |
---|
1510 | { |
---|
1511 | |
---|
1512 | $block->set_title("Concours Photo"); |
---|
1513 | $block->template = CONCOURS_ROOT.'/template/concours_menu.tpl'; |
---|
1514 | $block->data = $menu_list; |
---|
1515 | } |
---|
1516 | } |
---|
1517 | } |
---|
1518 | |
---|
1519 | |
---|
1520 | function section_init_concours() |
---|
1521 | { |
---|
1522 | global $tokens, $page; |
---|
1523 | if ($tokens[0] == 'concours') |
---|
1524 | $page['section'] = 'concours'; |
---|
1525 | } |
---|
1526 | |
---|
1527 | function index_concours() |
---|
1528 | { |
---|
1529 | global $page; |
---|
1530 | |
---|
1531 | if (isset($page['section']) and $page['section'] == 'concours') |
---|
1532 | { |
---|
1533 | include(CONCOURS_PATH . 'publish.php'); |
---|
1534 | } |
---|
1535 | } |
---|
1536 | |
---|
1537 | |
---|
1538 | } |
---|
1539 | |
---|
1540 | ?> |
---|