]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PreferenceApp.php
Use CSS
[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 getDescription()
52     {
53         return _("Analyze preferences based on voting budget and ratings.");
54     }
55
56     function getDefaultArguments()
57     {
58         return array(
59             'category' => null,
60             'lockedBudget' => null,
61             'pageTextLabel' => null,
62             'group' => null,
63             'roundCalc' => "true",
64             'neutralRating' => "3",
65             'declareBudget' => "true");
66     }
67
68     // info arg allows multiple columns
69     // info=mtime,hits,summary,version,author,locked,minor
70     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
71
72     function run($dbi, $argstr, &$request, $basepage)
73     {
74
75         extract($this->getArgs($argstr, $request));
76         if ($pageTextLabel == null && $category != null && $group == null) {
77             $group = $category;
78         }
79         if ($category == null || $pageTextLabel == null) {
80             return HTML::div(array('class' => "error"), "PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
81         }
82
83         $dbi = $request->getDbh();
84         $rdbi = RatingsDb::getTheRatingsDb();
85
86         $CATEGORY = $category;
87         $PAGE_TEXT_LABEL = $pageTextLabel;
88         $NEUTRAL_RATING = (int)$neutralRating;
89
90         $active_user = $request->getUser();
91         $html = HTML();
92         $html->pushContent("");
93
94         //Load participating Users
95         $users_array = array();
96         if ($group != null) {
97             $users_array = getMembers($group, $rdbi);
98         } else {
99             $people_iter = $rdbi->sql_get_users_rated();
100             while ($people_array = $people_iter->next()) {
101                 $users_array[] = $people_array['pagename'];
102             }
103         }
104         $people = array();
105         foreach ($users_array as $person_indv) {
106             if ($declareBudget == "true") {
107                 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");
108                 if (count($get_array) == 1) {
109                     $cans_text = $get_array[0];
110                     if (is_numeric($cans_text) && $cans_text >= 0) {
111                         $canBudget[$person_indv] = $cans_text; //Load the persons budget
112                     } else {
113                         $canBudget[$person_indv] = 0;
114                     }
115                     $people[] = $person_indv;
116                 }
117             } else {
118                 $canBudget[$person_indv] = $lockedBudget;
119                 $people[] = $person_indv;
120             }
121         }
122         if (count($people) < 1) {
123             return fmt("Nobody has used %s on their homepage", $PAGE_TEXT_LABEL);
124         }
125         //Get all pages from Category
126         $pageids = array();
127         $category_page = $dbi->getPage($CATEGORY);
128         $iter = $category_page->getLinks();
129         while ($item = $iter->next()) {
130             array_push($pageids, $item->getName());
131         }
132         $ratingTotals = array();
133         foreach ($people as $person) {
134             $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);
135             $ratingTotals[$person] = 0;
136             while ($ratings_array = $ratings_iter->next()) {
137                 $can_rating = $ratings_array['ratingvalue'];
138                 if ($can_rating >= $NEUTRAL_RATING) {
139                     $ratingTotals[$person] += $can_rating;
140                 }
141             }
142         }
143
144         //Generate numbers
145         $canTotals = array();
146         $peopleTotals = array();
147         foreach ($pageids as $soda) {
148             $canTotals[$soda] = 0;
149         }
150         foreach ($people as $person) {
151             foreach ($pageids as $soda) {
152                 $peopleTotals[$person][$soda] = 0;
153             }
154         }
155         foreach ($people as $person) {
156             foreach ($pageids as $page) {
157                 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);
158                 $can_rating_array = $can_rating_iter->next();
159                 $can_rating = $can_rating_array['ratingvalue'];
160                 if ($can_rating >= $NEUTRAL_RATING) {
161                     $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);
162                     if ($roundCalc == "true") {
163                         $adjustedCans = round($calc);
164                     } else {
165                         $adjustedCans = round($calc, 2);
166                     }
167                     $peopleTotals[$person][$page] = $adjustedCans;
168
169                     $canTotals[$page] = $canTotals[$page] + $adjustedCans;
170                 }
171             }
172         }
173         $outputArray = array();
174         foreach ($people as $person) {
175             foreach ($pageids as $page) {
176                 $outputArray[$person][$page] = 0;
177             }
178         }
179
180         $table = HTML::table();
181         $tr = HTML::tr();
182         $td = HTML::td(array('style' => 'background-color:#fff'));
183         $td->pushContent(" ");
184         $tr->pushContent($td);
185
186         foreach ($people as $person) {
187             $td = HTML::td(array('style' => 'background-color:#fff'));
188             $td->pushContent(HTML::a(array('href' => WikiURL($person),
189                     'class' => 'wiki'
190                 ),
191                 SplitPagename($person)));
192             //$td->pushContent(WikiLink(" $person "));
193             $tr->pushContent($td);
194         }
195         $td = HTML::td(array('style' => 'background-color:#fff'));
196         $td->pushContent(_("Total Units"));
197         $tr->pushContent($td);
198         $td = HTML::td(array('style' => 'background-color:#fff'));
199         $td->pushContent(_("Total Voters"));
200         $tr->pushContent($td);
201         $table->pushContent($tr);
202
203         for ($i = 0; $i < count($pageids); $i++) {
204             $total_cans = 0;
205             for ($j = 0; $j < count($people); $j++) {
206                 $td = HTML::td(array('align' => 'right'));
207                 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];
208                 $total_cans = $total_cans + $cans_per_soda;
209                 $outputArray[$people[$j]][$pageids[$i]] = $cans_per_soda;
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('style' => 'text-align:left;background-color:#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('style' => 'text-align:right;background-color:#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('class' => 'align-right'));
290         $td->pushContent(HTML::strong($cans_total));
291         $tr->pushContent($td);
292         $td = HTML::td(array('class' => 'align-right'));
293         $td->pushContent(HTML::strong($total_voters));
294         $tr->pushContent($td);
295         $table->pushContent($tr);
296
297         $table2 = HTML::table(array('style' => 'background-color:#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: