]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/wikilens/PageListColumns.php
Revert
[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         $this->_PageList_Column($params[0], $params[1], $params[2]);
158         $this->_dimension = $this->_pagelist->getOption('dimension');
159         if (!$this->_dimension) $this->_dimension = 0;
160     }
161
162     function format($pagelist, $page_handle, &$revision_handle)
163     {
164         if (empty($this->_user))
165             $this->_user =& RatingsUserFactory::getUser($GLOBALS['request']->_user->_userid);
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             "Star", $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         global $request;
241
242         $pagename = $page_handle->getName();
243         $active_user = $request->getUser();
244         $active_userid = $active_user->_userid;
245
246         $tu = & RatingsUserFactory::getUser($active_userid);
247         return $tu->get_rating($pagename, $this->_dimension);
248     }
249
250     function _getSortableValue($page_handle, &$revision_handle)
251     {
252         return $this->_getValue($page_handle, $revision_handle);
253     }
254 }
255
256 class _PageList_Column_prediction extends _PageList_Column
257 {
258     public $_active_ratings_user;
259     public $_users;
260
261     function _PageList_Column_prediction($params)
262     {
263         global $request;
264         $active_user = $request->getUser();
265         // This needs to be a reference so things aren't recomputed for this user
266         $this->_active_ratings_user =& RatingsUserFactory::getUser($active_user->getId());
267
268         $this->_pagelist =& $params[3];
269         $this->_PageList_Column($params[0], $params[1], $params[2]);
270         $this->_dimension = $this->_pagelist->getOption('dimension');
271         ;
272         if (!$this->_dimension) $this->_dimension = 0;
273         $this->_users = $this->_pagelist->getOption('users');
274     }
275
276     function format($pagelist, $page_handle, &$revision_handle)
277     {
278         $pred = $this->_getValue($page_handle, $revision_handle);
279         $mean = $this->_active_ratings_user->mean_rating($this->_dimension);
280         $td = HTML::td($this->_tdattr);
281
282         $div = HTML::div();
283         if ($pred > 0 && abs($pred - $mean) >= 0.75)
284             $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
285         $div->pushContent($pred);
286
287         $td->pushContent($div);
288
289         return $td;
290     }
291
292     function _getValue($page_handle, &$revision_handle)
293     {
294         $pagename = $page_handle->getName();
295
296         $html = HTML();
297         $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
298         return sprintf("%.1f", min(5, max(0, $pred)));
299     }
300
301     function _getSortableValue($page_handle, &$revision_handle)
302     {
303         return $this->_getValue($page_handle, $revision_handle);
304     }
305 }
306
307 class _PageList_Column_top3recs extends _PageList_Column_custom
308 {
309
310     public $_active_ratings_user;
311     public $_users;
312
313     function _PageList_Column_top3recs($params)
314     {
315         global $request;
316         $active_user = $request->getUser();
317         if (is_string($active_user)) {
318             //FIXME: try to find the bug at test.php which sets request->_user and ->_group
319             trigger_error("request->getUser => string: $active_user", E_USER_WARNING);
320             $active_user = new MockUser($active_user, true);
321         }
322         // No, I don't know exactly why, but this needs to be a reference for
323         // the memoization in pearson_similarity and mean_rating to work
324         $this->_active_ratings_user = new RatingsUser($active_user->getId());
325         $this->_PageList_Column($params[0], $params[1], $params[2]);
326
327         if (!empty($params[3])) {
328             $this->_pagelist =& $params[3];
329             $this->_dimension = $this->_pagelist->getOption('dimension');
330             if (!$this->_dimension) $this->_dimension = 0;
331             $this->_users = $this->_pagelist->getOption('users');
332         }
333     }
334
335     function _getValue($page_handle, &$revision_handle)
336     {
337         $ratings = $this->_active_ratings_user->get_ratings();
338         $iter = $page_handle->getLinks();
339         $recs = array();
340         while ($current = $iter->next()) {
341             //filter out already rated
342             if (!$this->_active_ratings_user->get_rating($current->getName(), $this->_dimension)) {
343                 $recs[$current->getName()] =
344                     $this->_active_ratings_user->knn_uu_predict($current->getName(),
345                         $this->_users, $this->_dimension);
346             }
347         }
348         arsort($recs);
349         $counter = 0;
350         if (count($recs) >= 3) {
351             $numToShow = 3;
352         } else {
353             // if <3 just show as many as there are
354             $numToShow = count($recs);
355         }
356         $html = HTML();
357         while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
358             if ($val < 3) {
359                 break;
360             }
361             if ($counter > 0) {
362                 $html->pushContent(" , ");
363             }
364             $html->pushContent(WikiLink($key));
365
366             $counter++;
367         }
368         if (count($recs) == 0 || $counter == 0) {
369             $html->pushContent(_("None"));
370         }
371
372         //return $top3list;
373         return $html;
374     }
375 }
376
377 // register custom PageList type
378 global $WikiTheme;
379 $WikiTheme->addPageListColumn
380 (array
381 (
382     'numbacklinks'
383     => array('_PageList_Column_numbacklinks', 'custom:numbacklinks',
384         _("# things"), 'center'),
385     'rating'
386     => array('_PageList_Column_ratingwidget', 'custom:rating',
387         _("Rate"), false),
388     'ratingvalue'
389     => array('_PageList_Column_ratingvalue', 'custom:ratingvalue',
390         _("Rating"), 'center'),
391     'coagreement'
392     => array('_PageList_Column_coagreement', 'custom:coagreement',
393         _("Go?"), 'center'),
394     'minmisery'
395     => array('_PageList_Column_minmisery', 'custom:minmisery',
396         _("MinMisery"), 'center'),
397     'averagerating'
398     => array('_PageList_Column_averagerating', 'custom:averagerating',
399         _("Avg. Rating"), 'left'),
400     'top3recs'
401     => array('_PageList_Column_top3recs', 'custom:top3recs',
402         _("Top Recommendations"), 'left'),
403     /*'prediction'
404       => array('_PageList_Column_prediction','custom:prediction',
405                 _("Prediction"), false),*/
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: