]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PreferenceApp.php
Error banner
[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  * <<PreferenceApp category="Soda" pageTextLabel="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                 return HTML::div(array('class' => "error"), "PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
68         }
69         
70         $dbi = $request->getDbh();
71         $rdbi = RatingsDb::getTheRatingsDb();
72         
73         $CATEGORY = $category;
74         $PAGE_TEXT_LABEL = $pageTextLabel;
75         $NEUTRAL_RATING = (int)$neutralRating;
76         
77         $active_user   = $request->getUser();
78         $active_userid = $active_user->_userid;
79         $html = HTML();
80         $html->pushContent("");
81                 
82         //Load participating Users
83         $users_array = array();
84         if($group != null){
85             $users_array = getMembers($group, $rdbi);
86         } else {
87             $people_iter = $rdbi->sql_get_users_rated();
88             while($people_array = $people_iter->next()){
89                 $users_array[] = $people_array['pagename'];     
90             }
91         }
92         $people = array();
93         foreach($users_array as $person_indv){
94             if($declareBudget == "true"){
95                 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");
96                 if(count($get_array) == 1){
97                     $cans_text = $get_array[0];
98                     if(is_numeric($cans_text) && $cans_text >= 0){
99                         $canBudget[$person_indv] = $cans_text; //Load the persons budget
100                     } else {
101                         $canBudget[$person_indv] = 0;
102                     }
103                     $people[] = $person_indv;   
104                 }       
105             } else {
106                 $canBudget[$person_indv] = $lockedBudget;
107                 $people[] = $person_indv;       
108             }
109         }
110         if(count($people) < 1){
111             return fmt("Nobody has used %s on their homepage", $PAGE_TEXT_LABEL);
112         }
113         //Get all pages from Category
114         $pageids = array();
115         $category_page = $dbi->getPage($CATEGORY);
116         $iter = $category_page->getLinks();
117         while ($item = $iter->next()){
118             array_push($pageids, $item->getName());
119         }
120         $ratingTotals = array();
121         foreach ($people as $person){
122                 $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);
123                 $ratingTotals[$person] = 0;
124                 while($ratings_array = $ratings_iter->next()){
125                     $can_rating = $ratings_array['ratingvalue'];
126                     if($can_rating >= $NEUTRAL_RATING){
127                         $ratingTotals[$person] += $can_rating;
128                     }
129                 }               
130         }
131                 
132         //Generate numbers
133         $canTotals = array();
134         $peopleTotals = array();
135         foreach($pageids as $soda){
136             $canTotals[$soda] = 0;      
137         }
138         foreach($people as $person){
139             foreach($pageids as $soda){
140                 $peopleTotals[$person][$soda] = 0;
141             }   
142         }
143         foreach($people as $person){
144             foreach($pageids as $page){
145                 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);
146                 $can_rating_array = $can_rating_iter->next();
147                 $can_rating = $can_rating_array['ratingvalue']; 
148                 if($can_rating >= $NEUTRAL_RATING){
149                     $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);
150                     if($roundCalc == "true"){
151                         $adjustedCans = round($calc);
152                     } else {
153                         $adjustedCans = round($calc, 2);
154                     }
155                     $peopleTotals[$person][$page] = $adjustedCans;
156                     
157                     $canTotals[$page] = $canTotals[$page] + $adjustedCans;
158                 }
159             }
160         }
161         $outputArray = array();
162         foreach($people as $person){
163             foreach($pageids as $page){
164                 $outputArray[$person][$page] = 0;
165             }
166         }
167         
168         $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
169         $tr = HTML::tr();
170         $td = HTML::td(array('bgcolor' => '#FFFFFF'));
171         $td->pushContent(" ");
172         $tr->pushContent($td);
173                 
174         foreach($people as $person){
175             $td = HTML::td(array('bgcolor' => '#FFFFFF'));
176             $td->pushContent(HTML::a(array('href' => WikiURL($person),
177                                            'class' => 'wiki'
178                                            ),
179                                      SplitPagename($person)));
180             //$td->pushContent(WikiLink(" $person "));
181             $tr->pushContent($td);      
182         }
183         $td = HTML::td(array('bgcolor' => '#FFFFFF'));
184         $td->pushContent(_("Total Units"));
185         $tr->pushContent($td);  
186         $td = HTML::td(array('bgcolor' => '#FFFFFF'));
187         $td->pushContent(_("Total Voters"));
188         $tr->pushContent($td);  
189         $table->pushContent($tr);
190                 
191         for($i = 0; $i < count($pageids); $i++){
192             $total_cans = 0;
193             for($j = 0; $j < count($people); $j++){
194                 $td = HTML::td(array('align' => 'right'));
195                 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];
196                 $total_cans = $total_cans + $cans_per_soda;
197                 $outputArray[$people[$j]][$pageids[$i]] = $cans_per_soda;
198             }                   
199         }
200         
201                 
202         foreach($people as $person){
203             $min_soda = "";
204             $min_cans = 9999999; //9 million, serving as "infinity"
205             $total_cans = 0;
206             foreach($pageids as $page){
207                 $cur_soda_cans = $outputArray[$person][$page];
208                 if($cur_soda_cans < $min_cans && $cur_soda_cans > 0){
209                     $min_cans = $cur_soda_cans;
210                     $min_soda = $page;
211                 }
212                 $total_cans = $total_cans + $cur_soda_cans;
213             }   
214             if($total_cans != $canBudget[$person] && $total_cans > 0){
215                 $diff = $canBudget[$person] - $total_cans;
216                 $outputArray[$person][$min_soda] = $outputArray[$person][$min_soda] + $diff;
217             }
218         }
219         for($i = 0; $i < count($pageids); $i++){
220             $tr = HTML::tr();
221             $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
222             $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
223                                            'class' => 'wiki'
224                                            ),
225                                      SplitPagename($pageids[$i])));
226             $tr->pushContent($td);
227             $total_cans = 0;
228             $total_voters = 0;
229             for($j = 0; $j < count($people); $j++){
230                 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));
231                 $output = $outputArray[$people[$j]][$pageids[$i]];
232                 $total_cans = $total_cans + $output;
233                 if($output == ""){
234                     $output = "-";
235                 } else {
236                     $total_voters++;
237                 }
238                 $td->pushContent($output);
239                 $tr->pushContent($td);
240             }   
241             if($total_cans == ""){
242                 $total_cans = "-";      
243             }
244             if($total_voters == ""){
245                 $total_voters = "-";    
246             }
247             $td = HTML::td(array('align' => 'right'));
248             $td->pushContent($total_cans); 
249             $tr->pushContent($td);      
250             $td = HTML::td(array('align' => 'right'));
251             $td->pushContent($total_voters); 
252             $tr->pushContent($td);      
253             $table->pushContent($tr);   
254         }
255                 
256         $tr = HTML::tr();
257         $td = HTML::td(array('align' => 'left'));
258         $td->pushContent(HTML::strong(_("Total Budget")));
259         $tr->pushContent($td);
260         $cans_total = 0;
261         $total_voters = 0;
262         for($i = 0; $i < count($people); $i++){
263             $td = HTML::td(array('align' => 'right'));
264             $cans_for_soda = 0;
265             foreach($pageids as $page){
266                 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
267             }
268             $cans = $cans_for_soda;
269             $cans_total = $cans_total + $cans;
270             if($cans == ""){
271                 $cans = "-";
272             } else {
273                 $total_voters++;
274             }
275             $td->pushContent(HTML::strong($cans));
276             $tr->pushContent($td);
277         }
278         $td = HTML::td(array('align' => 'right'));
279         $td->pushContent(HTML::strong($cans_total));
280         $tr->pushContent($td);
281         $td = HTML::td(array('align' => 'right'));
282         $td->pushContent(HTML::strong($total_voters));
283         $tr->pushContent($td);
284         $table->pushContent($tr);
285                 
286         $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
287         $table2->pushContent(HTML::tr(HTML::td($table)));
288         $html->pushContent($table2);
289                 
290         return $html;
291     }
292     
293 };
294
295 // Local Variables:
296 // mode: php
297 // tab-width: 8
298 // c-basic-offset: 4
299 // c-hanging-comment-ender-p: nil
300 // indent-tabs-mode: nil
301 // End:
302 ?>