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