/[drupal]/contributions/modules/user_quota/user_quota.module
ViewVC logotype

Contents of /contributions/modules/user_quota/user_quota.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.17 - (show annotations) (download) (as text)
Fri Dec 19 23:56:11 2008 UTC (11 months, 1 week ago) by greggles
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-BETA2, DRUPAL-6--1-0-BETA1, HEAD
Changes since 1.16: +1 -1 lines
File MIME type: text/x-php
coder cleanup
1 <?php
2 /**
3 * @file
4 * The user_quota module which provides per user, per content type quotas.
5 */
6
7 /**
8 * Implementation of hook_menu().
9 *
10 * @return array An array of menu items
11 */
12 function user_quota_menu() {
13
14 $item['admin/user/user_quota'] = array(
15 'title' => 'User quota',
16 'description' => 'View and manage user quotas.',
17 'page callback' => 'user_quota_settings',
18 'access arguments' => array('administer site configuration'),
19 'type' => MENU_NORMAL_ITEM,
20 );
21
22 $item['admin/user/user_quota/manage'] = array(
23 'title' => 'Manage a quota',
24 'description' => 'Manage the quota for a user or add a new quota for a user',
25 'page callback' => 'user_quota_manage',
26 'access arguments' => array('administer site configuration'),
27 'type' => MENU_NORMAL_ITEM,
28 );
29
30 return ($item);
31 }
32
33 /**
34 * This function prints up a report of users and their quotas
35 *
36 * @return string The content to display in the browser.
37 */
38 function user_quota_settings() {
39 $output = t('<p>This page shows an overall report of quotas for all users. Click on the "Details and manage" link for a specific user to see the history of their quota. You can also <a href="!uri">create a new quota for a user</a>.</p>', array('!uri' => url('admin/user/user_quota/manage/')));
40
41 $limit = 100;
42 $query = 'SELECT uq.uid, name, current_limit, type FROM {user_quota} uq INNER JOIN {users} u ON uq.uid = u.uid ORDER BY uid, type';
43 $results = pager_query($query, $limit);
44 $rows = array();
45 while ($result = db_fetch_object($results)) {
46 $rows[] = array(l($result->name, 'user/'. $result->uid), check_plain($result->current_limit), check_plain($result->type), l('Details and manage', 'admin/user/user_quota/manage/'. $result->uid));
47 }
48
49 $header = array(array('data' => 'User'), array('data' => 'Quota'), array('data' => 'Type'), array('data' => 'Operations'));
50
51 $output .= theme('table', $header, $rows);
52 $output .= theme('pager', NULL, $limit, 0);
53 return $output;
54 }
55
56 /**
57 * A page callback to manage user's accounts.
58 * @param $uid
59 * Optional user id.
60 *
61 */
62 function user_quota_manage($uid = NULL) {
63 $output = '';
64 if (isset($uid)) {
65 $account = user_load(array('uid' => $uid));
66 if ($account->uid) {
67 drupal_set_title(t('Manage quota for %user', array('%user' => $account->name)));
68 $output .= drupal_get_form('user_quota_manage_form', $account);
69
70 // Get current quota limits.
71 $current = db_query("SELECT current_limit, type FROM {user_quota} WHERE uid = %d", $uid);
72 $current_header = array(array('data' => t('Quota')),
73 array('data' => t('Content type'))
74 );
75 while ($current_result = db_fetch_object($current)) {
76 $current_rows[] = array($current_result->current_limit, check_plain($current_result->type));
77 }
78 if (isset($current_rows)) {
79 $output .= t('<h3>Current quotas</h3>');
80 $output .= theme('table', $current_header, $current_rows);
81 }
82
83 // Get historic quota alterations.
84 $history = db_query("SELECT quota, type, uqh.created, message, u.name, altering_uid FROM {user_quota_history} uqh LEFT JOIN {users} u ON uqh.altering_uid = u.uid WHERE uqh.uid = %d ORDER BY created DESC", $uid);
85 $history_header = array(array('data' => t('Quota')),
86 array('data' => t('Content type')),
87 array('data' => t('Date')),
88 array('data' => t('Altering user')),
89 array('data' => t('Message')),
90
91 );
92 while ($history_result = db_fetch_object($history)) {
93 $history_rows[] = array($history_result->quota, check_plain($history_result->type), format_date($history_result->created), l($history_result->name, 'user/'. $history_result->altering_uid), check_markup($history_result->message));
94 }
95 if (isset($history_rows)) {
96 $output .= t('<h3>Quota modification history</h3>');
97 $output .= theme('table', $history_header, $history_rows);
98 }
99 }
100 else {
101 drupal_set_message(t("Invalid URL argument, proceeding with the default form."));
102 $output .= drupal_get_form('user_quota_manage_form');
103 }
104 }
105 else {
106 $output .= drupal_get_form('user_quota_manage_form');
107 }
108 return $output;
109 }
110
111 /**
112 * A form to manage a user's quota.
113 */
114 function user_quota_manage_form($form_state, $account = NULL) {
115 $form['user_quota_form'] = array(
116 '#type' => 'fieldset',
117 '#title' => t('Modify quota'),
118 '#collapsible' => TRUE,
119 '#collapsed' => empty($account) ? FALSE : TRUE,
120 );
121 // The username
122 $form['user_quota_form']['username'] = array(
123 '#type' => 'textfield',
124 '#title' => t('Username'),
125 '#default_value' => isset($account->name) ? check_plain($account->name): '',
126 '#size' => 60,
127 '#autocomplete_path' => 'user/autocomplete',
128 '#maxlength' => 64,
129 '#required' => TRUE,
130 );
131
132 // The content type to limit
133 $types = node_get_types();
134 foreach ($types as $machine => $type) {
135 $options[$machine] = $type->name;
136 }
137 $form['user_quota_form']['node_type'] = array(
138 '#type' => 'select',
139 '#title' => t('Content type to limit'),
140 '#options' => $options,
141 '#default_value' => 'product',
142 );
143
144 // The limit number
145 $form['user_quota_form']['quota'] = array(
146 '#type' => 'textfield',
147 '#title' => t('Amount to alter quota (add or decrease)'),
148 '#default_value' => 0,
149 '#size' => 60,
150 '#maxlength' => 64,
151 '#required' => TRUE,
152 '#description' => t('Negative values will decrease the quota. It is possible for a user to have a negative quota.'),
153 );
154
155 // An optional message
156 $form['user_quota_form']['message'] = array(
157 '#type' => 'textarea',
158 '#title' => t('Modification message'),
159 '#description' => t('Provide a motivation for your change.'),
160 '#cols' => 60,
161 '#rows' => 2,
162 '#description' => t('Negative values will decrease the quota. It is possible for a user to have a negative quota.'),
163 );
164
165 // And go!
166 $form['user_quota_form']['submit'] = array(
167 '#type' => 'submit',
168 '#value' => t('Save'),
169 );
170
171 return $form;
172 }
173
174 function user_quota_manage_form_validate($form, &$form_state) {
175 // Is it a non-zero number?
176 if (!is_numeric($form['user_quota_form']['quota']['#value']) || $form['user_quota_form']['quota']['#value'] == 0) {
177 form_set_error('quota', t('Please enter a non-zero number'));
178 }
179 // Is it a real user?
180 $account = user_load(array('name' => $form['user_quota_form']['username']['#value']));
181 if (!$account->uid) {
182 form_set_error('username', t('Please select a valid username'));
183 }
184 }
185
186 function user_quota_manage_form_submit($form, &$form_state) {
187 $account = user_load(array('name' => $form['user_quota_form']['username']['#value']));
188 global $user;
189 user_quota_set_user_quota($account->uid, $form['user_quota_form']['quota']['#value'], $form['user_quota_form']['node_type']['#value'], $form['user_quota_form']['message']['#value'], $user->uid);
190 drupal_set_message(t('Quota updated for %user', array('%user' => $account->name)));
191 $form_state['redirect'] = url('admin/user/user_quota/manage/'. $account->uid, array('absolute' => TRUE));
192 }
193
194 /**
195 * Implementation of hook_nodeapi().
196 *
197 */
198 function user_quota_nodeapi(&$node, $op, $a3, $a4) {
199 // Catches creating new posts
200 if (empty($node->nid) && ($op == 'prepare' || $op == 'validate')) {
201 global $user;
202 user_quota_check_limit($node, $user);
203 }
204 elseif ($op == 'insert') {
205 // Decrement any records by the value set during form_alter
206 // Slightly sneaky use of UPDATE: if they are not in a quota it will affect 0 rows.
207 db_query("UPDATE {user_quota} SET current_limit = current_limit - %d WHERE uid = %d AND type = '%s'", $node->user_quota_decrement, $node->uid, $node->type);
208 if (db_affected_rows()) {
209 // If rows are affected then they have a quota, so log it in the history table.
210 global $user;
211 $message = t('Created @type content <a href="!uri">@title</a>.', array('@user-name' => $user->name, '@type' => $node->type, '!uri' => url('node/'. $node->nid), '@title' => $node->title));
212 db_query("INSERT INTO {user_quota_history} (uid, quota, type, created, message, altering_uid) VALUES (%d, %d, '%s', %d, '%s', %d)", $node->uid, -$node->user_quota_decrement, $node->type, time(), $message, $user->uid);
213 }
214 }
215 }
216
217 /**
218 * Check their quota for this node type.
219 *
220 * @param object $user
221 * The $user object
222 * @param string $type
223 * The node type
224 */
225 function user_quota_check_limit(&$node, $user) {
226 $limit = user_quota_get_limit($node->type, $user);
227 if ($limit === 'unlimited') {
228 return TRUE;
229 }
230 elseif ($limit > 0) {
231 return $limit;
232 }
233 else {
234 form_set_error('', t('You currently may not create another %type. You must first gain more credits.', array('%type' => $node->type)));
235 }
236 }
237
238 function user_quota_get_limit($type, $user = FALSE) {
239 if (!$user) {
240 global $user;
241 }
242 $limit = db_result(db_query("SELECT current_limit FROM {user_quota} WHERE uid = %d AND type = '%s'", $user->uid, $type));
243 if (is_numeric($limit)) {
244 return $limit;
245 }
246 else {
247 return 'unlimited';
248 }
249 }
250
251 /**
252 * Implementation of hook_form_alter().
253 *
254 * We always decrement by 1, another module can override that.
255 */
256 function user_quota_form_alter(&$form, $form_state, $form_id) {
257 // It is possible for a separate module to create fancier rules than just "decrement by 1 each time"
258 // That module would just have to set it's system.weight heavier than 0 and alter the
259 // node form to change this to a different value.
260 if ($form['#id'] == 'node-form' && empty($form['nid']['#value'])) {
261 $form['user_quota_decrement'] = array(
262 '#type' => 'hidden',
263 '#default_value' => 1,
264 );
265 }
266 }
267
268 /**
269 * Helper function to create a quota entry.
270 */
271 function user_quota_set_user_quota($uid, $limit, $type, $message, $altering_uid) {
272 // Log it in the history table.
273 db_query("INSERT INTO {user_quota_history} (uid, quota, type, created, message, altering_uid) VALUES (%d, %d, '%s', %d, '%s', %d)", $uid, $limit, $type, time(), $message, $altering_uid);
274
275 // Update it, if that doesn't change anything then add it.
276 db_query("UPDATE {user_quota} SET current_limit = current_limit + %d WHERE uid = %d AND type = '%s'", $limit, $uid, $type);
277 if (!db_affected_rows()) {
278 db_query("INSERT INTO {user_quota} (uid, current_limit, type) VALUES (%d, %d, '%s')", $uid, $limit, $type);
279 }
280 }

  ViewVC Help
Powered by ViewVC 1.1.2