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
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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');
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');
49 class WikiPlugin_PreferenceApp
53 return _("PreferenceApp");
56 function getDescription () {
57 return _("Analyzes preferences based on voting budget and ratings.");
60 function getDefaultArguments() {
63 'lockedBudget' => null,
64 'pageTextLabel' => null,
66 'roundCalc' => "true",
67 'neutralRating' => "3",
68 'declareBudget' => "true");
70 // info arg allows multiple columns
71 // info=mtime,hits,summary,version,author,locked,minor
72 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
74 function run($dbi, $argstr, &$request, $basepage) {
76 extract($this->getArgs($argstr, $request));
77 if($pageTextLabel == null && $category != null && $group == null){
80 if($category == null || $pageTextLabel == null){
81 return HTML::div(array('class' => "error"), "PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
84 $dbi = $request->getDbh();
85 $rdbi = RatingsDb::getTheRatingsDb();
87 $CATEGORY = $category;
88 $PAGE_TEXT_LABEL = $pageTextLabel;
89 $NEUTRAL_RATING = (int)$neutralRating;
91 $active_user = $request->getUser();
92 $active_userid = $active_user->_userid;
94 $html->pushContent("");
96 //Load participating Users
97 $users_array = array();
99 $users_array = getMembers($group, $rdbi);
101 $people_iter = $rdbi->sql_get_users_rated();
102 while($people_array = $people_iter->next()){
103 $users_array[] = $people_array['pagename'];
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
115 $canBudget[$person_indv] = 0;
117 $people[] = $person_indv;
120 $canBudget[$person_indv] = $lockedBudget;
121 $people[] = $person_indv;
124 if(count($people) < 1){
125 return fmt("Nobody has used %s on their homepage", $PAGE_TEXT_LABEL);
127 //Get all pages from Category
129 $category_page = $dbi->getPage($CATEGORY);
130 $iter = $category_page->getLinks();
131 while ($item = $iter->next()){
132 array_push($pageids, $item->getName());
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;
147 $canTotals = array();
148 $peopleTotals = array();
149 foreach($pageids as $soda){
150 $canTotals[$soda] = 0;
152 foreach($people as $person){
153 foreach($pageids as $soda){
154 $peopleTotals[$person][$soda] = 0;
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);
167 $adjustedCans = round($calc, 2);
169 $peopleTotals[$person][$page] = $adjustedCans;
171 $canTotals[$page] = $canTotals[$page] + $adjustedCans;
175 $outputArray = array();
176 foreach($people as $person){
177 foreach($pageids as $page){
178 $outputArray[$person][$page] = 0;
182 $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
184 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
185 $td->pushContent(" ");
186 $tr->pushContent($td);
188 foreach($people as $person){
189 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
190 $td->pushContent(HTML::a(array('href' => WikiURL($person),
193 SplitPagename($person)));
194 //$td->pushContent(WikiLink(" $person "));
195 $tr->pushContent($td);
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);
205 for($i = 0; $i < count($pageids); $i++){
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;
216 foreach($people as $person){
218 $min_cans = 9999999; //9 million, serving as "infinity"
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;
226 $total_cans = $total_cans + $cur_soda_cans;
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;
233 for($i = 0; $i < count($pageids); $i++){
235 $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
236 $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
239 SplitPagename($pageids[$i])));
240 $tr->pushContent($td);
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;
252 $td->pushContent($output);
253 $tr->pushContent($td);
255 if($total_cans == ""){
258 if($total_voters == ""){
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);
271 $td = HTML::td(array('align' => 'left'));
272 $td->pushContent(HTML::strong(_("Total Budget")));
273 $tr->pushContent($td);
276 for($i = 0; $i < count($people); $i++){
277 $td = HTML::td(array('align' => 'right'));
279 foreach($pageids as $page){
280 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
282 $cans = $cans_for_soda;
283 $cans_total = $cans_total + $cans;
289 $td->pushContent(HTML::strong($cans));
290 $tr->pushContent($td);
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);
300 $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
301 $table2->pushContent(HTML::tr(HTML::td($table)));
302 $html->pushContent($table2);
313 // c-hanging-comment-ender-p: nil
314 // indent-tabs-mode: nil