From 2f87684e097c087b18b6630878e5545c5c37699e Mon Sep 17 00:00:00 2001 From: rvelices Date: Mon, 14 Nov 2011 20:10:37 +0000 Subject: feature 2486: Add an admin view for rates by user git-svn-id: http://piwigo.org/svn/trunk@12624 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/rating.php | 8 + admin/rating_user.php | 212 ++++++++++++++++++++++++++ admin/themes/default/template/rating_user.tpl | 76 +++++++++ include/calendar_base.class.php | 4 +- include/ws_functions.inc.php | 39 +++++ ws.php | 11 +- 6 files changed, 346 insertions(+), 4 deletions(-) create mode 100644 admin/rating_user.php create mode 100644 admin/themes/default/template/rating_user.tpl diff --git a/admin/rating.php b/admin/rating.php index 2f0959023..70dc10fc3 100644 --- a/admin/rating.php +++ b/admin/rating.php @@ -33,6 +33,14 @@ include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); // +-----------------------------------------------------------------------+ check_status(ACCESS_ADMINISTRATOR); + +include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php'); +$tabsheet = new tabsheet(); +$tabsheet->add('rating', l10n('Photos'), get_root_url().'admin.php?page=rating'); +$tabsheet->add('rating_user', l10n('Users'), get_root_url().'admin.php?page=rating_user'); +$tabsheet->select('rating'); +$tabsheet->assign(); + // +-----------------------------------------------------------------------+ // | initialization | // +-----------------------------------------------------------------------+ diff --git a/admin/rating_user.php b/admin/rating_user.php new file mode 100644 index 000000000..9dcd28576 --- /dev/null +++ b/admin/rating_user.php @@ -0,0 +1,212 @@ +add('rating', l10n('Photos'), get_root_url().'admin.php?page=rating'); +$tabsheet->add('rating_user', l10n('Users'), get_root_url().'admin.php?page=rating_user'); +$tabsheet->select('rating_user'); +$tabsheet->assign(); + +$filter_min_rates = 2; +if (isset($_GET['f_min_rates'])) +{ + $filter_min_rates = (int)$_GET['f_min_rates']; +} + +global $conf; +$query = 'SELECT DISTINCT + u.'.$conf['user_fields']['id'].' AS id, + u.'.$conf['user_fields']['username'].' AS name, + ui.status + FROM '.USERS_TABLE.' AS u INNER JOIN '.USER_INFOS_TABLE.' AS ui + ON u.'.$conf['user_fields']['id'].' = ui.user_id'; + +$users_by_id = array(); +$result = pwg_query($query); +while ($row = pwg_db_fetch_assoc($result)) +{ + $user = array( + 'name' => $row['name'], + 'anon' => is_autorize_status(ACCESS_CLASSIC, $row['status']) ? false : true + ); + $users_by_id[(int)$row['id']] = $user; +} + +$by_rate_model = array(); +foreach($conf['rate_items'] as $rate) +{ + $by_rate_model[$rate] = array(); +} + + +$by_user_rating_model = array( 'rates' => $by_rate_model); + +$image_ids = array(); +$by_user_ratings = array(); +$query = ' +SELECT * FROM '.RATE_TABLE.' ORDER by date DESC'; +$result = pwg_query($query); +while ($row = pwg_db_fetch_assoc($result)) +{ + + if (!isset($users_by_id[$row['user_id']])) + { + $users_by_id[$row['user_id']] = array('name' => '???'.$row['user_id'], 'anon' => false); + } + $user = $users_by_id[$row['user_id']]; + if ($user['anon']) + { + $user_key = $user['name'].'('.$row['anonymous_id'].')'; + } + else + { + $user_key = $user['name']; + } + $rating = & $by_user_ratings[$user_key]; + if ( is_null($rating) ) + { + $rating = $by_user_rating_model; + $rating['uid'] = (int)$row['user_id']; + $rating['aid'] = $user['anon'] ? $row['anonymous_id'] : ''; + } + $rating['rates'][$row['rate']][] = array( + 'id' => $row['element_id'], + 'date' => $row['date'], + ); + $image_ids[$row['element_id']] = 1; + unset($rating); +} + + + +$image_urls = array(); +if (count($image_ids) > 0 ) +{ + $query = 'SELECT id, name, file, path, tn_ext + FROM '.IMAGES_TABLE.' + WHERE id IN ('.implode(',', array_keys($image_ids)).')'; + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) + { + $image_urls[ $row['id'] ] = array( + 'tn' => get_thumbnail_url($row), + 'page' => make_picture_url( array('image_id'=>$row['id'], 'image_file'=>$row['file']) ), + ); + } +} + +foreach($by_user_ratings as $id => &$rating) +{ + $c=0; $s=0; $ss=0; + foreach($rating['rates'] as $rate => $rates) + { + $ct = count($rates); + $c += $ct; + $s += $ct * $rate; + $ss += $ct * $rate * $rate; + } + + $var = ($ss - $s*$s/$c)/$c; + $rating += array( + 'id' => $id, + 'count' => $c, + 'avg' => $s/$c, + 'std' => sqrt($var), + 'cv' => $s==0 ? -1 : sqrt($var)/($s/$c), // http://en.wikipedia.org/wiki/Coefficient_of_variation + ); +} +unset($rating); + +// filter +foreach($by_user_ratings as $id => $rating) +{ + if ($rating['count'] <= $filter_min_rates) + { + unset($by_user_ratings[$id]); + } +} + + +function avg_compare($a, $b) +{ + $d = $a['avg'] - $b['avg']; + return ($d==0) ? 0 : ($d<0 ? -1 : 1); +} + +function count_compare($a, $b) +{ + $d = $a['count'] - $b['count']; + return ($d==0) ? 0 : ($d<0 ? -1 : 1); +} + +function std_compare($a, $b) +{ + $d = $a['std'] - $b['std']; + return ($d==0) ? 0 : ($d<0 ? -1 : 1); +} + +function cv_compare($a, $b) +{ + $d = $a['cv'] - $b['cv']; + return ($d==0) ? 0 : ($d<0 ? -1 : 1); +} + + +$order_by_index=3; +if (isset($_GET['order_by']) and is_numeric($_GET['order_by'])) +{ + $order_by_index = $_GET['order_by']; +} + +$available_order_by= array( + array(l10n('Average rate'), 'avg_compare'), + array(l10n('Number of rates'), 'count_compare'), + array('StDev', 'std_compare'), + array('Coeff of Variation', 'cv_compare'), + ); + +for ($i=0; $iappend( + 'order_by_options', + $available_order_by[$i][0] + ); +} +$template->assign('order_by_options_selected', array($order_by_index) ); + +$x = uasort($by_user_ratings, $available_order_by[$order_by_index][1] ); + +$template->assign( array( + 'F_ACTION' => get_root_url().'admin.php', + 'F_MIN_RATES' => $filter_min_rates, + 'available_rates' => $conf['rate_items'], + 'ratings' => $by_user_ratings, + 'image_urls' => $image_urls, + ) ); +$template->set_filename('rating', 'rating_user.tpl'); +$template->assign_var_from_handle('ADMIN_CONTENT', 'rating'); + +?> \ No newline at end of file diff --git a/admin/themes/default/template/rating_user.tpl b/admin/themes/default/template/rating_user.tpl new file mode 100644 index 000000000..b69815528 --- /dev/null +++ b/admin/themes/default/template/rating_user.tpl @@ -0,0 +1,76 @@ +
+
+ + + + + +
+
+ +{combine_script id='core.scripts' load='async' path='themes/default/js/scripts.js'} +{footer_script}{literal} +function del(elt,uid,aid) +{ + if (!confirm('{'Are you sure?'|@translate|@escape:'javascript'}')) + return false; + var tr = elt; + while ( tr.nodeName != "TR") tr = tr.parentNode; + tr = jQuery(tr).fadeTo(1000, 0.4); + var ws = new PwgWS({/literal}'{$ROOT_URL|@escape:javascript}'{literal}); + ws.callService( + 'pwg.rates.delete', {user_id:uid, anonymous_id:aid}, + { + method: 'POST', + onFailure: function(num, text) { tr.stop(); tr.fadeTo(0,1); alert(num + " " + text); }, + onSuccess: function(result) { if (result) {tr.remove();} else alert(result); } + } + ); + return false; +} +{/literal}{/footer_script} + + + + + + + +{foreach from=$available_rates item=rate} + +{/foreach} + + +{foreach from=$ratings item=rating key=user} + + + + + + + {foreach from=$rating.rates item=rates key=rate} + + {/foreach} + + +{/foreach} +
{'Username'|@translate}{'Number of rates'|@translate}{'Average rate'|@translate}StDevCV{$rate}
{$user}{$rating.count}{$rating.avg|@number_format:2}{$rating.std|@number_format:3}{$rating.cv|@number_format:3}{if !empty($rates)} + {capture assign=rate_over}{foreach from=$rates item=rate_arr}thumb-{$rate_arr.id} + {/foreach}{/capture} + {$rates|@count} + {/if}[{'Delete'|@translate}]
+ +{combine_script id='jquery.cluetip' load='footer' require='jquery' path='themes/default/js/plugins/jquery.cluetip.js'} +{footer_script require='jquery.cluetip'} +{literal} + jQuery('.cluetip').cluetip({ + width: 350, splitTitle: '|' + }); +{/literal} +{/footer_script} \ No newline at end of file diff --git a/include/calendar_base.class.php b/include/calendar_base.class.php index a64e20fe9..15f20f7f3 100644 --- a/include/calendar_base.class.php +++ b/include/calendar_base.class.php @@ -215,9 +215,7 @@ SELECT DISTINCT('.$this->calendar_levels[$level]['sql'].') as period, COUNT(DISTINCT id) as nb_images'. $this->inner_sql. $this->get_date_where($level).' - GROUP BY period - ORDER BY period ASC -;'; + GROUP BY period;'; $level_items = simple_hash_from_query($query, 'period', 'nb_images'); diff --git a/include/ws_functions.inc.php b/include/ws_functions.inc.php index 1ebb7502a..47d7d32f1 100644 --- a/include/ws_functions.inc.php +++ b/include/ws_functions.inc.php @@ -1883,6 +1883,45 @@ SELECT ); } +function ws_rates_delete($params, &$service) +{ + global $conf; + + if (!$service->isPost()) + { + return new PwgError(405, 'This method requires HTTP POST'); + } + + if (!is_admin()) + { + return new PwgError(401, 'Access denied'); + } + + $user_id = (int)$params['user_id']; + if ($user_id<=0) + { + return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid user_id'); + } + + $query = ' +DELETE FROM '.RATE_TABLE.' + WHERE user_id='.$user_id; + + if (!empty($params['anonymous_id'])) + { + $query .= ' AND anonymous_id=\''.$params['anonymous_id'].'\''; + } + + $changes = pwg_db_changes(pwg_query($query)); + if ($changes) + { + include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php'); + update_rating_score(); + } + return $changes; +} + + /** * perform a login (web service method) */ diff --git a/ws.php b/ws.php index a24cc47c4..d963a592f 100644 --- a/ws.php +++ b/ws.php @@ -154,7 +154,16 @@ function ws_addDefaultMethods( $arr ) ), 'sets the rank of a photo for a given album (POST method only, for admins)' ); - + + + $service->addMethod('pwg.rates.delete', 'ws_rates_delete', + array( + 'user_id' => array(), + 'anonymous_id' => array( 'default'=>'' ), + ), + 'deletes all rates for a user (POST method only, admins only)' + ); + $service->addMethod('pwg.session.getStatus', 'ws_session_getStatus', null, '' ); $service->addMethod('pwg.session.login', 'ws_session_login', array('username', 'password'), -- cgit v1.2.3