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