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 * <<PreferenceApp category="Soda" pageTextLabel="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 return HTML::div(array('class' => "error"), "PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");
70 $dbi = $request->getDbh();
71 $rdbi = RatingsDb::getTheRatingsDb();
73 $CATEGORY = $category;
74 $PAGE_TEXT_LABEL = $pageTextLabel;
75 $NEUTRAL_RATING = (int)$neutralRating;
77 $active_user = $request->getUser();
78 $active_userid = $active_user->_userid;
80 $html->pushContent("");
82 //Load participating Users
83 $users_array = array();
85 $users_array = getMembers($group, $rdbi);
87 $people_iter = $rdbi->sql_get_users_rated();
88 while($people_array = $people_iter->next()){
89 $users_array[] = $people_array['pagename'];
93 foreach($users_array as $person_indv){
94 if($declareBudget == "true"){
95 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");
96 if(count($get_array) == 1){
97 $cans_text = $get_array[0];
98 if(is_numeric($cans_text) && $cans_text >= 0){
99 $canBudget[$person_indv] = $cans_text; //Load the persons budget
101 $canBudget[$person_indv] = 0;
103 $people[] = $person_indv;
106 $canBudget[$person_indv] = $lockedBudget;
107 $people[] = $person_indv;
110 if(count($people) < 1){
111 return fmt("Nobody has used %s on their homepage", $PAGE_TEXT_LABEL);
113 //Get all pages from Category
115 $category_page = $dbi->getPage($CATEGORY);
116 $iter = $category_page->getLinks();
117 while ($item = $iter->next()){
118 array_push($pageids, $item->getName());
120 $ratingTotals = array();
121 foreach ($people as $person){
122 $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);
123 $ratingTotals[$person] = 0;
124 while($ratings_array = $ratings_iter->next()){
125 $can_rating = $ratings_array['ratingvalue'];
126 if($can_rating >= $NEUTRAL_RATING){
127 $ratingTotals[$person] += $can_rating;
133 $canTotals = array();
134 $peopleTotals = array();
135 foreach($pageids as $soda){
136 $canTotals[$soda] = 0;
138 foreach($people as $person){
139 foreach($pageids as $soda){
140 $peopleTotals[$person][$soda] = 0;
143 foreach($people as $person){
144 foreach($pageids as $page){
145 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);
146 $can_rating_array = $can_rating_iter->next();
147 $can_rating = $can_rating_array['ratingvalue'];
148 if($can_rating >= $NEUTRAL_RATING){
149 $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);
150 if($roundCalc == "true"){
151 $adjustedCans = round($calc);
153 $adjustedCans = round($calc, 2);
155 $peopleTotals[$person][$page] = $adjustedCans;
157 $canTotals[$page] = $canTotals[$page] + $adjustedCans;
161 $outputArray = array();
162 foreach($people as $person){
163 foreach($pageids as $page){
164 $outputArray[$person][$page] = 0;
168 $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));
170 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
171 $td->pushContent(" ");
172 $tr->pushContent($td);
174 foreach($people as $person){
175 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
176 $td->pushContent(HTML::a(array('href' => WikiURL($person),
179 SplitPagename($person)));
180 //$td->pushContent(WikiLink(" $person "));
181 $tr->pushContent($td);
183 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
184 $td->pushContent(_("Total Units"));
185 $tr->pushContent($td);
186 $td = HTML::td(array('bgcolor' => '#FFFFFF'));
187 $td->pushContent(_("Total Voters"));
188 $tr->pushContent($td);
189 $table->pushContent($tr);
191 for($i = 0; $i < count($pageids); $i++){
193 for($j = 0; $j < count($people); $j++){
194 $td = HTML::td(array('align' => 'right'));
195 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];
196 $total_cans = $total_cans + $cans_per_soda;
197 $outputArray[$people[$j]][$pageids[$i]] = $cans_per_soda;
202 foreach($people as $person){
204 $min_cans = 9999999; //9 million, serving as "infinity"
206 foreach($pageids as $page){
207 $cur_soda_cans = $outputArray[$person][$page];
208 if($cur_soda_cans < $min_cans && $cur_soda_cans > 0){
209 $min_cans = $cur_soda_cans;
212 $total_cans = $total_cans + $cur_soda_cans;
214 if($total_cans != $canBudget[$person] && $total_cans > 0){
215 $diff = $canBudget[$person] - $total_cans;
216 $outputArray[$person][$min_soda] = $outputArray[$person][$min_soda] + $diff;
219 for($i = 0; $i < count($pageids); $i++){
221 $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));
222 $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),
225 SplitPagename($pageids[$i])));
226 $tr->pushContent($td);
229 for($j = 0; $j < count($people); $j++){
230 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));
231 $output = $outputArray[$people[$j]][$pageids[$i]];
232 $total_cans = $total_cans + $output;
238 $td->pushContent($output);
239 $tr->pushContent($td);
241 if($total_cans == ""){
244 if($total_voters == ""){
247 $td = HTML::td(array('align' => 'right'));
248 $td->pushContent($total_cans);
249 $tr->pushContent($td);
250 $td = HTML::td(array('align' => 'right'));
251 $td->pushContent($total_voters);
252 $tr->pushContent($td);
253 $table->pushContent($tr);
257 $td = HTML::td(array('align' => 'left'));
258 $td->pushContent(HTML::strong(_("Total Budget")));
259 $tr->pushContent($td);
262 for($i = 0; $i < count($people); $i++){
263 $td = HTML::td(array('align' => 'right'));
265 foreach($pageids as $page){
266 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];
268 $cans = $cans_for_soda;
269 $cans_total = $cans_total + $cans;
275 $td->pushContent(HTML::strong($cans));
276 $tr->pushContent($td);
278 $td = HTML::td(array('align' => 'right'));
279 $td->pushContent(HTML::strong($cans_total));
280 $tr->pushContent($td);
281 $td = HTML::td(array('align' => 'right'));
282 $td->pushContent(HTML::strong($total_voters));
283 $tr->pushContent($td);
284 $table->pushContent($tr);
286 $table2 = HTML::table(array('bgcolor' => '#dedfdf'));
287 $table2->pushContent(HTML::tr(HTML::td($table)));
288 $html->pushContent($table2);
299 // c-hanging-comment-ender-p: nil
300 // indent-tabs-mode: nil