2 rcs_id('$Id: PreferenceApp.php,v 1.2 2004-07-07 21:00:53 dfrankow 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');
17 * PreferenceApp is used to analyze a category of items that a group
18 * of people have rated. A user is grouped to be analyzed in the group by
19 * 1) having rated at least one item in the database and 2) matching the optional
20 * criteria for declaring a budget on their homepage.
22 * An example of a budget decleration would be "TotalSoda: 50" on my homepage.
24 * PreferenceApp will output a matrix style table shows "how much" fractionally
25 * a group of people prefer an item over other items. For example, if my soda
26 * budget is 100 then PreferenceApp might assign 20 units of my budget to Moutain Dew.
28 * Author: mcassano circa April 2004
31 * <?plugin PreferenceApp category="Soda" pageLabel="TotalSoda" roundCalc="true" ?>
34 class WikiPlugin_PreferenceApp
38 return _("Preference App");
41 function getDescription () {
42 return _("Analyzes Preferences based on voting budget and ratings.");
45 function getVersion() {
46 return preg_replace("/[Revision: $]/", '',
50 function getDefaultArguments() {
53 'lockedBudget' => null,
54 'pageTextLabel' => null,
56 'roundCalc' => "true",
57 'neutralRating' => "3",
58 'declareBudget' => "true");
60 // info arg allows multiple columns
61 // info=mtime,hits,summary,version,author,locked,minor
62 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
65 function run($dbi, $argstr, $request) {
67 extract($this->getArgs($argstr, $request));
68 if($pageTextLabel == null && $category != null && $group == null){
73 if($category == null || $pageTextLabel == null){
74 $html = HTML("PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
79 $dbi = $request->getDbh();
80 $rdbi = RatingsDb::getTheRatingsDb();
82 $CATEGORY = $category;
83 $PAGE_TEXT_LABEL = $pageTextLabel;
84 $NEUTRAL_RATING = (int)$neutralRating;
86 $active_user = $request->getUser();
87 $active_userid = $active_user->_userid;
89 $html->pushContent("");
91 //Load participating Users
92 $users_array = array();
94 $users_array = getMembers($group, $rdbi);
97 $people_iter = $rdbi->sql_get_users_rated();
98 while($people_array = $people_iter->next()){
99 $users_array[] = $people_array['pagename'];
103 foreach($users_array as $person_indv){
104 if($declareBudget == "true"){
105 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");
106 if(count($get_array) == 1){
107 $cans_text = $get_array[0];
108 if(is_numeric($cans_text) && $cans_text >= 0){
109 $canBudget[$person_indv] = $cans_text; //Load the persons budget
111 $canBudget[$person_indv] = 0;
113 $people[] = $person_indv;
116 $canBudget[$person_indv] = $lockedBudget;
117 $people[] = $person_indv;
120 if(count($people) < 1){
121 $html = HTML("Nobody has used $PAGE_TEXT_LABEL on their homepage");
124 //Get all pages from Category
126 $category_page = $dbi->getPage($CATEGORY);
127 $iter = $category_page->getLinks();
128 while($item = $iter->next()){
129 array_push($pageids, $item->getName());
131 $ratingTotals = array();
132 foreach($people as $person){
133 $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);
134 $ratingTotals[$person] = 0;
135 while($ratings_array = $ratings_iter->next()){
136 $can_rating = $ratings_array['ratingvalue'];
137 if($can_rating >= $NEUTRAL_RATING){
138 $ratingTotals[$person] += $can_rating;
144 $canTotals = array();
145 $peopleTotals = array();
146 foreach($pageids as $soda){
147 $canTotals[$soda] = 0;
149 foreach($people as $person){
150 foreach($pageids as $soda){
151 $peopleTotals[$person][$soda] = 0;
154 foreach($people as $person){
155 foreach($pageids as $page){
156 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);
157 $can_rating_array = $can_rating_iter->next();
158 $can_rating = $can_rating_array['ratingvalue'];
159 if($can_rating >= $NEUTRAL_RATING){
160 $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);
161 if($roundCalc == "true"){
162 $adjustedCans = round($calc);
164 $adjustedCans = round($calc, 2);
166 $peopleTotals[$person][$page] = $adjustedCans;
168 $canTotals[$page] = $canTotals[$page] + $adjustedCans;
172 $outputArray = array();
173 foreach($people as $person){
174 foreach($pageids as $page){
175 $outputArray[$person][$page] = 0;
179 $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
181 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
182 $td->pushContent(" ");
183 $tr->pushContent($td);
185 foreach($people as $person){
186 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
187 $td->pushContent(HTML::a(array('href' => WikiURL($person),
190 SplitPagename($person)));
191 //$td->pushContent(WikiLink(" $person "));
192 $tr->pushContent($td);
194 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
195 $td->pushContent("Total Units");
196 $tr->pushContent($td);
197 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
198 $td->pushContent("Total Voters");
199 $tr->pushContent($td);
200 $table->pushContent($tr);
202 for($i = 0; $i < count($pageids); $i++){
204 for($j = 0; $j < count($people); $j++){
205 $td = HTML::td(array('align' => 'right'));
206 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];
207 $total_cans = $total_cans + $cans_per_soda;
208 $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