Changeset 11827
- Timestamp:
- Jul 25, 2011, 8:04:50 PM (13 years ago)
- Location:
- trunk
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/admin/configuration.php
r11589 r11827 110 110 'date_creation' => l10n('Creation date'), 111 111 'date_available' => l10n('Post date'), 112 'average_rate' => l10n(' Average rate'),112 'average_rate' => l10n('Rating score'), 113 113 'hit' => l10n('Most visited'), 114 114 'id' => 'Id', -
trunk/admin/element_set_ranks.php
r10513 r11827 274 274 'date_creation' => l10n('Creation date'), 275 275 'date_available' => l10n('Post date'), 276 'average_rate' => l10n(' Average rate'),276 'average_rate' => l10n('Rate score'), 277 277 'hit' => l10n('Most visited'), 278 278 'file' => l10n('File name'), -
trunk/admin/include/functions.php
r11753 r11827 1052 1052 1053 1053 /** 1054 * update images.average_rate field1055 * param int $element_id optional, otherwise applies to all1056 * @return void1057 */1058 function update_average_rate( $element_id=-1 )1059 {1060 $query = '1061 SELECT element_id,1062 ROUND(AVG(rate),2) AS average_rate1063 FROM '.RATE_TABLE;1064 if ( $element_id != -1 )1065 {1066 $query .= ' WHERE element_id=' . $element_id;1067 }1068 $query .= ' GROUP BY element_id;';1069 1070 $result = pwg_query($query);1071 1072 $datas = array();1073 1074 while ($row = pwg_db_fetch_assoc($result))1075 {1076 array_push(1077 $datas,1078 array(1079 'id' => $row['element_id'],1080 'average_rate' => $row['average_rate']1081 )1082 );1083 }1084 1085 mass_updates(1086 IMAGES_TABLE,1087 array(1088 'primary' => array('id'),1089 'update' => array('average_rate')1090 ),1091 $datas1092 );1093 1094 $query='1095 SELECT id FROM '.IMAGES_TABLE .'1096 LEFT JOIN '.RATE_TABLE.' ON id=element_id1097 WHERE element_id IS NULL AND average_rate IS NOT NULL';1098 if ( $element_id != -1 )1099 {1100 $query .= ' AND id=' . $element_id;1101 }1102 $to_update = array_from_query( $query, 'id');1103 1104 if ( !empty($to_update) )1105 {1106 $query='1107 UPDATE '.IMAGES_TABLE .'1108 SET average_rate=NULL1109 WHERE id IN (' . implode(',',$to_update) . ')';1110 pwg_query($query);1111 }1112 }1113 1114 /**1115 1054 * change the parent category of the given categories. The categories are 1116 1055 * supposed virtual. -
trunk/admin/maintenance.php
r8765 r11827 59 59 { 60 60 update_path(); 61 update_average_rate(); 61 include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php'); 62 update_rating_score(); 62 63 break; 63 64 } -
trunk/admin/rating.php
r8728 r11827 86 86 ;'; 87 87 pwg_query($query); 88 update_ average_rate( $vars['e'] );88 update_rating_score( $vars['e'] ); 89 89 } 90 90 … … 101 101 102 102 103 $query = 'SELECT COUNT(DISTINCT(i.id)) 104 FROM '.RATE_TABLE.' AS r, '.IMAGES_TABLE.' AS i 105 WHERE r.element_id=i.id'. $page['user_filter'] . 106 ';'; 103 $query = 'SELECT COUNT(DISTINCT(r.element_id)) 104 FROM '.RATE_TABLE.' AS r 105 WHERE 1=1'. $page['user_filter']; 107 106 list($nb_images) = pwg_db_fetch_row(pwg_query($query)); 108 107 … … 132 131 $available_order_by= array( 133 132 array(l10n('Rate date'), 'recently_rated DESC'), 134 array(l10n('Average rate'), 'average_rate DESC'), 133 array(l10n('Rating score'), 'score DESC'), 134 array(l10n('Average rate'), 'avg_rates DESC'), 135 135 array(l10n('Number of rates'), 'nb_rates DESC'), 136 136 array(l10n('Sum of rates'), 'sum_rates DESC'), … … 165 165 i.file, 166 166 i.tn_ext, 167 i.average_rate ,167 i.average_rate AS score, 168 168 MAX(r.date) AS recently_rated, 169 ROUND(AVG(r.rate),2) AS avg_rates, 169 170 COUNT(r.rate) AS nb_rates, 170 171 SUM(r.rate) AS sum_rates … … 208 209 'U_THUMB' => $thumbnail_src, 209 210 'U_URL' => $image_url, 210 'AVG_RATE' => $image['average_rate'], 211 'SCORE_RATE' => $image['score'], 212 'AVG_RATE' => $image['avg_rates'], 211 213 'SUM_RATE' => $image['sum_rates'], 212 214 'NB_RATES' => (int)$image['nb_rates'], -
trunk/admin/themes/default/template/configuration.tpl
r11589 r11827 416 416 <li> 417 417 <label> 418 <span class="property">{' Average rate'|@translate}</span>418 <span class="property">{'Rating score'|@translate}</span> 419 419 <input type="checkbox" name="picture_informations[average_rate]" {if ($display.picture_informations.average_rate)}checked="checked"{/if}> 420 420 </label> -
trunk/admin/themes/default/template/rating.tpl
r8682 r11827 38 38 <td>{'File'|@translate}</td> 39 39 <td>{'Number of rates'|@translate}</td> 40 <td>{'Rating score'|@translate}</td> 40 41 <td>{'Average rate'|@translate}</td> 41 42 <td>{'Sum of rates'|@translate}</td> … … 49 50 <td rowspan="{$image.NB_RATES_TOTAL+1}"><a href="{$image.U_URL}"><img src="{$image.U_THUMB}" alt="{$image.FILE}" title="{$image.FILE}"></a></td> 50 51 <td rowspan="{$image.NB_RATES_TOTAL+1}"><strong>{$image.NB_RATES}/{$image.NB_RATES_TOTAL}</strong></td> 52 <td rowspan="{$image.NB_RATES_TOTAL+1}"><strong>{$image.SCORE_RATE}</strong></td> 51 53 <td rowspan="{$image.NB_RATES_TOTAL+1}"><strong>{$image.AVG_RATE}</strong></td> 52 54 <td rowspan="{$image.NB_RATES_TOTAL+1}" style="border-right: 1px solid;" ><strong>{$image.SUM_RATE}</strong></td> -
trunk/include/functions_category.inc.php
r11512 r11827 291 291 array( 292 292 array(l10n('Default'), '', true), 293 array(l10n(' Average rate'), 'average_rate DESC', $conf['rate']),293 array(l10n('Rating score'), 'average_rate DESC', $conf['rate']), 294 294 array(l10n('Most visited'), 'hit DESC', true), 295 295 array(l10n('Creation date'), 'date_creation DESC', true), -
trunk/include/functions_rate.inc.php
r8728 r11827 117 117 pwg_query($query); 118 118 119 // update of images.average_rate field 119 return update_rating_score($image_id); 120 } 121 122 123 /* update images.average_rate field 124 * we use a bayesian average (http://en.wikipedia.org/wiki/Bayesian_average) with 125 C = average number of rates per item 126 m = global average rate (all rates) 127 128 * param int $element_id optional, otherwise applies to all 129 * @return array(average_rate, count) if element_id is specified 130 */ 131 function update_rating_score($element_id = false) 132 { 120 133 $query = ' 121 SELECT COUNT(rate) AS count 122 , ROUND(AVG(rate),2) AS average 123 FROM '.RATE_TABLE.' 124 WHERE element_id = '.$image_id.' 125 ;'; 126 $row = pwg_db_fetch_assoc(pwg_query($query)); 127 $query = ' 128 UPDATE '.IMAGES_TABLE.' 129 SET average_rate = '.$row['average'].' 130 WHERE id = '.$image_id.' 131 ;'; 132 pwg_query($query); 133 return $row; 134 SELECT element_id, 135 COUNT(rate) AS rcount, 136 SUM(rate) AS rsum 137 FROM '.RATE_TABLE.' 138 GROUP by element_id'; 139 140 $all_rates_count = 0; 141 $all_rates_avg = 0; 142 $item_ratecount_avg = 0; 143 $by_item = array(); 144 145 $result = pwg_query($query); 146 while ($row = pwg_db_fetch_assoc($result)) 147 { 148 $all_rates_count += $row['rcount']; 149 $all_rates_avg += $row['rsum']; 150 $by_item[$row['element_id']] = $row; 151 } 152 153 $all_rates_avg /= $all_rates_count; 154 $item_ratecount_avg = $all_rates_count / count($by_item); 155 156 $updates = array(); 157 foreach ($by_item as $id => $rate_summary ) 158 { 159 $score = ( $item_ratecount_avg * $all_rates_avg + $rate_summary['rsum'] ) / ($item_ratecount_avg + $rate_summary['rcount']); 160 $score = round($score,2); 161 if ($id==$element_id) 162 { 163 $return = array( 164 'score' => $score, 165 'average' => round($rate_summary['rsum'] / $rate_summary['rcount'], 2), 166 'count' => $rate_summary['rcount'], 167 ); 168 } 169 $updates[] = array( 'id'=>$id, 'average_rate'=>$score ); 170 } 171 mass_updates( 172 IMAGES_TABLE, 173 array( 174 'primary' => array('id'), 175 'update' => array('average_rate') 176 ), 177 $updates 178 ); 179 180 //set to null all items with no rate 181 if ( !isset($by_item[$element_id]) ) 182 { 183 $query=' 184 SELECT id FROM '.IMAGES_TABLE .' 185 LEFT JOIN '.RATE_TABLE.' ON id=element_id 186 WHERE element_id IS NULL AND average_rate IS NOT NULL'; 187 188 $to_update = array_from_query( $query, 'id'); 189 190 if ( !empty($to_update) ) 191 { 192 $query=' 193 UPDATE '.IMAGES_TABLE .' 194 SET average_rate=NULL 195 WHERE id IN (' . implode(',',$to_update) . ')'; 196 pwg_query($query); 197 } 198 } 199 200 return isset($return) ? $return : array('score'=>null, 'average'=>null, 'count'=>0 ); 134 201 } 135 202 -
trunk/include/picture_rate.inc.php
r8728 r11827 29 29 if ($conf['rate']) 30 30 { 31 if ( NULL != $picture['current']['average_rate'] ) 31 $rate_summary = array( 'count'=>0, 'score'=>$picture['current']['average_rate'], 'average'=>null ); 32 if ( NULL != $rate_summary['score'] ) 32 33 { 33 34 $query = ' … … 37 38 WHERE element_id = '.$picture['current']['id'].' 38 39 ;'; 39 $row = pwg_db_fetch_assoc(pwg_query($query));40 list($rate_summary['count'], $rate_summary['average']) = pwg_db_fetch_row(pwg_query($query)); 40 41 } 41 else 42 { // avg rate null -> no rate -> no need to query db 43 $row = array( 'count'=>0, 'average'=>NULL ); 44 } 45 $template->assign('rate_summary', $row); 42 $template->assign('rate_summary', $rate_summary); 46 43 47 44 $user_rate = null; 48 45 if ($conf['rate_anonymous'] or is_autorize_status(ACCESS_CLASSIC) ) 49 46 { 50 if ($r ow['count']>0)47 if ($rate_summary['count']>0) 51 48 { 52 49 $query = 'SELECT rate -
trunk/include/ws_functions.inc.php
r11756 r11827 782 782 } 783 783 //------------------------------------------------------------- related rates 784 $query = ' 784 $rating = array('score'=>$image_row['average_rate'], 'count'=>0, 'average'=>null); 785 if (isset($rating['score'])) 786 { 787 $query = ' 785 788 SELECT COUNT(rate) AS count 786 789 , ROUND(AVG(rate),2) AS average … … 788 791 WHERE element_id = '.$image_row['id'].' 789 792 ;'; 790 $rating = pwg_db_fetch_assoc(pwg_query($query)); 791 $rating['count'] = (int)$rating['count']; 793 $row = pwg_db_fetch_assoc(pwg_query($query)); 794 $rating['score'] = (float)$rating['score']; 795 $rating['average'] = (float)$row['average']; 796 $rating['count'] = (int)$row['count']; 797 } 792 798 793 799 //---------------------------------------------------------- related comments -
trunk/themes/default/js/rating.js
r7957 r11827 73 73 { 74 74 var t = gRatingOptions.ratingSummaryText; 75 var args =[result. average, result.count], idx = 0, rexp = new RegExp( /%\.?\d*[sdf]/ );75 var args =[result.score, result.count, result.average], idx = 0, rexp = new RegExp( /%\.?\d*[sdf]/ ); 76 76 while (idx<args.length) t=t.replace(rexp, args[idx++]); 77 77 gRatingOptions.ratingSummaryElement.innerHTML = t; -
trunk/themes/default/template/picture.tpl
r11062 r11827 197 197 {if $display_info.average_rate and isset($rate_summary)} 198 198 <tr id="Average"> 199 <td class="label">{' Average rate'|@translate}</td>199 <td class="label">{'Rating'|@translate}</td> 200 200 <td class="value" id="ratingSummary"> 201 201 {if $rate_summary.count} 202 202 {assign var='rate_text' value='%.2f (rated %d times)'|@translate} 203 {$pwg->sprintf($rate_text, $rate_summary. average, $rate_summary.count)}203 {$pwg->sprintf($rate_text, $rate_summary.score, $rate_summary.count, $rate_summary.average)} 204 204 {else} 205 205 {'no rate'|@translate}
Note: See TracChangeset
for help on using the changeset viewer.