-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathachievements.pages.inc
More file actions
396 lines (356 loc) · 17 KB
/
achievements.pages.inc
File metadata and controls
396 lines (356 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/**
* @file
* Page callbacks for the Achievements module.
*/
/**
* Menu callback; show the site-wide leaderboard.
*
* @see achievement_leaderboard_block()
*/
function achievements_leaderboard_totals() {
// force no sorting on # so it doesn't get the 'active' CSS, which screws up relative non-header display.
$header = array(array('data' => t('#'), 'sort' => NULL), t('Who'), t('Points'), t('Unlocks'), t('Latest achievement'));
$achievers = $rows = array('top' => array(), 'relative' => array());
// load up all our achievers for the page.
$query = db_select('achievement_totals', 'at')->extend('PagerDefault');
$query->addTag('achievement_totals')->join('users', 'u', 'u.uid = at.uid');
$query->fields('at', array('uid', 'points', 'unlocks', 'timestamp', 'achievement_id'))->fields('u', array('name'));
$query->orderBy('at.points', 'DESC')->orderBy('at.timestamp'); // @bug DESC/ASC doesn't index. ever. bastids.
$query->limit(variable_get('achievements_leaderboard_count_per_page', 10));
$achievers['top'] = $query->execute()->fetchAllAssoc('uid');
if (user_is_logged_in() && variable_get('achievements_leaderboard_relative', 'nearby_ranks') != 'disabled') {
$achievers['relative'] = (variable_get('achievements_leaderboard_relative', 'nearby_ranks') == 'nearby_ranks')
? achievements_totals_user(variable_get('achievements_leaderboard_relative_nearby_ranks', 2))
: achievements_totals_user(); // you and me, togetheRrrr FoReEEvvvVeeEeeRRrRR.
}
// display our achievers in "how awesome they are" order.
// Global rank isn't stored in the database (to prevent us from having
// complicated rejiggering every time an achievement is unlocked), but
// the order the query returns is the order of the leaderboard. thus,
// we start at 1 and increase the rank for every row we're showing.
// also, be careful to start with the right number on pager queries.
$rank = 0 + ($GLOBALS['pager_page_array'][0] * variable_get('achievements_leaderboard_count_per_page', 10));
foreach (array('top', 'relative') as $type) {
foreach ($achievers[$type] as $achiever) {
// determine the current rank and set some speshul classes.
$rank = $achiever->rank = isset($achiever->rank) ? $achiever->rank : $rank + 1;
$classes = ($achiever->uid == $GLOBALS['user']->uid) ? array('achievement-leaderboard-current-user') : array();
if ($rank <= 3) { // give special classes if its the current user or if it's a cherished leader position.
$classes[] = 'achievement-leaderboard-top-rank';
$classes[] = 'achievement-leaderboard-top-rank-' . $rank;
}
$rows[$type][$achiever->uid] = array(
'class' => $classes,
'data' => array(
array(
'data' => $rank,
'class' => array('achievement-leaderboard-rank')
),
array(
'data' => theme('username', array('account' => $achiever)),
'class' => array('achievement-leaderboard-username')
),
array(
'data' => l($achiever->points, 'user/' . $achiever->uid . '/achievements'),
'class' => array('achievement-leaderboard-points')
),
array(
'data' => l($achiever->unlocks, 'user/' . $achiever->uid . '/achievements'),
'class' => array('achievement-leaderboard-unlocks')
),
),
);
// since we're showing the "latest achievement" for a user, we have to
// determine what image to use and respect 'secret' status. we already
// do this in achievements_template_shared_variables(), so reuse it. we
// fake an #unlock so the 'unlocked' image is returned; the POV for this
// achievement display is the ranked user/row, NOT the current user.
$achievers[$type][$achiever->uid]->latest = array(
'#theme' => 'achievement_latest_unlock',
'#achievement' => achievements_load($achiever->achievement_id),
'#unlock' => array('timestamp' => $achiever->timestamp),
);
$rows[$type][$achiever->uid]['data'][] = array(
'data' => render($achievers[$type][$achiever->uid]->latest),
'class' => array('achievement-leaderboard-latest')
);
}
}
foreach (array('top', 'relative') as $type) {
$leaderboard = array(
'achievers' => $achievers,
'block' => FALSE,
'type' => $type,
'render' => array(
'#attributes' => array('class' => array('achievement-leaderboard', 'achievement-leaderboard-' . $type)),
'#header' => $type == 'top' ? $header : NULL, // relative piggybacks off of top.
'#empty' => $type == 'top' ? t('No one has been ranked yet.') : NULL,
'#rows' => $rows[$type],
'#theme' => 'table',
),
);
if ($type == 'top' || count($rows['relative'])) {
drupal_alter('achievements_leaderboard', $leaderboard);
$build['achievements']['leaderboard'][$type] = $leaderboard['render'];
}
}
$build['achievements']['leaderboard_pager'] = array(
'#theme' => 'pager',
);
return $build;
}
/**
* Block callback; show the global leaderboard.
*
* @see achievement_leaderboard_totals()
*/
function achievements_leaderboard_block() {
$header = array(array('data' => t('#'), 'sort' => NULL), t('Who'), t('Pts'));
$achievers = $rows = array('top' => array(), 'relative' => array());
$query = db_select('achievement_totals', 'at');
$query->addTag('achievement_totals')->join('users', 'u', 'u.uid = at.uid');
$query->fields('at', array('uid', 'points', 'unlocks'))->fields('u', array('name'));
$query->orderBy('at.points', 'DESC')->orderBy('at.timestamp'); // @bug DESC/ASC doesn't index.
$query->range(0, variable_get('achievements_leaderboard_block_count_top', 5));
$achievers['top'] = $query->execute()->fetchAllAssoc('uid');
if (user_is_logged_in() && !isset($achievers['top'][$GLOBALS['user']->uid]) && variable_get('achievements_leaderboard_block_relative', 'nearby_ranks') != 'disabled') {
$achievers['relative'] = (variable_get('achievements_leaderboard_block_relative', 'nearby_ranks') == 'nearby_ranks')
? achievements_totals_user(variable_get('achievements_leaderboard_block_relative_nearby_ranks', 1))
: achievements_totals_user(); // you and me, togetheRrrr almost nEveVvaeeeeEEERrrrrR111!
}
$rank = 0; // innocent widdle variable!
foreach (array('top', 'relative') as $type) {
foreach ($achievers[$type] as $achiever) {
$rank = $achiever->rank = isset($achiever->rank) ? $achiever->rank : $rank + 1;
$classes = ($achiever->uid == $GLOBALS['user']->uid) ? array('achievement-leaderboard-current-user') : array();
if ($rank <= 3) { // give special classes if its the current user or if it's a cherished leader position.
$classes[] = 'achievement-leaderboard-top-rank';
$classes[] = 'achievement-leaderboard-top-rank-' . $rank;
}
$rows[$type][$achiever->uid] = array(
'class' => $classes,
'data' => array(
array(
'data' => $rank,
'class' => array('achievement-leaderboard-rank')
),
array(
'data' => theme('username', array('account' => $achiever)),
'class' => array('achievement-leaderboard-username')
),
array(
'data' => l($achiever->points, 'user/' . $achiever->uid . '/achievements'),
'class' => array('achievement-leaderboard-points')
),
),
);
}
}
foreach (array('top', 'relative') as $type) {
$leaderboard = array(
'achievers' => $achievers,
'block' => TRUE,
'type' => $type,
'render' => array(
'#attributes' => array('class' => array('achievement-leaderboard', 'achievement-leaderboard-' . $type)),
'#header' => $type == 'top' ? $header : NULL, // relative piggybacks off of top.
'#empty' => $type == 'top' ? t('No one has been ranked yet.') : NULL,
'#rows' => $rows[$type],
'#theme' => 'table',
),
);
if ($type == 'top' || count($rows['relative'])) {
drupal_alter('achievements_leaderboard', $leaderboard);
$build['achievements']['leaderboard'][$type] = $leaderboard['render'];
}
}
$build['achievements']['see-more'] = array(
'#prefix' => '<div class="achievement-see-more">',
'#markup' => l(t('View full leaderboard »'), 'achievements/leaderboard'),
'#suffix' => '</div>',
);
return $build;
}
/**
* Menu callback; display a single achievement page with leaderboards.
*/
function achievements_leaderboard_for($achievement) {
drupal_set_title(t('Achievement: @title', array('@title' => $achievement['title'])), PASS_THROUGH);
$build['achievements']['achievement'] = array(
'#achievement' => $achievement,
'#unlock' => achievements_unlocked_already($achievement['id']),
'#theme' => 'achievement__leaderboard_for',
);
// get stats for first and most recent unlocks.
$query = db_select('achievement_unlocks', 'au');
$query->join('achievement_totals', 'at', 'at.uid = au.uid');
$query->join('users', 'u', 'u.uid = au.uid'); // same basic start for both queries.
$query->condition('au.achievement_id', $achievement['id']); // ... with a slight tweak.
$query->fields('au', array('uid', 'rank', 'timestamp'))->fields('at', array('points', 'unlocks'))->fields('u', array('name'));
$query2 = clone $query; // allows us to save a few lines of duplicate query building. never used clone before. awesome.
$achievers['first'] = $query->orderBy('rank')->range(0, 10)->execute()->fetchAllAssoc('uid'); // FI... sigh.
$achievers['recent'] = $query2->orderBy('timestamp', 'DESC')->range(0, 10)->execute()->fetchAllAssoc('uid');
foreach (array('first', 'recent') as $type) {
$rows = array(); // clear previous run.
foreach ($achievers[$type] as $achiever) {
$rows[$achiever->uid] = array(
'class' => array(),
'data' => array(
array(
'data' => $achiever->rank,
'class' => array('achievement-leaderboard-rank')
),
array(
'data' => theme('username', array('account' => $achiever)),
'class' => array('achievement-leaderboard-username')
),
array(
'data' => l($achiever->points, 'user/' . $achiever->uid . '/achievements'),
'class' => array('achievement-leaderboard-points')
),
array(
'data' => format_date($achiever->timestamp, 'short'),
'class' => array('achievement-leaderboard-when')
),
),
);
}
$leaderboard = array(
'achievers' => $achievers,
'block' => FALSE,
'type' => $type,
'render' => array(
'#attributes' => array('class' => array('achievement-leaderboard-' . $type)),
'#caption' => t('@type achievement unlocks', array('@type' => drupal_ucfirst($type))),
'#header' => array(t('#'), t('Who'), t('Points'), t('When')),
'#empty' => t('No one has unlocked this yet. Keep trying!'),
'#rows' => $rows,
'#theme' => 'table',
),
);
drupal_alter('achievements_leaderboard', $leaderboard);
$build['achievements']['leaderboard'][$type] = $leaderboard['render'];
}
return $build;
}
/**
* Menu callback; display all achievements for the passed user.
*
* @param $account
* The user object this request applies against.
*/
function achievements_user_page($account) {
drupal_set_title(t('Achievements for @name', array('@name' => $account->name)));
$unlocks = achievements_unlocked_already(NULL, $account->uid);
$achiever = array_pop(achievements_totals_user(0, $account->uid));
$build['achievements']['stats'] = array(
'#theme' => 'achievement_user_stats',
'#stats' => array(
'name' => $account->name,
'rank' => isset($achiever->rank) ? $achiever->rank : 0,
'points' => isset($achiever->points) ? $achiever->points : 0,
'unlocks_count' => count($unlocks),
'total_count' => count(achievements_load()),
),
);
$achievements_grouped = achievements_load(NULL, TRUE);
// theme wrappers let us wrap our children with a rethemable/overridable DIV.
$build['achievements']['tabs']['#theme_wrappers'] = array('achievement_groups_wrapper');
// use jQuery tabs if more than one group.
if (count($achievements_grouped) > 1) {
foreach ($achievements_grouped as $group_id => $group) {
$links['achievement-group-' . $group_id] = array(
'title' => $group['title'],
'fragment' => 'achievement-group-' . $group_id,
'href' => '', // just the fragment, please.
'external' => TRUE, // no base_path dammit. GZUS.
);
}
$build['achievements']['tabs']['navigation'] = array('#theme' => 'links', '#links' => $links);
$build['achievements']['tabs']['navigation']['#attached']['library'][] = array('system', 'ui.tabs');
$build['achievements']['tabs']['navigation']['#attached']['js']= array( // I AM NOT A JS EXPERT. OOooOH NO.
'jQuery(document).ready(function(){jQuery("#achievement-groups").tabs();});' => array('type' => 'inline')
);
}
foreach ($achievements_grouped as $group_id => $group) {
$locked_weight = 0; // we can't use definition order.
$build['achievements']['tabs']['groups'][$group_id]['#group_id'] = $group_id; // add a DIV in the wrapper.
$build['achievements']['tabs']['groups'][$group_id]['#theme_wrappers'] = array('achievement_group_wrapper');
foreach ($group['achievements'] as $achievement_id => $achievement) {
if (!empty($achievement['invisible']) && !isset($unlocks[$achievement_id])) {
continue; // invisibles only display if this $account has unlocked them.
}
// if it's not an invisible achievement, we've got to show something. $build out what and where.
$build['achievements']['tabs']['groups'][$group_id][$achievement_id]['#achievement'] = $achievement;
$build['achievements']['tabs']['groups'][$group_id][$achievement_id]['#theme'] = 'achievement__user_page';
if (isset($unlocks[$achievement_id])) {
$build['achievements']['tabs']['groups'][$group_id][$achievement_id]['#unlock'] = $unlocks[$achievement_id];
if (variable_get('achievements_unlocked_move_to_top', TRUE)) {
$build['achievements']['tabs']['groups'][$group_id][$achievement_id]['#weight'] = -$unlocks[$achievement_id]['timestamp'];
// by setting the negative weight to the timestamp, the latest unlocks are always shown at the top.
}
}
elseif (!isset($unlocks[$achievement_id]) && variable_get('achievements_unlocked_move_to_top', TRUE)) {
// if we're forcing unlocks to the top, locked achievements have to be forced to the bottom.
$build['achievements']['tabs']['groups'][$group_id][$achievement_id]['#weight'] = $locked_weight++;
}
}
}
return $build;
}
/**
* Menu callback; Retrieve autocomplete suggestions for achievement names.
*/
function achievements_autocomplete($string = '') {
$achievements = achievements_load(); // MmmMMmMm, gooOoaaAaalLLlsss.
array_walk($achievements, 'achievements_autocomplete_search', $string);
drupal_json_output(array_slice(array_filter($achievements), 0, 10));
}
/**
* array_walk helper function for achievements_autocomplete().
*/
function achievements_autocomplete_search(&$value, $key, $string) {
$value = (stripos($value['title'], $string) === FALSE) ? FALSE : $value['title'];
}
/**
* Default theme implementation for a user's achievement stats.
*
* @param variables
* An associative array containing:
* - stats: An array which contains the following keys:
* name, rank, points, unlocks_count, and total_count.
*/
function theme_achievement_user_stats($variables) {
$output = '<div class="achievement-user-stats">'; // VERY CONFUSING CODE COMMENT ABOUT PSYNAPTIC. YOU THOUGHT I WAS KIDDING?
$output .= t('@name is ranked #@rank with @points points. @unlocks_count of @total_count achievements have been unlocked.', array(
'@name' => $variables['stats']['name'],
'@rank' => $variables['stats']['rank'],
'@points' => $variables['stats']['points'],
'@unlocks_count' => $variables['stats']['unlocks_count'],
'@total_count' => $variables['stats']['total_count'],
));
$output .= '</div>';
return $output;
}
/**
* Default theme for the wrapper around a user's achievements page.
*
* @param $variables
* An associative array containing:
* - element: A render containing the user's achievements page.
*/
function theme_achievement_groups_wrapper($variables) {
return '<div id="achievement-groups">' . $variables['element']['#children'] . '</div>';
}
/**
* Default theme for the wrapper around an achievement group.
*
* @param $variables
* An associative array containing:
* - element: A render containing the achievement group.
*/
function theme_achievement_group_wrapper($variables) {
$group_id = 'achievement-group-' . $variables['element']['#group_id'];
return '<div id="' . $group_id . '" class="achievement-group">' . $variables['element']['#children'] . '</div>';
}