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