]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/wikilens/PageListColumns.php
various unit test fixes: print error backtrace on _DEBUG_TRACE; allusers fix; new...
[SourceForge/phpwiki.git] / lib / wikilens / PageListColumns.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageListColumns.php,v 1.5 2004-07-07 15:01:44 dfrankow 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         // No, I don't know exactly why, but this needs to be a reference for
301         // the memoization in pearson_similarity and mean_rating to work
302         $this->_active_ratings_user =& new RatingsUser($active_user->getId());
303         $this->_PageList_Column($params[0], $params[1], $params[2]);
304         
305         if (!empty($params[3])) {
306             $this->_pagelist =& $params[3];
307             $this->_dimension = $this->_pagelist->getOption('dimension');
308             $this->_users = $this->_pagelist->getOption('users');
309         }
310     }
311
312     function _getValue ($page_handle, &$revision_handle) 
313     {
314         $ratings = $this->_active_ratings_user->get_ratings();
315         $iter = $page_handle->getLinks();
316         $recs = array();
317         while($current = $iter->next()) {
318             //filter out already rated
319             if (!$this->_active_ratings_user->get_rating($current->getName(), $this->_dimension)) {
320                 $recs[$current->getName()] = 
321                     $this->_active_ratings_user->knn_uu_predict($current->getName(), 
322                                                                 $this->_users, $this->_dimension);
323             }
324         }
325         arsort($recs);
326         $counter = 0;
327         if (count($recs) >= 3){
328             $numToShow = 3;
329         }
330         else {
331             // if <3 just show as many as there are
332             $numToShow = count($recs);
333         }
334         $html = HTML();
335         while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
336             if ($val < 3){ 
337                 break;
338             }
339             if ($counter > 0){
340                 $html->pushContent(" , ");
341             }
342             $html->pushContent(WikiLink($key));
343             
344             $counter++;
345         }
346         if (count($recs) == 0 || $counter == 0){
347             $html->pushContent(_("None"));
348         }
349         
350         //return $top3list;
351         return $html;
352     }
353 };
354
355 // register custom PageList type
356 global $WikiTheme;
357 $WikiTheme->addPageListColumn
358   (array
359    (
360     'numbacklinks' 
361     => array('_PageList_Column_numbacklinks','custom:numbacklinks', _("# things"), false),
362     'rating'       
363     => array('_PageList_Column_ratingwidget','custom:rating', _("Rate"), false),
364     'coagreement'  
365     => array('_PageList_Column_coagreement','custom:coagreement', _("Go?"), 'center'),
366     'minmisery'    
367     => array('_PageList_Column_minmisery','custom:minmisery', _("MinMisery"), 'center'),
368     'averagerating' 
369     => array('_PageList_Column_averagerating','custom:averagerating', _("Avg. Rating"), 'left'),
370     'top3recs'
371     => array('_PageList_Column_top3recs','custom:top3recs', _("Top Recommendations"), 'left'),
372     ));
373
374 // $Log: not supported by cvs2svn $
375 // Revision 1.4  2004/06/30 20:12:09  dfrankow
376 // + Change numbacklinks function to use existing core functions.
377 //   It's slower, but it'll work.
378 //
379 // + Change ratingvalue column to get its specific user as column 5 (index 4).
380 //
381 // + ratingwidget column uses WikiPlugin_RateIt's RatingWidgetHtml
382 //
383 // Revision 1.3  2004/06/21 17:01:41  rurban
384 // fix typo and rating method call
385 //
386 // Revision 1.2  2004/06/21 16:22:32  rurban
387 // add DEFAULT_DUMP_DIR and HTML_DUMP_DIR constants, for easier cmdline dumps,
388 // fixed dumping buttons locally (images/buttons/),
389 // support pages arg for dumphtml,
390 // optional directory arg for dumpserial + dumphtml,
391 // fix a AllPages warning,
392 // show dump warnings/errors on DEBUG,
393 // don't warn just ignore on wikilens pagelist columns, if not loaded.
394 // RateIt pagelist column is called "rating", not "ratingwidget" (Dan?)
395 //
396 // Revision 1.1  2004/06/18 14:42:17  rurban
397 // added wikilens libs (not yet merged good enough, some work for DanFr)
398 // 
399
400 // Local Variables:
401 // mode: php
402 // tab-width: 8
403 // c-basic-offset: 4
404 // c-hanging-comment-ender-p: nil
405 // indent-tabs-mode: nil
406 // End:
407 ?>