PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/users/admin/activity.php

http://showslow.googlecode.com/
PHP | 286 lines | 247 code | 38 blank | 1 comment | 51 complexity | fa76c8ee9d51d849b182718121411e3c MD5 | raw file
  1. <?php
  2. require_once(dirname(__FILE__).'/admin.php');
  3. $ADMIN_SECTION = 'activity';
  4. require_once(dirname(__FILE__).'/header.php');
  5. $daily_active_users = User::getDailyActiveUsers();
  6. $selectedactivityid = null;
  7. $selectedactivity = null;
  8. $activityuser = null;
  9. $dates = array();
  10. $showactivities = null;
  11. if (array_key_exists('activityid', $_REQUEST) && $_REQUEST['activityid'] == 'all') {
  12. $showactivities = 'all';
  13. }
  14. if (!array_key_exists('activityid', $_REQUEST) || $_REQUEST['activityid'] == 'withpoints') {
  15. $showactivities = 'withpoints';
  16. }
  17. if (array_key_exists('activityid', $_REQUEST) && is_numeric($_REQUEST['activityid'])) {
  18. $selectedactivityid = $_REQUEST['activityid'];
  19. $selectedactivity = UserConfig::$activities[$selectedactivityid];
  20. $dates = User::getDailyPointsByActivity($selectedactivityid);
  21. } else {
  22. if (array_key_exists('userid', $_REQUEST)) {
  23. $activityuser = User::getUser($_REQUEST['userid']);
  24. }
  25. $daily_activity = User::getDailyActivityPoints($activityuser);
  26. foreach ($daily_active_users as $date => $active_users) {
  27. $dates[$date]['users'] = $active_users;
  28. }
  29. foreach ($daily_activity as $record) {
  30. if (!array_key_exists($record['date'], $dates)) {
  31. $dates[$record['date']] = array();
  32. }
  33. if (!array_key_exists('users', $dates[$record['date']])) {
  34. $dates[$record['date']]['users'] = 0;
  35. }
  36. if (!array_key_exists('points', $dates[$record['date']])) {
  37. $dates[$record['date']]['points'] = 0;
  38. }
  39. $dates[$record['date']]['points'] += $record['total'] * UserConfig::$activities[$record['activity']][1];
  40. }
  41. }
  42. $total = 0;
  43. ?>
  44. <script type='text/javascript' src='swfobject/swfobject/swfobject.js'></script>
  45. <script type='text/javascript' src='http://www.google.com/jsapi'></script>
  46. <script type='text/javascript'>
  47. google.load('visualization', '1', {'packages':['annotatedtimeline', 'corechart']});
  48. google.setOnLoadCallback(function() {
  49. var data = new google.visualization.DataTable();
  50. data.addColumn('date', 'Date');
  51. <?php
  52. if (is_null($selectedactivity)) {
  53. ?>
  54. data.addColumn('number', 'Total Points');
  55. <?php
  56. if (is_null($activityuser)) {
  57. ?>
  58. data.addColumn('number', 'Active Users');
  59. <?php
  60. }
  61. } else {
  62. ?>
  63. data.addColumn('number', 'Number of activities');
  64. <?php
  65. }
  66. ?>
  67. var daily = [
  68. <?php
  69. $first = true;
  70. foreach ($dates as $date => $record)
  71. {
  72. if (!$first) {
  73. ?>,
  74. <?php
  75. }
  76. else
  77. {
  78. $first = false;
  79. }?>
  80. [new Date('<?php echo $date?>'),<?php
  81. if (is_null($selectedactivity)) {
  82. if (array_key_exists('points', $record) && $record['points'] > 0) {
  83. echo $record['points'];
  84. } else {
  85. echo 0;
  86. }
  87. echo ',';
  88. if (is_null($activityuser)) {
  89. echo $record['users'];
  90. }
  91. } else {
  92. echo $record ? $record : 0;
  93. }
  94. ?>]
  95. <?php
  96. }
  97. ?>
  98. ];
  99. data.addRows(daily);
  100. if (swfobject.hasFlashPlayerVersion("5")) {
  101. var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
  102. chart.draw(data, {
  103. displayAnnotations: true,
  104. scaleColumns: [0, 1],
  105. scaleType: 'allmaximized'
  106. });
  107. } else {
  108. var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
  109. chart.draw(data, {
  110. legend: 'top'
  111. });
  112. }
  113. });
  114. </script>
  115. <div id='chart_div' style='width: 100%; height: 240px; margin-bottom: 1em'></div>
  116. <form action="" name="activities">
  117. <div>
  118. Filter activities:
  119. <select name="activityid" onchange="document.activities.submit();">
  120. <option value="withpoints"<?php echo $showactivities == 'withpoints' ? ' selected="yes"' : '' ?>>-- all activities with points (default) --</option>
  121. <option value="all"<?php echo $showactivities == 'all' ? ' selected="yes"' : '' ?>>-- all activities --</option>
  122. <?php
  123. function mostpoints($a, $b) {
  124. if (UserConfig::$activities[$a][1] > UserConfig::$activities[$b][1]) {
  125. return -1;
  126. } else if (UserConfig::$activities[$a][1] < UserConfig::$activities[$b][1]) {
  127. return 1;
  128. }
  129. return strcmp(UserConfig::$activities[$a][0], UserConfig::$activities[$b][0]);
  130. }
  131. uksort(UserConfig::$activities, 'mostpoints');
  132. $stats = User::getActivityStatistics();
  133. foreach (UserConfig::$activities as $id => $activity) {
  134. if (!array_key_exists($id, $stats)) {
  135. continue;
  136. }
  137. ?>
  138. <option value="<?php echo $id ?>"<?php echo $selectedactivityid == $id ? ' selected="yes"' : '' ?>><?php echo $activity[0] ?> (<?php echo $activity[1] ?> points)</option>
  139. <?php } ?>
  140. </select>
  141. Users:
  142. <?php if (is_null($activityuser)) {
  143. ?>all<?php
  144. } else {
  145. ?><a href="user.php?id=<?php echo $activityuser->getID()?>"><?php echo UserTools::escape($activityuser->getName())?></a> (<a href=".">reset</a>)<?php
  146. }
  147. ?>
  148. </form>
  149. </div>
  150. <table cellpadding="5" cellspacing="0" border="1" width="100%">
  151. <tr><th>Time</th>
  152. <?php
  153. if (is_null($selectedactivity)) {
  154. ?>
  155. <th>Activity</th><th>Points</th>
  156. <?php
  157. }
  158. if (is_null($activityuser)) {
  159. ?>
  160. <th>User</th>
  161. <?php
  162. }?>
  163. </tr>
  164. <?php
  165. $perpage = 20;
  166. $pagenumber = 0;
  167. if (array_key_exists('page', $_GET)) {
  168. $pagenumber = $_GET['page'];
  169. }
  170. // TODO get activities only for specific activity
  171. if (!is_null($selectedactivity)) {
  172. $activities = User::getUsersByActivity($selectedactivityid, $pagenumber, $perpage);
  173. } else if (is_null($activityuser)) {
  174. $activities = User::getUsersActivity($showactivities == 'all', $pagenumber, $perpage);
  175. }
  176. else
  177. {
  178. $activities = $activityuser->getActivity($showactivities == 'all', $pagenumber, $perpage);
  179. }
  180. ?>
  181. <tr><td colspan="4">
  182. <?php
  183. if (count($activities) == $perpage) {
  184. ?><a style="float: right" href="?<?php echo array_key_exists('userid', $_REQUEST) ? 'userid='.urlencode($_REQUEST['userid']).'&' : ''; echo array_key_exists('activityid', $_REQUEST) ? 'activityid='.urlencode($_REQUEST['activityid']).'&' : '' ?>page=<?php echo $pagenumber+1?>">next &gt;&gt;&gt;</a><?php
  185. }
  186. else
  187. {
  188. ?><span style="color: silver; float: right">next &gt;&gt;&gt;</span><?php
  189. }
  190. if ($pagenumber > 0) {
  191. ?><a style="float: left" href="?<?php echo array_key_exists('userid', $_REQUEST) ? 'userid='.urlencode($_REQUEST['userid']).'&' : ''; echo array_key_exists('activityid', $_REQUEST) ? 'activityid='.urlencode($_REQUEST['activityid']).'&' : '' ?>page=<?php echo $pagenumber-1?>">&lt;&lt;&lt;prev</a><?php
  192. }
  193. else
  194. {
  195. ?><span style="color: silver; float: left">&lt;&lt;&lt;prev</span><?php
  196. }
  197. ?>
  198. <span style="float: left; margin: 0 2em">Page <?php echo $pagenumber+1?></span>
  199. </td></tr>
  200. <?php
  201. $now = time();
  202. foreach ($activities as $activity)
  203. {
  204. $time = $activity['time'];
  205. $ago = intval(floor(($now - $time)/86400));
  206. $tz = date_default_timezone_get();
  207. $user = User::getUser($activity['user_id']);
  208. ?><tr valign="top">
  209. <td align="right"><?php echo date('M j, h:iA', $time)?> (<?php if ($ago <= 5) {?><span style="color: #00<?php echo sprintf('%02s', dechex((4 - $ago) * 150 / 4 + 50))?>00; font-weight: bold"><?php }?><?php echo $ago?> day<?php echo $ago > 1 ? 's' : '' ?> ago<?php if ($ago <= 5) {?></span><?php }?>)</td>
  210. <?php
  211. if (is_null($selectedactivity)) {
  212. ?>
  213. <td><a href="?activityid=<?php echo $activity['activity_id']?>"><?php $act = UserConfig::$activities[$activity['activity_id']]; echo $act[0] ?></a></td>
  214. <td><?php echo $act[1] ?></td>
  215. <?php
  216. }
  217. if (is_null($activityuser)) {
  218. ?>
  219. <td>
  220. <a href="user.php?id=<?php echo $user->getID()?>"><?php echo UserTools::escape($user->getName());?></a> (<a href="activity.php?userid=<?php echo $user->getID()?>">user activity</a>)
  221. </td>
  222. <?php
  223. }?>
  224. </tr><?php
  225. }
  226. ?>
  227. <tr><td colspan="6">
  228. <?php
  229. if (count($activities) == $perpage) {
  230. ?><a style="float: right" href="?<?php echo array_key_exists('userid', $_REQUEST) ? 'userid='.urlencode($_REQUEST['userid']).'&' : ''; echo array_key_exists('activityid', $_REQUEST) ? 'activityid='.urlencode($_REQUEST['activityid']).'&' : '' ?>page=<?php echo $pagenumber+1?>">next &gt;&gt;&gt;</a><?php
  231. }
  232. else
  233. {
  234. ?><span style="color: silver; float: right">next &gt;&gt;&gt;</span><?php
  235. }
  236. if ($pagenumber > 0) {
  237. ?><a style="float: left" href="?<?php echo array_key_exists('userid', $_REQUEST) ? 'userid='.urlencode($_REQUEST['userid']).'&' : ''; echo array_key_exists('activityid', $_REQUEST) ? 'activityid='.urlencode($_REQUEST['activityid']).'&' : '' ?>page=<?php echo $pagenumber-1?>">&lt;&lt;&lt;prev</a><?php
  238. }
  239. else
  240. {
  241. ?><span style="color: silver; float: left">&lt;&lt;&lt;prev</span><?php
  242. }
  243. ?>
  244. <span style="float: left; margin-left: 2em">Page <?php echo $pagenumber+1?></span>
  245. </td></tr>
  246. </table>
  247. <?php
  248. require_once(dirname(__FILE__).'/footer.php');