/[drupal]/contributions/modules/og_user_roles/og_user_roles.pages.inc
ViewVC logotype

Contents of /contributions/modules/og_user_roles/og_user_roles.pages.inc

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


Revision 1.4 - (show annotations) (download) (as text)
Fri Aug 14 12:00:30 2009 UTC (3 months, 1 week ago) by sun
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +3 -6 lines
File MIME type: text/x-php
#519794 by stella, sun: Added option for group nodes to allow group admins to define default role for new members.
1 <?php
2 // $Id: og_user_roles.pages.inc,v 1.3 2009/06/08 13:00:05 sun Exp $
3
4 /**
5 * @file
6 * Page callbacks for OG user roles.
7 */
8
9 /**
10 * Form builder for administrative settings.
11 */
12 function og_user_roles_admin_settings() {
13 // Get list of all og-enabled node types.
14 $group_types = og_get_types('group');
15 $types = array();
16 foreach ($group_types as $type) {
17 $types[$type] = node_get_types('name', $type);
18 }
19
20 // Get list of roles, not counting authenticated and anonymous user.
21 $all_roles = user_roles();
22 foreach ($all_roles as $rid => $role) {
23 if ($rid > DRUPAL_AUTHENTICATED_RID) {
24 $roles[$rid] = $role;
25 }
26 }
27 // If no assignable roles, advise user to add some.
28 if (empty($roles)) {
29 $form['og_user_roles'] = array(
30 '#value' => '<p>' . t('No assignable roles were found. User roles can be created on the <a href="@admin-user-roles-url">Roles</a> configuration page.', array('@admin-user-roles-url' => url('admin/user/roles'))) . '</p>',
31 );
32 return $form;
33 }
34
35 // Create a fieldset for each group type containing role selections.
36 $form['og_user_roles'] = array(
37 '#type' => 'fieldset',
38 '#title' => t('Assignable roles'),
39 );
40 foreach ($types as $type => $name) {
41 $form['og_user_roles']["og_user_roles_roles_$type"] = array(
42 '#type' => 'checkboxes',
43 '#title' => t('Roles for %name groups', array('%name' => $name)),
44 '#options' => $roles,
45 '#default_value' => variable_get("og_user_roles_roles_$type", array()),
46 );
47 }
48
49 $form['og_user_roles_defaults'] = array(
50 '#type' => 'fieldset',
51 '#title' => t('Default role assignments'),
52 '#description' => t('Select a role to automatically assign to users who join a group.'),
53 '#collapsible' => TRUE,
54 );
55 $form['og_user_roles_defaults']['og_user_roles_default_admin_role'] = array(
56 '#type' => 'select',
57 '#title' => t('Default role for new group administrators'),
58 '#options' => array(0 => t('None')) + $roles,
59 '#default_value' => variable_get('og_user_roles_default_admin_role', 0),
60 '#description' => t('This role can only be unassigned by removing a user from the administrators of a group.'),
61 );
62 $form['og_user_roles_defaults']['og_user_roles_default_role'] = array(
63 '#type' => 'select',
64 '#title' => t('Default role for new group members'),
65 '#options' => array(0 => t('None')) + $roles,
66 '#default_value' => variable_get('og_user_roles_default_role', 0),
67 '#description' => t('This setting can be overridden at the group level by group admins with the %permission permission.', array('%permission' => 'override group default role')),
68 );
69
70 return system_settings_form($form);
71 }
72
73 /**
74 * Menu callback; displays members and role selection
75 */
76 function og_user_roles_page($node) {
77 drupal_set_title(t('Member roles for !title', array('!title' => l($node->title, "node/$node->nid"))));
78
79 $roles = og_user_roles_get_group_roles($node->type);
80
81 // Retrieve list of all group users
82 $sql = og_list_users_sql(0, 0, 'ou.is_admin DESC, ou.is_active ASC, u.name ASC');
83 $result = pager_query($sql, 100, 0, NULL, $node->nid);
84 $pager = theme('pager', NULL, 100, 0);
85
86 $output = $pager;
87 $output .= drupal_get_form('og_user_roles_page_form', $node, $roles, $result);
88 $output .= $pager;
89
90 return $output;
91 }
92
93 /**
94 * Form builder for group user roles assignment.
95 */
96 function og_user_roles_page_form($form_state, $node, $roles, $result) {
97 $form['#node'] = $node;
98 // Used for theming.
99 $form['#id'] = 'og-user-roles';
100 $form['#roles'] = $roles;
101
102 // Unset role titles.
103 foreach ($roles as $rid => $name) {
104 $roles[$rid] = '';
105 }
106
107 $form['user_roles'] = array('#tree' => TRUE);
108 while ($account = db_fetch_object($result)) {
109 $form['user_roles'][$account->uid]['user'] = array(
110 '#value' => theme('username', $account),
111 );
112 $form['user_roles'][$account->uid]['roles'] = array(
113 '#type' => 'checkboxes',
114 '#default_value' => og_user_roles_get_roles_by_group($node->nid, $account->uid),
115 '#options' => $roles,
116 '#parents' => array('user_roles', $account->uid),
117 );
118 }
119 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
120
121 return $form;
122 }
123
124 /**
125 * Form submit handler for group user roles assignment form.
126 */
127 function og_user_roles_page_form_submit($form, &$form_state) {
128 $gid = $form['#node']->nid;
129
130 foreach ($form_state['values']['user_roles'] as $uid => $new_roles) {
131 og_user_roles_role_delete($gid, $uid);
132
133 $new_roles = array_filter($new_roles);
134 foreach ($new_roles as $rid => $checked) {
135 og_user_roles_role_add($gid, $uid, $rid);
136 }
137 }
138
139 drupal_set_message(t('The changes have been saved.'));
140 }
141
142 /**
143 * Render the group user roles assignment form like user permissions table.
144 */
145 function theme_og_user_roles_page_form($form) {
146 $header = array_merge(array(''), $form['#roles']);
147
148 $rows = array();
149 foreach (element_children($form['user_roles']) as $item) {
150 $row = array();
151 $row[] = drupal_render($form['user_roles'][$item]['user']);
152 foreach (element_children($form['user_roles'][$item]['roles']) as $role) {
153 $row[] = drupal_render($form['user_roles'][$item]['roles'][$role]);
154 }
155 $rows[] = $row;
156 }
157
158 $output = theme('table', $header, $rows);
159 $output .= drupal_render($form);
160 return $output;
161 }
162

  ViewVC Help
Powered by ViewVC 1.1.2