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

Diff of /contributions/modules/roledelay/roledelay.module

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

revision 1.3, Wed Jul 26 16:36:09 2006 UTC revision 1.3.4.1, Wed May 14 00:08:33 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id$  // $Id: roledelay.module,v 1.3 2006/07/26 16:36:09 handelaar Exp $
3    
4    /**
5     * hook_menu() implementation
6     */
7    function roledelay_menu($may_cache) {
8      global $user;
9      $items = array();
10      if ($may_cache) {
11    
12        //settings page
13        $items[] = array(
14          'path' => 'admin/settings/roledelay',
15          'title' => t('Role Delay'),
16          'description' => t('Set up role delay roles and waiting periods.'),
17          'callback' => 'drupal_get_form',
18          'callback arguments' => array('roledelay_settings'),
19        );
20      }
21      return $items;
22    }
23    
24  /**  /**
25   * hook_settings() implementation   * hook_settings() implementation
# Line 9  function roledelay_settings() { Line 29  function roledelay_settings() {
29    $roles['none'] = t('none');    $roles['none'] = t('none');
30    $periods = array(3600 => t('hours'), 86400 => t('days'), 604800 => t('weeks'));    $periods = array(3600 => t('hours'), 86400 => t('days'), 604800 => t('weeks'));
31    $increments = range(1, 28);    $increments = range(1, 28);
   
 /*  $form = form_select('User Role', 'roledelay_role1', variable_get('roledelay_role1', 'none'), $roles, t('The role that will be granted to new users after the specified wait time.'));  
   $form .= form_select('Wait period', 'roledelay_period1', variable_get('roledelay_period1', '3600'), $periods, t('The wait period type.'));  
   $form .= form_select('Wait Increment', 'roledelay_increment1', variable_get('roledelay_increment1', '1'), $increments, t('The number to increment the wait period type by.'));  
   $group = form_group(t('First Role Delay'), $form);  
   
   $form = form_select('User Role', 'roledelay_role2', variable_get('roledelay_role2', 'none'), $roles, t('The role that will be granted to new users after the specified wait time.'));  
   $form .= form_select('Wait period', 'roledelay_period2', variable_get('roledelay_period2', '3600'), $periods, t('The wait period type.'));  
   $form .= form_select('Wait Increment', 'roledelay_increment2', variable_get('roledelay_increment2', '1'), $increments, t('The number to increment the wait period type by.'));  
   $group .= form_group(t('Second Role Delay'), $form);  
   
   return $group;*/  
32    
33    $form['first_role_delay'] = array(    $form['first_role_delay'] = array(
34      '#type' => 'fieldset',      '#type' => 'fieldset',
35      '#title' => 'First Role Delay',      '#title' => 'First Role Delay',
36      );    );
37    $form['first_role_delay']['roledelay_role1'] = array(    $form['first_role_delay']['roledelay_role1'] = array(
38      '#type' => 'select',      '#type' => 'select',
39      '#title' => t('User role'),      '#title' => t('User role'),
40      '#default_value' => variable_get('roledelay_role1', 'none'),      '#default_value' => variable_get('roledelay_role1', 'none'),
41      '#options' => $roles,      '#options' => $roles,
42      '#description' => t('The role that will be granted to new users after the specified wait time.')      '#description' => t('The role that will be granted to new users after the specified wait time.'),
43      );    );
44    $form['first_role_delay']['roledelay_period1'] = array(    $form['first_role_delay']['roledelay_period1'] = array(
45      '#type' => 'select',      '#type' => 'select',
46      '#title' => t('Wait period'),      '#title' => t('Wait period'),
47      '#default_value' => variable_get('roledelay_period1', '3600'),      '#default_value' => variable_get('roledelay_period1', '3600'),
48      '#options' => $periods,      '#options' => $periods,
49      '#description' => t('The wait period type.')      '#description' => t('The wait period type.'),
50      );    );
51    $form['first_role_delay']['roledelay_increment1'] = array(    $form['first_role_delay']['roledelay_increment1'] = array(
52      '#type' => 'select',      '#type' => 'select',
53      '#title' => t('Wait increment'),      '#title' => t('Wait increment'),
54      '#default_value' => variable_get('roledelay_increment1', '1'),      '#default_value' => variable_get('roledelay_increment1', '1'),
55      '#options' => $increments,      '#options' => $increments,
56      '#description' => t('The number to increment the wait period type by.')      '#description' => t('The number to increment the wait period type by.'),
57      );    );
58    
   
