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