4 * Copyright 2004 Mike Cassano
6 * This file is part of PhpWiki.
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.
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.
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.
24 * wikilens specific Custom pagelist columns
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
33 require_once 'lib/PageList.php';
34 require_once 'lib/wikilens/RatingsUser.php';
35 require_once 'lib/plugin/RateIt.php';
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
44 class _PageList_Column_numbacklinks extends _PageList_Column_custom
46 function _getValue($page_handle, &$revision_handle)
48 $theIter = $page_handle->getBackLinks();
49 return $theIter->count();
52 function _getSortableValue($page_handle, &$revision_handle)
54 return $this->_getValue($page_handle, $revision_handle);
58 class _PageList_Column_coagreement extends _PageList_Column_custom
60 function _PageList_Column_coagreement($params)
62 $this->_pagelist =& $params[3];
63 $this->_PageList_Column($params[0], $params[1], $params[2]);
64 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
67 function _getValue($page_handle, &$revision_handle)
71 $pagename = $page_handle->getName();
73 $active_user = $request->getUser();
74 $active_userId = $active_user->getId();
75 $dbi = $request->getDbh();
76 $p = CoAgreement($dbi, $pagename, $this->_selectedBuddies, $active_userId);
86 //FIXME: $WikiTheme->getImageURL()
87 return HTML::img(array('src' => $WikiTheme->getImageURL($p)));
91 class _PageList_Column_minmisery extends _PageList_Column_custom
93 function _PageList_Column_minmisery($params)
95 $this->_pagelist =& $params[3];
96 $this->_PageList_Column($params[0], $params[1], $params[2]);
97 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
100 function _getValue($page_handle, &$revision_handle)
102 global $request, $WikiTheme;
104 $pagename = $page_handle->getName();
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)));
115 class _PageList_Column_averagerating extends _PageList_Column_custom
117 function _PageList_Column_averagerating($params)
119 $this->_pagelist =& $params[3];
120 $this->_PageList_Column($params[0], $params[1], $params[2]);
121 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
124 function _getValue($page_handle, &$revision_handle)
126 global $request, $WikiTheme;
128 $pagename = $page_handle->getName();
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);
135 $imgFix = floor($p * 2) / 2;
137 $html->pushContent(HTML::img(array('src' => $WikiTheme->getImageURL("Rateit" . $imgFix))));
138 $html->pushContent($p);
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.
148 class _PageList_Column_ratingvalue extends _PageList_Column
153 function _PageList_Column_ratingvalue($params)
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;
162 function format($pagelist, $page_handle, &$revision_handle)
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);
172 if ($rating != '-' && abs($rating - $mean) >= 0.75)
173 $div->setAttr('style', 'color: #' . ($rating > $mean ? '009900' : 'ff3333'));
174 $div->pushContent($rating);
176 $td->pushContent($div);
181 function _getValue($page_handle, &$revision_handle)
183 $pagename = $page_handle->getName();
186 $rating = $tu->get_rating($pagename, $this->_dimension);
188 // a dash (or *something*) arguably looks better than a big blank space
189 return ($rating ? $rating : "-");
192 function hasNoRatings($pages)
195 $use = & $this->_user;
196 foreach ($pages as $page) {
197 if ($use->get_rating($page, $this->_dimension)) {
208 function _getSortableValue($page_handle, &$revision_handle)
210 return $this->_getValue($page_handle, $revision_handle);
215 * Ratings widget for the logged-in user and the given page
216 * This uses the column name "rating".
218 class _PageList_Column_ratingwidget extends _PageList_Column_custom
220 function _PageList_Column_ratingwidget($params)
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;
228 function format($pagelist, $page_handle, &$revision_handle)
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');
238 function _getValue($page_handle, &$revision_handle)
242 $pagename = $page_handle->getName();
243 $active_user = $request->getUser();
244 $active_userid = $active_user->_userid;
246 $tu = & RatingsUserFactory::getUser($active_userid);
247 return $tu->get_rating($pagename, $this->_dimension);
250 function _getSortableValue($page_handle, &$revision_handle)
252 return $this->_getValue($page_handle, $revision_handle);
256 class _PageList_Column_prediction extends _PageList_Column
258 var $_active_ratings_user;
261 function _PageList_Column_prediction($params)
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());
268 $this->_pagelist =& $params[3];
269 $this->_PageList_Column($params[0], $params[1], $params[2]);
270 $this->_dimension = $this->_pagelist->getOption('dimension');
272 if (!$this->_dimension) $this->_dimension = 0;
273 $this->_users = $this->_pagelist->getOption('users');
276 function format($pagelist, $page_handle, &$revision_handle)
278 $pred = $this->_getValue($page_handle, $revision_handle);
279 $mean = $this->_active_ratings_user->mean_rating($this->_dimension);
280 $td = HTML::td($this->_tdattr);
283 if ($pred > 0 && abs($pred - $mean) >= 0.75)
284 $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
285 $div->pushContent($pred);
287 $td->pushContent($div);
292 function _getValue($page_handle, &$revision_handle)
294 $pagename = $page_handle->getName();
297 $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
298 return sprintf("%.1f", min(5, max(0, $pred)));
301 function _getSortableValue($page_handle, &$revision_handle)
303 return $this->_getValue($page_handle, $revision_handle);
307 class _PageList_Column_top3recs extends _PageList_Column_custom
310 var $_active_ratings_user;
313 function _PageList_Column_top3recs($params)
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);
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]);
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');
335 function _getValue($page_handle, &$revision_handle)
337 $ratings = $this->_active_ratings_user->get_ratings();
338 $iter = $page_handle->getLinks();
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);
350 if (count($recs) >= 3) {
353 // if <3 just show as many as there are
354 $numToShow = count($recs);
357 while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
362 $html->pushContent(" , ");
364 $html->pushContent(WikiLink($key));
368 if (count($recs) == 0 || $counter == 0) {
369 $html->pushContent(_("None"));
377 // register custom PageList type
379 $WikiTheme->addPageListColumn
383 => array('_PageList_Column_numbacklinks', 'custom:numbacklinks',
384 _("# things"), 'center'),
386 => array('_PageList_Column_ratingwidget', 'custom:rating',
389 => array('_PageList_Column_ratingvalue', 'custom:ratingvalue',
390 _("Rating"), 'center'),
392 => array('_PageList_Column_coagreement', 'custom:coagreement',
395 => array('_PageList_Column_minmisery', 'custom:minmisery',
396 _("MinMisery"), 'center'),
398 => array('_PageList_Column_averagerating', 'custom:averagerating',
399 _("Avg. Rating"), 'left'),
401 => array('_PageList_Column_top3recs', 'custom:top3recs',
402 _("Top Recommendations"), 'left'),
404 => array('_PageList_Column_prediction','custom:prediction',
405 _("Prediction"), false),*/
412 // c-hanging-comment-ender-p: nil
413 // indent-tabs-mode: nil