59    $form['second_role_delay'] = array(    $form['second_role_delay'] = array(
60      '#type' => 'fieldset',      '#type' => 'fieldset',
61      '#title' => 'Second Role Delay',      '#title' => 'Second Role Delay',
# Line 58  function roledelay_settings() { Line 65  function roledelay_settings() {
65      '#title' => t('User role'),      '#title' => t('User role'),
66      '#default_value' => variable_get('roledelay_role2', 'none'),      '#default_value' => variable_get('roledelay_role2', 'none'),
67      '#options' => $roles,      '#options' => $roles,
68      '#description' => t('The role that will be granted to new users after the specified wait time.')      '#description' => t('The role that will be granted to new users after the specified wait time.'),
69      );    );
70    $form['second_role_delay']['roledelay_period2'] = array(    $form['second_role_delay']['roledelay_period2'] = array(
71      '#type' => 'select',      '#type' => 'select',
72      '#title' => t('Wait period'),      '#title' => t('Wait period'),
73      '#default_value' => variable_get('roledelay_period2', '3600'),      '#default_value' => variable_get('roledelay_period2', '3600'),
74      '#options' => $periods,      '#options' => $periods,
75      '#description' => t('The wait period type.')      '#description' => t('The wait period type.'),
76      );    );
77    $form['second_role_delay']['roledelay_increment2'] = array(    $form['second_role_delay']['roledelay_increment2'] = array(
78      '#type' => 'select',      '#type' => 'select',
79      '#title' => t('Wait increment'),      '#title' => t('Wait increment'),
80      '#default_value' => variable_get('roledelay_increment2', '1'),      '#default_value' => variable_get('roledelay_increment2', '1'),
81      '#options' => $increments,      '#options' => $increments,
82      '#description' => t('The number to increment the wait period type by.')      '#description' => t('The number to increment the wait period type by.'),
83      );
84    
85      // Integration option with logintoboggan, if it is enabled
86      if (module_exists('logintoboggan')) {
87        $form['other'] = array(
88          '#type' => 'fieldset',
89          '#title' => 'Other',
90        );
91        $form['other']['logintoboggan_trigger'] = array(
92          '#type' => 'radios',
93          '#title' => t('Start roledelay timer when logintoboggan verifies user'),
94          '#default_value' => variable_get('logintoboggan_trigger', 0),
95          '#options' => array(t('disabled'), t('enabled')),
96          '#description' => t("If you're using logintoboggan to let a newly registrated user login immediately with a non-authenticated role, you may want to enable this option.  Otherwise, users could achieve delayed roles without ever verifying via logintoboggan. This may help prevent spambots from infiltrating the site."),
97      );      );
98      }
99    return $form;    return system_settings_form($form);
100  }  }
101    
   
