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

Diff of /contributions/modules/autoassignrole/autoassignrole.module

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

revision 1.9 by cyberswat, Tue Feb 12 17:27:58 2008 UTC revision 1.10 by cyberswat, Wed Aug 13 21:38:14 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id$  // $Id: autoassignrole.module,v 1.9 2008/02/12 17:27:58 cyberswat Exp $
3  define("AUTOASSIGNROLE_ROLE", "");  
4  define("AUTOASSIGNROLE_ROLE_ACTIVE", 0);  /**
5  define("AUTOASSIGNROLE_ROLE_USER_ACTIVE", 0);   * @file
6  define("AUTOASSIGNROLE_ROLE_USER", "");   *
7  define("AUTOASSIGNROLE_ROLE_USER_MULTIPLE", 0);   * The main autoassignrole.module file
8  define("AUTOASSIGNROLE_ROLE_USER_DESCRIPTION", "");   *
9  define("AUTOASSIGNROLE_ROLE_USER_TITLE", t("Role"));   * Designate a role to assign all new users to in addition to providing a
10  define("AUTOASSIGNROLE_ROLE_SORT", "SORT_ASC");   * mechanism for new users to select a role or role for their account.
11     */
12    
13  /**  /**
14   * Implementation of hook_menu().   * Implementation of hook_menu().
15   *   *
16   * @return array   * @return array
17   */   */
18  function autoassignrole_menu($may_cache) {  function autoassignrole_menu() {
19    $items = array();    $items = array();
20    if ($may_cache) {    $items['admin/user/autoassignrole'] = array(
     $items[] = array(  
     'path' => 'admin/user/autoassignrole',  
21      'title' => t('Auto assign role'),      'title' => t('Auto assign role'),
22      'description' => t('Auto Assign Role Settings.'),      'description' => t('Designate a role to assign all new users to.'),
23      'callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
24      'callback arguments' => 'autoassignrole_settings',      'page arguments' => array('autoassignrole_admin_form'),
25      'access' => user_access('administer autoassignrole'),      'access arguments' => array('administer autoassignrole'),
26      );      'type' => MENU_NORMAL_ITEM,
27    }      'file' => 'autoassignrole-admin.inc',
   return $items;  
 }  
 function autoassignrole_settings() {  
   $form['autoassignrole_settings_auto'] = array(  
   '#type' => 'fieldset',  
   '#title' => t('Automatic Role Assignment'),  
   '#collapsible' => TRUE,  
   '#collapsed' => TRUE,  
   );  
   $form['autoassignrole_settings_auto']['AUTOASSIGNROLE_ROLE_ACTIVE'] = array(  
     '#type' => 'radios',  
     '#title' => t('Automatic role assignment'),  
     '#description' => t('Automatic role assignment occurs when the user first logins to the account.  This happens without the users knowledge.'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_ACTIVE',0),  
     '#options' => array(1 => t('Enabled'), 0 => t('Disabled')),  
   );  
   $form['autoassignrole_settings_auto']['AUTOASSIGNROLE_ROLE'] = array(  
     '#type' => 'checkboxes',  
     '#title' => t('Role'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE',''),  
     '#options' => user_roles(),  
     '#description' => t('Select the roles to assign new users.'),  
   );  
   $form['autoassignrole_settings_user'] = array(  
   '#type' => 'fieldset',  
   '#title' => t('User Role Assignment'),  
   '#collapsible' => TRUE,  
   '#collapsed' => TRUE,  
   );  
   $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_ACTIVE'] = array(  
     '#type' => 'radios',  
     '#title' => t('User Role Assignment'),  
     '#description' => t('The end user will be allowed to select the following roes when they log in.'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_ACTIVE',0),  
     '#options' => array(1 => t('Enabled'), 0 => t('Disabled')),  
   );  
   $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER'] = array(  
     '#type' => 'checkboxes',  
     '#title' => t('Role'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER',''),  
     '#options' => user_roles(),  
     '#description' => t('Select the roles that are offered to new users.'),  
   );  
   $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_MULTIPLE'] = array(  
     '#type' => 'radios',  
     '#title' => t('User Role Selection'),  
     '#description' => t('Should the end user be allowed to choose a single role or can they choose multiple roles?'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_MULTIPLE',0),  
     '#options' => array(0 => t('Single Role'), 1 => t('Multiple Roles')),  
   );  
   $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_REQUIRED'] = array(  
     '#type' => 'checkbox',  
     '#title' => t('Required'),  
     '#description' => t('Should the end user be required to choose a role?'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_REQUIRED',0),  
   );  
   $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_SORT'] = array(  
     '#type' => 'select',  
     '#title' => t('Sorting'),  
     '#description' => t('Default sort order of roles the user will see.'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_SORT','SORT_ASC'),  
     '#options' => array('SORT_ASC' => t('Ascending'), 'SORT_DESC' => t('Descending')),  
   );  
   $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_FIELDSET'] = array(  
     '#type' => 'textfield',  
     '#title' => t('User Role Fieldset Title'),  
     '#description' => t('The title of the fieldset that contains role options.'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_FIELDSET', 'User Role Selection'),  
     '#size' => 60,  
     '#maxlength' => 128,  
     '#required' => FALSE,  
   );  
   $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_TITLE'] = array(  
     '#type' => 'textfield',  
     '#title' => t('User Role Title'),  
     '#description' => t('The title of the field that contains the role options the end user sees during registration.'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_TITLE',""),  
     '#size' => 60,  
     '#maxlength' => 128,  
     '#required' => FALSE,  
   );  
   $form['autoassignrole_settings_user']['AUTOASSIGNROLE_ROLE_USER_DESCRIPTION'] = array(  
     '#type' => 'textarea',  
     '#title' => t('User Role Description'),  
     '#description' => t('The description displayed to the end user when they are selecting thier role during registration.'),  
     '#default_value' => variable_get('AUTOASSIGNROLE_ROLE_USER_DESCRIPTION',""),  
     '#required' => FALSE,  
28    );    );
29    return system_settings_form($form);    return $items;
30  }  }
31    
32  /**  /**
33   * Implementation of hook_perm().   * Implementation of hook_perm().
34   * @return array   * @return array
# Line 127  function autoassignrole_perm() { Line 40  function autoassignrole_perm() {
40  /**  /**
41   * Implementation of hook_user().   * Implementation of hook_user().
42   */   */
43  function autoassignrole_user($type, &$edit, &$user, $category = NULL) {  function autoassignrole_user($op, &$edit, &$account, $category = NULL) {
44    switch ($type) {    switch ($op) {
45      case 'insert':      case 'insert':
46        if(variable_get('AUTOASSIGNROLE_ROLE_ACTIVE','0') == 1) {        if (_autoassignrole_settings('auto_active') == 1) {
47          $roles = variable_get('AUTOASSIGNROLE_ROLE','0');          $edit['roles'] = _autoassignrole_settings('auto_roles');
48          if(is_array($roles)) {        }
49            $sql = 'INSERT INTO {users_roles} (uid, rid) values (%d, %d)';        if (_autoassignrole_settings('user_active') == 1) {
50            foreach($roles as $key => $value) {          $roles = array();
51              if($value > 0) {          $user_roles = _autoassignrole_settings('user_roles');
52                db_query($sql, $user->uid, $value);          foreach ($edit['user_roles'] as $k => $v) {
53              }            if (in_array($k, $user_roles)) {
54                $roles[$k] = $v;
55            }            }
56          }          }
57            $edit['roles'] = $roles;
58        }        }
59        if(variable_get('AUTOASSIGNROLE_ROLE_USER_ACTIVE','0') == 1) {      break;
60          $sql = 'INSERT INTO {users_roles} (uid, rid) values (%d, %d)';      case 'register':
61          if(is_array($edit['AUTOASSIGNROLE_ROLE_USER'])) {        if (_autoassignrole_settings('user_active') == 1) {
62            foreach($edit['AUTOASSIGNROLE_ROLE_USER'] as $key => $value) {          $form['autoassignrole_user'] = array(
63              if($value > 0) {            '#type' => 'fieldset',
64                db_query($sql, $user->uid, $value);            '#title' => _autoassignrole_settings('user_fieldset_title'),
65              }            '#collapsible' => FALSE,
66              '#collapsed' => FALSE,
67            );
68            $roles = user_roles(TRUE);
69            $user_roles = _autoassignrole_settings('user_roles');
70            foreach ($roles as $k => $r) {
71              if (!in_array($k, $user_roles)) {
72                unset($roles[$k]);
73            }            }
         } else {  
           db_query($sql, $user->uid, $edit['AUTOASSIGNROLE_ROLE_USER']);  
74          }          }
75        }          if (_autoassignrole_settings('user_sort') == "SORT_ASC") {
76       break;            array_multisort($roles, SORT_ASC);
77            }
78            else {
79              array_multisort($roles, SORT_DESC);
80            }
81    
82            if (!$edit || !array_key_exists('user_roles', $edit)) {
83              $edit['user_roles'] = array();
84            }
85            $form['autoassignrole_user']['user_roles'] = array(
86              '#type' => 'checkboxes',
87              '#title' => t('Roles'),
88              '#default_value' => $edit['user_roles'],
89              '#options' => $roles,
90              '#description' => _autoassignrole_settings('user_description'),
91            );
92            return $form;
93          }
94        break;
95      }
96    }
97    function _autoassignrole_settings($value) {
98      static $settings = array();
99      if (count($settings) == 0) {
100        $result = db_query("SELECT arid, value from {autoassignrole}");
101        while ($s = db_fetch_object($result)) {
102          $settings[$s->arid] = $s->value;
103        }
104        return _autoassignrole_settings($value);
105    }    }
106  }    else {
107        switch ($value) {
108  function autoassignrole_form_alter($form_id, &$form) {        case 'auto_roles':
109    if ($form_id == "user_register" && variable_get('AUTOASSIGNROLE_ROLE_USER_ACTIVE','0') == 1) {        case 'user_roles':
110      $form['autoassignrole_user'] = array(          $roles = $settings[$value];
111      '#type' => 'fieldset',          $roles = unserialize($roles);
112      '#title' => variable_get('AUTOASSIGNROLE_ROLE_USER_FIELDSET','User Role Selection'),          foreach ($roles as $k => $r) {
113      '#collapsible' => FALSE,            if ($r == 0) {
114      '#collapsed' => FALSE,              unset($roles[$k]);
115      );            }
116      $form['autoassignrole_user']['AUTOASSIGNROLE_ROLE_USER'] = array(          }
117        '#type' => _autoassignrole_user_input('type'),          $return = $roles;
118        '#title' => variable_get('AUTOASSIGNROLE_ROLE_USER_TITLE',""),          break;
119        '#options' => _autoassignrole_intersect(),        default:
120        '#required' => _autoassignrole_user_input('required'),          $return = $settings[$value];
121        '#description' => variable_get('AUTOASSIGNROLE_ROLE_USER_DESCRIPTION',""),          break;
122      );      }
123        return $return;
124    }    }
125  }  }
126    
127  function _autoassignrole_user_input($args) {  function _autoassignrole_user_input($args) {
128    switch ($args) {    switch ($args) {
129      case 'type':      case 'type':
130        if(variable_get('AUTOASSIGNROLE_ROLE_USER_MULTIPLE',0) == 0) {        if (_autoassignrole_settings('user_multiple') == 0) {
131          $type = 'radios';          $type = 'radios';
132        } else {        }
133          else {
134          $type = 'checkboxes';          $type = 'checkboxes';
135        }        }
136        return $type;        return $type;
137        break;        break;
138      case 'required':      case 'required':
139        if(variable_get('AUTOASSIGNROLE_ROLE_USER_REQUIRED',0) == 0) {        if (_autoassignrole_settings('user_required') == 0) {
140          $required = FALSE;          $required = FALSE;
141        } else {        }
142          else {
143          $required = TRUE;          $required = TRUE;
144        }        }
145        return $required;        return $required;
146        break;        break;
147    }    }
148  }  }
149    
150  function _autoassignrole_intersect() {  function _autoassignrole_intersect() {
151    $autoassignrole_roles = variable_get("AUTOASSIGNROLE_ROLE_USER","");    $result = _autoassignrole_array_intersect_key(
152    foreach($autoassignrole_roles as $key => $value) {      user_roles(TRUE),
153      if($value == 0) {      _autoassignrole_settings('user_roles')
154        unset($autoassignrole_roles[$key]);    );
155      }    drupal_set_message(print_r($result,true));
156      if (_autoassignrole_settings('user_sort') == "SORT_ASC") {
157        array_multisort($result, SORT_ASC);
158    }    }
159    $result = _autoassignrole_array_intersect_key(user_roles(), $autoassignrole_roles);    else {
160    if(variable_get("AUTOASSIGNROLE_ROLE_SORT", "SORT_ASC") == "SORT_ASC") {      array_multisort($result, SORT_DESC);
     array_multisort($result,SORT_ASC);  
   } else {  
         array_multisort($result,SORT_DESC);  
161    }    }
162    return $result;    return $result;
163  }  }
# Line 222  function _autoassignrole_array_intersect Line 173  function _autoassignrole_array_intersect
173        }        }
174      }      }
175      return $isec;      return $isec;
176    } else {    }
177      else {
178      $res = array();      $res = array();
179      foreach (array_keys($isec) as $key) {      foreach (array_keys($isec) as $key) {
180        if (isset($keys[$key])) {        if (isset($keys[$key])) {

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.3