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.
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) {
48 $theIter = $page_handle->getBackLinks();
49 return $theIter->count();
52 function _getSortableValue ($page_handle, &$revision_handle) {
53 return $this->_getValue($page_handle, $revision_handle);
57 class _PageList_Column_coagreement extends _PageList_Column_custom
59 function _PageList_Column_coagreement ($params) {
60 $this->_pagelist =& $params[3];
61 $this->_PageList_Column($params[0], $params[1], $params[2]);
62 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
65 function _getValue ($page_handle, &$revision_handle)
69 $pagename = $page_handle->getName();
71 $active_user = $request->getUser();
72 $active_userId = $active_user->getId();
73 $dbi = $request->getDbh();
74 $p = CoAgreement($dbi, $pagename, $this->_selectedBuddies, $active_userId);
84 //FIXME: $WikiTheme->getImageURL()
85 return HTML::img(array('src' => $WikiTheme->getImageURL($p)));
89 class _PageList_Column_minmisery extends _PageList_Column_custom
91 function _PageList_Column_minmisery ($params) {
92 $this->_pagelist =& $params[3];
93 $this->_PageList_Column($params[0], $params[1], $params[2]);
94 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
97 function _getValue ($page_handle, &$revision_handle)
99 global $request, $WikiTheme;
101 $pagename = $page_handle->getName();
103 $active_user = $request->getUser();
104 $active_userId = $active_user->getId();
105 $dbi = $request->getDbh();
106 $p = MinMisery($dbi, $pagename, $this->_selectedBuddies, $active_userId);
107 $imgFix = floor($p * 2) / 2;
108 return HTML::img(array('src' => $WikiTheme->getImageURL("Rateit" . $imgFix)));
112 class _PageList_Column_averagerating extends _PageList_Column_custom
114 function _PageList_Column_averagerating ($params)
116 $this->_pagelist =& $params[3];
117 $this->_PageList_Column($params[0], $params[1], $params[2]);
118 $this->_selectedBuddies = $this->_pagelist->getOption('selectedBuddies');
121 function _getValue ($page_handle, &$revision_handle)
123 global $request, $WikiTheme;
125 $pagename = $page_handle->getName();
127 $active_user = $request->getUser();
128 $active_userId = $active_user->getId();
129 $dbi = $request->getDbh();
130 $p = round(AverageRating($dbi, $pagename, $this->_selectedBuddies, $active_userId), 2);
132 $imgFix = floor($p * 2) / 2;
134 $html->pushContent(HTML::img(array('src' => $WikiTheme->getImageURL("Rateit" . $imgFix))));
135 $html->pushContent($p);
141 * Show the value of a rating as a digit (or "-" if no value), given the
142 * user who is the rater.
143 * This requires the RatingsUser as 5th paramater.
145 class _PageList_Column_ratingvalue extends _PageList_Column {
149 function _PageList_Column_ratingvalue ($params) {
150 $this->_pagelist =& $params[3];
151 $this->_user =& $params[4];//$this->_pagelist->getOption('user');
152 $this->_PageList_Column($params[0], $params[1], $params[2]);
153 $this->_dimension = $this->_pagelist->getOption('dimension');
154 if (!$this->_dimension) $this->_dimension = 0;
157 function format ($pagelist, $page_handle, &$revision_handle)
159 if (empty($this->_user))
160 $this->_user =& RatingsUserFactory::getUser($GLOBALS['request']->_user->_userid);
161 assert(!empty($this->_user));
162 $rating = $this->_getValue($page_handle, $revision_handle);
163 $mean = $this->_user->mean_rating($this->_dimension);
164 $td = HTML::td($this->_tdattr);
167 if($rating != '-' && abs($rating - $mean) >= 0.75)
168 $div->setAttr('style', 'color: #' . ($rating > $mean ? '009900' : 'ff3333'));
169 $div->pushContent($rating);
171 $td->pushContent($div);
176 function _getValue ($page_handle, &$revision_handle)
178 $pagename = $page_handle->getName();
181 $rating = $tu->get_rating($pagename, $this->_dimension);
183 // a dash (or *something*) arguably looks better than a big blank space
184 return ($rating ? $rating : "-");
187 function hasNoRatings($pages)
190 $use = & $this->_user;
191 foreach ($pages as $page) {
192 if ($use->get_rating($page, $this->_dimension)) {
203 function _getSortableValue ($page_handle, &$revision_handle) {
204 return $this->_getValue($page_handle, $revision_handle);
209 * Ratings widget for the logged-in user and the given page
210 * This uses the column name "rating".
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');
218 if (!$this->_dimension) $this->_dimension = 0;
221 function format ($pagelist, $page_handle, &$revision_handle) {
222 $plugin = new WikiPlugin_RateIt();
223 $widget = $plugin->RatingWidgetHtml($page_handle->getName(), "",
224 "Star", $this->_dimension, "small");
225 $td = HTML::td($widget);
226 $td->setAttr('nowrap', 'nowrap');
230 function _getValue ($page_handle, &$revision_handle)
234 $pagename = $page_handle->getName();
235 $active_user = $request->getUser();
236 $active_userid = $active_user->_userid;
238 $tu = & RatingsUserFactory::getUser($active_userid);
239 return $tu->get_rating($pagename, $this->_dimension);
242 function _getSortableValue ($page_handle, &$revision_handle) {
243 return $this->_getValue($page_handle, $revision_handle);
247 class _PageList_Column_prediction extends _PageList_Column
249 var $_active_ratings_user;
252 function _PageList_Column_prediction ($params)
255 $active_user = $request->getUser();
256 // This needs to be a reference so things aren't recomputed for this user
257 $this->_active_ratings_user =& RatingsUserFactory::getUser($active_user->getId());
259 $this->_pagelist =& $params[3];
260 $this->_PageList_Column($params[0], $params[1], $params[2]);
261 $this->_dimension = $this->_pagelist->getOption('dimension');;
262 if (!$this->_dimension) $this->_dimension = 0;
263 $this->_users = $this->_pagelist->getOption('users');
266 function format ($pagelist, $page_handle, &$revision_handle) {
267 $pred = $this->_getValue($page_handle, $revision_handle);
268 $mean = $this->_active_ratings_user->mean_rating($this->_dimension);
269 $td = HTML::td($this->_tdattr);
272 if($pred > 0 && abs($pred - $mean) >= 0.75)
273 $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
274 $div->pushContent($pred);
276 $td->pushContent($div);
281 function _getValue ($page_handle, &$revision_handle)
283 $pagename = $page_handle->getName();
286 $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
287 return sprintf("%.1f", min(5, max(0, $pred)));
290 function _getSortableValue ($page_handle, &$revision_handle) {
291 return $this->_getValue($page_handle, $revision_handle);
295 class _PageList_Column_top3recs extends _PageList_Column_custom
298 var $_active_ratings_user;
301 function _PageList_Column_top3recs ($params) {
303 $active_user = $request->getUser();
304 if (is_string($active_user)) {
305 //FIXME: try to find the bug at test.php which sets request->_user and ->_group
306 trigger_error("request->getUser => string: $active_user", E_USER_WARNING);
307 $active_user = new MockUser($active_user,true);
309 // No, I don't know exactly why, but this needs to be a reference for
310 // the memoization in pearson_similarity and mean_rating to work
311 $this->_active_ratings_user = new RatingsUser($active_user->getId());
312 $this->_PageList_Column($params[0], $params[1], $params[2]);
314 if (!empty($params[3])) {
315 $this->_pagelist =& $params[3];
316 $this->_dimension = $this->_pagelist->getOption('dimension');
317 if (!$this->_dimension) $this->_dimension = 0;
318 $this->_users = $this->_pagelist->getOption('users');
322 function _getValue ($page_handle, &$revision_handle)
324 $ratings = $this->_active_ratings_user->get_ratings();
325 $iter = $page_handle->getLinks();
327 while($current = $iter->next()) {
328 //filter out already rated
329 if (!$this->_active_ratings_user->get_rating($current->getName(), $this->_dimension)) {
330 $recs[$current->getName()] =
331 $this->_active_ratings_user->knn_uu_predict($current->getName(),
332 $this->_users, $this->_dimension);
337 if (count($recs) >= 3){
341 // if <3 just show as many as there are
342 $numToShow = count($recs);
345 while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
350 $html->pushContent(" , ");
352 $html->pushContent(WikiLink($key));
356 if (count($recs) == 0 || $counter == 0){
357 $html->pushContent(_("None"));
365 // register custom PageList type
367 $WikiTheme->addPageListColumn
371 => array('_PageList_Column_numbacklinks','custom:numbacklinks',
372 _("# things"), 'center'),
374 => array('_PageList_Column_ratingwidget','custom:rating',
377 => array('_PageList_Column_ratingvalue','custom:ratingvalue',
378 _("Rating"), 'center'),
380 => array('_PageList_Column_coagreement','custom:coagreement',
383 => array('_PageList_Column_minmisery','custom:minmisery',
384 _("MinMisery"), 'center'),
386 => array('_PageList_Column_averagerating','custom:averagerating',
387 _("Avg. Rating"), 'left'),
389 => array('_PageList_Column_top3recs','custom:top3recs',
390 _("Top Recommendations"), 'left'),
392 => array('_PageList_Column_prediction','custom:prediction',
393 _("Prediction"), false),*/
400 // c-hanging-comment-ender-p: nil
401 // indent-tabs-mode: nil