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