]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/wikilens/Buddy.php
Reformat code
[SourceForge/phpwiki.git] / lib / wikilens / Buddy.php
1 <?php
2
3 // It is anticipated that when userid support is added to phpwiki,
4 // this object will hold much more information (e-mail,
5 // home(wiki)page, etc.) about the user.
6
7 // There seems to be no clean way to "log out" a user when using HTTP
8 // authentication. So we'll hack around this by storing the currently
9 // logged in username and other state information in a cookie.
10
11 // 2002-09-08 11:44:04 rurban
12 // Todo: Fix prefs cookie/session handling:
13 //       _userid and _homepage cookie/session vars still hold the
14 //       serialized string.
15 //       If no homepage, fallback to prefs in cookie as in 1.3.3.
16
17 require_once (dirname(__FILE__) . "/Utils.php");
18
19 function addBuddy($user, $buddy, $dbi)
20 {
21     $START_DELIM = _("Buddies:");
22     // the delimiter is really a comma, but include a space to make it look
23     // nicer (getBuddies strips out extra spaces when extracting buddies)
24     $DELIM = ", ";
25
26     addPageTextData($user, $dbi, $buddy, $START_DELIM, $DELIM);
27 }
28
29 function getBuddies($fromUser, $dbi, $thePage = "")
30 {
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 {
72     //Returns a "yes" 1, "no" -1, or "unsure" 0 for whether
73     //the group agrees on the page based on their ratings
74     $cur_page = $page;
75
76     $my_ratings_iter = $dbi->get_rating(0, $active_userid, $page);
77     $my_ratings_single = $my_ratings_iter->next();
78     $cur_rating = $my_ratings_single['ratingvalue'];
79
80     $MIDDLE_RATING = 3;
81
82     if ($cur_rating >= $MIDDLE_RATING) {
83         $agreePos = 1;
84     } else {
85         $agreePos = 0;
86     }
87     foreach ($users as $buddy) {
88         $buddy_rating_iter = $dbi->get_rating(0, $buddy, $cur_page);
89         $buddy_rating_array = $buddy_rating_iter->next();
90         $buddy_rating = $buddy_rating_array['ratingvalue'];
91         if ($buddy_rating == "") {
92             $agree = 1;
93         } else if ($agreePos && $buddy_rating >= $MIDDLE_RATING) {
94             $agree = 1;
95         } else if (!$agreePos && $buddy_rating < $MIDDLE_RATING) {
96             $agree = 1;
97         } else {
98             $agree = 0;
99             break;
100         }
101     }
102     if ($agree && $agreePos) {
103         return 1;
104     } else if ($agree && !$agreePos) {
105         return -1;
106     } else {
107         return 0;
108     }
109 }
110
111 function MinMisery($dbi, $page, $users, $active_userid)
112 {
113     //Returns the minimum rating for the page
114     //from all the users.
115
116     $cur_page = $page;
117
118     $my_ratings_iter = $dbi->get_rating(0, $active_userid, $page);
119     $my_ratings_single = $my_ratings_iter->next();
120     $cur_rating = $my_ratings_single['ratingvalue'];
121
122     $min = $cur_rating;
123     foreach ($users as $buddy) {
124         $buddy_rating_iter = $dbi->get_rating(0, $buddy, $cur_page);
125         $buddy_rating_array = $buddy_rating_iter->next();
126         $buddy_rating = $buddy_rating_array['ratingvalue'];
127         if ($buddy_rating != "" && $buddy_rating < $min) {
128             $min = $buddy_rating;
129         }
130     }
131     return $min;
132 }
133
134 function AverageRating($dbi, $page, $users, $active_userid)
135 {
136     //Returns the average rating for the page
137     //from all the users.
138
139     $cur_page = $page;
140
141     $my_ratings_iter = $dbi->get_rating(0, $active_userid, $page);
142     $my_ratings_single = $my_ratings_iter->next();
143     $cur_rating = $my_ratings_single['ratingvalue'];
144     if ($cur_rating != "") {
145         $total = $cur_rating;
146         $count = 1;
147     } else {
148         $total = 0;
149         $count = 0;
150     }
151     foreach ($users as $buddy) {
152         $buddy_rating_iter = $dbi->get_rating(0, $buddy, $cur_page);
153         $buddy_rating_array = $buddy_rating_iter->next();
154         $buddy_rating = $buddy_rating_array['ratingvalue'];
155         if ($buddy_rating != "") {
156             $total = $total + $buddy_rating;
157             $count++;
158         }
159     }
160     if ($count == 0) {
161         return 0;
162     } else {
163         return $total / $count;
164     }
165 }
166
167 // Local Variables:
168 // mode: php
169 // tab-width: 8
170 // c-basic-offset: 4
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil
173 // End: