From a14049f1079512e4e225a572419fdabfcb1ea61e Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 19 Sep 2006 20:39:55 +0000 Subject: [PATCH] metacourse role assignment fixes --- admin/roles/assign.html | 4 +-- admin/roles/assign.php | 77 +++++++++++++++++++++++++++++++++-------- lang/en_utf8/role.php | 3 ++ 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/admin/roles/assign.html b/admin/roles/assign.html index 3a7bb7facf..6afef8512c 100755 --- a/admin/roles/assign.html +++ b/admin/roles/assign.html @@ -5,10 +5,8 @@ if ($userid) { print (''); } -if ($courseid) { - print (''); -} ?> + diff --git a/admin/roles/assign.php b/admin/roles/assign.php index 739c8af61c..85d4934f1c 100755 --- a/admin/roles/assign.php +++ b/admin/roles/assign.php @@ -20,20 +20,34 @@ $userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs $courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs - if ($courseid) { - $course = get_record('course', 'id', $courseid); - } else { - $course = $SITE; - } - + $errors = array(); + if (! $context = get_context_instance_by_id($contextid)) { error("Context ID was incorrect (can't find it)"); } + + $inmeta = 0; + if ($context->aggregatelevel == CONTEXT_COURSE) { + $courseid = $context->instanceid; + if ($course = get_record('course', 'id', $courseid)) { + $inmeta = $course->metacourse; + } else { + error('Invalid course id'); + } + } else if (!empty($courseid)){ // we need this for user tabs in user context + if (!$course = get_record('course', 'id', $courseid)) { + error('Invalid course id'); + } + } else { + $courseid = SITEID; + $course = get_site(); + } + + require_login($courseid); require_capability('moodle/role:assign', $context); $assignableroles = get_assignable_roles($context); - /// Get some language strings @@ -45,7 +59,7 @@ $strcurrentcontext = get_string('currentcontext', 'role'); $strsearch = get_string('search'); $strshowall = get_string('showall'); - $strparticipants = get_string("participants"); + $strparticipants = get_string('participants'); @@ -99,8 +113,23 @@ foreach ($frm->addselect as $adduser) { $adduser = clean_param($adduser, PARAM_INT); - if (! role_assign($roleid, $adduser, 0, $context->id, $timestart, $timeend, $hidden)) { - error("Could not add user with id $adduser to this role!"); + $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 = get_record('user', 'id', $adduser, '','','','', 'id, firstname, lastname'); + $errors[] = get_string('metaassignerror', 'role', fullname($erruser)); + $allow = false; + } + } + } + if ($allow) { + if (! role_assign($roleid, $adduser, 0, $context->id, $timestart, $timeend, $hidden)) { + $errors[] = "Could not add user with id $adduser to this role!"; + } } } @@ -109,7 +138,15 @@ foreach ($frm->removeselect as $removeuser) { $removeuser = clean_param($removeuser, PARAM_INT); if (! role_unassign($roleid, $removeuser, 0, $context->id)) { - error("Could not remove user with id $removeuser from this role!"); + $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 = get_record('user', 'id', $removeuser, '','','','', 'id, firstname, lastname'); + $errors[] = get_string('metaunassignerror', 'role', fullname($erruser)); + $allow = false; + } } } @@ -162,9 +199,7 @@ if ($userid) { echo ''; } - if ($courseid) { - echo ''; - } + echo ''; echo ''.$strroletoassign.': '; choose_from_menu ($assignableroles, 'roleid', $roleid, get_string('listallroles', 'role'), $script='rolesform.submit()'); echo ''; @@ -173,8 +208,20 @@ include('assign.html'); print_simple_box_end(); + if (!empty($errors)) { + print_simple_box_start("center"); + foreach ($errors as $error) { + notify($error); + } + print_simple_box_end(); + } + } else { // Print overview table - + + // sync metacourse enrolments if needed + if ($inmeta) { + sync_metacourse($course); + } $userparam = (!empty($userid)) ? '&userid='.$userid : ''; $table->tablealign = 'center'; diff --git a/lang/en_utf8/role.php b/lang/en_utf8/role.php index b9e90e7623..bb7c47c7c0 100644 --- a/lang/en_utf8/role.php +++ b/lang/en_utf8/role.php @@ -15,6 +15,8 @@ $string['existingusers'] = '$a existing users'; $string['inherit'] = 'Inherit'; $string['listallroles'] = 'List all roles...'; $string['manageroles'] = 'Manage roles'; +$string['metaassignerror'] = 'Can not assign this role to user \"$a\" because Manage metacourse capability is needed.'; +$string['metaunassignerror'] = 'Role of user \"$a\" was automatically reassigned, please unassign the role in child courses instead.'; $string['nocapabilitiesincontext'] = 'No capabilities available in this context'; $string['overrideroles'] = 'Override roles'; $string['overrides'] = 'Overrides'; @@ -84,6 +86,7 @@ $string['course:viewscales'] = 'View scales'; $string['course:managescales'] = 'Manage scales'; $string['course:managegroups'] = 'Manage groups'; $string['course:managefiles'] = 'Manage files'; +$string['course:managemetacourse'] = 'Manage metacourse'; $string['course:managequestions'] = 'Manage questions'; $string['course:reset'] = 'Reset course'; $string['course:sectionvisibility'] = 'Control section visibility'; -- 2.39.5