? rconstantine.diff
Index: og_mandatory_group.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og_mandatory_group/og_mandatory_group.module,v
retrieving revision 1.9
diff -u -p -r1.9 og_mandatory_group.module
--- og_mandatory_group.module	28 Oct 2006 01:55:59 -0000	1.9
+++ og_mandatory_group.module	10 Jan 2007 02:25:35 -0000
@@ -1,8 +1,33 @@
 <?php
-// $Id: og_mandatory_group.module,v 1.9 2006/10/28 01:55:59 pwolanin Exp $
+/**
+ * This is the Mandatory Group module for Organic Groups
+ * 
+ * <p>This file contains information on the og::mandatory_group module. The module allows
+ * a site admin to make one or more groups mandatory to new site subscribers.</p>
+ * 
+ * @author pwolanin
+ * @version $Id: og_mandatory_group.module,v 1.9 2006/10/28 01:55:59 pwolanin Exp $;
+ * @package Organic groups
+ * @category Organic groups
+ * @author Ryan Constantine added phpDocumentor code and added checkbox functionality,
+ * as well as force update to existing users
+ * @filesource
+ * @license http://www.gnu.org/licenses/gpl.txt GNU_GENERAL_PUBLIC_LICENSE
+ * @link http://drupal.org/project/og_mandatory_group Visit the Mandatory_Group project page here.
+ * TODO alternate selector/checkboxes depending on the number of groups a site has (if more than 20,
+ * switch to drop-down selector)
+ * TODO let admin select which types of groups (based on 'selective') they want to choose from for mandatory groups
+ * TODO add ability to retoactively apply the required groups to all users
+ * TODO make the retroactive feature apply selectively based on 'selection' (could be used to only make
+ * 'selection' = 3 (closed groups) added to old users' group lists)
+ * TODO select which existing users to force-join group
+ * TODO create separate mandatory group lists based on account type (account type feature to be added as separate user module update)
+ */
 /**
  * Implementation of hook_help
  * 
+ * @param string $section
+ * @return string
  */
 function og_mandatory_group_help($section) {
   switch ($section) {
@@ -16,27 +41,51 @@ function og_mandatory_group_help($sectio
 /**
  * Implementation of hook_form_alter
  * 
+ * if we want a user to join at least one group other than the mandatory ones,
+ * alter the user_register form and add the functionality for them to sign up
+ * for those groups
+ * @param mixed $form_id
+ * @param mixed &$form
  */
 function og_mandatory_group_form_alter($form_id, &$form) {
 
   if ($form_id == 'user_register' && isset($form['og_register'])) {
-    $form['og_register']['og_mandatory_in_form'] = array('#type' => 'value', '#value' => FALSE,);
-
+    
     $group_count = count($form['og_register']['og_register']['#options']);
-    if ($mandatory_group = variable_get('og_mandatory_group', 0)) {
-      if ($mandatory_group_in_form = in_array($mandatory_group, array_keys($form['og_register']['og_register']['#options']))) {
-        $title = $form['og_register']['og_register']['#options'][$mandatory_group]. ' '. t('(This group is mandatory)');
-        $form['og_register']['og_mandatory_in_form'] = array(
-          '#type' => 'checkbox',
-          '#title' => $title,
-          '#value' => TRUE, 
-          '#attributes' => array('disabled' => 'disabled'),
-        );
-        unset($form['og_register']['og_register']['#options'][$mandatory_group]);
-        $group_count--;
-        $form['og_register'] = array_reverse($form['og_register']);
+    
+    if (($mandatory_group = variable_get('og_mandatory_group', array())) && (array_sum($mandatory_group) != 0)) {
+      $form['og_register']['og_mandatory_groups_in_form'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('The following groups are mandatory:'),
+      '#weight' => '-1',
+      '#tree' => TRUE,
+      ); 
+      //loop through the mandatory groups, use those whose values match the key
+      foreach ($mandatory_group as $key=>$value){
+        
+        /*check if $mandatory_group ($key) has a matching value.*/
+        if ($key == $value){
+          //with all groups now stored in og_mandatory_group, only set $mandatory_group_in_form if $key = $value
+          $mandatory_group_in_form = TRUE;
+          $form['og_register']['og_mandatory_in_form'] = array(
+            '#type' => 'value',
+            '#value' => $mandatory_group_in_form,
+          );
+          $title = $form['og_register']['og_register']['#options'][$key]. ' '. t('(This group is mandatory)');
+          $form['og_register']['og_mandatory_groups_in_form'][$key] = array(
+            '#type' => 'checkbox',
+            '#title' => $title,
+            '#value' => $value, 
+            '#attributes' => array('disabled' => 'disabled'),
+          );
+          unset($form['og_register']['og_register']['#options'][$key]);
+          $group_count--;
+        }
+        //preserve the keys? probably, since our keys need to match our values. hence the TRUE below.
+        $form['og_register'] = array_reverse($form['og_register'], TRUE);
       }
     } 
+    
     if ($group_count > 0 && variable_get('og_mandatory_additional_group', FALSE)) {
       if ($mandatory_group && $mandatory_group_in_form) {    
         $form['og_register']['minimum'] = array ('#value' => t('You must join at least one additional (non-mandatory) group.'),);
@@ -51,6 +100,11 @@ function og_mandatory_group_form_alter($
 /**
  * Implementation of hook_user
  * 
+ * contect the mandatory groups with the users. update the related tables accordingly.
+ * @param string $op
+ * @param mixed $&$edit
+ * @param mixed &$account
+ * @param unknown_type $category
  */
 function og_mandatory_group_user($op, &$edit, &$account, $category = NULL) {
   
@@ -60,9 +114,10 @@ function og_mandatory_group_user($op, &$
   switch ($op) {
     case 'validate':
       if (isset($edit['og_register']) && variable_get('og_mandatory_additional_group', FALSE)) { //only present during registration
-        if (count($edit['og_register']) > 0 && count(array_filter($edit['og_register'])) < 1) {
+      
+        if (count($edit['og_register']) > 0 && count(array_filter($edit['og_register'])) < 1) { //no callback in array_filter, so removes entries with FALSE values (FALSE, NULL, '')
           if ($edit['og_mandatory_in_form']) {
-            form_set_error('og_register', "You must join at least one group in addition to the mandatory group");
+            form_set_error('og_register', "You must join at least one group in addition to the mandatory groups");
           }
           else {
             form_set_error('og_register', "You must join at least one group");
@@ -71,50 +126,84 @@ function og_mandatory_group_user($op, &$
       }
       break;    
     case 'insert':
+      //Presuming that all mandatory groups are owned by the site admins... set up the email accordingly
       if (($group = variable_get('og_mandatory_group', 0)) != 0) {
-        og_save_subscription($group, $account->uid, array('is_active' => 1));
-        // mail the admins
-        $node = node_load($group);
-        $subj = t("'@name' joined @site and was assigned to group '@group'.", array('@group' => $node->title, '@name' => $account->name, '@site' => variable_get('site_name', 'drupal')));
+        $subj = t("@name joined @site and was assigned to the following groups...", array('@name' => $account->name, '@site' => variable_get('site_name', 'drupal')));
+        $bodystart = t("@name joined @site and was assigned to the following mandatory groups that you administer: \n", array('@name' => $account->name, '@site' => variable_get('site_name', 'drupal')));
+        $bodygroup = array();
+        foreach ($group as $key => $value){
+          if ($key == $value){
+          og_save_subscription($value, $account->uid, array('is_active' => 1));
+          // mail the admins
+          $node = node_load($value);
+          $bodygroup[$value] = t("\n@group \n", array('@group' => $node->title));
+          }
+          else {
+            unset($group[$key]);
+          }
+        }
         $from = variable_get('site_mail', ini_get('sendmail_from'));
 
+        //prepare the list of additional groups that the user subscribed for (including any optionally required)
         $groups = '';
         if ($edit['og_register']) {
           $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, o.* FROM {node} n INNER JOIN {og} o ON n.nid = o.nid WHERE n.type IN ('. str_pad('', count(variable_get('og_node_types', array('og'))) * 5 - 1, "'%s',") .') AND n.status = 1 AND o.register = 1'), variable_get('og_node_types', array('og')));
           while ($row = db_fetch_object($result)) {
             if ($edit['og_register'][$row->nid]) {
               switch ($row->selective) {
-          case OG_OPEN:
-            $groups .= check_plain($row->title) .' '. t('(open group)');
-            break;
-          case OG_MODERATED:
-            $groups .= check_plain($row->title) .' '. t('(moderated group)');
-            break;
-          case OG_INVITE_ONLY:
-            $groups .= check_plain($row->title) .' '. t('(invite only group)');
-            break;
-          case OG_CLOSED:
-            $groups .= check_plain($row->title) .' '. t('(closed group)');
-            break;
+                case OG_OPEN:
+                  $groups .= check_plain($row->title) .' '. t('(open group)');
+                  break;
+                case OG_MODERATED:
+                  $groups .= check_plain($row->title) .' '. t('(moderated group)');
+                  break;
+                case OG_INVITE_ONLY:
+                  $groups .= check_plain($row->title) .' '. t('(invite only group)');
+                  break;
+                case OG_CLOSED:
+                  $groups .= check_plain($row->title) .' '. t('(closed group)');
+                  break;
               }
               $groups .= "\n\t". url("node/$row->nid", NULL, NULL, TRUE) ."\n\n";
             }
           }
         }
-        $sql = og_list_users_sql(1, 1);
-        $result = db_query($sql, $group);
-        while ($row = db_fetch_object($result)) {
-          if ($row->mail) {
-            $body = t('You may manage subscribers at @url', array('@url' => url("og/users/$node->nid", NULL, NULL, TRUE)));
-            if (strlen($groups)) {
-              $body .= "\n". t('The user also subscribed to the following groups:') ."\n\n". $groups;
-            }
-            if (user_access('administer users', user_load(array('uid' => $row->uid)))) {
-              $body .= "\n". t('You may manage this user at @url', array('@url' => url("user/$account->uid/edit", NULL, NULL, TRUE)));
+        
+        $mailsetup = array();
+        
+        //create a list of subscribed groups to send each administrator of each group
+        //only send each admin the messages that pertain to him/her
+        foreach ($group as $key => $value){
+          $sql = og_list_users_sql(1, 1);
+          $node = node_load($value);
+          $result = db_query($sql, $value);
+          while ($row = db_fetch_object($result)){
+            if ($row->mail){
+              if (array_key_exists($row->mail, $mailsetup)){
+                $mailsetup[$row->mail]['body'] .= $bodygroup[$node->nid];
+                $mailsetup[$row->mail]['body'] .= t('You may manage subscribers at @url', array('@url' => url("og/users/$node->nid", NULL, NULL, TRUE)));
+              }
+              else{
+                $mailsetup[$row->mail] = array();
+                $mailsetup[$row->mail]['to'] = $row->mail;
+                $mailsetup[$row->mail]['mailuid'] = (string)$row->uid;
+                $mailsetup[$row->mail]['body'] = $bodystart;
+                $mailsetup[$row->mail]['body'] .= $bodygroup[$node->nid];
+                $mailsetup[$row->mail]['body'] .= t('You may manage subscribers at @url', array('@url' => url("og/users/$node->nid", NULL, NULL, TRUE)));
+              }
             }
-            drupal_mail('og_mandatory_group_register', $row->mail, $subj, $body, $from);
+          }//end while
+        }//end foreach
+        
+        foreach ($mailsetup as $address){
+          if (strlen($groups)){
+            $address['body'] .= "\n\n". t('The user also subscribed to the following groups:') ."\n\n". $groups;
           }
-        }
+          if (user_access('administer users', user_load(array('uid' => $address['mailuid'])))){
+            $address['body'] .= "\n". t('You may manage this user at @url', array('@url' => url("user/$account->uid/edit", NULL, NULL, TRUE)));
+          }
+            drupal_mail('og_mandatory_group_register' . ' ' . $address['mailuid'], $address['to'], $subj, $address['body'], $from);
+        }//foreach
       }
       break;
   }
@@ -124,6 +213,9 @@ function og_mandatory_group_user($op, &$
 /**
  * Implementation of hook_menu
  * 
+ * add this item to the menu system and set the callback to present the form
+ * @param bool $may_cache
+ * @return array
  */
 function og_mandatory_group_menu($may_cache) {
 
@@ -133,7 +225,7 @@ function og_mandatory_group_menu($may_ca
     $items[] = array(
        'path' => 'admin/og/og_mandatory_group',
        'title' => t('Organic groups mandatory group'),
-       'description' => t('Make one group mandatory for new users and/or require new users to pick a group.'),
+       'description' => t('Make one or more groups mandatory for new users and/or require new users to pick a group.'),
        'callback' => 'drupal_get_form',
        'callback arguments' => 'og_mandatory_group_settings',
        'access' => user_access('administer site configuration'), 
@@ -145,9 +237,11 @@ function og_mandatory_group_menu($may_ca
 /**
  * menu callback for module settings
  * 
+ * setup the form made of checkboxes - one for each group - as well as the option to require an additional
+ * group at registration time
+ * @return mixed
  */
 function og_mandatory_group_settings() {
-  $options[0] = theme('placeholder', t('none'));
   $result = db_query("SELECT n.nid, n.title, o.* FROM {node} n INNER JOIN {og} o ON n.nid = o.nid WHERE (o.selective = %d OR o.selective = %d) ORDER BY n.title", OG_CLOSED, OG_OPEN);
   while ($group = db_fetch_object($result)) {
     $options[$group->nid] = check_plain($group->title);
@@ -160,16 +254,20 @@ function og_mandatory_group_settings() {
         break;
     }
   }
+  
   $form['groups'] = array(
     '#type' => 'fieldset',
-    '#title' => t('Choose the mandatory group'),
+    '#title' => t('Choose the mandatory groups'),
   );  
   $form['groups']['og_mandatory_group'] = array(
-    '#type' => 'radios', 
+  //changed 'type' to checkboxes -- TODO use 'select' if more than 20 groups, 'checkboxes' if less than 20
+  //also TODO make the above select/checkbox to change on a number set by the admin
+    '#type' => 'checkboxes', 
     '#options' => $options, 
-    '#default_value' => variable_get('og_mandatory_group', 0),
+    '#default_value' => variable_get('og_mandatory_group', array()),
   );
   
+  //TODO allow the admin to specify how many additional groups must be joined
   $form['og_mandatory_additional_group'] = array(
       '#type' => 'checkbox', 
       '#title' => t('Require new users to join at least one group in addition to any mandatory group'),
@@ -179,8 +277,46 @@ function og_mandatory_group_settings() {
   return system_settings_form($form);
 }
 
+/**
+ * form validation
+ * 
+ * make sure each submitted entry was actually in the original options list. if not, remove it
+ * from the array and submit the new array.
+ * @param string $form_id the id of the form
+ * @param mixed $form_values the values from the og_mandatory_group_settings function (after passes through other functions?)
+ * @param mixed $form the entire array for the form which includes a lot of housekeeping
+ */
 function og_mandatory_group_settings_validate($form_id, $form_values, $form) {
-  if (!isset($form['groups']['og_mandatory_group']['#options'][$form_values['og_mandatory_group']])) {
-    form_set_error('groups', t('Invalid mandatory group- please choose an option from the list.'));
+  //make sure the array exists
+  if (is_array($form_values['og_mandatory_group'])){
+    //go through each checkbox's key/value pair
+    foreach ($form_values['og_mandatory_group'] as $key) {
+      /*Between the form entry and this validation function, in the case where all boxes are unchecked,
+	a key/value pair (0,1) was being added to the og_mandatory_group array. I could not track down
+	which function was responsible so I decided to take care of the problem here. To test yourself,
+	comment out the if statement below.
+
+	All key/value pairs should either be key=value (such as 3,3) or the value of a valid group(key)
+	should be zero (such as 3,0). In the first case, the key is the nid of the valid group and the
+	value is the same, and as a positive number, we will use it to make that group mandatory. In the
+	second case, the key should again be a valid nid and the zero indicates that the group is not
+	mandatory. 
+
+	As the phantom data that was added to og_mandatory_group was 0,1, this defies both requirements.
+	So we need to first check if the key (supposed nid) is actually a valid group from #options. Next,
+	we need to remove the key/value pair from the array and store the fixed array.
+
+	Strange note: if it wasn't strange enough having the mystery 0,1 pair show up when all boxes were
+	unchecked, if valid group nids were higher than 2(my lowest was 3), then a second selection of no
+	boxes would add the pair 1,1 to the list. Three or more tries wouldn't add any more mystery entries.
+	So if anyone can duplicate this problem and figure out what was happening, that would be great.*/
+      if (!isset($form['groups']['og_mandatory_group']['#options'][$key])){
+        unset($form_values['og_mandatory_group'][$key]);
+        form_set_value($form['groups']['og_mandatory_group'], $form_values['og_mandatory_group']);
+      }
+    } 
+  }
+  else {
+    form_set_error('groups', t('Form results malformed- please try again by reloading the page.'));
   }
 }
