]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/wikilens/PageListColumns.php
New FSF address
[SourceForge/phpwiki.git] / lib / wikilens / PageListColumns.php
1 <?php // -*-php-*-
2 // $Id$
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 along
20  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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  * See also info=numbacklinks,numpagelinks at plugin/ListPages.php:_PageList_Column_ListPages_count
43  * and info=count at plugin/BackLinks.php:PageList_Column_BackLinks_count
44  */
45 class _PageList_Column_numbacklinks extends _PageList_Column_custom
46 {
47     function _getValue ($page_handle, &$revision_handle) {
48         $theIter = $page_handle->getBackLinks();
49         return $theIter->count();
50     }
51
52     function _getSortableValue ($page_handle, &$revision_handle) {
53         return $this->_getValue($page_handle, $revision_handle);
54     }
55 };
56
57 class _PageList_Column_coagreement extends _PageList_Column_custom
58 {
59     function _PageList_Column_coagreement ($params) {
60         $this->_pagelist =& $params[3];
61         $this->_PageList_Column($params[0], $params[1], $params[2]);
62         $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
63     }
64
65     function _getValue ($page_handle, &$revision_handle)
66     {
67         global $request;
68
69         $pagename = $page_handle->getName();
70
71         $active_user = $request->getUser();
72         $active_userId = $active_user->getId();
73         $dbi = $request->getDbh();
74         $p = CoAgreement($dbi, $pagename, $this->_selectedBuddies, $active_userId);
75         if($p == 1){
76             $p = "yes";
77         } elseif($p == 0){
78             $p = "unsure";
79         } elseif($p == -1){
80             $p = "no";
81         } else {
82             $p = "error";
83         }
84         //FIXME: $WikiTheme->getImageURL()
85         return HTML::img(array('src' => $WikiTheme->getImageURL($p)));
86     }
87 }
88
89 class _PageList_Column_minmisery extends _PageList_Column_custom
90 {
91     function _PageList_Column_minmisery ($params) {
92         $this->_pagelist =& $params[3];
93         $this->_PageList_Column($params[0], $params[1], $params[2]);
94         $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
95     }
96
97     function _getValue ($page_handle, &$revision_handle)
98     {
99         global $request, $WikiTheme;
100
101         $pagename = $page_handle->getName();
102
103         $active_user = $request->getUser();
104         $active_userId = $active_user->getId();
105         $dbi = $request->getDbh();
106         $p = MinMisery($dbi, $pagename, $this->_selectedBuddies, $active_userId);
107            $imgFix = floor($p * 2) / 2;
108         return HTML::img(array('src' => $WikiTheme->getImageURL("Rateit" . $imgFix)));
109     }
110 }
111
112 class _PageList_Column_averagerating extends _PageList_Column_custom
113 {
114     function _PageList_Column_averagerating ($params)
115     {
116         $this->_pagelist =& $params[3];
117         $this->_PageList_Column($params[0], $params[1], $params[2]);
118         $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
119     }
120
121     function _getValue ($page_handle, &$revision_handle)
122     {
123         global $request, $WikiTheme;
124
125         $pagename = $page_handle->getName();
126
127         $active_user = $request->getUser();
128         $active_userId = $active_user->getId();
129         $dbi = $request->getDbh();
130         $p = round(AverageRating($dbi, $pagename, $this->_selectedBuddies, $active_userId), 2);
131
132         $imgFix = floor($p * 2) / 2;
133         $html = HTML();
134         $html->pushContent(HTML::img(array('src' => $WikiTheme->getImageURL("Rateit" . $imgFix))));
135         $html->pushContent($p);
136         return $html;
137     }
138 };
139
140 /**
141  * Show the value of a rating as a digit (or "-" if no value), given the
142  * user who is the rater.
143  * This requires the RatingsUser as 5th paramater.
144  */
145 class _PageList_Column_ratingvalue extends _PageList_Column {
146     var $_user;
147     var $_dimension;
148
149     function _PageList_Column_ratingvalue ($params) {
150         $this->_pagelist =& $params[3];
151         $this->_user =& $params[4];//$this->_pagelist->getOption('user');
152         $this->_PageList_Column($params[0], $params[1], $params[2]);
153         $this->_dimension   = $this->_pagelist->getOption('dimension');
154         if (!$this->_dimension) $this->_dimension = 0;
155     }
156
157     function format ($pagelist, $page_handle, &$revision_handle)
158     {
159         if (empty($this->_user))
160             $this->_user =& RatingsUserFactory::getUser($GLOBALS['request']->_user->_userid);
161         assert(!empty($this->_user));
162         $rating = $this->_getValue($page_handle, $revision_handle);
163         $mean = $this->_user->mean_rating($this->_dimension);
164         $td = HTML::td($this->_tdattr);
165
166         $div = HTML::div();
167         if($rating != '-' && abs($rating - $mean) >= 0.75)
168             $div->setAttr('style', 'color: #' . ($rating > $mean ? '009900' : 'ff3333'));
169         $div->pushContent($rating);
170
171         $td->pushContent($div);
172
173         return $td;
174     }
175
176     function _getValue ($page_handle, &$revision_handle)
177     {
178         $pagename = $page_handle->getName();
179
180         $tu =& $this->_user;
181         $rating = $tu->get_rating($pagename, $this->_dimension);
182
183         // a dash (or *something*) arguably looks better than a big blank space
184         return ($rating ? $rating : "-");
185     }
186
187     function hasNoRatings($pages)
188     {
189         $total = 0;
190         $use = & $this->_user;
191         foreach ($pages as $page) {
192             if ($use->get_rating($page, $this->_dimension)) {
193                 $total++;
194             }
195         }
196         if ($total == 0) {
197             return true;
198         }
199         return false;
200
201     }
202
203     function _getSortableValue ($page_handle, &$revision_handle) {
204         return $this->_getValue($page_handle, $revision_handle);
205     }
206 };
207
208 /**
209  * Ratings widget for the logged-in user and the given page
210  * This uses the column name "rating".
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         if (!$this->_dimension) $this->_dimension = 0;
219     }
220
221     function format ($pagelist, $page_handle, &$revision_handle) {
222         $plugin = new WikiPlugin_RateIt();
223         $widget = $plugin->RatingWidgetHtml($page_handle->getName(), "",
224                                             "Star", $this->_dimension, "small");
225         $td = HTML::td($widget);
226         $td->setAttr('nowrap', 'nowrap');
227         return $td;
228     }
229
230     function _getValue ($page_handle, &$revision_handle)
231     {
232         global $request;
233
234         $pagename = $page_handle->getName();
235         $active_user   = $request->getUser();
236         $active_userid = $active_user->_userid;
237
238         $tu = & RatingsUserFactory::getUser($active_userid);
239         return $tu->get_rating($pagename, $this->_dimension);
240     }
241
242     function _getSortableValue ($page_handle, &$revision_handle) {
243         return $this->_getValue($page_handle, $revision_handle);
244     }
245 };
246
247 class _PageList_Column_prediction extends _PageList_Column
248 {
249     var $_active_ratings_user;
250     var $_users;
251
252     function _PageList_Column_prediction ($params)
253     {
254         global $request;
255         $active_user = $request->getUser();
256         // This needs to be a reference so things aren't recomputed for this user
257         $this->_active_ratings_user =& RatingsUserFactory::getUser($active_user->getId());
258
259         $this->_pagelist =& $params[3];
260         $this->_PageList_Column($params[0], $params[1], $params[2]);
261         $this->_dimension = $this->_pagelist->getOption('dimension');;
262         if (!$this->_dimension) $this->_dimension = 0;
263         $this->_users = $this->_pagelist->getOption('users');
264     }
265
266     function format ($pagelist, $page_handle, &$revision_handle) {
267         $pred = $this->_getValue($page_handle, $revision_handle);
268         $mean = $this->_active_ratings_user->mean_rating($this->_dimension);
269         $td = HTML::td($this->_tdattr);
270
271         $div = HTML::div();
272         if($pred > 0 && abs($pred - $mean) >= 0.75)
273             $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
274         $div->pushContent($pred);
275
276         $td->pushContent($div);
277
278         return $td;
279     }
280
281     function _getValue ($page_handle, &$revision_handle)
282     {
283         $pagename = $page_handle->getName();
284
285         $html = HTML();
286         $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
287         return sprintf("%.1f", min(5, max(0, $pred)));
288     }
289
290     function _getSortableValue ($page_handle, &$revision_handle) {
291         return $this->_getValue($page_handle, $revision_handle);
292     }
293 };
294
295 class _PageList_Column_top3recs extends _PageList_Column_custom
296 {
297
298     var $_active_ratings_user;
299     var $_users;
300
301     function _PageList_Column_top3recs ($params) {
302         global $request;
303         $active_user = $request->getUser();
304         if (is_string($active_user)) {
305         //FIXME: try to find the bug at test.php which sets request->_user and ->_group
306         trigger_error("request->getUser => string: $active_user", E_USER_WARNING);
307         $active_user = new MockUser($active_user,true);
308         }
309         // No, I don't know exactly why, but this needs to be a reference for
310         // the memoization in pearson_similarity and mean_rating to work
311         $this->_active_ratings_user = new RatingsUser($active_user->getId());
312         $this->_PageList_Column($params[0], $params[1], $params[2]);
313
314         if (!empty($params[3])) {
315             $this->_pagelist =& $params[3];
316             $this->_dimension = $this->_pagelist->getOption('dimension');
317             if (!$this->_dimension) $this->_dimension = 0;
318             $this->_users = $this->_pagelist->getOption('users');
319         }
320     }
321
322     function _getValue ($page_handle, &$revision_handle)
323     {
324         $ratings = $this->_active_ratings_user->get_ratings();
325         $iter = $page_handle->getLinks();
326         $recs = array();
327         while($current = $iter->next()) {
328             //filter out already rated
329             if (!$this->_active_ratings_user->get_rating($current->getName(), $this->_dimension)) {
330                 $recs[$current->getName()] =
331                     $this->_active_ratings_user->knn_uu_predict($current->getName(),
332                                                                 $this->_users, $this->_dimension);
333             }
334         }
335         arsort($recs);
336         $counter = 0;
337         if (count($recs) >= 3){
338             $numToShow = 3;
339         }
340         else {
341             // if <3 just show as many as there are
342             $numToShow = count($recs);
343         }
344         $html = HTML();
345         while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
346             if ($val < 3){
347                 break;
348             }
349             if ($counter > 0){
350                 $html->pushContent(" , ");
351             }
352             $html->pushContent(WikiLink($key));
353
354             $counter++;
355         }
356         if (count($recs) == 0 || $counter == 0){
357             $html->pushContent(_("None"));
358         }
359
360         //return $top3list;
361         return $html;
362     }
363 };
364
365 // register custom PageList type
366 global $WikiTheme;
367 $WikiTheme->addPageListColumn
368   (array
369    (
370     'numbacklinks'
371       => array('_PageList_Column_numbacklinks','custom:numbacklinks',
372            _("# things"), 'center'),
373     'rating'
374       => array('_PageList_Column_ratingwidget','custom:rating',
375            _("Rate"), false),
376     'ratingvalue'
377       => array('_PageList_Column_ratingvalue','custom:ratingvalue',
378            _("Rating"), 'center'),
379     'coagreement'
380       => array('_PageList_Column_coagreement','custom:coagreement',
381            _("Go?"), 'center'),
382     'minmisery'
383       => array('_PageList_Column_minmisery','custom:minmisery',
384            _("MinMisery"), 'center'),
385     'averagerating'
386       => array('_PageList_Column_averagerating','custom:averagerating',
387            _("Avg. Rating"), 'left'),
388     'top3recs'
389       => array('_PageList_Column_top3recs','custom:top3recs',
390            _("Top Recommendations"), 'left'),
391     /*'prediction'
392       => array('_PageList_Column_prediction','custom:prediction',
393                 _("Prediction"), false),*/
394     ));
395
396 // Local Variables:
397 // mode: php
398 // tab-width: 8
399 // c-basic-offset: 4
400 // c-hanging-comment-ender-p: nil
401 // indent-tabs-mode: nil
402 // End:
403 ?>