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 along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 require_once 'lib/PageList.php';
24 require_once 'lib/wikilens/Buddy.php';
25 require_once 'lib/wikilens/PageListColumns.php';
28 require_once 'lib/WikiUserNew.php';
30 require_once 'lib/WikiUser.php';
33 * Show a user's ratings in a table, using PageList.
37 * This only works with the "wikilens" theme.
40 class WikiPlugin_UserRatings
44 return _("UserRatings");
47 function getDescription () {
48 return _("List the user's ratings.");
51 function getDefaultArguments() {
52 return array('dimension' => '0',
53 // array of userids to display ratings for; null/empty for
56 // array of pageids to display ratings for; null for all
57 // of current active user's ratings
59 // a category to display ratings for; null for no category;
60 // has higher precedence than pageids
62 'pagename' => '[pagename]', // hackish
64 'limit' => 0, // limit of <=0 is show-all
68 // rating columns are added later
69 'info' => 'pagename');
70 // getting a bit crowded with the buddies...
71 // 'info' => 'hits,pagename,author,ratingwidget');
73 // info arg allows multiple columns
74 // info=mtime,hits,summary,version,author,locked,minor
75 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
77 function run($dbi, $argstr, &$request, $basepage) {
78 extract($this->getArgs($argstr, $request));
80 // XXX: fix to reflect multi-user ratings?
81 $caption = _("Displaying %d ratings:");
83 $active_user = $request->getUser();
84 $active_userid = $active_user->_userid;
86 // check for request to display a category's ratings
87 if(isset($category) && is_string($category) && strlen($category))
90 $category_page = $dbi->getPage($category);
91 $iter = $category_page->getLinks();
92 while($item = $iter->next())
94 array_push($pageids, $item->getName());
96 // XXX: is there a way to retrieve the preferred plural
97 // representation of a category name?
98 // XXX: should the category text be a link? can it be one easily?
100 $caption = sprintf(_("Showing all %ss (%%d):"), $category);
102 // if null is passed as the pageids argument and no category was passed,
103 // show active user's ratings
104 elseif(!isset($pageids) || !is_array($pageids))
106 // XXX: need support for sorted ratings
107 // bug: pages excluded from the PageList via the "exclude" argument
108 // count toward the limit!
111 $active_user_ratings_user = & RatingsUserFactory::getUser($active_user->getId());
112 $current_user_ratings = $active_user_ratings_user->get_ratings();
115 //we're on a user's homepage, get *their* ratings
116 $this_page_user = & RatingsUserFactory::getUser($userPage);
117 $caption = _("Here are $userPage" . "'s %d page ratings:");
118 $ratings = $this_page_user->get_ratings();
120 $caption = _("Here are your %d page ratings:");
121 $ratings = $current_user_ratings;
125 foreach($ratings as $pagename => $page_ratings)
127 // limit is currently only honored for "own" ratings
128 if($limit > 0 && $i >= $limit)
132 if(isset($page_ratings[$dimension]))
134 array_push($pageids, $pagename);
138 // $caption = _("Here are your %d page ratings:");
139 //make $ratings the user's ratings again if it had been treated as the current page
141 $ratings = $current_user_ratings;
144 // if userids is null or empty, fill it with just the active user
145 if(!isset($userids) || !is_array($userids) || !count($userids))
147 // TKL: moved getBuddies call inside if statement because it was
148 // causing the userids[] parameter to be ignored
149 if(is_string($active_userid) && strlen($active_userid) && $active_user->isSignedIn() && !$userPage) {
150 if (isset($category_page)){
151 $userids = getBuddies($active_userid, $dbi, $category_page->getName());
153 $userids = getBuddies($active_userid, $dbi);
158 //we're on a user page, show that user's ratings as the only column
160 array_push($userids, $userPage);
165 // XXX: this wipes out the category caption...
166 // $caption = _("You must be logged in to view ratings.");
170 // find out which users we should show ratings for
172 // users allowed in the prediction calculation
173 $allowed_users = array();
174 // users actually allowed to be shown to the user
175 $allowed_users_toshow = array();
176 foreach($userids as $userid)
178 $user = & RatingsUserFactory::getUser($userid);
179 if($user->allow_view_ratings($active_user))
181 array_push($allowed_users_toshow, $user);
183 // all users should be allowed in calculation
184 array_push($allowed_users, $user);
185 // This line ensures $user is not a reference type after this loop
186 // If it is a reference type, that can produce very unexpected behavior!
189 // if no buddies, use allusers in prediction calculation
191 if (count($userids) == 0 || $userPage){
192 $allowed_users = array();
193 //$people_iter = $dbi->get_users_rated();
194 $people_dbi = RatingsDb::getTheRatingsDb();
195 $people_iter = $people_dbi->sql_get_users_rated();
196 while ($people_array = $people_iter->next()) {
197 if (isset($people_array['pagename'])) {
198 $userid = $people_array['pagename'];
199 $user = & RatingsUserFactory::getUser($userid);
200 array_push($allowed_users, $user);
206 $columns = $info ? explode(",", $info) : array();
207 // build our table...
208 $pagelist = new PageList($columns, $exclude, array('dimension' => $dimension, 'users' => $allowed_users_toshow));
211 //$preds = new _PageList_Column_prediction('prediction', _("Pred"), 'right', $dimension, $allowed_users);
212 $preds = array('_PageList_column_prediction','custom:prediction', _("Pred"),'right',' ' , $allowed_users);
213 $pagelist->addColumnObject($preds);
215 //$widget = new _PageList_Column_ratingwidget('ratingwidget', _("Rate"), 'left', $dimension);
216 $widget = array('_PageList_column_ratingwidget','custom:ratingwidget', _("Rate"), 'center');
217 $pagelist->addColumnObject($widget);
219 $noRatingUsers = array();
221 foreach($allowed_users_toshow as $idx => $user) {
222 // For proper caching behavior, get a ref, don't user $user
223 $u = & $allowed_users_toshow[$idx];
224 //$col = & new _PageList_Column_ratingvalue('ratingvalue', $u->getId(), 'right', $dimension, $u);
225 $col = array('_PageList_Column_ratingvalue','custom:ratingvalue', $u->getId(), 'right',' ' ,$u);
226 $pagelist->addColumnObject($col);
231 // add rows -- each row represents an item (page)
232 foreach($pageids as $pagename) {
233 // addPage can deal with cases where it is passed a string
234 $pagelist->addPage($pagename);
238 $pagelist->setCaption(_($caption));
249 // c-hanging-comment-ender-p: nil
250 // indent-tabs-mode: nil