]> git.mjollnir.org Git - moodle.git/commitdiff
metacourse role assignment fixes
authorskodak <skodak>
Tue, 19 Sep 2006 20:39:55 +0000 (20:39 +0000)
committerskodak <skodak>
Tue, 19 Sep 2006 20:39:55 +0000 (20:39 +0000)
admin/roles/assign.html
admin/roles/assign.php
lang/en_utf8/role.php

index 3a7bb7facf9623bccd8ec4972c3461e29a662aaa..6afef8512cba66e0735f518ed12ef2f9bd854abe 100755 (executable)
@@ -5,10 +5,8 @@
 if ($userid) {
     print ('<input type="hidden" name="userid" value="'.$userid.'"/>');
 }
-if ($courseid) {
-    print ('<input type="hidden" name="courseid" value="'.$courseid.'" />');
-}
 ?>
+<input type="hidden" name="courseid" value="<?php p($courseid) ?>" />
 <input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
 <input type="hidden" name="contextid" value="<?php p($contextid) ?>" />
 <input type="hidden" name="roleid" value="<?php p($roleid) ?>" />
index 739c8af61c56d401e4f05fa782f5cdde2a6e7408..85d4934f1c30c673e560d9dea4227d66639ca128 100755 (executable)
     $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');
 
     
 
 
             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!";
+                    }
                 }
             }
 
             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;
+                    }
                 }
             }
 
         if ($userid) {
             echo '<input type="hidden" name="userid" value="'.$userid.'" />';
         }
-        if ($courseid) {
-            echo '<input type="hidden" name="courseid" value="'.$courseid.'" />';
-        }
+        echo '<input type="hidden" name="courseid" value="'.$courseid.'" />';
         echo '<input type="hidden" name="contextid" value="'.$context->id.'" />'.$strroletoassign.': ';
         choose_from_menu ($assignableroles, 'roleid', $roleid, get_string('listallroles', 'role'), $script='rolesform.submit()');
         echo '</div></form>';
         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)) ? '&amp;userid='.$userid : '';
 
         $table->tablealign = 'center';
index b9e90e762306f2ba1d47dbcab972f3c99eb8498f..bb7c47c7c0f59e8aec5438d7d5e2816279bb9fea 100644 (file)
@@ -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';