Ignore:
Timestamp:
Feb 12, 2021, 7:00:27 PM (4 years ago)
Author:
tiico
Message:

Version 11.0.1 ConcoursPhoto (piwigo 11 compatible + some corrections/news)

Location:
extensions/ConcoursPhoto/include
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • extensions/ConcoursPhoto/include/Concours.class.php

    r9370 r32457  
    1212        var $concours_infos = array();
    1313       
    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 
    2314        // General configuration parameters
    2415        var $my_config = array();
    25 /*
    26         active_menubar
    27         nbconcours_menubar
    28 */
    2916        var $user_groups = array();
    3017        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();
     18
     19
     20        /**
     21         * Class constructor with concours id.
     22         * Return NULL if not existing or create it
     23         */
     24        function __construct($concours_id = null, $force_creation = NULL)
     25        {
    3626                if ($concours_id !== null)
    3727                {               
     
    4434
    4535       
    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 
     36        /**
     37         * Load configuration from database
     38         * Assign value to the variable $config
     39         */
     40        function load_config() {
     41                $query = 'SELECT value FROM '.CONFIG_TABLE.' WHERE param="concoursphoto";';
    5642                $result = pwg_query($query);
    5743
    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                      }
     44            if(isset($result)) {
     45                        $row = pwg_db_fetch_row($result);
     46                        if(is_string($row[0])) {
     47                                $this->my_config = unserialize(($row[0]));
     48                        }
    6549            }
    66                 $this->load_default_config();
    67         }
    68        
    69         // Initialize default values of params
     50        if ($this->debug) 
     51            foreach ($this->my_config as $key => $value)
     52                {
     53                    echo "my_config[".$key."] = ".$value."\n";
     54                }   
     55            $this->load_default_config();
     56        }
     57       
     58       
     59        /**
     60         * Load default configuration, initialize default values of params
     61         */
    7062        private function load_default_config()
    7163        {
     
    9688        }
    9789       
     90        /**
     91         * Save the current configuration (ie the value of $config) to the database
     92         */
     93        function set_config() {
     94                conf_update_param('concoursphoto', pwg_db_real_escape_string(serialize($this->my_config)));
     95        }
     96       
    9897       
    9998        // Retrieve user groups
     
    101100        {
    102101                global $user;
    103 if ($this->debug)
    104 foreach ($user as $id=>$val)
    105 {
    106         if (is_array($val))
    107                 foreach ($val as $id2=>$val2)
    108                         echo "user[".$id."][".$id2."]=".$val2."\n";
    109         else
    110                 echo "user[".$id."]=".$val."\n";
    111 }
    112 
    113102                $query = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id = ' . $user['id'] . ';';
    114 if ($this->debug)               echo $query."\n";
     103
    115104                $result = pwg_query($query);
    116                 while ($row = mysql_fetch_assoc($result))
     105                while ($row = pwg_db_fetch_array($result))
    117106                {
    118107                  array_push($this->user_groups, $row['group_id']);
    119108                }
    120                
    121 if ($this->debug)
    122 foreach ($this->user_groups as $gr)
    123         echo "Group=".$gr."\n";
    124109
    125110        }
     
    144129                        $result = pwg_query($query);
    145130                        if ($result)
    146                                 $this->concours_infos = mysql_fetch_assoc($result);
    147                 }
    148 
    149         }
     131                                $this->concours_infos = pwg_db_fetch_array($result);
     132                }
     133
     134        }
     135
    150136
    151137        // save informations on database for a concours_id
     
    165151                                                `groups`,
    166152                                                `method`,
    167                                                 `guest`
     153                                                `guest`,
     154                                                `admin`
    168155                       
    169156                                                )
     
    172159                                                        \"".$this->concours_infos['begin_date']."\", \"".$this->concours_infos['end_date']."\",
    173160                                                        ".$this->concours_infos['category'].", ".$this->concours_infos['groups'].",
    174                                                         ".$this->concours_infos['method'].", ".($this->concours_infos['guest'] ? "TRUE" : "FALSE")."
     161                                                        ".$this->concours_infos['method'].", ".($this->concours_infos['guest'] ? "TRUE" : "FALSE").",
     162                                                        ".($this->concours_infos['admin'] ? "TRUE" : "FALSE")."
    175163                                                        );";
    176164                        if (pwg_query($query) != null)
     
    199187                                                groups = ".$this->concours_infos['groups'].",
    200188                                                method = ".$this->concours_infos['method'].",
    201                                                 guest = ".($this->concours_infos['guest'] ? "TRUE" : "FALSE")."
     189                                                guest = ".($this->concours_infos['guest'] ? "TRUE" : "FALSE").",
     190                                                admin = ".($this->concours_infos['admin'] ? "TRUE" : "FALSE")."
    202191                                               
    203192                                                WHERE id = ".($concours_id !== NULL ? $concours_id : $this->concours_id)."
     
    245234                        $query = '
    246235                                DELETE
    247                                 FROM ' . CONCOURS_TABLE .'
    248                                 WHERE id =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . '
     236                                FROM ' . CONCOURS_RESULT_TABLE .'
     237                                WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . '
     238                                ';
     239                        pwg_query($query);
     240                        $query = '
     241                                DELETE
     242                                FROM ' . CONCOURS_DATA_TABLE .'
     243                                WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . '
    249244                                ';
    250245                        pwg_query($query);
     
    257252                        $query = '
    258253                                DELETE
    259                                 FROM ' . CONCOURS_DATA_TABLE .'
    260                                 WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . '
     254                                FROM ' . CONCOURS_TABLE .'
     255                                WHERE id =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . '
    261256                                ';
    262257                        pwg_query($query);
    263                         $query = '
    264                                 DELETE
    265                                 FROM ' . CONCOURS_RESULT_TABLE .'
    266                                 WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . '
    267                                 ';
    268                         pwg_query($query);
    269258                }
    270259                else
    271260                        return false;
    272261               
     262        }
     263
     264       
     265        // delete all closed concours from db
     266        function delete_allclosed_concours()
     267        {
     268                $concours_list=array();
     269                $query = '
     270                        SELECT id
     271                        FROM ' . CONCOURS_TABLE .'
     272                        WHERE time_to_sec(TIMEDIFF(now(), end_date)) > 0
     273                        AND  id>0;';
     274
     275                $result = pwg_query($query);
     276                while ($row = pwg_db_fetch_assoc($result))
     277                {
     278                        $this->delete_concours($row['id']);
     279                }
    273280        }
    274281
     
    288295
    289296                        $result = pwg_query($query);
    290 if ($this->debug) echo $query."\n";
    291297                        if ($result != NULL)
    292298                                return true;
     
    311317
    312318                        $result = pwg_query($query);
    313 if ($this->debug) echo $query."\n";
    314319                        if ($result != NULL)
    315320                                return true;
     
    321326        }
    322327
    323 
     328        // Get high value of concours (max score for each criterias)
     329        function get_MaxScore_concours($concours_id = null)
     330        {
     331                $max = 0;
     332                $query = '
     333                        SELECT criteria_id, max_value, ponderation, uppercriteria
     334                        FROM ' . CONCOURS_DETAIL_TABLE .'
     335                        WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . '
     336                        ORDER BY criteria_id
     337                        ';
     338                $result = pwg_query($query);
     339                while ($row = pwg_db_fetch_array($result))
     340                {
     341                        if ($row['uppercriteria'] != NULL || !$this->is_criterias_contains_sub($row['criteria_id'])) // subcriteria or uppercriteria without sub
     342                                $max += $row['max_value']*$row['ponderation'];
     343                }
     344                return $max;
     345
     346
     347        }       
     348       
    324349        // Get criterias that are stored on db for default mode (concours_id = 0)
    325350        function get_default_criterias()
     
    328353                $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id
    329354                                        FROM ' . CONCOURS_DETAIL_TABLE . ' ;';
    330                 list($next_element_id) = mysql_fetch_array(pwg_query($query));
     355                list($next_element_id) = pwg_db_fetch_array(pwg_query($query));
    331356
    332357                foreach ($criterias as $criteria)
    333358                {               
    334 if ($this->debug)                       echo "NEXT =".$next_element_id."\n";
     359                       
    335360                        $query = 'INSERT INTO '.CONCOURS_DETAIL_TABLE.'
    336361                                                (id, id_concours, criteria_id, name, descr, min_value, max_value, ponderation, uppercriteria)'
     
    343368                       
    344369                                $result = pwg_query($query);
    345 if ($this->debug)       echo $query;                   
     370
    346371                        $next_element_id = $next_element_id +1;
    347372                }
     
    361386                                ';
    362387                        $result = pwg_query($query);
    363 if ($this->debug) echo $query."\n";
    364                         while ($row = mysql_fetch_assoc($result))
     388                        while ($row = pwg_db_fetch_array($result))
    365389                        {
    366390                                array_push($criteria_list, $row);
     
    386410                                ';
    387411                        $result = pwg_query($query);
    388 if ($this->debug) echo $query."\n";
    389                         while ($row = mysql_fetch_assoc($result))
    390                         {
    391 if ($this->debug)                       echo "criteria_id=".$row['criteria_id']."\n";
     412                       
     413                        while ($row = pwg_db_fetch_array($result))
    392414                                array_push($criteria_list, $row);
    393                         }
    394415                        return $criteria_list;
    395416                }
     
    410431                                ';
    411432                        $result = pwg_query($query);
    412 if ($this->debug) echo $query."\n";
    413                         // mini 1 line
    414                         if(mysql_fetch_assoc($result))
     433                        if(pwg_db_fetch_array($result))
    415434                                return true;
    416435                        else
     
    435454                                ';
    436455                        $result = pwg_query($query);
    437 if ($this->debug) echo $query."\n";
    438                         while ($row = mysql_fetch_assoc($result))
     456                        while ($row = pwg_db_fetch_array($result))
    439457                        {
    440458                                array_push($criteria_list, $row);
     
    460478                                ';
    461479                        $result = pwg_query($query);
    462         if ($this->debug) echo $query."\n";
    463             $criteria = mysql_fetch_assoc($result);
    464         /*              while ($row = mysql_fetch_assoc($result))
    465                         {
    466                                 array_push($criteria, $row);
    467                         }
    468 */             
     480                       
     481            $criteria = pwg_db_fetch_array($result);
    469482                }
    470483                return $criteria;
     
    483496                        ';
    484497                $result = pwg_query($query);
    485 if ($this->debug) echo $query."\n";
    486 $criteria = mysql_fetch_assoc($result);
    487 /*              while ($row = mysql_fetch_assoc($result))
    488                 {
    489                         array_push($criteria, $row);
    490                 }
    491 */             
    492         return $criteria;
     498               
     499                $criteria = pwg_db_fetch_array($result);
     500                return $criteria;
    493501       
    494502        }
     
    504512                        $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id
    505513                                                FROM ' . CONCOURS_DETAIL_TABLE . ' ;';
    506                         list($next_element_id) = mysql_fetch_array(pwg_query($query));
     514                        list($next_element_id) = pwg_db_fetch_array(pwg_query($query));
     515
    507516                        $query = 'SELECT IF(MAX(criteria_id)+1 IS NULL, 1, MAX(criteria_id)+1) AS next_criteria_id
    508517                                                FROM ' . CONCOURS_DETAIL_TABLE . '
    509518                                          WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ) . ' ;';
    510                         list($next_criteria_id) = mysql_fetch_array(pwg_query($query));
     519                        list($next_criteria_id) = pwg_db_fetch_array(pwg_query($query));
    511520                       
    512521                        $query = '
     
    553562
    554563                        pwg_query($query);
    555 
    556                         if ($this->debug) echo $query."\n";
    557 
    558564                }               
    559565        }
     
    573579                                ';
    574580                        $result = pwg_query($query);
    575         if ($this->debug) echo $query."\n";
     581                       
    576582                        if($result)
    577583                                return true;
     
    594600                        ';
    595601                $result = pwg_query($query);
    596 if ($this->debug) echo $query."\n";
    597602
    598603        }       
     
    608613                $result = pwg_query($query);
    609614                // For each images
    610                 if (mysql_fetch_assoc($result))
     615                if (pwg_db_fetch_array($result))
    611616                        return true;
    612617                else
     
    614619       
    615620        }
     621       
     622
     623
    616624       
    617625        // function to return the number of votes for a concours by images
     
    628636                $result = pwg_query($query);
    629637                $nbvotes = array();
    630                 while ($row = mysql_fetch_assoc($result))
     638                while ($row = pwg_db_fetch_array($result))
    631639                {
    632640                        $nbvotes[$row['img_id']] = $row['NBVOTE'];
     
    634642       
    635643        // Add guest infos
    636         $query = 'SELECT img_id, COUNT(DISTINCT user_id, ipguest) AS NBVOTE FROM ' . CONCOURS_DATA_TABLE
     644        $query = 'SELECT img_id, COUNT(DISTINCT ipguest) AS NBVOTE FROM ' . CONCOURS_DATA_TABLE
    637645        .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id )
    638646        .' AND ipguest IS NOT NULL'
     
    641649               
    642650        $result = pwg_query($query);
    643     if ($this->debug)           echo "NBVOTE = ".$query."\n";
    644         while ($row = mysql_fetch_assoc($result))
     651        while ($row = pwg_db_fetch_array($result))
     652       
    645653            if (!isset($nbvotes[$row['img_id']]))
    646654                $nbvotes[$row['img_id']] = $row['NBVOTE'];
    647655            else
    648656                $nbvotes[$row['img_id']] += $row['NBVOTE'];
    649        
     657                       
    650658                return $nbvotes;
    651659        }
    652660
     661       
     662       
     663        // Get All user that are vote  for guest who have vote (IP Stores in db) in a concours
     664        function get_user_list($concours_id = NULL)
     665        {
     666                global $conf;
     667        $userid = array();
     668                if ($concours_id!== null or $this->concours_id !== null)
     669                {
     670                        $query = '
     671                                SELECT distinct(user_id)
     672                                FROM ' . CONCOURS_DATA_TABLE .'
     673                                WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . '
     674                AND ipguest IS  NULL
     675                                AND user_id !='.$conf['guest_id']
     676
     677                                .' ORDER BY user_id
     678                                ';
     679                        $result = pwg_query($query);
     680                        while ($row = pwg_db_fetch_array($result))
     681                        {
     682                                array_push($userid, $row['user_id']);
     683                        }
     684                }
     685                return $userid;
     686        }
     687       
     688       
    653689        // Get All iaddr for guest who have vote (IP Stores in db) in a concours
    654690        function get_guest_list($concours_id = NULL)
    655691        {
     692                global $conf;
    656693        $ipguest = array();
    657694                if ($concours_id!== null or $this->concours_id !== null)
     
    660697                                SELECT distinct(ipguest)
    661698                                FROM ' . CONCOURS_DATA_TABLE .'
    662                                 WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id ) . '
    663                 AND ipguest IS NOT NULL
     699                                WHERE id_concours =' . ($concours_id !== null ? $concours_id : $this->concours_id )
     700                .' AND user_id = '.$conf['guest_id']
     701                .' AND ipguest IS NOT NULL
    664702                                ORDER BY ipguest
    665703                                ';
    666704                        $result = pwg_query($query);
    667 if ($this->debug) echo $query."\n";
    668                         while ($row = mysql_fetch_assoc($result))
     705                        while ($row = pwg_db_fetch_array($result))
    669706                        {
    670707                                array_push($ipguest, $row['ipguest']);
     
    689726                // var which contains id=img_id and val =global note
    690727                $global_note = array();
     728                // FOR MODERATION
     729                // var which contains id=user_id and val =global note
     730                $vote_by_user = array();
     731                $amplitiudeMINmax = 999; // MIN of all max score (for each users)
     732                $amplitiudeMAXmin = -999; // MIN of all max score (for each users)
     733                $REFaverage = 0;        // reference average (average between amplitiudeMINmax and amplitiudeMAXmin)
     734                $global_note_mod1 = array();
     735                $global_note_mod2 = array();
     736               
    691737                if ($concours_id!== null or $this->concours_id !== null)
    692738                {
     
    698744                                $nbvotes = $this->nb_votes_by_img($concours_id);
    699745                               
     746               
    700747                                $user_id = array();
    701748                                // vars not initialized
     
    703750                                        $this->get_concours();
    704751                                $category = $this->concours_infos['category'];
    705 
    706 if ($this->debug)
    707         echo "CAT=".$category;
     752                               
    708753                                // Get all user_id from a concours
    709754                                $query = 'SELECT DISTINCT user_id'
     
    713758                                .';';
    714759                                $result = pwg_query($query);
    715 if ($this->debug)
    716 echo $query;
    717                                 while ($row = mysql_fetch_assoc($result))
     760
     761                                while ($row = pwg_db_fetch_array($result))
    718762                                        array_push($user_id, $row['user_id']);
    719763
    720764                // Add guest info is present
    721765                $ipguest = array();
    722                 if ($concours_id!== null)
     766                if ($concours_id!== null or $this->concours_id !== null)
     767                                {
     768                                        if ($this->debug)
     769                                                echo "Check IPGUEST";
    723770                    $ipguest = $this->get_guest_list(($concours_id !== null ? $concours_id : $this->concours_id ));
    724 
    725 if ($this->debug)
    726 {
    727         foreach ($user_id as $userid)
    728                         echo "USER = ".$userid;
    729         foreach ($ipguest as $userid)
    730                         echo "IPGUEST = ".$userid;
    731 }                               
     771                                }
     772
    732773                               
    733774                                // recover all img_id from the category
     
    736777                                .' WHERE category_id = '.$category.';';
    737778                               
     779                                $nb_image_concours = 0;
    738780                                $result = pwg_query($query);
    739781                                // For each images
    740                                 while ($row = mysql_fetch_assoc($result))
     782                                while ($row = pwg_db_fetch_array($result))
    741783                                {
    742                 if ($this->debug)       echo "IMG_ID=".$row['image_id']."\n";
     784                                        $nb_image_concours++;
     785               
    743786                                        foreach ($user_id as $i => $userid)
    744787                                        {
     788                                                $img_note_user = $this->get_img_globalnote_user($row['image_id'], null, $userid);
     789                                               
    745790                                                if (!isset($global_note[$row['image_id']]))
    746791                                                        $global_note[$row['image_id']] = 0;
    747                                                 $global_note[$row['image_id']] +=  $this->get_img_globalnote_user($row['image_id'], null, $userid);                     
     792                                        // FOR MODERATION
     793                                        // store the nb of vote for each user
     794                                                if ($img_note_user >= 0) // user has made a vote
     795                                                {
     796                                                        if (!isset($vote_by_user[$userid]['nbvote']))
     797                                                                $vote_by_user[$userid]['nbvote'] = 0;
     798                                                       
     799                                                        $vote_by_user[$userid]['nbvote'] ++;    // update vote counter
     800                                               
     801                                                        $global_note[$row['image_id']] +=  $img_note_user       ;               
     802                                               
     803
     804                                                // FOR MODERATION
     805                                                        // store the total of note for the same user
     806                                                        if (!isset($vote_by_user[$userid]['totalvote']))
     807                                                                $vote_by_user[$userid]['totalvote'] = 0;
     808                                                        $vote_by_user[$userid]['totalvote'] +=  $img_note_user;
     809                                                        // FOR MODERATION
     810                                                        // store min/max for each user
     811                                                        if (!isset($vote_by_user[$userid]['min']))      $vote_by_user[$userid]['min'] = 999;
     812                                                        if (!isset($vote_by_user[$userid]['max']))      $vote_by_user[$userid]['max'] = 0;
     813                                                       
     814                                                       
     815                                                        if ($vote_by_user[$userid]['min'] > $img_note_user) $vote_by_user[$userid]['min'] = $img_note_user;
     816                                                        if ($vote_by_user[$userid]['max'] < $img_note_user) $vote_by_user[$userid]['max'] = $img_note_user;
     817                                                }                                               
     818                                       
    748819                                        }
    749820                    // Add guest scores if present
    750                                         foreach ($ipguest as $i => $ipguestt)
     821                                        foreach ($ipguest as $i => $userid)
    751822                                        {
     823                                               
     824                                                $img_note_user = $this->get_img_globalnote_user($row['image_id'], null, null, $userid); // $userid contains the ip address for the guest!
    752825                                                if (!isset($global_note[$row['image_id']]))
    753826                                                        $global_note[$row['image_id']] = 0;
    754                                                 $global_note[$row['image_id']] +=  $this->get_img_globalnote_user($row['image_id'], null, null, $ipguestt);                     
     827
     828                                        // FOR MODERATION
     829                                        // store the nb of vote for each guest (with ipaddr)
     830                                                if ($img_note_user >= 0) // user has made a vote
     831                                                {
     832                                                        if (!isset($vote_by_user[$userid]['nbvote']))
     833                                                                $vote_by_user[$userid]['nbvote'] = 0;
     834                                                       
     835                                                        $vote_by_user[$userid]['nbvote'] ++;    // update vote counter
     836                                               
     837                                                        $global_note[$row['image_id']] +=  $img_note_user       ;               
     838                                               
     839
     840                                                // FOR MODERATION
     841                                                        // store the total of note for the same user
     842                                                        if (!isset($vote_by_user[$userid]['totalvote']))
     843                                                                $vote_by_user[$userid]['totalvote'] = 0;
     844                                                        $vote_by_user[$userid]['totalvote'] +=  $img_note_user;
     845                                                        // FOR MODERATION
     846                                                        // store min/max for each user
     847                                                        if (!isset($vote_by_user[$userid]['min']))      $vote_by_user[$userid]['min'] = 999;
     848                                                        if (!isset($vote_by_user[$userid]['max']))      $vote_by_user[$userid]['max'] = 0;
     849                                                       
     850                                                       
     851                                                        if ($vote_by_user[$userid]['min'] > $img_note_user) $vote_by_user[$userid]['min'] = $img_note_user;
     852                                                        if ($vote_by_user[$userid]['max'] < $img_note_user) $vote_by_user[$userid]['max'] = $img_note_user;
     853                                                }                                               
     854
    755855                                        }
    756856                                }
    757                                 if ($this->debug)
     857
     858                                // FOR MODERATION
     859                                // calcul the average by user and min/max global
     860                                foreach ($user_id as $i => $userid)
    758861                                {
    759                                         foreach ($global_note as $id => $val)
    760                                                 echo "Note img ( ".$id.") = ".$val."\n";
     862                                        $vote_by_user[$userid]['average'] = $vote_by_user[$userid]['totalvote']/$vote_by_user[$userid]['nbvote'];
     863                                        $vote_by_user[$userid]['lowerAmp'] = $vote_by_user[$userid]['min'];
     864                                        // calcul the max value of the concours (for the moderation)
     865                                        $vote_by_user[$userid]['upperAmp'] = $this->get_MaxScore_concours() - $vote_by_user[$userid]['max'];
     866                                       
     867                                        $vote_by_user[$userid]['minLevel'] = $vote_by_user[$userid]['average'] - $vote_by_user[$userid]['lowerAmp'];
     868                                        $vote_by_user[$userid]['maxLevel'] = $vote_by_user[$userid]['average'] + $vote_by_user[$userid]['upperAmp'];
     869                                        if ($amplitiudeMINmax > $vote_by_user[$userid]['maxLevel']) $amplitiudeMINmax = $vote_by_user[$userid]['maxLevel'];
     870                                        if ($amplitiudeMAXmin < $vote_by_user[$userid]['minLevel']) $amplitiudeMAXmin = $vote_by_user[$userid]['minLevel'];                             
    761871                                }
     872
     873                                // calcul the average by guest and min/max global
     874                                foreach ($ipguest as $i => $userid)
     875                                {
     876                                        $vote_by_user[$userid]['average'] = $vote_by_user[$userid]['totalvote']/$vote_by_user[$userid]['nbvote'];
     877                                        $vote_by_user[$userid]['lowerAmp'] = $vote_by_user[$userid]['min'];
     878                                        // calcul the max value of the concours (for the moderation)
     879                                        $vote_by_user[$userid]['upperAmp'] = $this->get_MaxScore_concours() - $vote_by_user[$userid]['max'];
     880                                       
     881                                        $vote_by_user[$userid]['minLevel'] = $vote_by_user[$userid]['average'] - $vote_by_user[$userid]['lowerAmp'];
     882                                        $vote_by_user[$userid]['maxLevel'] = $vote_by_user[$userid]['average'] + $vote_by_user[$userid]['upperAmp'];
     883                                        if ($amplitiudeMINmax > $vote_by_user[$userid]['maxLevel']) $amplitiudeMINmax = $vote_by_user[$userid]['maxLevel'];
     884                                        if ($amplitiudeMAXmin < $vote_by_user[$userid]['minLevel']) $amplitiudeMAXmin = $vote_by_user[$userid]['minLevel'];                             
     885
     886                                }
     887
     888
     889                                // FOR MODERATION
     890                                // calcul the reference average
     891                                $calcultemp = (int)($amplitiudeMINmax+$amplitiudeMAXmin)/2+0.5;
     892                                if ((int)(($amplitiudeMINmax+$amplitiudeMAXmin)/2+0.5) > $amplitiudeMAXmin)
     893                                {
     894                                        if ((int)(($amplitiudeMINmax+$amplitiudeMAXmin)/2+0.5) < $amplitiudeMINmax)
     895                                                $REFaverage = (int)(($amplitiudeMINmax+$amplitiudeMAXmin)/2+0.5);
     896                                        else
     897                                                $REFaverage = $amplitiudeMINmax;
     898                                }
     899                                else
     900                                        $REFaverage = $amplitiudeMAXmin;
     901                               
     902                                       
     903                                // FOR MODERATION
     904                                // calcul the moderation scopr for each users
     905                                foreach ($user_id as $i => $userid)
     906                                        $vote_by_user[$userid]['moderation'] = $REFaverage - $vote_by_user[$userid]['average'];
     907
     908                                // FOR MODERATION
     909                                // calcul the moderation scopr for each guest ($ipguest)
     910                                foreach ($ipguest as $i => $userid)
     911                                        $vote_by_user[$userid]['moderation'] = $REFaverage - $vote_by_user[$userid]['average'];
     912                                       
     913                                // FOR MODERATION
     914                                // need to parse again image for new calcul with moderation
     915                                // moderation 1 => For each photo without note (user = author or user != author but no vote) add each user avaerage to the total of note and calcul the average
     916                                // moderation 2 => For each photo with note, adapt/change all the value with the moderation value : user note + moderation.
     917
     918                                // parse again all img_id from the category
     919                                $query = 'SELECT DISTINCT(image_id)'
     920                                .' FROM ' .IMAGE_CATEGORY_TABLE
     921                                .' WHERE category_id = '.$category.';';
     922                               
     923                                $result = pwg_query($query);
     924                                // For each images
     925                                while ($row = pwg_db_fetch_array($result))
     926                                {
     927                                        // for each user
     928                                        foreach ($user_id as $i => $userid)
     929                                        {
     930                                                if (!isset($global_note_mod1[$row['image_id']]))
     931                                                        $global_note_mod1[$row['image_id']] = 0;
     932                                                $globalnote_user = $this->get_img_globalnote_user($row['image_id'], null, $userid, null, 1, $vote_by_user[$userid]['average']);
     933
     934                                                $global_note_mod1[$row['image_id']] += ($globalnote_user >= 0 ? $globalnote_user : 0)   ;
     935
     936                                                if (!isset($global_note_mod2[$row['image_id']]))
     937                                                        $global_note_mod2[$row['image_id']] = 0;
     938                                                $globalnote_user = $this->get_img_globalnote_user($row['image_id'], null, $userid, null, 2, $vote_by_user[$userid]['moderation']);
     939
     940                                                $global_note_mod2[$row['image_id']] +=  ($globalnote_user >= 0 ? $globalnote_user : 0)  ;               
     941                                        }
     942                                       
     943                                        // for each guest
     944                                        foreach ($ipguest as $i => $userid)
     945                                        {
     946                                                if (!isset($global_note_mod1[$row['image_id']]))
     947                                                        $global_note_mod1[$row['image_id']] = 0;
     948                                                $globalnote_user = $this->get_img_globalnote_user($row['image_id'], null, null, $userid, 1, $vote_by_user[$userid]['average']); // userid contains the ip addr for the guest
     949
     950                                                $global_note_mod1[$row['image_id']] += ($globalnote_user >= 0 ? $globalnote_user : 0)   ;
     951
     952                                                if (!isset($global_note_mod2[$row['image_id']]))
     953                                                        $global_note_mod2[$row['image_id']] = 0;
     954                                                $globalnote_user = $this->get_img_globalnote_user($row['image_id'], null, null, $userid, 2, $vote_by_user[$userid]['moderation']);
     955
     956                                                $global_note_mod2[$row['image_id']] +=  ($globalnote_user >= 0 ? $globalnote_user : 0)  ;               
     957                                        }
     958
     959                                       
     960                                }
     961                                       
    762962                               
    763963                                // update database and store result into RESULT table
    764964                                // note = global note
    765965                                $IMGmoyenne = 0;
     966                                $IMGmoyenneModeration1 = 0;
     967                                $IMGmoyenneModeration2 = 0;
     968                               
    766969                                foreach ($global_note as $id => $val)
    767970                                // id contains the image id
    768971                                {
    769 if ($this->debug)               echo $nbvotes[$id];
    770972                                        $IMGmoyenne = (isset($nbvotes[$id]) && $nbvotes[$id] != 0 ? $val/$nbvotes[$id] : 0);
     973                                        // Moderation
     974
     975                                        $IMGmoyenneModeration1 = ($nb_image_concours != 0 ? $global_note_mod1[$id]/$this->nb_user_by_concours() : 0);   // all photos have note
     976                                        $IMGmoyenneModeration2 = (isset($nbvotes[$id]) && $nbvotes[$id] != 0 ? $global_note_mod2[$id]/$nbvotes[$id] : 0);       // only voted photos are used
    771977                                       
    772                                         $query = 'INSERT INTO  ' . CONCOURS_RESULT_TABLE .'(`id_concours`, `img_id`, `date` , `note`, `moyenne`, `nbvotant` )
     978                                        $query = 'INSERT INTO  ' . CONCOURS_RESULT_TABLE .'(`id_concours`, `img_id`, `date` , `note`, `moyenne`,
     979                                        `moderation1`, `moderation2`,
     980                                        `nbvotant` )
    773981                                                        VALUES ('.($concours_id !== null ? $concours_id : $this->concours_id )
    774982                                                                         .', '.$id
     
    776984                                                                         .', '.$val
    777985                                                                         .', '.$IMGmoyenne
     986                                                                         .', '.$IMGmoyenneModeration1
     987                                                                         .', '.$IMGmoyenneModeration2
    778988                                                                         .', '.(isset($nbvotes[$id]) ? $nbvotes[$id] : 0)
    779989                                                                         .');';
    780                                 $result = pwg_query($query);
    781                                                  
     990                                $result = pwg_query($query);     
    782991                                       
    783992                                }
    784            
    785                                
    786 
    787                                
    788993                        }
    789994                        else
     
    794999        }
    7951000       
    796 
    797         // Check if an image's author is the same as the actual user
     1001       
     1002        // Get array with nb of vote for each user of a concours_id
     1003        function nb_user_by_concours($concours_id = NULL)
     1004        {
     1005                $nbusertotal = 0;
     1006                // user
     1007                $query = 'SELECT count(distinct(user_id)) as nb_user FROM ' . CONCOURS_DATA_TABLE .'
     1008                                WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id).'
     1009                                AND ipguest IS NULL
     1010                          ;';
     1011                $result = pwg_query($query);
     1012                if ($result)
     1013                {
     1014                        $row = pwg_db_fetch_array($result);
     1015                        $nbusertotal += $row['nb_user'];
     1016                }
     1017                // guest
     1018                $query = 'SELECT count(distinct(ipguest)) as nb_user FROM ' . CONCOURS_DATA_TABLE .'
     1019                                WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id).'
     1020                                AND ipguest IS NOT NULL
     1021                          ;';
     1022                $result = pwg_query($query);
     1023                if ($result)
     1024                {
     1025                        $row = pwg_db_fetch_array($result);
     1026                        $nbusertotal +=  $row['nb_user'];
     1027                }
     1028                return $nbusertotal;
     1029               
     1030        }
     1031       
     1032       
     1033        // Check if an image's author OR addebBy is the same as the actual user
     1034        // depending the config param
    7981035        function check_img_user($img_id)
    7991036        {
    8001037                global $user;
     1038
     1039
    8011040                $query = '
    802                         SELECT author
     1041                        SELECT author, added_by
    8031042                        FROM ' . IMAGES_TABLE .'
    8041043                        WHERE id =' . $img_id . '
     
    8091048                if ($result)
    8101049                {
    811                         $row = mysql_fetch_assoc($result);
     1050                        $row = pwg_db_fetch_array($result);
    8121051                        $authorid = get_userid($row['author']);
    813                         if ($authorid and ($authorid == $user['id']))
    814                                 return true;
    815                         else
    816                                 return false;
     1052                        $addedbyid = $row['added_by'];
     1053
     1054                        switch ($this->my_config['author_vote_available'])
     1055                        {
     1056                       
     1057                                case 0 : // no check (allow to vote for all photos)
     1058                                        return false;
     1059                                       
     1060                                case 1 : // check between author and user
     1061                                        if ($authorid)  // Author name present as username
     1062                                        {
     1063                                                if ($authorid == $user['id'])
     1064                                                        return true;
     1065                                                else
     1066                                                        return false;
     1067                                        }
     1068                                        else
     1069                                        {
     1070                                                // check if author name = user name
     1071                                                $AuthorArray = strip_punctuation2($row['author']);
     1072                                                $UserArray = strip_punctuation2($user['username']);
     1073                                                // Patch if author is empty
     1074                                                if (empty($AuthorArray))
     1075                                                        return false;
     1076                               
     1077                                                if (count(array_intersect($AuthorArray, $UserArray)) == count ($AuthorArray)) // found                         
     1078                                                        return true;
     1079                                                else
     1080                                                        return false;
     1081                                        }
     1082       
     1083                                       
     1084                                case 2 : // check between author and user
     1085                                        if (isset($addedbyid) AND $addedbyid == $user['id'])
     1086                                                return true;
     1087                                        else
     1088                                                return false;
     1089                                case 3 : // check between author OR addedby and user
     1090                                        // Check Author
     1091                                        if ($authorid AND $authorid == $user['id'])     // Author name present as username
     1092                                                        return true;
     1093                                        else
     1094                                        {
     1095                                                // check if author name = user name
     1096                                                $AuthorArray = strip_punctuation2($row['author']);
     1097                                                $UserArray = strip_punctuation2($user['username']);                                                             
     1098                                                if (empty($AuthorArray) AND (count(array_intersect($AuthorArray, $UserArray)) == count ($AuthorArray))) // found                               
     1099                                                        return true;
     1100                                        }                       
     1101                                        // check addebBy
     1102                                        if (isset($addedbyid) AND $addedbyid == $user['id'])
     1103                                                return true;
     1104       
     1105                                        return false;
     1106
     1107                                default :
     1108                                        return false;
     1109                               
     1110                        }
     1111
    8171112                }
    8181113                return false;
    819        
    8201114        }
    8211115       
     
    8261120                // Step0 : if user = guest, check the concours param and store ip address to user name.
    8271121                // Step1 : concours is defined to this category AND concours is open AND user is authorized to access to this category (thru group)
    828                 // Step1 bis : dont show concours if img_author = user_id
     1122                // Step1 bis : dont show concours if check author or added_by id is the samie as used_id (depending the param)
    8291123                // Step 2 : Recover stored informations in db for this user
    8301124                // Step 3 : Complete tpl information
     
    8391133
    8401134        // STEP 0
    841 if ($this->debug)       echo "STEP0\n";
    842 
    843     $user['ipguest'] = null;
    844 
    845     echo "user id=".$user['id'];
    846     // Check if the user is a guest only
    847     if (is_a_guest())
    848     {
    849         if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
    850             $IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
    851         elseif(isset($_SERVER['HTTP_CLIENT_IP']))   
    852             $IP = $_SERVER['HTTP_CLIENT_IP'];   
    853         else
    854             $IP = $_SERVER['REMOTE_ADDR']; 
    855              
    856         // store ip
    857         $user['ipguest'] = $IP;
    858 if ($this->debug)           
    859     echo "user ip guest=".$user['ipguest'];
    860 
    861 }
    862 
    863 
    864                
    865 // DEBUG
    866 if ($this->debug)
    867 {
    868     foreach ($user as $id=>$val)
    869     {
    870         if (is_array($val))
    871             foreach ($val as $id2=>$val2)
    872                 echo "user[".$id."][".$id2."]=".$val2."\n";
    873         else
    874             echo "user[".$id."]=".$val."\n";
    875     }
    876 }
    877 
    878 //  END DEBUG
     1135                $user['ipguest'] = null;
     1136
     1137                // Check if the user is a guest only
     1138                if (is_a_guest())
     1139                {
     1140                        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
     1141                                $IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
     1142                        elseif(isset($_SERVER['HTTP_CLIENT_IP']))   
     1143                                $IP = $_SERVER['HTTP_CLIENT_IP'];   
     1144                        else
     1145                                $IP = $_SERVER['REMOTE_ADDR']; 
     1146                                 
     1147                        // store ip
     1148                        $user['ipguest'] = $IP;
     1149                }
    8791150       
    8801151       
    881 // DEBUG
    882 if ($this->debug)
    883 foreach ($page as $id=>$val)
    884 {
    885         if (is_array($val))
    886                 foreach ($val as $id2=>$val2)
    887                         echo "page[".$id."][".$id2."]=".$val2."\n";
    888         else
    889                 echo "page[".$id."]=".$val."\n";
    890 }
    891 //  END DEBUG
    892 
    893         // disable author name  on image which are present in a concours.
    894         // check the categories where the current image is present and disable the author name
     1152                // disable author name  on image which are present in a concours.
     1153                // check the categories where the current image is present and disable the author name
    8951154                if (isset($this->my_config['mask_author']) && $this->my_config['mask_author'] == true)
    8961155                {
     
    9021161                          WHERE image_id = '.$page['image_id'].'
    9031162                          ;';
    904 //                     echo $query;
    9051163                        $result = pwg_query($query);
    9061164                        $related_categories = array();
    907                         while ($row = mysql_fetch_array($result))
     1165                        while ($row = pwg_db_fetch_array($result))
    9081166                        {
    9091167                          array_push($related_categories, $row['category_id']);
     
    9201178                    AND time_to_sec(TIMEDIFF(now(), end_date)) < 0
    9211179                ';
    922         //                echo $query;
    9231180                $result = pwg_query($query);
    9241181                // If one or more concours are found, the author name is masked
    925                 if ($result && mysql_fetch_assoc($result))
    926                 {
    927                     $template->assign('INFO_AUTHOR',l10n('concours_img_author'));
    928                 }
    929             }
    930                 }
    931 
    932         // End disable author name
    933 
    934 
    935                 if (($page['section']) == 'categories' AND !empty($page['category']))
    936                 {
    937 if ($this->debug)       echo "STEP1\n";
    938                         //------------
    939                         // Step 1
    940 
    941 /*           
    942             if ($this->my_config['mask_author'] && $this->my_config['mask_author'] == true)
    943             {
    944                 // Request for all concours prepared & actived
    945                 $query = '
    946                     SELECT *
    947                     FROM ' . CONCOURS_TABLE .'
    948                     WHERE category =' . $page['category']['id'] . '
    949                     AND time_to_sec(TIMEDIFF(now(), end_date)) < 0
    950                                 ';
    951 //                echo $query;
    952                 $result = pwg_query($query);
    953                 if ($result && mysql_fetch_assoc($result))
     1182                if ($result && pwg_db_fetch_array($result))
    9541183                    $template->assign('INFO_AUTHOR',l10n('concours_img_author'));
    9551184            }
    956 */
     1185                }
     1186
     1187        // End disable author name
     1188
     1189
     1190                if (($page['section']) == 'categories' AND !empty($page['category']))
     1191                {
     1192                        // Step 1
    9571193           
    9581194                        $query = '
     
    9651201
    9661202                        $result = pwg_query($query);
    967 if ($this->debug) echo $query."\n";
    968                         while ($row = mysql_fetch_assoc($result))
     1203
     1204                        while ($row = pwg_db_fetch_array($result))
    9691205                        {
    970                                 if (!empty($row['groups']))
     1206                                if (!is_a_guest() AND !empty($row['groups']))
    9711207                                {
    972 if ($this->debug)       echo "GROUPS=".$row['groups'];                         
    973                                         $authorized_groups = explode(',', $row['groups']);
    974                                         if (array_intersect($this->user_groups, $authorized_groups) == array())
    975                                         {
    976 if ($this->debug)       echo "ERROR GROUPS";
    977                                                 continue;
     1208
     1209                                        if (is_admin() AND $row['admin'])       // allow admin
     1210                                                $concours = $row;
     1211                                        else
     1212                                        {               
     1213                                                $authorized_groups = explode(',', $row['groups']);
     1214                                                if (array_intersect($this->user_groups, $authorized_groups) == array())
     1215                                                        continue;
     1216                                                // If no group is parameter for that concours  ==> available for all registered users
     1217                                                $concours = $row;
    9781218                                        }
    979                     // If no group is parameter for that concours  ==> available for all registered users
    980                                         $concours = $row;
    9811219                                }
    982                 else
     1220                else
     1221                                       
    9831222                                        $concours = $row;
    984 
    985 
    9861223                        }
    987 
    988 if ($this->debug)       echo "STEP1 bis\n";
    9891224                        //------------
    9901225                        // Step 1 bis
    9911226                        // Actual user is  the author of the picture ==>end
    992                         if ( !$this->my_config['author_vote_available']
    993                 AND $this->check_img_user($page['current_item']))
     1227                        // for the image, check (depending the config) if user = author or addedby or none
     1228                        if ($this->check_img_user($page['current_item']))
    9941229                                return;
    995                        
    996 
    997 if ($this->debug)       echo "STEP2\n";
     1230                               
     1231                        // Check if user is guest and if guest is allowed
     1232                        if (is_a_guest() AND array_key_exists('guest', $concours) AND !$concours['guest'])
     1233                                return;
     1234                        // Check if user is admin and if admin is allowed
     1235                        if (is_admin() AND (!isset($concours['admin']) OR (isset($concours['admin']) AND !$concours['admin'])))
     1236                                return;
     1237                       
    9981238                        //------------
    9991239                        // Step 2
     
    10031243                                if (isset($_POST['concours_submit']))
    10041244                                {
    1005                 
     1245         
    10061246//                    array_push($page['infos'] , l10n('concours_vote_saved'));
    10071247                   
    10081248                                        $user_note = "";
    1009                 if ($this->debug)                       echo "SUBMIT";
    1010                                 // concat all the notes to save on db
    1011                                
     1249                                       
     1250                                        // concat all the notes to save on db
    10121251                                        $firstcriterias = $this->get_firstlevel_criterias($concours['id']);
    10131252                                        foreach ($firstcriterias as $criteria)
    10141253                                        {
    1015                                                
    10161254                                                // First without sub criterias
    10171255                                                if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] ))
    10181256                                                {
    10191257                                                        // Check value
    1020                                                         $value = str_replace(",",".",$_POST[$criteria['criteria_id']]);
     1258                                                        $value = str_replace(",",".",(isset($_POST[$criteria['criteria_id']]) ? $_POST[$criteria['criteria_id']] : floatval($criteria['min_value'])));
    10211259                                                        $value = str_replace(" ","",$value);
    10221260                                                        $value = floatval($value);
     
    10341272                                                        {
    10351273                                                                // Check value
    1036                                                                 $value = str_replace(",",".",$_POST[$subcriteria['criteria_id']]);
     1274                                                                $value = str_replace(",",".",(isset($_POST[$subcriteria['criteria_id']]) ? $_POST[$subcriteria['criteria_id']] : floatval($subcriteria['min_value'] )));
    10371275                                                                $value = str_replace(" ","",$value);
    10381276                                                                $value = floatval($value);
     
    10471285                                        }
    10481286                               
    1049 //                              $datas = "0=0";
    10501287                                        $this->store_img_note_user($page['current_item'], $user_note, $concours['id'], $user['ipguest']);
    1051 if ($this->debug)       echo "COMMENT=".$_POST['CONCOURS_COMMENT'];
    10521288                                        $this->store_img_comment_user($page['current_item'], $_POST['concours_comment'], $concours['id'], $user['ipguest']);
    10531289                                }
     
    10561292                                if (isset($_POST['concours_raz']))
    10571293                                {
    1058 if ($this->debug)                       echo "RAZ";                             
    1059                                         $this->RAZ_img_note_user($page['current_item'], $concours['id'], $user['ipguest']);
     1294                                        $this->delete_img_note_user($page['current_item'], $concours['id'], $user['ipguest']);
     1295                                        $user_notes = array();
    10601296                                }
    10611297                                else
     
    10681304                                $comment = $this->get_img_comment_user($page['current_item'], $concours['id'], $user['id'], $user['ipguest']);
    10691305
    1070 if ($this->debug)       echo "STEP3\n";
    10711306                                //------------
    10721307                                // Step 3
     
    10751310                                foreach ($firstcriterias as $criteria)
    10761311                                {
    1077 if ($this->debug)                                       echo "criteriaID=".$criteria['criteria_id']."\n";
    10781312                                        // First without sub criterias
    10791313                                        if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours['id'] ))
     
    11021336                                                foreach ($secondcriterias as $subcriteria)
    11031337                                                {
    1104 if ($this->debug)                                                       echo "subcriteriaID=".$criteria['criteria_id']."\n";
    11051338                                                        $template->append( 'concours_criteria', array(
    11061339                                                                        'nosub' => true,
     
    11231356                                // if a user has already vote for this photo, check if the score change is allowed
    11241357
    1125 if ($this->debug)           echo "change=".($this->my_config['concours_change_score'] ? "OK" : "KO");
     1358
    11261359                if ($user_notes == array() OR $this->my_config['concours_change_score'])  // Allow user to change his vote after a validation
    11271360                    $template->assign( 'CONCOURS_CHANGE_SCORE', true);
     
    11291362                    $template->assign( 'CONCOURS_CHANGE_SCORE', false);
    11301363               
    1131                                
     1364                                $noteuser = $this->get_img_globalnote_user($page['current_item'], $concours['id'], null, $user['ipguest']);
    11321365                                // Add concours informations on template
    11331366                                $template->assign( 'concours_infos', array(
    11341367                                                        'name'  => $concours['name'],
    11351368                                                        'descr' => $concours['descr'],
    1136                             'note'  => $this->get_img_globalnote_user($page['current_item'], $concours['id'], null, $user['ipguest'])
     1369                            'note'  => ($noteuser >= 0 ? $noteuser : 0 ),
     1370                                                        'begin_date' => $concours['begin_date'],
     1371                                                        'end_date' => $concours['end_date'],
     1372                                                        'end_concours_min' => (strtotime($concours['end_date'])-time()) ,
     1373                                                        'max_note' => $this->get_concours_maxnote($concours['id']),
     1374                                                        'deadline_type' => $this->my_config['concours_deadline']
    11371375                                                                ));
    11381376                               
     1377
     1378                                $template->assign( 'SCORE_MODE', $this->my_config['score_mode']);
     1379                                $template->assign( 'TEXT_OVERLAY', $this->my_config['text_overlay']);
    11391380                       
    11401381                                $template->set_filenames(array('concours' =>  CONCOURS_ROOT.'/template/concours.tpl'));
    1141 //                              $template->concat('PLUGIN_PICTURE_AFTER', $template->parse('concours', true));
    11421382                                $template->concat('COMMENT_IMG', $template->parse('concours', true));
    11431383
    1144                                 //
    11451384                                $template->assign('INFO_AUTHOR',l10n('concours_img_author'));
    11461385                        }
     
    11481387        }
    11491388
     1389       
     1390        // RAZ notation from user to db
     1391        // fill criterias notes to 0 in DB
     1392        // return false if error
     1393        function delete_img_note_user($img_id, $concours_id = NULL, $ipguest = null, $userid=null)
     1394        {
     1395                global $user;
     1396               
     1397                if ($concours_id === null)
     1398                        if ($this->concours_id !== null)
     1399                                $concours_id = $this->concours_id;
     1400                        else
     1401                                return false;
     1402                               
     1403                $query = '
     1404                DELETE FROM ' . CONCOURS_DATA_TABLE .'
     1405                WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id ).'
     1406                AND user_id = '.($userid == null ? $user['id'] : $userid).'
     1407                AND img_id = '.$img_id.
     1408                ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '')           
     1409                .';';
     1410
     1411                return pwg_query($query);
     1412
     1413        }
     1414       
     1415       
    11501416        // RAZ notation from user to db
    11511417        // fill criterias notes to 0 in DB
     
    11631429                foreach ($firstcriterias as $criteria)
    11641430                {
    1165 if ($this->debug)                                       echo "ID=".$criteria['criteria_id']."\n";
    11661431                        // First without sub criterias
    11671432                        if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id ))
    1168                         {
    11691433                                $user_note .= (strlen($user_note) != 0 ? ";" : "").$criteria['criteria_id']."=".$criteria['min_value'];
    1170                         }
    11711434                        else
    11721435                        {
    11731436                                $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id );
    11741437                                foreach ($secondcriterias as $subcriteria)
    1175                                 {
    1176 if ($this->debug)                                                       echo "ID=".$criteria['criteria_id']."\n";
    11771438                                        $user_note .= (strlen($user_note) != 0 ? ";" : "").$subcriteria['criteria_id']."=".$subcriteria['min_value'];
    1178                                 }
    11791439                        }
    11801440                }
    1181 if ($this->debug)       echo "RAZ=>".$user_note."\n";
    11821441                if (strlen($user_note) != 0)
    11831442                {
     
    11871446                else
    11881447                        return false;
    1189                                
    1190 
    11911448        }
    11921449
     
    11991456                {
    12001457                        $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id  FROM ' . CONCOURS_DATA_TABLE . ' ;';
    1201                         list($next_element_id) = mysql_fetch_array(pwg_query($query));
    1202 if ($this->debug)       echo "Next_element=".$next_element_id."\n";
     1458                        list($next_element_id) = pwg_db_fetch_array(pwg_query($query));
     1459
    12031460                        $query = '
    12041461                        INSERT INTO ' . CONCOURS_DATA_TABLE .'
     
    12291486            .';';
    12301487                }
    1231 if ($this->debug)                       echo $query."\n";
    1232                         pwg_query($query);
     1488                pwg_query($query);
    12331489       
    12341490        }
     
    12401496        function get_img_note_user($img_id, $concours_id = NULL, $user_id = null, $ipguest = null)
    12411497        {
    1242                 global $user;
     1498                global $user, $conf;
    12431499                $img_note = array();
    12441500                if ($concours_id!== null or $this->concours_id !== null)
     
    12471503                        SELECT datas
    12481504                        FROM ' . CONCOURS_DATA_TABLE .'
    1249                         WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . '
    1250                         AND user_id = '. ($user_id !== null ? $user_id : $user['id']).'
    1251                         AND img_id = '. $img_id .
    1252             ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '')
     1505                        WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id)
     1506                        . ($ipguest == null ? ' AND user_id = '. ($user_id !== null ? $user_id : $user['id'])
     1507                           : ' AND user_id = '.$conf['guest_id'] .' AND ipguest = "'.$ipguest.'"')
     1508                        .' AND img_id = '. $img_id
    12531509                        .';';
    1254 if ($this->debug)                       echo $query."\n";
    1255                         $result = pwg_query($query);
    1256                        
    1257                         while ($row = mysql_fetch_assoc($result))
    1258                         {
    1259 if ($this->debug)                               echo "row[data]=".$row['datas']."\n";
    1260                                 $datas = explode(";",$row['datas']);
    1261                                 foreach ($datas as $val)
    1262                                 {
    1263                                         if (strpos($val, '=') !== FALSE)
    1264                                                 $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1);
    1265                                 }
    1266                         }
    1267 // DEBUG
    1268 if ($this->debug)
    1269 foreach ($img_note as $id=>$val)
    1270 {
    1271         if (is_array($val))
    1272                 foreach ($val as $id2=>$val2)
    1273                         echo "img_note[".$id."][".$id2."]=".$val2."\n";
    1274         else
    1275                 echo "img_note[".$id."]=".$val."\n";
    1276 }
    1277 //  END DEBUG
    1278                        
    1279                         return $img_note;
    1280                 }
    1281                 else
    1282                         return array();
    1283                
    1284                
    1285         }
    1286 
    1287        
    1288         // get notation's user from db
    1289         // Return the global note for a picture id from a user
    1290         function get_img_globalnote_user($img_id, $concours_id = NULL, $user_id = null, $ipguest = null)
    1291         {
    1292                 global $user;
    1293                 $img_note = array();
    1294                 $global_user_note = 0;
    1295                 if ($concours_id!== null or $this->concours_id !== null)
    1296                 {
    1297                         $query = '
    1298                         SELECT datas
    1299                         FROM ' . CONCOURS_DATA_TABLE .'
    1300                         WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id) . '
    1301                         AND user_id = '. ($user_id !== null ? $user_id : $user['id']).'
    1302                         AND img_id = '. $img_id .
    13031510            ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '')
    13041511                        .';';
    13051512                        $result = pwg_query($query);
    13061513                       
    1307                         while ($row = mysql_fetch_assoc($result))
     1514                        while ($row = pwg_db_fetch_array($result))
     1515
    13081516                        {
    13091517                                $datas = explode(";",$row['datas']);
     
    13151523                        }
    13161524                       
     1525                        return $img_note;
     1526                }
     1527                else
     1528                        return array();
     1529        }
     1530
     1531
     1532
     1533        // Return the max note for a concours
     1534        function get_concours_maxnote($concours_id = NULL)
     1535        {
     1536
     1537                if ($concours_id!== null or $this->concours_id !== null)
     1538                {
     1539                        $query = '
     1540                        SELECT SUM(max_value) AS TOTAL
     1541                        FROM ' . CONCOURS_DETAIL_TABLE .'
     1542                        WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id)
     1543                        .' AND uppercriteria IS NULL'
     1544                        .';';
     1545                       
     1546                        $result = pwg_query($query);
     1547                        if ($this->debug)                                       echo "query=".$query."<br>\n";
     1548                       
     1549                        if ($row = pwg_db_fetch_array($result))
     1550                        {
     1551                                return $row['TOTAL'];
     1552                        }
     1553                        else
     1554                        return -1;
     1555                       
     1556                }
     1557
     1558       
     1559        }
     1560
     1561
     1562       
     1563        // get notation's user from db
     1564        // Return the global note for a picture id from a user
     1565        // if moderationMode = 1, the 'moderation' contains the GlobalAverage for all the concours
     1566        // if moderationMode = 2, the 'moderation' contains the user moderation for all image that are voted by
     1567        // return -1 if there is no vote for that user
     1568        function get_img_globalnote_user($img_id, $concours_id = NULL, $user_id = null, $ipguest = null, $moderationMode = 0, $moderation = 0)
     1569        {
     1570                global $user, $conf;
     1571                $img_note = array();
     1572                $global_user_note = -1;
     1573                $note_found = false;
     1574                if ($concours_id!== null or $this->concours_id !== null)
     1575                {
     1576                        $query = '
     1577                        SELECT datas
     1578                        FROM ' . CONCOURS_DATA_TABLE .'
     1579                        WHERE id_concours =' . ($concours_id !== NULL ? $concours_id : $this->concours_id)
     1580                        . ($ipguest == null ? ' AND user_id = '. ($user_id !== null ? $user_id : $user['id'])
     1581                           : ' AND user_id = '.$conf['guest_id'] .' AND ipguest = "'.$ipguest.'"')
     1582                        .' AND img_id = '. $img_id
     1583                        .';';
     1584                        $result = pwg_query($query);
     1585                        if ($this->debug)                                       echo "query=".$query."<br>\n";
     1586                       
     1587                        while ($row = pwg_db_fetch_array($result))
     1588                        {
     1589                                $global_user_note = 0; // init note if one vote is present
     1590                                $note_found = true;
     1591                                $datas = explode(";",$row['datas']);
     1592                                foreach ($datas as $val)
     1593                                {
     1594                                        if (strpos($val, '=') !== FALSE)
     1595                                                $img_note[substr($val, 0, strpos($val, '='))]=substr($val, strpos($val, '=')+1);
     1596                                }
     1597                        }
     1598                       
    13171599                        $firstcriterias = $this->get_firstlevel_criterias($concours_id);
    13181600                        foreach ($firstcriterias as $criteria)
    13191601                        {
    1320 if ($this->debug)                                       echo "criteriaID=".$criteria['criteria_id']."\n";
    13211602                                // First without sub criterias
    13221603                                if (!$this->is_criterias_contains_sub($criteria['criteria_id'],$concours_id ))
     
    13271608                                else
    13281609                                {
    1329 
    13301610                                        $secondcriterias = $this->get_subcriterias($criteria['criteria_id'], $concours_id );
    13311611                                        foreach ($secondcriterias as $subcriteria)
    13321612                                        {
    1333 if ($this->debug)                                                       echo "subcriteriaID=".$subcriteria['criteria_id']."\n";
    13341613                                                if (isset($img_note[$subcriteria['criteria_id']]))
    13351614                                                $global_user_note += (int)$subcriteria['ponderation'] * (float)$img_note[$subcriteria['criteria_id']];
    13361615                                        }
    13371616                                }
    1338 
    13391617                        }
    13401618                       
     1619                        // MODERATION
     1620                                // moderation 1 => For each photo without note (user = author or user != author but no vote) add each user avaerage to the total of note and calcul the average
     1621                                // moderation 2 => For each photo with note, adapt/change all the value with the moderation value : user note + moderation.
     1622                       
     1623                        if ($user_id !== null) // only for user
     1624                        {
     1625                                if ($moderationMode == 1 && !$note_found)
     1626                                {               // Moderation1
     1627                                        $global_user_note = $moderation;
     1628                                }
     1629                                elseif ($moderationMode == 2 && $note_found)
     1630                                {       // Moderation2
     1631                                        $global_user_note += $moderation;
     1632                                }               
     1633                               
     1634                               
     1635                        }
    13411636                        return $global_user_note;
    13421637                }
     
    13541649                {
    13551650                        $query = 'SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id  FROM ' . CONCOURS_DATA_TABLE . ' ;';
    1356                         list($next_element_id) = mysql_fetch_array(pwg_query($query));
    1357 if ($this->debug)       echo "Next_element=".$next_element_id."\n";
     1651                        list($next_element_id) = pwg_db_fetch_array(pwg_query($query));
     1652
    13581653                        $query = '
    13591654                        INSERT INTO ' . CONCOURS_DATA_TABLE .'
     
    13811676            .';';
    13821677                }
    1383 if ($this->debug)                       echo $query."\n";
    13841678                        pwg_query($query);
    1385        
    13861679        }
    13871680       
     
    14021695            ($ipguest ? ' AND ipguest = "'.$ipguest.'"' : '')
    14031696                        .';';
    1404 if ($this->debug)                       echo $query."\n";
     1697
    14051698                        $result = pwg_query($query);
    14061699                       
    1407                         while ($row = mysql_fetch_assoc($result))
    1408                         {
    1409 if ($this->debug)                               echo "row[comment]=".$row['comment']."\n";
     1700                        while ($row = pwg_db_fetch_array($result))
    14101701                                $datas = $row['comment'];
    1411                         }
    14121702                }
    14131703                return $datas;
     
    14411731                // For each images
    14421732                $rang = 1;
    1443                 while ($row = mysql_fetch_assoc($result))
     1733                while ($row = pwg_db_fetch_array($result))
    14441734                {
    14451735                        $file .= ($row['nbvotant'] != 0 ? $rang : 'N/A').', '
     
    14671757        {
    14681758        global $conf;
     1759                $MethodType = "0";
    14691760                if ($concours_id === null)
    14701761                        $concours_id = $this->concours_id;
     
    14951786                        $ident1++;
    14961787                }
    1497 if ($this->debug)       echo "CRITERIA_LIST=".$criteria_list."\n";     
    14981788                $user_list = array();
    14991789                $users_criteria = "";
     
    15111801                $result = pwg_query($query);
    15121802                // For each user
    1513                 while ($row = mysql_fetch_assoc($result))
     1803                while ($row = pwg_db_fetch_array($result))
    15141804                {
    15151805                        array_push($user_list, $row);
    15161806                        $users_criteria .= (strlen($users_criteria) ? "," : "")."user, user_global_note, comment, ".$criteria_list;
    1517                        
    15181807                }
    15191808       
     
    15301819        }
    15311820
    1532 if ($this->debug)       echo "users_criteria=".$users_criteria."\n";   
    15331821               
    15341822                // All informations in csv format
    1535                 $file = "rang, id_concours, method, nom_concours, date_debut, date_fin, date_export, img_id, img_name, img_file, img_author, note, moyenne, nbvotant, datas,"
     1823                $file = "rang, id_concours, method, nom_concours, date_debut, date_fin, date_export, img_id, img_name, img_file, img_author, note, moyenne, moderation1, moderation2, nbvotant, datas,"
    15361824                .$users_criteria
    15371825                ."\r\n";
     
    15421830                $nbvotes = $this->nb_votes_by_img($concours_id);
    15431831
    1544                
    15451832                // recover all img_id from the category
    15461833                if (!(isset($this->concours_infos['method'])))
    15471834                        $this->concours_infos['method'] = 1;
    1548                 $query = 'SELECT id_concours, method, img_id, IMG.name, IMG.file, IMG.author, date, note, moyenne, nbvotant, datas, comment'
     1835                $query = 'SELECT id_concours, method, img_id, IMG.name, IMG.file, IMG.author, date, note, moyenne, moderation1, moderation2, nbvotant, datas, comment'
    15491836                .' FROM ' .CONCOURS_RESULT_TABLE
    15501837                .' INNER JOIN ' . CONCOURS_TABLE.' AS CONC on CONC.id = id_concours'
     
    15521839                .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id );
    15531840               
    1554                 if ($this->concours_infos['method'] == 1)       // total
     1841                switch ($this->concours_infos['method'])
     1842                {
     1843                case 1 :        // total
    15551844                        $query .= ' ORDER BY note DESC';
    1556                 elseif ($this->concours_infos['method'] == 2)   // moyenne
     1845                        $MethodType = "1-Sum";
     1846                        break;
     1847                case  2 :       // moyenne
    15571848                        $query .= ' ORDER BY moyenne DESC';
    1558                
     1849                        $MethodType = "2-Average";
     1850                        break;
     1851                case  3 :       // moderation1
     1852                        $query .= ' ORDER BY moderation1 DESC';
     1853                        $MethodType = "3-Average(Mod1)";
     1854                        break;
     1855                case  4 :       // moderation2
     1856                        $query .= ' ORDER BY moderation2 DESC';
     1857                        $MethodType = "4-Average(Mod2)";
     1858                        break;
     1859                }
     1860                               
    15591861                $query .=';';
    15601862               
    1561 if ($this->debug)               echo $query;
    15621863                $result = pwg_query($query);
    15631864                // For each images
     
    15651866        $previousNote = $previousMoy = 0;
    15661867
    1567                 while ($row = mysql_fetch_assoc($result))
     1868                while ($row = pwg_db_fetch_array($result))
    15681869                {
    15691870            // Check and verify for exaequo
     
    15751876                          AND ($row['moyenne'] == $previousMoy)))
    15761877                {
    1577                     $rang --;
    1578                    
     1878                    $rang --;         
    15791879                }
    15801880            }
     
    15821882                        $file .= ($row['nbvotant'] != 0 ? $rang : 'N/A').', '
    15831883                                        .$row['id_concours'].', '
    1584                                         .$row['method'].', '
     1884                                        .$MethodType.', '
    15851885                                        .str_replace(",", "",$this->concours_infos['name']).', '
    15861886                                        .$this->concours_infos['begin_date'].', '
     
    15941894                                        .$row['note'].', '
    15951895                                        .$row['moyenne'].', '
     1896                                        .$row['moderation1'].', '
     1897                                        .$row['moderation2'].', '
    15961898                                        .$row['nbvotant'].', '
    1597 //                                      .(isset($nbvotes[$row['img_id']]) ? $nbvotes[$row['img_id']] : 0).', '
    15981899                                        .($row['datas'] != null ? $row['datas'] : '')
    15991900                                        ;
     
    16051906                                $user_note = $this->get_img_note_user($row['img_id'], $concours_id, $uuser['user_id']);
    16061907                               
    1607                                 $file .= ', '.$uuser['username'].', '.$this->get_img_globalnote_user($row['img_id'], $concours_id, $uuser['user_id'])
    1608 //                                              .', '.($row['comment'] != null ? str_replace(",", "",$row['comment']) : '')
     1908                                $user_global_note = $this->get_img_globalnote_user($row['img_id'], $concours_id, $uuser['user_id']);
     1909                                $file .= ', '.$uuser['username'].', '.($user_global_note >= 0 ? $user_global_note  : 0)
    16091910                                                .', '.($comment != false ? str_replace(array(",","\r\n", "\n", "\r"), " ",$comment) : '')
    16101911                                                .', '
     
    16421943                        foreach ($ipguest as $ipguestt)
    16431944                        {
    1644 //                echo "IP=".$ipguestt."\n";
    16451945                                $comment = $this->get_img_comment_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt);
    16461946                       
    16471947                                $user_note = $this->get_img_note_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt);
    1648                                
    1649                                 $file .= ', Guest('.$ipguestt.'), '.$this->get_img_globalnote_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt)
    1650 //                                              .', '.($row['comment'] != null ? str_replace(",", "",$row['comment']) : '')
     1948                                $user_global_note = $this->get_img_globalnote_user($row['img_id'], $concours_id, $conf['guest_id'], $ipguestt);
     1949                               
     1950                                $file .= ', Guest('.$ipguestt.'), '.($user_global_note >= 0 ? $user_global_note : 0)
    16511951                                                .', '.($comment != false ? str_replace(array(",","\r\n", "\n", "\r"), " ",$comment) : '')
    16521952                                                .', '
     
    16871987
    16881988                }
    1689 if ($this->debug)               echo $file;
    16901989                return utf8_decode($file);
    16911990       
    16921991               
    16931992        }
    1694        
    16951993       
    16961994       
     
    17222020                                                ;";
    17232021                        pwg_query($query);
    1724 if ($this->debug)               echo $query;
    17252022                       
    17262023                        return $filename;
     
    17352032                .' WHERE id_concours = '.($concours_id !== null ? $concours_id : $this->concours_id )
    17362033                .';';
    1737 if ($this->debug)       echo $query;
    17382034                $result = pwg_query($query);
    17392035                if ($result)
    17402036                {
    1741                         $row = mysql_fetch_assoc($result);             
     2037                        $row = pwg_db_fetch_array($result);             
    17422038                        if ($row)
    17432039                                return $row['file_name'];
     
    17622058                        fpassthru($file);
    17632059                }
    1764        
    1765         }
    1766        
    1767         // Activation page administration
     2060        }
     2061       
     2062        // DEPRECATED Piwigo 11 Activation page administration
     2063/*
    17682064        function concours_admin_menu($menu)
    17692065        {
     
    17712067               array(
    17722068                    'NAME' => CONCOURS_NAME,
    1773                     'URL' => get_admin_plugin_menu_link(CONCOURS_ADMIN_PATH.'admin.php')
     2069                    'URL' => get_root_url().'admin.php?page=plugin-'.CONCOURS_DIR.'-manage'
    17742070                )
    17752071            );
    17762072            return $menu;
    17772073        }
     2074*/
    17782075       
    17792076        // register Concours Menubar
    17802077        public function register_blocks( $menu_ref_arr )
    17812078        {
    1782         $menu = & $menu_ref_arr[0];
    1783         if ($menu->get_id() != 'menubar' OR $this->my_config['active_menubar'] == false)
    1784           return;
    1785         $menu->register_block( new RegisteredBlock( 'CONCOURS_menu', 'concours', 'ConcoursPhoto'));
     2079                $menu = & $menu_ref_arr[0];
     2080                if ($menu->get_id() != 'menubar' OR $this->my_config['active_menubar'] == false)
     2081                  return;
     2082                $menu->register_block( new RegisteredBlock( 'CONCOURS_menu', 'concours', 'ConcoursPhoto'));
    17862083        }
    17872084
     
    17892086        public function blockmanager_apply( $menu_ref_arr )
    17902087        {
    1791  
     2088
    17922089                global $user, $template;
    17932090                $menu = & $menu_ref_arr[0];
     
    17982095                if ( ($block = $menu->get_block( 'CONCOURS_menu' ) ) != null )
    17992096                {
    1800 
    1801                   $menu_list=array();
    1802 
    1803                   if(is_admin())
    1804                   {
     2097                        $menu_list=array();
     2098
     2099                        if(is_admin())
     2100                        {
     2101
    18052102                        array_push($menu_list,
    18062103                          array(
    1807                                 'nfo' => "",
    1808                                 'text' => l10n('concours_admin_add'),
    1809                                 'link' => PHPWG_ROOT_PATH . 'admin.php?page=plugin&amp;section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&amp;action=new',
    1810                                 'edit' => ""
     2104                                'NAME' => l10n('concours_admin_add'),
     2105                                'TITLE' => l10n('concours_admin_add'),
     2106                                'URL' => PHPWG_ROOT_PATH . 'admin.php?page=plugin&amp;section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&amp;action=new',
     2107                                'REL' => ""
    18112108                          )
    18122109                        );
    1813                   }
     2110                       
     2111                        }
    18142112
    18152113                        // recover all img_id from the category
    1816                         $query = 'SELECT distinct(id_concours), groups, guest, CONC.name'
     2114                        $query = 'SELECT distinct(id_concours), groups, guest, admin, CONC.name'
    18172115                        .' FROM ' .CONCOURS_RESULT_TABLE
    18182116                        .' INNER JOIN '.CONCOURS_TABLE. ' AS CONC ON CONC.id = id_concours'
     
    18222120                        $result = pwg_query($query);
    18232121                        $nb_concours = 1;
     2122                       
    18242123                        // For each concours
    1825                         while ($row = mysql_fetch_assoc($result))
     2124                        while ($row = pwg_db_fetch_assoc($result))
     2125
    18262126                        {
    18272127                               
     
    18312131                $conc_to_show = false;  // Default ==> dont show
    18322132               
    1833                 // Guest
    1834                                 if (is_a_guest() AND $row['guest'])
     2133                // Guest OR admin
     2134                                if ((is_a_guest() AND $row['guest'])
     2135                                        OR (is_admin() AND $row['admin']))
    18352136                    $conc_to_show = true;
    18362137                // Group present
     2138                               
     2139                               
    18372140                elseif (!empty($row['groups']))
    18382141                                {
     
    18482151                                        {
    18492152                                        $nb_concours ++;
     2153
    18502154                                        array_push($menu_list,
    18512155                                          array(
    1852                                                 'nfo' => "",
    1853                                                 'text' => $row['name'],
    1854                                                 'link' => './index.php?/concours/'.$row['id_concours'],
    1855                                                 'edit' => (is_admin()? PHPWG_ROOT_PATH . 'admin.php?page=plugin&amp;section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&amp;concours=' . $row['id_concours'].'&amp;action=modify':"")
     2156                                                'NAME' => $row['name'],
     2157                                                'TITLE' => $row['name'],
     2158                                                'URL' => './index.php?/concours/'.$row['id_concours'],
     2159                                                'EDIT' => (is_admin()? PHPWG_ROOT_PATH . 'admin.php?page=plugin&amp;section=' . CONCOURS_DIR . '%2Fadmin%2Fadd_concours.php&amp;concours=' . $row['id_concours'].'&amp;action=modify':"")
    18562160                                          )
    18572161                                        );
     
    18612165                  if (count($menu_list))
    18622166                  {
    1863 
    1864                           $block->set_title(l10n('concoursphoto'));
    18652167                          $block->template = CONCOURS_ROOT.'/template/concours_menu.tpl';
    18662168                          $block->data = $menu_list;
     
    18692171        }
    18702172
    1871        
    18722173        function section_init_concours()
    18732174        {
     
    18752176                if ($tokens[0] == 'concours')
    18762177                  $page['section'] = 'concours';
     2178                elseif ($tokens[0] == 'concours_vote')
     2179                  $page['section'] = 'concours_vote';
    18772180        }
    18782181       
     
    18802183        {
    18812184                global $page;
    1882                
    1883                
    18842185                if (isset($page['section']) and $page['section'] == 'concours')
    18852186                {
     
    18882189        }
    18892190
     2191        function index_vote_concours()
     2192        {
     2193                global $page;
     2194                if (isset($page['section']) and $page['section'] == 'concours_vote')
     2195                {
     2196                        include(CONCOURS_PATH . 'concours_vote.php');
     2197                }
     2198        }
     2199       
    18902200       
    18912201  // Show the global note under the thumbnail
    1892   function thumbnail_note($tpl_var)
     2202  function thumbnail_note($tpl_var, $pictures)
    18932203  {
    1894 
    1895         global $page, $user;
     2204        global $page, $user, $template;
    18962205        $this->get_user_groups();
    18972206
     
    19092218        $user['ipguest'] = $IP;
    19102219    }
    1911        
    1912     if ($this->my_config['thumb_note'] && $this->my_config['thumb_note'] == true)
    1913     {
    1914         $concours = array();
    1915         if (($page['section']) == 'categories' AND !empty($page['category']))
    1916         {
    1917             $query = '
    1918                 SELECT *
    1919                 FROM ' . CONCOURS_TABLE .'
    1920                 WHERE category =' . $page['category']['id'] . '
    1921                 AND time_to_sec(TIMEDIFF(begin_date,now())) < 0
    1922                 AND time_to_sec(TIMEDIFF(now(), end_date)) < 0
    1923                 ';
    1924 
    1925             $result = pwg_query($query);
    1926     if ($this->debug) echo $query."\n";
    1927             while ($row = mysql_fetch_assoc($result))
    1928             {
    1929                
    1930                 if (!empty($row['groups']))
    1931                 {
    1932     if ($this->debug)   echo "GROUPS=".$row['groups'];                         
    1933                     $authorized_groups = explode(',', $row['groups']);
    1934                     if (array_intersect($this->user_groups, $authorized_groups) == array())
    1935                     {
    1936     if ($this->debug)   echo "ERROR GROUPS";
    1937                         continue;
    1938                     }
    1939                     $concours = $row;
    1940                 }
    1941                
    1942                 // If no group is parameter for that concours  ==> available for all registered users
    1943                 else
    1944                     $concours = $row;
    1945                
    1946             }
    1947             if (count($concours))
    1948             {
    1949                 // Check if user is guest and if guest is allowed
    1950                 if ((is_a_guest() AND $concours['guest'])
    1951                     OR !is_a_guest())
    1952                 {
    1953                     foreach($tpl_var as $cle=>$valeur)
    1954                     {
    1955                         // show only if the author is not the active user
    1956                         if ($tpl_var[$cle]['FILE_AUTHOR'] != $user['username']
    1957                             OR $this->my_config['author_vote_available']    // Allow author to vote (and also show his score)
    1958                             )
    1959                         {
    1960                             $tpl_var[$cle]['NAME'] = $tpl_var[$cle]['NAME'].'<br>&nbsp;'.l10n('thumbnail_global_note').' : '.$this->get_img_globalnote_user($tpl_var[$cle]['ID'], $concours['id'], $user['id'], $user['ipguest']);
    1961                         }
    1962                     }
    1963                 }
    1964             }
    1965         }
    1966     }
     2220
     2221        if (($page['section']) == 'categories' AND !empty($page['category']))
     2222        {
     2223               
     2224                $concours = array();
     2225
     2226                $query = '
     2227                        SELECT *
     2228                        FROM ' . CONCOURS_TABLE .'
     2229                        WHERE category =' . $page['category']['id']
     2230// comment to load prepared contests and opened contests                                . ' AND time_to_sec(TIMEDIFF(begin_date,now())) < 0'
     2231                        . ' AND time_to_sec(TIMEDIFF(now(), end_date)) < 0';
     2232
     2233                $result = pwg_query($query);
     2234                // DEBUG
     2235                if ($this->debug) echo $query."\n";
     2236                // END DEBUG
     2237                while ($row = pwg_db_fetch_array($result))
     2238                {                               
     2239                        if ((strtotime($row['begin_date'])-time() > 0) AND $this->my_config['mask_thumbnail'])
     2240                        {
     2241                        // disable picture if contest is prepared for all users
     2242                          $i=0;
     2243                          while ($i<count($tpl_var))
     2244                          {
     2245                                  array_splice($tpl_var, $i, 1);
     2246                                  array_splice($pictures, $i, 1);
     2247                          }     
     2248                        $concours = $row;
     2249                               
     2250                        }
     2251                        elseif (is_admin() AND $row['admin'])
     2252                                $concours = $row;
     2253                        elseif (!is_a_guest() AND !empty($row['groups']))
     2254                        {
     2255                                $authorized_groups = explode(',', $row['groups']);
     2256                                if (array_intersect($this->user_groups, $authorized_groups) == array())
     2257                                        continue;
     2258                                $concours = $row;
     2259                        }
     2260                       
     2261                        // If no group is parameter for that concours  ==> available for all registered users
     2262                        else
     2263                                $concours = $row;               
     2264                }
     2265               
     2266
     2267                if (count($concours))
     2268                {               
     2269                        // Add timeline for all users.
     2270                        if (strtotime($concours['begin_date'])-time() > 0)
     2271                        {
     2272                                $template->assign('begin_concours_min', (strtotime($concours['begin_date'])-time()));
     2273                                $template->assign('deadline_type',$this->my_config['concours_deadline']);
     2274                                $template->assign('begin_concours', $concours['begin_date']);
     2275                        }
     2276                        else
     2277                        {
     2278                                // Check if user is guest and if guest is allowed
     2279                                if ((is_a_guest() AND $concours['guest'])
     2280                                        OR (is_admin() AND $concours['admin'])
     2281                                        OR !is_a_guest())
     2282                                {
     2283                         
     2284                                        // check if contest is concours not already open (date begin > now)
     2285
     2286                               
     2287                                       
     2288                                                foreach($tpl_var as $cle=>$valeur)
     2289                                                {
     2290                                                        // show only if the author is not the active user
     2291
     2292                                                        $AuthorArray = strip_punctuation2($tpl_var[$cle]['author']);
     2293                                                        $AddedByID = ($tpl_var[$cle]['added_by']);
     2294                                                        $UserArray = strip_punctuation2($user['username']);
     2295
     2296                                                        if ($this->my_config['author_vote_available']  == 0 // Allow to vote for all photos (and also show his score)
     2297                                                            OR ($this->my_config['author_vote_available'] == 1 // check between author and user
     2298                                                                AND (empty($AuthorArray) OR count(array_intersect($AuthorArray, $UserArray)) != count ($AuthorArray))  // Author is the same as user name
     2299                                                                )       
     2300                                                            OR ($this->my_config['author_vote_available'] == 2 // check between author and user
     2301                                                                AND (!isset($AddedByID) OR ($user['id'] != $AddedByID))  // Addedby is the same as user name
     2302                                                                )
     2303                                                            OR ($this->my_config['author_vote_available'] == 3 // check between author OR addedby and user
     2304                                                                AND (!isset($AddedByID) OR ($user['id'] != $AddedByID))  // Addedby is the same as user name
     2305                                                                AND (empty($AuthorArray) OR count(array_intersect($AuthorArray, $UserArray)) != count ($AuthorArray))  // Author is the same as user name
     2306                                                                )
     2307                                                           )
     2308
     2309                                                        {
     2310                                                                if ($this->my_config['thumb_note'])      // display thumbnail note only if option is activated
     2311                                                                {
     2312                                                                        $user_global_note = $this->get_img_globalnote_user($tpl_var[$cle]['id'], $concours['id'], $user['id'], $user['ipguest']);
     2313                                                                        $tpl_var[$cle]['NAME'] = $tpl_var[$cle]['NAME'].'('.l10n('thumbnail_global_note').': '.($user_global_note >= 0 ? $user_global_note : 0).')';
     2314                                                                }
     2315                                                        }
     2316                                                }
     2317                                               
     2318                                                // Add deadline on description page     
     2319                                                $template->assign('end_concours', $concours['end_date']);
     2320                                                $template->assign('end_concours_min', (strtotime($concours['end_date'])-time()));
     2321                                                $template->assign('deadline_type',$this->my_config['concours_deadline']);
     2322                                                if (isset($this->my_config['active_global_score_page']) AND $this->my_config['active_global_score_page'])       // only if option is activated
     2323                                                        $template->assign('global_vote_link', PHPWG_ROOT_PATH . 'index.php?/concours_vote/'.$concours['id']);
     2324                                }
     2325                               
     2326                        }
     2327                        $template->assign('IMG_URL', CONCOURS_IMG_PATH);
     2328                       
     2329                        $template->set_filenames(array('concours_description' =>  CONCOURS_ROOT.'/template/concours_description.tpl'));
     2330                        $template->concat('CONTENT_DESCRIPTION', $template->parse('concours_description', true));
     2331
     2332                }
     2333        }
     2334       
    19672335    return $tpl_var;
    19682336  }
    1969        
     2337
     2338        function concours_stuffs_module($modules)
     2339        {
     2340                array_push($modules, array(
     2341                'path' => CONCOURS_PATH.'stuffs_module/',
     2342                'name' => l10n('concours_stuffs_name'),
     2343                'description' => l10n('concours_stuffs_description'),
     2344                )
     2345                );
     2346                return $modules;
     2347        }       
     2348       
     2349       
     2350       
     2351        function add_contest_desc($tpl_var)
     2352        {       
     2353                global $page, $user, $template;
     2354        // TO DO : Add contest information on subcategory
     2355/*     
     2356        echo "ICI<br>";
     2357        echo "tpl_VAR".(is_array($tpl_var) ? "OK" : $tpl_var)."<br>";
     2358        foreach ($tpl_var as $id=>$val)
     2359        {
     2360               
     2361                if (is_array($val))
     2362                        foreach ($val as $id2=>$val2)
     2363                        if (is_array($val2))
     2364                                foreach ($val2 as $id3=>$val3)
     2365                                if (is_array($val3))
     2366                                        foreach ($val3  as $id4=>$val4)
     2367                                                echo "tpl_var[".$id."][".$id2."][".$id3."][".$id4."]=".$val4."<br>\n";
     2368                                else
     2369                                        echo "tpl_var[".$id."][".$id2."][".$id3."]=".$val3."<br>\n";
     2370                        else
     2371                                echo "tpl_var[".$id."][".$id2."]=".$val2."<br>\n";
     2372                else
     2373                        echo "tpl_var[".$id."]=".$val."<br>\n";
     2374        }
     2375          $i=0;
     2376          while ($i<count($tpl_var))
     2377          {
     2378                $tpl_var[$i]['NAME'] = "TEST TEST";
     2379                $i++;
     2380               
     2381          }
     2382       
     2383                $template->set_filenames(array('concours_description' =>  CONCOURS_ROOT.'/template/concours_description-cat.tpl'));
     2384                $template->concat('DESCRIPTION', $template->parse('concours_description', true));
     2385               
     2386                return  $tpl_var;
     2387*/             
     2388        }
     2389
     2390
     2391
     2392
     2393        // Check if a concours is present (prepared or actived) for the category and user group
     2394        function concours_present_cat($category = NULL)
     2395        {
     2396                global $page;
     2397                $concours_present = false;
     2398                if (!isset($category))  // param not present
     2399                {
     2400                        if (($page['section']) == 'categories' AND !empty($page['category']))
     2401                                        $category = $page['category']['id'] ;
     2402                }
     2403
     2404                if (isset($category))
     2405                {
     2406                        $query = '
     2407                                SELECT *
     2408                                FROM ' . CONCOURS_TABLE .'
     2409                                WHERE category =' . $category . '
     2410                AND time_to_sec(TIMEDIFF(now(), end_date)) < 0
     2411                                ';
     2412
     2413                        $result = pwg_query($query);
     2414                        while ($row = pwg_db_fetch_array($result))
     2415                                return true;
     2416                }
     2417                return false;
     2418        }
     2419
     2420
     2421        // Disable Metatdata to picture for a prepared or active concours
     2422        function disable_meta_to_picture()
     2423        {
     2424                if (isset($this->my_config['mask_meta']) && $this->my_config['mask_meta'] == true)
     2425                {
     2426                        if ($this->concours_present_cat())
     2427                                pwg_set_session_var('show_metadata', 0 );
     2428                }
     2429        }
     2430
    19702431       
    19712432}
    19722433
     2434function strip_punctuation($string) {
     2435    $string = strtolower($string);      // lower case
     2436    $string = preg_replace("/\p{P}+/", "", $string); // remove punctuation
     2437    $string = preg_replace("/\p{Nd}+/", "", $string); // remove numeric
     2438    $string = preg_replace("/[\p{Z}\t\r\n\v\f]+/", "", $string); // remove spaces
     2439    $string = str_replace(" +", "", $string);   // remove space
     2440//echo "stripPunct=".$string."!\n";
     2441    return $string;
     2442}
     2443
     2444// return array
     2445function strip_punctuation2($string) {
     2446    $string = strtolower($string);      // lower case
     2447    $string = preg_replace("/\p{P}+/", " ", $string); // remove punctuation by space
     2448    $string = preg_replace("/\p{Nd}+/", "", $string); // remove numeric
     2449    $string = preg_replace("/[\p{Z}\t\r\n\v\f]+/", " ", $string); // remove spaces by spaces (only 1)
     2450//    $string = str_replace(" +", "", $string); // remove space
     2451        $stringArray = $returnValue = preg_split('/\\W/', $string, -1, PREG_SPLIT_NO_EMPTY);
     2452
     2453    return $stringArray;
     2454}
     2455
     2456
     2457
    19732458?>
  • extensions/ConcoursPhoto/include/default_values.inc.php

    r8946 r32457  
    77$concours_default_values['active_menubar'] = true; // Activate menubar view
    88$concours_default_values['nbconcours_menubar'] = 3;     // Nb of last published concours show on concours menubar
    9 $concours_default_values['mask_author'] =   true; // Mask author for prepared concours
     9$concours_default_values['mask_author'] =   true; // Mask author for prepared concours or active concours
     10$concours_default_values['mask_meta'] =     false; // Mask meta for prepared concours or active concours
    1011$concours_default_values['thumb_note'] = true; // Display global note (for user) under thumbnail
    1112$concours_default_values['check_exaequo'] = false; // Check exaequo and display the same rank
    12 $concours_default_values['author_vote_available'] = false; // Dont allow author to vote for his own photo
     13//$concours_default_values['author_vote_available'] = false; // Dont allow author to vote for his own photo
     14$concours_default_values['author_vote_available'] = 0; // 0: Disable check between curent user and Author/AddedBy info for a photo ==> All user cound vote 
     15                                                                                                           // 1: Check between "Author" (of photo) and "User"
     16                                                                                                           // 2: Check between "AddedBy" (of photo) and "User"
     17                                                                                                           // 3: Check between "Author" OR "AddedBy" and "User"
     18
    1319$concours_default_values['concours_change_score']   = true; // Allow a user to change his score afer a validation
     20$concours_default_values['concours_deadline']   = 2; // 0: dont show deadline; 1: deadline version 1; 2: deadline version 2
     21$concours_default_values['mask_thumbnail']   = true; // Mask thumbnail on category page before the contest start
     22$concours_default_values['nb_concours_page']   = 10; // nb of contest by page (on admin page)
     23$concours_default_values['active_global_score_page']   = false; // activate global page for vote/score (all image in 1 page)
     24$concours_default_values['score_mode']   = 1; // score mode. 0: text box / 1: stars
     25$concours_default_values['text_overlay']   = "Vote déjà enregistré <br>"; // text to display when the score is already present and the option concours_change_score is false (allow the user to change the score after vote)
    1426
    1527?>
Note: See TracChangeset for help on using the changeset viewer.