--- og_user_roles.module.5.x-2.7.orig	2008-01-11 00:03:46.000000000 -0800
+++ og_user_roles.module	2008-03-11 00:30:39.000000000 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: og_user_roles.module,v 1.1.2.11 2008/01/10 20:01:19 somebodysysop Exp $
+// $Id: og_user_roles.module,v 1.1.2.3 2007/07/08 19:50:44 somebodysysop Exp $
 
 /**
  * @file
@@ -24,7 +24,7 @@ function og_user_roles_help($section) {
  *
  */
 function og_user_roles_perm() {
-  return array('administer og_user_roles', 'configure member roles', 'create og_subgroups');
+  return array('administer og_user_roles', 'configure member roles', 'create og_subgroups', 'manage registration codes', 'use registration codes');
 }
 
 /**
@@ -79,8 +79,34 @@ function og_user_roles_admin_settings() 
       '#options' => $roles,
       '#default_value' => variable_get("og_user_roles_roles_$type", array()),
     );
+    $form["og_user_roles_$type"]["og_user_roles_assign_typegrouprole_$type"] = array(
+      '#type' => 'checkbox',
+      '#title' => t('<strong>Set default basic group (group limited) role for users who join groups of this type: %type_name?</strong>', array('%type_name' => $name)),
+      '#default_value' => variable_get("og_user_roles_assign_typegrouprole_$type", 0),
+      '#description' => t('Do you wish to automatically assign a specific "basic group role" to <b>every new group subscriber</b> at the time he subscribes to a group of this type: %type_name? The role is limited to the group that he is subscribed to. This role assignment can be removed by the groups\' admins', array('%type_name' => $name)),
+    );
+    $form["og_user_roles_$type"]["og_user_roles_typegrouprole_value_$type"] = array(
+      '#type' => 'select',
+      '#title' => t('Role to use as a group type %type_name role', array('%type_name' => $name)),
+      '#options' => $roles,
+      '#default_value' => variable_get("og_user_roles_typegrouprole_value_$type", 0),
+      '#description' => t('Select the role you wish to use as the "group role" for every new groupmember to groups of this type: %type_name.', array('%type_name' => $name)),
+    );
   }
 
+  $form['og_user_roles_regcode_gid_default'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Allow Group Admins to define Registration Codes for new group subscribers to moderated groups.'),
+    '#description' => t('Allows you to allow Group Administrators to define Registration Codes to allow users to subscribe to moderated groups without administrator approval.  Your Group Admins must have the <b>manage registration codes</b> permission to use this setting.'),
+    '#collapsible' => TRUE,
+  );
+  $form['og_user_roles_regcode_gid_default']['og_user_roles_assign_regcode_gid'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Allow Group Admins to set Registration Codes for users to join their moderated groups without admin approval?'),
+    '#default_value' => variable_get('og_user_roles_assign_regcode_gid', 0),
+    '#description' => t('Do you wish to allow Group Admins to define "Registration Codes" to allow users to subscribe to their groups without admin approval? The Group Admin will be able to define the Registration Codes on <b>edit</b> (not creation) of the group node.  Group Admin must have <b>manage registration codes</b> permission.'),
+  );
+
   $form['og_user_roles_approval'] = array(
     '#type' => 'fieldset',
     '#title' => t('Allow group admins to approve new signups.'),
@@ -123,7 +149,7 @@ function og_user_roles_admin_settings() 
     '#type' => 'checkbox',
     '#title' => t('Set default basic group (group limited) role for users who join groups?'),
     '#default_value' => variable_get('og_user_roles_assign_basicgrouprole', 0),
-    '#description' => t('Do you wish to automatically assign a specific "basic group role" to <b>every new group subscriber</b> at the time he subscribes to the group? The role is limited to the group that he is subscribed to. This role assignment can be be removed by the groups\' admins'),
+    '#description' => t('Do you wish to automatically assign a specific "basic group role" to <b>every new group subscriber</b> at the time he subscribes to the group? The role is limited to the group that he is subscribed to. This role assignment can be removed by the groups\' admins'),
   );
   $form['og_user_roles_basicgrouprole']['og_user_roles_basicgrouprole_value'] = array(
     '#type' => 'select',
@@ -166,6 +192,26 @@ function og_user_roles_admin_settings() 
     '#description' => t('Select the role you wish to use as the "founder" role.'),
   );
 
+  $form['og_user_roles_admingrouprole'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Default Group Role for new group administrator.'),
+    '#description' => t('Allows you to select a group role to automatically assign to users who are elevated to group administrator. The role is specific to the group(s) in which the user is a group administrator.  That is, the user will only have the privileges of the role in the group he is the administrator for.'),
+    '#collapsible' => TRUE,
+  );
+  $form['og_user_roles_admingrouprole']['og_user_roles_assign_admingrouprole'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Set default group (group limited) administrator role for users who are elevated to group administrator?'),
+    '#default_value' => variable_get('og_user_roles_assign_admingrouprole', 0),
+    '#description' => t('Do you wish to automatically assign a specific "administrator group role" to <b>every new group administrator</b> at the time he is elevated to group administrator status? The role is limited to the group that he is the administrator for. This role assignment can only be removed by removing the user as a group administrator.'),
+  );
+  $form['og_user_roles_admingrouprole']['og_user_roles_admingrouprole_value'] = array(
+    '#type' => 'select',
+    '#title' => t('Role to use as group administrator role'),
+    '#options' => $roles,
+    '#default_value' => variable_get('og_user_roles_admingrouprole_value', 0),
+    '#description' => t('Select the role you wish to use as the "group administrator role" for every new group administrator.'),
+  );
+
   $form['og_user_roles_notify'] = array(
     '#type' => 'fieldset',
     '#title' => t('Default Group Admin Notification for new subscribers.'),
@@ -324,6 +370,15 @@ function og_user_roles_menu($may_cache) 
       'access' => $user->uid,
       'type' => MENU_CALLBACK
     );
+    $items[] = array(
+      'path' => 'og/regcode',
+      'title' => t('Enter registration code'),
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('og_user_roles_register'),
+      'access' => user_access('use registration codes'),
+      'weight' => 10,
+      'type' => MENU_LOCAL_TASK,
+     );  
   }
   else {
     // modr8 modification
@@ -569,6 +624,13 @@ function og_user_roles_role_remove($gid)
 }
 
 /**
+ * Remove this role from this user in this group.
+ */
+function og_user_roles_role_delete($uid, $rid, $gid) {
+  db_query("DELETE FROM {og_users_roles} WHERE uid = %d AND gid = %d AND rid = %d", $uid, $gid, $rid);
+}
+
+/**
  * Remove og_user_roles variables for a group node that has been deleted.
  */
 function og_user_roles_variable_remove($gid) {
@@ -800,7 +862,7 @@ function og_user_roles_init() {
       // Modification to make it work with module "Relativity"
       // http://drupal.org/node/166253
       if (module_exists('relativity')) {
-      if (arg(3) == 'parent' && is_numeric(arg(4))) {
+        if (arg(3) == 'parent' && is_numeric(arg(4))) {
           $query .= '&parent_node='.arg(4);
         }
         elseif (!empty($_GET['parent_node'])) {
@@ -1029,6 +1091,36 @@ function og_user_roles_init() {
           drupal_goto($path);
       }
     }
+
+    // If this is a group relativity or book module node/add, re-direct to ognodeadd
+    if (arg(0) == 'node' && arg(1) == 'add'  && is_numeric(arg(4)) && (!in_array(arg(2), $exempted)) ) {
+      
+      $type = arg(2);
+      $path = 'node/ognodeadd';
+
+      // Get the gid for this parent
+      $nid = (int)arg(4);
+      $gid = og_user_roles_getgid($nid, $user->uid);
+
+      $query = 'type='. $type .'&gids[]='. $gid;
+
+      // Modification to make it work with module "Relativity"
+      // http://drupal.org/node/166253
+      // http://drupal.org/node/227978
+      if (module_exists('relativity')) {
+        if (arg(3) == 'parent') {
+          $query .= '&parent_node='.arg(4);
+        }
+        elseif (!empty($_GET['parent_node'])) {
+          $query .= '&parent_node='.$_GET['parent_node'];
+        }
+      }
+      // Book module as per: http://drupal.org/node/228386
+      if (module_exists('book') && $type == 'book' && arg(3) == 'parent' && is_numeric(arg(4))) {
+        $path .= '/book/parent/'. arg(4);
+      }
+       drupal_goto($path, $query);
+    }
   }
 }
 
@@ -1041,6 +1133,26 @@ function og_user_roles_init() {
 function og_user_roles_user($op, &$edit, &$user, $category = NULL) {
 
   if ($op == 'insert') {
+    // Registration Code 
+    if (isset($edit['og_user_roles_regcode']) && variable_get('og_user_roles_assign_regcode_gid', 0) == 1) { //only present during registration
+      $gid = og_user_roles_gid_from_regcode($edit['og_user_roles_regcode']);
+      if ($gid > 0) {
+        $node = node_load($gid);
+        $title = $node->title;
+        $return = og_save_subscription($gid, $user->uid, array('is_active' => 1)); // as per http://drupal.org/node/156224
+        drupal_set_message(t('Subscription request to ' . l($title, 'node/'. $gid) . ' approved.'));	
+        // Delete this registration code if 'delete_regcode' is set
+        if (variable_get('og_user_roles_delete_regcode_' . $gid, 0) == 1) og_user_roles_delete_regcode($gid, $edit['og_user_roles_regcode']);
+        // Places all new registration code group subscribers into default regcode role.
+        if (variable_get('og_user_roles_assign_regcoderole_' . $gid, 0)) { // check to see if this variable exists
+          if (variable_get('og_user_roles_assign_regcode_gid', 0) == 1 && variable_get('og_user_roles_assign_regcoderole_' . $gid, 0) == 1 ) {
+            $rid = variable_get('og_user_roles_regcoderole_value_' . $gid, 0);
+            og_user_roles_role_join($user->uid, $rid, $gid); // assign user to group role in that group
+          }
+        }
+      }
+    }
+
     // If site requires admin approval and group admins are allowed to approve users
 	// Variable "user_register" values: 0 = Admin Only, 1 = No Admin Approval, 2 = Admin Approval
     if (variable_get('og_user_roles_approval_default', 0) == 1 && (variable_get('user_register', 1) == 2 || ($user->status == 0))) {
@@ -1086,6 +1198,18 @@ function og_user_roles_user($op, &$edit,
     }
   }
 
+  if ($op == 'validate') {
+    // Registration Code
+    if (isset($edit['og_user_roles_regcode'])) { //only present during registration
+      if (variable_get('og_user_roles_assign_regcode_gid', 0) == 1 && $edit['og_user_roles_regcode'] != '') {
+        $gid = og_user_roles_gid_from_regcode($edit['og_user_roles_regcode']);
+        if ($gid == 0) {
+          form_set_error('og_user_roles_regcode', "Not a valid group registration code.  If you do not have a registration code, please make sure the Group registration code box is empty.");
+        }
+      }
+    }
+  }
+
   // Add the group roles to $user->roles if this is a group
   // This should only be effective until the next global $user call
   if ($op == 'load') {
@@ -1133,9 +1257,28 @@ function og_user_roles_og($op, $nid, $ui
           og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
         }
       }
+
+      // --- Modification - 2008-01-10 - as per: http://drupal.org/node/197489
+      // Places all new group administrators into default group administrator role.
+      $admin = $args['is_admin'];
+      if (variable_get('og_user_roles_assign_admingrouprole', 0) == 1 && $admin == 1) {
+        $rid = variable_get('og_user_roles_admingrouprole_value', 0);
+        og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
+      }
+
       $user = user_load(array('uid' => $uid));
       $node = node_load($nid);
 
+      // --- Modification - 2008-03-10 - as per: http://drupal.org/node/229715
+      // Places all new group subscribers into default role per group type.
+      $type = $node->type;
+	  if (variable_get('og_user_roles_assign_typegrouprole_' . $type, 0)) {
+	    if (variable_get('og_user_roles_assign_typegrouprole_' . $type, 0) == 1) {
+          $rid = variable_get('og_user_roles_typegrouprole_value_' . $type, 0);
+          og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
+        }
+      }
+
       // Send notification of new og subscription to administrators of group;
       if (variable_get('og_user_roles_notify_default', 0) == 1 && module_exists('mimemail')) {
         $group = check_plain($node->title);
@@ -1160,6 +1303,20 @@ function og_user_roles_og($op, $nid, $ui
       } // end if
       break;
 
+    case 'user update':
+      // --- Modification - 2008-01-10 - as per: http://drupal.org/node/197489
+      // Places all new group administrators into default group administrator role.
+      $admin = $args['is_admin'];
+      if (variable_get('og_user_roles_assign_admingrouprole', 0) == 1 && $admin == 1) {
+        $rid = variable_get('og_user_roles_admingrouprole_value', 0);
+        og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
+      }
+      if (variable_get('og_user_roles_assign_admingrouprole', 0) == 1 && $admin == 0) {
+        $rid = variable_get('og_user_roles_admingrouprole_value', 0);
+        og_user_roles_role_delete($uid, $rid, $nid); // delete this admin role for this user in this group (if it exists)
+      }
+      break;
+
     case 'user delete':
       // Remove all roles for this user in this group if he is being unsubscribed;
       og_user_roles_role_leave($uid, $nid);
@@ -1284,6 +1441,61 @@ function og_user_roles_nodeapi(&$node, $
 
       break;
 
+    case 'validate':
+      // Check to see if this is a group post, and if so, if public posts are allowed;
+        // Do we have group context?
+		if ($group_node = og_get_group_context()) {
+		  // Get the group node id
+		  $gid = $group_node->nid;
+		  // Check the nopublic variable for this group
+          if (variable_get('og_user_roles_assign_nopublic_' . $gid, 0) == 1) { 
+		    // Check public status of this post
+		    if ($node->og_public) { 
+			  form_set_error('og_public', t('No public posts allowed in this group.  You may NOT check the <strong>Public</strong> box on this post.'));
+			}
+			// Check forum status of this post
+			if (module_exists('og_forum')) {
+			  if ($node->type == 'forum') {
+                // Get the forum vocabulary ID
+                $vid = _forum_get_vid();
+                $ok = FALSE;
+				// Get all forums for this group
+				  $sql = "SELECT tid FROM {og_term} WHERE nid = %d";
+                  $forums = array();
+                  $result = db_query($sql, $gid);
+                  while ($nid = db_fetch_array($result)){
+                    $forums[] = $nid['tid'];
+                  }
+                // Get the forum containers only for this group
+				$containers = variable_get('forum_containers', array());
+				$my_containers = array_intersect($containers, $forums);
+				// Get the list of names for forum containers for this group
+				$names = "";
+                foreach ($my_containers as $item) {
+                  $temp = taxonomy_get_term($item);
+                  $names .= $temp->name . "  ";
+                }
+                // Get the vocabulary terms for this node
+                foreach ($node->taxonomy as $term) {
+                  // Check to make sure this term is in the forum vocabulary
+                  if (db_result(db_query('SELECT COUNT(*) FROM {term_data} WHERE tid = %d AND vid = %d', $term, $vid))) {
+                    // Make sure this term is in the same group
+                    if ($gid == og_user_roles_gid_from_tid($term)) {
+                      $tid = $term;
+        			  $ok = TRUE;
+                    }
+                  }
+                }
+			    // Check the gid of this tid against this gid
+			    if (!$ok) {
+                  form_set_error('tid', t('You have selected the wrong <strong>Forum</strong> for this post.  You may only select a forum from the <strong>Forums</strong> pull-down menu that is in the same group as this post.  Please try again. Group ('.$gid.') Term ('.$term.') Suggested container(s): %names', array('%names' => $names)));
+				}
+			  }
+			}
+		  }
+		}
+      break;
+
     case 'delete':
       // Remove og_users_roles and variables related to group node if it is deleted;
         // If this is a group node
@@ -1393,12 +1605,19 @@ function og_user_roles_all_roles($user) 
     // Modified 2007-09-14 for "email";
     // Modified 2007-12-20 for "revisions": http://drupal.org/node/202196
     // Modified 2008-01-10 for "workflow": http://drupal.org/node/208363
-    if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'outline' || arg(2) == 'track' || arg(2) == 'results' || arg(2) == 'access' || arg(2) == 'modr8' || arg(2) == 'ogmodr8' || arg(2) == 'delete' || arg(2) == 'galleries' || arg(2) == 'email' || arg(2) == 'revisions' || arg(2) == 'signups' || arg(2) == 'agenda' || arg(2) == 'workflow')) {
+    if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'outline' || arg(2) == 'track' || arg(2) == 'results' || arg(2) == 'access' || arg(2) == 'modr8' || arg(2) == 'ogmodr8' || arg(2) == 'delete' || arg(2) == 'galleries' || arg(2) == 'email' || arg(2) == 'revisions' || arg(2) == 'signups' || arg(2) == 'agenda' || arg(2) == 'workflow' || arg(2) == 'votes')) {
       $location = 2;
       $nid = (int)arg(1);	
       $gid = og_user_roles_getgid($nid, $uid);
     }
-
+
+    // poll.module
+    if (arg(0) == 'poll' && is_numeric(arg(2)) && (arg(1) == 'cancel')) {
+      $location = 20;
+      $nid = (int)arg(2);	
+      $gid = og_user_roles_getgid($nid, $uid);
+    }
+
     // Edit users (og_user_roles_approval)
 	//
 	// This will get a group context if the current user and the
@@ -1664,6 +1883,17 @@ function og_user_roles_all_roles($user) 
       if ($_SESSION['og_last']) $gid = $_SESSION['og_last']->nid;
 	}
 
+    // IMCE 
+	if (arg(0) == 'imce' && (arg(1) == 'browse' || arg(1) == 'upload')) {
+      $location = 160;
+      if ($_SESSION['og_last']) {
+        $gid = $_SESSION['og_last']->nid;
+        // Set group context.
+        $group_node = node_load($gid);
+        og_set_group_context($group_node);
+	  }
+	}
+
     // Agenda module
     // http://doadance.scbbs.com/node/agenda/list/279
     if (arg(0) == 'node' && arg(1) == 'agenda' && arg(2) == 'list' && is_numeric(arg(3)) ) {
@@ -1691,6 +1921,15 @@ function og_user_roles_all_roles($user) 
       }
     } 
 
+    // Book module
+	// As per: http://drupal.org/node/228386
+    // http://doadance.scbbs.com/book/export/html/279
+    if (arg(0) == 'book' && arg(1) == 'export' && arg(2) == 'html' && is_numeric(arg(3)) ) {
+      $location = 19;
+      $nid = (int)arg(3);
+      $gid = og_user_roles_getgid($nid, $uid);
+    }
+
    // Modification. As per: http://drupal.org/node/176390
    // Present a hook for other modules: hook_og_user_roles_gid()  
    if ($results = module_invoke_all('og_user_roles_gid')) {
@@ -2262,6 +2501,26 @@ function og_user_roles_gid_from_referrer
   return $gid;
 }
 
+/**
+ * Get gid from registration code
+ */
+function og_user_roles_gid_from_regcode($regcode) {
+  $gid = 0;
+  // Get the list of OG groups
+  $groups = og_all_groups_options();
+  foreach ($groups as $group => $title) {
+    // Only process groups that are allowed to have registration codes
+    if (variable_get('og_user_roles_assign_regcode_' . $group, 0) == 1) {
+      // Get the array of codes for this group.
+      $group_array = explode(',', variable_get('og_user_roles_regcode_value_' . $group, ''));
+      if (in_array($regcode, $group_array)) {
+        $gid = $group;
+      }
+    }
+  }
+  return $gid;
+}
+
 /**
  * This module assumes that you have content_access and acl modules installed
  *
@@ -2642,6 +2901,20 @@ function og_user_roles_checkview($viewna
  * group.
  */
 function og_user_roles_form_alter($form_id, &$form) {
+  // If this is a registration form, put the Group Register box on it if appropriate.
+  // Anonymous users need 'use registration codes' permission
+  // 'Allow Registration Codes' in OGR settings must be checked on.
+  if ($form_id == 'user_register') {
+    if (!isset($form['og_register'])) {
+      $form['og_register'] = array('#type' => 'fieldset', '#title' => t('Groups'));
+    }
+    $form['og_register']['og_user_roles_regcode'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Group registration code'),
+      '#access' => user_access('use registration codes'),
+      '#description' => t('If you received a group registration code, enter it here.'),
+    );
+  }
   // If this is a group node edit (use og_last to determine if this is a group node) and
   // the user has the 'configure member roles' permission, and we allow group admins
   // to set default group roles, then print the 'basicgrouprole' form on the
@@ -2686,6 +2959,95 @@ function og_user_roles_form_alter($form_
     }
   }
 
+  // If this is a group node edit (use og_last to determine if this is a group node) 
+  if (arg(0) == 'node' && ($_SESSION['og_last']->nid == arg(1)) && arg(2) == 'edit' ) {
+    $gid = arg(1);
+
+    // First, we need to get a list of all og-enabled node types
+    $group_types = variable_get('og_node_types', array('og'));
+    foreach ($group_types as $type) {
+      // Get list of assignable group roles for this group type
+      $role_ids = variable_get("og_user_roles_roles_{$type}", array());
+      $all_roles = user_roles();
+      foreach ($role_ids as $rid => $checked) {
+        if ($checked != 0) {
+          $roles[$rid] = $all_roles[$rid];
+        }
+      }
+      
+      // If the form loaded is this type then we can move on
+      if ($type != '' && $form_id == $type . "_node_form")  {
+
+        // Add the code to disallow public posts to the
+        // group edit form.
+
+          $form['og_user_roles_nopublic_gid'] = array(
+            '#type' => 'fieldset',
+            '#title' => t('Disallow public posts in this group.'),
+            '#description' => t('Allows you to prevent any <strong>Public</strong> posts from this group.  That is, no post made in this group may have the <strong>Public</strong> box checked.  In addition, if OG Forums is installed and this is a group <strong>Forum topic</strong> post, then the forum group selected in the <strong>Forums</strong> pull-down menu on the edit form must match this group, or the post will be rejected.'),
+            '#collapsible' => TRUE,
+            );
+          $form['og_user_roles_nopublic_gid']['og_user_roles_assign_nopublic_' . $gid] = array(
+            '#type' => 'checkbox',
+            '#title' => t('Do not allow public posts in this group?'),
+            '#default_value' => variable_get('og_user_roles_assign_nopublic_' . $gid, 0),
+            '#description' => t('Do you wish to disallow public postings from this group?'),
+            );
+          $form['#submit'] += array('og_user_roles_nopublic_form_submit' => array($form_id, &$form));          
+
+        // If the user has the 'manage registration codes' permission, and we allow group admins
+        // to set registration codes, then print the 'regcode' form on the
+        // group edit form.
+        if (user_access('manage registration codes') && variable_get('og_user_roles_assign_regcode_gid', 0) == 1) {
+
+          $form['og_user_roles_regcode_gid'] = array(
+            '#type' => 'fieldset',
+            '#title' => t('Registration code for new subscribers to this group.'),
+            '#description' => t('Allows you to enter registration codes that can be used for new users to automatically subscribe to this group and bypass moderation. This will only work if this group is in <b>moderation</b> status.  You must have the <b>manage registration codes</b> permission to use this setting.'),
+            '#collapsible' => TRUE,
+            );
+          $form['og_user_roles_regcode_gid']['og_user_roles_assign_regcode_' . $gid] = array(
+            '#type' => 'checkbox',
+            '#title' => t('Set registration codes to allow users to join this group?'),
+            '#default_value' => variable_get('og_user_roles_assign_regcode_' . $gid, 0),
+            '#description' => t('Do you wish to enter registration codes that will allow new subscribers to bypass admin approval and join this group?'),
+            );
+          $form['og_user_roles_regcode_gid']['og_user_roles_delete_regcode_' . $gid] = array(
+            '#type' => 'checkbox',
+            '#title' => t('Delete registration code after it is used?'),
+            '#default_value' => variable_get('og_user_roles_delete_regcode_' . $gid, 0),
+            '#description' => t('Do you want to delete each individual registration code after it is used to successfully subscribe a user to the group?'),
+            );
+          $form['og_user_roles_regcode_gid']['og_user_roles_regcode_value_' . $gid] = array(
+            '#type' => 'textarea',
+            '#title' => t('Registration codes for allowing users to subscribe to this group without admin approval'),
+            '#default_value' => variable_get('og_user_roles_regcode_value_' . $gid, ''),
+            '#cols' => 50,
+            '#rows' => 2,
+            '#description' => t('Specify here the registration codes which will allow users who enter them to subscribe to this moderated group without being manually approved by the group administrator. Structure your entries like this:
+              <p><strong>Code1,Code2,Code3<br /></strong></p>
+              <p>Each registration code separated by a comma. No spaces.'),
+            );
+          $form['og_user_roles_regcode_gid']['og_user_roles_assign_regcoderole_' . $gid] = array(
+            '#type' => 'checkbox',
+            '#title' => t('Set default basic group (group limited) role for users who join this group using this registration code?'),
+            '#default_value' => variable_get('og_user_roles_assign_regcoderole_' . $gid, 0),
+            '#description' => t('Do you wish to automatically assign a specific "registration code group role" to new subscribers to this group <b>who use this registration code to subscribe</b> to this group? The role is limited to this group. This role assignment can be be removed by the groups\' admin(s)'),
+            );
+          $form['og_user_roles_regcode_gid']['og_user_roles_regcoderole_value_' . $gid] = array(
+            '#type' => 'select',
+            '#title' => t('Role to assign'),
+            '#options' => $roles,
+            '#default_value' => variable_get('og_user_roles_regcoderole_value_' . $gid, 0),
+            '#description' => t('Select the role you wish to use as the "registration code group role" for every new subscriber who subscribes to this group using one of the registration codes above.'),
+            );      
+          $form['#validate'] += array('og_user_roles_regcode_form_validate' => array($form_id, &$form));          
+          $form['#submit'] += array('og_user_roles_regcode_form_submit' => array($form_id, &$form));          
+        }
+	  }
+    }
+  }
+
   // If this is a role deletion function
   if (arg(0) == 'admin' && arg(1) == 'user' && arg(2) == 'roles' && arg(3) == 'edit' && is_numeric(arg(4))) {
     $rid = arg(4);
@@ -2703,6 +3065,56 @@ function og_user_roles_basicgrouprole_fo
   variable_set('og_user_roles_assign_basicgrouprole_' . $gid, $form_values['og_user_roles_assign_basicgrouprole_' . $gid]);
   variable_set('og_user_roles_basicgrouprole_value_' . $gid, $form_values['og_user_roles_basicgrouprole_value_' . $gid]);
 }
+function og_user_roles_nopublic_form_submit($form_id, $form_values) {
+  $gid = arg(1);
+  variable_set('og_user_roles_assign_nopublic_' . $gid, $form_values['og_user_roles_assign_nopublic_' . $gid]);
+}
+function og_user_roles_regcode_form_submit($form_id, $form_values) {
+  $gid = arg(1);
+  variable_set('og_user_roles_assign_regcode_' . $gid, $form_values['og_user_roles_assign_regcode_' . $gid]);
+  variable_set('og_user_roles_delete_regcode_' . $gid, $form_values['og_user_roles_delete_regcode_' . $gid]);
+  variable_set('og_user_roles_regcode_value_' . $gid, $form_values['og_user_roles_regcode_value_' . $gid]);
+  variable_set('og_user_roles_assign_regcoderole_' . $gid, $form_values['og_user_roles_assign_regcoderole_' . $gid]);
+  variable_set('og_user_roles_regcoderole_value_' . $gid, $form_values['og_user_roles_regcoderole_value_' . $gid]);
+}
+/*
+ * Here we want to make sure that the submitted regcode value is not duplicated in another group.
+ */
+function og_user_roles_regcode_form_validate($form_id, $form_values) {
+  $gid = arg(1);
+  // No need to go through any of this if the 'Allow Group Admins to set Registration Codes' is not set
+  if (variable_get('og_user_roles_assign_regcode_gid', 0) == 1) {
+    // If this group allows for registration codes to be set
+    if ($form_values['og_user_roles_assign_regcode_' . $gid] == 1) {
+      // If this value is blank, return error
+      $regcode_array = explode(',', $form_values['og_user_roles_regcode_value_' . $gid]);
+      if (empty($regcode_array[0])) {
+        form_set_error('og_user_roles_regcode_value_' . $gid, "No valid registration code entered.");
+      }
+      // Get the list of OG groups
+      $groups = og_all_groups_options();
+      foreach ($groups as $group => $title) {
+	    // Get all groups except current one.
+        if ($group != $gid) {
+          // Only process groups that are allowed to have registration codes
+          if (variable_get('og_user_roles_assign_regcode_' . $group, 0) == 1) {
+	   	    // Get the array of codes for this group.
+            $group_array = explode(',', variable_get('og_user_roles_regcode_value_' . $group, ''));
+            // Check to see this array as any elements which are duplicated
+            if ($duplicated = array_intersect($regcode_array, $group_array)) {
+              // If there are duplicates, then display the group in which they are duplicated ONLY if this is a site admin
+              if (user_access('administer nodes')) {
+                form_set_error('og_user_roles_regcode_value_' . $gid, "Registration codes <b>" . implode(', ', $duplicated) . "</b> duplicated in group " . l($title, 'node/' . $group));
+			  } else {
+                form_set_error('og_user_roles_regcode_value_' . $gid, "Registration codes <b>" . implode(', ', $duplicated) . "</b> duplicated.");
+			  }
+            }
+    	  }
+	    }
+      }
+    }
+  }
+}
 function og_user_roles_role_submit($form_id, $form_values) {
   // If this is a delete role operation
   if ($form_values['op'] == t('Delete role')) {
@@ -2764,10 +3176,165 @@ function og_user_roles_theme() {
 }
 
 /**
+ * hook_og_create_links
+ *
  * Modification as per: http://drupal.org/node/203875
  * $group is an object containing the group node
  */
 function og_user_roles_og_create_links($group) {
   $links[] = l(t('Create content'), "node/add", array('title' => t('Add new content in this group.')), "gids[]=$group->nid");
   return $links;
-}
\ No newline at end of file
+}
+
+/**
+ * hook_block
+ *
+ */
+function og_user_roles_block($op = 'list', $delta = 0, $edit = array()) {
+  global $user;
+
+  if ($op == 'list') {
+    $blocks[0] = array('info' => t('Register Codes'),
+      'weight' => 0, 'enabled' => 1, 'region' => 'left');
+    return $blocks;
+  }
+  else if ($op == 'view') {
+    switch($delta) {
+      case 0:
+        if ($node = og_get_group_context()) {
+          $subscription = og_user_roles_og_status($node);
+          if ($subscription == 'none') {
+            if ($user->uid) {
+              if (variable_get('og_user_roles_assign_regcode_gid', 0) == 1 && variable_get('og_user_roles_assign_regcode_' . $node->nid, 0) == 1 ) {
+                $block = array('subject' => t('Register Codes'), 'content' => l('Enter your registration code', 'og/regcode'));
+              }
+            }
+		  }
+		}
+        break;
+    }
+    return $block;
+  }
+}
+
+/**
+ * hook_og_block_details
+ * 
+ * Trying to get this to work to put "Enter registration code" link in group menu
+ * if in moderation.
+ */
+function og_user_roles_og_block_details($group) {
+  if ($group->og_selective == OG_MODERATED && variable_get('og_user_roles_assign_regcode_gid', 0) == 1 && variable_get('og_user_roles_assign_regcode_' . $group->nid, '') == 1) {
+    $links[] = l(t('Enter registration code'), "og/regcode", array('title' => t('Subscribe to this group using a registration code.')));
+  }  
+  $oldblock = og_og_block_details($group);
+  $block['content'] = $oldblock['content'] . theme('item_list', $links);
+  $block['subject'] = $oldblock['subject'];
+  return $block;
+}
+
+/**
+ *  Menu callback function to register with a code.
+ *
+ */
+function og_user_roles_register() {
+  global $user;
+
+  if ($user->uid) {
+    drupal_set_message(t('If you have received a registration code for access to a group, enter it here.'));
+  } else {
+    drupal_set_message(l('You must register/login use registration codes.', 'user/login'));
+  }
+
+  $form['og_user_roles_regcode'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Group registration code'),
+    '#required' => TRUE,
+    '#size' => 25,
+	'#access' => $user->uid,
+  );
+   
+  if ($user->uid > 0) {
+    $form['submit'] = array('#type' => 'submit', '#value' => t('Submit code') );
+  }
+  return $form;
+}
+
+function og_user_roles_register_validate($form_id, $form_values, $form) {
+  global $user;
+  $gid = og_user_roles_gid_from_regcode($form_values['og_user_roles_regcode']);
+  if ($gid == 0) {
+    form_set_error('og_user_roles_regcode', "Not a valid group registration code.");
+  }
+  if (!user_access('use registration codes')) {
+    form_set_error('og_user_roles_regcode', "You do not have permission to use registration codes!");
+  }
+  if (!$user->uid) {
+    form_set_error('og_user_roles_regcode', "You must be logged in to use registration codes!");
+  }
+}
+
+function og_user_roles_register_submit($form_id, $form_values) {
+  global $user;
+  
+  if ($user->uid) {
+    $gid = og_user_roles_gid_from_regcode($form_values['og_user_roles_regcode']);
+	if ($gid > 0) {
+	  $node = node_load($gid);
+	  $title = $node->title;
+      $return = og_save_subscription($gid, $user->uid, array('is_active' => 1)); // as per http://drupal.org/node/156224
+      drupal_set_message(t('Subscription request to ' . l($title, 'node/'. $gid) . ' approved.'));	
+      // Delete this registration code if 'delete_regcode' is set
+      if (variable_get('og_user_roles_delete_regcode_' . $gid, 0) == 1) og_user_roles_delete_regcode($gid, $form_values['og_user_roles_regcode']);
+      // Places all new registration code group subscribers into default regcode role.
+      if (variable_get('og_user_roles_assign_regcoderole_' . $gid, 0)) { // check to see if this variable exists
+        if (variable_get('og_user_roles_assign_regcode_gid', 0) == 1 && variable_get('og_user_roles_assign_regcoderole_' . $gid, 0) == 1 ) {
+          $rid = variable_get('og_user_roles_regcoderole_value_' . $gid, 0);
+          og_user_roles_role_join($user->uid, $rid, $gid); // assign user to group role in that group
+        }
+      }
+	}
+  } else {
+    drupal_access_denied();
+  }
+
+}
+
+function og_user_roles_delete_regcode($gid, $regcode) {
+  // Get the array of registration codes for this $gid
+  $group_array = explode(',', variable_get('og_user_roles_regcode_value_' . $gid, ''));
+  // Create new array that contains all codes for this one.
+  foreach ($group_array as $item) {
+    if ($item != $regcode) {
+      $new_array[] = $item;    
+    }
+  }
+  // Implode the new array
+  $new = implode(",", $new_array);
+  // Write the new value.
+  variable_set('og_user_roles_regcode_value_' . $gid, $new);  
+}
+
+/**
+ * Determine the subscription status of this user in this group
+ *
+ * @param
+ *   Group node
+ * @return status
+ *   The status of subscriber containing 'active' or 'requested' or 'none'
+ */
+ function og_user_roles_og_status($node) {
+  global $user;
+  $subscription = 'none';
+  $result = db_query('select * from {og_uid} where nid = %d', $node->nid);
+  while ($row = db_fetch_object($result)) {
+    if ($row->uid == $user->uid) {
+      if ($row->is_active) {
+        $subscription = 'active';
+      } else {
+        $subscription = 'requested';
+      }
+    }
+  }
+  return $subscription;
+}
