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 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;
164 function format($pagelist, $page_handle, &$revision_handle)
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 "BStar", $this->_dimension, "small");
233 $td = HTML::td($widget);
234 $td->setAttr('nowrap', 'nowrap');
238 function _getValue($page_handle, &$revision_handle)
240 // Returns average rating of a page
241 $pagename = $page_handle->getName();
242 $rdbi = RatingsDb::getTheRatingsDb();
243 return $rdbi->getAvg($pagename, $this->_dimension);
246 function _getSortableValue($page_handle, &$revision_handle)
248 return $this->_getValue($page_handle, $revision_handle);
252 class _PageList_Column_prediction extends _PageList_Column
254 public $_active_ratings_user;
257 function _PageList_Column_prediction($params)
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());
264 $this->_pagelist =& $params[3];
265 $this->_PageList_Column($params[0], $params[1], $params[2]);
266 $this->_dimension = $this->_pagelist->getOption('dimension');
268 if (!$this->_dimension) $this->_dimension = 0;
269 $this->_users = $this->_pagelist->getOption('users');
272 function format($pagelist, $page_handle, &$revision_handle)
274 $pred = $this->_getValue($page_handle, $revision_handle);
275 $mean = $this->_active_ratings_user->mean_rating($this->_dimension);
276 $td = HTML::td($this->_tdattr);
279 if ($pred > 0 && abs($pred - $mean) >= 0.75)
280 $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
281 $div->pushContent($pred);
283 $td->pushContent($div);
288 function _getValue($page_handle, &$revision_handle)
290 $pagename = $page_handle->getName();
293 $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
294 return sprintf("%.1f", min(5, max(0, $pred)));
297 function _getSortableValue($page_handle, &$revision_handle)
299 return $this->_getValue($page_handle, $revision_handle);
303 class _PageList_Column_top3recs extends _PageList_Column_custom
306 public $_active_ratings_user;
309 function _PageList_Column_top3recs($params)
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);
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]);
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');
331 function _getValue($page_handle, &$revision_handle)
333 $ratings = $this->_active_ratings_user->get_ratings();
334 $iter = $page_handle->getLinks();
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);
346 if (count($recs) >= 3) {
349 // if <3 just show as many as there are
350 $numToShow = count($recs);
353 while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
358 $html->pushContent(" , ");
360 $html->pushContent(WikiLink($key));
364 if (count($recs) == 0 || $counter == 0) {
365 $html->pushContent(_("None"));
373 // register custom PageList type
375 $WikiTheme->addPageListColumn
379 => array('_PageList_Column_numbacklinks', 'custom:numbacklinks',
380 _("# things"), 'center'),
382 => array('_PageList_Column_ratingwidget', 'custom:rating',
385 => array('_PageList_Column_ratingvalue', 'custom:ratingvalue',
386 _("Rating"), 'center'),
388 => array('_PageList_Column_coagreement', 'custom:coagreement',
391 => array('_PageList_Column_minmisery', 'custom:minmisery',
392 _("MinMisery"), 'center'),
394 => array('_PageList_Column_averagerating', 'custom:averagerating',
395 _("Avg. Rating"), 'left'),
397 => array('_PageList_Column_top3recs', 'custom:top3recs',
398 _("Top Recommendations"), 'left'),
400 => array('_PageList_Column_prediction','custom:prediction',
401 _("Prediction"), false),*/
408 // c-hanging-comment-ender-p: nil
409 // indent-tabs-mode: nil