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