2 rcs_id('$Id: PageListColumns.php,v 1.6 2004-07-08 20:30:07 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");
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.
43 class _PageList_Column_numbacklinks extends _PageList_Column_custom
45 function _getValue ($page_handle, &$revision_handle)
47 //return $page_handle->getNumLinks();
48 $theIter = $page_handle->getBackLinks();
50 while($curr = $theIter->next()){
56 function _getSortableValue ($page_handle, &$revision_handle) {
57 return $this->_getValue($page_handle, &$revision_handle);
61 class _PageList_Column_coagreement extends _PageList_Column_custom
63 function _PageList_Column_coagreement ($params) {
64 $this->_pagelist =& $params[3];
65 $this->_PageList_Column($params[0], $params[1], $params[2]);
66 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
69 function _getValue ($page_handle, &$revision_handle)
73 $pagename = $page_handle->getName();
75 $active_user = $request->getUser();
76 $active_userId = $active_user->getId();
77 $dbi = $request->getDbh();
78 $p = CoAgreement($dbi, $pagename, $this->_selectedBuddies, $active_userId);
88 //FIXME: $WikiTheme->getImageURL()
89 return HTML::img(array('src' => "../images/" . $p . ".gif"));
93 class _PageList_Column_minmisery extends _PageList_Column_custom
95 function _PageList_Column_minmisery ($params) {
96 $this->_pagelist =& $params[3];
97 $this->_PageList_Column($params[0], $params[1], $params[2]);
98 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
101 function _getValue ($page_handle, &$revision_handle)
105 $pagename = $page_handle->getName();
107 $active_user = $request->getUser();
108 $active_userId = $active_user->getId();
109 $dbi = $request->getDbh();
110 $p = MinMisery($dbi, $pagename, $this->_selectedBuddies, $active_userId);
111 $imgFix = floor($p * 2) / 2;
112 //FIXME: $WikiTheme->getImageURL()
113 return HTML::img(array('src' => "../images/" . $imgFix . ".png"));
117 class _PageList_Column_averagerating extends _PageList_Column_custom
119 function init ($field, $default_heading, $align, &$pagelist)
121 $this->_PageList_Column($field, $default_heading, $align);
122 $this->_selectedBuddies = $pagelist->getOption('selectedBuddies');
125 function _getValue ($page_handle, &$revision_handle)
129 $pagename = $page_handle->getName();
131 $active_user = $request->getUser();
132 $active_userId = $active_user->getId();
133 $dbi = $request->getDbh();
134 $p = round(AverageRating($dbi, $pagename, $this->_selectedBuddies, $active_userId), 2);
136 $imgFix = floor($p * 2) / 2;
138 //FIXME: $WikiTheme->getImageURL()
139 $html->pushContent(HTML::img(array('src' => "../images/" . $imgFix . ".png")));
140 $html->pushContent($p);
146 * Show the value of a rating as a digit (or "-" if no value), given the
147 * user who is the rater.
149 class _PageList_Column_ratingvalue extends _PageList_Column {
153 function _PageList_Column_ratingvalue ($params) {
154 $this->_pagelist =& $params[3];
155 $this->_user =& $params[4];//$this->_pagelist->getOption('user');
156 assert(!empty($this->_user));
157 $this->_PageList_Column($params[0], $params[1], $params[2]);
158 $this->_dimension = $this->_pagelist->getOption('dimension');
161 function format ($pagelist, $page_handle, &$revision_handle)
163 $rating = $this->_getValue($page_handle, $revision_handle);
164 $mean = $this->_user->mean_rating($this->_dimension);
165 $td = HTML::td($this->_tdattr);
168 if($rating != '-' && abs($rating - $mean) >= 0.75)
169 $div->setAttr('style', 'color: #' . ($rating > $mean ? '009900' : 'ff3333'));
170 $div->pushContent($rating);
172 $td->pushContent($div);
177 function _getValue ($page_handle, &$revision_handle)
179 $pagename = $page_handle->getName();
181 $tu = & $this->_user;
182 $rating = $tu->get_rating($pagename, $this->_dimension);
184 // a dash (or *something*) arguably looks better than a big blank space
185 return ($rating ? $rating : "-");
188 function hasNoRatings($pages)
191 $use = & $this->_user;
192 foreach ($pages as $page) {
193 if ($use->get_rating($page, $this->_dimension)) {
204 function _getSortableValue ($page_handle, &$revision_handle) {
205 return $this->_getValue($page_handle, &$revision_handle);
210 * Ratings widget for the logged-in user and the given page
212 class _PageList_Column_ratingwidget extends _PageList_Column_custom
214 function _PageList_Column_ratingwidget ($params) {
215 $this->_pagelist =& $params[3];
216 $this->_PageList_Column($params[0], $params[1], $params[2]);
217 $this->_dimension = $this->_pagelist->getOption('dimension');
220 function format ($pagelist, $page_handle, &$revision_handle) {
221 $widget = WikiPlugin_RateIt::RatingWidgetHtml($page_handle->getName(), "", "pagerat", $this->_dimension, "small");
222 $td = HTML::td($widget);
223 $td->setAttr('nowrap', 'nowrap');
227 function _getValue ($page_handle, &$revision_handle)
231 $pagename = $page_handle->getName();
232 $active_user = $request->getUser();
233 $active_userid = $active_user->_userid;
235 $tu = & RatingsUserFactory::getUser($active_userid);
236 return $tu->get_rating($pagename, $this->_dimension);
239 function _getSortableValue ($page_handle, &$revision_handle) {
240 return $this->_getValue($page_handle, &$revision_handle);
244 class _PageList_Column_prediction extends _PageList_Column
246 var $_active_ratings_user;
249 function _PageList_Column_prediction ($params)
252 $active_user = $request->getUser();
253 // This needs to be a reference so things aren't recomputed for this user
254 $this->_active_ratings_user =& RatingsUserFactory::getUser($active_user->getId());
256 $this->_pagelist =& $params[3];
257 $this->_PageList_Column($params[0], $params[1], $params[2]);
258 $this->_dimension = $this->_pagelist->getOption('dimension');;
259 $this->_users = $this->_pagelist->getOption('users');
262 function format ($pagelist, $page_handle, &$revision_handle) {
263 $pred = $this->_getValue($page_handle, $revision_handle);
264 $mean = $this->_active_ratings_user->mean_rating($this->_dimension);
265 $td = HTML::td($this->_tdattr);
268 if($pred > 0 && abs($pred - $mean) >= 0.75)
269 $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
270 $div->pushContent($pred);
272 $td->pushContent($div);
277 function _getValue ($page_handle, &$revision_handle)
279 $pagename = $page_handle->getName();
282 $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
283 return sprintf("%.1f", min(5, max(0, $pred)));
286 function _getSortableValue ($page_handle, &$revision_handle) {
287 return $this->_getValue($page_handle, &$revision_handle);
291 class _PageList_Column_top3recs extends _PageList_Column_custom
294 var $_active_ratings_user;
297 function _PageList_Column_top3recs ($params) {
299 $active_user = $request->getUser();
300 if (is_string($active_user)) {
301 //FIXME: try to find the bug at test.php which sets request->_user and ->_group
302 trigger_error("request->getUser => string: $active_user", E_USER_WARNING);
303 $active_user = new MockUser($active_user,true);
305 // No, I don't know exactly why, but this needs to be a reference for
306 // the memoization in pearson_similarity and mean_rating to work
307 $this->_active_ratings_user =& new RatingsUser($active_user->getId());
308 $this->_PageList_Column($params[0], $params[1], $params[2]);
310 if (!empty($params[3])) {
311 $this->_pagelist =& $params[3];
312 $this->_dimension = $this->_pagelist->getOption('dimension');
313 $this->_users = $this->_pagelist->getOption('users');
317 function _getValue ($page_handle, &$revision_handle)
319 $ratings = $this->_active_ratings_user->get_ratings();
320 $iter = $page_handle->getLinks();
322 while($current = $iter->next()) {
323 //filter out already rated
324 if (!$this->_active_ratings_user->get_rating($current->getName(), $this->_dimension)) {
325 $recs[$current->getName()] =
326 $this->_active_ratings_user->knn_uu_predict($current->getName(),
327 $this->_users, $this->_dimension);
332 if (count($recs) >= 3){
336 // if <3 just show as many as there are
337 $numToShow = count($recs);
340 while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
345 $html->pushContent(" , ");
347 $html->pushContent(WikiLink($key));
351 if (count($recs) == 0 || $counter == 0){
352 $html->pushContent(_("None"));
360 // register custom PageList type
362 $WikiTheme->addPageListColumn
366 => array('_PageList_Column_numbacklinks','custom:numbacklinks', _("# things"), false),
368 => array('_PageList_Column_ratingwidget','custom:rating', _("Rate"), false),
370 => array('_PageList_Column_coagreement','custom:coagreement', _("Go?"), 'center'),
372 => array('_PageList_Column_minmisery','custom:minmisery', _("MinMisery"), 'center'),
374 => array('_PageList_Column_averagerating','custom:averagerating', _("Avg. Rating"), 'left'),
376 => array('_PageList_Column_top3recs','custom:top3recs', _("Top Recommendations"), 'left'),
379 // $Log: not supported by cvs2svn $
380 // Revision 1.5 2004/07/07 15:01:44 dfrankow
381 // Allow ratingvalue, ratingwidget, prediction, numbacklinks columns to be sortable
383 // Revision 1.4 2004/06/30 20:12:09 dfrankow
384 // + Change numbacklinks function to use existing core functions.
385 // It's slower, but it'll work.
387 // + Change ratingvalue column to get its specific user as column 5 (index 4).
389 // + ratingwidget column uses WikiPlugin_RateIt's RatingWidgetHtml
391 // Revision 1.3 2004/06/21 17:01:41 rurban
392 // fix typo and rating method call
394 // Revision 1.2 2004/06/21 16:22:32 rurban
395 // add DEFAULT_DUMP_DIR and HTML_DUMP_DIR constants, for easier cmdline dumps,
396 // fixed dumping buttons locally (images/buttons/),
397 // support pages arg for dumphtml,
398 // optional directory arg for dumpserial + dumphtml,
399 // fix a AllPages warning,
400 // show dump warnings/errors on DEBUG,
401 // don't warn just ignore on wikilens pagelist columns, if not loaded.
402 // RateIt pagelist column is called "rating", not "ratingwidget" (Dan?)
404 // Revision 1.1 2004/06/18 14:42:17 rurban
405 // added wikilens libs (not yet merged good enough, some work for DanFr)
412 // c-hanging-comment-ender-p: nil
413 // indent-tabs-mode: nil