PageRenderTime 35ms CodeModel.GetById 9ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/gforge/plugins/wiki/www/lib/wikilens/RatingsDb.php

https://github.com/neymanna/fusionforge
PHP | 778 lines | 341 code | 41 blank | 396 comment | 116 complexity | b448729e3a10d3674b6443b4d0377d5e MD5 | raw file
  1<?php // -*-php-*-
  2rcs_id('$Id: RatingsDb.php,v 1.13 2005/10/10 19:51:41 rurban Exp $');
  3
  4/*
  5 * @author:  Dan Frankowski (wikilens group manager), Reini Urban (as plugin)
  6 *
  7 * TODO: 
  8 * - fix RATING_STORAGE = WIKIPAGE
  9 * - fix smart caching
 10 * - finish mysuggest.c (external engine with data from mysql)
 11 * - add php_prediction from wikilens
 12 * - add the various show modes (esp. TopN queries in PHP)
 13 */
 14/*
 15 CREATE TABLE rating (
 16        dimension INT(4) NOT NULL,
 17        raterpage INT(11) NOT NULL,
 18        rateepage INT(11) NOT NULL,
 19        ratingvalue FLOAT NOT NULL,
 20        rateeversion INT(11) NOT NULL,
 21        isPrivate ENUM('yes','no'),
 22        tstamp TIMESTAMP(14) NOT NULL,
 23        PRIMARY KEY (dimension, raterpage, rateepage)
 24 );
 25*/
 26
 27//FIXME! for other than SQL backends
 28if (!defined('RATING_STORAGE'))
 29    define('RATING_STORAGE', 'SQL');         //TODO: support ADODB
 30    //define('RATING_STORAGE','WIKIPAGE');   // not fully supported yet
 31// leave undefined for internal, slow php engine.
 32//if (!defined('RATING_EXTERNAL'))
 33//    define('RATING_EXTERNAL',PHPWIKI_DIR . 'suggest.exe');
 34
 35// Dimensions
 36if (!defined('EXPLICIT_RATINGS_DIMENSION'))
 37    define('EXPLICIT_RATINGS_DIMENSION', 0);
 38if (!defined('LIST_ITEMS_DIMENSION'))
 39    define('LIST_ITEMS_DIMENSION', 1);
 40if (!defined('LIST_OWNER_DIMENSION'))
 41    define('LIST_OWNER_DIMENSION', 2);
 42if (!defined('LIST_TYPE_DIMENSION'))
 43    define('LIST_TYPE_DIMENSION', 3);
 44
 45
 46//TODO: split class into SQL and metadata backends
 47class RatingsDb extends WikiDB {
 48
 49    function RatingsDb() {
 50        global $request;
 51        $this->_dbi = &$request->_dbi;
 52        $this->_backend = &$this->_dbi->_backend;
 53        $this->dimension = null;
 54        if (RATING_STORAGE == 'SQL') {
 55            $this->_sqlbackend = &$this->_backend;
 56            if (isa($this->_backend, 'WikiDB_backend_PearDB'))
 57                $this->dbtype = "PearDB";
 58            elseif (isa($this->_backend, 'WikiDB_backend_ADODOB'))
 59                $this->dbtype = "ADODB";
 60            else {
 61            	include_once("lib/WikiDB/backend/ADODB.php");
 62                $this->_sqlbackend = new WikiDB_backend_ADODB($GLOBALS['DBParams']);
 63            	$this->dbtype = "ADODB";
 64            }
 65            $this->iter_class = "WikiDB_backend_".$this->dbtype."_generic_iter";
 66
 67            extract($this->_sqlbackend->_table_names);
 68            if (empty($rating_tbl)) {
 69                $rating_tbl = (!empty($GLOBALS['DBParams']['prefix']) 
 70                               ? $GLOBALS['DBParams']['prefix'] : '') . 'rating';
 71                $this->_sqlbackend->_table_names['rating_tbl'] = $rating_tbl;
 72            }
 73        } else {
 74        	$this->iter_class = "WikiDB_Array_PageIterator";
 75        }
 76    }
 77    
 78    // this is a singleton.  It ensures there is only 1 ratingsDB.
 79    function & getTheRatingsDb(){
 80        static $_theRatingsDb;
 81        
 82        if (!isset($_theRatingsDb)){
 83            $_theRatingsDb = new RatingsDb();
 84        } 
 85        //echo "rating db is $_theRatingsDb";
 86        return $_theRatingsDb;
 87    }
 88   
 89
 90/// *************************************************************************************
 91// FIXME
 92// from Reini Urban's RateIt plugin
 93    function addRating($rating, $userid, $pagename, $dimension) {
 94        if (RATING_STORAGE == 'SQL') {
 95            $page = $this->_dbi->getPage($pagename);
 96            $current = $page->getCurrentRevision();
 97            $rateeversion = $current->getVersion();
 98            $this->sql_rate($userid, $pagename, $rateeversion, $dimension, $rating);
 99        } else {
100            $this->metadata_set_rating($userid, $pagename, $dimension, $rating);
101        }
102    }
103
104    function deleteRating($userid=null, $pagename=null, $dimension=null) {
105        if (is_null($dimension)) $dimension = $this->dimension;
106        if (is_null($userid))    $userid = $this->userid; 
107        if (is_null($pagename))  $pagename = $this->pagename;
108        if (RATING_STORAGE == 'SQL') {
109            $this->sql_delete_rating($userid, $pagename, $dimension);
110        } else {
111            $this->metadata_set_rating($userid, $pagename, $dimension, -1);
112        }
113    }
114    
115    function getRating($userid=null, $pagename=null, $dimension=null) {
116        if (RATING_STORAGE == 'SQL') {
117            $ratings_iter = $this->sql_get_rating($dimension, $userid, $pagename);
118            if ($rating = $ratings_iter->next() and isset($rating['ratingvalue'])) {
119                return $rating['ratingvalue'];
120            } else 
121                return false;
122        } else {
123            return $this->metadata_get_rating($userid, $pagename, $dimension);
124        }
125    }
126
127    function getUsersRated($dimension=null, $orderby = null) {
128        if (is_null($dimension)) $dimension = $this->dimension;
129        if (is_null($userid))    $userid = $this->userid; 
130        if (is_null($pagename))  $pagename = $this->pagename;
131        if (RATING_STORAGE == 'SQL') {
132            $ratings_iter = $this->sql_get_users_rated($dimension, $orderby);
133            if ($rating = $ratings_iter->next() and isset($rating['ratingvalue'])) {
134                return $rating['ratingvalue'];
135            } else 
136                return false;
137        } else {
138            return $this->metadata_get_users_rated($dimension, $orderby);
139        }
140    }
141   
142    
143    /**
144     * Get ratings.
145     *
146     * @param dimension  The rating dimension id.
147     *                   Example: 0
148     *                   [optional]
149     *                   If this is null (or left off), the search for ratings
150     *                   is not restricted by dimension.
151     *
152     * @param rater  The page id of the rater, i.e. page doing the rating.
153     *               This is a Wiki page id, often of a user page.
154     *               Example: "DanFr"
155     *               [optional]
156     *               If this is null (or left off), the search for ratings
157     *               is not restricted by rater.
158     *               TODO: Support an array
159     *
160     * @param ratee  The page id of the ratee, i.e. page being rated.
161     *               Example: "DudeWheresMyCar"
162     *               [optional]
163     *               If this is null (or left off), the search for ratings
164     *               is not restricted by ratee.
165     *
166     * @param orderby An order-by clause with fields and (optionally) ASC
167     *                or DESC.
168     *               Example: "ratingvalue DESC"
169     *               [optional]
170     *               If this is null (or left off), the search for ratings
171     *               has no guaranteed order
172     *
173     * @param pageinfo The type of page that has its info returned (i.e.,
174     *               'pagename', 'hits', and 'pagedata') in the rows.
175     *               Example: "rater"
176     *               [optional]
177     *               If this is null (or left off), the info returned
178     *               is for the 'ratee' page (i.e., thing being rated).
179     *
180     * @return DB iterator with results 
181     */
182    function get_rating($dimension=null, $rater=null, $ratee=null,
183                        $orderby = null, $pageinfo = "ratee") {
184        if (RATING_STORAGE == 'SQL') {
185            $ratings_iter = $this->sql_get_rating($dimension, $rater, $pagename);
186            if ($rating = $ratings_iter->next() and isset($rating['ratingvalue'])) {
187                return $rating['ratingvalue'];
188            } else 
189                return false;
190        } else {
191            return $this->metadata_get_rating($rater, $pagename, $dimension);
192        }
193        /*
194        return $this->_backend->get_rating($dimension, $rater, $ratee,
195                                           $orderby, $pageinfo);
196        */
197    }
198    
199    function get_users_rated($dimension=null, $orderby = null) {
200        if (RATING_STORAGE == 'SQL') {
201            $ratings_iter = $this->sql_get_users_rated($dimension, $orderby);
202            if ($rating = $ratings_iter->next() and isset($rating['ratingvalue'])) {
203               return $rating['ratingvalue'];
204            } else 
205                return false;
206        } else {
207            return $this->metadata_get_users_rated($dimension, $orderby);
208        }
209        /*
210        return $this->_backend->get_users_rated($dimension, $orderby);
211        */
212    }
213
214    /**
215     * Like get_rating(), but return a WikiDB_PageIterator
216     * FIXME!
217     */
218    function get_rating_page($dimension=null, $rater=null, $ratee=null,
219                        $orderby = null, $pageinfo = "ratee") {
220        if (RATING_STORAGE == 'SQL') {
221            return $this->sql_get_rating($dimension, $rater, $ratee, $orderby, $pageinfo);
222        } else {
223        	// empty dummy iterator
224        	$pages = array();
225        	return new WikiDB_Array_PageIterator($pages);
226        }
227    }
228
229    /**
230     * Delete a rating.
231     *
232     * @param rater  The page id of the rater, i.e. page doing the rating.
233     *               This is a Wiki page id, often of a user page.
234     * @param ratee  The page id of the ratee, i.e. page being rated.
235     * @param dimension  The rating dimension id.
236     *
237     * @access public
238     *
239     * @return true upon success
240     */
241    function delete_rating($rater, $ratee, $dimension) {
242        if (RATING_STORAGE == 'SQL') {
243            $this->sql_delete_rating($rater, $ratee, $dimension);
244        } else {
245            $this->metadata_set_rating($rater, $ratee, $dimension, -1);
246        }
247    }
248
249    /**
250     * Rate a page.
251     *
252     * @param rater  The page id of the rater, i.e. page doing the rating.
253     *               This is a Wiki page id, often of a user page.
254     * @param ratee  The page id of the ratee, i.e. page being rated.
255     * @param rateeversion  The version of the ratee page.
256     * @param dimension  The rating dimension id.
257     * @param rating The rating value (a float).
258     *
259     * @access public
260     *
261     * @return true upon success
262     */
263    function rate($rater, $ratee, $rateeversion, $dimension, $rating) {
264        if (RATING_STORAGE == 'SQL') {
265            $page = $this->_dbi->getPage($pagename);
266            $current = $page->getCurrentRevision();
267            $rateeversion = $current->getVersion();
268            $this->sql_rate($userid, $pagename, $rateeversion, $dimension, $rating);
269        } else {
270            $this->metadata_set_rating($userid, $pagename, $dimension, $rating);
271        }
272    }
273    
274    //function getUsersRated(){}
275    
276//*******************************************************************************
277    // TODO:
278    // Use wikilens/RatingsUser.php for the php methods.
279    //
280    // Old:
281    // Currently we have to call the "suggest" CGI
282    //   http://www-users.cs.umn.edu/~karypis/suggest/
283    // until we implement a simple recommendation engine.
284    // Note that "suggest" is only free for non-profit organizations.
285    // I am currently writing a binary CGI mysuggest using suggest, which loads 
286    // data from mysql.
287    function getPrediction($userid=null, $pagename=null, $dimension=null) {
288        if (is_null($dimension)) $dimension = $this->dimension;
289        if (is_null($userid))    $userid   = $this->userid; 
290        if (is_null($pagename))  $pagename = $this->pagename;
291
292        if (RATING_STORAGE == 'SQL') {
293            $dbh = &$this->_dbi->_backend;
294            if (isset($pagename))
295                $page = $dbh->_get_pageid($pagename);
296            else 
297                return 0;
298            if (isset($userid))
299                $user = $dbh->_get_pageid($userid);
300            else 
301                return 0;
302        }
303        if (defined('RATING_EXTERNAL') and RATING_EXTERNAL) {
304            // how call mysuggest.exe? as CGI or natively
305            //$rating = HTML::Raw("<!--#include virtual=".RATING_ENGINE." -->");
306            $args = "-u$user -p$page -malpha"; // --top 10
307            if (isset($dimension))
308                $args .= " -d$dimension";
309            $rating = passthru(RATING_EXTERNAL . " $args");
310        } else {
311            $rating = $this->php_prediction($userid, $pagename, $dimension);
312        }
313        return $rating;
314    }
315
316    /**
317     * TODO: slow item-based recommendation engine, similar to suggest RType=2.
318     *       Only the SUGGEST_EstimateAlpha part
319     * Take wikilens/RatingsUser.php for the php methods.
320     */
321    function php_prediction($userid=null, $pagename=null, $dimension=null) {
322        if (is_null($dimension)) $dimension = $this->dimension;
323        if (is_null($userid))    $userid   = $this->userid; 
324        if (is_null($pagename))  $pagename = $this->pagename;
325        if (empty($this->buddies)) {
326            require_once("lib/wikilens/RatingsUser.php");
327            require_once("lib/wikilens/Buddy.php");
328            $user = RatingsUserFactory::getUser($userid);
329            $this->buddies = getBuddies($user, $GLOBALS['request']->_dbi);
330        }
331        return $user->knn_uu_predict($pagename, $this->buddies, $dimension);
332    }
333    
334    function getNumUsers($pagename=null, $dimension=null) {
335        if (is_null($dimension)) $dimension = $this->dimension;
336        if (is_null($pagename))  $pagename = $this->pagename;
337        if (RATING_STORAGE == 'SQL') {
338            $ratings_iter = $this->sql_get_rating($dimension, null, $pagename,
339                                                  null, "ratee");
340            return $ratings_iter->count();
341        } else {
342            if (!$pagename) return 0;
343            $page = $this->_dbi->getPage($pagename);
344            $data = $page->get('rating');
345            if (!empty($data[$dimension]))
346                return count($data[$dimension]);
347            else 
348                return 0;
349        }
350    }
351
352    // TODO: metadata method
353    function getAvg($pagename=null, $dimension=null) {
354        if (is_null($dimension)) $dimension = $this->dimension;
355        if (is_null($pagename))  $pagename = $this->pagename;
356        if (RATING_STORAGE == 'SQL') {
357            $dbi = &$this->_sqlbackend;
358	    if (isset($pagename) || isset($dimension)) {
359		$where = "WHERE";
360	    }
361            if (isset($pagename)) {
362                $raterid = $this->_backend->_get_pageid($pagename, true);
363                $where .= " raterpage=$raterid";
364            }
365            if (isset($dimension)) {
366		if (isset($pagename)) $where .= " AND";
367                $where .= " dimension=$dimension";
368            }
369            //$dbh = &$this->_dbi;
370            extract($dbi->_table_names);
371            $query = "SELECT AVG(ratingvalue) as avg"
372		   . " FROM $rating_tbl r, $page_tbl p "
373		   . $where
374		   . " GROUP BY raterpage";
375            $result = $dbi->_dbh->query($query);
376            $iter = new $this->iter_class($this, $result);
377            $row = $iter->next();
378            return $row['avg'];
379        } else {
380            if (!$pagename) return 0;
381            $page = $this->_dbi->getPage($pagename);
382            $data = $page->get('rating');
383            if (!empty($data[$dimension]))
384                // hash of userid => rating
385                return array_sum(array_values($data[$dimension])) / count($data[$dimension]);
386	    else
387		return 0;
388        }
389    }
390//*******************************************************************************
391
392    /**
393     * Get ratings.
394     *
395     * @param dimension  The rating dimension id.
396     *                   Example: 0
397     *                   [optional]
398     *                   If this is null (or left off), the search for ratings
399     *                   is not restricted by dimension.
400     *
401     * @param rater  The page id of the rater, i.e. page doing the rating.
402     *               This is a Wiki page id, often of a user page.
403     *               Example: "DanFr"
404     *               [optional]
405     *               If this is null (or left off), the search for ratings
406     *               is not restricted by rater.
407     *               TODO: Support an array
408     *
409     * @param ratee  The page id of the ratee, i.e. page being rated.
410     *               Example: "DudeWheresMyCar"
411     *               [optional]
412     *               If this is null (or left off), the search for ratings
413     *               is not restricted by ratee.
414     *               TODO: Support an array
415     *
416     * @param orderby An order-by clause with fields and (optionally) ASC
417     *                or DESC.
418     *               Example: "ratingvalue DESC"
419     *               [optional]
420     *               If this is null (or left off), the search for ratings
421     *               has no guaranteed order
422     *
423     * @param pageinfo The type of page that has its info returned (i.e.,
424     *               'pagename', 'hits', and 'pagedata') in the rows.
425     *               Example: "rater"
426     *               [optional]
427     *               If this is null (or left off), the info returned
428     *               is for the 'ratee' page (i.e., thing being rated).
429     *
430     * @return DB iterator with results 
431     */
432    function sql_get_rating($dimension=null, $rater=null, $ratee=null,
433                            $orderby=null, $pageinfo = "ratee") {
434        if (empty($dimension)) $dimension=null;
435        $result = $this->_sql_get_rating_result($dimension, $rater, $ratee, $orderby, $pageinfo);
436        return new $this->iter_class($this, $result);
437    }
438
439    function sql_get_users_rated($dimension=null, $orderby=null) {
440        if (empty($dimension)) $dimension=null;
441        $result = $this->_sql_get_rating_result($dimension, null, null, $orderby, "rater");
442        return new $this->iter_class($this, $result);
443    }
444
445    /**
446     * @access private
447     * @return result ressource, suitable to the iterator
448     */
449    function _sql_get_rating_result($dimension=null, $rater=null, $ratee=null,
450                                    $orderby=null, $pageinfo = "ratee") {
451        // pageinfo must be 'rater' or 'ratee'
452        if (($pageinfo != "ratee") && ($pageinfo != "rater"))
453            return;
454        $dbi = &$this->_sqlbackend;
455        //$dbh = &$this->_dbi;
456        extract($dbi->_table_names);
457        $where = "WHERE r." . $pageinfo . "page = p.id";
458        if (isset($dimension)) {
459            $where .= " AND dimension=$dimension";
460        }
461        if (isset($rater)) {
462            $raterid = $this->_backend->_get_pageid($rater, true);
463            $where .= " AND raterpage=$raterid";
464        }
465        if (isset($ratee)) {
466            if(is_array($ratee)){
467        		$where .= " AND (";
468        		for($i = 0; $i < count($ratee); $i++){
469        			$rateeid = $this->_backend->_get_pageid($ratee[$i], true);
470            		$where .= "rateepage=$rateeid";
471        			if($i != (count($ratee) - 1)){
472        				$where .= " OR ";
473        			}
474        		}
475        		$where .= ")";
476        	} else {
477        		$rateeid = $this->_backend->_get_pageid($ratee, true);
478            	$where .= " AND rateepage=$rateeid";
479        	}
480        }
481        $orderbyStr = "";
482        if (isset($orderby)) {
483            $orderbyStr = " ORDER BY " . $orderby;
484        }
485        if (isset($rater) or isset($ratee)) $what = '*';
486        // same as _get_users_rated_result()
487        else $what = 'DISTINCT p.pagename';
488
489        $query = "SELECT $what"
490               . " FROM $rating_tbl r, $page_tbl p "
491               . $where
492               . $orderbyStr;
493        $result = $dbi->_dbh->query($query);
494        return $result;
495    }
496
497    /**
498     * Delete a rating.
499     *
500     * @param rater  The page id of the rater, i.e. page doing the rating.
501     *               This is a Wiki page id, often of a user page.
502     * @param ratee  The page id of the ratee, i.e. page being rated.
503     * @param dimension  The rating dimension id.
504     *
505     * @access public
506     *
507     * @return true upon success
508     */
509    function sql_delete_rating($rater, $ratee, $dimension) {
510        //$dbh = &$this->_dbi;
511        $dbi = &$this->_sqlbackend;
512        extract($dbi->_table_names);
513
514        $dbi->lock();
515        $raterid = $this->_backend->_get_pageid($rater, true);
516        $rateeid = $this->_backend->_get_pageid($ratee, true);
517        $where = "WHERE raterpage=$raterid and rateepage=$rateeid";
518        if (isset($dimension)) {
519            $where .= " AND dimension=$dimension";
520        }
521        $dbi->_dbh->query("DELETE FROM $rating_tbl $where");
522        $dbi->unlock();
523        return true;
524    }
525
526    /**
527     * Rate a page.
528     *
529     * @param rater  The page id of the rater, i.e. page doing the rating.
530     *               This is a Wiki page id, often of a user page.
531     * @param ratee  The page id of the ratee, i.e. page being rated.
532     * @param rateeversion  The version of the ratee page.
533     * @param dimension  The rating dimension id.
534     * @param rating The rating value (a float).
535     *
536     * @access public
537     *
538     * @return true upon success
539     */
540    //               ($this->userid, $this->pagename, $page->version, $this->dimension, $rating);
541    function sql_rate($rater, $ratee, $rateeversion, $dimension, $rating) {
542        $dbi = &$this->_sqlbackend;
543        extract($dbi->_table_names);
544        if (empty($rating_tbl))
545            $rating_tbl = $this->_dbi->getParam('prefix') . 'rating';
546
547        $dbi->lock();
548        $raterid = $this->_backend->_get_pageid($rater, true);
549        $rateeid = $this->_backend->_get_pageid($ratee, true);
550        assert($raterid);
551        assert($rateeid);
552        //mysql optimize: REPLACE if raterpage and rateepage are keys
553        $dbi->_dbh->query("DELETE from $rating_tbl WHERE dimension=$dimension AND raterpage=$raterid AND rateepage=$rateeid");
554        $where = "WHERE raterpage='$raterid' AND rateepage='$rateeid'";
555        $insert = "INSERT INTO $rating_tbl (dimension, raterpage, rateepage, ratingvalue, rateeversion)"
556            ." VALUES ('$dimension', $raterid, $rateeid, '$rating', '$rateeversion')";
557        $dbi->_dbh->query($insert);
558        
559        $dbi->unlock();
560        return true;
561    }
562
563    function metadata_get_rating($userid, $pagename, $dimension) {
564    	if (!$pagename) return false;
565        $page = $this->_dbi->getPage($pagename);
566        $data = $page->get('rating');
567        if (!empty($data[$dimension][$userid]))
568            return (float)$data[$dimension][$userid];
569        else 
570            return false;
571    }
572
573    function metadata_set_rating($userid, $pagename, $dimension, $rating = -1) {
574    	if (!$pagename) return false;
575    	$page = $this->_dbi->getPage($pagename);
576        $data = $page->get('rating');
577        if ($rating == -1)
578            unset($data[$dimension][$userid]);
579        else {
580            if (empty($data[$dimension][$userid]))
581                $data[$dimension] = array($userid => (float)$rating);
582            else
583                $data[$dimension][$userid] = $rating;
584        }
585        $page->set('rating',$data);
586    }
587   
588}
589
590/*
591class RatingsDB_backend_PearDB 
592extends WikiDB_backend_PearDB {
593    function get_rating($dimension=null, $rater=null, $ratee=null,
594                        $orderby=null, $pageinfo = "ratee") {
595        $result = $this->_get_rating_result(
596                         $dimension, $rater, $ratee, $orderby, $pageinfo);
597        return new WikiDB_backend_PearDB_generic_iter($this, $result);
598    }
599    
600    function get_users_rated($dimension=null, $orderby=null) {
601        $result = $this->_get_users_rated_result(
602                         $dimension, $orderby);
603        return new WikiDB_backend_PearDB_generic_iter($this, $result);
604    }
605
606    function get_rating_page($dimension=null, $rater=null, $ratee=null,
607                             $orderby=null, $pageinfo = "ratee") {
608        $result = $this->_get_rating_result(
609                         $dimension, $rater, $ratee, $orderby, $pageinfo);
610        return new WikiDB_backend_PearDB_iter($this, $result);
611    }
612
613    function _get_rating_result($dimension=null, $rater=null, $ratee=null,
614                                $orderby=null, $pageinfo = "ratee") {
615        // pageinfo must be 'rater' or 'ratee'
616        if (($pageinfo != "ratee") && ($pageinfo != "rater"))
617            return;
618
619        $dbh = &$this->_dbh;
620        extract($this->_table_names);
621
622        $where = "WHERE r." . $pageinfo . "page = p.id";
623        if (isset($dimension)) {
624            $where .= " AND dimension=$dimension";
625        }
626        if (isset($rater)) {
627            $raterid = $this->_get_pageid($rater, true);
628            $where .= " AND raterpage=$raterid";
629        }
630        if (isset($ratee)) {
631        	if(is_array($ratee)){
632        		$where .= " AND (";
633        		for($i = 0; $i < count($ratee); $i++){
634        			$rateeid = $this->_get_pageid($ratee[$i], true);
635            		$where .= "rateepage=$rateeid";
636        			if($i != (count($ratee) - 1)){
637        				$where .= " OR ";
638        			}
639        		}
640        		$where .= ")";
641        	} else {
642        		$rateeid = $this->_get_pageid($ratee, true);
643            	$where .= " AND rateepage=$rateeid";
644        	}
645        }
646
647        $orderbyStr = "";
648        if (isset($orderby)) {
649            $orderbyStr = " ORDER BY " . $orderby;
650        }
651
652        $query = "SELECT *"
653            . " FROM $rating_tbl r, $page_tbl p "
654            . $where
655            . $orderbyStr;
656
657        $result = $dbh->query($query);
658
659        return $result;
660    }
661    
662    function _get_users_rated_result($dimension=null, $orderby=null) {
663        $dbh = &$this->_dbh;
664        extract($this->_table_names);
665
666        $where = "WHERE p.id=r.raterpage";
667        if (isset($dimension)) {
668            $where .= " AND dimension=$dimension";
669        }
670        $orderbyStr = "";
671        if (isset($orderby)) {
672            $orderbyStr = " ORDER BY " . $orderby;
673        }
674
675        $query = "SELECT DISTINCT p.pagename"
676            . " FROM $rating_tbl r, $page_tbl p "
677            . $where
678            . $orderbyStr;
679
680        $result = $dbh->query($query);
681
682        return $result;
683    }
684    function delete_rating($rater, $ratee, $dimension) {
685        $dbh = &$this->_dbh;
686        extract($this->_table_names);
687
688        $this->lock();
689        $raterid = $this->_get_pageid($rater, true);
690        $rateeid = $this->_get_pageid($ratee, true);
691
692        $dbh->query("DELETE FROM $rating_tbl WHERE raterpage=$raterid and rateepage=$rateeid and dimension=$dimension");
693        $this->unlock();
694        return true;
695    }
696
697    function rate($rater, $ratee, $rateeversion, $dimension, $rating, $isPrivate = 'no') {
698        $dbh = &$this->_dbh;
699        extract($this->_table_names);
700
701        $this->lock();
702        $raterid = $this->_get_pageid($rater, true);
703        $rateeid = $this->_get_pageid($ratee, true);
704
705        $dbh->query("DELETE FROM $rating_tbl WHERE raterpage=$raterid and rateepage=$rateeid and dimension=$dimension and isPrivate='$isPrivate'");
706        // NOTE: Leave tstamp off the insert, and MySQL automatically updates it
707        $dbh->query("INSERT INTO $rating_tbl (dimension, raterpage, rateepage, ratingvalue, rateeversion, isPrivate) VALUES ($dimension, $raterid, $rateeid, $rating, $rateeversion, '$isPrivate')");
708        $this->unlock();
709        return true;
710    }
711} 
712*/
713
714// $Log: RatingsDb.php,v $
715// Revision 1.13  2005/10/10 19:51:41  rurban
716// fix aesthetic issues by John Stevens
717//
718// Revision 1.12  2004/11/15 16:00:02  rurban
719// enable RateIt imgPrefix: '' or 'Star' or 'BStar',
720// enable blue prediction icons,
721// enable buddy predictions.
722//
723// Revision 1.11  2004/11/01 10:44:00  rurban
724// seperate PassUser methods into seperate dir (memory usage)
725// fix WikiUser (old) overlarge data session
726// remove wikidb arg from various page class methods, use global ->_dbi instead
727// ...
728//
729// Revision 1.10  2004/10/05 17:00:04  rurban
730// support paging for simple lists
731// fix RatingDb sql backend.
732// remove pages from AllPages (this is ListPages then)
733//
734// Revision 1.9  2004/10/05 00:33:44  rurban
735// intermediate fix for non-sql WikiDB and SQL rating
736//
737// Revision 1.8  2004/07/20 18:00:50  dfrankow
738// Add EXPLICIT_RATINGS_DIMENSION constant.  More dimensions on the way
739// for lists.
740//
741// Fix delete_rating().
742//
743// Revision 1.7  2004/07/08 19:14:57  rurban
744// more metadata fixes
745//
746// Revision 1.6  2004/07/08 19:04:45  rurban
747// more unittest fixes (file backend, metadata RatingsDb)
748//
749// Revision 1.5  2004/07/08 13:50:33  rurban
750// various unit test fixes: print error backtrace on _DEBUG_TRACE; allusers fix; new PHPWIKI_NOMAIN constant for omitting the mainloop
751//
752// Revision 1.4  2004/07/07 19:47:36  dfrankow
753// Fixes to get PreferencesApp to work-- thanks syilek
754//
755// Revision 1.3  2004/06/30 20:05:36  dfrankow
756// + Add getTheRatingsDb() singleton.
757// + Remove defaulting of dimension, userid, pagename in getRating--
758//   it didn't work
759// + Fix typo in get_rating.
760// + Fix _sql_get_rating_result
761// + Fix sql_rate().  It's now not transactionally safe yet, but at least it
762//   works.
763//
764// Revision 1.2  2004/06/19 10:22:41  rurban
765// outcomment the pear specific methods to let all pages load
766//
767// Revision 1.1  2004/06/18 14:42:17  rurban
768// added wikilens libs (not yet merged good enough, some work for DanFr)
769// 
770
771// Local Variables:
772// mode: php
773// tab-width: 8
774// c-basic-offset: 4
775// c-hanging-comment-ender-p: nil
776// indent-tabs-mode: nil
777// End:
778?>