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 | '.($datas['uppercriteria'] == false ? '' : 'uppercriteria = '.($datas['uppercriteria'] ? $datas['uppercriteria'].',' : 'NULL ,')).' |
---|
519 | ponderation = '.$datas['ponderation'].' |
---|
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 | |
---|
732 | if ($this->my_config['mask_author'] && $this->my_config['mask_author'] == true) |
---|
733 | { |
---|
734 | // Request for all concours prepared & actived |
---|
735 | $query = ' |
---|
736 | SELECT * |
---|
737 | FROM ' . CONCOURS_TABLE .' |
---|
738 | WHERE category =' . $page['category']['id'] . ' |
---|
739 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
740 | '; |
---|
741 | // echo $query; |
---|
742 | $result = pwg_query($query); |
---|
743 | if ($result && mysql_fetch_assoc($result)) |
---|
744 | $template->assign('INFO_AUTHOR',l10n('concours_img_author')); |
---|
745 | } |
---|
746 | |
---|
747 | |
---|
748 | $query = ' |
---|
749 | SELECT * |
---|
750 | FROM ' . CONCOURS_TABLE .' |
---|
751 | WHERE category =' . $page['category']['id'] . ' |
---|
752 | AND time_to_sec(TIMEDIFF(begin_date,now())) < 0 |
---|
753 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
754 | '; |
---|
755 | |
---|
756 | $result = pwg_query($query); |
---|
757 | if ($this->debug) echo $query."\n"; |
---|
758 | while ($row = mysql_fetch_assoc($result)) |
---|
759 | { |
---|
760 | |
---|
761 | if (!empty($row['groups'])) |
---|
762 | { |
---|
763 | if ($this->debug) echo "GROUPS=".$row['groups']; |
---|
764 | $authorized_groups = explode(',', $row['groups']); |
---|
765 | if (array_intersect($this->user_groups, $authorized_groups) == array()) |
---|
766 | { |
---|
767 | if ($this->debug) echo "ERROR GROUPS"; |
---|
768 | continue; |
---|
769 | } |
---|
770 | $concours = $row; |
---|
771 | } |
---|
772 | |
---|
773 | } |
---|
774 | |
---|
775 | if ($this->debug) echo "STEP1 bis\n"; |
---|
776 | //------------ |
---|
777 | // Step 1 bis |
---|
778 | // Actual user is the author of the picture ==>end |
---|
779 | if ($this->check_img_user($page['current_item'])) |
---|
780 | return; |
---|
781 | |
---|
782 | |
---|
783 | if ($this->debug) echo "STEP2\n"; |
---|
784 | //------------ |
---|
785 | // Step 2 |
---|
786 | if ($concours != array()) |
---|
787 | { |
---|
788 | // If user validate the notation |
---|
789 | if (isset($_POST['concours_submit'])) |
---|
790 | { |
---|
791 | $user_note = ""; |
---|
792 | if ($this->debug) echo "SUBMIT"; |
---|
793 | // concat all the notes to save on db |
---|
794 | |
---|
795 | $firstcriterias = $this->get_firstlevel_criterias($concours['id']); |
---|
796 | foreach ($firstcriterias as $criteria) |
---|
797 | { |
---|
798 | |
---|
799 | // First without sub criterias |
---|
800 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] )) |
---|
801 | { |
---|
802 | // Check value |
---|
803 | $value = str_replace(",",".",$_POST[$criteria['criteria_id']]); |
---|
804 | $value = str_replace(" ","",$value); |
---|
805 | $value = floatval($value); |
---|
806 | if ($value < floatval($criteria['min_value'])) |
---|
807 | $value = floatval($criteria['min_value']); |
---|
808 | if ($value > floatval($criteria['max_value'])) |
---|
809 | $value = floatval($criteria['max_value']); |
---|
810 | |
---|
811 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$criteria['criteria_id']."=".$value; |
---|
812 | } |
---|
813 | else |
---|
814 | { |
---|
815 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours['id'] ); |
---|
816 | foreach ($secondcriterias as $subcriteria) |
---|
817 | { |
---|
818 | // Check value |
---|
819 | $value = str_replace(",",".",$_POST[$subcriteria['criteria_id']]); |
---|
820 | $value = str_replace(" ","",$value); |
---|
821 | $value = floatval($value); |
---|
822 | if ($value < floatval($subcriteria['min_value'])) |
---|
823 | $value = floatval($subcriteria['min_value']); |
---|
824 | if ($value > floatval($subcriteria['max_value'])) |
---|
825 | $value = floatval($subcriteria['max_value']); |
---|
826 | |
---|
827 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$subcriteria['criteria_id']."=".$value; |
---|
828 | } |
---|
829 | } |
---|
830 | } |
---|
831 | |
---|
832 | // $datas = "0=0"; |
---|
833 | $this->store_img_note_user($page['current_item'], $user_note, $concours['id']); |
---|
834 | if ($this->debug) echo "COMMENT=".$_POST['CONCOURS_COMMENT']; |
---|
835 | $this->store_img_comment_user($page['current_item'], $_POST['concours_comment'], $concours['id']); |
---|
836 | } |
---|
837 | |
---|
838 | // If user want to erase notes |
---|
839 | if (isset($_POST['concours_raz'])) |
---|
840 | { |
---|
841 | if ($this->debug) echo "RAZ"; |
---|
842 | $this->RAZ_img_note_user($page['current_item'], $concours['id']); |
---|
843 | } |
---|
844 | else |
---|
845 | { |
---|
846 | // Recover previous note in DB (if exists) |
---|
847 | $user_notes = $this->get_img_note_user($page['current_item'], $concours['id']); |
---|
848 | } |
---|
849 | |
---|
850 | // Comment is not RAZ, always restore |
---|
851 | $comment = $this->get_img_comment_user($page['current_item'], $concours['id']); |
---|
852 | |
---|
853 | if ($this->debug) echo "STEP3\n"; |
---|
854 | //------------ |
---|
855 | // Step 3 |
---|
856 | // Recover all 1st level criterias |
---|
857 | $firstcriterias = $this->get_firstlevel_criterias($concours['id']); |
---|
858 | foreach ($firstcriterias as $criteria) |
---|
859 | { |
---|
860 | if ($this->debug) echo "criteriaID=".$criteria['criteria_id']."\n"; |
---|
861 | // First without sub criterias |
---|
862 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] )) |
---|
863 | { |
---|
864 | $template->append( 'concours_criteria', array( |
---|
865 | 'nosub' => true, |
---|
866 | 'level' => 1, |
---|
867 | 'id' => $criteria['criteria_id'], // id du critere |
---|
868 | 'name' => $criteria['name'], // id du critere |
---|
869 | 'lib' => $criteria['descr'], //.'(min='$criteria['min_value'].';max='.$criteria['min_value'].')', // libelle du critrer |
---|
870 | 'val' => (isset($user_notes[$criteria['criteria_id']])?$user_notes[$criteria['criteria_id']] : $criteria['min_value']), // valeur du critere |
---|
871 | 'min' => $criteria['min_value'], // min |
---|
872 | 'max' => $criteria['max_value'] // max |
---|
873 | )); |
---|
874 | } |
---|
875 | else |
---|
876 | { |
---|
877 | $template->append( 'concours_criteria', array( |
---|
878 | 'nosub' => false, |
---|
879 | 'level' => 1, |
---|
880 | 'id' => $criteria['criteria_id'], // id du critere |
---|
881 | 'name' => $criteria['name'], // id du critere |
---|
882 | 'lib' => $criteria['descr'] |
---|
883 | )); |
---|
884 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours['id'] ); |
---|
885 | foreach ($secondcriterias as $subcriteria) |
---|
886 | { |
---|
887 | if ($this->debug) echo "subcriteriaID=".$criteria['criteria_id']."\n"; |
---|
888 | $template->append( 'concours_criteria', array( |
---|
889 | 'nosub' => true, |
---|
890 | 'level' => 2, |
---|
891 | 'id' => $subcriteria['criteria_id'], // id du critere |
---|
892 | 'name' => $subcriteria['name'], // id du critere |
---|
893 | 'lib' => $subcriteria['descr'], //.'(min='$criteria['min_value'].';max='.$criteria['min_value'].')', // libelle du critrer |
---|
894 | 'val' => (isset($user_notes[$subcriteria['criteria_id']])?$user_notes[$subcriteria['criteria_id']] : $subcriteria['min_value']), |
---|
895 | 'min' => $subcriteria['min_value'], // min |
---|
896 | 'max' => $subcriteria['max_value'] // max |
---|
897 | )); |
---|
898 | |
---|
899 | } |
---|
900 | } |
---|
901 | |
---|
902 | } |
---|
903 | // Add the comment |
---|
904 | $template->assign( 'CONCOURS_COMMENT', $comment); |
---|
905 | |
---|
906 | // Add concours informations on template |
---|
907 | $template->assign( 'concours_infos', array( |
---|
908 | 'name' => $concours['name'], |
---|
909 | 'descr' => $concours['descr'] |
---|
910 | )); |
---|
911 | |
---|
912 | |
---|
913 | $template->set_filenames(array('concours' => CONCOURS_ROOT.'/template/concours.tpl')); |
---|
914 | // $template->concat('PLUGIN_PICTURE_AFTER', $template->parse('concours', true)); |
---|
915 | $template->concat('COMMENT_IMG', $template->parse('concours', true)); |
---|
916 | |
---|
917 | // |
---|
918 | $template->assign('INFO_AUTHOR',l10n('concours_img_author')); |
---|
919 | } |
---|
920 | } |
---|
921 | } |
---|
922 | |
---|
923 | // RAZ notation from user to db |
---|
924 | // fill criterias notes to 0 in DB |
---|
925 | // return false if error |
---|
926 | function RAZ_img_note_user($img_id, $concours_id = NULL) |
---|
927 | { |
---|
928 | $user_note = ""; |
---|
929 | if ($concours_id === null) |
---|
930 | if ($this->concours_id !== null) |
---|
931 | $concours_id = $this->concours_id; |
---|
932 | else |
---|
933 | return false; |
---|
934 | |
---|
935 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
936 | foreach ($firstcriterias as $criteria) |
---|
937 | { |
---|
938 | if ($this->debug) echo "ID=".$criteria['criteria_id']."\n"; |
---|
939 | // First without sub criterias |
---|
940 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
941 | { |
---|
942 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$criteria['criteria_id']."=".$criteria['min_value']; |
---|
943 | } |
---|
944 | else |
---|
945 | { |
---|
946 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
947 | foreach ($secondcriterias as $subcriteria) |
---|
948 | { |
---|
949 | if ($this->debug) echo "ID=".$criteria['criteria_id']."\n"; |
---|
950 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$subcriteria['criteria_id']."=".$subcriteria['min_value']; |
---|
951 | } |
---|
952 | } |
---|
953 | } |
---|
954 | if ($this->debug) echo "RAZ=>".$user_note."\n"; |
---|
955 | if (strlen($user_note) != 0) |
---|
956 | { |
---|
957 | $this->store_img_note_user($img_id, $user_note, $concours_id); |
---|
958 | return true; |
---|
959 | } |
---|
960 | else |
---|
961 | return false; |
---|
962 | |
---|
963 | |
---|
964 | } |
---|
965 | |
---|
966 | // Store notation from user to db |
---|
967 | // save with format "criteria_id=note;criteria_id=note..." for data |
---|
968 | function store_img_note_user($img_id, $datas, $concours_id = NULL) |
---|
969 | { |
---|
970 | global $user; |
---|
971 | if ($this->get_img_note_user( $img_id, $concours_id) == array()) |
---|
972 | { |
---|
973 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id FROM ' . CONCOURS_DATA_TABLE . ' ;'; |
---|
974 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
975 | if ($this->debug) echo "Next_element=".$next_element_id."\n"; |
---|
976 | $query = ' |
---|
977 | INSERT INTO ' . CONCOURS_DATA_TABLE .' |
---|
978 | (`id`, |
---|
979 | `id_concours` , |
---|
980 | `user_id` , |
---|
981 | `create_date` , |
---|
982 | `img_id` , |
---|
983 | `datas` ) |
---|
984 | VALUES ( '.$next_element_id.',' |
---|
985 | .($concours_id !== null ? $concours_id : $this->concours_id ) .' |
---|
986 | , '. $user['id'] .', now(), '.$img_id.', "'.$datas.'");'; |
---|
987 | } |
---|
988 | else |
---|
989 | { |
---|
990 | $query = ' |
---|
991 | UPDATE ' . CONCOURS_DATA_TABLE .' |
---|
992 | SET datas = "'.$datas.'", create_date = now() |
---|
993 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).' |
---|
994 | AND user_id = '.$user['id'].' |
---|
995 | AND img_id = '.$img_id.';'; |
---|
996 | } |
---|
997 | if ($this->debug) echo $query."\n"; |
---|
998 | pwg_query($query); |
---|
999 | |
---|
1000 | } |
---|
1001 | |
---|
1002 | // get notation's user from db |
---|
1003 | // Return an array with : |
---|
1004 | // Criteria_id ==> value |
---|
1005 | // Note : criterias with subcriterias are not saved and not restored. They are calculated for global result |
---|
1006 | function get_img_note_user($img_id, $concours_id = NULL, $user_id = null) |
---|
1007 | { |
---|
1008 | global $user; |
---|
1009 | $img_note = array(); |
---|
1010 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1011 | { |
---|
1012 | $query = ' |
---|
1013 | SELECT datas |
---|
1014 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1015 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
1016 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1017 | AND img_id = '. $img_id .' |
---|
1018 | '; |
---|
1019 | if ($this->debug) echo $query."\n"; |
---|
1020 | $result = pwg_query($query); |
---|
1021 | |
---|
1022 | while ($row = mysql_fetch_assoc($result)) |
---|
1023 | { |
---|
1024 | if ($this->debug) echo "row[data]=".$row['datas']."\n"; |
---|
1025 | $datas = explode(";",$row['datas']); |
---|
1026 | foreach ($datas as $val) |
---|
1027 | { |
---|
1028 | if (strpos($val, '=') !== FALSE) |
---|
1029 | $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1); |
---|
1030 | } |
---|
1031 | } |
---|
1032 | // DEBUG |
---|
1033 | if ($this->debug) |
---|
1034 | foreach ($img_note as $id=>$val) |
---|
1035 | { |
---|
1036 | if (is_array($val)) |
---|
1037 | foreach ($val as $id2=>$val2) |
---|
1038 | echo "img_note[".$id."][".$id2."]=".$val2."\n"; |
---|
1039 | else |
---|
1040 | echo "img_note[".$id."]=".$val."\n"; |
---|
1041 | } |
---|
1042 | // END DEBUG |
---|
1043 | |
---|
1044 | return $img_note; |
---|
1045 | } |
---|
1046 | else |
---|
1047 | return array(); |
---|
1048 | |
---|
1049 | |
---|
1050 | } |
---|
1051 | |
---|
1052 | |
---|
1053 | // get notation's user from db |
---|
1054 | // Return the global note for a picture id from a user |
---|
1055 | function get_img_globalnote_user($img_id, $concours_id = NULL, $user_id = null) |
---|
1056 | { |
---|
1057 | global $user; |
---|
1058 | $img_note = array(); |
---|
1059 | $global_user_note = 0; |
---|
1060 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1061 | { |
---|
1062 | $query = ' |
---|
1063 | SELECT datas |
---|
1064 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1065 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
1066 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1067 | AND img_id = '. $img_id .' |
---|
1068 | '; |
---|
1069 | $result = pwg_query($query); |
---|
1070 | |
---|
1071 | while ($row = mysql_fetch_assoc($result)) |
---|
1072 | { |
---|
1073 | $datas = explode(";",$row['datas']); |
---|
1074 | foreach ($datas as $val) |
---|
1075 | { |
---|
1076 | if (strpos($val, '=') !== FALSE) |
---|
1077 | $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1); |
---|
1078 | } |
---|
1079 | } |
---|
1080 | |
---|
1081 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1082 | foreach ($firstcriterias as $criteria) |
---|
1083 | { |
---|
1084 | if ($this->debug) echo "criteriaID=".$criteria['criteria_id']."\n"; |
---|
1085 | // First without sub criterias |
---|
1086 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1087 | { |
---|
1088 | if (isset($img_note[$criteria['criteria_id']])) |
---|
1089 | $global_user_note += (int)$criteria['ponderation'] * (float)$img_note[$criteria['criteria_id']]; |
---|
1090 | } |
---|
1091 | else |
---|
1092 | { |
---|
1093 | |
---|
1094 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1095 | foreach ($secondcriterias as $subcriteria) |
---|
1096 | { |
---|
1097 | if ($this->debug) echo "subcriteriaID=".$subcriteria['criteria_id']."\n"; |
---|
1098 | if (isset($img_note[$subcriteria['criteria_id']])) |
---|
1099 | $global_user_note += (int)$subcriteria['ponderation'] * (float)$img_note[$subcriteria['criteria_id']]; |
---|
1100 | } |
---|
1101 | } |
---|
1102 | |
---|
1103 | } |
---|
1104 | |
---|
1105 | return $global_user_note; |
---|
1106 | } |
---|
1107 | else |
---|
1108 | return array(); |
---|
1109 | |
---|
1110 | |
---|
1111 | } |
---|
1112 | |
---|
1113 | // Store comment from user to db |
---|
1114 | function store_img_comment_user($img_id, $datas, $concours_id = NULL) |
---|
1115 | { |
---|
1116 | global $user; |
---|
1117 | if (($comment = $this->get_img_note_user( $img_id, $concours_id)) == false) |
---|
1118 | { |
---|
1119 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id FROM ' . CONCOURS_DATA_TABLE . ' ;'; |
---|
1120 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
1121 | if ($this->debug) echo "Next_element=".$next_element_id."\n"; |
---|
1122 | $query = ' |
---|
1123 | INSERT INTO ' . CONCOURS_DATA_TABLE .' |
---|
1124 | (`id`, |
---|
1125 | `id_concours` , |
---|
1126 | `user_id` , |
---|
1127 | `create_date` , |
---|
1128 | `img_id` , |
---|
1129 | `comment`) |
---|
1130 | VALUES ( '.$next_element_id.',' |
---|
1131 | .($concours_id !== null ? $concours_id : $this->concours_id ) .' |
---|
1132 | , '. $user['id'] .', now(), '.$img_id.', "'.$datas.'");'; |
---|
1133 | } |
---|
1134 | else |
---|
1135 | { |
---|
1136 | $query = ' |
---|
1137 | UPDATE ' . CONCOURS_DATA_TABLE .' |
---|
1138 | SET comment = "'.$datas.'", create_date = now() |
---|
1139 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).' |
---|
1140 | AND user_id = '.$user['id'].' |
---|
1141 | AND img_id = '.$img_id.';'; |
---|
1142 | } |
---|
1143 | if ($this->debug) echo $query."\n"; |
---|
1144 | pwg_query($query); |
---|
1145 | |
---|
1146 | } |
---|
1147 | |
---|
1148 | |
---|
1149 | // Get comment on an image for a user |
---|
1150 | function get_img_comment_user($img_id, $concours_id = NULL, $user_id = null) |
---|
1151 | { |
---|
1152 | global $user; |
---|
1153 | $datas = false; |
---|
1154 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1155 | { |
---|
1156 | $query = ' |
---|
1157 | SELECT comment |
---|
1158 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1159 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
1160 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1161 | AND img_id = '. $img_id .' |
---|
1162 | '; |
---|
1163 | if ($this->debug) echo $query."\n"; |
---|
1164 | $result = pwg_query($query); |
---|
1165 | |
---|
1166 | while ($row = mysql_fetch_assoc($result)) |
---|
1167 | { |
---|
1168 | if ($this->debug) echo "row[comment]=".$row['comment']."\n"; |
---|
1169 | $datas = $row['comment']; |
---|
1170 | } |
---|
1171 | } |
---|
1172 | return $datas; |
---|
1173 | } |
---|
1174 | |
---|
1175 | |
---|
1176 | // Generate csv file for a closed concours with result generated |
---|
1177 | function generate_csv($concours_id = NULL) |
---|
1178 | { |
---|
1179 | $file = "rang, id_concours, img_id, img_name, img_file, date, note, datas\r\n"; |
---|
1180 | |
---|
1181 | // recover all img_id from the category |
---|
1182 | $query = 'SELECT id_concours, img_id, IMG.name, IMG.file, date, note, datas' |
---|
1183 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1184 | .' INNER JOIN ' . IMAGES_TABLE.' AS IMG on IMG.id = img_id' |
---|
1185 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1186 | .' ORDER BY note DESC' |
---|
1187 | .';'; |
---|
1188 | |
---|
1189 | $result = pwg_query($query); |
---|
1190 | // For each images |
---|
1191 | $rang = 1; |
---|
1192 | while ($row = mysql_fetch_assoc($result)) |
---|
1193 | { |
---|
1194 | $file .= $rang.', ' |
---|
1195 | .$row['id_concours'].', ' |
---|
1196 | .$row['img_id'].', ' |
---|
1197 | .$row['name'].', ' |
---|
1198 | .$row['file'].', ' |
---|
1199 | .$row['date'].', ' |
---|
1200 | .$row['note'].', ' |
---|
1201 | .($row['datas'] != null ? $row['datas'] : '') |
---|
1202 | ."\r\n"; |
---|
1203 | $rang ++; |
---|
1204 | } |
---|
1205 | |
---|
1206 | return $file; |
---|
1207 | |
---|
1208 | |
---|
1209 | } |
---|
1210 | |
---|
1211 | // Generate csv file for a closed concours with result generated and all users informations |
---|
1212 | function generate_detail_csv($concours_id = NULL) |
---|
1213 | { |
---|
1214 | if ($concours_id === null) |
---|
1215 | $concours_id = $this->concours_id; |
---|
1216 | // Prepare the list of criteria |
---|
1217 | $criteria_list = ""; |
---|
1218 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1219 | $ident1 = 1; |
---|
1220 | foreach ($firstcriterias as $criteria) |
---|
1221 | { |
---|
1222 | // format (id:name) |
---|
1223 | $criteria_list .= (strlen($criteria_list) ? "," : "") |
---|
1224 | .$ident1.":".$criteria['name'] |
---|
1225 | ."(id=".$criteria['criteria_id'].")"; |
---|
1226 | // First wit sub criterias |
---|
1227 | if ($this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1228 | { |
---|
1229 | $ident2 = 1; |
---|
1230 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1231 | foreach ($secondcriterias as $subcriteria) |
---|
1232 | { |
---|
1233 | // format (id:name) |
---|
1234 | $criteria_list .= (strlen($criteria_list) ? "," : "") |
---|
1235 | .$ident1.".".$ident2 |
---|
1236 | .":".$subcriteria['name']."(id=".$subcriteria['criteria_id'].")"; |
---|
1237 | $ident2 ++; |
---|
1238 | } |
---|
1239 | } |
---|
1240 | $ident1++; |
---|
1241 | } |
---|
1242 | if ($this->debug) echo "CRITERIA_LIST=".$criteria_list."\n"; |
---|
1243 | $user_list = array(); |
---|
1244 | $users_criteria = ""; |
---|
1245 | // Get all the users who have notes for the concours |
---|
1246 | $query = 'SELECT distinct(user_id), USER.username' |
---|
1247 | .' FROM ' .CONCOURS_DATA_TABLE |
---|
1248 | .' INNER JOIN ' . USERS_TABLE.' AS USER on USER.id = user_id' |
---|
1249 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1250 | .' ORDER BY username ASC' |
---|
1251 | .';'; |
---|
1252 | $result = pwg_query($query); |
---|
1253 | // For each user |
---|
1254 | while ($row = mysql_fetch_assoc($result)) |
---|
1255 | { |
---|
1256 | array_push($user_list, $row); |
---|
1257 | $users_criteria .= (strlen($users_criteria) ? "," : "")."user, user_global_note, comment, ".$criteria_list; |
---|
1258 | |
---|
1259 | } |
---|
1260 | |
---|
1261 | if ($this->debug) echo "users_criteria=".$users_criteria."\n"; |
---|
1262 | |
---|
1263 | // All informations in csv format |
---|
1264 | $file = "rang, id_concours, nom_concours, date_debut, date_fin, date_export, img_id, img_name, img_file, img_author, note, nbvotant, datas," |
---|
1265 | .$users_criteria |
---|
1266 | ."\r\n"; |
---|
1267 | |
---|
1268 | // nb of users who vote for each image |
---|
1269 | $query = 'SELECT img_id, COUNT(DISTINCT user_id) AS NBVOTE FROM ' . CONCOURS_DATA_TABLE |
---|
1270 | .' GROUP BY img_id ' |
---|
1271 | .';'; |
---|
1272 | |
---|
1273 | $result = pwg_query($query); |
---|
1274 | // echo $query; |
---|
1275 | $nbvotes = array(); |
---|
1276 | while ($row = mysql_fetch_assoc($result)) |
---|
1277 | { |
---|
1278 | $nbvotes[$row['img_id']] = $row['NBVOTE']; |
---|
1279 | } |
---|
1280 | /* |
---|
1281 | foreach ($nbvotes as $id=>$val) |
---|
1282 | { |
---|
1283 | echo "Vote img(".$id.")=".$val."\n"; |
---|
1284 | } |
---|
1285 | */ |
---|
1286 | // recover all img_id from the category |
---|
1287 | $query = 'SELECT id_concours, img_id, IMG.name, IMG.file, IMG.author, date, note, datas, comment' |
---|
1288 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1289 | .' INNER JOIN ' . IMAGES_TABLE.' AS IMG on IMG.id = img_id' |
---|
1290 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1291 | .' ORDER BY note DESC' |
---|
1292 | .';'; |
---|
1293 | |
---|
1294 | $result = pwg_query($query); |
---|
1295 | // echo $query; |
---|
1296 | // For each images |
---|
1297 | $rang = 1; |
---|
1298 | while ($row = mysql_fetch_assoc($result)) |
---|
1299 | { |
---|
1300 | $file .= $rang.', ' |
---|
1301 | .$row['id_concours'].', ' |
---|
1302 | .str_replace(",", "",$this->concours_infos['name']).', ' |
---|
1303 | .$this->concours_infos['begin_date'].', ' |
---|
1304 | .$this->concours_infos['end_date'].', ' |
---|
1305 | .$row['date'].', ' |
---|
1306 | |
---|
1307 | .$row['img_id'].', ' |
---|
1308 | .str_replace(",", "",$row['name']).', ' |
---|
1309 | .$row['file'].', ' |
---|
1310 | .$row['author'].', ' |
---|
1311 | .$row['note'].', ' |
---|
1312 | .(isset($nbvotes[$row['img_id']]) ? $nbvotes[$row['img_id']] : 0).', ' |
---|
1313 | .($row['datas'] != null ? $row['datas'] : '') |
---|
1314 | ; |
---|
1315 | foreach ($user_list as $uuser) |
---|
1316 | { |
---|
1317 | |
---|
1318 | $user_note = $this->get_img_note_user($row['img_id'], $concours_id, $uuser['user_id']); |
---|
1319 | |
---|
1320 | $file .= ', '.$uuser['username'].', '.$this->get_img_globalnote_user($row['img_id'], $concours_id, $uuser['user_id']) |
---|
1321 | .', '.($row['comment'] != null ? str_replace(",", "",$row['comment']) : '') |
---|
1322 | .', ' |
---|
1323 | ; |
---|
1324 | |
---|
1325 | $user_note_by_crit = ""; |
---|
1326 | foreach ($firstcriterias as $criteria) |
---|
1327 | { |
---|
1328 | // First without sub criterias |
---|
1329 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1330 | { |
---|
1331 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").(isset($user_note[$criteria['criteria_id']]) ? $user_note[$criteria['criteria_id']] : ' '); |
---|
1332 | } |
---|
1333 | else |
---|
1334 | { |
---|
1335 | $user_note_by_subcrit = ""; |
---|
1336 | $subcrit_note = 0; |
---|
1337 | $user_has_vote = false; |
---|
1338 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1339 | foreach ($secondcriterias as $subcriteria) |
---|
1340 | { |
---|
1341 | if (isset($user_note[$subcriteria['criteria_id']])) $user_has_vote = true; |
---|
1342 | $user_note_by_subcrit .= (strlen($user_note_by_subcrit) ? "," : "").(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : ' '); |
---|
1343 | $subcrit_note += (int)$subcriteria['ponderation'] * (float)(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : 0); |
---|
1344 | |
---|
1345 | } |
---|
1346 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").($user_has_vote ? $subcrit_note : ' ').",".$user_note_by_subcrit; |
---|
1347 | } |
---|
1348 | } |
---|
1349 | $file .= $user_note_by_crit; |
---|
1350 | |
---|
1351 | } |
---|
1352 | |
---|
1353 | $file .="\r\n"; |
---|
1354 | $rang ++; |
---|
1355 | } |
---|
1356 | // echo $file; |
---|
1357 | return $file; |
---|
1358 | |
---|
1359 | |
---|
1360 | } |
---|
1361 | |
---|
1362 | |
---|
1363 | |
---|
1364 | // Save csv datas to physical file |
---|
1365 | function save_file($file,$concours_id = NULL) |
---|
1366 | { |
---|
1367 | // check if already saved file |
---|
1368 | if ($filename = $this->get_file_name($concours_id)) |
---|
1369 | return $filename; |
---|
1370 | else |
---|
1371 | { |
---|
1372 | $dirpath = CONCOURS_RESULT_FOLDER; |
---|
1373 | |
---|
1374 | $filename = date('Ymd')."_concours".($concours_id !== null ? $concours_id : $this->concours_id ).".csv"; |
---|
1375 | if (!is_dir($dirpath)) |
---|
1376 | @mkdir($dirpath); |
---|
1377 | if (file_exists($dirpath. $filename)) |
---|
1378 | unlink($dirpath. $filename); |
---|
1379 | |
---|
1380 | $fh = fopen($dirpath. $filename, 'w') or die("can't open file"); |
---|
1381 | fwrite($fh, $file); |
---|
1382 | fclose($fh); |
---|
1383 | |
---|
1384 | //update result database with filename |
---|
1385 | $query = "UPDATE " . CONCOURS_RESULT_TABLE . " |
---|
1386 | SET |
---|
1387 | file_name = \"".$filename."\" |
---|
1388 | WHERE id_concours= ".($concours_id !== NULL ? $concours_id : $this->concours_id)." |
---|
1389 | ;"; |
---|
1390 | pwg_query($query); |
---|
1391 | // echo $query; |
---|
1392 | |
---|
1393 | return $filename; |
---|
1394 | } |
---|
1395 | } |
---|
1396 | |
---|
1397 | // Get result file name from db |
---|
1398 | function get_file_name($concours_id = NULL) |
---|
1399 | { |
---|
1400 | $query = 'SELECT distinct(file_name)' |
---|
1401 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1402 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1403 | .';'; |
---|
1404 | $row = mysql_fetch_assoc(pwg_query($query)); |
---|
1405 | if ($row) |
---|
1406 | return $row['file_name']; |
---|
1407 | else |
---|
1408 | return false; |
---|
1409 | |
---|
1410 | } |
---|
1411 | |
---|
1412 | // Get csv datas from a saved file |
---|
1413 | function get_file($concours_id = NULL) |
---|
1414 | { |
---|
1415 | $filename = CONCOURS_RESULT_FOLDER. |
---|
1416 | $size = filesize($filename); |
---|
1417 | header('Content-Type: application/octet-stream'); |
---|
1418 | header('Content-Length: '.$size); |
---|
1419 | header('Content-Disposition: attachment; filename='.$filename); |
---|
1420 | header('Content-Transfer-Encoding: binary'); |
---|
1421 | |
---|
1422 | $file = @fopen($filename, ‘rb’); |
---|
1423 | if ($file) { |
---|
1424 | // stream the file and exit the script when complete |
---|
1425 | fpassthru($file); |
---|
1426 | } |
---|
1427 | |
---|
1428 | } |
---|
1429 | |
---|
1430 | // Activation page administration |
---|
1431 | function concours_admin_menu($menu) |
---|
1432 | { |
---|
1433 | array_push($menu, |
---|
1434 | array( |
---|
1435 | 'NAME' => CONCOURS_NAME, |
---|
1436 | 'URL' => get_admin_plugin_menu_link(CONCOURS_ADMIN_PATH.'admin.php') |
---|
1437 | ) |
---|
1438 | ); |
---|
1439 | return $menu; |
---|
1440 | } |
---|
1441 | |
---|
1442 | // register Concours Menubar |
---|
1443 | public function register_blocks( $menu_ref_arr ) |
---|
1444 | { |
---|
1445 | $menu = & $menu_ref_arr[0]; |
---|
1446 | if ($menu->get_id() != 'menubar' OR $this->my_config['active_menubar'] == false) |
---|
1447 | return; |
---|
1448 | $menu->register_block( new RegisteredBlock( 'CONCOURS_menu', 'Polls', 'MYPOLLS')); |
---|
1449 | } |
---|
1450 | |
---|
1451 | /* |
---|
1452 | initialise menubar's menu |
---|
1453 | called by menubar object when making menu |
---|
1454 | */ |
---|
1455 | public function blockmanager_apply( $menu_ref_arr ) |
---|
1456 | { |
---|
1457 | |
---|
1458 | global $user, $template; |
---|
1459 | $menu = & $menu_ref_arr[0]; |
---|
1460 | |
---|
1461 | if ($this->user_groups == array()) |
---|
1462 | $this->get_user_groups(); |
---|
1463 | |
---|
1464 | if ( ($block = $menu->get_block( 'CONCOURS_menu' ) ) != null ) |
---|
1465 | { |
---|
1466 | if($user['id']==2) |
---|
1467 | { |
---|
1468 | $vote_id = $_SERVER['REMOTE_ADDR']; |
---|
1469 | } |
---|
1470 | else |
---|
1471 | { |
---|
1472 | $vote_id = $user['id']; |
---|
1473 | } |
---|
1474 | |
---|
1475 | // $polls_list=$this->get_polls_list($vote_id, is_admin(), $user['language']); |
---|
1476 | $menu_list=array(); |
---|
1477 | |
---|
1478 | if(is_admin()) |
---|
1479 | { |
---|
1480 | array_push($menu_list, |
---|
1481 | array( |
---|
1482 | 'nfo' => "", |
---|
1483 | 'text' => l10n('concours_admin_add'), |
---|
1484 | 'link' => PHPWG_ROOT_PATH . 'admin.php?page=plugin&section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&action=new', |
---|
1485 | 'edit' => "" |
---|
1486 | ) |
---|
1487 | ); |
---|
1488 | } |
---|
1489 | |
---|
1490 | // recover all img_id from the category |
---|
1491 | $query = 'SELECT distinct(id_concours), groups, CONC.name' |
---|
1492 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1493 | .' INNER JOIN '.CONCOURS_TABLE. ' AS CONC ON CONC.id = id_concours' |
---|
1494 | .' ORDER BY date DESC' |
---|
1495 | .';'; |
---|
1496 | |
---|
1497 | $result = pwg_query($query); |
---|
1498 | $nb_concours = 1; |
---|
1499 | // For each concours |
---|
1500 | while ($row = mysql_fetch_assoc($result)) |
---|
1501 | { |
---|
1502 | |
---|
1503 | if ($nb_concours > $this->my_config['nbconcours_menubar']) |
---|
1504 | break; |
---|
1505 | |
---|
1506 | if (!empty($row['groups'])) |
---|
1507 | { |
---|
1508 | $authorized_groups = explode(',', $row['groups']); |
---|
1509 | if (array_intersect($this->user_groups, $authorized_groups) == array()) |
---|
1510 | { |
---|
1511 | continue; |
---|
1512 | } |
---|
1513 | $nb_concours ++; |
---|
1514 | array_push($menu_list, |
---|
1515 | array( |
---|
1516 | 'nfo' => "", |
---|
1517 | 'text' => $row['name'], |
---|
1518 | // 'link' => CONCOURS_PATH.'publish.php?id='.$row['id_concours'], |
---|
1519 | 'link' => './index.php?/concours/'.$row['id_concours'], |
---|
1520 | 'edit' => (is_admin()? PHPWG_ROOT_PATH . 'admin.php?page=plugin&section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&concours=' . $row['id_concours'].'&action=modify':"") |
---|
1521 | ) |
---|
1522 | ); |
---|
1523 | } |
---|
1524 | } |
---|
1525 | |
---|
1526 | if (count($menu_list)) |
---|
1527 | { |
---|
1528 | |
---|
1529 | $block->set_title("Concours Photo"); |
---|
1530 | $block->template = CONCOURS_ROOT.'/template/concours_menu.tpl'; |
---|
1531 | $block->data = $menu_list; |
---|
1532 | } |
---|
1533 | } |
---|
1534 | } |
---|
1535 | |
---|
1536 | |
---|
1537 | function section_init_concours() |
---|
1538 | { |
---|
1539 | global $tokens, $page; |
---|
1540 | if ($tokens[0] == 'concours') |
---|
1541 | $page['section'] = 'concours'; |
---|
1542 | } |
---|
1543 | |
---|
1544 | function index_concours() |
---|
1545 | { |
---|
1546 | global $page; |
---|
1547 | |
---|
1548 | if (isset($page['section']) and $page['section'] == 'concours') |
---|
1549 | { |
---|
1550 | include(CONCOURS_PATH . 'publish.php'); |
---|
1551 | } |
---|
1552 | } |
---|
1553 | |
---|
1554 | |
---|
1555 | } |
---|
1556 | |
---|
1557 | ?> |
---|