4 require_once('lib/PageList.php');
5 require_once('lib/InlineParser.php');
7 require_once('lib/wikilens/Utils.php');
8 require_once('lib/WikiTheme.php');
9 require_once('lib/wikilens/Buddy.php');
10 require_once('lib/wikilens/RatingsDb.php');
13 * PreferenceApp is used to analyze a category of items that a group
14 * of people have rated. A user is grouped to be analyzed in the group by
15 * 1) having rated at least one item in the database and 2) matching the optional
16 * criteria for declaring a budget on their homepage.
18 * An example of a budget decleration would be "TotalSoda: 50" on my homepage.
20 * PreferenceApp will output a matrix style table shows "how much" fractionally
21 * a group of people prefer an item over other items. For example, if my soda
22 * budget is 100 then PreferenceApp might assign 20 units of my budget to Moutain Dew.
24 * Author: mcassano circa April 2004
27 * <?plugin PreferenceApp category="Soda" pageLabel="TotalSoda" roundCalc="true" ?>
30 class WikiPlugin_PreferenceApp
34 return _("PreferenceApp");
37 function getDescription () {
38 return _("Analyzes preferences based on voting budget and ratings.");
41 function getVersion() {
42 return preg_replace("/[Revision: $]/", '',
46 function getDefaultArguments() {
49 'lockedBudget' => null,
50 'pageTextLabel' => null,
52 'roundCalc' => "true",
53 'neutralRating' => "3",
54 'declareBudget' => "true");
56 // info arg allows multiple columns
57 // info=mtime,hits,summary,version,author,locked,minor
58 // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges
60 function run($dbi, $argstr, &$request, $basepage) {
62 extract($this->getArgs($argstr, $request));
63 if($pageTextLabel == null && $category != null && $group == null){
66 if($category == null || $pageTextLabel == null){
67 $html = HTML("PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
71 $dbi = $request->getDbh();
72 $rdbi = RatingsDb::getTheRatingsDb();
74 $CATEGORY = $category;
75 $PAGE_TEXT_LABEL = $pageTextLabel;
76 $NEUTRAL_RATING = (int)$neutralRating;
78 $active_user = $request->getUser();
79 $active_userid = $active_user->_userid;
81 $html->pushContent("");
83 //Load participating Users
84 $users_array = array();
86 $users_array = getMembers($group, $rdbi);
88 $people_iter = $rdbi->sql_get_users_rated();
89 while($people_array = $people_iter->next()){
90 $users_array[] = $people_array['pagename'];
94 foreach($users_array as $person_indv){
95 if($declareBudget == "true"){
96 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");
97 if(count($get_array) == 1){
98 $cans_text = $get_array[0];
99 if(is_numeric($cans_text) && $cans_text >= 0){
100 $canBudget[$person_indv] = $cans_text; //Load the persons budget
102 $canBudget[$person_indv] = 0;
104 $people[] = $person_indv;
107 $canBudget[$person_indv] = $lockedBudget;
108 $people[] = $person_indv;
111 if(count($people) < 1){
112 return fmt("Nobody has used %s on their homepage", $PAGE_TEXT_LABEL);
114 //Get all pages from Category
116 $category_page = $dbi->getPage($CATEGORY);
117 $iter = $category_page->getLinks();
118 while ($item = $iter->next()){
119 array_push($pageids, $item->getName());
121 $ratingTotals = array();
122 foreach ($people as $person){
123 $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);
124 $ratingTotals[$person] = 0;
125 while($ratings_array = $ratings_iter->next()){
126 $can_rating = $ratings_array['ratingvalue'];
127 if($can_rating >= $NEUTRAL_RATING){
128 $ratingTotals[$person] += $can_rating;
134 $canTotals = array();
135 $peopleTotals = array();
136 foreach($pageids as $soda){
137 $canTotals[$soda] = 0;
139 foreach($people as $person){
140 foreach($pageids as $soda){
141 $peopleTotals[$person][$soda] = 0;
144 foreach($people as $person){
145 foreach($pageids as $page){
146 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);
147 $can_rating_array = $can_rating_iter->next();
148 $can_rating = $can_rating_array['ratingvalue'];
149 if($can_rating >= $NEUTRAL_RATING){
150 $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);
151 if($roundCalc == "true"){
152 $adjustedCans = round($calc);
154 $adjustedCans = round($calc, 2);
156 $peopleTotals[$person][$page] = $adjustedCans;
158 $canTotals[$page] = $canTotals[$page] + $adjustedCans;
162 $outputArray = array();
163 foreach($people as $person){
164 foreach($pageids as $page){
165 $outputArray[$person][$page] = 0;
169 $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
171 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
172 $td->pushContent(" ");
173 $tr->pushContent($td);
175 foreach($people as $person){
176 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
177 $td->pushContent(HTML::a(array('href' => WikiURL($person),
180 SplitPagename($person)));
181 //$td->pushContent(WikiLink(" $person "));
182 $tr->pushContent($td);
184 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
185 $td->pushContent(_("Total Units"));
186 $tr->pushContent($td);
187 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
188 $td->pushContent(_("Total Voters"));
189 $tr->pushContent($td);
190 $table->pushContent($tr);
192 for($i = 0; $i < count($pageids); $i++){
194 for($j = 0; $j < count($people); $j++){
195 $td = HTML::td(array('align' => 'right'));
196 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];
197 $total_cans = $total_cans + $cans_per_soda;
198 $outputArray[$people[$j]][$pageids[$i]] = $cans_per_soda;
203 foreach($people as $person){
205 $min_cans = 9999999; //9 million, serving as "infinity"
207 foreach($pageids as $page){
208 $cur_soda_cans = $outputArray[$person][$page];
209 if($cur_soda_cans < $min_cans && $cur_soda_cans > 0){
210 $min_cans = $cur_soda_cans;
213 $total_cans = $total_cans + $cur_soda_cans;
215 if($total_cans != $canBudget[$person] && $total_cans > 0){
216 $diff = $canBudget[$person] - $total_cans;
217 $outputArray[$person][$min_soda] = $outputArray[$person][$min_soda] + $diff;
220 for($i = 0; $i < count($pageids); $i++){
222 $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
223 $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
226 SplitPagename($pageids[$i])));
227 $tr->pushContent($td);
230 for($j = 0; $j < count($people); $j++){
231 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));
232 $output = $outputArray[$people[$j]][$pageids[$i]];
233 $total_cans = $total_cans + $output;
239 $td->pushContent($output);
240 $tr->pushContent($td);
242 if($total_cans == ""){
245 if($total_voters == ""){
248 $td = HTML::td(array('align' => 'right'));
249 $td->pushContent($total_cans);
250 $tr->pushContent($td);
251 $td = HTML::td(array('align' => 'right'));
252 $td->pushContent($total_voters);
253 $tr->pushContent($td);
254 $table->pushContent($tr);
258 $td = HTML::td(array('align' => 'left'));
259 $td->pushContent(HTML::strong(_("Total Budget")));
260 $tr->pushContent($td);
263 for($i = 0; $i < count($people); $i++){
264 $td = HTML::td(array('align' => 'right'));
266 foreach($pageids as $page){
267 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
269 $cans = $cans_for_soda;
270 $cans_total = $cans_total + $cans;
276 $td->pushContent(HTML::strong($cans));
277 $tr->pushContent($td);
279 $td = HTML::td(array('align' => 'right'));
280 $td->pushContent(HTML::strong($cans_total));
281 $tr->pushContent($td);
282 $td = HTML::td(array('align' => 'right'));
283 $td->pushContent(HTML::strong($total_voters));
284 $tr->pushContent($td);
285 $table->pushContent($tr);
287 $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
288 $table2->pushContent(HTML::tr(HTML::td($table)));
289 $html->pushContent($table2);
300 // c-hanging-comment-ender-p: nil
301 // indent-tabs-mode: nil