2 rcs_id('$Id: PageListColumns.php,v 1.2 2004-06-21 16:22:32 rurban Exp $');
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
20 along with PhpWiki; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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");
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.
42 class _PageList_Column_numbacklinks extends _PageList_Column_custom
44 function _getValue ($page_handle, &$revision_handle)
46 return $page_handle->getNumLinks();
50 class _PageList_Column_coagreement extends _PageList_Column_custom
52 function _PageList_Column_coagreement ($params) {
53 $this->_pagelist =& $params[3];
54 $this->_PageList_Column($params[0], $params[1], $params[2]);
55 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
58 function _getValue ($page_handle, &$revision_handle)
62 $pagename = $page_handle->getName();
64 $active_user = $request->getUser();
65 $active_userId = $active_user->getId();
66 $dbi = $request->getDbh();
67 $p = CoAgreement($dbi, $pagename, $this->_selectedBuddies, $active_userId);
77 //FIXME: $WikiTheme->getImageURL()
78 return HTML::img(array('src' => "../images/" . $p . ".gif"));
82 class _PageList_Column_minmisery extends _PageList_Column_custom
84 function _PageList_Column_minmisery ($params) {
85 $this->_pagelist =& $params[3];
86 $this->_PageList_Column($params[0], $params[1], $params[2]);
87 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
90 function _getValue ($page_handle, &$revision_handle)
94 $pagename = $page_handle->getName();
96 $active_user = $request->getUser();
97 $active_userId = $active_user->getId();
98 $dbi = $request->getDbh();
99 $p = MinMisery($dbi, $pagename, $this->_selectedBuddies, $active_userId);
100 $imgFix = floor($p * 2) / 2;
101 //FIXME: $WikiTheme->getImageURL()
102 return HTML::img(array('src' => "../images/" . $imgFix . ".png"));
106 class _PageList_Column_averagerating extends _PageList_Column_custom
108 function init ($field, $default_heading, $align, &$pagelist)
110 $this->_PageList_Column($field, $default_heading, $align);
111 $this->_selectedBuddies = $pagelist->getOption('selectedBuddies');
114 function _getValue ($page_handle, &$revision_handle)
118 $pagename = $page_handle->getName();
120 $active_user = $request->getUser();
121 $active_userId = $active_user->getId();
122 $dbi = $request->getDbh();
123 $p = round(AverageRating($dbi, $pagename, $this->_selectedBuddies, $active_userId), 2);
125 $imgFix = floor($p * 2) / 2;
127 //FIXME: $WikiTheme->getImageURL()
128 $html->pushContent(HTML::img(array('src' => "../images/" . $imgFix . ".png")));
129 $html->pushContent($p);
135 * Show the value of a rating as a digit (or "-" if no value), given the
136 * user who is the rater.
138 class _PageList_Column_ratingvalue extends _PageList_Column {
142 function _PageList_Column_ratingvalue ($params) {
143 $this->_pagelist =& $params[3];
144 $this->_user = $this->_pagelist->getOption('user');
145 assert(!empty($this->_user));
146 $this->_PageList_Column($params[0], $params[1], $params[2]);
147 $this->_dimension = $this->_pagelist->getOption('dimension');;
150 function format ($pagelist, $page_handle, &$revision_handle)
152 $rating = $this->_getValue($page_handle, $revision_handle);
153 $mean = $this->_user->mean_rating($this->_dimension);
154 $td = HTML::td($this->_tdattr);
157 if($rating != '-' && abs($rating - $mean) >= 0.75)
158 $div->setAttr('style', 'color: #' . ($rating > $mean ? '009900' : 'ff3333'));
159 $div->pushContent($rating);
161 $td->pushContent($div);
166 function _getValue ($page_handle, &$revision_handle)
168 $pagename = $page_handle->getName();
170 $tu = & $this->_user;
171 $rating = $tu->get_rating($pagename, $this->_dimension);
173 // a dash (or *something*) arguably looks better than a big blank space
174 return ($rating ? $rating : "-");
177 function hasNoRatings($pages)
180 $use = & $this->_user;
181 foreach ($pages as $page) {
182 if ($use->get_rating($page, $this->_dimension)) {
195 * Ratings widget for the logged-in user and the given page
197 class _PageList_Column_ratingwidget extends _PageList_Column_custom
199 function _PageList_Column_ratingwidget ($params) {
200 $this->_pagelist =& $params[3];
201 $this->_PageList_Column($params[0], $params[1], $params[2]);
202 $this->_dimension = $this->_pagelist->getOption('dimension');
205 function format ($pagelist, $page_handle, &$revision_handle) {
206 $widget = RatingWidgetHtml($page_handle->getName(), "", "pagerat", $this->_dimension, "small");
207 $td = HTML::td($widget);
208 $td->setAttr('nowrap', 'nowrap');
212 function _getValue ($page_handle, &$revision_handle)
216 $pagename = $page_handle->getName();
217 $active_user = $request->getUser();
218 $active_userid = $active_user->_userid;
220 $tu = & RatingsUserFactory::getUser($active_userid);
221 return $tu->get_rating($pagename, $this->_dimension);
225 class _PageList_Column_prediction extends _PageList_Column
227 var $_active_ratings_user;
230 function _PageList_Column_prediction ($params)
233 $active_user = $request->getUser();
234 // This needs to be a reference so things aren't recomputed for this user
235 $this->_active_ratings_user =& RatingsUserFactory::getUser($active_user->getId());
236 $this->_pagelist =& $params[3];
237 $this->_PageList_Column($params[0], $params[1], $params[2]);
238 $this->_dimension = $this->_pagelist->getOption('dimension');;
239 $this->_users = $this->_pagelist->getOption('users');
242 function format ($pagelist, $page_handle, &$revision_handle) {
243 $pred = $this->_getValue($page_handle, $revision_handle);
244 $mean = $this->_active_ratings_user->mean_rating($this->_dimension);
245 $td = HTML::td($this->_tdattr);
248 if($pred > 0 && abs($pred - $mean) >= 0.75)
249 $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
250 $div->pushContent($pred);
252 $td->pushContent($div);
257 function _getValue ($page_handle, &$revision_handle)
259 $pagename = $page_handle->getName();
262 $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
263 return sprintf("%.1f", min(5, max(0, $pred)));
267 class _PageList_Column_top3recs extends _PageList_Column_custom
270 var $_active_ratings_user;
273 function _PageList_Column_top3recs ($params) {
275 $active_user = $request->getUser();
276 // No, I don't know exactly why, but this needs to be a reference for
277 // the memoization in pearson_similarity and mean_rating to work
278 $this->_active_ratings_user =& new RatingsUser($active_user->getId());
279 $this->_PageList_Column($params[0], $params[1], $params[2]);
280 if (!empty($params[3])) {
281 $this->_pagelist =& $params[3];
282 $this->_dimension = $this->_pagelist->getOption('dimension');;
283 $this->_users = $this->_pagelist->getOption('users');
287 function _getValue ($page_handle, &$revision_handle)
289 $ratings = $this->_active_ratings_user->get_ratings();
291 $iter = $page_handle->getLinks();
293 while($current = $iter->next()) {
294 //filter out already rated
295 if (!$this->_active_ratings_user->get_rating($current->getName(), $this->_dimension)) {
296 $recs[$current->getName()] =
297 $this->_active_ratings_user->knn_uu_predict($current->getName(),
298 $this->_users, $this->_dimension);
303 if (count($recs) >= 3){
307 // if <3 just show as many as there are
308 $numToShow = count($recs);
311 while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
316 $html->pushContent(" , ");
318 $html->pushContent(WikiLink($key));
322 if (count($recs) == 0 || $counter == 0){
323 $html->pushContent(_("None"));
331 // register custom PageList type
332 $WikiTheme->addPageListColumn
336 => array('_PageList_Column_numbacklinks','custom:numbacklinks', _("# things"), false),
338 => array('_PageList_Column_ratingwidget','custom:rating', _("Rate"), false),
340 => array('_PageList_Column_coagreement','custom:coagreement', _("Go?"), 'center'),
342 => array('_PageList_Column_minmisery','custom:minmisery', _("MinMisery"), 'center'),
344 => array('_PageList_Column_averagerating','custom:averagerating', _("Avg. Rating"), 'left'),
346 => array('_PageList_Column_top3recs','custom:top3recs', _("Top Recommendations"), 'left'),
349 // $Log: not supported by cvs2svn $
350 // Revision 1.1 2004/06/18 14:42:17 rurban
351 // added wikilens libs (not yet merged good enough, some work for DanFr)
358 // c-hanging-comment-ender-p: nil
359 // indent-tabs-mode: nil