]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PreferenceApp.php
Remove rcs_id
[SourceForge/phpwiki.git] / lib / plugin / PreferenceApp.php
1 <?php //-*-php-*-
2 // $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 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
216         foreach($people as $person){
217             $min_soda = "";
218             $min_cans = 9999999; //9 million, serving as "infinity"
219             $total_cans = 0;
220             foreach($pageids as $page){
221                 $cur_soda_cans = $outputArray[$person][$page];
222                 if($cur_soda_cans < $min_cans && $cur_soda_cans > 0){
223                     $min_cans = $cur_soda_cans;
224                     $min_soda = $page;
225                 }
226                 $total_cans = $total_cans + $cur_soda_cans;
227             }
228             if($total_cans != $canBudget[$person] && $total_cans > 0){
229                 $diff = $canBudget[$person] - $total_cans;
230                 $outputArray[$person][$min_soda] = $outputArray[$person][$min_soda] + $diff;
231             }
232         }
233         for($i = 0; $i < count($pageids); $i++){
234             $tr = HTML::tr();
235             $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
236             $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
237                                            'class' => 'wiki'
238                                            ),
239                                      SplitPagename($pageids[$i])));
240             $tr->pushContent($td);
241             $total_cans = 0;
242             $total_voters = 0;
243             for($j = 0; $j < count($people); $j++){
244                 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));
245                 $output = $outputArray[$people[$j]][$pageids[$i]];
246                 $total_cans = $total_cans + $output;
247                 if($output == ""){
248                     $output = "-";
249                 } else {
250                     $total_voters++;
251                 }
252                 $td->pushContent($output);
253                 $tr->pushContent($td);
254             }
255             if($total_cans == ""){
256                 $total_cans = "-";
257             }
258             if($total_voters == ""){
259                 $total_voters = "-";
260             }
261             $td = HTML::td(array('align' => 'right'));
262             $td->pushContent($total_cans);
263             $tr->pushContent($td);
264             $td = HTML::td(array('align' => 'right'));
265             $td->pushContent($total_voters);
266             $tr->pushContent($td);
267             $table->pushContent($tr);
268         }
269
270         $tr = HTML::tr();
271         $td = HTML::td(array('align' => 'left'));
272         $td->pushContent(HTML::strong(_("Total Budget")));
273         $tr->pushContent($td);
274         $cans_total = 0;
275         $total_voters = 0;
276         for($i = 0; $i < count($people); $i++){
277             $td = HTML::td(array('align' => 'right'));
278             $cans_for_soda = 0;
279             foreach($pageids as $page){
280                 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
281             }
282             $cans = $cans_for_soda;
283             $cans_total = $cans_total + $cans;
284             if($cans == ""){
285                 $cans = "-";
286             } else {
287                 $total_voters++;
288             }
289             $td->pushContent(HTML::strong($cans));
290             $tr->pushContent($td);
291         }
292         $td = HTML::td(array('align' => 'right'));
293         $td->pushContent(HTML::strong($cans_total));
294         $tr->pushContent($td);
295         $td = HTML::td(array('align' => 'right'));
296         $td->pushContent(HTML::strong($total_voters));
297         $tr->pushContent($td);
298         $table->pushContent($tr);
299
300         $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
301         $table2->pushContent(HTML::tr(HTML::td($table)));
302         $html->pushContent($table2);
303
304         return $html;
305     }
306
307 };
308
309 // Local Variables:
310 // mode: php
311 // tab-width: 8
312 // c-basic-offset: 4
313 // c-hanging-comment-ender-p: nil
314 // indent-tabs-mode: nil
315 // End:
316 ?>