]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PreferenceApp.php
WikiLens preferences-showing app
[SourceForge/phpwiki.git] / lib / plugin / PreferenceApp.php
1 <?php //-*-php-*-\r
2 rcs_id('$Id: PreferenceApp.php,v 1.1 2004-07-07 19:46:39 dfrankow Exp $');\r
3 \r
4 /**\r
5\r
6 */\r
7 require_once('lib/PageList.php');\r
8 require_once('lib/InlineParser.php');\r
9 \r
10 require_once('lib/wikilens/Utils.php');\r
11 require_once('lib/Theme.php');\r
12 require_once('lib/wikilens/Buddy.php');\r
13 require_once('lib/wikilens/RatingsDb.php');\r
14 \r
15 \r
16 /**\r
17  * PreferenceApp is used to analyze a category of items that a group\r
18  * of people have rated.  A user is grouped to be analyzed in the group by\r
19  * 1) having rated at least one item in the database and 2) matching the optional \r
20  * criteria for declaring a budget on their homepage.  \r
21  *\r
22  * An example of a budget decleration would be "TotalSoda: 50" on my homepage.\r
23  *\r
24  * PreferenceApp will output a matrix style table shows "how much" fractionally \r
25  * a group of people prefer an item over other items.  For example, if my soda \r
26  * budget is 100 then PreferenceApp might assign 20 units of my budget to Moutain Dew.\r
27  *\r
28  * Author: mcassano circa April 2004\r
29  *\r
30  * Usage:\r
31  * <?plugin PreferenceApp category="Soda" pageLabel="TotalSoda" roundCalc="true" ?>\r
32  */\r
33 \r
34 class WikiPlugin_PreferenceApp\r
35 extends WikiPlugin\r
36 {\r
37     function getName () {\r
38         return _("Preference App");\r
39     }\r
40 \r
41     function getDescription () {\r
42         return _("Analyzes Preferences based on voting budget and ratings.");\r
43     }\r
44 \r
45     function getVersion() {\r
46         return preg_replace("/[Revision: $]/", '',\r
47                             "\$Revision: 1.1 $");\r
48     }\r
49 \r
50     function getDefaultArguments() {\r
51         return array(\r
52                                 'category' => null,\r
53                                 'lockedBudget' => null,\r
54                                 'pageTextLabel' => null,\r
55                                 'group' => null,\r
56                                 'roundCalc' => "true",\r
57                                 'neutralRating' => "3",\r
58                                 'declareBudget' => "true");            \r
59     }\r
60     // info arg allows multiple columns\r
61     // info=mtime,hits,summary,version,author,locked,minor\r
62     // exclude arg allows multiple pagenames exclude=HomePage,RecentChanges\r
63         \r
64 \r
65     function run($dbi, $argstr, $request) {\r
66         \r
67         extract($this->getArgs($argstr, $request));\r
68         if($pageTextLabel == null && $category != null && $group == null){\r
69             $group = $category;                                 \r
70         }\r
71         /**\r
72          * defunct check\r
73         if($category == null || $pageTextLabel == null){\r
74                 $html = HTML("PreferencesApp Error: You must declare at least parameters category and pageTextLabel.");\r
75                 return $html;\r
76         }\r
77         */\r
78         \r
79         $dbi = $request->getDbh();\r
80         $rdbi = RatingsDb::getTheRatingsDb();\r
81         \r
82         $CATEGORY = $category;\r
83         $PAGE_TEXT_LABEL = $pageTextLabel;\r
84         $NEUTRAL_RATING = (int)$neutralRating;\r
85         \r
86         $active_user   = $request->getUser();\r
87         $active_userid = $active_user->_userid;\r
88                 $html = HTML();\r
89                 $html->pushContent("");\r
90                 \r
91                 //Load participating Users\r
92                 $users_array = array();\r
93                 if($group != null){\r
94                         $users_array = getMembers($group, $rdbi);\r
95 \r
96         } else {\r
97             $people_iter = $rdbi->sql_get_users_rated();\r
98                         while($people_array = $people_iter->next()){\r
99                                 $users_array[] = $people_array['pagename'];     \r
100                         }\r
101                 }\r
102                 $people = array();\r
103         foreach($users_array as $person_indv){\r
104                         if($declareBudget == "true"){\r
105                 $get_array = getPageTextData($person_indv, $dbi, $PAGE_TEXT_LABEL, "cans");\r
106                                 if(count($get_array) == 1){\r
107                                         $cans_text = $get_array[0];\r
108                                         if(is_numeric($cans_text) && $cans_text >= 0){\r
109                                                 $canBudget[$person_indv] = $cans_text; //Load the persons budget\r
110                                         } else {\r
111                                                 $canBudget[$person_indv] = 0;\r
112                                         }\r
113                                         $people[] = $person_indv;       \r
114                                 }       \r
115                         } else {\r
116                                 $canBudget[$person_indv] = $lockedBudget;\r
117                                 $people[] = $person_indv;       \r
118                         }\r
119                 }\r
120                 if(count($people) < 1){\r
121                         $html = HTML("Nobody has used $PAGE_TEXT_LABEL on their homepage");\r
122                         return $html;   \r
123                 }\r
124                 //Get all pages from Category\r
125                 $pageids = array();\r
126         $category_page = $dbi->getPage($CATEGORY);\r
127         $iter = $category_page->getLinks();\r
128         while($item = $iter->next()){\r
129                 array_push($pageids, $item->getName());\r
130         }\r
131         $ratingTotals = array();\r
132         foreach($people as $person){\r
133                 $ratings_iter = $rdbi->sql_get_rating(0, $person, $pageids);\r
134                 $ratingTotals[$person] = 0;\r
135                         while($ratings_array = $ratings_iter->next()){\r
136                                 $can_rating = $ratings_array['ratingvalue'];\r
137                                 if($can_rating >= $NEUTRAL_RATING){\r
138                                         $ratingTotals[$person] += $can_rating;\r
139                                 }\r
140                         }               \r
141         }\r
142                 \r
143                 //Generate numbers\r
144                 $canTotals = array();\r
145                 $peopleTotals = array();\r
146                 foreach($pageids as $soda){\r
147                         $canTotals[$soda] = 0;  \r
148                 }\r
149                 foreach($people as $person){\r
150                         foreach($pageids as $soda){\r
151                                 $peopleTotals[$person][$soda] = 0;\r
152                         }       \r
153                 }\r
154                 foreach($people as $person){\r
155                         foreach($pageids as $page){\r
156                                 $can_rating_iter = $rdbi->sql_get_rating(0, $person, $page);\r
157                                 $can_rating_array = $can_rating_iter->next();\r
158                                 $can_rating = $can_rating_array['ratingvalue']; \r
159                                 if($can_rating >= $NEUTRAL_RATING){\r
160                                         $calc = (($can_rating / $ratingTotals[$person]) * $canBudget[$person]);\r
161                                         if($roundCalc == "true"){\r
162                                                 $adjustedCans = round($calc);\r
163                                         } else {\r
164                                                 $adjustedCans = round($calc, 2);\r
165                                         }\r
166                                         $peopleTotals[$person][$page] = $adjustedCans;\r
167                                         \r
168                                         $canTotals[$page] = $canTotals[$page] + $adjustedCans;\r
169                                 }\r
170                         }\r
171                 }\r
172                 $outputArray = array();\r
173                 foreach($people as $person){\r
174                         foreach($pageids as $page){\r
175                                 $outputArray[$person][$page] = 0;\r
176                         }\r
177                 }\r
178                 \r
179                 $table = HTML::table(array('cellpadding' => '5', 'cellspacing' => '1', 'border' => '0'));\r
180                 $tr = HTML::tr();\r
181                 $td = HTML::td(array('bgcolor' => '#FFFFFF'));\r
182                 $td->pushContent(" ");\r
183                 $tr->pushContent($td);\r
184                 \r
185                 foreach($people as $person){\r
186                         $td = HTML::td(array('bgcolor' => '#FFFFFF'));\r
187                         $td->pushContent(HTML::a(array('href' => WikiURL($person),\r
188                                               'class' => 'wiki'\r
189                                               ),\r
190                                         SplitPagename($person)));\r
191                         //$td->pushContent(WikiLink(" $person "));\r
192                         $tr->pushContent($td);  \r
193                 }\r
194                 $td = HTML::td(array('bgcolor' => '#FFFFFF'));\r
195                 $td->pushContent("Total Units");\r
196                 $tr->pushContent($td);  \r
197                 $td = HTML::td(array('bgcolor' => '#FFFFFF'));\r
198                 $td->pushContent("Total Voters");\r
199                 $tr->pushContent($td);  \r
200                 $table->pushContent($tr);\r
201                 \r
202                 for($i = 0; $i < count($pageids); $i++){\r
203                         $total_cans = 0;\r
204                         for($j = 0; $j < count($people); $j++){\r
205                                 $td = HTML::td(array('align' => 'right'));\r
206                                 $cans_per_soda = $peopleTotals[$people[$j]][$pageids[$i]];\r
207                                 $total_cans = $total_cans + $cans_per_soda;\r
208                                 $outputArray[$people[$j]][$pageids[$i]] = $cans_per_soda;\r
209                         }                       \r
210                 }\r
211                         \r
212                 \r
213                 foreach($people as $person){\r
214                         $min_soda = "";\r
215                         $min_cans = 9999999; //9 million, serving as "infinity"\r
216                         $total_cans = 0;\r
217                         foreach($pageids as $page){\r
218                                 $cur_soda_cans = $outputArray[$person][$page];\r
219                                 if($cur_soda_cans < $min_cans && $cur_soda_cans > 0){\r
220                                         $min_cans = $cur_soda_cans;\r
221                                         $min_soda = $page;\r
222                                 }\r
223                                 $total_cans = $total_cans + $cur_soda_cans;\r
224                         }       \r
225                         if($total_cans != $canBudget[$person] && $total_cans > 0){\r
226                                 $diff = $canBudget[$person] - $total_cans;\r
227                                 $outputArray[$person][$min_soda] = $outputArray[$person][$min_soda] + $diff;\r
228                         }\r
229                 }\r
230                 for($i = 0; $i < count($pageids); $i++){\r
231                         $tr = HTML::tr();\r
232                         $td = HTML::td(array('align' => 'left', 'bgcolor' => '#f7f7f7'));\r
233                         $td->pushContent(HTML::a(array('href' => WikiURL($pageids[$i]),\r
234                                               'class' => 'wiki'\r
235                                               ),\r
236                                         SplitPagename($pageids[$i])));\r
237                         $tr->pushContent($td);\r
238                         $total_cans = 0;\r
239                         $total_voters = 0;\r
240                         for($j = 0; $j < count($people); $j++){\r
241                                 $td = HTML::td(array('align' => 'right', 'bgcolor' => '#f7f7f7'));\r
242                                 $output = $outputArray[$people[$j]][$pageids[$i]];\r
243                                 $total_cans = $total_cans + $output;\r
244                                 if($output == ""){\r
245                                         $output = "-";\r
246                                 } else {\r
247                                         $total_voters++;\r
248                                 }\r
249                                 $td->pushContent($output);\r
250                                 $tr->pushContent($td);\r
251                         }       \r
252                         if($total_cans == ""){\r
253                                 $total_cans = "-";      \r
254                         }\r
255                         if($total_voters == ""){\r
256                                 $total_voters = "-";    \r
257                         }\r
258                         $td = HTML::td(array('align' => 'right'));\r
259                         $td->pushContent($total_cans); \r
260                         $tr->pushContent($td);  \r
261                         $td = HTML::td(array('align' => 'right'));\r
262                         $td->pushContent($total_voters); \r
263                         $tr->pushContent($td);  \r
264                         $table->pushContent($tr);       \r
265                 }\r
266                 \r
267                 $tr = HTML::tr();\r
268                 $td = HTML::td(array('align' => 'left'));\r
269                 $td->pushContent(HTML::strong("Total Budget"));\r
270                 $tr->pushContent($td);\r
271                 $cans_total = 0;\r
272                 $total_voters = 0;\r
273                 for($i = 0; $i < count($people); $i++){\r
274                         $td = HTML::td(array('align' => 'right'));\r
275                         $cans_for_soda = 0;\r
276                         foreach($pageids as $page){\r
277                                 $cans_for_soda = $cans_for_soda + $outputArray[$people[$i]][$page];\r
278                         }\r
279                         $cans = $cans_for_soda;\r
280                         $cans_total = $cans_total + $cans;\r
281                         if($cans == ""){\r
282                                 $cans = "-";\r
283                         } else {\r
284                                 $total_voters++;\r
285                         }\r
286                         $td->pushContent(HTML::strong($cans));\r
287                         $tr->pushContent($td);\r
288                 }\r
289                 $td = HTML::td(array('align' => 'right'));\r
290                 $td->pushContent(HTML::strong($cans_total));\r
291                 $tr->pushContent($td);\r
292                 $td = HTML::td(array('align' => 'right'));\r
293                 $td->pushContent(HTML::strong($total_voters));\r
294                 $tr->pushContent($td);\r
295                 $table->pushContent($tr);\r
296                 \r
297                 $table2 = HTML::table(array('bgcolor' => '#dedfdf'));\r
298                 $table2->pushContent(HTML::tr(HTML::td($table)));\r
299                 $html->pushContent($table2);\r
300                 \r
301                 return $html;\r
302     }\r
303     \r
304 };\r
305 \r
306 // Local Variables:\r
307 // mode: php\r
308 // tab-width: 8\r
309 // c-basic-offset: 4\r
310 // c-hanging-comment-ender-p: nil\r
311 // indent-tabs-mode: nil\r
312 // End:\r
313 ?>