]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/wikilens/PageListColumns.php
add DEFAULT_DUMP_DIR and HTML_DUMP_DIR constants, for easier cmdline dumps,
[SourceForge/phpwiki.git] / lib / wikilens / PageListColumns.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageListColumns.php,v 1.2 2004-06-21 16:22:32 rurban Exp $');
3
4 /*
5  Copyright 2004 Mike Cassano
6
7  This file is part of PhpWiki.
8
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.
13
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.
18
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
22  */
23
24 /**
25  * wikilens specific Custom pagelist columns
26  *
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
32  */
33
34 require_once('lib/PageList.php');
35 require_once("lib/wikilens/RatingsUser.php");
36
37 /**
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  */
42 class _PageList_Column_numbacklinks extends _PageList_Column_custom
43 {
44     function _getValue ($page_handle, &$revision_handle) 
45     {
46         return $page_handle->getNumLinks();
47     }
48 };
49
50 class _PageList_Column_coagreement extends _PageList_Column_custom 
51 {
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');
56     }
57
58     function _getValue ($page_handle, &$revision_handle) 
59     {
60         global $request;
61
62         $pagename = $page_handle->getName();
63
64         $active_user = $request->getUser();
65         $active_userId = $active_user->getId();
66         $dbi = $request->getDbh();      
67         $p = CoAgreement($dbi, $pagename, $this->_selectedBuddies, $active_userId);
68         if($p == 1){
69             $p = "yes";
70         } elseif($p == 0){
71             $p = "unsure";
72         } elseif($p == -1){
73             $p = "no";  
74         } else {
75             $p = "error";       
76         }   
77         //FIXME: $WikiTheme->getImageURL()
78         return HTML::img(array('src' => "../images/" . $p . ".gif"));
79     }
80 }
81
82 class _PageList_Column_minmisery extends _PageList_Column_custom 
83 {
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');
88     }
89
90     function _getValue ($page_handle, &$revision_handle) 
91     {
92         global $request;
93
94         $pagename = $page_handle->getName();
95
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"));
103     }
104 }
105
106 class _PageList_Column_averagerating extends _PageList_Column_custom
107 {
108     function init ($field, $default_heading, $align, &$pagelist) 
109     {
110         $this->_PageList_Column($field, $default_heading, $align);
111         $this->_selectedBuddies = $pagelist->getOption('selectedBuddies');
112     }
113
114     function _getValue ($page_handle, &$revision_handle) 
115     {
116         global $request;
117
118         $pagename = $page_handle->getName();
119
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);
124       
125         $imgFix = floor($p * 2) / 2;
126         $html = HTML();
127         //FIXME: $WikiTheme->getImageURL()
128         $html->pushContent(HTML::img(array('src' => "../images/" . $imgFix . ".png")));
129         $html->pushContent($p);
130         return $html;
131     }
132 };
133  
134 /**
135  * Show the value of a rating as a digit (or "-" if no value), given the
136  * user who is the rater.
137  */
138 class _PageList_Column_ratingvalue extends _PageList_Column {
139     var $_user;
140     var $_dimension;
141
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');;
148     }
149
150     function format ($pagelist, $page_handle, &$revision_handle) 
151     {
152         $rating = $this->_getValue($page_handle, $revision_handle);
153         $mean = $this->_user->mean_rating($this->_dimension);
154         $td = HTML::td($this->_tdattr);
155
156         $div = HTML::div();
157         if($rating != '-' && abs($rating - $mean) >= 0.75)
158             $div->setAttr('style', 'color: #' . ($rating > $mean ? '009900' : 'ff3333'));
159         $div->pushContent($rating);
160
161         $td->pushContent($div);
162
163         return $td;
164     }
165
166     function _getValue ($page_handle, &$revision_handle) 
167     {
168         $pagename = $page_handle->getName();
169
170         $tu = & $this->_user;
171         $rating = $tu->get_rating($pagename, $this->_dimension);
172
173         // a dash (or *something*) arguably looks better than a big blank space
174         return ($rating ? $rating : "-");
175     }
176     
177     function hasNoRatings($pages)
178     {      
179         $total = 0;
180         $use = & $this->_user;
181         foreach ($pages as $page) {
182             if ($use->get_rating($page, $this->_dimension)) {
183                 $total++;
184             }
185         }
186         if ($total == 0) {
187             return true;
188         }
189         return false; 
190           
191     }
192 };
193
194 /**
195  * Ratings widget for the logged-in user and the given page
196  */
197 class _PageList_Column_ratingwidget extends _PageList_Column_custom 
198 {
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');
203     }
204
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');
209         return $td;
210     }
211
212     function _getValue ($page_handle, &$revision_handle) 
213     {
214         global $request;
215
216         $pagename = $page_handle->getName();
217         $active_user   = $request->getUser();
218         $active_userid = $active_user->_userid;
219         
220         $tu = & RatingsUserFactory::getUser($active_userid);
221         return $tu->get_rating($pagename, $this->_dimension);
222     }
223 };
224
225 class _PageList_Column_prediction extends _PageList_Column 
226 {
227     var $_active_ratings_user;
228     var $_users;
229
230     function _PageList_Column_prediction ($params) 
231     {
232         global $request;
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');
240     }
241
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);
246
247         $div = HTML::div();
248         if($pred > 0 && abs($pred - $mean) >= 0.75)
249             $div->setAttr('style', 'color: #' . ($pred > $mean ? '009900' : 'ff3333'));
250         $div->pushContent($pred);
251
252         $td->pushContent($div);
253
254         return $td;
255     }
256
257     function _getValue ($page_handle, &$revision_handle) 
258     {
259         $pagename = $page_handle->getName();
260
261         $html = HTML();
262         $pred = $this->_active_ratings_user->knn_uu_predict($pagename, $this->_users, $this->_dimension);
263         return sprintf("%.1f", min(5, max(0, $pred)));
264     }
265 };
266
267 class _PageList_Column_top3recs extends _PageList_Column_custom
268 {
269     
270     var $_active_ratings_user;
271     var $_users;
272
273     function _PageList_Column_top3recs ($params) {
274         global $request;
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');
284         }
285     }
286
287     function _getValue ($page_handle, &$revision_handle) 
288     {
289         $ratings = $this->_active_ratings_user->get_ratings();
290         
291         $iter = $page_handle->getLinks();
292         $recs = array();
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);
299             }
300         }
301         arsort($recs);
302         $counter = 0;
303         if (count($recs) >= 3){
304             $numToShow = 3;
305         }
306         else {
307             // if <3 just show as many as there are
308             $numToShow = count($recs);
309         }
310         $html = HTML();
311         while ((list($key, $val) = each($recs)) && $counter < $numToShow) {
312             if ($val < 3){ 
313                 break;
314             }
315             if ($counter > 0){
316                 $html->pushContent(" , ");
317             }
318             $html->pushContent(WikiLink($key));
319             
320             $counter++;
321         }
322         if (count($recs) == 0 || $counter == 0){
323             $html->pushContent(_("None"));
324         }
325         
326         //return $top3list;
327         return $html;
328     }
329 };
330
331 // register custom PageList type
332 $WikiTheme->addPageListColumn
333   (array
334    (
335     'numbacklinks' 
336     => array('_PageList_Column_numbacklinks','custom:numbacklinks', _("# things"), false),
337     'rating'       
338     => array('_PageList_Column_ratingwidget','custom:rating', _("Rate"), false),
339     'coagreement'  
340     => array('_PageList_Column_coagreement','custom:coagreement', _("Go?"), 'center'),
341     'minmisery'    
342     => array('_PageList_Column_minmisery','custom:minmisery', _("MinMisery"), 'center'),
343     'averagerating' 
344     => array('_PageList_Column_averagerating','custom:averagerating', _("Avg. Rating"), 'left'),
345     'top3recs'
346     => array('_PageList_Column_top3recs','custom:top3recs', _("Top Recommendations"), 'left'),
347     ));
348
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)
352 // 
353
354 // Local Variables:
355 // mode: php
356 // tab-width: 8
357 // c-basic-offset: 4
358 // c-hanging-comment-ender-p: nil
359 // indent-tabs-mode: nil
360 // End:
361 ?>