source: extensions/ConcoursPhoto/include/Concours.class.php @ 4428

Last change on this file since 4428 was 4428, checked in by tiico, 14 years ago

[ConcoursPhoto] Correct some bugs. Add audit and user audit page.

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