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