9a0c84866d1554ec3941b3b471b022f20e64e41d
[project/achievements.git] / achievements.admin.inc
1 <?php
2
3 /**
4 * @file
5 * Admin callbacks for the Achievements module.
6 */
7
8 /**
9 * Configure achievements.
10 */
11 function achievements_settings() {
12 $form['achievements_cache'] = array(
13 '#type' => 'fieldset',
14 '#title' => t('Clear cache'),
15 );
16 $form['achievements_cache']['achievements_clear_info_cache'] = array(
17 '#type' => 'submit', // just like performance cache clears.
18 '#submit' => array('achievements_clear_info_cache_submit'),
19 '#value' => t('Clear achievements information cache'),
20 );
21
22 $form['achievements_manual'] = array(
23 '#description' => t("Taking an achievement from a user will <em>not</em> change other users' unlock ranking. <strong>This functionality should be used sparingly for progress-based achievements as it can create odd inconsistencies in the internal statistics.</strong> For example, giving a user the \"125 comments posted\" achievement does <em>not</em> mean they've actually posted 125 comments - if they've only made 15 legitimate comments, they'll still need to post 235 more to achieve \"250 comments posted\". Similarly, taking away an achievement will delete any relevant internal statistics, causing the user to \"start over\" with that achievement even if applicable and countable data remains."),
24 '#title' => t('Manual overrides'),
25 '#type' => 'fieldset',
26 );
27 $form['achievements_manual']['achievements_manual_user'] = array(
28 '#autocomplete_path' => 'user/autocomplete',
29 '#maxlength' => '60',
30 '#title' => t('Username or UID'),
31 '#type' => 'textfield',
32 );
33 $form['achievements_manual']['achievements_manual_achievement'] = array(
34 '#autocomplete_path' => 'achievements/autocomplete',
35 '#title' => t('Achievement ID'),
36 '#type' => 'textfield',
37 );
38 $form['achievements_manual']['actions'] = array('#type' => 'actions');
39 $form['achievements_manual']['actions']['achievements_manual_give'] = array(
40 '#achievement_action' => 'give',
41 '#type' => 'submit',
42 '#submit' => array('achievements_manual_submit'),
43 '#value' => t('Give achievement'),
44 );
45 $form['achievements_manual']['actions']['achievements_manual_take'] = array(
46 '#achievement_action' => 'take',
47 '#type' => 'submit',
48 '#submit' => array('achievements_manual_submit'),
49 '#value' => t('Take achievement'),
50 );
51
52 $form['achievements_rankings'] = array(
53 '#description' => t('These settings affect the <a href="@url">global leaderboard</a>. Enabling the "relative leaderboard" will show the current user\'s position and, optionally, a number of ranks before and after that position. For example, if the current user is ranked 12th and you configure 3 nearby ranks, the relative leaderboard will show ranks 9 through 15.', array('@url' => url('achievements/leaderboard'))),
54 '#title' => t('Leaderboard ranks'),
55 '#type' => 'fieldset',
56 );
57 $form['achievements_rankings']['achievements_leaderboard_count_per_page'] = array(
58 '#type' => 'select',
59 '#title' => t('Number of top ranks per page'),
60 '#default_value' => variable_get('achievements_leaderboard_count_per_page', 10),
61 '#options' => drupal_map_assoc(array(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100)),
62 );
63 $form['achievements_rankings']['achievements_leaderboard_relative'] = array(
64 '#type' => 'radios',
65 '#title' => t('Relative leaderboard display'),
66 '#default_value' => variable_get('achievements_leaderboard_relative', 'nearby_ranks'),
67 '#options' => array(
68 'disabled' => t('Don\'t show the relative leaderboard'),
69 'user_only' => t('Show only the current user'),
70 'nearby_ranks' => t('Show the current user and nearby ranks'),
71 ),
72 );
73 $form['achievements_rankings']['achievements_leaderboard_relative_nearby_ranks'] = array(
74 '#type' => 'select',
75 '#title' => t('Number of nearby ranks to display'),
76 '#default_value' => variable_get('achievements_leaderboard_relative_nearby_ranks', 2),
77 '#options' => drupal_map_assoc(range(1, 10)),
78 );
79
80 $form['achievements_config'] = array(
81 '#title' => t('Additional configuration'),
82 '#type' => 'fieldset',
83 );
84 $module_path = drupal_get_path('module', 'achievements');
85 foreach (array('unlocked', 'locked', 'hidden') as $image_type) {
86 $form['achievements_config']['achievements_image_' . $image_type] = array(
87 '#default_value' => variable_get('achievements_image_' . $image_type, $module_path . '/images/default-' . $image_type . '-70.jpg'),
88 '#title' => t('Default @image_type picture', array('@image_type' => $image_type)),
89 '#type' => 'textfield',
90 );
91 }
92 $form['achievements_config']['achievements_unlocked_move_to_top'] = array(
93 '#type' => 'checkbox',
94 '#title' => t("Move the user's unlocked achievements to the top of their achievement page."),
95 '#default_value' => variable_get('achievements_unlocked_move_to_top', TRUE), // default to gamer-style.
96 );
97
98 return system_settings_form($form);
99 }
100
101 /**
102 * Submit callback; clear achievement info cache.
103 */
104 function achievements_clear_info_cache_submit() {
105 achievements_load(NULL, FALSE, TRUE);
106 drupal_set_message(t('Achievements information cache cleared.'));
107 }
108
109 /**
110 * Submit callback; give or take away an achievement from a user.
111 */
112 function achievements_manual_submit($form, &$form_state) {
113 if (empty($form_state['values']['achievements_manual_user'])) { // ZOMG, it's #required, fool!
114 drupal_set_message(t('A username or user ID is required to give or take achievements.'), 'error');
115 return;
116 }
117
118 if (empty($form_state['values']['achievements_manual_achievement'])) { // HITCHCOCK STABBY STAB.
119 drupal_set_message(t('An achievement ID is required to give or take achievements.'), 'error');
120 return;
121 }
122
123 // well, we've got form values, so load 'em in and check for validity.
124 $account = !is_numeric($form_state['values']['achievements_manual_user'])
125 ? user_load_by_name($form_state['values']['achievements_manual_user'])
126 : user_load($form_state['values']['achievements_manual_user']);
127 $achievement = achievements_load($form_state['values']['achievements_manual_achievement']);
128
129 if (!isset($account->uid) || $account->uid == 0) { // Wha? Who?
130 drupal_set_message(t('%username is not a valid user.', // I TRIED TO HELP.
131 array('%username' => $form_state['values']['achievements_manual_user'])), 'error');
132 return;
133 }
134
135 if (!isset($achievement['title'])) { // -100 DKP! That's not even funny anymore.
136 drupal_set_message(t('%achievement_id is not a valid achievement.', // M... many whelps?
137 array('%achievement_id' => $form_state['values']['achievements_manual_achievement'])), 'error');
138 return;
139 }
140
141 // if we've a valid user and achievement ID, give 'em the achievement, ya freeloaders.
142 if ($account && $achievement && $form_state['clicked_button']['#achievement_action'] == 'give') {
143 achievements_unlocked($achievement['id'], $account->uid);
144 drupal_set_message(t('%username has been given %achievement.', // yeah, yeah.
145 array('%username' => $account->name, '%achievement' => $achievement['title'])));
146 return;
147 }
148
149 // if we're taking away an achievement, first check if they have it.
150 if ($form_state['clicked_button']['#achievement_action'] == 'take') {
151 if (!achievements_unlocked_already($achievement['id'], $account->uid)) {
152 drupal_set_message(t('%username has not unlocked %achievement.', // palm to forehead.
153 array('%username' => $account->name, '%achievement' => $achievement['title'])), 'error');
154 return;
155 }
156
157 achievements_locked($achievement['id'], $account->uid);
158 drupal_set_message(t('%username has had %achievement taken away.', // nope, nope.
159 array('%username' => $account->name, '%achievement' => $achievement['title'])));
160 return;
161 }
162 }