5 * Copyright 2004 Mike Cassano
7 * This file is part of PhpWiki.
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.
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.
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.
25 * wikilens specific Custom pagelist columns
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
34 require_once 'lib/PageList.php';
35 require_once 'lib/wikilens/RatingsUser.php';
36 require_once 'lib/plugin/RateIt.php';
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
45 class _PageList_Column_numbacklinks extends _PageList_Column_custom
47 function _getValue($page_handle, &$revision_handle)
49 $theIter = $page_handle->getBackLinks();
50 return $theIter->count();
53 function _getSortableValue($page_handle, &$revision_handle)
55 return $this->_getValue($page_handle, $revision_handle);
61 class _PageList_Column_coagreement extends _PageList_Column_custom
63 function _PageList_Column_coagreement($params)
65 $this->_pagelist =& $params[3];
66 $this->_PageList_Column($params[0], $params[1], $params[2]);
67 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
70 function _getValue($page_handle, &$revision_handle)
74 $pagename = $page_handle->getName();
76 $active_user = $request->getUser();
77 $active_userId = $active_user->getId();
78 $dbi = $request->getDbh();
79 $p = CoAgreement($dbi, $pagename, $this->_selectedBuddies, $active_userId);
89 //FIXME: $WikiTheme->getImageURL()
90 return HTML::img(array('src' => $WikiTheme->getImageURL($p)));
94 class _PageList_Column_minmisery extends _PageList_Column_custom
96 function _PageList_Column_minmisery($params)
98 $this->_pagelist =& $params[3];
99 $this->_PageList_Column($params[0], $params[1], $params[2]);
100 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
103 function _getValue($page_handle, &$revision_handle)
105 global $request, $WikiTheme;
107 $pagename = $page_handle->getName();
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)));
118 class _PageList_Column_averagerating extends _PageList_Column_custom
120 function _PageList_Column_averagerating($params)
122 $this->_pagelist =& $params[3];
123 $this->_PageList_Column($params[0], $params[1], $params[2]);
124 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
127 function _getValue($page_handle, &$revision_handle)
129 global $request, $WikiTheme;
131 $pagename = $page_handle->getName();
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);
138 $imgFix = floor($p * 2) / 2;
140 $html->pushContent(HTML::img(array('src' => $WikiTheme->getImageURL("Rateit" . $imgFix))));
141 $html->pushContent($p);
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.
153 class _PageList_Column_ratingvalue extends _PageList_Column
158 function _PageList_Column_ratingvalue($params)
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;
167 function format($pagelist, $page_handle, &$revision_handle)
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);
177 if ($rating != '-' && abs($rating - $mean) >= 0.75)
178 $div->setAttr('style', 'color: #' . ($rating > $mean ? '009900' : 'ff3333'));
179 $div->pushContent($rating);
181 $td->pushContent($div);
186 function _getValue($page_handle, &$revision_handle)
188 $pagename = $page_handle->getName();
191 $rating = $tu->get_rating($pagename, $this->_dimension);
193 // a dash (or *something*) arguably looks better than a big blank space
194 return ($rating ? $rating : "-");
197 function hasNoRatings($pages)
200 $use = & $this->_user;
201 foreach ($pages as $page) {
202 if ($use->get_rating($page, $this->_dimension)) {
213 function _getSortableValue($page_handle, &$revision_handle)
215 return $this->_getValue($page_handle, $revision_handle);
222 * Ratings widget for the logged-in user and the given page
223 * This uses the column name "rating".
225 class _PageList_Column_ratingwidget extends _PageList_Column_custom
227 function _PageList_Column_ratingwidget($params)
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;
235 function format($pagelist, $page_handle, &$revision_handle)
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');
245 function _getValue($page_handle, &$revision_handle)
249 $pagename = $page_handle->getName();
250 $active_user = $request->getUser();
251 $active_userid = $active_user->_userid;
253 $tu = & RatingsUserFactory::getUser($active_userid);
254 return $tu->get_rating($pagename, $this->_dimension);
257 function _getSortableValue($page_handle, &$revision_handle)
259 return $this->_getValue($page_handle, $revision_handle);
265 class _PageList_Column_prediction extends _PageList_Column
267 var $_active_ratings_user;
270 function _PageList_Column_prediction($params)
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());
277 $this->_pagelist =& $params[3];
278 $this->_PageList_Column($params[0], $params[1], $params[2]);
279 $this->_dimension = $this->_pagelist->getOption('dimension');
281 if (!$this->_dimension) $this->_dimension = 0;
282 $this->_users = $this->_pagelist->getOption('users');
285 function format($pagelist, $page_handle, &$revision_handle)
287 $pred = $this->_getValue($page_handle, $revision_handle);
288 $mean = $this->_active_ratings_user->mean_rating($this->_dimension);
289 $td = HTML::td($this->_tdattr);
292 if ($pred > 0 && abs($pred - $mean) >= 0.75)
293 $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
294 $div->pushContent($pred);
296 $td->pushContent($div);
301 function _getValue($page_handle, &$revision_handle)
303 $pagename = $page_handle->getName();
306 $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
307 return sprintf("%.1f", min(5, max(0, $pred)));
310 function _getSortableValue($page_handle, &$revision_handle)
312 return $this->_getValue($page_handle, $revision_handle);
318 class _PageList_Column_top3recs extends _PageList_Column_custom
321 var $_active_ratings_user;
324 function _PageList_Column_top3recs($params)
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);
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]);
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');
346 function _getValue($page_handle, &$revision_handle)
348 $ratings = $this->_active_ratings_user->get_ratings();
349 $iter = $page_handle->getLinks();
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);
361 if (count($recs) >= 3) {
364 // if <3 just show as many as there are
365 $numToShow = count($recs);
368 while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
373 $html->pushContent(" , ");
375 $html->pushContent(WikiLink($key));
379 if (count($recs) == 0 || $counter == 0) {
380 $html->pushContent(_("None"));
390 // register custom PageList type
392 $WikiTheme->addPageListColumn
396 => array('_PageList_Column_numbacklinks', 'custom:numbacklinks',
397 _("# things"), 'center'),
399 => array('_PageList_Column_ratingwidget', 'custom:rating',
402 => array('_PageList_Column_ratingvalue', 'custom:ratingvalue',
403 _("Rating"), 'center'),
405 => array('_PageList_Column_coagreement', 'custom:coagreement',
408 => array('_PageList_Column_minmisery', 'custom:minmisery',
409 _("MinMisery"), 'center'),
411 => array('_PageList_Column_averagerating', 'custom:averagerating',
412 _("Avg. Rating"), 'left'),
414 => array('_PageList_Column_top3recs', 'custom:top3recs',
415 _("Top Recommendations"), 'left'),
417 => array('_PageList_Column_prediction','custom:prediction',
418 _("Prediction"), false),*/
425 // c-hanging-comment-ender-p: nil
426 // indent-tabs-mode: nil