| 1 |
<?php
|
| 2 |
// $Id: og_mandatory_group.module,v 1.11 2008/09/05 01:22:36 pwolanin Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_help
|
| 6 |
*
|
| 7 |
*/
|
| 8 |
function og_mandatory_group_help($path, $arg) {
|
| 9 |
$output = '';
|
| 10 |
switch ($path) {
|
| 11 |
case 'admin/og_settings/og_mandatory_group':
|
| 12 |
$output = '<p>'. t('Choose one group that all new users will be put into. You can chose either an open or a closed group. The user will be auto-approved as member of the group.');
|
| 13 |
$output .= '</p><p>'. t('You may also choose to require the new user to join one group in addition to any mandatory group. This setting will only have an effect if there are groups listed in the user registration form.'). '</p>';
|
| 14 |
break;
|
| 15 |
}
|
| 16 |
return $output;
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_form_alter
|
| 21 |
*
|
| 22 |
*/
|
| 23 |
function og_mandatory_group_form_alter(&$form, $form_state, $form_id) {
|
| 24 |
|
| 25 |
if ($form_id == 'user_register' && isset($form['og_register'])) {
|
| 26 |
$form['og_register']['og_mandatory_in_form'] = array('#type' => 'value', '#value' => FALSE,);
|
| 27 |
|
| 28 |
$group_count = count($form['og_register']['og_register']['#options']);
|
| 29 |
if ($mandatory_group = variable_get('og_mandatory_group', 0)) {
|
| 30 |
if ($mandatory_group_in_form = in_array($mandatory_group, array_keys($form['og_register']['og_register']['#options']))) {
|
| 31 |
$title = $form['og_register']['og_register']['#options'][$mandatory_group]. ' '. t('(This group is mandatory)');
|
| 32 |
$form['og_register']['og_mandatory_in_form'] = array(
|
| 33 |
'#type' => 'checkbox',
|
| 34 |
'#title' => $title,
|
| 35 |
'#value' => TRUE,
|
| 36 |
'#attributes' => array('disabled' => 'disabled'),
|
| 37 |
);
|
| 38 |
unset($form['og_register']['og_register']['#options'][$mandatory_group]);
|
| 39 |
$group_count--;
|
| 40 |
$form['og_register'] = array_reverse($form['og_register']);
|
| 41 |
}
|
| 42 |
}
|
| 43 |
if ($group_count > 0 && variable_get('og_mandatory_additional_group', FALSE)) {
|
| 44 |
if ($mandatory_group && $mandatory_group_in_form) {
|
| 45 |
$form['og_register']['minimum'] = array ('#value' => t('You must join at least one additional (non-mandatory) group.'),);
|
| 46 |
}
|
| 47 |
else {
|
| 48 |
$form['og_register']['minimum'] = array ('#value' => t('You must join at least one group.'),);
|
| 49 |
}
|
| 50 |
}
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Implementation of hook_user
|
| 56 |
*
|
| 57 |
*/
|
| 58 |
function og_mandatory_group_user($op, &$edit, &$account, $category = NULL) {
|
| 59 |
|
| 60 |
if (!module_exists('og')) {
|
| 61 |
return;
|
| 62 |
}
|
| 63 |
switch ($op) {
|
| 64 |
case 'validate':
|
| 65 |
if (isset($edit['og_register']) && variable_get('og_mandatory_additional_group', FALSE)) { //only present during registration
|
| 66 |
if (count($edit['og_register']) > 0 && count(array_filter($edit['og_register'])) < 1) {
|
| 67 |
if ($edit['og_mandatory_in_form']) {
|
| 68 |
form_set_error('og_register', "You must join at least one group in addition to the mandatory group");
|
| 69 |
}
|
| 70 |
else {
|
| 71 |
form_set_error('og_register', "You must join at least one group");
|
| 72 |
}
|
| 73 |
}
|
| 74 |
}
|
| 75 |
break;
|
| 76 |
case 'insert':
|
| 77 |
// Sloppy modules may try to register an account with uid == 0.
|
| 78 |
if (($group = variable_get('og_mandatory_group', 0)) != 0 && $account->uid) {
|
| 79 |
og_save_subscription($group, $account->uid, array('is_active' => 1));
|
| 80 |
// mail the admins
|
| 81 |
$node = node_load($group);
|
| 82 |
$subj = t("'@name' joined @site and was assigned to group '@group'.", array('@group' => $node->title, '@name' => $account->name, '@site' => variable_get('site_name', 'drupal')));
|
| 83 |
$from = variable_get('site_mail', ini_get('sendmail_from'));
|
| 84 |
|
| 85 |
$groups = '';
|
| 86 |
if (!empty($edit['og_register'])) {
|
| 87 |
$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.og_register = 1'), variable_get('og_node_types', array('og')));
|
| 88 |
while ($row = db_fetch_object($result)) {
|
| 89 |
if ($edit['og_register'][$row->nid]) {
|
| 90 |
switch ($row->og_selective) {
|
| 91 |
case OG_OPEN:
|
| 92 |
$groups .= check_plain($row->title) .' '. t('(open group)');
|
| 93 |
break;
|
| 94 |
case OG_MODERATED:
|
| 95 |
$groups .= check_plain($row->title) .' '. t('(moderated group)');
|
| 96 |
break;
|
| 97 |
case OG_INVITE_ONLY:
|
| 98 |
$groups .= check_plain($row->title) .' '. t('(invite only group)');
|
| 99 |
break;
|
| 100 |
case OG_CLOSED:
|
| 101 |
$groups .= check_plain($row->title) .' '. t('(closed group)');
|
| 102 |
break;
|
| 103 |
}
|
| 104 |
$groups .= "\n\t". url("node/$row->nid", array('absolute' => TRUE)) ."\n\n";
|
| 105 |
}
|
| 106 |
}
|
| 107 |
}
|
| 108 |
$sql = og_list_users_sql(1, 1);
|
| 109 |
$result = db_query($sql, $group);
|
| 110 |
while ($row = db_fetch_object($result)) {
|
| 111 |
if ($row->mail) {
|
| 112 |
$body = t('You may manage subscribers at @url', array('@url' => url("og/users/$node->nid", array('absolute' => TRUE))));
|
| 113 |
if (strlen($groups)) {
|
| 114 |
$body .= "\n". t('The user also subscribed to the following groups:') ."\n\n". $groups;
|
| 115 |
}
|
| 116 |
if (user_access('administer users', user_load(array('uid' => $row->uid)))) {
|
| 117 |
$body .= "\n". t('You may manage this user at @url', array('@url' => url("user/$account->uid/edit", array('absolute' => TRUE))));
|
| 118 |
}
|
| 119 |
// array of params with the context so the message will be sent properly
|
| 120 |
$params = array('context' => array('subject' => $subj, 'body' => $body));
|
| 121 |
drupal_mail('og_mandatory_group', 'notify', $row->mail, language_default(), $params, $from);
|
| 122 |
}
|
| 123 |
}
|
| 124 |
}
|
| 125 |
break;
|
| 126 |
}
|
| 127 |
}
|
| 128 |
|
| 129 |
/**
|
| 130 |
* Implementation of hook_mail()
|
| 131 |
*/
|
| 132 |
function og_mandatory_group_mail($key, &$message, $params) {
|
| 133 |
$context = $params['context'];
|
| 134 |
$message['subject'] = str_replace(array("\r", "\n"), '', $context['subject']);
|
| 135 |
$message['body'] = drupal_html_to_text($context['body']);
|
| 136 |
}
|
| 137 |
|
| 138 |
/**
|
| 139 |
* Implementation of hook_menu
|
| 140 |
*
|
| 141 |
*/
|
| 142 |
function og_mandatory_group_menu() {
|
| 143 |
$items['admin/og/og_mandatory_group'] = array(
|
| 144 |
'title' => t('Organic groups mandatory group'),
|
| 145 |
'description' => t('Make one group mandatory for new users and/or require new users to pick a group.'),
|
| 146 |
'page callback' => 'drupal_get_form',
|
| 147 |
'page arguments' => array('og_mandatory_group_settings'),
|
| 148 |
'access arguments' => array('administer site configuration'),
|
| 149 |
);
|
| 150 |
return $items;
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
* menu callback for module settings
|
| 155 |
*
|
| 156 |
*/
|
| 157 |
function og_mandatory_group_settings() {
|
| 158 |
$options[0] = theme('placeholder', t('none'));
|
| 159 |
$result = db_query("SELECT n.nid, n.title, o.* FROM {node} n INNER JOIN {og} o ON n.nid = o.nid WHERE (o.og_selective = %d OR o.og_selective = %d) ORDER BY n.title", OG_CLOSED, OG_OPEN);
|
| 160 |
while ($group = db_fetch_object($result)) {
|
| 161 |
$options[$group->nid] = check_plain($group->title);
|
| 162 |
switch ($group->og_selective) {
|
| 163 |
case OG_OPEN:
|
| 164 |
$options[$group->nid] .= ' '. theme('placeholder', t('Open group, users can unsubscribe.'));
|
| 165 |
break;
|
| 166 |
case OG_CLOSED:
|
| 167 |
$options[$group->nid] .= ' '. theme('placeholder', t('Closed group, users can not leave.'));
|
| 168 |
break;
|
| 169 |
}
|
| 170 |
}
|
| 171 |
$form['groups'] = array(
|
| 172 |
'#type' => 'fieldset',
|
| 173 |
'#title' => t('Choose the mandatory group'),
|
| 174 |
);
|
| 175 |
$form['groups']['og_mandatory_group'] = array(
|
| 176 |
'#type' => 'radios',
|
| 177 |
'#options' => $options,
|
| 178 |
'#default_value' => variable_get('og_mandatory_group', 0),
|
| 179 |
);
|
| 180 |
|
| 181 |
$form['og_mandatory_additional_group'] = array(
|
| 182 |
'#type' => 'checkbox',
|
| 183 |
'#title' => t('Require new users to join at least one group in addition to any mandatory group'),
|
| 184 |
'#default_value' => variable_get('og_mandatory_additional_group', FALSE),
|
| 185 |
);
|
| 186 |
$form['#validate'][] = 'og_mandatory_group_settings_validate';
|
| 187 |
return system_settings_form($form);
|
| 188 |
}
|
| 189 |
|
| 190 |
function og_mandatory_group_settings_validate($form, &$form_state) {
|
| 191 |
if (!isset($form['groups']['og_mandatory_group']['#options'][$form_state['values']['og_mandatory_group']])) {
|
| 192 |
form_set_error('groups', t('Invalid mandatory group- please choose an option from the list.'));
|
| 193 |
}
|
| 194 |
}
|