2 rcs_id('$Id: PreferenceApp.php,v 1.4 2004-12-20 16:05:14 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
64 function run($dbi, $argstr, &$request, $basepage) {
66 extract($this->getArgs($argstr, $request));
67 if($pageTextLabel == null && $category != null && $group == null){
72 if($category == null || $pageTextLabel == null){
73 $html = HTML("PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
78 $dbi = $request->getDbh();
79 $rdbi = RatingsDb::getTheRatingsDb();
81 $CATEGORY = $category;
82 $PAGE_TEXT_LABEL = $pageTextLabel;
83 $NEUTRAL_RATING = (int)$neutralRating;
85 $active_user = $request->getUser();
86 $active_userid = $active_user->_userid;
88 $html->pushContent("");
90 //Load participating Users
91 $users_array = array();
93 $users_array = getMembers($group, $rdbi);
96 $people_iter = $rdbi->sql_get_users_rated();
97 while($people_array = $people_iter->next()){
98 $users_array[] = $people_array['pagename'];
102 foreach($users_array as $person_indv){
103 if($declareBudget == "true"){
104 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");
105 if(count($get_array) == 1){
106 $cans_text = $get_array[0];
107 if(is_numeric($cans_text) && $cans_text >= 0){
108 $canBudget[$person_indv] = $cans_text; //Load the persons budget
110 $canBudget[$person_indv] = 0;
112 $people[] = $person_indv;
115 $canBudget[$person_indv] = $lockedBudget;
116 $people[] = $person_indv;
119 if(count($people) < 1){
120 $html = HTML("Nobody has used $PAGE_TEXT_LABEL on their homepage");
123 //Get all pages from Category
125 $category_page = $dbi->getPage($CATEGORY);
126 $iter = $category_page->getLinks();
127 while($item = $iter->next()){
128 array_push($pageids, $item->getName());
130 $ratingTotals = array();
131 foreach($people as $person){
132 $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);
133 $ratingTotals[$person] = 0;
134 while($ratings_array = $ratings_iter->next()){
135 $can_rating = $ratings_array['ratingvalue'];
136 if($can_rating >= $NEUTRAL_RATING){
137 $ratingTotals[$person] += $can_rating;
143 $canTotals = array();
144 $peopleTotals = array();
145 foreach($pageids as $soda){
146 $canTotals[$soda] = 0;
148 foreach($people as $person){
149 foreach($pageids as $soda){
150 $peopleTotals[$person][$soda] = 0;
153 foreach($people as $person){
154 foreach($pageids as $page){
155 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);
156 $can_rating_array = $can_rating_iter->next();
157 $can_rating = $can_rating_array['ratingvalue'];
158 if($can_rating >= $NEUTRAL_RATING){
159 $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);
160 if($roundCalc == "true"){
161 $adjustedCans = round($calc);
163 $adjustedCans = round($calc, 2);
165 $peopleTotals[$person][$page] = $adjustedCans;
167 $canTotals[$page] = $canTotals[$page] + $adjustedCans;
171 $outputArray = array();
172 foreach($people as $person){
173 foreach($pageids as $page){
174 $outputArray[$person][$page] = 0;
178 $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
180 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
181 $td->pushContent(" ");
182 $tr->pushContent($td);
184 foreach($people as $person){
185 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
186 $td->pushContent(HTML::a(array('href' => WikiURL($person),
189 SplitPagename($person)));
190 //$td->pushContent(WikiLink(" $person "));
191 $tr->pushContent($td);
193 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
194 $td->pushContent("Total Units");
195 $tr->pushContent($td);
196 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
197 $td->pushContent("Total Voters");
198 $tr->pushContent($td);
199 $table->pushContent($tr);
201 for($i = 0; $i < count($pageids); $i++){
203 for($j = 0; $j < count($people); $j++){
204 $td = HTML::td(array('align' => 'right'));
205 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];
206 $total_cans = $total_cans + $cans_per_soda;
207 $outputArray[$people[$j]][$pageids[$i]] = $cans_per_soda;
212 foreach($people as $person){
214 $min_cans = 9999999; //9 million, serving as "infinity"
216 foreach($pageids as $page){
217 $cur_soda_cans = $outputArray[$person][$page];
218 if($cur_soda_cans < $min_cans && $cur_soda_cans > 0){
219 $min_cans = $cur_soda_cans;
222 $total_cans = $total_cans + $cur_soda_cans;
224 if($total_cans != $canBudget[$person] && $total_cans > 0){
225 $diff = $canBudget[$person] - $total_cans;
226 $outputArray[$person][$min_soda] = $outputArray[$person][$min_soda] + $diff;
229 for($i = 0; $i < count($pageids); $i++){
231 $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
232 $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
235 SplitPagename($pageids[$i])));
236 $tr->pushContent($td);
239 for($j = 0; $j < count($people); $j++){
240 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));
241 $output = $outputArray[$people[$j]][$pageids[$i]];
242 $total_cans = $total_cans + $output;
248 $td->pushContent($output);
249 $tr->pushContent($td);
251 if($total_cans == ""){
254 if($total_voters == ""){
257 $td = HTML::td(array('align' => 'right'));
258 $td->pushContent($total_cans);
259 $tr->pushContent($td);
260 $td = HTML::td(array('align' => 'right'));
261 $td->pushContent($total_voters);
262 $tr->pushContent($td);
263 $table->pushContent($tr);
267 $td = HTML::td(array('align' => 'left'));
268 $td->pushContent(HTML::strong("Total Budget"));
269 $tr->pushContent($td);
272 for($i = 0; $i < count($people); $i++){
273 $td = HTML::td(array('align' => 'right'));
275 foreach($pageids as $page){
276 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
278 $cans = $cans_for_soda;
279 $cans_total = $cans_total + $cans;
285 $td->pushContent(HTML::strong($cans));
286 $tr->pushContent($td);
288 $td = HTML::td(array('align' => 'right'));
289 $td->pushContent(HTML::strong($cans_total));
290 $tr->pushContent($td);
291 $td = HTML::td(array('align' => 'right'));
292 $td->pushContent(HTML::strong($total_voters));
293 $tr->pushContent($td);
294 $table->pushContent($tr);
296 $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
297 $table2->pushContent(HTML::tr(HTML::td($table)));
298 $html->pushContent($table2);
309 // c-hanging-comment-ender-p: nil
310 // indent-tabs-mode: nil