diff --git a/admin/roles/assign.html b/admin/roles/assign.html deleted file mode 100755 index 099bb2dad4d..00000000000 --- a/admin/roles/assign.html +++ /dev/null @@ -1,111 +0,0 @@ -
- - diff --git a/admin/roles/assign.php b/admin/roles/assign.php index 8c78c30a9c9..a072fbf1ad6 100755 --- a/admin/roles/assign.php +++ b/admin/roles/assign.php @@ -1,28 +1,25 @@ dirroot.'/mod/forum/lib.php'); + require_once(dirname(__FILE__) . '/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); + require_once($CFG->dirroot.'/user/selector/lib.php'); + require_js(array('yui_yahoo', 'yui_dom', 'yui_event')); + require_js($CFG->admin . '/roles/roles.js'); - define("MAX_USERS_PER_PAGE", 5000); define("MAX_USERS_TO_LIST_PER_ROLE", 10); - $contextid = required_param('contextid',PARAM_INT); // context id - $roleid = optional_param('roleid', 0, PARAM_INT); // required role id - $add = optional_param('add', 0, PARAM_BOOL); - $remove = optional_param('remove', 0, PARAM_BOOL); - $showall = optional_param('showall', 0, PARAM_BOOL); - $searchtext = optional_param('searchtext', '', PARAM_RAW); // search string + $contextid = required_param('contextid',PARAM_INT); + $roleid = optional_param('roleid', 0, PARAM_INT); + $userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs + $courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs $hidden = optional_param('hidden', 0, PARAM_BOOL); // whether this assignment is hidden $extendperiod = optional_param('extendperiod', 0, PARAM_INT); $extendbase = optional_param('extendbase', 0, PARAM_INT); - $userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs - $courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs $errors = array(); - $baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/role/assign.php?contextid=' . $contextid; + $baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/assign.php?contextid=' . $contextid; if (!empty($userid)) { $baseurl .= '&userid='.$userid; } @@ -55,17 +52,23 @@ $course = clone($SITE); } +/// Check login and permissions. require_login($course); - require_capability('moodle/role:assign', $context); -/// needed for tabs.php - +/// These are needed early because of tabs.php $overridableroles = get_overridable_roles($context, ROLENAME_BOTH); list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true); -/// Get some language strings +/// Make sure this user can assign this role + if ($roleid && !isset($assignableroles[$roleid])) { + $a = stdClass; + $a->role = $roleid; + $a->context = $contextname; + print_error('cannotassignrolehere', '', get_context_url($context), $a); + } +/// Get some language strings $strpotentialusers = get_string('potentialusers', 'role'); $strexistingusers = get_string('existingusers', 'role'); $straction = get_string('assignroles', 'role'); @@ -74,6 +77,7 @@ $strparticipants = get_string('participants'); $strsearchresults = get_string('searchresults'); +/// Build the list of options for the enrolment period dropdown. $unlimitedperiod = get_string('unlimited'); $defaultperiod = $course->enrolperiod; for ($i=1; $i<=365; $i++) { @@ -81,6 +85,7 @@ $periodmenu[$seconds] = get_string('numdays', '', $i); } +/// Build the list of options for the starting from dropdown. $timeformat = get_string('strftimedate'); $today = time(); $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0); @@ -101,23 +106,11 @@ } } -/// Make sure this user can assign this role - - if ($roleid) { - if (!isset($assignableroles[$roleid])) { - error ('you can not override this role in this context'); - } - } - - if ($userid) { +/// Print the header and tabs + if ($context->contextlevel == CONTEXT_USER) { $user = $DB->get_record('user', array('id'=>$userid)); $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context)); - } - -/// Print the header and tabs - - if ($context->contextlevel == CONTEXT_USER) { /// course header $navlinks = array(); if ($courseid != SITEID) { @@ -141,243 +134,179 @@ $showroles = 1; $currenttab = 'assign'; include_once($CFG->dirroot.'/user/tabs.php'); + } else if ($context->contextlevel == CONTEXT_SYSTEM) { admin_externalpage_setup('assignroles'); admin_externalpage_print_header(); - } else if ($context->contextlevel==CONTEXT_COURSE and $context->instanceid == SITEID) { + + } else if ($context->contextlevel == CONTEXT_COURSE and $context->instanceid == SITEID) { admin_externalpage_setup('frontpageroles'); admin_externalpage_print_header(); $currenttab = 'assign'; include_once('tabs.php'); + } else { $currenttab = 'assign'; include_once('tabs.php'); } - -/// Process incoming role assignment - - if ($frm = data_submitted()) { - - if ($add and !empty($frm->addselect) and confirm_sesskey()) { - - foreach ($frm->addselect as $adduser) { - if (!$adduser = clean_param($adduser, PARAM_INT)) { - continue; - } - $allow = true; - if ($inmeta) { - if (has_capability('moodle/course:managemetacourse', $context, $adduser)) { - //ok - } else { - $managerroles = get_roles_with_capability('moodle/course:managemetacourse', CAP_ALLOW, $context); - if (!empty($managerroles) and !array_key_exists($roleid, $managerroles)) { - $erruser = $DB->get_record('user', array('id'=>$adduser), 'id, firstname, lastname'); - $errors[] = get_string('metaassignerror', 'role', fullname($erruser)); - $allow = false; - } - } - } - if ($allow) { - switch($extendbase) { - case 0: - $timestart = $course->startdate; - break; - case 3: - $timestart = $today; - break; - case 4: - $timestart = $course->enrolstartdate; - break; - case 5: - $timestart = $course->enrolenddate; - break; - } - - if($extendperiod > 0) { - $timeend = $timestart + $extendperiod; - } else { - $timeend = 0; - } - if (! role_assign($roleid, $adduser, 0, $context->id, $timestart, $timeend, $hidden)) { - $errors[] = "Could not add user with id $adduser to this role!"; - } - } - } - - $rolename = $DB->get_field('role', 'name', array('id'=>$roleid)); - add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id); - } else if ($remove and !empty($frm->removeselect) and confirm_sesskey()) { - - $sitecontext = get_context_instance(CONTEXT_SYSTEM); - $topleveladmin = false; - - // we only worry about this if the role has doanything capability at site level - if ($context->id == $sitecontext->id && $adminroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext)) { - foreach ($adminroles as $adminrole) { - if ($adminrole->id == $roleid) { - $topleveladmin = true; - } - } - } - - foreach ($frm->removeselect as $removeuser) { - $removeuser = clean_param($removeuser, PARAM_INT); - - if ($topleveladmin && ($removeuser == $USER->id)) { // Prevent unassigning oneself from being admin - continue; - } - - if (! role_unassign($roleid, $removeuser, 0, $context->id)) { - $errors[] = "Could not remove user with id $removeuser from this role!"; - } else if ($inmeta) { - sync_metacourse($courseid); - $newroles = get_user_roles($context, $removeuser, false); - if (!empty($newroles) and !array_key_exists($roleid, $newroles)) { - $erruser = $DB->get_record('user', array('id'=>$removeuser), 'id, firstname, lastname'); - $errors[] = get_string('metaunassignerror', 'role', fullname($erruser)); - $allow = false; - } - } - } - - $rolename = $DB->get_field('role', 'name', array('id'=>$roleid)); - add_to_log($course->id, 'role', 'unassign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id); - } else if ($showall) { - $searchtext = ''; - } - } - +/// Print heading. if ($isfrontpage) { print_heading_with_help(get_string('frontpageroles', 'admin'), 'assignroles'); } else { print_heading_with_help(get_string('assignrolesin', 'role', $contextname), 'assignroles'); } - if ($context->contextlevel==CONTEXT_SYSTEM) { +/// Print a warning if we are assigning system roles. + if ($context->contextlevel == CONTEXT_SYSTEM) { print_box(get_string('globalroleswarning', 'role')); } - if ($roleid) { + if ($roleid) { /// UI for assigning a particular role. - /// Get all existing participants in this context. - // Why is this not done with get_users??? - - if (!$contextusers = get_role_users($roleid, $context, false, 'u.id, u.firstname, u.lastname, u.email, ra.hidden')) { - $contextusers = array(); + /// Create the user selector objects. + $options = array('context' => $context, 'roleid' => $roleid); + if ($context->contextlevel > CONTEXT_COURSE) { + $potentialuserselector = new potential_assignees_below_course('addselect', $options); + } else { + $potentialuserselector = new potential_assignees_course_and_above('addselect', $options); + } + if ($context->contextlevel == CONTEXT_SYSTEM && is_admin_role($roleid)) { + $currentuserselector = new existing_role_holders_site_admin('removeselect', $options); + } else { + $currentuserselector = new existing_role_holders('removeselect', $options); } - $select = "username <> 'guest' AND deleted = 0 AND confirmed = 1"; - $params = array(); + /// Process incoming role assignments + if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) { + $userstoassign = $potentialuserselector->get_selected_users(); + if (!empty($userstoassign)) { - $usercount = $DB->count_records_select('user', $select, $params) - count($contextusers); - - $searchtext = trim($searchtext); - - if ($searchtext !== '') { // Search for a subset of remaining users - $LIKE = $DB->sql_ilike(); - $FULLNAME = $DB->sql_fullname(); - - $select .= " AND ($FULLNAME $LIKE :search1 OR email $LIKE :search2) "; - $params['search1'] = "%$searchtext%"; - $params['search2'] = "%$searchtext%"; - } - - if ($context->contextlevel > CONTEXT_COURSE) { // mod or block (or group?) - - /************************************************************************ - * * - * context level is above or equal course context level * - * in this case we pull out all users matching search criteria (if any) * - * * - * MDL-11324 * - * a mini get_users_by_capability() call here, this is done instead of * - * get_users_by_capability() because * - * 1) get_users_by_capability() does not deal with searching by name * - * 2) exceptions array can be potentially large for large courses * - * 3) $DB->get_recordset_sql() is more efficient * - * * - ************************************************************************/ - - if ($possibleroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context)) { - - $doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, get_context_instance(CONTEXT_SYSTEM)); - - $validroleids = array(); - foreach ($possibleroles as $possiblerole) { - if (isset($doanythingroles[$possiblerole->id])) { // We don't want these included - continue; + foreach ($userstoassign as $adduser) { + $allow = true; + if ($inmeta) { + if (has_capability('moodle/course:managemetacourse', $context, $adduser->id)) { + //ok + } else { + $managerroles = get_roles_with_capability('moodle/course:managemetacourse', CAP_ALLOW, $context); + if (!empty($managerroles) and !array_key_exists($roleid, $managerroles)) { + $erruser = $DB->get_record('user', array('id'=>$adduser->id), 'id, firstname, lastname'); + $errors[] = get_string('metaassignerror', 'role', fullname($erruser)); + $allow = false; + } + } } - if ($caps = role_context_capabilities($possiblerole->id, $context, 'moodle/course:view')) { // resolved list - if (isset($caps['moodle/course:view']) && $caps['moodle/course:view'] > 0) { // resolved capability > 0 - $validroleids[] = $possiblerole->id; + + if ($allow) { + switch($extendbase) { + case 0: + $timestart = $course->startdate; + break; + case 3: + $timestart = $today; + break; + case 4: + $timestart = $course->enrolstartdate; + break; + case 5: + $timestart = $course->enrolenddate; + break; + } + + if($extendperiod > 0) { + $timeend = $timestart + $extendperiod; + } else { + $timeend = 0; + } + if (! role_assign($roleid, $adduser->id, 0, $context->id, $timestart, $timeend, $hidden)) { + $a = new stdClass; + $a->role = $rolenames[$roleid]; + $a->user = fullname($adduser); + $errors[] = get_string('assignerror', 'role', $a); } } } - if ($validroleids) { - $roleids = '('.implode(',', $validroleids).')'; + $potentialuserselector->invalidate_selected_users(); + $currentuserselector->invalidate_selected_users(); - $fields = "SELECT u.id, u.firstname, u.lastname, u.email"; - $countfields = "SELECT COUNT('x')"; - - $sql = " FROM {user} u - JOIN {role_assignments} ra ON ra.userid = u.id - JOIN {role} r ON r.id = ra.roleid - WHERE ra.contextid ".get_related_contexts_string($context)." - AND $select AND ra.roleid in $roleids - AND u.id NOT IN ( - SELECT u.id - FROM {role_assignments} r, {user} u - WHERE r.contextid = :contextid - AND u.id = r.userid - AND r.roleid = :roleid)"; - $params['contextid'] = $contextid; - $params['roleid'] = $roleid; - - $availableusers = $DB->get_recordset_sql("$fields $sql", $params); - $usercount = $DB->count_records_sql("$countfields $sql", $params); - - } else { - $availableusers = array(); - $usercount = 0; - } + $rolename = $DB->get_field('role', 'name', array('id'=>$roleid)); + add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id); } - - } else { - - /************************************************************************ - * * - * context level is above or equal course context level * - * in this case we pull out all users matching search criteria (if any) * - * * - ************************************************************************/ - - /// MDL-11111 do not include user already assigned this role in this context as available users - /// so that the number of available users is right and we save time looping later - $fields = "SELECT id, firstname, lastname, email"; - $countfields = "SELECT COUNT('x')"; - - $sql = " FROM {user} - WHERE $select - AND id NOT IN ( - SELECT u.id - FROM {role_assignments} r, {user} u - WHERE r.contextid = :contextid - AND u.id = r.userid - AND r.roleid = :roleid)"; - $order = "ORDER BY lastname ASC, firstname ASC"; - - $params['contextid'] = $contextid; - $params['roleid'] = $roleid; - - $availableusers = $DB->get_recordset_sql("$fields $sql $order", $params); - $usercount = $DB->count_records_sql("$countfields $sql", $params); } - print_simple_box_start('center'); - include('assign.html'); - print_simple_box_end(); + /// Process incoming role unassignments + if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) { + $userstounassign = $currentuserselector->get_selected_users(); + if (!empty($userstounassign)) { + + foreach ($userstounassign as $removeuser) { + if (! role_unassign($roleid, $removeuser->id, 0, $context->id)) { + $a = new stdClass; + $a->role = $rolenames[$roleid]; + $a->user = fullname($removeuser); + $errors[] = get_string('unassignerror', 'role', $a); + } else if ($inmeta) { + sync_metacourse($courseid); + $newroles = get_user_roles($context, $removeuser->id, false); + if (empty($newroles) || array_key_exists($roleid, $newroles)) { + $errors[] = get_string('metaunassignerror', 'role', fullname($removeuser)); + } + } + } + + $potentialuserselector->invalidate_selected_users(); + $currentuserselector->invalidate_selected_users(); + + $rolename = $DB->get_field('role', 'name', array('id'=>$roleid)); + add_to_log($course->id, 'role', 'unassign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id); + } + } + + /// Print the form. + check_theme_arrows(); +?> + + +'; @@ -408,7 +337,7 @@ } // Get the names of role holders for roles with between 1 and MAX_USERS_TO_LIST_PER_ROLE users, - // and so determine whether to show the extra column. + // and so determine whether to show the extra column. $rolehodlernames = array(); $strmorethanmax = get_string('morethan', 'role', MAX_USERS_TO_LIST_PER_ROLE); $showroleholders = false; diff --git a/admin/roles/roles.js b/admin/roles/roles.js index 2f85336f0e2..c1588116c97 100644 --- a/admin/roles/roles.js +++ b/admin/roles/roles.js @@ -111,3 +111,17 @@ cap_table_filter = { } } }; + +function init_add_assign_page() { + var addselect = user_selector.get('addselect'); + document.getElementById('add').disabled = addselect.is_selection_empty(); + addselect.subscribe('selectionchanged', function(isempty) { + document.getElementById('add').disabled = isempty; + }); + + var removeselect = user_selector.get('removeselect'); + document.getElementById('remove').disabled = removeselect.is_selection_empty(); + removeselect.subscribe('selectionchanged', function(isempty) { + document.getElementById('remove').disabled = isempty; + }); +} \ No newline at end of file diff --git a/group/members.php b/group/members.php index 14f5daa4df6..daba4070b61 100644 --- a/group/members.php +++ b/group/members.php @@ -94,7 +94,7 @@ check_theme_arrows();+ |
@@ -106,7 +106,7 @@ check_theme_arrows(); |
- + |
diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index dbaf14e9335..cf47e555cf4 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -22,6 +22,7 @@ $string['cannotaddrss'] = 'You do not have permission to add rss feeds'; $string['cannotaddmodule'] = '$a module could not be added to the module list!'; $string['cannotaddnewmodule'] = 'Could not add a new module of $a'; $string['cannotaddnewinstance'] = 'Could not add a new instance of $a'; +$string['cannotassignrolehere'] = 'You are not allowed to assign this role (id = $a->roleid) in this context ($a->context)'; $string['cannotsaveconfig'] = 'Problem saving config \"$a->name\" as \"$a->value\" for plugin \"$a->plugin\"'; $string['cannotsavecomment'] = 'Cannot save comment'; $string['cannotsavefile'] = 'Cannot save the file \"$a\"!'; diff --git a/lang/en_utf8/role.php b/lang/en_utf8/role.php index 15747e54284..b6177efd22f 100644 --- a/lang/en_utf8/role.php +++ b/lang/en_utf8/role.php @@ -8,6 +8,7 @@ $string['allowassign'] = 'Allow role assignments'; $string['allowoverride'] = 'Allow role overrides'; $string['allsiteusers'] = 'All site users'; $string['assignanotherrole'] = 'Assign another role'; +$string['assignerror'] = 'Error while assigning the role $a->role to user $a->user.'; $string['assignroles'] = 'Assign roles'; $string['assignrolesin'] = 'Assign roles in $a'; $string['assignglobalroles'] = 'Assign system roles'; @@ -55,6 +56,7 @@ $string['course:viewhiddenuserfields'] = 'View hidden user fields'; $string['course:viewparticipants'] = 'View participants'; $string['course:viewscales'] = 'View scales'; $string['course:visibility'] = 'Hide/show courses'; +$string['createhiddenassign'] = 'Create hidden role assignments'; $string['deletecourseoverrides'] = 'Delete all overrides in course'; $string['deletelocalroles'] = 'Delete all local role assignments'; $string['grade:edit'] = 'Edit grades'; @@ -87,8 +89,11 @@ $string['explainpermissions'] = 'Explain permissions'; $string['explainpermissionsdetails'] = 'For capability $a->capability in context $a->context when logged in as $a->fullname.'; $string['explainpermissionsinfo'] = ' To use this table:
|