4 * Copyright (C) 2004 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
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.
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.
29 * An example of a budget decleration would be "TotalSoda: 50" on my homepage.
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.
35 * Author: mcassano circa April 2004
38 * <<PreferenceApp category="Soda" pageTextLabel="TotalSoda" roundCalc="true" >>
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';
48 class WikiPlugin_PreferenceApp
51 function getDescription()
53 return _("Analyze preferences based on voting budget and ratings.");
56 function getDefaultArguments()
60 'lockedBudget' => null,
61 'pageTextLabel' => null,
63 'roundCalc' => "true",
64 'neutralRating' => "3",
65 'declareBudget' => "true");
68 // info arg allows multiple columns
69 // info=mtime,hits,summary,version,author,locked,minor
70 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
72 function run($dbi, $argstr, &$request, $basepage)
75 extract($this->getArgs($argstr, $request));
76 if ($pageTextLabel == null && $category != null && $group == null) {
79 if ($category == null || $pageTextLabel == null) {
80 return HTML::div(array('class' => "error"), "PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
83 $dbi = $request->getDbh();
84 $rdbi = RatingsDb::getTheRatingsDb();
86 $CATEGORY = $category;
87 $PAGE_TEXT_LABEL = $pageTextLabel;
88 $NEUTRAL_RATING = (int)$neutralRating;
90 $active_user = $request->getUser();
92 $html->pushContent("");
94 //Load participating Users
95 $users_array = array();
97 $users_array = getMembers($group, $rdbi);
99 $people_iter = $rdbi->sql_get_users_rated();
100 while ($people_array = $people_iter->next()) {
101 $users_array[] = $people_array['pagename'];
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
113 $canBudget[$person_indv] = 0;
115 $people[] = $person_indv;
118 $canBudget[$person_indv] = $lockedBudget;
119 $people[] = $person_indv;
122 if (count($people) < 1) {
123 return fmt("Nobody has used %s on their homepage", $PAGE_TEXT_LABEL);
125 //Get all pages from Category
127 $category_page = $dbi->getPage($CATEGORY);
128 $iter = $category_page->getLinks();
129 while ($item = $iter->next()) {
130 array_push($pageids, $item->getName());
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;
145 $canTotals = array();
146 $peopleTotals = array();
147 foreach ($pageids as $soda) {
148 $canTotals[$soda] = 0;
150 foreach ($people as $person) {
151 foreach ($pageids as $soda) {
152 $peopleTotals[$person][$soda] = 0;
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);
165 $adjustedCans = round($calc, 2);
167 $peopleTotals[$person][$page] = $adjustedCans;
169 $canTotals[$page] = $canTotals[$page] + $adjustedCans;
173 $outputArray = array();
174 foreach ($people as $person) {
175 foreach ($pageids as $page) {
176 $outputArray[$person][$page] = 0;
180 $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
182 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
183 $td->pushContent(" ");
184 $tr->pushContent($td);
186 foreach ($people as $person) {
187 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
188 $td->pushContent(HTML::a(array('href' => WikiURL($person),
191 SplitPagename($person)));
192 //$td->pushContent(WikiLink(" $person "));
193 $tr->pushContent($td);
195 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
196 $td->pushContent(_("Total Units"));
197 $tr->pushContent($td);
198 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
199 $td->pushContent(_("Total Voters"));
200 $tr->pushContent($td);
201 $table->pushContent($tr);
203 for ($i = 0; $i < count($pageids); $i++) {
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;
213 foreach ($people as $person) {
215 $min_cans = 9999999; //9 million, serving as "infinity"
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;
223 $total_cans = $total_cans + $cur_soda_cans;
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;
230 for ($i = 0; $i < count($pageids); $i++) {
232 $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
233 $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
236 SplitPagename($pageids[$i])));
237 $tr->pushContent($td);
240 for ($j = 0; $j < count($people); $j++) {
241 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));
242 $output = $outputArray[$people[$j]][$pageids[$i]];
243 $total_cans = $total_cans + $output;
249 $td->pushContent($output);
250 $tr->pushContent($td);
252 if ($total_cans == "") {
255 if ($total_voters == "") {
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);
268 $td = HTML::td(array('align' => 'left'));
269 $td->pushContent(HTML::strong(_("Total Budget")));
270 $tr->pushContent($td);
273 for ($i = 0; $i < count($people); $i++) {
274 $td = HTML::td(array('align' => 'right'));
276 foreach ($pageids as $page) {
277 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
279 $cans = $cans_for_soda;
280 $cans_total = $cans_total + $cans;
286 $td->pushContent(HTML::strong($cans));
287 $tr->pushContent($td);
289 $td = HTML::td(array('align' => 'right'));
290 $td->pushContent(HTML::strong($cans_total));
291 $tr->pushContent($td);
292 $td = HTML::td(array('align' => 'right'));
293 $td->pushContent(HTML::strong($total_voters));
294 $tr->pushContent($td);
295 $table->pushContent($tr);
297 $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
298 $table2->pushContent(HTML::tr(HTML::td($table)));
299 $html->pushContent($table2);
310 // c-hanging-comment-ender-p: nil
311 // indent-tabs-mode: nil