rolesignup test code for D6

  1. <?php
  2. /* $Id: rolesignup.module,v 1.2.4.1 2007/01/20 21:49:48 sym Exp $ */
  3.  
  4. /********************************************************************
  5. * Drupal Hooks
  6. ********************************************************************/
  7. /**
  8.  * Implementation of module_enable()
  9.  */
  10. function rolesignup_enable() {
  11.         cache_clear_all();
  12.         menu_cache_clear();
  13.         menu_rebuild();
  14. }
  15.  
  16. /**
  17.  * Implementation of module_disable()
  18.  */
  19. function rolesignup_disable() {
  20.         cache_clear_all();
  21.         menu_cache_clear();
  22.         menu_rebuild();
  23. }
  24.  
  25. /**
  26.  * Implementation of hook_menu()
  27.  */
  28. function rolesignup_menu() {
  29.   return array(
  30.     'user/register/role/%' => array(
  31.         'page callback' => 'role_register_page',
  32.         'page arguments' => array(3),
  33.         'title' => t('Register'),
  34.         'type' => MENU_LOCAL_TASK
  35.     )
  36.   );
  37. }
  38.  
  39. /**
  40.  * Implementation of hook_perm()
  41.  */
  42. function rolesignup_perm() {
  43.   return array('register for role');
  44. }
  45.  
  46. /**
  47.  * Implementation of hook_form_alter()
  48.  */
  49. //function rolesignup_form_alter(&$form, $form_state, $form_id) {
  50. //  if ($form_id == "user_register") {
  51. //    global $user;
  52. //    if (!user_access('administer users') && !$_SESSION['role']) {
  53. //      drupal_goto('user/register/role', drupal_get_destination());
  54. //    } else {
  55. //      $form['role'] = array(
  56. //        '#type' => 'value',
  57. //        '#value' => $_SESSION['role'],
  58. //        );
  59. //    }
  60. //    $form['rolelink'] = array(
  61. //      '#value' => theme('select_role'),
  62. //      '#weight' => -11,      
  63. //      );
  64. //  }
  65. //}
  66.  
  67. function role_register_page($string = NULL) {
  68.   $roles = user_roles(1, 'register for role');
  69.   drupal_set_title("Please select your Role below");
  70.   return drupal_get_form('rolelist_form', $roles, $string);
  71. }
  72.  
  73. /**
  74.  * Implementation of hook_user()
  75.  */
  76. function rolesignup_user($op, &$edit, &$account, $category = NULL) {
  77.   switch ($op) {
  78.     case 'insert':
  79.     $roles = user_roles(1,'register for role');
  80.     if ($roles[$_SESSION['role']] && !in_array($_SESSION['role'], array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
  81.       drupal_set_message($account->uid);
  82.       drupal_set_message($_SESSION['role']);
  83.       db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $_SESSION['role']);
  84.     }
  85.     break;
  86.     case 'register':
  87.             if(!$category || $category == "account") {
  88.                         if (!$_SESSION['role']) {
  89.                                 drupal_goto('user/register/role/invalid', drupal_get_destination());
  90.                         }
  91.                         $roles = user_roles();
  92.                         $rolename = $roles[$_SESSION['role']];
  93.                         $form = array();
  94.                         $form["#prefix"] = "<p class='messages role_select'>You are registering for the '".ucwords(strtolower($rolename))."' Role.<br/></p>";
  95.                         $form["back"] = array(
  96.                                 "#type" => "submit"
  97.                                 ,"#submit" => array("rolesignup_goto_role_selection")
  98.                                 ,"#value" => "Change Role"
  99.                         );
  100.                         return $form;
  101.             }
  102.     break;
  103.   }
  104. }
  105.  
  106. function rolesignup_goto_role_selection($form, &$form_state) {
  107.         $url = 'user/register/role/'.$_SESSION['role'];
  108.         unset($_SESSION['role']);
  109.         drupal_goto($url, drupal_get_destination());
  110. }
  111.  
  112. function rolelist_form($form_state = NULL, $roles, $string) {
  113.   $rids = array_keys($roles);
  114.   if(in_array($string, $rids)) {
  115.         $rid = $string;
  116.   } else {
  117.         $rid = $rids[0];
  118.   }
  119.   $form = array();
  120.   $form["role"] = array(
  121.         "#type" => "select"
  122.         ,"#options" => $roles
  123.         ,"#default_value" => $rid
  124.         ,"#description" => "roles define the behaviour of your account."
  125.         ,"#required" => TRUE
  126.   );
  127.   $form["next"] = array(
  128.         "#type" => "submit"
  129.         ,"#value" => "Next"
  130.   );
  131.   return $form;
  132. }
  133.  
  134. function rolelist_form_submit($form, &$form_state) {
  135.         $_SESSION['role'] = $form_state["values"]["role"];
  136.         drupal_goto('user/register');
  137. }