1 | <?php |
---|
2 | /* |
---|
3 | * Plugin Name: ConcoursPhoto |
---|
4 | * File : Concours.class.php |
---|
5 | */ |
---|
6 | |
---|
7 | global $user, $conf; |
---|
8 | |
---|
9 | class Concours |
---|
10 | { |
---|
11 | var $concours_id = null; |
---|
12 | var $concours_infos = array(); |
---|
13 | |
---|
14 | // General configuration parameters |
---|
15 | var $my_config = array(); |
---|
16 | var $user_groups = array(); |
---|
17 | var $debug = false; |
---|
18 | |
---|
19 | |
---|
20 | /** |
---|
21 | * Class constructor with concours id. |
---|
22 | * Return NULL if not existing or create it |
---|
23 | */ |
---|
24 | function __construct($concours_id = null, $force_creation = NULL) |
---|
25 | { |
---|
26 | if ($concours_id !== null) |
---|
27 | { |
---|
28 | $this->concours_id = (int)$concours_id; |
---|
29 | $this->get_concours($concours_id); |
---|
30 | } |
---|
31 | // Load general parameters |
---|
32 | $this->load_config(); |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | /** |
---|
37 | * Load configuration from database |
---|
38 | * Assign value to the variable $config |
---|
39 | */ |
---|
40 | function load_config() { |
---|
41 | $query = 'SELECT value FROM '.CONFIG_TABLE.' WHERE param="concoursphoto";'; |
---|
42 | $result = pwg_query($query); |
---|
43 | |
---|
44 | if(isset($result)) { |
---|
45 | $row = pwg_db_fetch_row($result); |
---|
46 | if(is_string($row[0])) { |
---|
47 | $this->my_config = unserialize(($row[0])); |
---|
48 | } |
---|
49 | } |
---|
50 | if ($this->debug) |
---|
51 | foreach ($this->my_config as $key => $value) |
---|
52 | { |
---|
53 | echo "my_config[".$key."] = ".$value."\n"; |
---|
54 | } |
---|
55 | $this->load_default_config(); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * Load default configuration, initialize default values of params |
---|
61 | */ |
---|
62 | private function load_default_config() |
---|
63 | { |
---|
64 | include CONCOURS_INC_PATH.'default_values.inc.php'; |
---|
65 | foreach ($concours_default_values as $key => $value) |
---|
66 | { |
---|
67 | if (!isset($this->my_config[$key])) $this->my_config[$key] = $value; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | // Save general configuration to config_database |
---|
72 | function save_config() |
---|
73 | { |
---|
74 | $query = ' |
---|
75 | REPLACE INTO '.CONFIG_TABLE.' |
---|
76 | VALUES( |
---|
77 | \'concoursphoto\', |
---|
78 | \''.serialize($this->my_config).'\', |
---|
79 | \'Configuration Concours Photo\') |
---|
80 | ;'; |
---|
81 | |
---|
82 | $result = pwg_query($query); |
---|
83 | |
---|
84 | if($result) |
---|
85 | return true; |
---|
86 | else |
---|
87 | return false; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Save the current configuration (ie the value of $config) to the database |
---|
92 | */ |
---|
93 | function set_config() { |
---|
94 | conf_update_param('concoursphoto', pwg_db_real_escape_string(serialize($this->my_config))); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | // Retrieve user groups |
---|
99 | function get_user_groups() |
---|
100 | { |
---|
101 | global $user; |
---|
102 | $query = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id = ' . $user['id'] . ';'; |
---|
103 | |
---|
104 | $result = pwg_query($query); |
---|
105 | while ($row = pwg_db_fetch_array($result)) |
---|
106 | { |
---|
107 | array_push($this->user_groups, $row['group_id']); |
---|
108 | } |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | // create a concours and store it to db. |
---|
113 | // return id for concours. |
---|
114 | function create_concours() |
---|
115 | {} |
---|
116 | |
---|
117 | // Get informations array for a concours id |
---|
118 | function get_concours($concours_id = null) |
---|
119 | { |
---|
120 | if ($concours_id!== null or $this->concours_id !== null) |
---|
121 | { |
---|
122 | $query = ' |
---|
123 | SELECT * |
---|
124 | FROM ' . CONCOURS_TABLE .' |
---|
125 | WHERE id =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
126 | LIMIT 1 |
---|
127 | '; |
---|
128 | |
---|
129 | $result = pwg_query($query); |
---|
130 | if ($result) |
---|
131 | $this->concours_infos = pwg_db_fetch_array($result); |
---|
132 | } |
---|
133 | |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | // save informations on database for a concours_id |
---|
138 | function save_concours($concours_id = null) |
---|
139 | { |
---|
140 | if ($concours_id!== null or $this->concours_id !== null) |
---|
141 | { |
---|
142 | |
---|
143 | $query = "INSERT INTO " . CONCOURS_TABLE . " |
---|
144 | ( `id`, |
---|
145 | `create_date`, |
---|
146 | `name`, |
---|
147 | `descr`, |
---|
148 | `begin_date`, |
---|
149 | `end_date`, |
---|
150 | `category`, |
---|
151 | `groups`, |
---|
152 | `method`, |
---|
153 | `guest`, |
---|
154 | `admin` |
---|
155 | |
---|
156 | ) |
---|
157 | VALUES (".($concours_id !== NULL ? $concours_id : $this->concours_id).", now(), |
---|
158 | \"".$this->concours_infos['name']."\", \"".$this->concours_infos['descr']."\", |
---|
159 | \"".$this->concours_infos['begin_date']."\", \"".$this->concours_infos['end_date']."\", |
---|
160 | ".$this->concours_infos['category'].", ".$this->concours_infos['groups'].", |
---|
161 | ".$this->concours_infos['method'].", ".($this->concours_infos['guest'] ? "TRUE" : "FALSE").", |
---|
162 | ".($this->concours_infos['admin'] ? "TRUE" : "FALSE")." |
---|
163 | );"; |
---|
164 | if (pwg_query($query) != null) |
---|
165 | return true; |
---|
166 | else |
---|
167 | return false; |
---|
168 | } |
---|
169 | else |
---|
170 | return false; |
---|
171 | } |
---|
172 | |
---|
173 | // update informations on database for a concours_id |
---|
174 | function update_concours($concours_id = null) |
---|
175 | { |
---|
176 | if ($concours_id!== null or $this->concours_id !== null) |
---|
177 | { |
---|
178 | |
---|
179 | $query = "UPDATE " . CONCOURS_TABLE . " |
---|
180 | SET |
---|
181 | create_date = now(), |
---|
182 | name = \"".$this->concours_infos['name']."\", |
---|
183 | descr = \"".$this->concours_infos['descr']."\", |
---|
184 | begin_date = \"".$this->concours_infos['begin_date']."\", |
---|
185 | end_date = \"".$this->concours_infos['end_date']."\", |
---|
186 | category = ".$this->concours_infos['category'].", |
---|
187 | groups = ".$this->concours_infos['groups'].", |
---|
188 | method = ".$this->concours_infos['method'].", |
---|
189 | guest = ".($this->concours_infos['guest'] ? "TRUE" : "FALSE").", |
---|
190 | admin = ".($this->concours_infos['admin'] ? "TRUE" : "FALSE")." |
---|
191 | |
---|
192 | WHERE id = ".($concours_id !== NULL ? $concours_id : $this->concours_id)." |
---|
193 | ;"; |
---|
194 | pwg_query($query); |
---|
195 | if (pwg_query($query) != null) |
---|
196 | return true; |
---|
197 | else |
---|
198 | return false; |
---|
199 | } |
---|
200 | else |
---|
201 | return false; |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | // update field on database for a concours_id |
---|
206 | function update_concoursfield($field_id, $concours_id = null) |
---|
207 | { |
---|
208 | if ($concours_id!== null or $this->concours_id !== null) |
---|
209 | { |
---|
210 | |
---|
211 | $query = "UPDATE " . CONCOURS_TABLE . " |
---|
212 | SET |
---|
213 | " . $field_id." = ". $this->concours_infos[$field_id] . " |
---|
214 | |
---|
215 | WHERE id = ".($concours_id !== NULL ? $concours_id : $this->concours_id)." |
---|
216 | ;"; |
---|
217 | pwg_query($query); |
---|
218 | if (pwg_query($query) != null) |
---|
219 | return true; |
---|
220 | else |
---|
221 | return false; |
---|
222 | } |
---|
223 | else |
---|
224 | return false; |
---|
225 | |
---|
226 | } |
---|
227 | |
---|
228 | // delete concours from db (and all sub informations such as details, vote and result |
---|
229 | function delete_concours($concours_id = null) |
---|
230 | { |
---|
231 | if ($concours_id!== null or $this->concours_id !== null) |
---|
232 | { |
---|
233 | |
---|
234 | $query = ' |
---|
235 | DELETE |
---|
236 | FROM ' . CONCOURS_RESULT_TABLE .' |
---|
237 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
238 | '; |
---|
239 | pwg_query($query); |
---|
240 | $query = ' |
---|
241 | DELETE |
---|
242 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
243 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
244 | '; |
---|
245 | pwg_query($query); |
---|
246 | $query = ' |
---|
247 | DELETE |
---|
248 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
249 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
250 | '; |
---|
251 | pwg_query($query); |
---|
252 | $query = ' |
---|
253 | DELETE |
---|
254 | FROM ' . CONCOURS_TABLE .' |
---|
255 | WHERE id =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
256 | '; |
---|
257 | pwg_query($query); |
---|
258 | } |
---|
259 | else |
---|
260 | return false; |
---|
261 | |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | // delete all closed concours from db |
---|
266 | function delete_allclosed_concours() |
---|
267 | { |
---|
268 | $concours_list=array(); |
---|
269 | $query = ' |
---|
270 | SELECT id |
---|
271 | FROM ' . CONCOURS_TABLE .' |
---|
272 | WHERE time_to_sec(TIMEDIFF(now(), end_date)) > 0 |
---|
273 | AND id>0;'; |
---|
274 | |
---|
275 | $result = pwg_query($query); |
---|
276 | while ($row = pwg_db_fetch_assoc($result)) |
---|
277 | { |
---|
278 | $this->delete_concours($row['id']); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | // today's date is between open and close date of concours? |
---|
284 | function is_active($concours_id = NULL) |
---|
285 | { |
---|
286 | if ($concours_id!== null or $this->concours_id !== null) |
---|
287 | { |
---|
288 | $query = ' |
---|
289 | SELECT id |
---|
290 | FROM ' . CONCOURS_TABLE .' |
---|
291 | WHERE id =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
292 | AND time_to_sec(TIMEDIFF(begin_date,now())) < 0 |
---|
293 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
294 | '; |
---|
295 | |
---|
296 | $result = pwg_query($query); |
---|
297 | if ($result != NULL) |
---|
298 | return true; |
---|
299 | else |
---|
300 | return false; |
---|
301 | } |
---|
302 | else |
---|
303 | return NULL; |
---|
304 | } |
---|
305 | |
---|
306 | // today's date is not between open and close date of concours? |
---|
307 | function is_closed($concours_id = NULL) |
---|
308 | { |
---|
309 | if ($concours_id!== null or $this->concours_id !== null) |
---|
310 | { |
---|
311 | $query = ' |
---|
312 | SELECT id |
---|
313 | FROM ' . CONCOURS_TABLE .' |
---|
314 | WHERE id =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
315 | AND time_to_sec(TIMEDIFF(now(), end_date)) > 0 |
---|
316 | '; |
---|
317 | |
---|
318 | $result = pwg_query($query); |
---|
319 | if ($result != NULL) |
---|
320 | return true; |
---|
321 | else |
---|
322 | return false; |
---|
323 | } |
---|
324 | else |
---|
325 | return NULL; |
---|
326 | } |
---|
327 | |
---|
328 | // Get high value of concours (max score for each criterias) |
---|
329 | function get_MaxScore_concours($concours_id = null) |
---|
330 | { |
---|
331 | $max = 0; |
---|
332 | $query = ' |
---|
333 | SELECT criteria_id, max_value, ponderation, uppercriteria |
---|
334 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
335 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
336 | ORDER BY criteria_id |
---|
337 | '; |
---|
338 | $result = pwg_query($query); |
---|
339 | while ($row = pwg_db_fetch_array($result)) |
---|
340 | { |
---|
341 | if ($row['uppercriteria'] != NULL || !$this->is_criterias_contains_sub($row['criteria_id'])) // subcriteria or uppercriteria without sub |
---|
342 | $max += $row['max_value']*$row['ponderation']; |
---|
343 | } |
---|
344 | return $max; |
---|
345 | |
---|
346 | |
---|
347 | } |
---|
348 | |
---|
349 | // Get criterias that are stored on db for default mode (concours_id = 0) |
---|
350 | function get_default_criterias() |
---|
351 | { |
---|
352 | $criterias = $this->get_criterias_list(0); |
---|
353 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id |
---|
354 | FROM ' . CONCOURS_DETAIL_TABLE . ' ;'; |
---|
355 | list($next_element_id) = pwg_db_fetch_array(pwg_query($query)); |
---|
356 | |
---|
357 | foreach ($criterias as $criteria) |
---|
358 | { |
---|
359 | |
---|
360 | $query = 'INSERT INTO '.CONCOURS_DETAIL_TABLE.' |
---|
361 | (id, id_concours, criteria_id, name, descr, min_value, max_value, ponderation, uppercriteria)' |
---|
362 | .'VALUES ('.$next_element_id.', '.$this->concours_id.', ' |
---|
363 | .$criteria['criteria_id'].', "'.$criteria['name'].'", "' |
---|
364 | .$criteria['descr'].'", '.$criteria['min_value'].', ' |
---|
365 | .$criteria['max_value'].', '.$criteria['ponderation'].', ' |
---|
366 | .($criteria['uppercriteria'] ? $criteria['uppercriteria'] : 'NULL').') |
---|
367 | ;'; |
---|
368 | |
---|
369 | $result = pwg_query($query); |
---|
370 | |
---|
371 | $next_element_id = $next_element_id +1; |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | // Get criterias from a concours |
---|
376 | function get_criterias_list($concours_id = NULL) |
---|
377 | { |
---|
378 | $criteria_list = array(); |
---|
379 | if ($concours_id!== null or $this->concours_id !== null) |
---|
380 | { |
---|
381 | $query = ' |
---|
382 | SELECT * |
---|
383 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
384 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
385 | ORDER BY criteria_id |
---|
386 | '; |
---|
387 | $result = pwg_query($query); |
---|
388 | while ($row = pwg_db_fetch_array($result)) |
---|
389 | { |
---|
390 | array_push($criteria_list, $row); |
---|
391 | } |
---|
392 | return $criteria_list; |
---|
393 | } |
---|
394 | else |
---|
395 | return $criteria_list; |
---|
396 | } |
---|
397 | |
---|
398 | // Get list of the fist level criterias |
---|
399 | function get_firstlevel_criterias($concours_id = NULL) |
---|
400 | { |
---|
401 | $criteria_list = array(); |
---|
402 | if ($concours_id!== null or $this->concours_id !== null) |
---|
403 | { |
---|
404 | $query = ' |
---|
405 | SELECT * |
---|
406 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
407 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
408 | AND uppercriteria IS NULL |
---|
409 | ORDER BY criteria_id |
---|
410 | '; |
---|
411 | $result = pwg_query($query); |
---|
412 | |
---|
413 | while ($row = pwg_db_fetch_array($result)) |
---|
414 | array_push($criteria_list, $row); |
---|
415 | return $criteria_list; |
---|
416 | } |
---|
417 | else |
---|
418 | return $criteria_list; |
---|
419 | } |
---|
420 | |
---|
421 | // check if a criteria contains subcriterias |
---|
422 | function is_criterias_contains_sub($criteria_id, $concours_id = NULL) |
---|
423 | { |
---|
424 | if ($concours_id!== null or $this->concours_id !== null) |
---|
425 | { |
---|
426 | $query = ' |
---|
427 | SELECT criteria_id |
---|
428 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
429 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
430 | AND uppercriteria = '.$criteria_id.' |
---|
431 | '; |
---|
432 | $result = pwg_query($query); |
---|
433 | if(pwg_db_fetch_array($result)) |
---|
434 | return true; |
---|
435 | else |
---|
436 | return false; |
---|
437 | } |
---|
438 | return NULL; |
---|
439 | } |
---|
440 | |
---|
441 | |
---|
442 | // Get list of subcriterias from a criteria_id |
---|
443 | function get_subcriterias($criteria_id, $concours_id = NULL) |
---|
444 | { |
---|
445 | $criteria_list = array(); |
---|
446 | if ($concours_id!== null or $this->concours_id !== null) |
---|
447 | { |
---|
448 | $query = ' |
---|
449 | SELECT * |
---|
450 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
451 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
452 | AND uppercriteria = '.$criteria_id.' |
---|
453 | ORDER BY criteria_id |
---|
454 | '; |
---|
455 | $result = pwg_query($query); |
---|
456 | while ($row = pwg_db_fetch_array($result)) |
---|
457 | { |
---|
458 | array_push($criteria_list, $row); |
---|
459 | } |
---|
460 | return $criteria_list; |
---|
461 | } |
---|
462 | else |
---|
463 | return $criteria_list; |
---|
464 | } |
---|
465 | |
---|
466 | // Get a detail from a criteria |
---|
467 | function get_criteria($criteria_id, $concours_id = NULL) |
---|
468 | { |
---|
469 | $criteria = array(); |
---|
470 | |
---|
471 | if ($concours_id!== null or $this->concours_id !== null) |
---|
472 | { |
---|
473 | $query = ' |
---|
474 | SELECT * |
---|
475 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
476 | WHERE id_concours = '. ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
477 | AND criteria_id =' . $criteria_id . ' |
---|
478 | '; |
---|
479 | $result = pwg_query($query); |
---|
480 | |
---|
481 | $criteria = pwg_db_fetch_array($result); |
---|
482 | } |
---|
483 | return $criteria; |
---|
484 | |
---|
485 | } |
---|
486 | |
---|
487 | // Get a detail from a criteria |
---|
488 | function get_criteria_by_id($id) |
---|
489 | { |
---|
490 | $criteria = array(); |
---|
491 | |
---|
492 | $query = ' |
---|
493 | SELECT * |
---|
494 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
495 | WHERE id =' . $id . ' |
---|
496 | '; |
---|
497 | $result = pwg_query($query); |
---|
498 | |
---|
499 | $criteria = pwg_db_fetch_array($result); |
---|
500 | return $criteria; |
---|
501 | |
---|
502 | } |
---|
503 | |
---|
504 | // Add a criteria to a concours |
---|
505 | // Datas is an array and contains all infos. |
---|
506 | // Return the criteria id |
---|
507 | function add_criteria($datas, $concours_id = NULL) |
---|
508 | { |
---|
509 | // determines the criteria_id |
---|
510 | if ($concours_id!== null or $this->concours_id !== null) |
---|
511 | { |
---|
512 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id |
---|
513 | FROM ' . CONCOURS_DETAIL_TABLE . ' ;'; |
---|
514 | list($next_element_id) = pwg_db_fetch_array(pwg_query($query)); |
---|
515 | |
---|
516 | $query = 'SELECT IF(MAX(criteria_id)+1 IS NULL, 1, MAX(criteria_id)+1) AS next_criteria_id |
---|
517 | FROM ' . CONCOURS_DETAIL_TABLE . ' |
---|
518 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) . ' ;'; |
---|
519 | list($next_criteria_id) = pwg_db_fetch_array(pwg_query($query)); |
---|
520 | |
---|
521 | $query = ' |
---|
522 | INSERT INTO ' . CONCOURS_DETAIL_TABLE .' |
---|
523 | (`id`, |
---|
524 | `id_concours`, |
---|
525 | `criteria_id`, |
---|
526 | `name`, |
---|
527 | `descr`, |
---|
528 | `min_value`, |
---|
529 | `max_value`, |
---|
530 | `ponderation`, |
---|
531 | `uppercriteria`) |
---|
532 | VALUES ( |
---|
533 | '.$next_element_id.', '.($concours_id !== null ? $concours_id : $this->concours_id ).', |
---|
534 | '.$next_criteria_id.', "'.$datas['name'].'", "'.$datas['descr'].'", '.$datas['min_value'].' |
---|
535 | , '.$datas['max_value'].', '.$datas['ponderation'].' |
---|
536 | , '.($datas['uppercriteria'] ? $datas['uppercriteria'] : 'NULL').' |
---|
537 | );'; |
---|
538 | |
---|
539 | pwg_query($query); |
---|
540 | if ($this->debug) echo $query."\n"; |
---|
541 | |
---|
542 | } |
---|
543 | } |
---|
544 | |
---|
545 | // Update a criteria to a concours |
---|
546 | // Datas is an array and contains all infos. |
---|
547 | function update_criteria($datas, $concours_id = NULL) |
---|
548 | { |
---|
549 | // determines the criteria_id |
---|
550 | if ($concours_id!== null or $this->concours_id !== null) |
---|
551 | { |
---|
552 | $query = ' |
---|
553 | UPDATE ' . CONCOURS_DETAIL_TABLE .' |
---|
554 | SET |
---|
555 | name = "'.$datas['name'].'", |
---|
556 | descr = "'.$datas['descr'].'", |
---|
557 | min_value = '.$datas['min_value'].', |
---|
558 | max_value = '.$datas['max_value'].', |
---|
559 | '.($datas['uppercriteria'] == false ? '' : 'uppercriteria = '.($datas['uppercriteria'] ? $datas['uppercriteria'].',' : 'NULL ,')).' |
---|
560 | ponderation = '.$datas['ponderation'].' |
---|
561 | WHERE id = '.$datas['id'].';'; |
---|
562 | |
---|
563 | pwg_query($query); |
---|
564 | } |
---|
565 | } |
---|
566 | |
---|
567 | |
---|
568 | // Delete a criteria from a concours |
---|
569 | function delete_criteria($criteria_id, $concours_id = NULL) |
---|
570 | { |
---|
571 | if ($concours_id!== null or $this->concours_id !== null) |
---|
572 | { |
---|
573 | |
---|
574 | $query = ' |
---|
575 | DELETE |
---|
576 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
577 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
578 | AND criteria_id = '.$criteria_id.' |
---|
579 | '; |
---|
580 | $result = pwg_query($query); |
---|
581 | |
---|
582 | if($result) |
---|
583 | return true; |
---|
584 | else |
---|
585 | return false; |
---|
586 | } |
---|
587 | else |
---|
588 | return null; |
---|
589 | |
---|
590 | } |
---|
591 | |
---|
592 | // Delete a criteria (with id) from a concours |
---|
593 | function delete_criteria_by_id($id) |
---|
594 | { |
---|
595 | |
---|
596 | $query = ' |
---|
597 | DELETE |
---|
598 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
599 | WHERE id =' . $id . ' |
---|
600 | '; |
---|
601 | $result = pwg_query($query); |
---|
602 | |
---|
603 | } |
---|
604 | |
---|
605 | // check if a result is already present in the database |
---|
606 | function is_result_present($concours_id = null) |
---|
607 | { |
---|
608 | // recover all img_id from the category |
---|
609 | $query = 'SELECT DISTINCT(id_concours)' |
---|
610 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
611 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).';'; |
---|
612 | |
---|
613 | $result = pwg_query($query); |
---|
614 | // For each images |
---|
615 | if (pwg_db_fetch_array($result)) |
---|
616 | return true; |
---|
617 | else |
---|
618 | return false; |
---|
619 | |
---|
620 | } |
---|
621 | |
---|
622 | |
---|
623 | |
---|
624 | |
---|
625 | // function to return the number of votes for a concours by images |
---|
626 | function nb_votes_by_img($concours_id = NULL) |
---|
627 | { |
---|
628 | |
---|
629 | // nb of users who vote for each image (for a selected concours |
---|
630 | $query = 'SELECT img_id, COUNT(DISTINCT user_id) AS NBVOTE FROM ' . CONCOURS_DATA_TABLE |
---|
631 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
632 | .' AND ipguest IS NULL' |
---|
633 | .' GROUP BY img_id ' |
---|
634 | .';'; |
---|
635 | |
---|
636 | $result = pwg_query($query); |
---|
637 | $nbvotes = array(); |
---|
638 | while ($row = pwg_db_fetch_array($result)) |
---|
639 | { |
---|
640 | $nbvotes[$row['img_id']] = $row['NBVOTE']; |
---|
641 | } |
---|
642 | |
---|
643 | // Add guest infos |
---|
644 | $query = 'SELECT img_id, COUNT(DISTINCT ipguest) AS NBVOTE FROM ' . CONCOURS_DATA_TABLE |
---|
645 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
646 | .' AND ipguest IS NOT NULL' |
---|
647 | .' GROUP BY img_id ' |
---|
648 | .';'; |
---|
649 | |
---|
650 | $result = pwg_query($query); |
---|
651 | while ($row = pwg_db_fetch_array($result)) |
---|
652 | |
---|
653 | if (!isset($nbvotes[$row['img_id']])) |
---|
654 | $nbvotes[$row['img_id']] = $row['NBVOTE']; |
---|
655 | else |
---|
656 | $nbvotes[$row['img_id']] += $row['NBVOTE']; |
---|
657 | |
---|
658 | return $nbvotes; |
---|
659 | } |
---|
660 | |
---|
661 | |
---|
662 | |
---|
663 | // Get All user that are vote for guest who have vote (IP Stores in db) in a concours |
---|
664 | function get_user_list($concours_id = NULL) |
---|
665 | { |
---|
666 | global $conf; |
---|
667 | $userid = array(); |
---|
668 | if ($concours_id!== null or $this->concours_id !== null) |
---|
669 | { |
---|
670 | $query = ' |
---|
671 | SELECT distinct(user_id) |
---|
672 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
673 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . ' |
---|
674 | AND ipguest IS NULL |
---|
675 | AND user_id !='.$conf['guest_id'] |
---|
676 | |
---|
677 | .' ORDER BY user_id |
---|
678 | '; |
---|
679 | $result = pwg_query($query); |
---|
680 | while ($row = pwg_db_fetch_array($result)) |
---|
681 | { |
---|
682 | array_push($userid, $row['user_id']); |
---|
683 | } |
---|
684 | } |
---|
685 | return $userid; |
---|
686 | } |
---|
687 | |
---|
688 | |
---|
689 | // Get All iaddr for guest who have vote (IP Stores in db) in a concours |
---|
690 | function get_guest_list($concours_id = NULL) |
---|
691 | { |
---|
692 | global $conf; |
---|
693 | $ipguest = array(); |
---|
694 | if ($concours_id!== null or $this->concours_id !== null) |
---|
695 | { |
---|
696 | $query = ' |
---|
697 | SELECT distinct(ipguest) |
---|
698 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
699 | WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
700 | .' AND user_id = '.$conf['guest_id'] |
---|
701 | .' AND ipguest IS NOT NULL |
---|
702 | ORDER BY ipguest |
---|
703 | '; |
---|
704 | $result = pwg_query($query); |
---|
705 | while ($row = pwg_db_fetch_array($result)) |
---|
706 | { |
---|
707 | array_push($ipguest, $row['ipguest']); |
---|
708 | } |
---|
709 | if ($this->debug) |
---|
710 | { |
---|
711 | foreach ($ipguest as $ip) |
---|
712 | echo "IP=".$ip."\n"; |
---|
713 | } |
---|
714 | |
---|
715 | return $ipguest; |
---|
716 | } |
---|
717 | else |
---|
718 | return $ipguest; |
---|
719 | } |
---|
720 | |
---|
721 | |
---|
722 | // After concours is completed (closed date is passed), generate the result and store it to DB |
---|
723 | function create_result($concours_id = NULL) |
---|
724 | { |
---|
725 | global $conf; |
---|
726 | // var which contains id=img_id and val =global note |
---|
727 | $global_note = array(); |
---|
728 | // FOR MODERATION |
---|
729 | // var which contains id=user_id and val =global note |
---|
730 | $vote_by_user = array(); |
---|
731 | $amplitiudeMINmax = 999; // MIN of all max score (for each users) |
---|
732 | $amplitiudeMAXmin = -999; // MIN of all max score (for each users) |
---|
733 | $REFaverage = 0; // reference average (average between amplitiudeMINmax and amplitiudeMAXmin) |
---|
734 | $global_note_mod1 = array(); |
---|
735 | $global_note_mod2 = array(); |
---|
736 | |
---|
737 | if ($concours_id!== null or $this->concours_id !== null) |
---|
738 | { |
---|
739 | // only for closed concours and not already result generated |
---|
740 | if ($this->is_closed($concours_id) AND ! $this->is_result_present($concours_id)) |
---|
741 | { |
---|
742 | |
---|
743 | // array with the number of vote by image for the concours. Needed for the moyenne |
---|
744 | $nbvotes = $this->nb_votes_by_img($concours_id); |
---|
745 | |
---|
746 | |
---|
747 | $user_id = array(); |
---|
748 | // vars not initialized |
---|
749 | if ($this->concours_infos == array()) |
---|
750 | $this->get_concours(); |
---|
751 | $category = $this->concours_infos['category']; |
---|
752 | |
---|
753 | // Get all user_id from a concours |
---|
754 | $query = 'SELECT DISTINCT user_id' |
---|
755 | .' FROM ' .CONCOURS_DATA_TABLE |
---|
756 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
757 | .' AND user_id <> '.$conf['guest_id'] |
---|
758 | .';'; |
---|
759 | $result = pwg_query($query); |
---|
760 | |
---|
761 | while ($row = pwg_db_fetch_array($result)) |
---|
762 | array_push($user_id, $row['user_id']); |
---|
763 | |
---|
764 | // Add guest info is present |
---|
765 | $ipguest = array(); |
---|
766 | if ($concours_id!== null or $this->concours_id !== null) |
---|
767 | { |
---|
768 | if ($this->debug) |
---|
769 | echo "Check IPGUEST"; |
---|
770 | $ipguest = $this->get_guest_list(($concours_id !== null ? $concours_id : $this->concours_id )); |
---|
771 | } |
---|
772 | |
---|
773 | |
---|
774 | // recover all img_id from the category |
---|
775 | $query = 'SELECT DISTINCT(image_id)' |
---|
776 | .' FROM ' .IMAGE_CATEGORY_TABLE |
---|
777 | .' WHERE category_id = '.$category.';'; |
---|
778 | |
---|
779 | $nb_image_concours = 0; |
---|
780 | $result = pwg_query($query); |
---|
781 | // For each images |
---|
782 | while ($row = pwg_db_fetch_array($result)) |
---|
783 | { |
---|
784 | $nb_image_concours++; |
---|
785 | |
---|
786 | foreach ($user_id as $i => $userid) |
---|
787 | { |
---|
788 | $img_note_user = $this->get_img_globalnote_user($row['image_id'], null, $userid); |
---|
789 | |
---|
790 | if (!isset($global_note[$row['image_id']])) |
---|
791 | $global_note[$row['image_id']] = 0; |
---|
792 | // FOR MODERATION |
---|
793 | // store the nb of vote for each user |
---|
794 | if ($img_note_user >= 0) // user has made a vote |
---|
795 | { |
---|
796 | if (!isset($vote_by_user[$userid]['nbvote'])) |
---|
797 | $vote_by_user[$userid]['nbvote'] = 0; |
---|
798 | |
---|
799 | $vote_by_user[$userid]['nbvote'] ++; // update vote counter |
---|
800 | |
---|
801 | $global_note[$row['image_id']] += $img_note_user ; |
---|
802 | |
---|
803 | |
---|
804 | // FOR MODERATION |
---|
805 | // store the total of note for the same user |
---|
806 | if (!isset($vote_by_user[$userid]['totalvote'])) |
---|
807 | $vote_by_user[$userid]['totalvote'] = 0; |
---|
808 | $vote_by_user[$userid]['totalvote'] += $img_note_user; |
---|
809 | // FOR MODERATION |
---|
810 | // store min/max for each user |
---|
811 | if (!isset($vote_by_user[$userid]['min'])) $vote_by_user[$userid]['min'] = 999; |
---|
812 | if (!isset($vote_by_user[$userid]['max'])) $vote_by_user[$userid]['max'] = 0; |
---|
813 | |
---|
814 | |
---|
815 | if ($vote_by_user[$userid]['min'] > $img_note_user) $vote_by_user[$userid]['min'] = $img_note_user; |
---|
816 | if ($vote_by_user[$userid]['max'] < $img_note_user) $vote_by_user[$userid]['max'] = $img_note_user; |
---|
817 | } |
---|
818 | |
---|
819 | } |
---|
820 | // Add guest scores if present |
---|
821 | foreach ($ipguest as $i => $userid) |
---|
822 | { |
---|
823 | |
---|
824 | $img_note_user = $this->get_img_globalnote_user($row['image_id'], null, null, $userid); // $userid contains the ip address for the guest! |
---|
825 | if (!isset($global_note[$row['image_id']])) |
---|
826 | $global_note[$row['image_id']] = 0; |
---|
827 | |
---|
828 | // FOR MODERATION |
---|
829 | // store the nb of vote for each guest (with ipaddr) |
---|
830 | if ($img_note_user >= 0) // user has made a vote |
---|
831 | { |
---|
832 | if (!isset($vote_by_user[$userid]['nbvote'])) |
---|
833 | $vote_by_user[$userid]['nbvote'] = 0; |
---|
834 | |
---|
835 | $vote_by_user[$userid]['nbvote'] ++; // update vote counter |
---|
836 | |
---|
837 | $global_note[$row['image_id']] += $img_note_user ; |
---|
838 | |
---|
839 | |
---|
840 | // FOR MODERATION |
---|
841 | // store the total of note for the same user |
---|
842 | if (!isset($vote_by_user[$userid]['totalvote'])) |
---|
843 | $vote_by_user[$userid]['totalvote'] = 0; |
---|
844 | $vote_by_user[$userid]['totalvote'] += $img_note_user; |
---|
845 | // FOR MODERATION |
---|
846 | // store min/max for each user |
---|
847 | if (!isset($vote_by_user[$userid]['min'])) $vote_by_user[$userid]['min'] = 999; |
---|
848 | if (!isset($vote_by_user[$userid]['max'])) $vote_by_user[$userid]['max'] = 0; |
---|
849 | |
---|
850 | |
---|
851 | if ($vote_by_user[$userid]['min'] > $img_note_user) $vote_by_user[$userid]['min'] = $img_note_user; |
---|
852 | if ($vote_by_user[$userid]['max'] < $img_note_user) $vote_by_user[$userid]['max'] = $img_note_user; |
---|
853 | } |
---|
854 | |
---|
855 | } |
---|
856 | } |
---|
857 | |
---|
858 | // FOR MODERATION |
---|
859 | // calcul the average by user and min/max global |
---|
860 | foreach ($user_id as $i => $userid) |
---|
861 | { |
---|
862 | $vote_by_user[$userid]['average'] = $vote_by_user[$userid]['totalvote']/$vote_by_user[$userid]['nbvote']; |
---|
863 | $vote_by_user[$userid]['lowerAmp'] = $vote_by_user[$userid]['min']; |
---|
864 | // calcul the max value of the concours (for the moderation) |
---|
865 | $vote_by_user[$userid]['upperAmp'] = $this->get_MaxScore_concours() - $vote_by_user[$userid]['max']; |
---|
866 | |
---|
867 | $vote_by_user[$userid]['minLevel'] = $vote_by_user[$userid]['average'] - $vote_by_user[$userid]['lowerAmp']; |
---|
868 | $vote_by_user[$userid]['maxLevel'] = $vote_by_user[$userid]['average'] + $vote_by_user[$userid]['upperAmp']; |
---|
869 | if ($amplitiudeMINmax > $vote_by_user[$userid]['maxLevel']) $amplitiudeMINmax = $vote_by_user[$userid]['maxLevel']; |
---|
870 | if ($amplitiudeMAXmin < $vote_by_user[$userid]['minLevel']) $amplitiudeMAXmin = $vote_by_user[$userid]['minLevel']; |
---|
871 | } |
---|
872 | |
---|
873 | // calcul the average by guest and min/max global |
---|
874 | foreach ($ipguest as $i => $userid) |
---|
875 | { |
---|
876 | $vote_by_user[$userid]['average'] = $vote_by_user[$userid]['totalvote']/$vote_by_user[$userid]['nbvote']; |
---|
877 | $vote_by_user[$userid]['lowerAmp'] = $vote_by_user[$userid]['min']; |
---|
878 | // calcul the max value of the concours (for the moderation) |
---|
879 | $vote_by_user[$userid]['upperAmp'] = $this->get_MaxScore_concours() - $vote_by_user[$userid]['max']; |
---|
880 | |
---|
881 | $vote_by_user[$userid]['minLevel'] = $vote_by_user[$userid]['average'] - $vote_by_user[$userid]['lowerAmp']; |
---|
882 | $vote_by_user[$userid]['maxLevel'] = $vote_by_user[$userid]['average'] + $vote_by_user[$userid]['upperAmp']; |
---|
883 | if ($amplitiudeMINmax > $vote_by_user[$userid]['maxLevel']) $amplitiudeMINmax = $vote_by_user[$userid]['maxLevel']; |
---|
884 | if ($amplitiudeMAXmin < $vote_by_user[$userid]['minLevel']) $amplitiudeMAXmin = $vote_by_user[$userid]['minLevel']; |
---|
885 | |
---|
886 | } |
---|
887 | |
---|
888 | |
---|
889 | // FOR MODERATION |
---|
890 | // calcul the reference average |
---|
891 | $calcultemp = (int)($amplitiudeMINmax+$amplitiudeMAXmin)/2+0.5; |
---|
892 | if ((int)(($amplitiudeMINmax+$amplitiudeMAXmin)/2+0.5) > $amplitiudeMAXmin) |
---|
893 | { |
---|
894 | if ((int)(($amplitiudeMINmax+$amplitiudeMAXmin)/2+0.5) < $amplitiudeMINmax) |
---|
895 | $REFaverage = (int)(($amplitiudeMINmax+$amplitiudeMAXmin)/2+0.5); |
---|
896 | else |
---|
897 | $REFaverage = $amplitiudeMINmax; |
---|
898 | } |
---|
899 | else |
---|
900 | $REFaverage = $amplitiudeMAXmin; |
---|
901 | |
---|
902 | |
---|
903 | // FOR MODERATION |
---|
904 | // calcul the moderation scopr for each users |
---|
905 | foreach ($user_id as $i => $userid) |
---|
906 | $vote_by_user[$userid]['moderation'] = $REFaverage - $vote_by_user[$userid]['average']; |
---|
907 | |
---|
908 | // FOR MODERATION |
---|
909 | // calcul the moderation scopr for each guest ($ipguest) |
---|
910 | foreach ($ipguest as $i => $userid) |
---|
911 | $vote_by_user[$userid]['moderation'] = $REFaverage - $vote_by_user[$userid]['average']; |
---|
912 | |
---|
913 | // FOR MODERATION |
---|
914 | // need to parse again image for new calcul with moderation |
---|
915 | // moderation 1 => For each photo without note (user = author or user != author but no vote) add each user avaerage to the total of note and calcul the average |
---|
916 | // moderation 2 => For each photo with note, adapt/change all the value with the moderation value : user note + moderation. |
---|
917 | |
---|
918 | // parse again all img_id from the category |
---|
919 | $query = 'SELECT DISTINCT(image_id)' |
---|
920 | .' FROM ' .IMAGE_CATEGORY_TABLE |
---|
921 | .' WHERE category_id = '.$category.';'; |
---|
922 | |
---|
923 | $result = pwg_query($query); |
---|
924 | // For each images |
---|
925 | while ($row = pwg_db_fetch_array($result)) |
---|
926 | { |
---|
927 | // for each user |
---|
928 | foreach ($user_id as $i => $userid) |
---|
929 | { |
---|
930 | if (!isset($global_note_mod1[$row['image_id']])) |
---|
931 | $global_note_mod1[$row['image_id']] = 0; |
---|
932 | $globalnote_user = $this->get_img_globalnote_user($row['image_id'], null, $userid, null, 1, $vote_by_user[$userid]['average']); |
---|
933 | |
---|
934 | $global_note_mod1[$row['image_id']] += ($globalnote_user >= 0 ? $globalnote_user : 0) ; |
---|
935 | |
---|
936 | if (!isset($global_note_mod2[$row['image_id']])) |
---|
937 | $global_note_mod2[$row['image_id']] = 0; |
---|
938 | $globalnote_user = $this->get_img_globalnote_user($row['image_id'], null, $userid, null, 2, $vote_by_user[$userid]['moderation']); |
---|
939 | |
---|
940 | $global_note_mod2[$row['image_id']] += ($globalnote_user >= 0 ? $globalnote_user : 0) ; |
---|
941 | } |
---|
942 | |
---|
943 | // for each guest |
---|
944 | foreach ($ipguest as $i => $userid) |
---|
945 | { |
---|
946 | if (!isset($global_note_mod1[$row['image_id']])) |
---|
947 | $global_note_mod1[$row['image_id']] = 0; |
---|
948 | $globalnote_user = $this->get_img_globalnote_user($row['image_id'], null, null, $userid, 1, $vote_by_user[$userid]['average']); // userid contains the ip addr for the guest |
---|
949 | |
---|
950 | $global_note_mod1[$row['image_id']] += ($globalnote_user >= 0 ? $globalnote_user : 0) ; |
---|
951 | |
---|
952 | if (!isset($global_note_mod2[$row['image_id']])) |
---|
953 | $global_note_mod2[$row['image_id']] = 0; |
---|
954 | $globalnote_user = $this->get_img_globalnote_user($row['image_id'], null, null, $userid, 2, $vote_by_user[$userid]['moderation']); |
---|
955 | |
---|
956 | $global_note_mod2[$row['image_id']] += ($globalnote_user >= 0 ? $globalnote_user : 0) ; |
---|
957 | } |
---|
958 | |
---|
959 | |
---|
960 | } |
---|
961 | |
---|
962 | |
---|
963 | // update database and store result into RESULT table |
---|
964 | // note = global note |
---|
965 | $IMGmoyenne = 0; |
---|
966 | $IMGmoyenneModeration1 = 0; |
---|
967 | $IMGmoyenneModeration2 = 0; |
---|
968 | |
---|
969 | foreach ($global_note as $id => $val) |
---|
970 | // id contains the image id |
---|
971 | { |
---|
972 | $IMGmoyenne = (isset($nbvotes[$id]) && $nbvotes[$id] != 0 ? $val/$nbvotes[$id] : 0); |
---|
973 | // Moderation |
---|
974 | |
---|
975 | $IMGmoyenneModeration1 = ($nb_image_concours != 0 ? $global_note_mod1[$id]/$this->nb_user_by_concours() : 0); // all photos have note |
---|
976 | $IMGmoyenneModeration2 = (isset($nbvotes[$id]) && $nbvotes[$id] != 0 ? $global_note_mod2[$id]/$nbvotes[$id] : 0); // only voted photos are used |
---|
977 | |
---|
978 | $query = 'INSERT INTO ' . CONCOURS_RESULT_TABLE .'(`id_concours`, `img_id`, `date` , `note`, `moyenne`, |
---|
979 | `moderation1`, `moderation2`, |
---|
980 | `nbvotant` ) |
---|
981 | VALUES ('.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
982 | .', '.$id |
---|
983 | .', now() ' |
---|
984 | .', '.$val |
---|
985 | .', '.$IMGmoyenne |
---|
986 | .', '.$IMGmoyenneModeration1 |
---|
987 | .', '.$IMGmoyenneModeration2 |
---|
988 | .', '.(isset($nbvotes[$id]) ? $nbvotes[$id] : 0) |
---|
989 | .');'; |
---|
990 | $result = pwg_query($query); |
---|
991 | |
---|
992 | } |
---|
993 | } |
---|
994 | else |
---|
995 | return false; |
---|
996 | } |
---|
997 | else |
---|
998 | return false; |
---|
999 | } |
---|
1000 | |
---|
1001 | |
---|
1002 | // Get array with nb of vote for each user of a concours_id |
---|
1003 | function nb_user_by_concours($concours_id = NULL) |
---|
1004 | { |
---|
1005 | $nbusertotal = 0; |
---|
1006 | // user |
---|
1007 | $query = 'SELECT count(distinct(user_id)) as nb_user FROM ' . CONCOURS_DATA_TABLE .' |
---|
1008 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id).' |
---|
1009 | AND ipguest IS NULL |
---|
1010 | ;'; |
---|
1011 | $result = pwg_query($query); |
---|
1012 | if ($result) |
---|
1013 | { |
---|
1014 | $row = pwg_db_fetch_array($result); |
---|
1015 | $nbusertotal += $row['nb_user']; |
---|
1016 | } |
---|
1017 | // guest |
---|
1018 | $query = 'SELECT count(distinct(ipguest)) as nb_user FROM ' . CONCOURS_DATA_TABLE .' |
---|
1019 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id).' |
---|
1020 | AND ipguest IS NOT NULL |
---|
1021 | ;'; |
---|
1022 | $result = pwg_query($query); |
---|
1023 | if ($result) |
---|
1024 | { |
---|
1025 | $row = pwg_db_fetch_array($result); |
---|
1026 | $nbusertotal += $row['nb_user']; |
---|
1027 | } |
---|
1028 | return $nbusertotal; |
---|
1029 | |
---|
1030 | } |
---|
1031 | |
---|
1032 | |
---|
1033 | // Check if an image's author OR addebBy is the same as the actual user |
---|
1034 | // depending the config param |
---|
1035 | function check_img_user($img_id) |
---|
1036 | { |
---|
1037 | global $user; |
---|
1038 | |
---|
1039 | |
---|
1040 | $query = ' |
---|
1041 | SELECT author, added_by |
---|
1042 | FROM ' . IMAGES_TABLE .' |
---|
1043 | WHERE id =' . $img_id . ' |
---|
1044 | '; |
---|
1045 | |
---|
1046 | $result = pwg_query($query); |
---|
1047 | if ($this->debug) echo $query."\n"; |
---|
1048 | if ($result) |
---|
1049 | { |
---|
1050 | $row = pwg_db_fetch_array($result); |
---|
1051 | $authorid = get_userid($row['author']); |
---|
1052 | $addedbyid = $row['added_by']; |
---|
1053 | |
---|
1054 | switch ($this->my_config['author_vote_available']) |
---|
1055 | { |
---|
1056 | |
---|
1057 | case 0 : // no check (allow to vote for all photos) |
---|
1058 | return false; |
---|
1059 | |
---|
1060 | case 1 : // check between author and user |
---|
1061 | if ($authorid) // Author name present as username |
---|
1062 | { |
---|
1063 | if ($authorid == $user['id']) |
---|
1064 | return true; |
---|
1065 | else |
---|
1066 | return false; |
---|
1067 | } |
---|
1068 | else |
---|
1069 | { |
---|
1070 | // check if author name = user name |
---|
1071 | $AuthorArray = strip_punctuation2($row['author']); |
---|
1072 | $UserArray = strip_punctuation2($user['username']); |
---|
1073 | // Patch if author is empty |
---|
1074 | if (empty($AuthorArray)) |
---|
1075 | return false; |
---|
1076 | |
---|
1077 | if (count(array_intersect($AuthorArray, $UserArray)) == count ($AuthorArray)) // found |
---|
1078 | return true; |
---|
1079 | else |
---|
1080 | return false; |
---|
1081 | } |
---|
1082 | |
---|
1083 | |
---|
1084 | case 2 : // check between author and user |
---|
1085 | if (isset($addedbyid) AND $addedbyid == $user['id']) |
---|
1086 | return true; |
---|
1087 | else |
---|
1088 | return false; |
---|
1089 | case 3 : // check between author OR addedby and user |
---|
1090 | // Check Author |
---|
1091 | if ($authorid AND $authorid == $user['id']) // Author name present as username |
---|
1092 | return true; |
---|
1093 | else |
---|
1094 | { |
---|
1095 | // check if author name = user name |
---|
1096 | $AuthorArray = strip_punctuation2($row['author']); |
---|
1097 | $UserArray = strip_punctuation2($user['username']); |
---|
1098 | if (empty($AuthorArray) AND (count(array_intersect($AuthorArray, $UserArray)) == count ($AuthorArray))) // found |
---|
1099 | return true; |
---|
1100 | } |
---|
1101 | // check addebBy |
---|
1102 | if (isset($addedbyid) AND $addedbyid == $user['id']) |
---|
1103 | return true; |
---|
1104 | |
---|
1105 | return false; |
---|
1106 | |
---|
1107 | default : |
---|
1108 | return false; |
---|
1109 | |
---|
1110 | } |
---|
1111 | |
---|
1112 | } |
---|
1113 | return false; |
---|
1114 | } |
---|
1115 | |
---|
1116 | |
---|
1117 | // Add tpl to picture.php page to display concours informations and vote |
---|
1118 | function display_concours_to_picture() |
---|
1119 | { |
---|
1120 | // Step0 : if user = guest, check the concours param and store ip address to user name. |
---|
1121 | // Step1 : concours is defined to this category AND concours is open AND user is authorized to access to this category (thru group) |
---|
1122 | // Step1 bis : dont show concours if check author or added_by id is the samie as used_id (depending the param) |
---|
1123 | // Step 2 : Recover stored informations in db for this user |
---|
1124 | // Step 3 : Complete tpl information |
---|
1125 | // Step 4 : concat tpl |
---|
1126 | |
---|
1127 | global $page, $user, $template, $conf; |
---|
1128 | |
---|
1129 | // Get user group. |
---|
1130 | $this->get_user_groups(); |
---|
1131 | |
---|
1132 | $concours = array(); |
---|
1133 | |
---|
1134 | // STEP 0 |
---|
1135 | $user['ipguest'] = null; |
---|
1136 | |
---|
1137 | // Check if the user is a guest only |
---|
1138 | if (is_a_guest()) |
---|
1139 | { |
---|
1140 | if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) |
---|
1141 | $IP = $_SERVER['HTTP_X_FORWARDED_FOR']; |
---|
1142 | elseif(isset($_SERVER['HTTP_CLIENT_IP'])) |
---|
1143 | $IP = $_SERVER['HTTP_CLIENT_IP']; |
---|
1144 | else |
---|
1145 | $IP = $_SERVER['REMOTE_ADDR']; |
---|
1146 | |
---|
1147 | // store ip |
---|
1148 | $user['ipguest'] = $IP; |
---|
1149 | } |
---|
1150 | |
---|
1151 | |
---|
1152 | // disable author name on image which are present in a concours. |
---|
1153 | // check the categories where the current image is present and disable the author name |
---|
1154 | if (isset($this->my_config['mask_author']) && $this->my_config['mask_author'] == true) |
---|
1155 | { |
---|
1156 | // Get all categories where the current image is present |
---|
1157 | $query = ' |
---|
1158 | SELECT category_id,uppercats,commentable,global_rank |
---|
1159 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1160 | INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id |
---|
1161 | WHERE image_id = '.$page['image_id'].' |
---|
1162 | ;'; |
---|
1163 | $result = pwg_query($query); |
---|
1164 | $related_categories = array(); |
---|
1165 | while ($row = pwg_db_fetch_array($result)) |
---|
1166 | { |
---|
1167 | array_push($related_categories, $row['category_id']); |
---|
1168 | } |
---|
1169 | // |
---|
1170 | //echo related_categories; |
---|
1171 | if (count($related_categories)) |
---|
1172 | { |
---|
1173 | // Request for all concours prepared & actived on each categories |
---|
1174 | $query = ' |
---|
1175 | SELECT * |
---|
1176 | FROM ' . CONCOURS_TABLE .' |
---|
1177 | WHERE category IN ('.implode(',', $related_categories).') |
---|
1178 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
1179 | '; |
---|
1180 | $result = pwg_query($query); |
---|
1181 | // If one or more concours are found, the author name is masked |
---|
1182 | if ($result && pwg_db_fetch_array($result)) |
---|
1183 | $template->assign('INFO_AUTHOR',l10n('concours_img_author')); |
---|
1184 | } |
---|
1185 | } |
---|
1186 | |
---|
1187 | // End disable author name |
---|
1188 | |
---|
1189 | |
---|
1190 | if (($page['section']) == 'categories' AND !empty($page['category'])) |
---|
1191 | { |
---|
1192 | // Step 1 |
---|
1193 | |
---|
1194 | $query = ' |
---|
1195 | SELECT * |
---|
1196 | FROM ' . CONCOURS_TABLE .' |
---|
1197 | WHERE category =' . $page['category']['id'] . ' |
---|
1198 | AND time_to_sec(TIMEDIFF(begin_date,now())) < 0 |
---|
1199 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
1200 | '; |
---|
1201 | |
---|
1202 | $result = pwg_query($query); |
---|
1203 | |
---|
1204 | while ($row = pwg_db_fetch_array($result)) |
---|
1205 | { |
---|
1206 | if (!is_a_guest() AND !empty($row['groups'])) |
---|
1207 | { |
---|
1208 | |
---|
1209 | if (is_admin() AND $row['admin']) // allow admin |
---|
1210 | $concours = $row; |
---|
1211 | else |
---|
1212 | { |
---|
1213 | $authorized_groups = explode(',', $row['groups']); |
---|
1214 | if (array_intersect($this->user_groups, $authorized_groups) == array()) |
---|
1215 | continue; |
---|
1216 | // If no group is parameter for that concours ==> available for all registered users |
---|
1217 | $concours = $row; |
---|
1218 | } |
---|
1219 | } |
---|
1220 | else |
---|
1221 | |
---|
1222 | $concours = $row; |
---|
1223 | } |
---|
1224 | //------------ |
---|
1225 | // Step 1 bis |
---|
1226 | // Actual user is the author of the picture ==>end |
---|
1227 | // for the image, check (depending the config) if user = author or addedby or none |
---|
1228 | if ($this->check_img_user($page['current_item'])) |
---|
1229 | return; |
---|
1230 | |
---|
1231 | // Check if user is guest and if guest is allowed |
---|
1232 | if (is_a_guest() AND array_key_exists('guest', $concours) AND !$concours['guest']) |
---|
1233 | return; |
---|
1234 | // Check if user is admin and if admin is allowed |
---|
1235 | if (is_admin() AND (!isset($concours['admin']) OR (isset($concours['admin']) AND !$concours['admin']))) |
---|
1236 | return; |
---|
1237 | |
---|
1238 | //------------ |
---|
1239 | // Step 2 |
---|
1240 | if ($concours != array()) |
---|
1241 | { |
---|
1242 | // If user validate the notation |
---|
1243 | if (isset($_POST['concours_submit'])) |
---|
1244 | { |
---|
1245 | |
---|
1246 | // array_push($page['infos'] , l10n('concours_vote_saved')); |
---|
1247 | |
---|
1248 | $user_note = ""; |
---|
1249 | |
---|
1250 | // concat all the notes to save on db |
---|
1251 | $firstcriterias = $this->get_firstlevel_criterias($concours['id']); |
---|
1252 | foreach ($firstcriterias as $criteria) |
---|
1253 | { |
---|
1254 | // First without sub criterias |
---|
1255 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] )) |
---|
1256 | { |
---|
1257 | // Check value |
---|
1258 | $value = str_replace(",",".",(isset($_POST[$criteria['criteria_id']]) ? $_POST[$criteria['criteria_id']] : floatval($criteria['min_value']))); |
---|
1259 | $value = str_replace(" ","",$value); |
---|
1260 | $value = floatval($value); |
---|
1261 | if ($value < floatval($criteria['min_value'])) |
---|
1262 | $value = floatval($criteria['min_value']); |
---|
1263 | if ($value > floatval($criteria['max_value'])) |
---|
1264 | $value = floatval($criteria['max_value']); |
---|
1265 | |
---|
1266 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$criteria['criteria_id']."=".$value; |
---|
1267 | } |
---|
1268 | else |
---|
1269 | { |
---|
1270 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours['id'] ); |
---|
1271 | foreach ($secondcriterias as $subcriteria) |
---|
1272 | { |
---|
1273 | // Check value |
---|
1274 | $value = str_replace(",",".",(isset($_POST[$subcriteria['criteria_id']]) ? $_POST[$subcriteria['criteria_id']] : floatval($subcriteria['min_value'] ))); |
---|
1275 | $value = str_replace(" ","",$value); |
---|
1276 | $value = floatval($value); |
---|
1277 | if ($value < floatval($subcriteria['min_value'])) |
---|
1278 | $value = floatval($subcriteria['min_value']); |
---|
1279 | if ($value > floatval($subcriteria['max_value'])) |
---|
1280 | $value = floatval($subcriteria['max_value']); |
---|
1281 | |
---|
1282 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$subcriteria['criteria_id']."=".$value; |
---|
1283 | } |
---|
1284 | } |
---|
1285 | } |
---|
1286 | |
---|
1287 | $this->store_img_note_user($page['current_item'], $user_note, $concours['id'], $user['ipguest']); |
---|
1288 | $this->store_img_comment_user($page['current_item'], $_POST['concours_comment'], $concours['id'], $user['ipguest']); |
---|
1289 | } |
---|
1290 | |
---|
1291 | // If user want to erase notes |
---|
1292 | if (isset($_POST['concours_raz'])) |
---|
1293 | { |
---|
1294 | $this->delete_img_note_user($page['current_item'], $concours['id'], $user['ipguest']); |
---|
1295 | $user_notes = array(); |
---|
1296 | } |
---|
1297 | else |
---|
1298 | { |
---|
1299 | // Recover previous note in DB (if exists) |
---|
1300 | $user_notes = $this->get_img_note_user($page['current_item'], $concours['id'], $user['id'], $user['ipguest']); |
---|
1301 | } |
---|
1302 | |
---|
1303 | // Comment is not RAZ, always restore |
---|
1304 | $comment = $this->get_img_comment_user($page['current_item'], $concours['id'], $user['id'], $user['ipguest']); |
---|
1305 | |
---|
1306 | //------------ |
---|
1307 | // Step 3 |
---|
1308 | // Recover all 1st level criterias |
---|
1309 | $firstcriterias = $this->get_firstlevel_criterias($concours['id']); |
---|
1310 | foreach ($firstcriterias as $criteria) |
---|
1311 | { |
---|
1312 | // First without sub criterias |
---|
1313 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] )) |
---|
1314 | { |
---|
1315 | $template->append( 'concours_criteria', array( |
---|
1316 | 'nosub' => true, |
---|
1317 | 'level' => 1, |
---|
1318 | 'id' => $criteria['criteria_id'], // id du critere |
---|
1319 | 'name' => $criteria['name'], // id du critere |
---|
1320 | 'lib' => $criteria['descr'], //.'(min='$criteria['min_value'].';max='.$criteria['min_value'].')', // libelle du critrer |
---|
1321 | 'val' => (isset($user_notes[$criteria['criteria_id']])?$user_notes[$criteria['criteria_id']] : $criteria['min_value']), // valeur du critere |
---|
1322 | 'min' => $criteria['min_value'], // min |
---|
1323 | 'max' => $criteria['max_value'] // max |
---|
1324 | )); |
---|
1325 | } |
---|
1326 | else |
---|
1327 | { |
---|
1328 | $template->append( 'concours_criteria', array( |
---|
1329 | 'nosub' => false, |
---|
1330 | 'level' => 1, |
---|
1331 | 'id' => $criteria['criteria_id'], // id du critere |
---|
1332 | 'name' => $criteria['name'], // id du critere |
---|
1333 | 'lib' => $criteria['descr'] |
---|
1334 | )); |
---|
1335 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours['id'] ); |
---|
1336 | foreach ($secondcriterias as $subcriteria) |
---|
1337 | { |
---|
1338 | $template->append( 'concours_criteria', array( |
---|
1339 | 'nosub' => true, |
---|
1340 | 'level' => 2, |
---|
1341 | 'id' => $subcriteria['criteria_id'], // id du critere |
---|
1342 | 'name' => $subcriteria['name'], // id du critere |
---|
1343 | 'lib' => $subcriteria['descr'], //.'(min='$criteria['min_value'].';max='.$criteria['min_value'].')', // libelle du critrer |
---|
1344 | 'val' => (isset($user_notes[$subcriteria['criteria_id']])?$user_notes[$subcriteria['criteria_id']] : $subcriteria['min_value']), |
---|
1345 | 'min' => $subcriteria['min_value'], // min |
---|
1346 | 'max' => $subcriteria['max_value'] // max |
---|
1347 | )); |
---|
1348 | |
---|
1349 | } |
---|
1350 | } |
---|
1351 | |
---|
1352 | } |
---|
1353 | // Add the comment |
---|
1354 | $template->assign( 'CONCOURS_COMMENT', $comment); |
---|
1355 | |
---|
1356 | // if a user has already vote for this photo, check if the score change is allowed |
---|
1357 | |
---|
1358 | |
---|
1359 | if ($user_notes == array() OR $this->my_config['concours_change_score']) // Allow user to change his vote after a validation |
---|
1360 | $template->assign( 'CONCOURS_CHANGE_SCORE', true); |
---|
1361 | else |
---|
1362 | $template->assign( 'CONCOURS_CHANGE_SCORE', false); |
---|
1363 | |
---|
1364 | $noteuser = $this->get_img_globalnote_user($page['current_item'], $concours['id'], null, $user['ipguest']); |
---|
1365 | // Add concours informations on template |
---|
1366 | $template->assign( 'concours_infos', array( |
---|
1367 | 'name' => $concours['name'], |
---|
1368 | 'descr' => $concours['descr'], |
---|
1369 | 'note' => ($noteuser >= 0 ? $noteuser : 0 ), |
---|
1370 | 'begin_date' => $concours['begin_date'], |
---|
1371 | 'end_date' => $concours['end_date'], |
---|
1372 | 'end_concours_min' => (strtotime($concours['end_date'])-time()) , |
---|
1373 | 'max_note' => $this->get_concours_maxnote($concours['id']), |
---|
1374 | 'deadline_type' => $this->my_config['concours_deadline'] |
---|
1375 | )); |
---|
1376 | |
---|
1377 | |
---|
1378 | $template->assign( 'SCORE_MODE', $this->my_config['score_mode']); |
---|
1379 | $template->assign( 'TEXT_OVERLAY', $this->my_config['text_overlay']); |
---|
1380 | |
---|
1381 | $template->set_filenames(array('concours' => CONCOURS_ROOT.'/template/concours.tpl')); |
---|
1382 | $template->concat('COMMENT_IMG', $template->parse('concours', true)); |
---|
1383 | |
---|
1384 | $template->assign('INFO_AUTHOR',l10n('concours_img_author')); |
---|
1385 | } |
---|
1386 | } |
---|
1387 | } |
---|
1388 | |
---|
1389 | |
---|
1390 | // RAZ notation from user to db |
---|
1391 | // fill criterias notes to 0 in DB |
---|
1392 | // return false if error |
---|
1393 | function delete_img_note_user($img_id, $concours_id = NULL, $ipguest = null, $userid=null) |
---|
1394 | { |
---|
1395 | global $user; |
---|
1396 | |
---|
1397 | if ($concours_id === null) |
---|
1398 | if ($this->concours_id !== null) |
---|
1399 | $concours_id = $this->concours_id; |
---|
1400 | else |
---|
1401 | return false; |
---|
1402 | |
---|
1403 | $query = ' |
---|
1404 | DELETE FROM ' . CONCOURS_DATA_TABLE .' |
---|
1405 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).' |
---|
1406 | AND user_id = '.($userid == null ? $user['id'] : $userid).' |
---|
1407 | AND img_id = '.$img_id. |
---|
1408 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1409 | .';'; |
---|
1410 | |
---|
1411 | return pwg_query($query); |
---|
1412 | |
---|
1413 | } |
---|
1414 | |
---|
1415 | |
---|
1416 | // RAZ notation from user to db |
---|
1417 | // fill criterias notes to 0 in DB |
---|
1418 | // return false if error |
---|
1419 | function RAZ_img_note_user($img_id, $concours_id = NULL, $ipguest = null) |
---|
1420 | { |
---|
1421 | $user_note = ""; |
---|
1422 | if ($concours_id === null) |
---|
1423 | if ($this->concours_id !== null) |
---|
1424 | $concours_id = $this->concours_id; |
---|
1425 | else |
---|
1426 | return false; |
---|
1427 | |
---|
1428 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1429 | foreach ($firstcriterias as $criteria) |
---|
1430 | { |
---|
1431 | // First without sub criterias |
---|
1432 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1433 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$criteria['criteria_id']."=".$criteria['min_value']; |
---|
1434 | else |
---|
1435 | { |
---|
1436 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1437 | foreach ($secondcriterias as $subcriteria) |
---|
1438 | $user_note .= (strlen($user_note) != 0 ? ";" : "").$subcriteria['criteria_id']."=".$subcriteria['min_value']; |
---|
1439 | } |
---|
1440 | } |
---|
1441 | if (strlen($user_note) != 0) |
---|
1442 | { |
---|
1443 | $this->store_img_note_user($img_id, $user_note, $concours_id, $ipguest); |
---|
1444 | return true; |
---|
1445 | } |
---|
1446 | else |
---|
1447 | return false; |
---|
1448 | } |
---|
1449 | |
---|
1450 | // Store notation from user to db |
---|
1451 | // save with format "criteria_id=note;criteria_id=note..." for data |
---|
1452 | function store_img_note_user($img_id, $datas, $concours_id = NULL, $ipguest = null) |
---|
1453 | { |
---|
1454 | global $user; |
---|
1455 | if ($this->get_img_note_user( $img_id, $concours_id, $user['id'], $ipguest) == array() ) |
---|
1456 | { |
---|
1457 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id FROM ' . CONCOURS_DATA_TABLE . ' ;'; |
---|
1458 | list($next_element_id) = pwg_db_fetch_array(pwg_query($query)); |
---|
1459 | |
---|
1460 | $query = ' |
---|
1461 | INSERT INTO ' . CONCOURS_DATA_TABLE .' |
---|
1462 | (`id`, |
---|
1463 | `id_concours` , |
---|
1464 | `user_id` , |
---|
1465 | `create_date` , |
---|
1466 | `img_id` , |
---|
1467 | `datas`'. |
---|
1468 | ($ipguest ? ',`ipguest` ' : '') |
---|
1469 | |
---|
1470 | .') |
---|
1471 | VALUES ( '.$next_element_id.',' |
---|
1472 | .($concours_id !== null ? $concours_id : $this->concours_id ) .' |
---|
1473 | , '. $user['id'] .', now(), '.$img_id.', "'.$datas.'"' |
---|
1474 | .($ipguest ? ',"'.$user['ipguest'].'"' : '') |
---|
1475 | .');'; |
---|
1476 | } |
---|
1477 | else |
---|
1478 | { |
---|
1479 | $query = ' |
---|
1480 | UPDATE ' . CONCOURS_DATA_TABLE .' |
---|
1481 | SET datas = "'.$datas.'", create_date = now() |
---|
1482 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).' |
---|
1483 | AND user_id = '.$user['id'].' |
---|
1484 | AND img_id = '.$img_id. |
---|
1485 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1486 | .';'; |
---|
1487 | } |
---|
1488 | pwg_query($query); |
---|
1489 | |
---|
1490 | } |
---|
1491 | |
---|
1492 | // get notation's user from db |
---|
1493 | // Return an array with : |
---|
1494 | // Criteria_id ==> value |
---|
1495 | // Note : criterias with subcriterias are not saved and not restored. They are calculated for global result |
---|
1496 | function get_img_note_user($img_id, $concours_id = NULL, $user_id = null, $ipguest = null) |
---|
1497 | { |
---|
1498 | global $user, $conf; |
---|
1499 | $img_note = array(); |
---|
1500 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1501 | { |
---|
1502 | $query = ' |
---|
1503 | SELECT datas |
---|
1504 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1505 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) |
---|
1506 | . ($ipguest == null ? ' AND user_id = '. ($user_id !== null ? $user_id : $user['id']) |
---|
1507 | : ' AND user_id = '.$conf['guest_id'] .' AND ipguest = "'.$ipguest.'"') |
---|
1508 | .' AND img_id = '. $img_id |
---|
1509 | .';'; |
---|
1510 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1511 | .';'; |
---|
1512 | $result = pwg_query($query); |
---|
1513 | |
---|
1514 | while ($row = pwg_db_fetch_array($result)) |
---|
1515 | |
---|
1516 | { |
---|
1517 | $datas = explode(";",$row['datas']); |
---|
1518 | foreach ($datas as $val) |
---|
1519 | { |
---|
1520 | if (strpos($val, '=') !== FALSE) |
---|
1521 | $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1); |
---|
1522 | } |
---|
1523 | } |
---|
1524 | |
---|
1525 | return $img_note; |
---|
1526 | } |
---|
1527 | else |
---|
1528 | return array(); |
---|
1529 | } |
---|
1530 | |
---|
1531 | |
---|
1532 | |
---|
1533 | // Return the max note for a concours |
---|
1534 | function get_concours_maxnote($concours_id = NULL) |
---|
1535 | { |
---|
1536 | |
---|
1537 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1538 | { |
---|
1539 | $query = ' |
---|
1540 | SELECT SUM(max_value) AS TOTAL |
---|
1541 | FROM ' . CONCOURS_DETAIL_TABLE .' |
---|
1542 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) |
---|
1543 | .' AND uppercriteria IS NULL' |
---|
1544 | .';'; |
---|
1545 | |
---|
1546 | $result = pwg_query($query); |
---|
1547 | if ($this->debug) echo "query=".$query."<br>\n"; |
---|
1548 | |
---|
1549 | if ($row = pwg_db_fetch_array($result)) |
---|
1550 | { |
---|
1551 | return $row['TOTAL']; |
---|
1552 | } |
---|
1553 | else |
---|
1554 | return -1; |
---|
1555 | |
---|
1556 | } |
---|
1557 | |
---|
1558 | |
---|
1559 | } |
---|
1560 | |
---|
1561 | |
---|
1562 | |
---|
1563 | // get notation's user from db |
---|
1564 | // Return the global note for a picture id from a user |
---|
1565 | // if moderationMode = 1, the 'moderation' contains the GlobalAverage for all the concours |
---|
1566 | // if moderationMode = 2, the 'moderation' contains the user moderation for all image that are voted by |
---|
1567 | // return -1 if there is no vote for that user |
---|
1568 | function get_img_globalnote_user($img_id, $concours_id = NULL, $user_id = null, $ipguest = null, $moderationMode = 0, $moderation = 0) |
---|
1569 | { |
---|
1570 | global $user, $conf; |
---|
1571 | $img_note = array(); |
---|
1572 | $global_user_note = -1; |
---|
1573 | $note_found = false; |
---|
1574 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1575 | { |
---|
1576 | $query = ' |
---|
1577 | SELECT datas |
---|
1578 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1579 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) |
---|
1580 | . ($ipguest == null ? ' AND user_id = '. ($user_id !== null ? $user_id : $user['id']) |
---|
1581 | : ' AND user_id = '.$conf['guest_id'] .' AND ipguest = "'.$ipguest.'"') |
---|
1582 | .' AND img_id = '. $img_id |
---|
1583 | .';'; |
---|
1584 | $result = pwg_query($query); |
---|
1585 | if ($this->debug) echo "query=".$query."<br>\n"; |
---|
1586 | |
---|
1587 | while ($row = pwg_db_fetch_array($result)) |
---|
1588 | { |
---|
1589 | $global_user_note = 0; // init note if one vote is present |
---|
1590 | $note_found = true; |
---|
1591 | $datas = explode(";",$row['datas']); |
---|
1592 | foreach ($datas as $val) |
---|
1593 | { |
---|
1594 | if (strpos($val, '=') !== FALSE) |
---|
1595 | $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1); |
---|
1596 | } |
---|
1597 | } |
---|
1598 | |
---|
1599 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1600 | foreach ($firstcriterias as $criteria) |
---|
1601 | { |
---|
1602 | // First without sub criterias |
---|
1603 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1604 | { |
---|
1605 | if (isset($img_note[$criteria['criteria_id']])) |
---|
1606 | $global_user_note += (int)$criteria['ponderation'] * (float)$img_note[$criteria['criteria_id']]; |
---|
1607 | } |
---|
1608 | else |
---|
1609 | { |
---|
1610 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1611 | foreach ($secondcriterias as $subcriteria) |
---|
1612 | { |
---|
1613 | if (isset($img_note[$subcriteria['criteria_id']])) |
---|
1614 | $global_user_note += (int)$subcriteria['ponderation'] * (float)$img_note[$subcriteria['criteria_id']]; |
---|
1615 | } |
---|
1616 | } |
---|
1617 | } |
---|
1618 | |
---|
1619 | // MODERATION |
---|
1620 | // moderation 1 => For each photo without note (user = author or user != author but no vote) add each user avaerage to the total of note and calcul the average |
---|
1621 | // moderation 2 => For each photo with note, adapt/change all the value with the moderation value : user note + moderation. |
---|
1622 | |
---|
1623 | if ($user_id !== null) // only for user |
---|
1624 | { |
---|
1625 | if ($moderationMode == 1 && !$note_found) |
---|
1626 | { // Moderation1 |
---|
1627 | $global_user_note = $moderation; |
---|
1628 | } |
---|
1629 | elseif ($moderationMode == 2 && $note_found) |
---|
1630 | { // Moderation2 |
---|
1631 | $global_user_note += $moderation; |
---|
1632 | } |
---|
1633 | |
---|
1634 | |
---|
1635 | } |
---|
1636 | return $global_user_note; |
---|
1637 | } |
---|
1638 | else |
---|
1639 | return array(); |
---|
1640 | |
---|
1641 | |
---|
1642 | } |
---|
1643 | |
---|
1644 | // Store comment from user to db |
---|
1645 | function store_img_comment_user($img_id, $datas, $concours_id = NULL, $ipguest = null) |
---|
1646 | { |
---|
1647 | global $user; |
---|
1648 | if (($comment = $this->get_img_note_user( $img_id, $concours_id, null, $ipguest)) == false) |
---|
1649 | { |
---|
1650 | $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id FROM ' . CONCOURS_DATA_TABLE . ' ;'; |
---|
1651 | list($next_element_id) = pwg_db_fetch_array(pwg_query($query)); |
---|
1652 | |
---|
1653 | $query = ' |
---|
1654 | INSERT INTO ' . CONCOURS_DATA_TABLE .' |
---|
1655 | (`id`, |
---|
1656 | `id_concours` , |
---|
1657 | `user_id` , |
---|
1658 | `create_date` , |
---|
1659 | `img_id` , |
---|
1660 | `comment`' |
---|
1661 | .($ipguest ? ' `ipguest`' : ''). |
---|
1662 | ') |
---|
1663 | VALUES ( '.$next_element_id.',' |
---|
1664 | .($concours_id !== null ? $concours_id : $this->concours_id ) .' |
---|
1665 | , '. $user['id'] .', now(), '.$img_id.', "'.$datas.'");'; |
---|
1666 | } |
---|
1667 | else |
---|
1668 | { |
---|
1669 | $query = ' |
---|
1670 | UPDATE ' . CONCOURS_DATA_TABLE .' |
---|
1671 | SET comment = "'.$datas.'", create_date = now() |
---|
1672 | WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).' |
---|
1673 | AND user_id = '.$user['id'].' |
---|
1674 | AND img_id = '.$img_id. |
---|
1675 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1676 | .';'; |
---|
1677 | } |
---|
1678 | pwg_query($query); |
---|
1679 | } |
---|
1680 | |
---|
1681 | |
---|
1682 | // Get comment on an image for a user |
---|
1683 | function get_img_comment_user($img_id, $concours_id = NULL, $user_id = null, $ipguest = null) |
---|
1684 | { |
---|
1685 | global $user; |
---|
1686 | $datas = false; |
---|
1687 | if ($concours_id!== null or $this->concours_id !== null) |
---|
1688 | { |
---|
1689 | $query = ' |
---|
1690 | SELECT comment |
---|
1691 | FROM ' . CONCOURS_DATA_TABLE .' |
---|
1692 | WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . ' |
---|
1693 | AND user_id = '. ($user_id !== null ? $user_id : $user['id']).' |
---|
1694 | AND img_id = '. $img_id . |
---|
1695 | ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '') |
---|
1696 | .';'; |
---|
1697 | |
---|
1698 | $result = pwg_query($query); |
---|
1699 | |
---|
1700 | while ($row = pwg_db_fetch_array($result)) |
---|
1701 | $datas = $row['comment']; |
---|
1702 | } |
---|
1703 | return $datas; |
---|
1704 | } |
---|
1705 | |
---|
1706 | |
---|
1707 | // Generate csv file for a closed concours with result generated |
---|
1708 | function generate_csv($concours_id = NULL) |
---|
1709 | { |
---|
1710 | global $conf; |
---|
1711 | $file = "rang, id_concours,method, img_id, img_name, img_file, date, note, moyenne, nbvotant, datas\r\n"; |
---|
1712 | |
---|
1713 | if (!(isset($this->concours_infos['method']))) |
---|
1714 | $this->concours_infos['method'] = 1; |
---|
1715 | |
---|
1716 | // recover all img_id from the category |
---|
1717 | $query = 'SELECT id_concours,method, img_id, IMG.name, IMG.file, date, note, moyenne, nbvotant, datas' |
---|
1718 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1719 | .' INNER JOIN ' . CONCOURS_TABLE.' AS CONC on CONC.id = id_concours' |
---|
1720 | .' INNER JOIN ' . IMAGES_TABLE.' AS IMG on IMG.id = img_id' |
---|
1721 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ); |
---|
1722 | |
---|
1723 | if ($this->concours_infos['method'] == 1) // total |
---|
1724 | $query .= ' ORDER BY note DESC'; |
---|
1725 | elseif ($this->concours_infos['method'] == 2) // moyenne |
---|
1726 | $query .= ' ORDER BY moyenne DESC'; |
---|
1727 | |
---|
1728 | $query .=';'; |
---|
1729 | |
---|
1730 | $result = pwg_query($query); |
---|
1731 | // For each images |
---|
1732 | $rang = 1; |
---|
1733 | while ($row = pwg_db_fetch_array($result)) |
---|
1734 | { |
---|
1735 | $file .= ($row['nbvotant'] != 0 ? $rang : 'N/A').', ' |
---|
1736 | .$row['id_concours'].', ' |
---|
1737 | .$row['method'].', ' |
---|
1738 | .$row['img_id'].', ' |
---|
1739 | .$row['name'].', ' |
---|
1740 | .$row['file'].', ' |
---|
1741 | .$row['date'].', ' |
---|
1742 | .$row['note'].', ' |
---|
1743 | .$row['moyenne'].', ' |
---|
1744 | .$row['nbvotant'].', ' |
---|
1745 | .($row['datas'] != null ? $row['datas'] : '') |
---|
1746 | ."\r\n"; |
---|
1747 | $rang ++; |
---|
1748 | } |
---|
1749 | |
---|
1750 | return $file; |
---|
1751 | |
---|
1752 | |
---|
1753 | } |
---|
1754 | |
---|
1755 | // Generate csv file for a closed concours with result generated and all users informations |
---|
1756 | function generate_detail_csv($concours_id = NULL) |
---|
1757 | { |
---|
1758 | global $conf; |
---|
1759 | $MethodType = "0"; |
---|
1760 | if ($concours_id === null) |
---|
1761 | $concours_id = $this->concours_id; |
---|
1762 | // Prepare the list of criteria |
---|
1763 | $criteria_list = ""; |
---|
1764 | $firstcriterias = $this->get_firstlevel_criterias($concours_id); |
---|
1765 | $ident1 = 1; |
---|
1766 | foreach ($firstcriterias as $criteria) |
---|
1767 | { |
---|
1768 | // format (id:name) |
---|
1769 | $criteria_list .= (strlen($criteria_list) ? "," : "") |
---|
1770 | .$ident1.":".$criteria['name'] |
---|
1771 | ."(id=".$criteria['criteria_id'].")"; |
---|
1772 | // First wit sub criterias |
---|
1773 | if ($this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1774 | { |
---|
1775 | $ident2 = 1; |
---|
1776 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1777 | foreach ($secondcriterias as $subcriteria) |
---|
1778 | { |
---|
1779 | // format (id:name) |
---|
1780 | $criteria_list .= (strlen($criteria_list) ? "," : "") |
---|
1781 | .$ident1.".".$ident2 |
---|
1782 | .":".$subcriteria['name']."(id=".$subcriteria['criteria_id'].")"; |
---|
1783 | $ident2 ++; |
---|
1784 | } |
---|
1785 | } |
---|
1786 | $ident1++; |
---|
1787 | } |
---|
1788 | $user_list = array(); |
---|
1789 | $users_criteria = ""; |
---|
1790 | |
---|
1791 | |
---|
1792 | // Get all the users who have notes for the concours |
---|
1793 | $query = 'SELECT distinct(user_id), USER.username' |
---|
1794 | .' FROM ' .CONCOURS_DATA_TABLE |
---|
1795 | .' INNER JOIN ' . USERS_TABLE.' AS USER on USER.id = user_id' |
---|
1796 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
1797 | // Dont take the guest informations because |
---|
1798 | .' AND user_id <> '.$conf['guest_id'] |
---|
1799 | .' ORDER BY username ASC' |
---|
1800 | .';'; |
---|
1801 | $result = pwg_query($query); |
---|
1802 | // For each user |
---|
1803 | while ($row = pwg_db_fetch_array($result)) |
---|
1804 | { |
---|
1805 | array_push($user_list, $row); |
---|
1806 | $users_criteria .= (strlen($users_criteria) ? "," : "")."user, user_global_note, comment, ".$criteria_list; |
---|
1807 | } |
---|
1808 | |
---|
1809 | $ipguest = array(); |
---|
1810 | // Get guest info (if available) |
---|
1811 | if ($this->concours_infos['guest']) |
---|
1812 | { |
---|
1813 | $ipguest = $this->get_guest_list(); |
---|
1814 | // For each guest |
---|
1815 | foreach ( $ipguest as $i => $userid ) //on parcours le tableau |
---|
1816 | { |
---|
1817 | $users_criteria .= (strlen($users_criteria) ? "," : "")."user, user_global_note, comment, ".$criteria_list; |
---|
1818 | } |
---|
1819 | } |
---|
1820 | |
---|
1821 | |
---|
1822 | // All informations in csv format |
---|
1823 | $file = "rang, id_concours, method, nom_concours, date_debut, date_fin, date_export, img_id, img_name, img_file, img_author, note, moyenne, moderation1, moderation2, nbvotant, datas," |
---|
1824 | .$users_criteria |
---|
1825 | ."\r\n"; |
---|
1826 | |
---|
1827 | // nb of users who vote for each image |
---|
1828 | |
---|
1829 | // NOT NEEDED, INFO STORE IN DATABASE 'RESULT' |
---|
1830 | $nbvotes = $this->nb_votes_by_img($concours_id); |
---|
1831 | |
---|
1832 | // recover all img_id from the category |
---|
1833 | if (!(isset($this->concours_infos['method']))) |
---|
1834 | $this->concours_infos['method'] = 1; |
---|
1835 | $query = 'SELECT id_concours, method, img_id, IMG.name, IMG.file, IMG.author, date, note, moyenne, moderation1, moderation2, nbvotant, datas, comment' |
---|
1836 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
1837 | .' INNER JOIN ' . CONCOURS_TABLE.' AS CONC on CONC.id = id_concours' |
---|
1838 | .' INNER JOIN ' . IMAGES_TABLE.' AS IMG on IMG.id = img_id' |
---|
1839 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ); |
---|
1840 | |
---|
1841 | switch ($this->concours_infos['method']) |
---|
1842 | { |
---|
1843 | case 1 : // total |
---|
1844 | $query .= ' ORDER BY note DESC'; |
---|
1845 | $MethodType = "1-Sum"; |
---|
1846 | break; |
---|
1847 | case 2 : // moyenne |
---|
1848 | $query .= ' ORDER BY moyenne DESC'; |
---|
1849 | $MethodType = "2-Average"; |
---|
1850 | break; |
---|
1851 | case 3 : // moderation1 |
---|
1852 | $query .= ' ORDER BY moderation1 DESC'; |
---|
1853 | $MethodType = "3-Average(Mod1)"; |
---|
1854 | break; |
---|
1855 | case 4 : // moderation2 |
---|
1856 | $query .= ' ORDER BY moderation2 DESC'; |
---|
1857 | $MethodType = "4-Average(Mod2)"; |
---|
1858 | break; |
---|
1859 | } |
---|
1860 | |
---|
1861 | $query .=';'; |
---|
1862 | |
---|
1863 | $result = pwg_query($query); |
---|
1864 | // For each images |
---|
1865 | $rang = 1; |
---|
1866 | $previousNote = $previousMoy = 0; |
---|
1867 | |
---|
1868 | while ($row = pwg_db_fetch_array($result)) |
---|
1869 | { |
---|
1870 | // Check and verify for exaequo |
---|
1871 | if ($this->my_config['check_exaequo']) |
---|
1872 | { |
---|
1873 | if ( ($this->concours_infos['method'] == 1 // total |
---|
1874 | AND ($row['note'] == $previousNote)) |
---|
1875 | OR ($this->concours_infos['method'] == 2 // moyenne |
---|
1876 | AND ($row['moyenne'] == $previousMoy))) |
---|
1877 | { |
---|
1878 | $rang --; |
---|
1879 | } |
---|
1880 | } |
---|
1881 | |
---|
1882 | $file .= ($row['nbvotant'] != 0 ? $rang : 'N/A').', ' |
---|
1883 | .$row['id_concours'].', ' |
---|
1884 | .$MethodType.', ' |
---|
1885 | .str_replace(",", "",$this->concours_infos['name']).', ' |
---|
1886 | .$this->concours_infos['begin_date'].', ' |
---|
1887 | .$this->concours_infos['end_date'].', ' |
---|
1888 | .$row['date'].', ' |
---|
1889 | |
---|
1890 | .$row['img_id'].', ' |
---|
1891 | .str_replace(",", "",$row['name']).', ' |
---|
1892 | .$row['file'].', ' |
---|
1893 | .$row['author'].', ' |
---|
1894 | .$row['note'].', ' |
---|
1895 | .$row['moyenne'].', ' |
---|
1896 | .$row['moderation1'].', ' |
---|
1897 | .$row['moderation2'].', ' |
---|
1898 | .$row['nbvotant'].', ' |
---|
1899 | .($row['datas'] != null ? $row['datas'] : '') |
---|
1900 | ; |
---|
1901 | foreach ($user_list as $uuser) |
---|
1902 | { |
---|
1903 | |
---|
1904 | $comment = $this->get_img_comment_user($row['img_id'], $concours_id, $uuser['user_id']); |
---|
1905 | |
---|
1906 | $user_note = $this->get_img_note_user($row['img_id'], $concours_id, $uuser['user_id']); |
---|
1907 | |
---|
1908 | $user_global_note = $this->get_img_globalnote_user($row['img_id'], $concours_id, $uuser['user_id']); |
---|
1909 | $file .= ', '.$uuser['username'].', '.($user_global_note >= 0 ? $user_global_note : 0) |
---|
1910 | .', '.($comment != false ? str_replace(array(",","\r\n", "\n", "\r"), " ",$comment) : '') |
---|
1911 | .', ' |
---|
1912 | ; |
---|
1913 | |
---|
1914 | $user_note_by_crit = ""; |
---|
1915 | foreach ($firstcriterias as $criteria) |
---|
1916 | { |
---|
1917 | // First without sub criterias |
---|
1918 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1919 | { |
---|
1920 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").(isset($user_note[$criteria['criteria_id']]) ? $user_note[$criteria['criteria_id']] : ' '); |
---|
1921 | } |
---|
1922 | else |
---|
1923 | { |
---|
1924 | $user_note_by_subcrit = ""; |
---|
1925 | $subcrit_note = 0; |
---|
1926 | $user_has_vote = false; |
---|
1927 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1928 | foreach ($secondcriterias as $subcriteria) |
---|
1929 | { |
---|
1930 | if (isset($user_note[$subcriteria['criteria_id']])) $user_has_vote = true; |
---|
1931 | $user_note_by_subcrit .= (strlen($user_note_by_subcrit) ? "," : "").(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : ' '); |
---|
1932 | $subcrit_note += (int)$subcriteria['ponderation'] * (float)(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : 0); |
---|
1933 | |
---|
1934 | } |
---|
1935 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").($user_has_vote ? $subcrit_note : ' ').",".$user_note_by_subcrit; |
---|
1936 | } |
---|
1937 | } |
---|
1938 | $file .= $user_note_by_crit; |
---|
1939 | |
---|
1940 | } |
---|
1941 | |
---|
1942 | // Add guest infos (if present) |
---|
1943 | foreach ($ipguest as $ipguestt) |
---|
1944 | { |
---|
1945 | $comment = $this->get_img_comment_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt); |
---|
1946 | |
---|
1947 | $user_note = $this->get_img_note_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt); |
---|
1948 | $user_global_note = $this->get_img_globalnote_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt); |
---|
1949 | |
---|
1950 | $file .= ', Guest('.$ipguestt.'), '.($user_global_note >= 0 ? $user_global_note : 0) |
---|
1951 | .', '.($comment != false ? str_replace(array(",","\r\n", "\n", "\r"), " ",$comment) : '') |
---|
1952 | .', ' |
---|
1953 | ; |
---|
1954 | |
---|
1955 | $user_note_by_crit = ""; |
---|
1956 | foreach ($firstcriterias as $criteria) |
---|
1957 | { |
---|
1958 | // First without sub criterias |
---|
1959 | if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id )) |
---|
1960 | { |
---|
1961 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").(isset($user_note[$criteria['criteria_id']]) ? $user_note[$criteria['criteria_id']] : ' '); |
---|
1962 | } |
---|
1963 | else |
---|
1964 | { |
---|
1965 | $user_note_by_subcrit = ""; |
---|
1966 | $subcrit_note = 0; |
---|
1967 | $user_has_vote = false; |
---|
1968 | $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id ); |
---|
1969 | foreach ($secondcriterias as $subcriteria) |
---|
1970 | { |
---|
1971 | if (isset($user_note[$subcriteria['criteria_id']])) $user_has_vote = true; |
---|
1972 | $user_note_by_subcrit .= (strlen($user_note_by_subcrit) ? "," : "").(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : ' '); |
---|
1973 | $subcrit_note += (int)$subcriteria['ponderation'] * (float)(isset($user_note[$subcriteria['criteria_id']]) ? $user_note[$subcriteria['criteria_id']] : 0); |
---|
1974 | |
---|
1975 | } |
---|
1976 | $user_note_by_crit .= (strlen($user_note_by_crit) ? "," : "").($user_has_vote ? $subcrit_note : ' ').",".$user_note_by_subcrit; |
---|
1977 | } |
---|
1978 | } |
---|
1979 | $file .= $user_note_by_crit; |
---|
1980 | |
---|
1981 | } |
---|
1982 | |
---|
1983 | $file .="\r\n"; |
---|
1984 | $rang ++; |
---|
1985 | $previousNote = $row['note']; |
---|
1986 | $previousMoy = $row['moyenne']; |
---|
1987 | |
---|
1988 | } |
---|
1989 | return utf8_decode($file); |
---|
1990 | |
---|
1991 | |
---|
1992 | } |
---|
1993 | |
---|
1994 | |
---|
1995 | // Save csv datas to physical file |
---|
1996 | function save_file($file,$concours_id = NULL) |
---|
1997 | { |
---|
1998 | // check if already saved file |
---|
1999 | if ($filename = $this->get_file_name($concours_id)) |
---|
2000 | return $filename; |
---|
2001 | else |
---|
2002 | { |
---|
2003 | $dirpath = CONCOURS_RESULT_FOLDER; |
---|
2004 | |
---|
2005 | $filename = date('Ymd')."_concours".($concours_id !== null ? $concours_id : $this->concours_id ).".csv"; |
---|
2006 | if (!is_dir($dirpath)) |
---|
2007 | @mkdir($dirpath); |
---|
2008 | if (file_exists($dirpath. $filename)) |
---|
2009 | unlink($dirpath. $filename); |
---|
2010 | |
---|
2011 | $fh = fopen($dirpath. $filename, 'w') or die("can't open file"); |
---|
2012 | fwrite($fh, $file); |
---|
2013 | fclose($fh); |
---|
2014 | |
---|
2015 | //update result database with filename |
---|
2016 | $query = "UPDATE " . CONCOURS_RESULT_TABLE . " |
---|
2017 | SET |
---|
2018 | file_name = \"".$filename."\" |
---|
2019 | WHERE id_concours= ".($concours_id !== NULL ? $concours_id : $this->concours_id)." |
---|
2020 | ;"; |
---|
2021 | pwg_query($query); |
---|
2022 | |
---|
2023 | return $filename; |
---|
2024 | } |
---|
2025 | } |
---|
2026 | |
---|
2027 | // Get result file name from db |
---|
2028 | function get_file_name($concours_id = NULL) |
---|
2029 | { |
---|
2030 | $query = 'SELECT distinct(file_name)' |
---|
2031 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
2032 | .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) |
---|
2033 | .';'; |
---|
2034 | $result = pwg_query($query); |
---|
2035 | if ($result) |
---|
2036 | { |
---|
2037 | $row = pwg_db_fetch_array($result); |
---|
2038 | if ($row) |
---|
2039 | return $row['file_name']; |
---|
2040 | else |
---|
2041 | return false; |
---|
2042 | } |
---|
2043 | } |
---|
2044 | |
---|
2045 | // Get csv datas from a saved file |
---|
2046 | function get_file($concours_id = NULL) |
---|
2047 | { |
---|
2048 | $filename = CONCOURS_RESULT_FOLDER. |
---|
2049 | $size = filesize($filename); |
---|
2050 | header('Content-Type: application/octet-stream'); |
---|
2051 | header('Content-Length: '.$size); |
---|
2052 | header('Content-Disposition: attachment; filename='.$filename); |
---|
2053 | header('Content-Transfer-Encoding: binary'); |
---|
2054 | |
---|
2055 | $file = @fopen($filename, ‘rb’); |
---|
2056 | if ($file) { |
---|
2057 | // stream the file and exit the script when complete |
---|
2058 | fpassthru($file); |
---|
2059 | } |
---|
2060 | } |
---|
2061 | |
---|
2062 | // DEPRECATED Piwigo 11 Activation page administration |
---|
2063 | /* |
---|
2064 | function concours_admin_menu($menu) |
---|
2065 | { |
---|
2066 | array_push($menu, |
---|
2067 | array( |
---|
2068 | 'NAME' => CONCOURS_NAME, |
---|
2069 | 'URL' => get_root_url().'admin.php?page=plugin-'.CONCOURS_DIR.'-manage' |
---|
2070 | ) |
---|
2071 | ); |
---|
2072 | return $menu; |
---|
2073 | } |
---|
2074 | */ |
---|
2075 | |
---|
2076 | // register Concours Menubar |
---|
2077 | public function register_blocks( $menu_ref_arr ) |
---|
2078 | { |
---|
2079 | $menu = & $menu_ref_arr[0]; |
---|
2080 | if ($menu->get_id() != 'menubar' OR $this->my_config['active_menubar'] == false) |
---|
2081 | return; |
---|
2082 | $menu->register_block( new RegisteredBlock( 'CONCOURS_menu', 'concours', 'ConcoursPhoto')); |
---|
2083 | } |
---|
2084 | |
---|
2085 | // initialise menubar's menu. called by menubar object when making menu |
---|
2086 | public function blockmanager_apply( $menu_ref_arr ) |
---|
2087 | { |
---|
2088 | |
---|
2089 | global $user, $template; |
---|
2090 | $menu = & $menu_ref_arr[0]; |
---|
2091 | |
---|
2092 | if ($this->user_groups == array()) |
---|
2093 | $this->get_user_groups(); |
---|
2094 | |
---|
2095 | if ( ($block = $menu->get_block( 'CONCOURS_menu' ) ) != null ) |
---|
2096 | { |
---|
2097 | $menu_list=array(); |
---|
2098 | |
---|
2099 | if(is_admin()) |
---|
2100 | { |
---|
2101 | |
---|
2102 | array_push($menu_list, |
---|
2103 | array( |
---|
2104 | 'NAME' => l10n('concours_admin_add'), |
---|
2105 | 'TITLE' => l10n('concours_admin_add'), |
---|
2106 | 'URL' => PHPWG_ROOT_PATH . 'admin.php?page=plugin&section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&action=new', |
---|
2107 | 'REL' => "" |
---|
2108 | ) |
---|
2109 | ); |
---|
2110 | |
---|
2111 | } |
---|
2112 | |
---|
2113 | // recover all img_id from the category |
---|
2114 | $query = 'SELECT distinct(id_concours), groups, guest, admin, CONC.name' |
---|
2115 | .' FROM ' .CONCOURS_RESULT_TABLE |
---|
2116 | .' INNER JOIN '.CONCOURS_TABLE. ' AS CONC ON CONC.id = id_concours' |
---|
2117 | .' ORDER BY id DESC' |
---|
2118 | .';'; |
---|
2119 | |
---|
2120 | $result = pwg_query($query); |
---|
2121 | $nb_concours = 1; |
---|
2122 | |
---|
2123 | // For each concours |
---|
2124 | while ($row = pwg_db_fetch_assoc($result)) |
---|
2125 | |
---|
2126 | { |
---|
2127 | |
---|
2128 | if ($nb_concours > $this->my_config['nbconcours_menubar']) |
---|
2129 | break; |
---|
2130 | |
---|
2131 | $conc_to_show = false; // Default ==> dont show |
---|
2132 | |
---|
2133 | // Guest OR admin |
---|
2134 | if ((is_a_guest() AND $row['guest']) |
---|
2135 | OR (is_admin() AND $row['admin'])) |
---|
2136 | $conc_to_show = true; |
---|
2137 | // Group present |
---|
2138 | |
---|
2139 | |
---|
2140 | elseif (!empty($row['groups'])) |
---|
2141 | { |
---|
2142 | $authorized_groups = explode(',', $row['groups']); |
---|
2143 | if (array_intersect($this->user_groups, $authorized_groups) != array()) |
---|
2144 | $conc_to_show = true; |
---|
2145 | } |
---|
2146 | // Allowed for any user (logged) |
---|
2147 | elseif (empty($row['groups'])) |
---|
2148 | $conc_to_show = true; |
---|
2149 | |
---|
2150 | if ($conc_to_show) |
---|
2151 | { |
---|
2152 | $nb_concours ++; |
---|
2153 | |
---|
2154 | array_push($menu_list, |
---|
2155 | array( |
---|
2156 | 'NAME' => $row['name'], |
---|
2157 | 'TITLE' => $row['name'], |
---|
2158 | 'URL' => './index.php?/concours/'.$row['id_concours'], |
---|
2159 | 'EDIT' => (is_admin()? PHPWG_ROOT_PATH . 'admin.php?page=plugin&section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&concours=' . $row['id_concours'].'&action=modify':"") |
---|
2160 | ) |
---|
2161 | ); |
---|
2162 | } |
---|
2163 | } |
---|
2164 | |
---|
2165 | if (count($menu_list)) |
---|
2166 | { |
---|
2167 | $block->template = CONCOURS_ROOT.'/template/concours_menu.tpl'; |
---|
2168 | $block->data = $menu_list; |
---|
2169 | } |
---|
2170 | } |
---|
2171 | } |
---|
2172 | |
---|
2173 | function section_init_concours() |
---|
2174 | { |
---|
2175 | global $tokens, $page; |
---|
2176 | if ($tokens[0] == 'concours') |
---|
2177 | $page['section'] = 'concours'; |
---|
2178 | elseif ($tokens[0] == 'concours_vote') |
---|
2179 | $page['section'] = 'concours_vote'; |
---|
2180 | } |
---|
2181 | |
---|
2182 | function index_concours() |
---|
2183 | { |
---|
2184 | global $page; |
---|
2185 | if (isset($page['section']) and $page['section'] == 'concours') |
---|
2186 | { |
---|
2187 | include(CONCOURS_PATH . 'publish.php'); |
---|
2188 | } |
---|
2189 | } |
---|
2190 | |
---|
2191 | function index_vote_concours() |
---|
2192 | { |
---|
2193 | global $page; |
---|
2194 | if (isset($page['section']) and $page['section'] == 'concours_vote') |
---|
2195 | { |
---|
2196 | include(CONCOURS_PATH . 'concours_vote.php'); |
---|
2197 | } |
---|
2198 | } |
---|
2199 | |
---|
2200 | |
---|
2201 | // Show the global note under the thumbnail |
---|
2202 | function thumbnail_note($tpl_var, $pictures) |
---|
2203 | { |
---|
2204 | global $page, $user, $template; |
---|
2205 | $this->get_user_groups(); |
---|
2206 | |
---|
2207 | $user['ipguest'] = null; |
---|
2208 | if (is_a_guest()) |
---|
2209 | { |
---|
2210 | if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) |
---|
2211 | $IP = $_SERVER['HTTP_X_FORWARDED_FOR']; |
---|
2212 | elseif(isset($_SERVER['HTTP_CLIENT_IP'])) |
---|
2213 | $IP = $_SERVER['HTTP_CLIENT_IP']; |
---|
2214 | else |
---|
2215 | $IP = $_SERVER['REMOTE_ADDR']; |
---|
2216 | |
---|
2217 | // store ip |
---|
2218 | $user['ipguest'] = $IP; |
---|
2219 | } |
---|
2220 | |
---|
2221 | if (($page['section']) == 'categories' AND !empty($page['category'])) |
---|
2222 | { |
---|
2223 | |
---|
2224 | $concours = array(); |
---|
2225 | |
---|
2226 | $query = ' |
---|
2227 | SELECT * |
---|
2228 | FROM ' . CONCOURS_TABLE .' |
---|
2229 | WHERE category =' . $page['category']['id'] |
---|
2230 | // comment to load prepared contests and opened contests . ' AND time_to_sec(TIMEDIFF(begin_date,now())) < 0' |
---|
2231 | . ' AND time_to_sec(TIMEDIFF(now(), end_date)) < 0'; |
---|
2232 | |
---|
2233 | $result = pwg_query($query); |
---|
2234 | // DEBUG |
---|
2235 | if ($this->debug) echo $query."\n"; |
---|
2236 | // END DEBUG |
---|
2237 | while ($row = pwg_db_fetch_array($result)) |
---|
2238 | { |
---|
2239 | if ((strtotime($row['begin_date'])-time() > 0) AND $this->my_config['mask_thumbnail']) |
---|
2240 | { |
---|
2241 | // disable picture if contest is prepared for all users |
---|
2242 | $i=0; |
---|
2243 | while ($i<count($tpl_var)) |
---|
2244 | { |
---|
2245 | array_splice($tpl_var, $i, 1); |
---|
2246 | array_splice($pictures, $i, 1); |
---|
2247 | } |
---|
2248 | $concours = $row; |
---|
2249 | |
---|
2250 | } |
---|
2251 | elseif (is_admin() AND $row['admin']) |
---|
2252 | $concours = $row; |
---|
2253 | elseif (!is_a_guest() AND !empty($row['groups'])) |
---|
2254 | { |
---|
2255 | $authorized_groups = explode(',', $row['groups']); |
---|
2256 | if (array_intersect($this->user_groups, $authorized_groups) == array()) |
---|
2257 | continue; |
---|
2258 | $concours = $row; |
---|
2259 | } |
---|
2260 | |
---|
2261 | // If no group is parameter for that concours ==> available for all registered users |
---|
2262 | else |
---|
2263 | $concours = $row; |
---|
2264 | } |
---|
2265 | |
---|
2266 | |
---|
2267 | if (count($concours)) |
---|
2268 | { |
---|
2269 | // Add timeline for all users. |
---|
2270 | if (strtotime($concours['begin_date'])-time() > 0) |
---|
2271 | { |
---|
2272 | $template->assign('begin_concours_min', (strtotime($concours['begin_date'])-time())); |
---|
2273 | $template->assign('deadline_type',$this->my_config['concours_deadline']); |
---|
2274 | $template->assign('begin_concours', $concours['begin_date']); |
---|
2275 | } |
---|
2276 | else |
---|
2277 | { |
---|
2278 | // Check if user is guest and if guest is allowed |
---|
2279 | if ((is_a_guest() AND $concours['guest']) |
---|
2280 | OR (is_admin() AND $concours['admin']) |
---|
2281 | OR !is_a_guest()) |
---|
2282 | { |
---|
2283 | |
---|
2284 | // check if contest is concours not already open (date begin > now) |
---|
2285 | |
---|
2286 | |
---|
2287 | |
---|
2288 | foreach($tpl_var as $cle=>$valeur) |
---|
2289 | { |
---|
2290 | // show only if the author is not the active user |
---|
2291 | |
---|
2292 | $AuthorArray = strip_punctuation2($tpl_var[$cle]['author']); |
---|
2293 | $AddedByID = ($tpl_var[$cle]['added_by']); |
---|
2294 | $UserArray = strip_punctuation2($user['username']); |
---|
2295 | |
---|
2296 | if ($this->my_config['author_vote_available'] == 0 // Allow to vote for all photos (and also show his score) |
---|
2297 | OR ($this->my_config['author_vote_available'] == 1 // check between author and user |
---|
2298 | AND (empty($AuthorArray) OR count(array_intersect($AuthorArray, $UserArray)) != count ($AuthorArray)) // Author is the same as user name |
---|
2299 | ) |
---|
2300 | OR ($this->my_config['author_vote_available'] == 2 // check between author and user |
---|
2301 | AND (!isset($AddedByID) OR ($user['id'] != $AddedByID)) // Addedby is the same as user name |
---|
2302 | ) |
---|
2303 | OR ($this->my_config['author_vote_available'] == 3 // check between author OR addedby and user |
---|
2304 | AND (!isset($AddedByID) OR ($user['id'] != $AddedByID)) // Addedby is the same as user name |
---|
2305 | AND (empty($AuthorArray) OR count(array_intersect($AuthorArray, $UserArray)) != count ($AuthorArray)) // Author is the same as user name |
---|
2306 | ) |
---|
2307 | ) |
---|
2308 | |
---|
2309 | { |
---|
2310 | if ($this->my_config['thumb_note']) // display thumbnail note only if option is activated |
---|
2311 | { |
---|
2312 | $user_global_note = $this->get_img_globalnote_user($tpl_var[$cle]['id'], $concours['id'], $user['id'], $user['ipguest']); |
---|
2313 | $tpl_var[$cle]['NAME'] = $tpl_var[$cle]['NAME'].'('.l10n('thumbnail_global_note').': '.($user_global_note >= 0 ? $user_global_note : 0).')'; |
---|
2314 | } |
---|
2315 | } |
---|
2316 | } |
---|
2317 | |
---|
2318 | // Add deadline on description page |
---|
2319 | $template->assign('end_concours', $concours['end_date']); |
---|
2320 | $template->assign('end_concours_min', (strtotime($concours['end_date'])-time())); |
---|
2321 | $template->assign('deadline_type',$this->my_config['concours_deadline']); |
---|
2322 | if (isset($this->my_config['active_global_score_page']) AND $this->my_config['active_global_score_page']) // only if option is activated |
---|
2323 | $template->assign('global_vote_link', PHPWG_ROOT_PATH . 'index.php?/concours_vote/'.$concours['id']); |
---|
2324 | } |
---|
2325 | |
---|
2326 | } |
---|
2327 | $template->assign('IMG_URL', CONCOURS_IMG_PATH); |
---|
2328 | |
---|
2329 | $template->set_filenames(array('concours_description' => CONCOURS_ROOT.'/template/concours_description.tpl')); |
---|
2330 | $template->concat('CONTENT_DESCRIPTION', $template->parse('concours_description', true)); |
---|
2331 | |
---|
2332 | } |
---|
2333 | } |
---|
2334 | |
---|
2335 | return $tpl_var; |
---|
2336 | } |
---|
2337 | |
---|
2338 | function concours_stuffs_module($modules) |
---|
2339 | { |
---|
2340 | array_push($modules, array( |
---|
2341 | 'path' => CONCOURS_PATH.'stuffs_module/', |
---|
2342 | 'name' => l10n('concours_stuffs_name'), |
---|
2343 | 'description' => l10n('concours_stuffs_description'), |
---|
2344 | ) |
---|
2345 | ); |
---|
2346 | return $modules; |
---|
2347 | } |
---|
2348 | |
---|
2349 | |
---|
2350 | |
---|
2351 | function add_contest_desc($tpl_var) |
---|
2352 | { |
---|
2353 | global $page, $user, $template; |
---|
2354 | // TO DO : Add contest information on subcategory |
---|
2355 | /* |
---|
2356 | echo "ICI<br>"; |
---|
2357 | echo "tpl_VAR".(is_array($tpl_var) ? "OK" : $tpl_var)."<br>"; |
---|
2358 | foreach ($tpl_var as $id=>$val) |
---|
2359 | { |
---|
2360 | |
---|
2361 | if (is_array($val)) |
---|
2362 | foreach ($val as $id2=>$val2) |
---|
2363 | if (is_array($val2)) |
---|
2364 | foreach ($val2 as $id3=>$val3) |
---|
2365 | if (is_array($val3)) |
---|
2366 | foreach ($val3 as $id4=>$val4) |
---|
2367 | echo "tpl_var[".$id."][".$id2."][".$id3."][".$id4."]=".$val4."<br>\n"; |
---|
2368 | else |
---|
2369 | echo "tpl_var[".$id."][".$id2."][".$id3."]=".$val3."<br>\n"; |
---|
2370 | else |
---|
2371 | echo "tpl_var[".$id."][".$id2."]=".$val2."<br>\n"; |
---|
2372 | else |
---|
2373 | echo "tpl_var[".$id."]=".$val."<br>\n"; |
---|
2374 | } |
---|
2375 | $i=0; |
---|
2376 | while ($i<count($tpl_var)) |
---|
2377 | { |
---|
2378 | $tpl_var[$i]['NAME'] = "TEST TEST"; |
---|
2379 | $i++; |
---|
2380 | |
---|
2381 | } |
---|
2382 | |
---|
2383 | $template->set_filenames(array('concours_description' => CONCOURS_ROOT.'/template/concours_description-cat.tpl')); |
---|
2384 | $template->concat('DESCRIPTION', $template->parse('concours_description', true)); |
---|
2385 | |
---|
2386 | return $tpl_var; |
---|
2387 | */ |
---|
2388 | } |
---|
2389 | |
---|
2390 | |
---|
2391 | |
---|
2392 | |
---|
2393 | // Check if a concours is present (prepared or actived) for the category and user group |
---|
2394 | function concours_present_cat($category = NULL) |
---|
2395 | { |
---|
2396 | global $page; |
---|
2397 | $concours_present = false; |
---|
2398 | if (!isset($category)) // param not present |
---|
2399 | { |
---|
2400 | if (($page['section']) == 'categories' AND !empty($page['category'])) |
---|
2401 | $category = $page['category']['id'] ; |
---|
2402 | } |
---|
2403 | |
---|
2404 | if (isset($category)) |
---|
2405 | { |
---|
2406 | $query = ' |
---|
2407 | SELECT * |
---|
2408 | FROM ' . CONCOURS_TABLE .' |
---|
2409 | WHERE category =' . $category . ' |
---|
2410 | AND time_to_sec(TIMEDIFF(now(), end_date)) < 0 |
---|
2411 | '; |
---|
2412 | |
---|
2413 | $result = pwg_query($query); |
---|
2414 | while ($row = pwg_db_fetch_array($result)) |
---|
2415 | return true; |
---|
2416 | } |
---|
2417 | return false; |
---|
2418 | } |
---|
2419 | |
---|
2420 | |
---|
2421 | // Disable Metatdata to picture for a prepared or active concours |
---|
2422 | function disable_meta_to_picture() |
---|
2423 | { |
---|
2424 | if (isset($this->my_config['mask_meta']) && $this->my_config['mask_meta'] == true) |
---|
2425 | { |
---|
2426 | if ($this->concours_present_cat()) |
---|
2427 | pwg_set_session_var('show_metadata', 0 ); |
---|
2428 | } |
---|
2429 | } |
---|
2430 | |
---|
2431 | |
---|
2432 | } |
---|
2433 | |
---|
2434 | function strip_punctuation($string) { |
---|
2435 | $string = strtolower($string); // lower case |
---|
2436 | $string = preg_replace("/\p{P}+/", "", $string); // remove punctuation |
---|
2437 | $string = preg_replace("/\p{Nd}+/", "", $string); // remove numeric |
---|
2438 | $string = preg_replace("/[\p{Z}\t\r\n\v\f]+/", "", $string); // remove spaces |
---|
2439 | $string = str_replace(" +", "", $string); // remove space |
---|
2440 | //echo "stripPunct=".$string."!\n"; |
---|
2441 | return $string; |
---|
2442 | } |
---|
2443 | |
---|
2444 | // return array |
---|
2445 | function strip_punctuation2($string) { |
---|
2446 | $string = strtolower($string); // lower case |
---|
2447 | $string = preg_replace("/\p{P}+/", " ", $string); // remove punctuation by space |
---|
2448 | $string = preg_replace("/\p{Nd}+/", "", $string); // remove numeric |
---|
2449 | $string = preg_replace("/[\p{Z}\t\r\n\v\f]+/", " ", $string); // remove spaces by spaces (only 1) |
---|
2450 | // $string = str_replace(" +", "", $string); // remove space |
---|
2451 | $stringArray = $returnValue = preg_split('/\\W/', $string, -1, PREG_SPLIT_NO_EMPTY); |
---|
2452 | |
---|
2453 | return $stringArray; |
---|
2454 | } |
---|
2455 | |
---|
2456 | |
---|
2457 | |
---|
2458 | ?> |
---|