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

Contents of /contributions/modules/account_expiry/account_expiry.module

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


Revision 1.3 - (show annotations) (download) (as text)
Fri May 2 04:04:17 2008 UTC (18 months, 3 weeks ago) by jehnich
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +1 -1 lines
File MIME type: text/x-php
CHANGELOG.txt added
1 <?php
2 // $Id:
3
4 /**
5 * @file
6 * Allows to maintain an expiry date for a user account
7 */
8
9 function account_expiry_help($section = null) {
10 switch ($section) {
11 case 'admin/modules#description':
12 case 'admin/help#account_expiry':
13 return t('Allows to maintain an expiry date for a user account.');
14 break;
15 }
16 }
17
18 function account_expiry_perm() {
19 return array('administer account expiry');
20 }
21
22 function account_expiry_menu($may_cache) {
23 $items[] = array(
24 'path' => 'admin/settings/account_expiry',
25 'title' => t('Account Expiry'),
26 'description' => t('Change settings for the Account Expiry module.'),
27 'callback' => 'drupal_get_form',
28 'callback arguments' => 'account_expiry_admin_settings',
29 'access' => user_access('administer account expiry'),
30 );
31 $items[] = array(
32 'path' => 'admin/user/account_expiry',
33 'title' => t('User Account Expiry'),
34 'description' => t('Change expiry date for user account.'),
35 'callback' => 'drupal_get_form',
36 'callback arguments' => 'account_expiry_user_settings',
37 'access' => user_access('administer account expiry'),
38 );
39 return $items;
40 }
41
42 /**
43 * Implementation of hook_block().
44 */
45 function account_expiry_block($op = 'list', $delta = 0, $edit = array()) {
46 global $user;
47 switch ($op) {
48 case 'list':
49 $blocks[0]['info'] = t('Account Expiry');
50 return $blocks;
51 case 'view':
52 switch ($delta) {
53 case 0:
54 $block['subject'] = t('Account Expiry');
55 $sql = "SELECT expiry_date FROM {account_expiry} WHERE uid = %d";
56 $result = db_query($sql, $user->uid);
57 while ($existing = db_fetch_object($result)) {
58 $expirydate = $existing->expiry_date;
59 }
60 $block['content'] = format_date($expirydate);
61 break;
62 }
63 return $block;
64 }
65 }
66
67 function account_expiry_admin_settings() {
68 $form['defaults'] = array(
69 '#type' => 'fieldset',
70 '#title' => t('Account expiry default settings'),
71 );
72 $form['defaults']['account_expiry_default_days'] = array(
73 '#type' => 'textfield',
74 '#title' => t('Default number of days until account expires'),
75 '#default_value' => variable_get('account_expiry_default_days', 30),
76 '#description' => t('Expiry date for particular users can be altered from this default'),
77 '#size' => 2,
78 '#maxlength' => 4,
79 );
80
81 $form['types'] = array(
82 '#type' => 'fieldset',
83 '#title' => t('Account types'),
84 );
85
86 if (module_exists('accounttypes')) {
87 $typeselector = "Account";
88 $message = t('AccountTypes module is active - select account types that will have expiry date');
89 $selector = array();
90 }
91 else {
92 $typeselector = "";
93 $message = t('AccountTypes module is not active - ');
94 $selector = array();
95 }
96 $form['types']['message'] = array(
97 '#value' => $message,
98 );
99 switch ($typeselector) {
100 case 'Account':
101 //Get account types
102 $accounttypes = array();
103 $sql = "SELECT atid, name from {accounttypes}";
104 $result = db_query($sql);
105 while ($at = db_fetch_object($result)) {
106 $accounttypes[$at->atid] = $at->name;
107 }
108 $defaulttypes = variable_get('account_expiry_accounttypes', array(0));
109 $form['types']['account_expiry_accounttypes'] = array(
110 '#type' => 'checkboxes',
111 '#title' => t('Account types'),
112 '#options' => $accounttypes,
113 '#default_value' => $defaulttypes,
114 );
115 break;
116 }
117 return system_settings_form($form);
118 }
119
120 function account_expiry_form_alter($form_id, &$form) {
121 if ($form_id == 'user_edit' && user_access('administer account expiry')) {
122 $atid = $form['_account']['#value']->selectAT;
123 if (_account_expiry_isexpiryaccount($atid)) {
124 $uid = $form['_account']['#value']->uid;
125 $sql = "SELECT expiry_date FROM {account_expiry} WHERE uid = %d";
126 $result = db_query($sql, $uid);
127 while ($existing = db_fetch_object($result)) {
128 $expirydate = $existing->expiry_date;
129 }
130 $form['account']['expiry_date'] = array(
131 '#type' => 'date',
132 '#title' => t('Account expiry date'),
133 '#weight' => 2,
134 '#default_value' => array(
135 'day' => format_date($expirydate, 'custom', 'j'),
136 'month' => format_date($expirydate, 'custom', 'n'),
137 'year' => format_date($expirydate, 'custom', 'Y')
138 ),
139 '#description' => t('Set accordingly'),
140 );
141 }
142 }
143 }
144
145 function account_expiry_user($op, $edit, &$account, $category = NULL) {
146 if (!$edit['selectAT']) {
147 if (isset($account->selectAT)) {
148 $select_at = $account->selectAT;
149 }
150 }
151 else {
152 $select_at = $edit['selectAT'];
153 }
154 switch ($op) {
155 case 'view':
156 //dsm($account);
157 if (_account_expiry_isexpiryaccount($select_at)) {
158 $sql = "SELECT expiry_date FROM {account_expiry} WHERE uid = %d";
159 $result = db_query($sql, $account->uid);
160 while ($existing = db_fetch_object($result)) {
161 $expirydate = $existing->expiry_date;
162 }
163 $items[] = array(
164 'value' => format_date($expirydate),
165 'class' => 'accountexpiry',
166 );
167 return array(t('Account Expiry Date') => $items);
168 }
169 break;
170
171 case 'insert':
172 if (_account_expiry_isexpiryaccount($select_at)) {
173 account_expiry_edit($edit['uid']);
174 }
175 break;
176
177 case 'after_update':
178 //dsm($account);
179 //dsm($edit);
180 if (_account_expiry_isexpiryaccount($select_at)) {
181 $day = $account->expiry_date['day'];
182 $month = $account->expiry_date['month'];
183 $year = $account->expiry_date['year'];
184 $expiry_timestamp = mktime(0, 0, 0, $month, $day, $year);
185 account_expiry_edit($account->uid, $expiry_timestamp);
186 }
187 else {
188 account_expiry_delete($account->uid);
189 }
190 break;
191
192 case 'delete':
193 account_expiry_delete($account->uid);
194 break;
195 }
196 }
197
198
199 function account_expiry_user_settings() {
200 $form['header'] = array(
201 '#type' => 'value',
202 '#value' => array(
203 array('data' => t('Name')),
204 array('data' => t('Expiry')),
205 array('data' => t('Status')),
206 array('data' => t('UID')),
207 array('data' => t('Account Type')),
208 )
209 );
210 $user_accounttypes = _account_expiry_getuseraccounttypes();
211
212 $sql = 'SELECT a.uid, u.name, a.expiry_date, u.status
213 FROM {account_expiry} a
214 INNER JOIN {users} u
215 ON a.uid = u.uid
216 WHERE u.uid > %d
217 ORDER BY %s';
218
219 $result = db_query($sql, 1, 'expiry_date');
220
221 while ($existing = db_fetch_object($result)) {
222 $atid = $user_accounttypes[$existing->uid]['atid'];
223 $form['atid'][$atid]['uid'][$existing->uid] = array(
224 '#value' => $existing->uid,
225 );
226 $form['atid'][$atid]['name'][$existing->uid] = array(
227 '#value' => $existing->name,
228 );
229 $form['atid'][$atid]['status'][$existing->uid] = array(
230 '#value' => $existing->status,
231 );
232 $form['atid'][$atid]['accounttype'][$existing->uid] = array(
233 '#value' => $user_accounttypes[$existing->uid]['name'],
234 );
235 $form['atid'][$atid]['expiry_date'][$existing->uid] = array(
236 '#type' => 'date',
237 '#default_value' => array(
238 'day' => format_date($existing->expiry_date, 'custom', 'j'),
239 'month' => format_date($existing->expiry_date, 'custom', 'n'),
240 'year' => format_date($existing->expiry_date, 'custom', 'Y')
241 ),
242 );
243 $form['atid'][$atid]['expiry_date_orig'][$existing->uid] = array(
244 '#type' => 'value',
245 '#value' => $existing->expiry_date,
246 );
247 }
248 $form['expiry_date_edit'] = array(
249 '#type' => 'value',
250 '#value' => array(),
251 );
252 $form['submit'] = array(
253 '#type' => 'submit',
254 '#value' => t('Submit')
255 );
256 $form['#tree'] = TRUE;
257 return $form;
258 }
259
260 function theme_account_expiry_user_settings($form) {
261 $status = array('Blocked', 'Active');
262 $output = "";
263 foreach (element_children($form['atid']) as $atid) {
264 $rows = array();
265 $legend = t('Account expiry settings for %atname accounts', array('%atname' => _account_expiry_getaccounttypename($atid)));
266 $output .= "<div><fieldset><legend>". $legend ."</legend>";
267 foreach (element_children($form['atid'][$atid]['expiry_date']) as $key) {
268 $row = array();
269 $row['data'][0] = l(drupal_render($form['atid'][$atid]['name'][$key]), 'user/'. drupal_render($form['atid'][$atid]['uid'][$key]));
270 $row['data'][1] = drupal_render($form['atid'][$atid]['expiry_date'][$key]);
271 $row['data'][2] = $status[drupal_render($form['atid'][$atid]['status'][$key])];
272 $row['data'][3] = $key;
273 $row['data'][4] = drupal_render($form['atid'][$atid]['accounttype'][$key]);
274 $rows[] = $row;
275 }
276 $output .= theme('table', $form['header']['#value'], $rows);
277 $output .= "</fieldset></div>";
278 }
279 $output .= drupal_render($form); // Process any other fields and display them
280 return $output;
281 }
282
283 function account_expiry_user_settings_submit($form_id, $form_values) {
284 foreach (element_children($form_values['atid']) as $atid ) {
285 foreach (element_children($form_values['atid'][$atid]['expiry_date']) as $uid ) {
286 $old_timestamp = $form_values['atid'][$atid]['expiry_date_orig'][$uid];
287 $day = $form_values['atid'][$atid]['expiry_date'][$uid]['day'];
288 $month = $form_values['atid'][$atid]['expiry_date'][$uid]['month'];
289 $year = $form_values['atid'][$atid]['expiry_date'][$uid]['year'];
290 $expiry_timestamp = mktime(0, 0, 0, $month, $day, $year);
291 account_expiry_edit($uid, $expiry_timestamp);
292 }
293 }
294 }
295
296 function account_expiry_edit($uid, $expiry_timestamp = NULL) {
297 if (is_null($expiry_timestamp)) {
298 $expiry_timestamp = _account_expiry_getdefaultexpiry();
299 }
300 $sql = "SELECT ae.uid, ae.expiry_date, u.status
301 FROM {users} u
302 INNER JOIN {account_expiry} ae ON u.uid = ae.uid
303 WHERE u.uid = %d";
304 $result = db_query($sql, $uid);
305 if ($existing = db_fetch_object($result)) {
306 account_expiry_update($uid, $existing, $expiry_timestamp);
307 }
308 else {
309 account_expiry_insert($uid, $expiry_timestamp);
310 }
311 }
312
313 function account_expiry_insert($uid, $expiry_timestamp) {
314 $sql = "INSERT INTO {account_expiry} (uid, expiry_date) VALUES (%d, %d)";
315 if (db_query($sql, $uid, $expiry_timestamp)) {
316 drupal_set_message(t('User expiry date set.'));
317 }
318 else {
319 drupal_set_message(t('User expiry date has NOT been set.'), 'error');
320 }
321 }
322
323 function account_expiry_update($uid, $existing, $expiry_timestamp) {
324 //Convert the date array to a unix timestamp
325 //http://drupal.org/node/61580 & http://drupal.org/node/46805
326 $now = time();
327 if ($existing->expiry_date != $expiry_timestamp) { // Date got changed on the form
328 $sql = "UPDATE {account_expiry} SET expiry_date = %d WHERE uid = %d";
329 if (db_query($sql, $expiry_timestamp, $uid)) {
330 if (($expiry_timestamp < $now) AND ($existing->status == 1)) {
331 // Active user has expiry now in past
332 account_expiry_expire($uid);
333 }
334 else if (($expiry_timestamp > $now) AND ($existing->status == 0)) {
335 // Blocked user has expiry now in future
336 account_expiry_activate($uid);
337 }
338 else {// only date updated, no change of status
339 drupal_set_message(t('Expiry date updated for user ') . $uid);
340 }
341 }
342 else {
343 drupal_set_message(t('Account Expiry update failed for user ') . $uid, 'error');
344 }
345 }
346 }
347
348 function account_expiry_expire($uid) {
349 $sql = "UPDATE {users} SET status = 0 WHERE uid = %d";
350 $result = FALSE;
351 if (db_query($sql, $uid)) {
352 drupal_set_message(t('Account blocked for user '. $uid));
353 $result = TRUE;
354 }
355 else {
356 drupal_set_message(t('Account has NOT been blocked for user '. $uid), 'error');
357 }
358 return $result;
359 }
360
361 function account_expiry_activate($uid) {
362 $sql = "UPDATE {users} SET status = 1 WHERE uid = %d";
363 if (db_query($sql, $uid)) {
364 drupal_set_message(t('Account set to Active for user '. $uid));
365 }
366 else {
367 drupal_set_message(t('Account has NOT been set to Active for user '. $uid), 'error');
368 }
369 }
370
371 function account_expiry_delete($uid) {
372 $sql = "DELETE FROM {account_expiry} WHERE uid = %d";
373 if (db_query($sql, $uid)) {
374 drupal_set_message(t('Expiry date removed for user '. $uid));
375 }
376 }
377
378 //http://drupal.org/node/195244
379 function account_expiry_cron() {
380 $now = time();
381 $sql = "SELECT u.uid, u.name
382 FROM {users} u
383 INNER JOIN {account_expiry} ae ON u.uid = ae.uid
384 WHERE u.status = 1
385 AND ae.expiry_date < %d";
386 $result = db_query($sql, $now);
387 while ($expiry = db_fetch_object($result)) {
388 if (account_expiry_expire ($expiry->uid)) {
389 $message = t('Account blocked for user: @name ', array('@name' => $expiry->name));
390 $link = l('user '. $expiry->uid, 'user/'. $expiry->uid);
391 watchdog('Account Expiry', $message, WATCHDOG_NOTICE, $link);
392 }
393 }
394 }
395
396 function _account_expiry_getdefaultexpiry() {
397 $default_days = variable_get('account_expiry_default_days', 30);
398 $expiry_date = time() + ($default_days * 24 * 60 * 60);
399 // default_days; 24 hours; 60 mins; 60secs
400 return $expiry_date;
401 }
402
403 function _account_expiry_getaccounttypename($atid) {
404 $sql = "SELECT name FROM {accounttypes} WHERE atid = %d";
405 $result = db_query($sql, $atid);
406 while ($accounttype = db_fetch_object($result)) {
407 $accounttypename = $accounttype->name;
408 }
409 return $accounttypename;
410 }
411
412 function _account_expiry_getuseraccounttypes() {
413 $useraccounttypes = array();
414 $sql = "SELECT a.name, au.atid, au.uid
415 FROM {accounttypes_users} au
416 INNER JOIN {accounttypes} a ON au.atid = a.atid WHERE au.uid > %d
417 ORDER BY %s";
418 $result = db_query($sql, 1, 'uid');
419 while ($useraccount = db_fetch_object($result)) {
420 $useraccounttypes[$useraccount->uid]['atid'] = $useraccount->atid;
421 $useraccounttypes[$useraccount->uid]['name'] = $useraccount->name;
422 }
423 return $useraccounttypes;
424 }
425
426 function _account_expiry_isexpiryaccount($accountid) {
427 $accounttypes = variable_get('account_expiry_accounttypes', array(0));
428 $accountexpire = FALSE;
429 foreach ($accounttypes as $atid => $expire) {
430 if ($accountid == $atid && $expire) {
431 $accountexpire = TRUE;
432 }
433 }
434 return $accountexpire;
435 }

  ViewVC Help
Powered by ViewVC 1.1.2