/[drupal]/contributions/modules/og_multiple_mandatory_groups_by_role/og_multiple_mandatory_groups_by_role.module
ViewVC logotype

Contents of /contributions/modules/og_multiple_mandatory_groups_by_role/og_multiple_mandatory_groups_by_role.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Sat Jul 19 16:56:25 2008 UTC (16 months, 1 week ago) by rconstantine
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +2 -2 lines
File MIME type: text/x-php
#268107 Fixed user edit group assignment - I think
1 <?php
2 /**
3 * This is the Multiple Mandatory Groups by Role module for Organic Groups
4 *
5 * <p>This file contains information on the og_multiple_mandatory_groups_by_role module. The module allows
6 * a site admin to make one or more groups mandatory to new site subscribers, to Group admins, or by
7 * role.</p>
8 *
9 * @author Ryan Constantine
10 * @version $Id:$;
11 * @package OG_MMGBR
12 * @category NeighborForge
13 * @filesource
14 * @license http://www.gnu.org/licenses/gpl.txt GNU_GENERAL_PUBLIC_LICENSE
15 * @link http://drupal.org/project/og_multiple_mandatory_groups_by_role Visit the OG_Multiple_Mandatory_Groups_By_Role project page here.
16 * TODO alternate selector/checkboxes depending on the number of groups a site has (if more than 20,
17 * switch to drop-down selector)
18 * TODO let admin select which types of groups (based on 'selective') they want to choose from for mandatory groups (a filter)
19 * TODO make the retroactive feature apply selectively based on 'selection' (could be used to only make
20 * 'selection' = 3 (closed groups) added/deleted at updates)
21 * TODO create separate mandatory group lists based on account type (account type feature to be added as separate user module update)
22 */
23 /**
24 * Implementation of hook_help
25 *
26 * @param string $path
27 * @param array $arg
28 * @return string
29 */
30 function og_multiple_mandatory_groups_by_role_help($path, $arg) {
31 switch ($path) {
32 case 'admin/og_settings/og_multiple_mandatory_groups_by_role':
33 $output = '<p>' .t('Choose one or more 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 a member of the group.');
34 $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>';
35 $output .= '</p><p>' .t('Additionally, you may specify mandatory groups for those who are assigned certain roles.'). '</p>';
36 $output .= '</p><p>' .t('Lastly, you can specify mandatory groups for Group admins.'). '</p>';
37 return $output;
38 }
39 } // function og_multiple_mandatory_groups_by_role_help()
40
41 /**
42 * Implementation of hook_perm
43 *
44 * Valid permissions for this module
45 * @return array An array of valid permissions for the onthisdate module
46 */
47 function og_multiple_mandatory_groups_by_role_perm() {
48 return array('administer og mandatory groups by role');
49 } // function og_multiple_mandatory_groups_by_role_perm()
50
51 /**
52 * Implementation of hook_menu
53 *
54 * Add this module to the menu system and set the callback to present the forms.
55 * @return array
56 */
57 function og_multiple_mandatory_groups_by_role_menu() {
58
59 $items = array();
60
61 $items['admin/og/og_multiple_mandatory_groups_by_role'] = array( //main settings page; add roles to have mandatory groups for; links to edit and delete settings
62 'title' => 'Multiple mandatory groups by role',
63 'description' => 'Make one or more groups mandatory for new users, group admins, specific roles and/or require new users to pick an additional group to join at registration.',
64 'page callback' => 'drupal_get_form',
65 'page arguments' => array('og_multiple_mandatory_groups_by_role_main'),
66 'access callback' => 'user_access',
67 'access arguments' => array('administer og mandatory groups by role'),
68 'type' => MENU_NORMAL_ITEM,
69 );
70
71 $items['admin/og/og_multiple_mandatory_groups_by_role/overview'] = array( //tab to take us to main settings page
72 'title' => 'Overview',
73 'page callback' => 'drupal_get_form',
74 'page arguments' => array('og_multiple_mandatory_groups_by_role_main'),
75 'access callback' => 'user_access',
76 'access arguments' => array('administer og mandatory groups by role'),
77 'type' => MENU_DEFAULT_LOCAL_TASK,
78 'weight' => -10,
79 );
80
81 $items['admin/og/og_multiple_mandatory_groups_by_role/admin'] = array( //assign mandatory groups for each role under administration
82 'title' => 'Assign Groups',
83 'description' => 'Assign which groups you\'d like to be mandatory for each role.',
84 'page callback' => 'drupal_get_form',
85 'page arguments' => array('og_multiple_mandatory_groups_by_role_admin'),
86 'access callback' => 'user_access',
87 'access arguments' => array('administer og mandatory groups by role'),
88 'type' => MENU_LOCAL_TASK,
89 );
90
91 $items['admin/og/og_multiple_mandatory_groups_by_role/admin/%og_multiple_mandatory_groups_by_role_admin'] = array( //assign mandatory groups for each role under administration
92 'title' => 'Assign Groups',
93 'description' => 'Assign which groups you\'d like to be mandatory for each role.',
94 'page callback' => 'drupal_get_form',
95 'page arguments' => array('og_multiple_mandatory_groups_by_role_admin', 4),
96 'access callback' => 'user_access',
97 'access arguments' => array('administer og mandatory groups by role'),
98 'type' => MENU_LOCAL_TASK,
99 );
100
101 $items['admin/og/og_multiple_mandatory_groups_by_role/delete'] = array( //delete a role from mandatory group management
102 'title' => 'Delete',
103 'page callback' => 'drupal_get_form',
104 'page arguments' => array('og_multiple_mandatory_groups_by_role_confirm_delete'),
105 'access callback' => 'user_access',
106 'access arguments' => array('administer og mandatory groups by role'),
107 'type' => MENU_CALLBACK,
108 );
109
110 $items['admin/og/og_multiple_mandatory_groups_by_role/delete_unsubscribe'] = array( //delete a role from mandatory group management
111 'title' => 'Delete Role and Unsubscribe All Users',
112 'page callback' => 'drupal_get_form',
113 'page arguments' => array('og_multiple_mandatory_groups_by_role_confirm_delete_unsub'),
114 'access callback' => 'user_access',
115 'access arguments' => array('administer og mandatory groups by role'),
116 'type' => MENU_CALLBACK,
117 );
118
119 $items['admin/og/og_multiple_mandatory_groups_by_role/groups'] = array( //assign mandatory groups for each role under administration
120 'title' => 'Available Groups',
121 'description' => 'Decide which groups are available to make mandatory.',
122 'page callback' => 'drupal_get_form',
123 'page arguments' => array('og_multiple_mandatory_groups_by_role_groups'),
124 'access callback' => 'user_access',
125 'access arguments' => array('administer og mandatory groups by role'),
126 'type' => MENU_LOCAL_TASK,
127 );
128
129 $items['admin/og/og_multiple_mandatory_groups_by_role/groups_delete'] = array( //delete a group from available groups list
130 'title' => 'Delete group from availability list',
131 'page callback' => 'drupal_get_form',
132 'page arguments' => array('og_multiple_mandatory_groups_by_role_group_confirm_delete'),
133 'access callback' => 'user_access',
134 'access arguments' => array('administer og mandatory groups by role'),
135 'type' => MENU_CALLBACK,
136 );
137
138 $items['admin/og/og_multiple_mandatory_groups_by_role/groups_delete_unsubscribe'] = array( //delete a group from available groups list
139 'title' => 'Delete group from availability list and unsubscribe users',
140 'page callback' => 'drupal_get_form',
141 'page arguments' => array('og_multiple_mandatory_groups_by_role_group_confirm_delete_unsubscribe'),
142 'access callback' => 'user_access',
143 'access arguments' => array('administer og mandatory groups by role'),
144 'type' => MENU_CALLBACK,
145 );
146
147 return $items;
148 } // function og_multiple_mandatory_groups_by_role_menu()
149
150 //Site administration section---------------------------------------------------------------------------------
151 //This section is only for administration forms and form processing
152
153 /**
154 * Callback function: Add roles to administer the mandatory groups for.
155 *
156 * @param integer $rid
157 * The RID of the role.
158 * @return mixed
159 */
160 function og_multiple_mandatory_groups_by_role_main($rid = NULL) {
161 if ($admin = user_access('administer og mandatory groups by role')) {
162 $options = user_roles(1);
163 unset($options[2]);
164 $added = _og_multiple_mandatory_groups_by_role_get_added();
165 foreach ($added as $role_id => $name) {//remove from the select, groups we're already working with
166 if (array_key_exists($role_id, $options)) {
167 unset($options[$role_id]);
168 }
169 }
170 $form['roles'] = array(
171 '#type' => 'select',
172 '#title' => t('Roles'),
173 '#options' => $options,
174 );
175 $form['submit'] = array(
176 '#type' => 'submit',
177 '#value' => t('Add role'),
178 );
179 }
180 return $form;
181 } // function og_multiple_mandatory_groups_by_role_main()
182
183 /**
184 * Validate the role name before adding or saving it
185 *
186 * @param string $form_id
187 * @param mixed $form_state['values']
188 */
189 function og_multiple_mandatory_groups_by_role_main_validate($form, &$form_state) {
190 if ($form_state['values']['roles']) {
191 //make sure the user isn't injecting something not on the list.
192 $check_options = _og_multiple_mandatory_groups_by_role_get_added();
193 //if ($form_state['values']['op'] == t('Add role')) {
194 if (array_key_exists($form_state['values']['roles'], $check_options)) {
195 form_set_error('role', t('The role name %name is already added. Please choose another role name.', array('%name' => $form_state['values']['roles']['name'])));
196 }
197 //}
198 }
199 else {
200 form_set_error('name', t('You must specify a valid role name.'));
201 }
202 }//function og_multiple_mandatory_groups_by_role_main_validate()
203
204 /**
205 * Submit the role name and id to be added to the database.
206 *
207 * @param string $form_id
208 * @param mixed $form_state['values']
209 * @return string
210 */
211 function og_multiple_mandatory_groups_by_role_main_submit($form, &$form_state) {
212 //if ($form_state['values']['op'] == t('Add role')) {
213 $all_roles = user_roles(1);
214 unset($options[2]);
215 db_query("INSERT INTO {og_multi_mand_groups_role} (rid, role_name, mand_groups) VALUES (%d, '%s', '%s')", $form_state['values']['roles'], $all_roles[$form_state['values']['roles']], serialize(array()));
216 drupal_set_message(t('The role has been added.'));
217 //}
218 $form_state['redirect'] = 'admin/og/og_multiple_mandatory_groups_by_role';
219 } //function og_multiple_mandatory_groups_by_role_main_submit()
220
221 /**
222 * Theme for the add page of the main function.
223 *
224 * <p>This lists all of the current roles with links to edit them or their assigned groups, followed with
225 * a field and button to add a new role name to the list.</p>
226 * @param mixed $form
227 * @return mixed
228 */
229 function theme_og_multiple_mandatory_groups_by_role_main($form) {
230 $rows = array();
231 $header = array(t('Role'), array('data' => t('Operations'), 'colspan' => 3));
232 $added_roles = _og_multiple_mandatory_groups_by_role_get_added();
233 //keep the all_user settings at the top...
234 $name = $added_roles[-1];
235 $admin = l(t('assign groups'), 'admin/og/og_multiple_mandatory_groups_by_role/admin/' . -1);
236 $rows[] = array($name, $admin, NULL, NULL);
237 //...with the default group settings next...
238 $name = $added_roles[0];
239 $admin = l(t('assign groups'), 'admin/og/og_multiple_mandatory_groups_by_role/admin/' . 0);
240 $rows[] = array($name, $admin, NULL, NULL);
241 //...then finish the rest in alphabetical order
242 foreach ($added_roles as $rid => $name) {
243 if ($rid != -1 && $rid != 0) {
244 $admin = l(t('assign groups'), 'admin/og/og_multiple_mandatory_groups_by_role/admin/' .$rid);
245 $delete = l(t('delete'), 'admin/og/og_multiple_mandatory_groups_by_role/delete/' .$rid);
246 $delete_uns = l(t('delete and unsubscribe'), 'admin/og/og_multiple_mandatory_groups_by_role/delete_unsubscribe/' .$rid);
247 $rows[] = array($name, $admin, $delete, $delete_uns);
248 }
249 }
250 $rows[] = array(drupal_render($form['roles']), array('data' => drupal_render($form['submit']), colspan => 3));
251
252 $output = drupal_render($form);
253 $output .= theme('table', $header, $rows);
254
255 return $output;
256 } //function theme_og_multiple_mandatory_groups_by_role_main()
257
258 /**
259 * A theme function
260 */
261 function og_multiple_mandatory_groups_by_role_theme() {
262 return array(
263 'og_multiple_mandatory_groups_by_role_groups' => array(
264 'arguments' => array('form' => NULL),
265 ),
266 'og_multiple_mandatory_groups_by_role_main' => array(
267 'arguments' => array('form' => NULL),
268 ),
269 );
270 }
271
272 /**
273 * Returns a confirmation page for deleting a role from the mandatory group admin table.
274 *
275 * @param integer $rid Value of role to be deleted; passed in from path.
276 * @return array $form The RID is passed to the submit.
277 */
278 function og_multiple_mandatory_groups_by_role_confirm_delete($rid = NULL) {
279 if ($rid == NULL) {
280 drupal_goto('admin/og/og_multiple_mandatory_groups_by_role');
281 return;
282 }
283 $sql = "SELECT ommgr.rid, ommgr.role_name FROM {og_multi_mand_groups_role} ommgr WHERE ommgr.rid = %d";
284 $result = db_fetch_object(db_query($sql, $rid));
285 $form['name'] = array('#type' => 'value', '#value' => $result->role_name);
286 $form['rid'] = array('#type' => 'value', '#value' => $rid);
287 $return_address = 'admin/og/og_multiple_mandatory_groups_by_role';
288 $form = confirm_form($form, t('Are you sure you want to delete the role %name from the mandatory group manager?',
289 array('%name' => $result->role_name)), $return_address,
290 t('Deleting a role here will make groups previously associated with that role no longer mandatory. Users will not be unsubscribed from the group(s). This action cannot be undone.'),
291 t('Delete'), t('Cancel'));
292 return $form;
293 } // function og_multiple_mandatory_groups_by_role_confirm_delete()
294
295 /**
296 * Implementation of forms api _submit call. Deletes a role from the og_multi_mand_groups_role table after confirmation.
297 *
298 * @param string $form_id
299 * @param mixed $form_state['values']
300 * @return string $return_address The path to return to after we've deleted the record.
301 */
302 function og_multiple_mandatory_groups_by_role_confirm_delete_submit($form, &$form_state) {
303 $dbquery = db_query('DELETE FROM {og_multi_mand_groups_role} WHERE rid = %d', $form_state['values']['rid']);
304 if ($dbquery) {
305 drupal_set_message(t('The role %rid has been removed from the mandatory groups admin table.', array('%rid' => $form_state['values']['name'])));
306 watchdog('content', 'og_multiple_mandatory_groups_by_role: deleted %rid.', array('%rid' => $form_state['values']['name']));
307 }
308 $return_address = 'admin/og/og_multiple_mandatory_groups_by_role';
309 $form_state['redirect'] = $return_address;
310 } // function og_multiple_mandatory_groups_by_role_confirm_delete_submit()
311
312 /**
313 * Returns a confirmation page for deleting a role from the mandatory group admin table.
314 *
315 * @param integer $rid Value of role to be deleted; passed in from path.
316 * @return array $form The RID is passed to the submit.
317 */
318 function og_multiple_mandatory_groups_by_role_confirm_delete_unsub($rid = NULL) {
319 if ($rid == NULL) {
320 drupal_goto('admin/og/og_multiple_mandatory_groups_by_role');
321 return;
322 }
323 $sql = "SELECT ommgr.rid, ommgr.role_name FROM {og_multi_mand_groups_role} ommgr WHERE ommgr.rid = %d";
324 $result = db_fetch_object(db_query($sql, $rid));
325 $form['name'] = array('#type' => 'value', '#value' => $result->role_name);
326 $form['rid'] = array('#type' => 'value', '#value' => $rid);
327 $return_address = 'admin/og/og_multiple_mandatory_groups_by_role';
328 $form = confirm_form($form, t('Are you sure you want to delete the role %name from the mandatory group manager?',
329 array('%name' => $result->role_name)), $return_address,
330 t('Deleting a role here will make groups previously associated with that role no longer mandatory. Users will be unsubscribed from the group(s). This action cannot be undone.'),
331 t('Delete'), t('Cancel'));
332 return $form;
333 } // function og_multiple_mandatory_groups_by_role_confirm_delete_unsub()
334
335 /**
336 * Implementation of forms api _submit call. Deletes a role from the og_multi_mand_groups_role table after confirmation.
337 *
338 * @param string $form_id
339 * @param mixed $form_state['values']
340 * @return string $return_address The path to return to after we've deleted the record.
341 */
342 function og_multiple_mandatory_groups_by_role_confirm_delete_unsub_submit($form, &$form_state) {
343 //unsubscribe users that have been in this role's groups if they won't still be in the groups via other roles
344 $all_allowed_groups = _og_multiple_mandatory_groups_by_groups_get_added();
345 $sql = "SELECT o.rid, o.role_name, o.mand_groups FROM {og_multi_mand_groups_role} o ORDER BY o.rid ASC";
346 $results = db_query($sql);
347 $all_groups = array();
348 $these_groups = array();
349 while ($result = db_fetch_object($results)) {
350 if ($result->rid != $form_state['values']['rid']) {
351 $mand_groups = unserialize($result->mand_groups);
352 foreach ($mand_groups as $gid => $value) {
353 if (!key_exists($gid, $all_groups) && ($mand_groups[$gid] == $gid)) {
354 $all_groups[$gid] = $value;
355 }
356 }
357 }
358 else {
359 $mand_groups = unserialize($result->mand_groups);
360 foreach ($mand_groups as $gid => $value) {
361 if (!key_exists($gid, $these_groups) && ($mand_groups[$gid] == $gid)) {
362 $these_groups[$gid] = $value;
363 }
364 }
365 }
366 }
367 foreach ($these_groups as $gid => $value) {
368 if (key_exists($gid, $all_groups)) {
369 unset($these_groups[$gid]);
370 }
371 }
372 foreach ($these_groups as $gid => $value) {
373 $sql = "SELECT ou.uid, ou.nid FROM {og_uid} ou LEFT JOIN {node} n ON ou.nid = n.nid AND ou.uid = n.uid WHERE ou.nid = %d AND n.uid IS NULL ORDER BY ou.uid ASC";
374 $results = db_query($sql, $gid);
375 while ($result = db_fetch_object($results)) {
376 og_delete_subscription($result->nid, $result->uid);
377 drupal_set_message(t('The user %uid has been unsubscribed from group %gid.', array('%uid' => $result->uid, '%gid' => $all_allowed_groups[$result->nid])));
378 }
379 }
380
381 $dbquery = db_query('DELETE FROM {og_multi_mand_groups_role} WHERE rid = %d', $form_state['values']['rid']);
382 if ($dbquery) {
383 drupal_set_message(t('The role %rid has been removed from the mandatory groups admin table.', array('%rid' => $form_state['values']['name'])));
384 watchdog('content', 'og_multiple_mandatory_groups_by_role: deleted %rid.', array('%rid' => $form_state['values']['name']));
385 }
386 $return_address = 'admin/og/og_multiple_mandatory_groups_by_role';
387 $form_state['redirect'] = $return_address;
388 } // function og_multiple_mandatory_groups_by_role_confirm_delete_unsub_submit()
389
390 /**
391 * Menu callback; passes a og_multiple_mandatory_groups_by_role_admin
392 */
393 function og_multiple_mandatory_groups_by_role_admin_load($rid) {
394 if (!is_numeric($rid)) {
395 return NULL;
396 }
397 $roles = _og_multiple_mandatory_groups_by_role_get_added();
398 if (!isset($roles[$rid])) {
399 return FALSE;
400 }
401 return $rid;
402 }
403
404 /**
405 * Callback function: Assign mandatory groups to roles.
406 *
407 * @param
408 * @return array $form
409 */
410 function og_multiple_mandatory_groups_by_role_admin($form_state, $rid = NULL) {
411 if ($rid == NULL) {
412 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr ORDER BY ommgr.role_name ASC";
413 $results = db_query($sql);
414 $coll = 1;
415 }
416 else {
417 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr WHERE ommgr.rid = %d";
418 $results = db_query($sql, $rid);
419 $coll = 0;
420 }
421 $options = _og_multiple_mandatory_groups_by_groups_get_added();
422 $unsubs = _og_multiple_mandatory_groups_by_role_get_insubs();
423 $form['roles'] = array('#tree' => TRUE);
424 while ($result = db_fetch_object($results)) { //grab a role that we're monitoring
425 //fetch the stored groups associated with this role
426 $groups_of_role = unserialize($result->mand_groups);
427 //create this part of the form
428 $form['roles'][$result->role_name] = array(
429 '#type' => 'fieldset',
430 '#title' => $result->role_name,
431 '#tree' => TRUE,
432 '#collapsible' => TRUE,
433 '#collapsed' => $coll,
434 '#weight' => 0,
435 );
436 if ($result->role_name == t('All users')) {
437 $form['roles'][$result->role_name]['#weight'] = -10;
438 }
439 if ($result->role_name == t('Group admins')) {
440 $form['roles'][$result->role_name]['#description'] = t('For the Group admins section only, make sure that mandatory groups from this section are not used in the other role sections!!!');
441 $form['roles'][$result->role_name]['#weight'] = -8;
442 }
443 $form['roles'][$result->role_name]['og_multiple_mandatory_groups_by_role'] = array(
444 '#type' => 'checkboxes',
445 '#options' => $options,
446 '#default_value' => $groups_of_role,
447 );
448 $form['roles'][$result->role_name]['og_multiple_mandatory_groups_by_role_old_values'] = array(
449 '#type' => 'hidden',
450 '#value' => $result->mand_groups,
451 );
452 if ($result->role_name == t('All users')) {
453 $form['roles'][$result->role_name]['og_mandatory_additional_group'] = array(
454 '#type' => 'checkbox',
455 '#title' => t('Require One Additional Group'),
456 '#description' => t('Require new users to join at least one group in addition to any mandatory group(s).'),
457 '#default_value' => variable_get('og_mandatory_additional_group', FALSE),
458 );
459 }
460 //users with that role to make sure they are subscribed/unsubscribed to the right groups
461 $form['roles'][$result->role_name]['unsub'] = array(
462 '#type' => 'checkbox',
463 '#title' => 'Unsubscribe unchecked boxes',
464 '#description' => t('Unsubscribe all members of this role from the unchecked groups as indicated above at next save.'),
465 '#default_value' => isset($unsubs[$result->role_name]) ? $unsubs[$result->role_name] : 0,
466 '#return_value' => 1,
467 );
468 }
469
470 if ($rid == NULL) {
471 $form['roles']['retro'] = array(
472 '#type' => 'checkbox',
473 '#title' => 'Update Subscriptions Retroactively',
474 '#description' => t('Subscribe or unsubscribe all members of each role to the groups as indicated above at next save.<br />
475 WARNING: This is potentially a VERY expensive procedure. It is best to save any changes first, then check this and save again.<br />
476 This will update users who existed prior to your use of this module and synchronize them with the above settings.'),
477 '#default_value' => 0,
478 '#return_value' => 1,
479 );
480 }
481 //create a submit button
482 $form['submit'] = array('#type' => 'submit', '#value' => t('Save assignments'));
483
484 return $form;
485 } //function og_multiple_mandatory_groups_by_role_admin()
486
487 /**
488 * Submit the role/group associations for storage
489 *
490 * @param string $form_id
491 * @param mixed $form_state['values']
492 */
493 function og_multiple_mandatory_groups_by_role_admin_submit($form, &$form_state) {
494 $error = FALSE;
495 $hold_updates = array();
496 $all_users = array();
497 $old = array();
498 $retro = $form_state['values']['roles']['retro'];
499
500 foreach (element_children($form_state['values']['roles']) as $role) {
501 //for some reason, if we have a role without assignments and we save, erroneous values are added in sequence.
502 //by unsetting any 0 index we find, this doesn't happen -- might not be happening in D6, so we could take it out!!!
503 if (isset($form_state['values']['roles'][$role]['og_multiple_mandatory_groups_by_role']['0'])) {
504 unset($form_state['values']['roles'][$role]['og_multiple_mandatory_groups_by_role']['0']);
505 }
506 $query = db_query("UPDATE {og_multi_mand_groups_role} SET mand_groups = '%s' WHERE role_name = '%s'",
507 serialize($form_state['values']['roles'][$role]['og_multiple_mandatory_groups_by_role']), $role);
508 $old[$role] = unserialize($form_state['values']['roles'][$role]['og_multiple_mandatory_groups_by_role_old_values']);
509 $hold_updates[$role] = $form_state['values']['roles'][$role]['og_multiple_mandatory_groups_by_role']; // we have to hold all values until later so we don't unsubscribe from groups we shouldn't
510
511 if (!$query) {
512 $error = TRUE;
513 }
514 }
515 //drupal_set_message('<pre>OLD: ' .print_r($old, TRUE). '</pre>');
516 //drupal_set_message('<pre>Hold updates: ' .print_r($hold_updates, TRUE). '</pre>');
517 //collect info on users so we don't unsubscribe from groups on accident; we always overwrite subscriptions, but only add unsubscriptions when a subscription doesn't exist
518 //first get all users and set groups to subscribe and unsubscribe from
519 if (key_exists('All users', $hold_updates)) {
520 $sql = "SELECT u.uid FROM {users} u WHERE u.uid > 1 ORDER BY u.uid ASC";
521 $results = db_query($sql);
522 while ($result = db_fetch_object($results)) {
523 foreach ($hold_updates['All users'] as $group => $val) {
524 if (($group == $val) && (($val != $old['All users'][$group]) || $retro)) {//either we're forcing the update (retro==1) or something changed
525 $all_users[$result->uid][$group] = $val;
526 }
527 elseif (($group != $val) && (!isset($all_users[$result->uid][$group])) && (($val != $old['All users'][$group]) || $retro || $form_state['values']['roles']['All users']['unsub'])) {
528 //the group is not required by the current role, and another role has not yet required it, and either the setting has changed, or we're forcing it (retro==1)
529 $all_users[$result->uid][$group] = 0;
530 }
531 }
532 }
533 }
534 //drupal_set_message('<pre>All users1: ' .print_r($all_users, TRUE). '</pre>');
535
536 //then get any group admins and set groups to subscribe/unsubscribe
537 if (key_exists('Group admins', $hold_updates)) {
538 $sql = "SELECT u.uid, u.nid FROM {og_uid} u WHERE u.uid > 1 AND u.is_admin > 0 ORDER BY u.uid ASC";
539 $results = db_query($sql);
540 while ($result = db_fetch_object($results)) {
541 foreach ($hold_updates['Group admins'] as $group => $val) {
542 if (($group == $val) && (($val != $old[$role][$group]) || $retro)) { //the user admins at least one group
543 $all_users[$result->uid][$group] = $val;
544 }
545 elseif (($group == $val) && !isset($all_users[$result->uid][$group]) && (($val != $old[$role][$group]) || $retro || $form_state['values']['roles']['All users']['unsub'])) { //the user doesn't admin any group
546 $all_users[$result->uid][$group] = 0;
547 }
548 elseif (($group != $val) && !isset($all_users[$result->uid][$group]) && (($val != $old[$role][$group]) || $retro || $form_state['values']['roles']['All users']['unsub'])) { //the user doesn't admin any group
549 $all_users[$result->uid][$group] = 0;
550 }
551 }
552 }
553 }
554 //drupal_set_message('<pre>All users2: ' .print_r($all_users, TRUE). '</pre>');
555
556 //last, go through each role, find the role's membership, set groups to subscribe/unsubscribe for each user of that role
557 foreach ($hold_updates as $role => $value) {
558 if ($role != 'All users' && $role != 'Group admins') {
559 $sql = "SELECT ur.uid, ur.rid FROM {users_roles} ur INNER JOIN {role} r ON ur.rid = r.rid WHERE r.name = '%s' AND ur.uid > 1 ORDER BY ur.uid ASC";
560 $results = db_query($sql, $role);
561 //done with og_user_roles module compatibility, continue with regular roles
562 while ($result = db_fetch_object($results)) {
563 foreach ($value as $group => $val) {
564 if (($group == $val) && (($val != $old[$role][$group]) || $retro)) {//the group is required for this role and it either changed or we're forcing ($retro==1) the save
565 $all_users[$result->uid][$group] = $val;
566 }
567 elseif (($group == $val) && isset($all_users[$result->uid][$group]) && ($all_users[$result->uid][$group] == 0)) {//we set it to zero above, but now need to set it to subscribe the group
568 $all_users[$result->uid][$group] = $val;
569 }
570 elseif (($group != $val) && !isset($all_users[$result->uid][$group]) && (($val != $old[$role][$group]) || $retro || $form_state['values']['roles']['All users']['unsub'])) {
571 $all_users[$result->uid][$group] = 0;
572 }
573 }
574 }
575 }
576 }
577 //drupal_set_message('<pre>All users3: ' .print_r($all_users, TRUE). '</pre>');
578
579 //finally, find which groups the user admins for, then subscribe and unsubscribe as needed, taking care not to unsubscribe an admin
580 foreach ($all_users as $user_id => $group_id) {
581 if (!is_array($all_users[$user_id]['owns'])) {
582 $all_users[$user_id]['owns'] = array();
583 $sql = "SELECT u.nid FROM {og_uid} u WHERE uid = %d AND is_admin > 0 ORDER BY u.nid ASC";
584 $results = db_query($sql, $user_id);
585 while ($result = db_fetch_object($results)) {
586 $all_users[$user_id]['owns'][] = $result->nid;
587 }
588 }
589 foreach ($group_id as $gid => $value) {
590 $sql = "SELECT * FROM {og_uid} u WHERE uid = %d AND nid = %d";
591 $results = db_fetch_object(db_query($sql, $user_id, $gid));
592 if (($gid == $value) && !$results) { //only subscribe if no subscription exists
593 og_save_subscription($gid, $user_id, array('is_active' => 1));
594 if (user_access('administer users')) {
595 drupal_set_message(t('The user %uid has been subscribed to group %gid.', array('%uid' => $user_id, '%gid' => $gid)));
596 }
597 }
598 elseif (($gid != $value) && $results && !in_array($gid, $all_users[$user_id]['owns'])) { //only unsubscribe if a subscription exists
599 og_delete_subscription($gid, $user_id);
600 if (user_access('administer users')) {
601 drupal_set_message(t('The user %uid has been unsubscribed from group %gid.', array('%uid' => $user_id, '%gid' => $gid)));
602 }
603 }
604 }
605 }
606
607
608 if (isset($form_state['values']['roles']['All users']['og_mandatory_additional_group'])) {
609 variable_set('og_mandatory_additional_group', $form_state['values']['roles']['All users']['og_mandatory_additional_group']);
610 }
611 else {
612 variable_set('og_mandatory_additional_group', 0);
613 }
614
615 foreach ($form_state['values']['roles'] as $group => $therest) {
616 if ($group != 'retro') {
617 $unsubs[$group] = $form_state['values']['roles'][$group]['unsub'];
618 }
619 }
620 if ($unsubs = serialize($unsubs)) {
621 variable_set('og_mandatory_unsubs', $unsubs);
622 }
623 else {
624 variable_set('og_mandatory_unsubs', array());
625 }
626
627 if ($error) {
628 drupal_set_message(t('There was an error processing the data. Try again, or contact your site administrator.'), 'error');
629 }
630 else {
631 drupal_set_message(t('The changes have been saved.'));
632 }
633 } //function og_multiple_mandatory_groups_by_role_admin_submit()
634
635 /**
636 * Callback function: Add groups to an availability list. These groups will be available to use as mandatory groups.
637 *
638 * @param integer $gid
639 * The GID of the group.
640 * @return mixed
641 */
642 function og_multiple_mandatory_groups_by_role_groups($gid = NULL) {
643 if ($admin = user_access('administer og mandatory groups by role')) {
644
645 $option_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);
646 while ($group = db_fetch_object($option_result)) {
647 $options[$group->nid] = $group->title;
648 switch ($group->selective) {
649 case OG_OPEN:
650 $options[$group->nid] .= ' | ' .t('Open group, users can unsubscribe.');
651 break;
652 case OG_CLOSED:
653 $options[$group->nid] .= ' | ' .t('Closed group, users can not leave.');
654 break;
655 }
656 }
657
658 $added = _og_multiple_mandatory_groups_by_groups_get_added();
659 foreach ($added as $group_id => $name) {//remove from the select, groups we're already working with
660 if (array_key_exists($group_id, $options)) {
661 unset($options[$group_id]);
662 }
663 }
664 $form['groups'] = array(
665 '#type' => 'select',
666 '#title' => t('Open or Closed Groups'),
667 '#description' => t('Any group added from this list will be automatically subscribed or unsubscribed as required with each change<br />
668 a) on the multiple user edit page (admin/user/user)<br />
669 b) on the user edit page (user/X/edit)<br />
670 c) on the user registration page (admin/user/user/create)<br />
671 Do not add any groups to the list for which you do not want this automatic feature.'),
672 '#options' => $options,
673 );
674 $form['submit'] = array(
675 '#type' => 'submit',
676 '#value' => t('Add group'),
677 );
678 }
679 return $form;
680 } // function og_multiple_mandatory_groups_by_role_groups()
681
682 /**
683 * Validate the group name before adding or saving it
684 *
685 * @param string $form_id
686 * @param mixed $form_state['values']
687 */
688 function og_multiple_mandatory_groups_by_role_groups_validate($form, &$form_state) {
689 if ($form_state['values']['groups']) {
690 //make sure the user isn't injecting something not on the list.
691 $check_options = _og_multiple_mandatory_groups_by_groups_get_added();
692 //if ($form_state['values']['op'] == t('Add group')) {
693 if (array_key_exists($form_state['values']['groups'], $check_options)) {
694 form_set_error('groups', t('The group name %name is already added. Please choose another group name.', array('%name' => $form_state['values']['groups']['name'])));
695 }
696 //}
697 }
698 else {
699 form_set_error('name', t('You must specify a valid group name.'));
700 }
701 }//function og_multiple_mandatory_groups_by_role_groups_validate()
702
703 /**
704 * Submit the group name and id to be added to the database.
705 *
706 * @param string $form_id
707 * @param mixed $form_state['values']
708 * @return string
709 */
710 function og_multiple_mandatory_groups_by_role_groups_submit($form, &$form_state) {
711 //if ($form_state['values']['op'] == t('Add group')) {
712 $sql = "SELECT og.nid, n.title FROM {og} og INNER JOIN {node} n ON og.nid = n.nid ORDER BY og.nid ASC";
713 $results = db_query($sql);
714 while ($result = db_fetch_object($results)) {
715 $all_groups[$result->nid] = $result -> title;
716 }
717 db_query("INSERT INTO {og_multi_mand_groups_role_groups} (gid, group_name) VALUES (%d, '%s')", $form_state['values']['groups'], $all_groups[$form_state['values']['groups']]);
718 drupal_set_message(t('The group has been added.'));
719 //}
720 $form_state['redirect'] = 'admin/og/og_multiple_mandatory_groups_by_role/groups';
721 } //function og_multiple_mandatory_groups_by_role_groups_submit()
722
723 /**
724 * Theme for the add page of the main function.
725 *
726 * <p>This lists all of the current roles with links to edit them or their assigned groups, followed with
727 * a field and button to add a new role name to the list.</p>
728 * @param mixed $form
729 * @return mixed
730 */
731 function theme_og_multiple_mandatory_groups_by_role_groups($form) {
732 $rows = array();
733 $header = array(t('Group'), array('data' => t('Operations')));
734 $added_groups = _og_multiple_mandatory_groups_by_groups_get_added();
735 foreach ($added_groups as $gid => $name) {
736 $delete = l(t('delete'), 'admin/og/og_multiple_mandatory_groups_by_role/groups_delete/' .$gid);
737 $delete_uns = l(t('delete and unsubscribe'), 'admin/og/og_multiple_mandatory_groups_by_role/groups_delete_unsubscribe/' .$gid);
738 $rows[] = array($name, $delete, $delete_uns);
739 }
740 $rows[] = array(drupal_render($form['groups']), array('data' => drupal_render($form['submit']), colspan => 3));
741
742 $output = drupal_render($form);
743 $output .= theme('table', $header, $rows);
744
745 return $output;
746 } //function theme_og_multiple_mandatory_groups_by_role_groups()
747
748 /**
749 * Returns a confirmation page for deleting a group from the mandatory group availability table.
750 *
751 * @param integer $gid Value of group to be deleted; passed in from path.
752 * @return array $form The GID is passed to the submit.
753 */
754 function og_multiple_mandatory_groups_by_role_group_confirm_delete($gid= NULL) {
755 if ($gid == NULL) {
756 drupal_goto('admin/og/og_multiple_mandatory_groups_by_role/groups');
757 return;
758 }
759 $sql = "SELECT ommgrg.gid, ommgrg.group_name FROM {og_multi_mand_groups_role_groups} ommgrg WHERE ommgrg.gid = %d";
760 $result = db_fetch_object(db_query($sql, $gid));
761 $form['name'] = array('#type' => 'value', '#value' => $result->group_name);
762 $form['gid'] = array('#type' => 'value', '#value' => $gid);
763 $return_address = 'admin/og/og_multiple_mandatory_groups_by_role/groups';
764 $form = confirm_form($form, t('Are you sure you want to delete the group %name from the mandatory group availability list?',
765 array('%name' => $result->group_name)), $return_address,
766 t('Deleting a group here will make that group no longer mandatory under any role. Users will NOT be unsubscribed from the group(s). This action cannot be undone.'), t('Delete'), t('Cancel'));
767 return $form;
768 } // function og_multiple_mandatory_groups_by_role_group_confirm_delete()
769
770 /**
771 * Implementation of forms api call. Deletes a group from the og_multi_mand_groups_role_groups table after confirmation.
772 *
773 * @param string $form_id
774 * @param mixed $form_state['values']
775 * @return string $return_address The path to return to after we've deleted the record.
776 */
777 function og_multiple_mandatory_groups_by_role_group_confirm_delete_submit($form, &$form_state) {
778 //update the monitored roles' mandatory lists
779 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr ORDER BY ommgr.rid";
780 $results = db_query($sql);
781 while ($result = db_fetch_object($results)) {
782 $mand_groups = unserialize($result->mand_groups);
783 unset($mand_groups[$form_state['values']['gid']]);
784 $query = db_query("UPDATE {og_multi_mand_groups_role} SET mand_groups = '%s' WHERE role_name = '%s'", serialize($mand_groups), $result->role_name);
785 }
786 $dbquery = db_query('DELETE FROM {og_multi_mand_groups_role_groups} WHERE gid = %d', $form_state['values']['gid']);
787 if ($dbquery) {
788 drupal_set_message(t('The group %gid has been removed from the mandatory group availability table.', array('%gid' => $form_state['values']['name'])));
789 watchdog('content', 'og_multiple_mandatory_groups_by_role_groups: deleted %gid.', array('%gid' => $form_state['values']['name']));
790 }
791 $return_address = 'admin/og/og_multiple_mandatory_groups_by_role/groups';
792 $form_state['redirect'] = $return_address;
793 } // function og_multiple_mandatory_groups_by_role_group_confirm_delete()
794
795 /**
796 * Returns a confirmation page for deleting a group from the mandatory group availability table.
797 *
798 * @param integer $gid Value of group to be deleted; passed in from path.
799 * @return array $form The GID is passed to the submit.
800 */
801 function og_multiple_mandatory_groups_by_role_group_confirm_delete_unsubscribe($gid= NULL) {
802 if ($gid == NULL) {
803 drupal_goto('admin/og/og_multiple_mandatory_groups_by_role/groups');
804 return;
805 }
806 $sql = "SELECT ommgrg.gid, ommgrg.group_name FROM {og_multi_mand_groups_role_groups} ommgrg WHERE ommgrg.gid = %d";
807 $result = db_fetch_object(db_query($sql, $gid));
808 $form['name'] = array('#type' => 'value', '#value' => $result->group_name);
809 $form['gid'] = array('#type' => 'value', '#value' => $gid);
810 $return_address = 'admin/og/og_multiple_mandatory_groups_by_role/groups';
811 $form = confirm_form($form, t('Are you sure you want to delete the group %name from the mandatory group availability list?',
812 array('%name' => $result->group_name)), $return_address,
813 t('Deleting a group here will make that group no longer mandatory under any role. Users will be unsubscribed from the group(s). This action cannot be undone.'), t('Delete'), t('Cancel'));
814 return $form;
815 } // function og_multiple_mandatory_groups_by_role_group_confirm_delete_unsubscribe()
816
817 /**
818 * Implementation of forms api call. Deletes a group from the og_multi_mand_groups_role_groups table after confirmation.
819 *
820 * @param string $form_id
821 * @param mixed $form_state['values']
822 * @return string $return_address The path to return to after we've deleted the record.
823 */
824 function og_multiple_mandatory_groups_by_role_group_confirm_delete_unsubscribe_submit($form, &$form_state) {
825 //unsubscribe users that have been in this group since we have been taking care of auto-subscibing/unsubscribing for this group anyway, don't unsubscribe the owner
826 $sql = "SELECT ou.uid, ou.nid FROM {og_uid} ou LEFT JOIN {node} n ON ou.nid = n.nid AND ou.uid = n.uid WHERE ou.nid = %d AND n.uid IS NULL ORDER BY ou.uid ASC";
827 $results = db_query($sql, $form_state['values']['gid']);
828 while ($result = db_fetch_object($results)) {
829 og_delete_subscription($result->nid, $result->uid);
830 drupal_set_message(t('The user %uid has been unsubscribed from group %gid.', array('%uid' => $result->uid, '%gid' => $form_state['values']['name'])));
831 }
832 //update the monitored roles' mandatory lists
833 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr ORDER BY ommgr.rid";
834 $results = db_query($sql);
835 while ($result = db_fetch_object($results)) {
836 $mand_groups = unserialize($result->mand_groups);
837 unset($mand_groups[$form_state['values']['gid']]);
838 $query = db_query("UPDATE {og_multi_mand_groups_role} SET mand_groups = '%s' WHERE role_name = '%s'", serialize($mand_groups), $result->role_name);
839 }
840 $dbquery = db_query('DELETE FROM {og_multi_mand_groups_role_groups} WHERE gid = %d', $form_state['values']['gid']);
841 if ($dbquery) {
842 drupal_set_message(t('The group %gid has been removed from the mandatory group availability table.', array('%gid' => $form_state['values']['name'])));
843 watchdog('content', 'og_multiple_mandatory_groups_by_role_groups: deleted %gid.', array('%gid' => $form_state['values']['name']));
844 }
845 $return_address = 'admin/og/og_multiple_mandatory_groups_by_role/groups';
846 $form_state['redirect'] = $return_address;
847 } // function og_multiple_mandatory_groups_by_role_group_confirm_delete_unsubscribe()
848
849 //Nuts and bolts, Meat and potatoes section---------------------------------------------------------------
850 //This section contains all processing to make sure that as users are un/assigned roles, that they are
851 //subscribed to the correct groups. Also, we need to take care of newly registered users. We also need
852 //to monitor changes to what we are monitoring. If a role is added to our list, we need to make sure that
853 //those who already have the role are subscribed to the group... etc. etc.
854
855 /**
856 * Implementation of hook_nodeapi
857 *
858 * @param mixed &$node The node being operated on.
859 * @param string $op The operation being performed.
860 * @param array $a3 Used with $op == 'view' or 'validate'
861 * @param array $a4 Used with $op == 'view'
862 */
863 function og_multiple_mandatory_groups_by_role_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
864 switch ($op) {
865 case 'insert':
866 $og_node_types = variable_get('og_node_types', array());
867 if (in_array($node->type, $og_node_types)) {
868 global $user;
869 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr WHERE ommgr.rid = 0";
870 $result = db_fetch_object(db_query($sql));
871 $mandatory_groups = unserialize($result->mand_groups);
872 if (array_sum($mandatory_groups) != 0) {
873 foreach ($mandatory_groups as $key => $value) {
874 if ($key == $value) {
875 og_save_subscription($value, $user->uid, array('is_active' => 1));
876 }
877 }
878 }
879 }
880 break;
881 case 'update':
882 if ($group = db_fetch_object(db_query("SELECT gid FROM {og_multi_mand_groups_role_groups} WHERE gid = %d", $node->nid))) {
883 $sql = "UPDATE {og_multi_mand_groups_role_groups} SET group_name = '%s' WHERE gid = %d";
884 $result = db_query($sql, $node->title, $group->gid);
885 if (!$result) {
886 drupal_set_message('Problem updating the group name for the <em>OG Multiple Mandatory Groups by Role</em> table.', 'error');
887 }
888 }
889 break;
890 case 'delete':
891 $og_node_types = variable_get('og_node_types', array());
892 if (in_array($node->type, $og_node_types)) {
893 //if the owner of the group doesn't have any more groups, unsubscribe them from the "Group admins" mandatory groups
894 $sql = "SELECT u.uid, u.nid FROM {og_uid} u WHERE u.uid = %d AND u.is_admin > 0";
895 $results = db_query($sql, $node->uid);
896 $still_admin = FALSE;
897 while ($result = db_fetch_object($results)) {
898 if ($result->nid != $node->nid) {
899 $still_admin = TRUE;
900 }
901 }
902 if ($still_admin == FALSE) {
903 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr WHERE ommgr.rid = 0";
904 $result = db_fetch_object(db_query($sql));
905 $mandatory_groups = unserialize($result->mand_groups);
906 if (array_sum($mandatory_groups) != 0) {
907 foreach ($mandatory_groups as $key => $value) {
908 if ($key == $value) {
909 og_delete_subscription($value, $node->uid);
910 }
911 }
912 }
913 }
914 //we need to clean up our own data when a group is deleted
915 $sql = "SELECT gid FROM {og_multi_mand_groups_role_groups} WHERE gid = %d";
916 if ($result = db_fetch_object(db_query($sql, $node->nid))) {
917 //update the monitored roles' mandatory lists
918 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr ORDER BY ommgr.rid";
919 $results = db_query($sql);
920 while ($result = db_fetch_object($results)) {
921 $mand_groups = unserialize($result->mand_groups);
922 unset($mand_groups[$node->nid]);
923 $query = db_query("UPDATE {og_multi_mand_groups_role} SET mand_groups = '%s' WHERE role_name = '%s'", serialize($mand_groups), $result->role_name);
924 }
925 $dbquery = db_query('DELETE FROM {og_multi_mand_groups_role_groups} WHERE gid = %d', $node->nid);
926 if ($dbquery) {
927 drupal_set_message(t('The group %gid has been removed from the mandatory group availability table.', array('%gid' => $node->title)));
928 watchdog('content', 'og_multiple_mandatory_groups_by_role_groups: deleted %gid.', array('%gid' => $node->title));
929 }
930 }
931 }
932 break;
933 }
934 } // function og_multiple_mandatory_groups_by_role_nodeapi()
935
936 /**
937 * Implementation of hook_form_alter
938 *
939 * if we want a user to join at least one group other than the mandatory ones,
940 * alter the user_register form and add the functionality for them to sign up
941 * for those groups
942 * @param array &$form
943 * @param array $form_state
944 * @param string $form_id
945 */
946 function og_multiple_mandatory_groups_by_role_form_alter(&$form, $form_state, $form_id) {
947
948 if ($form_id == 'user_register' && isset($form['og_register'])) {
949 $group_count = count($form['og_register']['og_register']['#options']);
950 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr WHERE ommgr.rid = -1";
951 $result = db_fetch_object(db_query($sql));
952 $mandatory_groups = unserialize($result->mand_groups);
953 if ($mandatory_groups && (array_sum($mandatory_groups) != 0)) {
954 $form['og_register']['og_multiple_mandatory_groups_by_roles_in_form'] = array(
955 '#type' => 'fieldset',
956 '#title' => t('The following groups are mandatory:'),
957 '#weight' => '-1',
958 '#tree' => TRUE,
959 );
960 //loop through the mandatory groups, use those whose values match the key
961 foreach ($mandatory_groups as $key => $value) {
962
963 /*check if $mandatory_group ($key) has a matching value.*/
964 if ($key == $value) {
965 //with all groups now stored in og_multiple_mandatory_groups_by_role, only set $mandatory_group_in_form if $key = $value
966 $mandatory_group_in_form = TRUE;
967 $form['og_register']['og_mandatory_in_form'] = array(
968 '#type' => 'value',
969 '#value' => $mandatory_group_in_form,
970 );
971 $title = $form['og_register']['og_register']['#options'][$key]. ' ' .t('(This group is mandatory)');
972 $form['og_register']['og_multiple_mandatory_groups_by_roles_in_form'][$key] = array(
973 '#type' => 'checkbox',
974 '#title' => $title,
975 '#value' => $value,
976 '#disabled' => TRUE,
977 );
978 unset($form['og_register']['og_register']['#options'][$key]);
979 $group_count--;
980 }
981 //preserve the keys? probably, since our keys need to match our values. hence the TRUE below.
982 $form['og_register'] = array_reverse($form['og_register'], TRUE);
983 }
984 }
985
986 if ($group_count > 0 && variable_get('og_mandatory_additional_group', FALSE)) {
987 if ($mandatory_groups && $mandatory_group_in_form) {
988 $form['og_register']['minimum'] = array ('#value' => t('You must join at least one additional (non-mandatory) group.'));
989 }
990 else {
991 $form['og_register']['minimum'] = array ('#value' => t('You must join at least one group.'));
992 }
993 }
994 }
995
996 if ($form_id == 'user_admin_role') { //React to a role deletion and update our data accordingly.
997 $id = arg(4);
998 if ($id) {
999 $form['#submit'][] = 'og_multiple_mandatory_groups_by_role_confirm_delete_unsub';
1000 }
1001 }
1002 } // function og_multiple_mandatory_groups_by_role_form_alter()
1003
1004 /**
1005 * Implementation of hook_user
1006 *
1007 * connect the mandatory groups with the users. update the related tables accordingly.
1008 * @param string $op
1009 * @param mixed $&$edit
1010 * @param mixed &$account
1011 * @param unknown_type $category
1012 */
1013 function og_multiple_mandatory_groups_by_role_user($op, &$edit, &$account, $category = NULL) {
1014 if (!module_exists('og')) {
1015 return;
1016 }
1017 switch ($op) {
1018 case 'validate':
1019 if (isset($edit['og_register']) && variable_get('og_mandatory_additional_group', FALSE)) { //only present during registration
1020
1021 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, '')
1022 if ($edit['og_mandatory_in_form']) {
1023 form_set_error('og_register', "You must join at least one group in addition to the mandatory groups");
1024 }
1025 else {
1026 form_set_error('og_register', "You must join at least one group");
1027 }
1028 }
1029 }
1030 break;
1031
1032 case 'insert':
1033 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr WHERE ommgr.rid = -1";
1034 $result = db_fetch_object(db_query($sql));
1035 $mandatory_groups = unserialize($result->mand_groups);
1036 if (array_sum($mandatory_groups) != 0) {
1037 foreach ($mandatory_groups as $key => $value) {
1038 if ($key == $value) {
1039 og_save_subscription($value, $account->uid, array('is_active' => 1));
1040 }
1041 }
1042 }
1043
1044 if (isset($edit['roles'])) {
1045 foreach ($edit['roles'] as $key_r => $value_r) {
1046 if ($key_r == $value_r) {
1047 $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr WHERE ommgr.rid = %d";
1048 if($result = db_fetch_object(db_query($sql, $key_r))) {
1049 $mandatory_groups = unserialize($result->mand_groups);
1050 foreach ($mandatory_groups as $key_g => $value_g) {
1051 if ($key_g == $value_g) {
1052 og_save_subscription($value_g, $account->uid,