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