102  /**  /**
103   * hook_user() implementation   * hook_user() implementation
104   */   */
105  function roledelay_user($op, &$edit, &$user, $category = NULL) {  function roledelay_user($op, &$edit, &$user, $category = NULL) {
106    if($op == 'insert' && (variable_get('roledelay_role1', 'none') != 'none')) {    // Start timer on insert (default)
107      if ($op == 'insert' && (variable_get('roledelay_role1', 'none') != 'none') && (variable_get('logintoboggan_trigger', 0) == 0)) {
108      // calculate the timestamp when the stage will be reached      // calculate the timestamp when the stage will be reached
109      $wait = $user->created + ((variable_get('roledelay_increment1', '1')+1) * variable_get('roledelay_period1', '3600'));      $wait = $user->created + ((variable_get('roledelay_increment1', '1')+1) * variable_get('roledelay_period1', '3600'));
110      db_query("INSERT INTO {roledelay1} (uid, waitstamp) VALUES (%d, %d)", $user->uid, $wait);      db_query("INSERT INTO {roledelay1} (uid, waitstamp) VALUES (%d, %d)", $user->uid, $wait);
111      if(variable_get('roledelay_role2', 'none') != 'none') {      if (variable_get('roledelay_role2', 'none') != 'none') {
112        $wait = $user->created + ((variable_get('roledelay_increment2', '1')+1) * variable_get('roledelay_period2', '3600'));        $wait = $user->created + ((variable_get('roledelay_increment2', '1')+1) * variable_get('roledelay_period2', '3600'));
113        db_query("INSERT INTO {roledelay2} (uid, waitstamp) VALUES (%d, %d)", $user->uid, $wait);        db_query("INSERT INTO {roledelay2} (uid, waitstamp) VALUES (%d, %d)", $user->uid, $wait);
114      }      }
115    }    }
116      // Start timer on logingtoboggan verification if configured as such
117      else if ($op == 'update' && (variable_get('roledelay_role1', 'none') != 'none') && (variable_get('logintoboggan_trigger', 0) == 1) && $user->logintoboggan_email_validated) {
118        // calculate the timestamp when the stage will be reached
119        $rd = db_result(db_query("SELECT waitstamp FROM {roledelay1} WHERE uid = %d", $user->uid));
120        if (!$rd) {
121          $wait = time() + ((variable_get('roledelay_increment1', '1')+1) * variable_get('roledelay_period1', '3600'));
122          db_query("INSERT INTO {roledelay1} (uid, waitstamp) VALUES (%d, %d)", $user->uid, $wait);
123          if (variable_get('roledelay_role2', 'none') != 'none') {
124            $wait = time() + ((variable_get('roledelay_increment2', '1')+1) * variable_get('roledelay_period2', '3600'));
125            db_query("INSERT INTO {roledelay2} (uid, waitstamp) VALUES (%d, %d)", $user->uid, $wait);
126          }
127        }
128        unset($user->logintoboggan_email_validated);
129      }
130  }  }
131    
132  /**  /**
# Line 99  function roledelay_user($op, &$edit, &$u Line 134  function roledelay_user($op, &$edit, &$u
134   */   */
135  function roledelay_cron() {  function roledelay_cron() {
136    // only proceed if the first role delay stage is setup.    // only proceed if the first role delay stage is setup.
137    if(variable_get('roledelay_role1', 'none') != 'none') {    if (variable_get('roledelay_role1', 'none') != 'none') {
138      $roles = user_roles(TRUE);      $roles = user_roles(TRUE);
139      $role = variable_get('roledelay_role1', 'none');      $role = variable_get('roledelay_role1', 'none');
140    
141      // we use time() here because we store the wait timestamp value in the db, not the created timestamp      // we use time() here because we store the wait timestamp value in the db, not the created
142      $results = db_query("SELECT u.name, r.* FROM {roledelay1} r LEFT JOIN {users} u ON u.uid = r.uid WHERE waitstamp < %d", time());      // timestamp
143      while($u = db_fetch_object($results)) {      $results = db_query("SELECT u.name, r.* FROM {roledelay1} r LEFT JOIN {users} u ON u.uid = r.uid WHERE waitstamp < %d AND u.uid != 0", time());
144        while ($u = db_fetch_object($results)) {
145        $count = db_fetch_object(db_query("SELECT count(*) AS count FROM {users_roles} WHERE uid = %d AND rid = %d", $u->uid, $role));        $count = db_fetch_object(db_query("SELECT count(*) AS count FROM {users_roles} WHERE uid = %d AND rid = %d", $u->uid, $role));
146        if(!$count->count) {        if (!$count->count) {
147          db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $u->uid, $role);          db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $u->uid, $role);
148          watchdog('roledelay', t('User %name added to %role role. Stage one', array('%name' => l($u->name, 'user/'. $u->uid), '%role' => $roles[$role])));          watchdog('roledelay', t('User %name added to %role role. Stage one', array('%name' => l($u->name, 'user/'. $u->uid), '%role' => $roles[$role])));
149        }        }
# Line 116  function roledelay_cron() { Line 152  function roledelay_cron() {
152        }        }
153        db_query("DELETE FROM {roledelay1} WHERE uid = %d", $u->uid);        db_query("DELETE FROM {roledelay1} WHERE uid = %d", $u->uid);
154      }      }
155    
156      // process the second stage if we have the second role delay stage setup      // process the second stage if we have the second role delay stage setup
157      if(variable_get('roledelay_role2', 'none') != 'none') {      if (variable_get('roledelay_role2', 'none') != 'none') {
158        $role = variable_get('roledelay_role2', 'none');        $role = variable_get('roledelay_role2', 'none');
159        $results = db_query("SELECT u.name, r.* FROM {roledelay2} r LEFT JOIN {users} u ON u.uid = r.uid WHERE waitstamp < %d", time());        $results = db_query("SELECT u.name, r.* FROM {roledelay2} r LEFT JOIN {users} u ON u.uid = r.uid WHERE waitstamp < %d AND u.uid != 0", time());
160        while($u = db_fetch_object($results)) {        while ($u = db_fetch_object($results)) {
161          $count = db_fetch_object(db_query("SELECT count(*) AS count FROM {users_roles} WHERE uid = %d AND rid = %d", $u->uid, $role));          $count = db_fetch_object(db_query("SELECT count(*) AS count FROM {users_roles} WHERE uid = %d AND rid = %d", $u->uid, $role));
162          if(!$count->count) {          if (!$count->count) {
163            db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $u->uid, $role);            db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $u->uid, $role);
164            watchdog('roledelay', t('User %name added to %role role. Stage two', array('%name' => l($u->name, 'user/'. $u->uid), '%role' => $roles[$role])));            watchdog('roledelay', t('User %name added to %role role. Stage two', array('%name' => l($u->name, 'user/'. $u->uid), '%role' => $roles[$role])));
165          }          }
# Line 140  function roledelay_cron() { Line 177  function roledelay_cron() {
177   */   */
178  function roledelay_help($section) {  function roledelay_help($section) {
179    switch ($section) {    switch ($section) {
     case 'admin/modules#description':  
       return t('Grants users additional roles after a certain wait period from registration.');  
       break;  
180      case 'admin/settings/roledelay':      case 'admin/settings/roledelay':
181        return t('Please note, if the \'First Role Delay\' section\'s \'User Role\' value is set to \'none\', then the \'Second Role Delay\' section has no effect.');        return t('Please note, if the \'First Role Delay\' section\'s \'User Role\' value is set to \'none\', then the \'Second Role Delay\' section has no effect.');
182        break;        break;

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.3.4.1

  ViewVC Help
Powered by ViewVC 1.1.2