]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/wikilens/Buddy.php
No tabs
[SourceForge/phpwiki.git] / lib / wikilens / Buddy.php
1 <?php //-*-php-*-
2 // $Id$
3
4 // It is anticipated that when userid support is added to phpwiki,
5 // this object will hold much more information (e-mail,
6 // home(wiki)page, etc.) about the user.
7
8 // There seems to be no clean way to "log out" a user when using HTTP
9 // authentication. So we'll hack around this by storing the currently
10 // logged in username and other state information in a cookie.
11
12 // 2002-09-08 11:44:04 rurban
13 // Todo: Fix prefs cookie/session handling:
14 //       _userid and _homepage cookie/session vars still hold the
15 //       serialized string.
16 //       If no homepage, fallback to prefs in cookie as in 1.3.3.
17
18 require_once (dirname(__FILE__)."/Utils.php");
19
20 function addBuddy($user, $buddy, $dbi)
21 {
22     $START_DELIM = _("Buddies:");
23     // the delimiter is really a comma, but include a space to make it look
24     // nicer (getBuddies strips out extra spaces when extracting buddies)
25     $DELIM = ", ";
26
27     addPageTextData($user, $dbi, $buddy, $START_DELIM, $DELIM);
28 }
29
30 function getBuddies($fromUser, $dbi, $thePage = ""){
31     $START_DELIM = $thePage . _("Buddies:");
32     $DELIM = ",";
33     $buddies_array = getPageTextData($fromUser, $dbi, $START_DELIM, $DELIM);
34     if (count($buddies_array) == 0 and $thePage !== "") {
35         $buddies_array = getPageTextData($fromUser, $dbi, _("Buddies:"), $DELIM);
36     }
37     if (empty($buddies_array)) {
38     // 1. calculate buddies automatically from the 10 top raters with the most numratings (min. 5 ratings).
39     //    of all pages (only SQL)
40     // or 2. from 10 random raters of this page (non-SQL)
41     // or 3. from all members of your group (department) if <= 20
42     $rdbi = RatingsDb::getTheRatingsDb();
43     $dimension = '';
44         if (RATING_STORAGE == 'SQL') {
45         //$result = $this->_sql_get_rating_result($dimension, null, null, 'numrating', "rater");
46         $dbh = &$rdbi->_sqlbackend;
47         extract($dbh->_table_names);
48         $query = "SELECT raterpage, COUNT(rateepage) as numrating"
49         . " FROM $rating_tbl r, $page_tbl p "
50 //        . " WHERE ratingvalue > 0 AND numrating > 5"
51         . " WHERE ratingvalue > 0"
52         . " GROUP BY raterpage"
53         . " ORDER BY numrating"
54         . " LIMIT 10";
55         $result = $dbh->_dbh->query($query);
56     } else {
57         // from 10 random raters of this page (non-SQL)
58         ;
59     }
60
61     }
62     $result = array();
63     if (is_array($buddies_array))
64       foreach ($buddies_array as $userid) {
65         $result[] = new RatingsUser($userid);
66       }
67     return $result;
68 }
69
70 function CoAgreement($dbi, $page, $users, $active_userid){
71     //Returns a "yes" 1, "no" -1, or "unsure" 0 for whether
72     //the group agrees on the page based on their ratings
73     $cur_page = $page;
74
75     $my_ratings_iter = $dbi->get_rating(0, $active_userid, $page);
76     $my_ratings_single = $my_ratings_iter->next();
77     $cur_rating = $my_ratings_single['ratingvalue'];
78
79     $MIDDLE_RATING = 3;
80
81     if($cur_rating >= $MIDDLE_RATING){
82         $agreePos = 1;
83     } else {
84         $agreePos = 0;
85     }
86     foreach($users as $buddy){
87         $buddy_rating_iter = $dbi->get_rating(0, $buddy, $cur_page);
88         $buddy_rating_array = $buddy_rating_iter->next();
89         $buddy_rating = $buddy_rating_array['ratingvalue'];
90         if($buddy_rating == ""){
91             $agree = 1;
92         }else if($agreePos && $buddy_rating >= $MIDDLE_RATING){
93             $agree = 1;
94         } else if(!$agreePos && $buddy_rating < $MIDDLE_RATING){
95             $agree = 1;
96         } else {
97             $agree = 0;
98             break;
99         }
100     }
101     if($agree && $agreePos){
102         return 1;
103     } else if($agree && !$agreePos){
104         return -1;
105     } else {
106         return 0;
107     }
108 }
109
110 function MinMisery($dbi, $page, $users, $active_userid){
111     //Returns the minimum rating for the page
112     //from all the users.
113
114     $cur_page = $page;
115
116     $my_ratings_iter = $dbi->get_rating(0, $active_userid, $page);
117     $my_ratings_single = $my_ratings_iter->next();
118     $cur_rating = $my_ratings_single['ratingvalue'];
119
120     $min = $cur_rating;
121     foreach($users as $buddy){
122         $buddy_rating_iter = $dbi->get_rating(0, $buddy, $cur_page);
123         $buddy_rating_array = $buddy_rating_iter->next();
124         $buddy_rating = $buddy_rating_array['ratingvalue'];
125         if($buddy_rating != "" && $buddy_rating < $min){
126             $min = $buddy_rating;
127         }
128     }
129     return $min;
130 }
131
132 function AverageRating($dbi, $page, $users, $active_userid){
133     //Returns the average rating for the page
134     //from all the users.
135
136     $cur_page = $page;
137
138     $my_ratings_iter = $dbi->get_rating(0, $active_userid, $page);
139     $my_ratings_single = $my_ratings_iter->next();
140     $cur_rating = $my_ratings_single['ratingvalue'];
141     if($cur_rating != ""){
142         $total = $cur_rating;
143         $count = 1;
144     } else {
145         $total = 0;
146         $count = 0;
147     }
148     foreach($users as $buddy){
149         $buddy_rating_iter = $dbi->get_rating(0, $buddy, $cur_page);
150         $buddy_rating_array = $buddy_rating_iter->next();
151         $buddy_rating = $buddy_rating_array['ratingvalue'];
152         if($buddy_rating != ""){
153             $total = $total + $buddy_rating;
154             $count++;
155         }
156     }
157     if($count == 0){
158         return 0;
159     } else {
160         return $total / $count;
161     }
162 }
163
164 // Local Variables:
165 // mode: php
166 // tab-width: 8
167 // c-basic-offset: 4
168 // c-hanging-comment-ender-p: nil
169 // indent-tabs-mode: nil
170 // End:
171 ?>