2 rcs_id('$Id: PreferenceApp.php,v 1.5 2007-05-29 16:56:29 rurban Exp $');
7 require_once('lib/PageList.php');
8 require_once('lib/InlineParser.php');
10 require_once('lib/wikilens/Utils.php');
11 require_once('lib/Theme.php');
12 require_once('lib/wikilens/Buddy.php');
13 require_once('lib/wikilens/RatingsDb.php');
16 * PreferenceApp is used to analyze a category of items that a group
17 * of people have rated. A user is grouped to be analyzed in the group by
18 * 1) having rated at least one item in the database and 2) matching the optional
19 * criteria for declaring a budget on their homepage.
21 * An example of a budget decleration would be "TotalSoda: 50" on my homepage.
23 * PreferenceApp will output a matrix style table shows "how much" fractionally
24 * a group of people prefer an item over other items. For example, if my soda
25 * budget is 100 then PreferenceApp might assign 20 units of my budget to Moutain Dew.
27 * Author: mcassano circa April 2004
30 * <?plugin PreferenceApp category="Soda" pageLabel="TotalSoda" roundCalc="true" ?>
33 class WikiPlugin_PreferenceApp
37 return _("PreferenceApp");
40 function getDescription () {
41 return _("Analyzes preferences based on voting budget and ratings.");
44 function getVersion() {
45 return preg_replace("/[Revision: $]/", '',
49 function getDefaultArguments() {
52 'lockedBudget' => null,
53 'pageTextLabel' => null,
55 'roundCalc' => "true",
56 'neutralRating' => "3",
57 'declareBudget' => "true");
59 // info arg allows multiple columns
60 // info=mtime,hits,summary,version,author,locked,minor
61 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
63 function run($dbi, $argstr, &$request, $basepage) {
65 extract($this->getArgs($argstr, $request));
66 if($pageTextLabel == null && $category != null && $group == null){
69 if($category == null || $pageTextLabel == null){
70 $html = HTML("PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
74 $dbi = $request->getDbh();
75 $rdbi = RatingsDb::getTheRatingsDb();
77 $CATEGORY = $category;
78 $PAGE_TEXT_LABEL = $pageTextLabel;
79 $NEUTRAL_RATING = (int)$neutralRating;
81 $active_user = $request->getUser();
82 $active_userid = $active_user->_userid;
84 $html->pushContent("");
86 //Load participating Users
87 $users_array = array();
89 $users_array = getMembers($group, $rdbi);
91 $people_iter = $rdbi->sql_get_users_rated();
92 while($people_array = $people_iter->next()){
93 $users_array[] = $people_array['pagename'];
97 foreach($users_array as $person_indv){
98 if($declareBudget == "true"){
99 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");
100 if(count($get_array) == 1){
101 $cans_text = $get_array[0];
102 if(is_numeric($cans_text) && $cans_text >= 0){
103 $canBudget[$person_indv] = $cans_text; //Load the persons budget
105 $canBudget[$person_indv] = 0;
107 $people[] = $person_indv;
110 $canBudget[$person_indv] = $lockedBudget;
111 $people[] = $person_indv;
114 if(count($people) < 1){
115 return fmt("Nobody has used %s on their homepage", $PAGE_TEXT_LABEL);
117 //Get all pages from Category
119 $category_page = $dbi->getPage($CATEGORY);
120 $iter = $category_page->getLinks();
121 while ($item = $iter->next()){
122 array_push($pageids, $item->getName());
124 $ratingTotals = array();
125 foreach ($people as $person){
126 $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);
127 $ratingTotals[$person] = 0;
128 while($ratings_array = $ratings_iter->next()){
129 $can_rating = $ratings_array['ratingvalue'];
130 if($can_rating >= $NEUTRAL_RATING){
131 $ratingTotals[$person] += $can_rating;
137 $canTotals = array();
138 $peopleTotals = array();
139 foreach($pageids as $soda){
140 $canTotals[$soda] = 0;
142 foreach($people as $person){
143 foreach($pageids as $soda){
144 $peopleTotals[$person][$soda] = 0;
147 foreach($people as $person){
148 foreach($pageids as $page){
149 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);
150 $can_rating_array = $can_rating_iter->next();
151 $can_rating = $can_rating_array['ratingvalue'];
152 if($can_rating >= $NEUTRAL_RATING){
153 $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);
154 if($roundCalc == "true"){
155 $adjustedCans = round($calc);
157 $adjustedCans = round($calc, 2);
159 $peopleTotals[$person][$page] = $adjustedCans;
161 $canTotals[$page] = $canTotals[$page] + $adjustedCans;
165 $outputArray = array();
166 foreach($people as $person){
167 foreach($pageids as $page){
168 $outputArray[$person][$page] = 0;
172 $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
174 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
175 $td->pushContent(" ");
176 $tr->pushContent($td);
178 foreach($people as $person){
179 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
180 $td->pushContent(HTML::a(array('href' => WikiURL($person),
183 SplitPagename($person)));
184 //$td->pushContent(WikiLink(" $person "));
185 $tr->pushContent($td);
187 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
188 $td->pushContent(_("Total Units"));
189 $tr->pushContent($td);
190 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
191 $td->pushContent(_("Total Voters"));
192 $tr->pushContent($td);
193 $table->pushContent($tr);
195 for($i = 0; $i < count($pageids); $i++){
197 for($j = 0; $j < count($people); $j++){
198 $td = HTML::td(array('align' => 'right'));
199 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];
200 $total_cans = $total_cans + $cans_per_soda;
201 $outputArray[$people[$j]][$pageids[$i]] = $cans_per_soda;
206 foreach($people as $person){
208 $min_cans = 9999999; //9 million, serving as "infinity"
210 foreach($pageids as $page){
211 $cur_soda_cans = $outputArray[$person][$page];
212 if($cur_soda_cans < $min_cans && $cur_soda_cans > 0){
213 $min_cans = $cur_soda_cans;
216 $total_cans = $total_cans + $cur_soda_cans;
218 if($total_cans != $canBudget[$person] && $total_cans > 0){
219 $diff = $canBudget[$person] - $total_cans;
220 $outputArray[$person][$min_soda] = $outputArray[$person][$min_soda] + $diff;
223 for($i = 0; $i < count($pageids); $i++){
225 $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
226 $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
229 SplitPagename($pageids[$i])));
230 $tr->pushContent($td);
233 for($j = 0; $j < count($people); $j++){
234 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));
235 $output = $outputArray[$people[$j]][$pageids[$i]];
236 $total_cans = $total_cans + $output;
242 $td->pushContent($output);
243 $tr->pushContent($td);
245 if($total_cans == ""){
248 if($total_voters == ""){
251 $td = HTML::td(array('align' => 'right'));
252 $td->pushContent($total_cans);
253 $tr->pushContent($td);
254 $td = HTML::td(array('align' => 'right'));
255 $td->pushContent($total_voters);
256 $tr->pushContent($td);
257 $table->pushContent($tr);
261 $td = HTML::td(array('align' => 'left'));
262 $td->pushContent(HTML::strong(_("Total Budget")));
263 $tr->pushContent($td);
266 for($i = 0; $i < count($people); $i++){
267 $td = HTML::td(array('align' => 'right'));
269 foreach($pageids as $page){
270 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
272 $cans = $cans_for_soda;
273 $cans_total = $cans_total + $cans;
279 $td->pushContent(HTML::strong($cans));
280 $tr->pushContent($td);
282 $td = HTML::td(array('align' => 'right'));
283 $td->pushContent(HTML::strong($cans_total));
284 $tr->pushContent($td);
285 $td = HTML::td(array('align' => 'right'));
286 $td->pushContent(HTML::strong($total_voters));
287 $tr->pushContent($td);
288 $table->pushContent($tr);
290 $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
291 $table2->pushContent(HTML::tr(HTML::td($table)));
292 $html->pushContent($table2);
303 // c-hanging-comment-ender-p: nil
304 // indent-tabs-mode: nil