]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/wikilens/PageListColumns.php
plugin->run consistency: request as reference, added basepage.
[SourceForge/phpwiki.git] / lib / wikilens / PageListColumns.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageListColumns.php,v 1.6 2004-07-08 20:30:07 rurban Exp $');
3
4 /*
5  Copyright 2004 Mike Cassano
6
7  This file is part of PhpWiki.
8
9  PhpWiki is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  PhpWiki is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with PhpWiki; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /**
25  * wikilens specific Custom pagelist columns
26  *
27  * Rationale: Certain themes should be able to extend the predefined list 
28  *  of pagelist types. E.g. certain plugins, like MostPopular might use 
29  *  info=pagename,hits,rating
30  *  which displays the rating column whenever the wikilens theme is active.
31  *  Similarly as in certain plugins, like WikiAdminRename or _WikiTranslation
32  */
33
34 require_once('lib/PageList.php');
35 require_once("lib/wikilens/RatingsUser.php");
36 require_once('lib/plugin/RateIt.php');
37
38 /**
39  * Column representing the number of backlinks to the page.
40  * Perhaps this number should be made a 'field' of a page, in
41  * which case this column type would not be necessary.
42  */
43 class _PageList_Column_numbacklinks extends _PageList_Column_custom
44 {
45     function _getValue ($page_handle, &$revision_handle) 
46     {
47         //return $page_handle->getNumLinks();
48         $theIter = $page_handle->getBackLinks();
49         $total = 0;
50         while($curr = $theIter->next()){
51             $total++;
52         }
53         return $total;
54     }
55     
56     function _getSortableValue ($page_handle, &$revision_handle) {
57         return $this->_getValue($page_handle, &$revision_handle);
58     }
59 };
60
61 class _PageList_Column_coagreement extends _PageList_Column_custom 
62 {
63     function _PageList_Column_coagreement ($params) {
64         $this->_pagelist =& $params[3];
65         $this->_PageList_Column($params[0], $params[1], $params[2]);
66         $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
67     }
68
69     function _getValue ($page_handle, &$revision_handle) 
70     {
71         global $request;
72
73         $pagename = $page_handle->getName();
74
75         $active_user = $request->getUser();
76         $active_userId = $active_user->getId();
77         $dbi = $request->getDbh();      
78         $p = CoAgreement($dbi, $pagename, $this->_selectedBuddies, $active_userId);
79         if($p == 1){
80             $p = "yes";
81         } elseif($p == 0){
82             $p = "unsure";
83         } elseif($p == -1){
84             $p = "no";  
85         } else {
86             $p = "error";       
87         }   
88         //FIXME: $WikiTheme->getImageURL()
89         return HTML::img(array('src' => "../images/" . $p . ".gif"));
90     }
91 }
92
93 class _PageList_Column_minmisery extends _PageList_Column_custom 
94 {
95     function _PageList_Column_minmisery ($params) {
96         $this->_pagelist =& $params[3];
97         $this->_PageList_Column($params[0], $params[1], $params[2]);
98         $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
99     }
100
101     function _getValue ($page_handle, &$revision_handle) 
102     {
103         global $request;
104
105         $pagename = $page_handle->getName();
106
107         $active_user = $request->getUser();
108         $active_userId = $active_user->getId();
109         $dbi = $request->getDbh();      
110         $p = MinMisery($dbi, $pagename, $this->_selectedBuddies, $active_userId);
111         $imgFix = floor($p * 2) / 2;
112         //FIXME: $WikiTheme->getImageURL()
113         return HTML::img(array('src' => "../images/" . $imgFix . ".png"));
114     }
115 }
116
117 class _PageList_Column_averagerating extends _PageList_Column_custom
118 {
119     function init ($field, $default_heading, $align, &$pagelist) 
120     {
121         $this->_PageList_Column($field, $default_heading, $align);
122         $this->_selectedBuddies = $pagelist->getOption('selectedBuddies');
123     }
124
125     function _getValue ($page_handle, &$revision_handle) 
126     {
127         global $request;
128
129         $pagename = $page_handle->getName();
130
131         $active_user = $request->getUser();
132         $active_userId = $active_user->getId();
133         $dbi = $request->getDbh();      
134         $p = round(AverageRating($dbi, $pagename, $this->_selectedBuddies, $active_userId), 2);
135       
136         $imgFix = floor($p * 2) / 2;
137         $html = HTML();
138         //FIXME: $WikiTheme->getImageURL()
139         $html->pushContent(HTML::img(array('src' => "../images/" . $imgFix . ".png")));
140         $html->pushContent($p);
141         return $html;
142     }
143 };
144  
145 /**
146  * Show the value of a rating as a digit (or "-" if no value), given the
147  * user who is the rater.
148  */
149 class _PageList_Column_ratingvalue extends _PageList_Column {
150     var $_user;
151     var $_dimension;
152
153     function _PageList_Column_ratingvalue ($params) {
154         $this->_pagelist =& $params[3];
155         $this->_user =& $params[4];//$this->_pagelist->getOption('user');
156         assert(!empty($this->_user));
157         $this->_PageList_Column($params[0], $params[1], $params[2]);
158         $this->_dimension   = $this->_pagelist->getOption('dimension');
159     }
160
161     function format ($pagelist, $page_handle, &$revision_handle) 
162     {
163         $rating = $this->_getValue($page_handle, $revision_handle);
164         $mean = $this->_user->mean_rating($this->_dimension);
165         $td = HTML::td($this->_tdattr);
166
167         $div = HTML::div();
168         if($rating != '-' && abs($rating - $mean) >= 0.75)
169             $div->setAttr('style', 'color: #' . ($rating > $mean ? '009900' : 'ff3333'));
170         $div->pushContent($rating);
171
172         $td->pushContent($div);
173
174         return $td;
175     }
176
177     function _getValue ($page_handle, &$revision_handle) 
178     {
179         $pagename = $page_handle->getName();
180
181         $tu = & $this->_user;
182         $rating = $tu->get_rating($pagename, $this->_dimension);
183
184         // a dash (or *something*) arguably looks better than a big blank space
185         return ($rating ? $rating : "-");
186     }
187     
188     function hasNoRatings($pages)
189     {      
190         $total = 0;
191         $use = & $this->_user;
192         foreach ($pages as $page) {
193             if ($use->get_rating($page, $this->_dimension)) {
194                 $total++;
195             }
196         }
197         if ($total == 0) {
198             return true;
199         }
200         return false; 
201           
202     }
203     
204     function _getSortableValue ($page_handle, &$revision_handle) {
205         return $this->_getValue($page_handle, &$revision_handle);
206     }
207 };
208
209 /**
210  * Ratings widget for the logged-in user and the given page
211  */
212 class _PageList_Column_ratingwidget extends _PageList_Column_custom 
213 {
214     function _PageList_Column_ratingwidget ($params) {
215         $this->_pagelist =& $params[3];
216         $this->_PageList_Column($params[0], $params[1], $params[2]);
217         $this->_dimension = $this->_pagelist->getOption('dimension');
218     }
219
220     function format ($pagelist, $page_handle, &$revision_handle) {
221         $widget = WikiPlugin_RateIt::RatingWidgetHtml($page_handle->getName(), "", "pagerat", $this->_dimension, "small");
222         $td = HTML::td($widget);
223         $td->setAttr('nowrap', 'nowrap');
224         return $td;
225     }
226
227     function _getValue ($page_handle, &$revision_handle) 
228     {
229         global $request;
230
231         $pagename = $page_handle->getName();
232         $active_user   = $request->getUser();
233         $active_userid = $active_user->_userid;
234         
235         $tu = & RatingsUserFactory::getUser($active_userid);
236         return $tu->get_rating($pagename, $this->_dimension);
237     }
238     
239     function _getSortableValue ($page_handle, &$revision_handle) {
240         return $this->_getValue($page_handle, &$revision_handle);
241     }
242 };
243
244 class _PageList_Column_prediction extends _PageList_Column 
245 {
246     var $_active_ratings_user;
247     var $_users;
248
249     function _PageList_Column_prediction ($params) 
250     {
251         global $request;
252         $active_user = $request->getUser();
253         // This needs to be a reference so things aren't recomputed for this user
254         $this->_active_ratings_user =& RatingsUserFactory::getUser($active_user->getId());
255         
256         $this->_pagelist =& $params[3];
257         $this->_PageList_Column($params[0], $params[1], $params[2]);
258         $this->_dimension = $this->_pagelist->getOption('dimension');;
259         $this->_users = $this->_pagelist->getOption('users');
260     }
261
262     function format ($pagelist, $page_handle, &$revision_handle) {
263         $pred = $this->_getValue($page_handle, $revision_handle);
264         $mean = $this->_active_ratings_user->mean_rating($this->_dimension);
265         $td = HTML::td($this->_tdattr);
266
267         $div = HTML::div();
268         if($pred > 0 && abs($pred - $mean) >= 0.75)
269             $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
270         $div->pushContent($pred);
271
272         $td->pushContent($div);
273
274         return $td;
275     }
276
277     function _getValue ($page_handle, &$revision_handle) 
278     {
279         $pagename = $page_handle->getName();
280
281         $html = HTML();
282         $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
283         return sprintf("%.1f", min(5, max(0, $pred)));
284     }
285     
286     function _getSortableValue ($page_handle, &$revision_handle) {
287         return $this->_getValue($page_handle, &$revision_handle);
288     }
289 };
290
291 class _PageList_Column_top3recs extends _PageList_Column_custom
292 {
293     
294     var $_active_ratings_user;
295     var $_users;
296
297     function _PageList_Column_top3recs ($params) {
298         global $request;
299         $active_user = $request->getUser();
300         if (is_string($active_user)) {
301                 //FIXME: try to find the bug at test.php which sets request->_user and ->_group
302                 trigger_error("request->getUser => string: $active_user", E_USER_WARNING);
303                 $active_user = new MockUser($active_user,true);
304         }
305         // No, I don't know exactly why, but this needs to be a reference for
306         // the memoization in pearson_similarity and mean_rating to work
307         $this->_active_ratings_user =& new RatingsUser($active_user->getId());
308         $this->_PageList_Column($params[0], $params[1], $params[2]);
309         
310         if (!empty($params[3])) {
311             $this->_pagelist =& $params[3];
312             $this->_dimension = $this->_pagelist->getOption('dimension');
313             $this->_users = $this->_pagelist->getOption('users');
314         }
315     }
316
317     function _getValue ($page_handle, &$revision_handle) 
318     {
319         $ratings = $this->_active_ratings_user->get_ratings();
320         $iter = $page_handle->getLinks();
321         $recs = array();
322         while($current = $iter->next()) {
323             //filter out already rated
324             if (!$this->_active_ratings_user->get_rating($current->getName(), $this->_dimension)) {
325                 $recs[$current->getName()] = 
326                     $this->_active_ratings_user->knn_uu_predict($current->getName(), 
327                                                                 $this->_users, $this->_dimension);
328             }
329         }
330         arsort($recs);
331         $counter = 0;
332         if (count($recs) >= 3){
333             $numToShow = 3;
334         }
335         else {
336             // if <3 just show as many as there are
337             $numToShow = count($recs);
338         }
339         $html = HTML();
340         while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
341             if ($val < 3){ 
342                 break;
343             }
344             if ($counter > 0){
345                 $html->pushContent(" , ");
346             }
347             $html->pushContent(WikiLink($key));
348             
349             $counter++;
350         }
351         if (count($recs) == 0 || $counter == 0){
352             $html->pushContent(_("None"));
353         }
354         
355         //return $top3list;
356         return $html;
357     }
358 };
359
360 // register custom PageList type
361 global $WikiTheme;
362 $WikiTheme->addPageListColumn
363   (array
364    (
365     'numbacklinks' 
366     => array('_PageList_Column_numbacklinks','custom:numbacklinks', _("# things"), false),
367     'rating'       
368     => array('_PageList_Column_ratingwidget','custom:rating', _("Rate"), false),
369     'coagreement'  
370     => array('_PageList_Column_coagreement','custom:coagreement', _("Go?"), 'center'),
371     'minmisery'    
372     => array('_PageList_Column_minmisery','custom:minmisery', _("MinMisery"), 'center'),
373     'averagerating' 
374     => array('_PageList_Column_averagerating','custom:averagerating', _("Avg. Rating"), 'left'),
375     'top3recs'
376     => array('_PageList_Column_top3recs','custom:top3recs', _("Top Recommendations"), 'left'),
377     ));
378
379 // $Log: not supported by cvs2svn $
380 // Revision 1.5  2004/07/07 15:01:44  dfrankow
381 // Allow ratingvalue, ratingwidget, prediction, numbacklinks columns to be sortable
382 //
383 // Revision 1.4  2004/06/30 20:12:09  dfrankow
384 // + Change numbacklinks function to use existing core functions.
385 //   It's slower, but it'll work.
386 //
387 // + Change ratingvalue column to get its specific user as column 5 (index 4).
388 //
389 // + ratingwidget column uses WikiPlugin_RateIt's RatingWidgetHtml
390 //
391 // Revision 1.3  2004/06/21 17:01:41  rurban
392 // fix typo and rating method call
393 //
394 // Revision 1.2  2004/06/21 16:22:32  rurban
395 // add DEFAULT_DUMP_DIR and HTML_DUMP_DIR constants, for easier cmdline dumps,
396 // fixed dumping buttons locally (images/buttons/),
397 // support pages arg for dumphtml,
398 // optional directory arg for dumpserial + dumphtml,
399 // fix a AllPages warning,
400 // show dump warnings/errors on DEBUG,
401 // don't warn just ignore on wikilens pagelist columns, if not loaded.
402 // RateIt pagelist column is called "rating", not "ratingwidget" (Dan?)
403 //
404 // Revision 1.1  2004/06/18 14:42:17  rurban
405 // added wikilens libs (not yet merged good enough, some work for DanFr)
406 // 
407
408 // Local Variables:
409 // mode: php
410 // tab-width: 8
411 // c-basic-offset: 4
412 // c-hanging-comment-ender-p: nil
413 // indent-tabs-mode: nil
414 // End:
415 ?>