]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UserRatings.php
Rename lib/WikiUserNew.php --> lib/WikiUser.php
[SourceForge/phpwiki.git] / lib / plugin / UserRatings.php
1 <?php
2
3 /**
4  * Copyright 2004 Dan Frankowski
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 require_once 'lib/PageList.php';
24 require_once 'lib/wikilens/Buddy.php';
25 require_once 'lib/wikilens/PageListColumns.php';
26 require_once 'lib/WikiUser.php';
27
28 /**
29  * Show a user's ratings in a table, using PageList.
30  * Usage:
31  * <<UserRatings >>
32  *
33  * This only works with the "wikilens" theme.
34  */
35
36 class WikiPlugin_UserRatings
37     extends WikiPlugin
38 {
39     function getDescription()
40     {
41         return _("List the user's ratings.");
42     }
43
44     function getDefaultArguments()
45     {
46         return array('dimension' => '0',
47             // array of userids to display ratings for; null/empty for
48             // active user only
49             'userids' => null,
50             // array of pageids to display ratings for; null for all
51             // of current active user's ratings
52             'pageids' => null,
53             // a category to display ratings for; null for no category;
54             // has higher precedence than pageids
55             'category' => null,
56             'pagename' => '[pagename]', // hackish
57             'exclude' => '',
58             'limit' => 0, // limit of <=0 is show-all
59             'noheader' => 0,
60             'userPage' => false,
61             'nobuds' => false,
62             // rating columns are added later
63             'info' => 'pagename');
64         // getting a bit crowded with the buddies...
65         // 'info'     => 'hits,pagename,author,ratingwidget');
66     }
67
68     // info arg allows multiple columns
69     // info=mtime,hits,summary,version,author,locked,minor
70     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
71
72     function run($dbi, $argstr, &$request, $basepage)
73     {
74         extract($this->getArgs($argstr, $request));
75
76         // XXX: fix to reflect multi-user ratings?
77         $caption = _("Displaying %d ratings:");
78
79         $active_user = $request->getUser();
80         $active_userid = $active_user->_userid;
81
82         // check for request to display a category's ratings
83         if (isset($category) && is_string($category) && strlen($category)) {
84             $pageids = array();
85             $category_page = $dbi->getPage($category);
86             $iter = $category_page->getLinks();
87             while ($item = $iter->next()) {
88                 array_push($pageids, $item->getName());
89             }
90             // XXX: is there a way to retrieve the preferred plural
91             // representation of a category name?
92             // XXX: should the category text be a link?  can it be one easily?
93
94             $caption = sprintf(_("Showing all %ss (%%d):"), $category);
95         } // if null is passed as the pageids argument and no category was passed,
96         // show active user's ratings
97         elseif (!isset($pageids) || !is_array($pageids)) {
98             // XXX: need support for sorted ratings
99             // bug: pages excluded from the PageList via the "exclude" argument
100             // count toward the limit!
101             $pageids = array();
102
103             $active_user_ratings_user = & RatingsUserFactory::getUser($active_user->getId());
104             $current_user_ratings = $active_user_ratings_user->get_ratings();
105
106             if ($userPage) {
107                 //we're on a user's homepage, get *their* ratings
108                 $this_page_user = & RatingsUserFactory::getUser($userPage);
109                 $caption = _("Here are $userPage" . "'s %d page ratings:");
110                 $ratings = $this_page_user->get_ratings();
111             } else {
112                 $caption = _("Here are your %d page ratings:");
113                 $ratings = $current_user_ratings;
114             }
115
116             $i = 0;
117             foreach ($ratings as $pagename => $page_ratings) {
118                 // limit is currently only honored for "own" ratings
119                 if ($limit > 0 && $i >= $limit) {
120                     break;
121                 }
122                 if (isset($page_ratings[$dimension])) {
123                     array_push($pageids, $pagename);
124                     $i++;
125                 }
126             }
127             // $caption = _("Here are your %d page ratings:");
128             //make $ratings the user's ratings again if it had been treated as the current page
129             // name's ratings
130             $ratings = $current_user_ratings;
131         }
132
133         // if userids is null or empty, fill it with just the active user
134         if (!isset($userids) || !is_array($userids) || !count($userids)) {
135             // TKL: moved getBuddies call inside if statement because it was
136             // causing the userids[] parameter to be ignored
137             if (is_string($active_userid) && strlen($active_userid) && $active_user->isSignedIn() && !$userPage) {
138                 if (isset($category_page)) {
139                     $userids = getBuddies($active_userid, $dbi, $category_page->getName());
140                 } else {
141                     $userids = getBuddies($active_userid, $dbi);
142                 }
143             } elseif ($userPage) {
144                 //we're on a user page, show that user's ratings as the only column
145                 $userids = array();
146                 array_push($userids, $userPage);
147             } else {
148                 $userids = array();
149                 // XXX: this wipes out the category caption...
150                 // $caption = _("You must be logged in to view ratings.");
151             }
152         }
153
154         // find out which users we should show ratings for
155
156         // users allowed in the prediction calculation
157         $allowed_users = array();
158         // users actually allowed to be shown to the user
159         $allowed_users_toshow = array();
160         foreach ($userids as $userid) {
161             $user = & RatingsUserFactory::getUser($userid);
162             if ($user->allow_view_ratings($active_user)) {
163                 array_push($allowed_users_toshow, $user);
164             }
165             // all users should be allowed in calculation
166             array_push($allowed_users, $user);
167             // This line ensures $user is not a reference type after this loop
168             // If it is a reference type, that can produce very unexpected behavior!
169             unset($user);
170         }
171         // if no buddies, use allusers in prediction calculation
172
173         if (count($userids) == 0 || $userPage) {
174             $allowed_users = array();
175             //$people_iter = $dbi->get_users_rated();
176             $people_dbi = RatingsDb::getTheRatingsDb();
177             $people_iter = $people_dbi->sql_get_users_rated();
178             while ($people_array = $people_iter->next()) {
179                 if (isset($people_array['pagename'])) {
180                     $userid = $people_array['pagename'];
181                     $user = & RatingsUserFactory::getUser($userid);
182                     array_push($allowed_users, $user);
183                 }
184             }
185
186         }
187
188         $columns = $info ? explode(",", $info) : array();
189         // build our table...
190         $pagelist = new PageList($columns, $exclude, array('dimension' => $dimension, 'users' => $allowed_users_toshow));
191
192         // augment columns
193         //$preds = new _PageList_Column_prediction('prediction', _("Pred"), 'right', $dimension, $allowed_users);
194         $preds = array('_PageList_Column_prediction', 'custom:prediction', _("Pred"), 'right', ' ', $allowed_users);
195         $pagelist->addColumnObject($preds);
196
197         //$widget = new _PageList_Column_ratingwidget('ratingwidget', _("Rate"), 'left', $dimension);
198         $widget = array('_PageList_Column_ratingwidget', 'custom:ratingwidget', _("Rate"), 'center');
199         $pagelist->addColumnObject($widget);
200
201         $noRatingUsers = array();
202         if (!$nobuds) {
203             foreach ($allowed_users_toshow as $idx => $user) {
204                 // For proper caching behavior, get a ref, don't user $user
205                 $u = & $allowed_users_toshow[$idx];
206                 //$col = & new _PageList_Column_ratingvalue('ratingvalue', $u->getId(), 'right', $dimension, $u);
207                 $col = array('_PageList_Column_ratingvalue', 'custom:ratingvalue', $u->getId(), 'right', ' ', $u);
208                 $pagelist->addColumnObject($col);
209                 unset($u);
210             }
211         }
212
213         // add rows -- each row represents an item (page)
214         foreach ($pageids as $pagename) {
215             // addPage can deal with cases where it is passed a string
216             $pagelist->addPage($pagename);
217         }
218
219         if (!$noheader) {
220             $pagelist->setCaption(_($caption));
221         }
222
223         return $pagelist;
224     }
225 }
226
227 // Local Variables:
228 // mode: php
229 // tab-width: 8
230 // c-basic-offset: 4
231 // c-hanging-comment-ender-p: nil
232 // indent-tabs-mode: nil
233 // End: