Index: og_mandatory_group.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/og_mandatory_group/og_mandatory_group.module,v retrieving revision 1.4.2.1 diff -u -p -r1.4.2.1 og_mandatory_group.module --- og_mandatory_group.module 22 Jul 2006 12:40:59 -0000 1.4.2.1 +++ og_mandatory_group.module 25 Sep 2006 03:02:35 -0000 @@ -14,11 +14,58 @@ function og_mandatory_group_help($sectio } /** + * Implementation of hook_form_alter + * + */ +function og_mandatory_group_form_alter ($form_id, &$form) { + if ($form_id == 'user_register') { + unset($form['og_register']['og_register']['#options'][0]); //temporary fix + + $group_count = count($form['og_register']['og_register']['#options']); + if ($mandatory_group = variable_get('og_mandatory_group', 0)) { + if (in_array($mandatory_group, array_keys($form['og_register']['og_register']['#options']))) { + $form['og_register']['og_register']['#options'][$mandatory_group] .= ' ('. t('This group is mandatory'). ')'; + $form['og_register']['og_register']['#default_value'] = array($mandatory_group); + $form['og_mandatory_in_form'] = array('#type' => 'value', '#value' => $mandatory_group,); + if ($group_count > 1 && variable_get('og_mandatory_additional_group', FALSE)) { + $form['og_register']['minimum'] = array ('#value' => t('You must join at least one additional (non-mandatory) group.'),); + } + } + else { + $form['og_mandatory_in_form'] = array('#type' => 'value', '#value' => FALSE,); + if ($group_count > 0 && variable_get('og_mandatory_additional_group', FALSE)) { + $form['og_register']['minimum'] = array ('#value' => t('You must join at least one group.'),); + } + } + } + } +} + +/** * Implementation of hook_user * */ function og_mandatory_group_user($op, &$edit, &$account, $category = NULL) { + + if (!module_exist('og')) { + return; + } switch ($op) { + case 'validate': + if (isset($edit['og_register']) && variable_get('og_mandatory_additional_group', FALSE)) { //only present during registration + if ($edit['og_mandatory_in_form']) { + $edit['og_register'][$edit['og_mandatory_in_form']] = TRUE; //for counting purposes, make sure the mandatory group is checked + if (count($edit['og_register']) > 1 && count(array_filter($edit['og_register'])) < 2) { + form_set_error('og_register', "You must join at least one non-mandatory group"); + } + } + else { + if (count($edit['og_register']) > 0 && count(array_filter($edit['og_register'])) < 1) { + form_set_error('og_register', "You must join at least one group"); + } + } + } + break; case 'insert': if (($group = variable_get('og_mandatory_group', 0)) != 0) { og_save_subscription($group, $account->uid, array('is_active' => 1)); @@ -29,28 +76,28 @@ function og_mandatory_group_user($op, &$ $headers = "From: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from"; $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; - } - $groups .= "\n\t". url("node/$row->nid", NULL, NULL, TRUE) ."\n\n"; - } - } - } + 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; + } + $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)) { @@ -90,7 +137,16 @@ function og_mandatory_group_settings() { } } if (count($options)) { - $form['og_mandatory_group'] = array('#type' => 'radios', '#options' => $options, '#default_value' => variable_get('og_mandatory_group', 0)); - return $form; + $form['og_mandatory_group'] = array( + '#type' => 'radios', + '#options' => $options, + '#default_value' => variable_get('og_mandatory_group', 0), + ); } + $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'), + '#default_value' => variable_get('og_mandatory_additional_group', FALSE), + ); + return $form; }