$user->uid)); foreach ($user->og_groups as $key => $sub) { if (og_user_roles_is_allowed($key)) { // Add another tab to the group subscribers page for admins to // configure member roles $items[] = array( 'path' => "og/users/$key/roles", 'callback' => 'og_user_roles_page', 'title' => t('configure og member roles'), 'callback arguments' => array($key), 'type' => MENU_LOCAL_TASK, 'access' => node_access('update', array('nid' => $key, 'status' => 1)), 'weight' => 5, ); } } } return $items; } /** * Implementation of hook_settings(). */ function og_user_roles_settings() { // Get list of all og-enabled node types $group_types = variable_get('og_node_types', array('og')); foreach ($group_types as $type) { $types[$type] = node_get_name($type); } // Get list of roles, not counting authenticated and anonymous user $all_roles = user_roles(); foreach ($all_roles as $rid => $role) { if ($rid > 2) { $roles[$rid] = $role; } } // If no assignable roles, advise user to add some. if (!count($roles)) { $form['og_user_roles'] = array( '#value' => '

'. t('No assignable roles were found. Please create at least one role under administer >> access >> roles.', array('%access' => url('admin/access/roles'))) .'

', ); return $form; } // Create a fieldset for each group type containing role selections foreach ($types as $type => $name) { $form["og_user_roles_$type"] = array( '#type' => 'fieldset', '#title' => t('%type_name role options', array('%type_name' => theme('placeholder', $name))), ); $form["og_user_roles_$type"]["og_user_roles_roles_$type"] = array( '#type' => 'checkboxes', '#title' => t('Assignable roles'), '#options' => $roles, '#default_value' => variable_get("og_user_roles_roles_$type", array()), ); } return $form; } /** * Menu callback; displays members and role selection */ function og_user_roles_page($gid) { $output = ''; $node = node_load($gid); // Get roles associated with this group. We rebuild the associative // array because the settings form only passes RID and we need the name. $role_ids = variable_get("og_user_roles_roles_{$node->type}", array()); $all_roles = user_roles(); foreach ($role_ids as $rid => $checked) { if ($checked != 0) { $roles[$rid] = $all_roles[$rid]; } } if (is_array($roles)) { // Retrieve list of all group users $sql = og_list_users_sql(0); $result = pager_query($sql, 100, 0, NULL, $gid); $output .= theme('pager',NULL, 100); $form['user_roles'] = array('#tree' => TRUE); // Make sure form array isn't flattened while ($account = db_fetch_object($result)) { $form['user_roles']['users'][$account->uid] = array( '#type'=>'value', '#value' => $account->uid ); $form['user_roles']['roles'][$account->uid] = array( '#type' => 'checkboxes', '#options' => $roles, '#default_value' => _user_roles($account->uid, $roles), ); } // Output form to screen $form['submit'] = array('#type' => 'submit', '#value' => t('Save changes')); $output .= drupal_get_form('og_user_roles_page', $form); } else { drupal_set_message(t('No roles have been assigned as group roles yet.')); } print theme('page', $output); } /** * Process the form submission */ function og_user_roles_page_submit($form_id, $form_values) { if ( is_numeric(arg(2)) ) { $gid = (int)arg(2); } foreach ($form_values['user_roles']['roles'] as $uid => $roles) { foreach ($roles as $rid => $checked) { $exists = db_result(db_query("SELECT * FROM {og_users_roles} WHERE uid = %d AND rid = %d AND gid = %d", $uid, $rid, $gid)); if ($checked && !$exists) { db_query("INSERT INTO {og_users_roles} (uid, rid, gid) VALUES (%d, %d, %d)", $uid, $rid, $gid); } elseif (!$checked && $exists) { db_query("DELETE FROM {og_users_roles} WHERE uid = %d AND rid = %d AND gid = %d", $uid, $rid, $gid); } } } // Have to rebuild the menu here, in case new menu items were added menu_rebuild(); drupal_set_message(t('Your changes have been saved')); } /** * This checks to see what roles a current user has against a given set of roles. */ function _user_roles($uid, $roles = array()) { if ( is_numeric(arg(2)) ) { $gid = (int)arg(2); } $roles_output = array(); if (is_array($roles)) { foreach ($roles as $rid => $role) { $result = db_result(db_query("SELECT * FROM {og_users_roles} WHERE uid = %d AND rid = %d AND gid = %d", $uid, $rid, $gid)); if ($result) { $roles_output[$rid] = $rid; } } } return $roles_output; } /** * Theme function to render the table for the og_user_roles form. */ function theme_og_user_roles_page($form) { $output .= "\n" . '
' . "\n"; $output .= '
' . t('Here you can assign group roles to members. This will give that member permission of that role. It will apply to all posts of this group type.') . "
\n"; $header[] = array('data' => t('Users')); $header[] = array('data' => t('Roles'), 'colspan' => 2); $rows = array(); $i = 0; foreach ($form['user_roles']['users'] as $user_form) { $uid = $user_form['#value']; if ($uid) { $rows[$i][] = theme('username', user_load(array('uid'=> $uid))); $rows[$i][] = form_render($form['user_roles']['roles'][$uid]); $i++; } } $output .= theme_table($header, $rows, $attributes = array('id' => 'og-roles-table'), $caption = NULL); $output .= form_render($form['submit']); $output .= "
\n"; $output .= form_render($form); return $output; } /** * Determine if a group is allowed to configure member roles. * * @param $nid * A node ID * @return boolean * TRUE if this group type allows roles to be assigned, otherwise FALSE */ function og_user_roles_is_allowed($nid) { $node = node_load($nid); if (in_array($node->type, variable_get('og_node_types', array('og'))) && variable_get("og_user_roles_roles_$node->type", NULL)) { return TRUE; } else { return FALSE; } }