2 rcs_id('$Id: UserRatings.php,v 1.5 2007-06-02 18:25:16 rurban Exp $');
4 Copyright 2004 Dan Frankowski
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 require_once('lib/PageList.php');
27 require_once('lib/wikilens/Buddy.php');
28 require_once("lib/wikilens/PageListColumns.php");
31 require_once("lib/WikiUserNew.php");
33 require_once("lib/WikiUser.php");
36 * Show a user's ratings in a table, using PageList.
38 * <?plugin UserRatings ?>
40 * This only works with the "wikilens" theme.
43 class WikiPlugin_UserRatings
47 return _("UserRatings");
50 function getDescription () {
51 return _("List the user's ratings.");
54 function getVersion() {
55 return preg_replace("/[Revision: $]/", '',
59 function getDefaultArguments() {
60 return array('dimension' => '0',
61 // array of userids to display ratings for; null/empty for
64 // array of pageids to display ratings for; null for all
65 // of current active user's ratings
67 // a category to display ratings for; null for no category;
68 // has higher precedence than pageids
70 'pagename' => '[pagename]', // hackish
72 'limit' => 0, // limit of <=0 is show-all
76 // rating columns are added later
77 'info' => 'pagename');
78 // getting a bit crowded with the buddies...
79 // 'info' => 'hits,pagename,author,ratingwidget');
81 // info arg allows multiple columns
82 // info=mtime,hits,summary,version,author,locked,minor
83 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
85 function run($dbi, $argstr, &$request, $basepage) {
86 extract($this->getArgs($argstr, $request));
88 // XXX: fix to reflect multi-user ratings?
89 $caption = _("Displaying %d ratings:");
91 $active_user = $request->getUser();
92 $active_userid = $active_user->_userid;
94 // check for request to display a category's ratings
95 if(isset($category) && is_string($category) && strlen($category))
98 $category_page = $dbi->getPage($category);
99 $iter = $category_page->getLinks();
100 while($item = $iter->next())
102 array_push($pageids, $item->getName());
104 // XXX: is there a way to retrieve the preferred plural
105 // representation of a category name?
106 // XXX: should the category text be a link? can it be one easily?
108 $caption = sprintf(_("Showing all %ss (%%d):"), $category);
110 // if null is passed as the pageids argument and no category was passed,
111 // show active user's ratings
112 elseif(!isset($pageids) || !is_array($pageids))
114 // XXX: need support for sorted ratings
115 // bug: pages excluded from the PageList via the "exclude" argument
116 // count toward the limit!
119 $active_user_ratings_user = & RatingsUserFactory::getUser($active_user->getId());
120 $current_user_ratings = $active_user_ratings_user->get_ratings();
123 //we're on a user's homepage, get *their* ratings
124 $this_page_user = & RatingsUserFactory::getUser($userPage);
125 $caption = _("Here are $userPage" . "'s %d page ratings:");
126 $ratings = $this_page_user->get_ratings();
128 $caption = _("Here are your %d page ratings:");
129 $ratings = $current_user_ratings;
134 foreach($ratings as $pagename => $page_ratings)
136 // limit is currently only honored for "own" ratings
137 if($limit > 0 && $i >= $limit)
141 if(isset($page_ratings[$dimension]))
143 array_push($pageids, $pagename);
147 // $caption = _("Here are your %d page ratings:");
148 //make $ratings the user's ratings again if it had been treated as the current page
150 $ratings = $current_user_ratings;
153 // if userids is null or empty, fill it with just the active user
154 if(!isset($userids) || !is_array($userids) || !count($userids))
156 // TKL: moved getBuddies call inside if statement because it was
157 // causing the userids[] parameter to be ignored
158 if(is_string($active_userid) && strlen($active_userid) && $active_user->isSignedIn() && !$userPage) {
159 if (isset($category_page)){
160 $userids = getBuddies($active_userid, $dbi, $category_page->getName());
162 $userids = getBuddies($active_userid, $dbi);
167 //we're on a user page, show that user's ratings as the only column
169 array_push($userids, $userPage);
174 // XXX: this wipes out the category caption...
175 // $caption = _("You must be logged in to view ratings.");
179 // find out which users we should show ratings for
181 // users allowed in the prediction calculation
182 $allowed_users = array();
183 // users actually allowed to be shown to the user
184 $allowed_users_toshow = array();
185 foreach($userids as $userid)
187 $user = & RatingsUserFactory::getUser($userid);
188 if($user->allow_view_ratings($active_user))
190 array_push($allowed_users_toshow, $user);
192 // all users should be allowed in calculation
193 array_push($allowed_users, $user);
194 // This line ensures $user is not a reference type after this loop
195 // If it is a reference type, that can produce very unexpected behavior!
198 // if no buddies, use allusers in prediction calculation
200 if (count($userids) == 0 || $userPage){
201 $allowed_users = array();
202 //$people_iter = $dbi->get_users_rated();
203 $people_dbi = RatingsDb::getTheRatingsDb();
204 $people_iter = $people_dbi->sql_get_users_rated();
205 while ($people_array = $people_iter->next()) {
206 if (isset($people_array['pagename'])) {
207 $userid = $people_array['pagename'];
208 $user = & RatingsUserFactory::getUser($userid);
209 array_push($allowed_users, $user);
216 $columns = $info ? explode(",", $info) : array();
217 // build our table...
218 $pagelist = new PageList($columns, $exclude, array('dimension' => $dimension, 'users' => $allowed_users_toshow));
221 //$preds = new _PageList_Column_prediction('prediction', _("Pred"), 'right', $dimension, $allowed_users);
222 $preds = array('_PageList_column_prediction','custom:prediction', _("Pred"),'right',' ' , $allowed_users);
223 $pagelist->addColumnObject($preds);
225 //$widget = new _PageList_Column_ratingwidget('ratingwidget', _("Rate"), 'left', $dimension);
226 $widget = array('_PageList_column_ratingwidget','custom:ratingwidget', _("Rate"), 'center');
227 $pagelist->addColumnObject($widget);
229 $noRatingUsers = array();
231 foreach($allowed_users_toshow as $idx => $user) {
232 // For proper caching behavior, get a ref, don't user $user
233 $u = & $allowed_users_toshow[$idx];
234 //$col = & new _PageList_Column_ratingvalue('ratingvalue', $u->getId(), 'right', $dimension, $u);
235 $col = array('_PageList_Column_ratingvalue','custom:ratingvalue', $u->getId(), 'right',' ' ,$u);
236 $pagelist->addColumnObject($col);
241 // add rows -- each row represents an item (page)
242 foreach($pageids as $pagename) {
243 // addPage can deal with cases where it is passed a string
244 $pagelist->addPage($pagename);
248 $pagelist->setCaption(_($caption));
261 // c-hanging-comment-ender-p: nil
262 // indent-tabs-mode: nil