]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PreferenceApp.php
gettext msg unification
[SourceForge/phpwiki.git] / lib / plugin / PreferenceApp.php
1 <?php //-*-php-*-
2 rcs_id('$Id: PreferenceApp.php,v 1.4 2004-12-20 16:05:14 rurban Exp $');
3
4 /**
5
6 */
7 require_once('lib/PageList.php');
8 require_once('lib/InlineParser.php');
9
10 require_once('lib/wikilens/Utils.php');
11 require_once('lib/Theme.php');
12 require_once('lib/wikilens/Buddy.php');
13 require_once('lib/wikilens/RatingsDb.php');
14
15 /**
16  * PreferenceApp is used to analyze a category of items that a group
17  * of people have rated.  A user is grouped to be analyzed in the group by
18  * 1) having rated at least one item in the database and 2) matching the optional 
19  * criteria for declaring a budget on their homepage.  
20  *
21  * An example of a budget decleration would be "TotalSoda: 50" on my homepage.
22  *
23  * PreferenceApp will output a matrix style table shows "how much" fractionally 
24  * a group of people prefer an item over other items.  For example, if my soda 
25  * budget is 100 then PreferenceApp might assign 20 units of my budget to Moutain Dew.
26  *
27  * Author: mcassano circa April 2004
28  *
29  * Usage:
30  * <?plugin PreferenceApp category="Soda" pageLabel="TotalSoda" roundCalc="true" ?>
31  */
32
33 class WikiPlugin_PreferenceApp
34 extends WikiPlugin
35 {
36     function getName () {
37         return _("PreferenceApp");
38     }
39
40     function getDescription () {
41         return _("Analyzes preferences based on voting budget and ratings.");
42     }
43
44     function getVersion() {
45         return preg_replace("/[Revision: $]/", '',
46                             "\$Revision: 1.4 $");
47     }
48
49     function getDefaultArguments() {
50         return array(
51                                 'category' => null,
52                                 'lockedBudget' => null,
53                                 'pageTextLabel' => null,
54                                 'group' => null,
55                                 'roundCalc' => "true",
56                                 'neutralRating' => "3",
57                                 'declareBudget' => "true");            
58     }
59     // info arg allows multiple columns
60     // info=mtime,hits,summary,version,author,locked,minor
61     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
62         
63
64     function run($dbi, $argstr, &$request, $basepage) {
65         
66         extract($this->getArgs($argstr, $request));
67         if($pageTextLabel == null && $category != null && $group == null){
68             $group = $category;                                 
69         }
70         /**
71          * defunct check
72         if($category == null || $pageTextLabel == null){
73                 $html = HTML("PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
74                 return $html;
75         }
76         */
77         
78         $dbi = $request->getDbh();
79         $rdbi = RatingsDb::getTheRatingsDb();
80         
81         $CATEGORY = $category;
82         $PAGE_TEXT_LABEL = $pageTextLabel;
83         $NEUTRAL_RATING = (int)$neutralRating;
84         
85         $active_user   = $request->getUser();
86         $active_userid = $active_user->_userid;
87                 $html = HTML();
88                 $html->pushContent("");
89                 
90                 //Load participating Users
91                 $users_array = array();
92                 if($group != null){
93                         $users_array = getMembers($group, $rdbi);
94
95         } else {
96             $people_iter = $rdbi->sql_get_users_rated();
97                         while($people_array = $people_iter->next()){
98                                 $users_array[] = $people_array['pagename'];     
99                         }
100                 }
101                 $people = array();
102         foreach($users_array as $person_indv){
103                         if($declareBudget == "true"){
104                 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");
105                                 if(count($get_array) == 1){
106                                         $cans_text = $get_array[0];
107                                         if(is_numeric($cans_text) && $cans_text >= 0){
108                                                 $canBudget[$person_indv] = $cans_text; //Load the persons budget
109                                         } else {
110                                                 $canBudget[$person_indv] = 0;
111                                         }
112                                         $people[] = $person_indv;       
113                                 }       
114                         } else {
115                                 $canBudget[$person_indv] = $lockedBudget;
116                                 $people[] = $person_indv;       
117                         }
118                 }
119                 if(count($people) < 1){
120                         $html = HTML("Nobody has used $PAGE_TEXT_LABEL on their homepage");
121                         return $html;   
122                 }
123                 //Get all pages from Category
124                 $pageids = array();
125         $category_page = $dbi->getPage($CATEGORY);
126         $iter = $category_page->getLinks();
127         while($item = $iter->next()){
128                 array_push($pageids, $item->getName());
129         }
130         $ratingTotals = array();
131         foreach($people as $person){
132                 $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);
133                 $ratingTotals[$person] = 0;
134                         while($ratings_array = $ratings_iter->next()){
135                                 $can_rating = $ratings_array['ratingvalue'];
136                                 if($can_rating >= $NEUTRAL_RATING){
137                                         $ratingTotals[$person] += $can_rating;
138                                 }
139                         }               
140         }
141                 
142                 //Generate numbers
143                 $canTotals = array();
144                 $peopleTotals = array();
145                 foreach($pageids as $soda){
146                         $canTotals[$soda] = 0;  
147                 }
148                 foreach($people as $person){
149                         foreach($pageids as $soda){
150                                 $peopleTotals[$person][$soda] = 0;
151                         }       
152                 }
153                 foreach($people as $person){
154                         foreach($pageids as $page){
155                                 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);
156                                 $can_rating_array = $can_rating_iter->next();
157                                 $can_rating = $can_rating_array['ratingvalue']; 
158                                 if($can_rating >= $NEUTRAL_RATING){
159                                         $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);
160                                         if($roundCalc == "true"){
161                                                 $adjustedCans = round($calc);
162                                         } else {
163                                                 $adjustedCans = round($calc, 2);
164                                         }
165                                         $peopleTotals[$person][$page] = $adjustedCans;
166                                         
167                                         $canTotals[$page] = $canTotals[$page] + $adjustedCans;
168                                 }
169                         }
170                 }
171                 $outputArray = array();
172                 foreach($people as $person){
173                         foreach($pageids as $page){
174                                 $outputArray[$person][$page] = 0;
175                         }
176                 }
177                 
178                 $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
179                 $tr = HTML::tr();
180                 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
181                 $td->pushContent(" ");
182                 $tr->pushContent($td);
183                 
184                 foreach($people as $person){
185                         $td = HTML::td(array('bgcolor' => '#FFFFFF'));
186                         $td->pushContent(HTML::a(array('href' => WikiURL($person),
187                                               'class' => 'wiki'
188                                               ),
189                                         SplitPagename($person)));
190                         //$td->pushContent(WikiLink(" $person "));
191                         $tr->pushContent($td);  
192                 }
193                 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
194                 $td->pushContent("Total Units");
195                 $tr->pushContent($td);  
196                 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
197                 $td->pushContent("Total Voters");
198                 $tr->pushContent($td);  
199                 $table->pushContent($tr);
200                 
201                 for($i = 0; $i < count($pageids); $i++){
202                         $total_cans = 0;
203                         for($j = 0; $j < count($people); $j++){
204                                 $td = HTML::td(array('align' => 'right'));
205                                 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];
206                                 $total_cans = $total_cans + $cans_per_soda;
207                                 $outputArray[$people[$j]][$pageids[$i]] = $cans_per_soda;
208                         }                       
209                 }
210                         
211                 
212                 foreach($people as $person){
213                         $min_soda = "";
214                         $min_cans = 9999999; //9 million, serving as "infinity"
215                         $total_cans = 0;
216                         foreach($pageids as $page){
217                                 $cur_soda_cans = $outputArray[$person][$page];
218                                 if($cur_soda_cans < $min_cans && $cur_soda_cans > 0){
219                                         $min_cans = $cur_soda_cans;
220                                         $min_soda = $page;
221                                 }
222                                 $total_cans = $total_cans + $cur_soda_cans;
223                         }       
224                         if($total_cans != $canBudget[$person] && $total_cans > 0){
225                                 $diff = $canBudget[$person] - $total_cans;
226                                 $outputArray[$person][$min_soda] = $outputArray[$person][$min_soda] + $diff;
227                         }
228                 }
229                 for($i = 0; $i < count($pageids); $i++){
230                         $tr = HTML::tr();
231                         $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
232                         $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
233                                               'class' => 'wiki'
234                                               ),
235                                         SplitPagename($pageids[$i])));
236                         $tr->pushContent($td);
237                         $total_cans = 0;
238                         $total_voters = 0;
239                         for($j = 0; $j < count($people); $j++){
240                                 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));
241                                 $output = $outputArray[$people[$j]][$pageids[$i]];
242                                 $total_cans = $total_cans + $output;
243                                 if($output == ""){
244                                         $output = "-";
245                                 } else {
246                                         $total_voters++;
247                                 }
248                                 $td->pushContent($output);
249                                 $tr->pushContent($td);
250                         }       
251                         if($total_cans == ""){
252                                 $total_cans = "-";      
253                         }
254                         if($total_voters == ""){
255                                 $total_voters = "-";    
256                         }
257                         $td = HTML::td(array('align' => 'right'));
258                         $td->pushContent($total_cans); 
259                         $tr->pushContent($td);  
260                         $td = HTML::td(array('align' => 'right'));
261                         $td->pushContent($total_voters); 
262                         $tr->pushContent($td);  
263                         $table->pushContent($tr);       
264                 }
265                 
266                 $tr = HTML::tr();
267                 $td = HTML::td(array('align' => 'left'));
268                 $td->pushContent(HTML::strong("Total Budget"));
269                 $tr->pushContent($td);
270                 $cans_total = 0;
271                 $total_voters = 0;
272                 for($i = 0; $i < count($people); $i++){
273                         $td = HTML::td(array('align' => 'right'));
274                         $cans_for_soda = 0;
275                         foreach($pageids as $page){
276                                 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
277                         }
278                         $cans = $cans_for_soda;
279                         $cans_total = $cans_total + $cans;
280                         if($cans == ""){
281                                 $cans = "-";
282                         } else {
283                                 $total_voters++;
284                         }
285                         $td->pushContent(HTML::strong($cans));
286                         $tr->pushContent($td);
287                 }
288                 $td = HTML::td(array('align' => 'right'));
289                 $td->pushContent(HTML::strong($cans_total));
290                 $tr->pushContent($td);
291                 $td = HTML::td(array('align' => 'right'));
292                 $td->pushContent(HTML::strong($total_voters));
293                 $tr->pushContent($td);
294                 $table->pushContent($tr);
295                 
296                 $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
297                 $table2->pushContent(HTML::tr(HTML::td($table)));
298                 $html->pushContent($table2);
299                 
300                 return $html;
301     }
302     
303 };
304
305 // Local Variables:
306 // mode: php
307 // tab-width: 8
308 // c-basic-offset: 4
309 // c-hanging-comment-ender-p: nil
310 // indent-tabs-mode: nil
311 // End:
312 ?>