]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PreferenceApp.php
Update getDescription
[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         $active_userid = $active_user->_userid;
97         $html = HTML();
98         $html->pushContent("");
99
100         //Load participating Users
101         $users_array = array();
102         if ($group != null) {
103             $users_array = getMembers($group, $rdbi);
104         } else {
105             $people_iter = $rdbi->sql_get_users_rated();
106             while ($people_array = $people_iter->next()) {
107                 $users_array[] = $people_array['pagename'];
108             }
109         }
110         $people = array();
111         foreach ($users_array as $person_indv) {
112             if ($declareBudget == "true") {
113                 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");
114                 if (count($get_array) == 1) {
115                     $cans_text = $get_array[0];
116                     if (is_numeric($cans_text) && $cans_text >= 0) {
117                         $canBudget[$person_indv] = $cans_text; //Load the persons budget
118                     } else {
119                         $canBudget[$person_indv] = 0;
120                     }
121                     $people[] = $person_indv;
122                 }
123             } else {
124                 $canBudget[$person_indv] = $lockedBudget;
125                 $people[] = $person_indv;
126             }
127         }
128         if (count($people) < 1) {
129             return fmt("Nobody has used %s on their homepage", $PAGE_TEXT_LABEL);
130         }
131         //Get all pages from Category
132         $pageids = array();
133         $category_page = $dbi->getPage($CATEGORY);
134         $iter = $category_page->getLinks();
135         while ($item = $iter->next()) {
136             array_push($pageids, $item->getName());
137         }
138         $ratingTotals = array();
139         foreach ($people as $person) {
140             $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);
141             $ratingTotals[$person] = 0;
142             while ($ratings_array = $ratings_iter->next()) {
143                 $can_rating = $ratings_array['ratingvalue'];
144                 if ($can_rating >= $NEUTRAL_RATING) {
145                     $ratingTotals[$person] += $can_rating;
146                 }
147             }
148         }
149
150         //Generate numbers
151         $canTotals = array();
152         $peopleTotals = array();
153         foreach ($pageids as $soda) {
154             $canTotals[$soda] = 0;
155         }
156         foreach ($people as $person) {
157             foreach ($pageids as $soda) {
158                 $peopleTotals[$person][$soda] = 0;
159             }
160         }
161         foreach ($people as $person) {
162             foreach ($pageids as $page) {
163                 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);
164                 $can_rating_array = $can_rating_iter->next();
165                 $can_rating = $can_rating_array['ratingvalue'];
166                 if ($can_rating >= $NEUTRAL_RATING) {
167                     $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);
168                     if ($roundCalc == "true") {
169                         $adjustedCans = round($calc);
170                     } else {
171                         $adjustedCans = round($calc, 2);
172                     }
173                     $peopleTotals[$person][$page] = $adjustedCans;
174
175                     $canTotals[$page] = $canTotals[$page] + $adjustedCans;
176                 }
177             }
178         }
179         $outputArray = array();
180         foreach ($people as $person) {
181             foreach ($pageids as $page) {
182                 $outputArray[$person][$page] = 0;
183             }
184         }
185
186         $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
187         $tr = HTML::tr();
188         $td = HTML::td(array('bgcolor' => '#FFFFFF'));
189         $td->pushContent(" ");
190         $tr->pushContent($td);
191
192         foreach ($people as $person) {
193             $td = HTML::td(array('bgcolor' => '#FFFFFF'));
194             $td->pushContent(HTML::a(array('href' => WikiURL($person),
195                     'class' => 'wiki'
196                 ),
197                 SplitPagename($person)));
198             //$td->pushContent(WikiLink(" $person "));
199             $tr->pushContent($td);
200         }
201         $td = HTML::td(array('bgcolor' => '#FFFFFF'));
202         $td->pushContent(_("Total Units"));
203         $tr->pushContent($td);
204         $td = HTML::td(array('bgcolor' => '#FFFFFF'));
205         $td->pushContent(_("Total Voters"));
206         $tr->pushContent($td);
207         $table->pushContent($tr);
208
209         for ($i = 0; $i < count($pageids); $i++) {
210             $total_cans = 0;
211             for ($j = 0; $j < count($people); $j++) {
212                 $td = HTML::td(array('align' => 'right'));
213                 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];
214                 $total_cans = $total_cans + $cans_per_soda;
215                 $outputArray[$people[$j]][$pageids[$i]] = $cans_per_soda;
216             }
217         }
218
219         foreach ($people as $person) {
220             $min_soda = "";
221             $min_cans = 9999999; //9 million, serving as "infinity"
222             $total_cans = 0;
223             foreach ($pageids as $page) {
224                 $cur_soda_cans = $outputArray[$person][$page];
225                 if ($cur_soda_cans < $min_cans && $cur_soda_cans > 0) {
226                     $min_cans = $cur_soda_cans;
227                     $min_soda = $page;
228                 }
229                 $total_cans = $total_cans + $cur_soda_cans;
230             }
231             if ($total_cans != $canBudget[$person] && $total_cans > 0) {
232                 $diff = $canBudget[$person] - $total_cans;
233                 $outputArray[$person][$min_soda] = $outputArray[$person][$min_soda] + $diff;
234             }
235         }
236         for ($i = 0; $i < count($pageids); $i++) {
237             $tr = HTML::tr();
238             $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
239             $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
240                     'class' => 'wiki'
241                 ),
242                 SplitPagename($pageids[$i])));
243             $tr->pushContent($td);
244             $total_cans = 0;
245             $total_voters = 0;
246             for ($j = 0; $j < count($people); $j++) {
247                 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));
248                 $output = $outputArray[$people[$j]][$pageids[$i]];
249                 $total_cans = $total_cans + $output;
250                 if ($output == "") {
251                     $output = "-";
252                 } else {
253                     $total_voters++;
254                 }
255                 $td->pushContent($output);
256                 $tr->pushContent($td);
257             }
258             if ($total_cans == "") {
259                 $total_cans = "-";
260             }
261             if ($total_voters == "") {
262                 $total_voters = "-";
263             }
264             $td = HTML::td(array('align' => 'right'));
265             $td->pushContent($total_cans);
266             $tr->pushContent($td);
267             $td = HTML::td(array('align' => 'right'));
268             $td->pushContent($total_voters);
269             $tr->pushContent($td);
270             $table->pushContent($tr);
271         }
272
273         $tr = HTML::tr();
274         $td = HTML::td(array('align' => 'left'));
275         $td->pushContent(HTML::strong(_("Total Budget")));
276         $tr->pushContent($td);
277         $cans_total = 0;
278         $total_voters = 0;
279         for ($i = 0; $i < count($people); $i++) {
280             $td = HTML::td(array('align' => 'right'));
281             $cans_for_soda = 0;
282             foreach ($pageids as $page) {
283                 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
284             }
285             $cans = $cans_for_soda;
286             $cans_total = $cans_total + $cans;
287             if ($cans == "") {
288                 $cans = "-";
289             } else {
290                 $total_voters++;
291             }
292             $td->pushContent(HTML::strong($cans));
293             $tr->pushContent($td);
294         }
295         $td = HTML::td(array('align' => 'right'));
296         $td->pushContent(HTML::strong($cans_total));
297         $tr->pushContent($td);
298         $td = HTML::td(array('align' => 'right'));
299         $td->pushContent(HTML::strong($total_voters));
300         $tr->pushContent($td);
301         $table->pushContent($tr);
302
303         $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
304         $table2->pushContent(HTML::tr(HTML::td($table)));
305         $html->pushContent($table2);
306
307         return $html;
308     }
309
310 }
311
312 // Local Variables:
313 // mode: php
314 // tab-width: 8
315 // c-basic-offset: 4
316 // c-hanging-comment-ender-p: nil
317 // indent-tabs-mode: nil
318 // End: