]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/UserRatings.php
prfotect against empty people pages
[SourceForge/phpwiki.git] / lib / plugin / UserRatings.php
1 <?php // -*-php-*-
2 rcs_id('$Id: UserRatings.php,v 1.5 2007-06-02 18:25:16 rurban Exp $');
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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  */
25
26 require_once('lib/PageList.php');
27 require_once('lib/wikilens/Buddy.php');
28 require_once("lib/wikilens/PageListColumns.php");
29
30 if (ENABLE_USER_NEW)
31     require_once("lib/WikiUserNew.php");
32 else
33     require_once("lib/WikiUser.php");
34
35 /**
36  * Show a user's ratings in a table, using PageList.
37  * Usage:
38  * <?plugin UserRatings ?>
39  *
40  * This only works with the "wikilens" theme.
41  */
42
43 class WikiPlugin_UserRatings
44 extends WikiPlugin
45 {
46     function getName () {
47         return _("UserRatings");
48     }
49
50     function getDescription () {
51         return _("List the user's ratings.");
52     }
53
54     function getVersion() {
55         return preg_replace("/[Revision: $]/", '',
56                             "\$Revision: 1.5 $");
57     }
58
59     function getDefaultArguments() {
60         return array('dimension' => '0',
61                      // array of userids to display ratings for; null/empty for 
62                      // active user only
63                      'userids'  => null,
64                      // array of pageids to display ratings for; null for all
65                      // of current active user's ratings
66                      'pageids'  => null,
67                      // a category to display ratings for; null for no category;
68                      // has higher precedence than pageids
69                      'category' => null,
70                      'pagename' => '[pagename]', // hackish
71                      'exclude'  => '',
72                      'limit'    => 0, // limit of <=0 is show-all
73                      'noheader' => 0,
74                      'userPage' => false,
75                      'nobuds'     => false,
76                      // rating columns are added later
77                      'info'     => 'pagename');
78                      // getting a bit crowded with the buddies...
79                      // 'info'     => 'hits,pagename,author,ratingwidget');
80     }
81     // info arg allows multiple columns
82     // info=mtime,hits,summary,version,author,locked,minor
83     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
84
85     function run($dbi, $argstr, &$request, $basepage) {
86         extract($this->getArgs($argstr, $request));
87
88         // XXX: fix to reflect multi-user ratings?
89         $caption = _("Displaying %d ratings:");
90
91         $active_user   = $request->getUser();
92         $active_userid = $active_user->_userid;
93         
94         // check for request to display a category's ratings
95         if(isset($category) && is_string($category) && strlen($category))
96         {
97             $pageids = array();
98             $category_page = $dbi->getPage($category);
99             $iter = $category_page->getLinks();
100             while($item = $iter->next())
101             {
102                 array_push($pageids, $item->getName());
103             }
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?
107             
108             $caption = sprintf(_("Showing all %ss (%%d):"), $category);
109         }
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))
113         {
114             // XXX: need support for sorted ratings
115             // bug: pages excluded from the PageList via the "exclude" argument 
116             // count toward the limit!
117             $pageids = array();
118             
119             $active_user_ratings_user = & RatingsUserFactory::getUser($active_user->getId());
120             $current_user_ratings = $active_user_ratings_user->get_ratings();
121             
122             if ($userPage){
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();
127             } else {
128                 $caption = _("Here are your %d page ratings:");
129                 $ratings = $current_user_ratings;
130             }    
131             
132             
133             $i = 0;
134             foreach($ratings as $pagename => $page_ratings)
135             {
136                 // limit is currently only honored for "own" ratings
137                 if($limit > 0 && $i >= $limit)
138                 {
139                     break;
140                 }
141                 if(isset($page_ratings[$dimension]))
142                 {
143                     array_push($pageids, $pagename);
144                     $i++;
145                 }
146             }
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
149            // name's ratings
150            $ratings = $current_user_ratings;
151         }
152
153         // if userids is null or empty, fill it with just the active user
154         if(!isset($userids) || !is_array($userids) || !count($userids))
155         {
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());
161                 } else {
162                    $userids = getBuddies($active_userid, $dbi);
163                 } 
164             }
165             elseif ($userPage)
166             {
167                 //we're on a user page, show that user's ratings as the only column
168                 $userids = array();
169                 array_push($userids, $userPage);   
170             }
171             else
172             {
173                 $userids = array();
174                 // XXX: this wipes out the category caption...
175                 // $caption = _("You must be logged in to view ratings.");
176             }
177         }
178
179         // find out which users we should show ratings for
180         
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)
186         {
187             $user = & RatingsUserFactory::getUser($userid);
188             if($user->allow_view_ratings($active_user))
189             {
190                 array_push($allowed_users_toshow, $user);
191             }
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!
196             unset($user);
197         }
198         // if no buddies, use allusers in prediction calculation
199         
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);
210                 }
211             }
212             
213          }
214         
215
216         $columns = $info ? explode(",", $info) : array();
217         // build our table...
218         $pagelist = new PageList($columns, $exclude, array('dimension' => $dimension, 'users' => $allowed_users_toshow));
219
220         // augment columns
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);
224         
225         //$widget = new _PageList_Column_ratingwidget('ratingwidget', _("Rate"), 'left', $dimension);        
226         $widget = array('_PageList_column_ratingwidget','custom:ratingwidget', _("Rate"), 'center');
227         $pagelist->addColumnObject($widget);
228         
229         $noRatingUsers = array();
230         if (!$nobuds){
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);
237                 unset($u);
238             }
239         }
240
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);
245         }
246         
247         if (! $noheader) {
248             $pagelist->setCaption(_($caption));
249         }
250         
251
252         return $pagelist;
253     }
254 };
255
256
257 // Local Variables:
258 // mode: php
259 // tab-width: 8
260 // c-basic-offset: 4
261 // c-hanging-comment-ender-p: nil
262 // indent-tabs-mode: nil
263 // End:
264 ?>