'admin/settings/apply_for_role', 'title' => t('Apply for role administration'), 'description' => t('Administer which roles users can apply for.'), 'callback' => 'apply_for_role_settings', 'access' => user_access('administer apply for role'), ); $items[] = array( 'path' => 'admin/user/apply_for_role', 'title' => t('Manage role applications'), 'description' => t('View, approve and delete role applications.'), 'callback' => 'apply_for_role_approve', 'access' => user_access('approve role applications'), ); $items[] = array( 'path' => 'admin/user/apply_for_role/approve', 'title' => t('Approve role application'), 'callback' => 'drupal_get_form', 'callback arguments' => array('apply_for_role_manage_form'), 'access' => user_access('approve role applications'), 'type' => MENU-CALLBACK, ); $items[] = array( 'path' => 'admin/user/apply_for_role/remove', 'title' => t('Remove role application'), 'callback' => 'drupal_get_form', 'callback arguments' => array('apply_for_role_manage_form'), 'access' => user_access('approve role applications'), 'type' => MENU-CALLBACK, ); } else { if ($user->uid && arg(0) == 'user' && is_numeric(arg(1))) { $items[] = array( 'path' => 'user/'. $user->uid .'/apply_for_role', 'title' => t('Apply for role'), 'callback' => 'apply_for_role_page', 'access' => user_access('apply for roles'), 'type' => MENU_LOCAL_TASK, ); } } return $items; } /** * Administration */ function apply_for_role_settings() { return drupal_get_form('apply_for_role_settings_form'); } function apply_for_role_settings_form() { $selected_roles = variable_get('users_apply_roles', array()); foreach ((array)$selected_roles as $rid => $value) { if ($rid > 2) { $selected_rids[] = $rid; } } $form['options'] = array( '#type' => 'fieldset', '#title' => t('Apply for role options'), ); $form['options']['remove'] = array( '#type' => 'radios', '#title' => t('Allow users to unassign roles'), '#options' => array(t('No'), t('Yes')), '#default_value' => variable_get('apply_for_role_remove', 0), '#description' => t("Choosing 'yes' will allow users to unassign roles."), '#required' => TRUE, ); $form['options']['multiple'] = array( '#type' => 'radios', '#title' => t('Allow multiple roles per application'), '#options' => array(t('No'), t('Yes')), '#default_value' => variable_get('apply_for_role_multiple', 0), '#description' => t("Chosing 'no' will limit users to applying for only one role per role application. Choosing 'yes' will allow users to apply for multiple roles per role application."), '#required' => TRUE, ); $form['options']['register'] = array( '#type' => 'radios', '#title' => t('Allow users to apply for roles on registration'), '#options' => array(t('No'), t('Yes')), '#default_value' => variable_get('apply_for_role_register', 0), '#description' => t("Choosing 'yes' will allow users to apply for roles when creating a new account."), '#required' => TRUE, ); $form['options']['total'] = array( '#type' => 'textfield', '#title' => t('Total number of roles'), '#default_value' => variable_get('apply_for_role_allowed_roles', 1), '#description' => t("After this number of applied/assigned roles, user is no longer allowed to apply for roles."), '#required' => TRUE, ); $roles = (user_roles(TRUE)); unset($roles[DRUPAL_AUTHENTICATED_RID]); $form['roles'] = array( '#type' => 'checkboxes', '#title' => t('Roles'), '#default_value' => isset($selected_rids) ? $selected_rids : array(2), '#options' => $roles, '#description' => t('Select the roles users will be able to apply for.'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit') ); return $form; } function apply_for_role_settings_form_submit($form_id, $form) { $roles = user_roles(TRUE); foreach ((array)$form['roles'] as $key => $value) { if ($value) { $selected_roles[$value] = $roles[$value]; } } variable_set('users_apply_roles', $selected_roles); variable_set('apply_for_role_multiple', $form['multiple']); variable_set('apply_for_role_remove', $form['remove']); variable_set('apply_for_role_register', $form['register']); variable_set('apply_for_role_allowed_roles', $form['total']); drupal_set_message(t('Apply for role settings have been saved.')); return 'admin/settings/apply_for_role'; } /** * User management */ function apply_for_role_approve() { $header = array( array('data' => t('Username'), 'field' => 'u.name'), array('data' => t('Current Roles')), array('data' => t('Apply for role'), 'field' => 'rid'), array('data' => t('Apply Date'), 'field' => 'apply_date', 'sort' => 'desc'), array('data' => t('Approved'), 'field' => 'approved'), array('data' => t('Approve Date'), 'field' => 'approve_date'), array('data' => t('Delete')), ); $result = db_query("SELECT * FROM {users_roles_apply} a LEFT JOIN {users} u ON u.uid = a.uid ". tablesort_sql($header)); $roles = user_roles(1); $rows = array(); while ($row = db_fetch_object($result)) { $user = user_load(array('uid' => $row->uid)); foreach ($user->roles as $rid => $role) { $user_roles .= $role .'
'; } if ($row->approved == 0) { $active = '
'. l(t('Approve'), 'admin/user/apply_for_role/approve/'. $row->uid .'/'. $row->rid, array('title' => t('Approve this user'))) ."
\n"; } else { $active = '
'. t('Approved') ."
\n"; } $delete = '
'. l(t('Delete'), 'admin/user/apply_for_role/remove/'. $row->uid .'/'. $row->rid, array('title' => t('Remove application'))) ."
\n"; $rows[] = array( array('data' => '
'. l($row->name, 'user/'. $row->uid) ."
\n", 'class' => 'title'), array('data' => '
'. $user_roles ."
\n"), array('data' => $roles[$row->rid]), array('data' => format_date($row->apply_date)), array('data' => $active, 'class' => 'icon'), array('data' => (!empty($row->approve_date) ? format_date($row->approve_date) : '')), array('data' => $delete, 'class' => 'icon'), ); unset($user_roles); } if (count($rows) == 0) { $rows[] = array(array('data' => ''. t('There are currently no applications.') .'', 'colspan' => 7)); } $output .= theme('table', $header, $rows); return $output; } function apply_for_role_manage_form() { $uid = arg(4); $rid = arg(5); $form['uid'] = array('#type' => 'hidden', '#value' => $uid); $form['rid'] = array('#type' => 'hidden', '#value' => $rid); $user = user_load(array('uid' => $uid)); $roles = user_roles(1); switch (arg(3)) { case 'approve': return confirm_form($form, t('Do you want to approve the application from user !username for role !role ', array('!username' => $user->name, '!role' => $roles[$rid])), 'admin/settings/apply_for_role', t('The role will be automatically assigned to the user'), t('Approve')); break; case 'remove': return confirm_form($form, t('Do you want to remove the application from user !username for role !role ', array('!username' => $user->name, '!role' => $roles[$rid])), 'admin/settings/apply_for_role', t('The role will be automatically deleted from the user'), t('Delete')); break; } } function apply_for_role_manage_form_submit($form_id, $form_values) { switch (arg(3)) { case 'approve': if (apply_for_role_approve_apply($form_values['uid'], $form_values['rid'])) { drupal_set_message(t('The application was approved.')); } else { drupal_set_message(t('Error approving application. Please try again!'), 'error'); } break; case 'remove': if (apply_for_role_remove_apply($form_values['uid'], $form_values['rid'])) { drupal_set_message(t('The application was deleted.')); } else { drupal_set_message(t('Error deleting application. Please try again!'), 'error'); } break; } cache_clear_all(); return 'admin/user/apply_for_role'; }; /** * User interface */ function apply_for_role_page() { return drupal_get_form('apply_for_role_apply_form'); } function apply_for_role_apply_form() { global $user; $roles = variable_get('users_apply_roles', array()); $multiple = variable_get('apply_for_role_multiple', array()); $allowed_roles = variable_get('apply_for_role_allowed_roles', array()); $user_unassign = variable_get('apply_for_role_remove', array()); foreach ($roles as $rid => $role) { // Check if the user has this role or has applied for this role if (!$user->roles[$rid] && (db_result(db_query("SELECT uid, rid FROM {users_roles_apply} WHERE uid = %d AND rid = %d", $user->uid, $rid)) == 0)) { $apply_roles[$rid] = $role; } else { $remove_roles[$rid] = $role; } } //Is user allowed only one role? and has user applied for any roles/assigned any roles if ( count($remove_roles) < $allowed_roles ) { if (is_array($apply_roles)) { $form['apply'] = array( '#type' => 'fieldset', '#title' => t('Apply for roles'), ); if ($multiple == 1) { $form['apply']['apply_rid'] = array( '#type' => 'checkboxes', '#title' => t('Select the role or roles you want to apply for'), '#options' => $apply_roles, ); } else { $form['apply']['apply_rid'] = array( '#type' => 'radios', '#title' => t('Select the role you want to apply for'), '#default_value' => '', '#options' => $apply_roles, ); } } else { drupal_set_message(t('No roles are available at this time.'), 'notice'); } } else { drupal_set_message(t('You are only allowed %roles.', array('%roles' => format_plural($allowed_roles, '1 role', '@count roles'))),'notice'); } //Let the user unassign roles if (variable_get('apply_for_role_remove',0) == 1) { if ( count($remove_roles) > 0) { if (is_array($remove_roles)) { $form['remove'] = array( '#type' => 'fieldset', '#title' => t('Unassign roles'), ); $form['remove']['remove_rid'] = array( '#type' => 'checkboxes', '#title' => t('Select the role or roles you want to unassign'), '#options' => $remove_roles, ); } else { drupal_set_message(t('No roles to unassign at this time.'), 'notice'); } } else { drupal_set_message(t('You need some assigned roles before you can unassign.'),'notice'); } } $form['submit'] = array( '#type' => 'submit', '#value' => t('Apply') ); return $form; } function apply_for_role_apply_form_submit($form_id, $form_values) { global $user; $received = array(); $not_received = array(); if (is_array($form_values['apply_rid'])) { foreach ($form_values['apply_rid'] as $rid => $value) { if (!empty($value)) { if (apply_for_role_add_apply($user->uid, $value)) { $received[] = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $value)); } else { $not_received[] = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $value)); } } } } else { if (apply_for_role_add_apply($user->uid, $form_values['apply_rid'])) { $received[] = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $form_values['rid'])); } else { $not_received[] = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $form_values['rid'])); } } $removed = 0; if (is_array($form_values['remove_rid'])) { foreach ($form_values['remove_rid'] as $rid => $value) { if (!empty($value)) { if (apply_for_role_remove_role($user->uid, $value)) { $removed++; } } } } else { if (apply_for_role_remove_role($user->uid, $value) && $value != NULL) { $removed++; } } $count_received = count($received); $count_not_received = count($not_received); if (!empty($count_received)) { drupal_set_message(t('%message %roles', array('%message' => format_plural($count_received, t('Your application was received for the following role:'), t('Your applications were received for the following roles:')), '%roles' => implode(', ', $received)))); } if (!empty($count_not_received)) { drupal_set_message(t('%message %roles', array('%message' => format_plural($count_not_received, t('There was a problem processing your application for the following role:'), t('There was a problem processing your applications for the following roles:')), '%roles' => implode(', ', $not_received))), 'error'); } if ($removed > 0) { drupal_set_message(t('%roles', array('%roles' => format_plural($allowed_roles, '1 role removed', '@count roles removed'))),'notice'); } return 'user/'. $user->uid; } function theme_apply_for_role_apply_form($form) { $output = drupal_render($form); return $output; } /** * Implementation of hook_user(). */ function apply_for_role_user($op, &$edit, &$user, $category = NULL) { switch ($op) { case 'register': // Admin created account aren't processed by the module. if (user_access('administer users')) { break; } if (variable_get('apply_for_role_register', array())) { $roles = variable_get('users_apply_roles', array()); $multiple = variable_get('apply_for_role_multiple', array()); foreach ($roles as $rid => $role) { if ($rid > 2) { $filter_roles[$rid] = $role; } } $form['apply_for_role'] = array( '#type' => 'fieldset', '#title' => t('Apply for roles'), '#collapsible' => FALSE, ); if (is_array($filter_roles)) { if ($multiple == 1) { $form['apply_for_role']['rid'] = array( '#type' => 'checkboxes', '#title' => t('Select the role or roles you want to apply for'), '#options' => $filter_roles, ); } else { $filter_roles[0] = t(''); ksort($filter_roles); $form['apply_for_role']['rid'] = array( '#type' => 'select', '#title' => t('Select the role you want to apply for'), '#default_value' => '', '#options' => $filter_roles, ); } } return $form; } break; case 'insert': if (variable_get('apply_for_role_register', array())) { if (is_array($edit['rid'])) { foreach ($edit['rid'] as $rid => $value) { if (!empty($value)) { if (apply_for_role_add_apply($user->uid, $value)) { $received[] = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $value)); } else { $not_received[] = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $value)); } } } } else { if (!empty($edit['rid'])) { if (apply_for_role_add_apply($user->uid, $edit['rid'])) { $received[] = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $edit['rid'])); } else { $not_received[] = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $edit['rid'])); } } } $count_received = count($received); $count_not_received = count($not_received); if (!empty($count_received)) { drupal_set_message(t('%message %roles', array('%message' => format_plural($count_received, t('Your application was received for the following role:'), t('Your applications were received for the following roles:')), '%roles' => implode(', ', $received)))); } if (!empty($count_not_received)) { drupal_set_message(t('%message %roles', array('%message' => format_plural($count_not_received, t('There was a problem processing your application for the following role:'), t('There was a problem processing your applications for the following roles:')), '%roles' => implode(', ', $not_received))), 'error'); } } break; case 'delete': db_query("DELETE FROM {users_roles_apply} WHERE uid = %d", $user->uid); } } /** * Callbacks */ function apply_for_role_check_role_exist($uid, $rid) { if ($uid) { $user = user_load(array('uid' => $uid)); if ($user->uid) { if ($user->roles[$rid]) { return TRUE; } } } return FALSE; } function apply_for_role_add_apply($uid, $rid) { if (!apply_for_role_check_role_exist($uid, $rid)) { // Check if already applyed for this role $res = db_query("SELECT uid, rid FROM {users_roles_apply} WHERE uid = %d AND rid = %d", $uid, $rid); if (db_num_rows($res) == 0) { db_query("INSERT INTO {users_roles_apply} (uid, rid, approved, apply_date) VALUES (%d, %d, 0, %d)", $uid, $rid, time()); return TRUE; } } } function apply_for_role_approve_apply($uid, $rid) { if (!apply_for_role_check_role_exist($uid, $rid)) { $res = db_query("SELECT uid, rid, approved FROM {users_roles_apply} WHERE uid = %d AND rid = %d", $uid, $rid); if (db_num_rows($res)) { $row = db_fetch_object($res); if ($row->approved == 0) { apply_for_role_add_role($uid, $rid); db_query("UPDATE {users_roles_apply} SET approved = 1, approve_date = %d WHERE uid = %d AND rid = %d", time(), $uid, $rid); return TRUE; } } } } function apply_for_role_remove_apply($uid, $rid) { $res = db_query("SELECT uid, rid, approved FROM {users_roles_apply} WHERE uid = %d AND rid = %d", $uid, $rid); if (db_num_rows($res)) { apply_for_role_delete_role($uid, $rid); db_query("DELETE FROM {users_roles_apply} WHERE uid = %d AND rid = %d", $uid, $rid); return TRUE; } } function apply_for_role_add_role($uid, $rid) { if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) { db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $uid, $rid); } } function apply_for_role_remove_role($uid, $rid) { if (apply_for_role_check_role_exist($uid,$rid)) { if (apply_for_role_delete_role($uid, $rid)) { return TRUE; } } else { if (apply_for_role_remove_apply($uid, $rid)) { return TRUE; } } } function apply_for_role_delete_role($uid, $rid) { if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) { db_query('DELETE FROM {users_roles} WHERE uid = %d AND rid = %d', $uid, $rid); } }