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