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