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