]> git.mjollnir.org Git - moodle.git/commitdiff
get_my_courses(): Support course-level login as
authormartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:07:21 +0000 (07:07 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:07:21 +0000 (07:07 +0000)
A walkthrough of course-login-as functionality shows that is
Just Works, except that get_my_courses() was showing all the
courses. So we fix it.

And cleanup load_all_capabilities() - things just work
transparently.

lib/accesslib.php
lib/datalib.php

index af6386780c8bc4dc315b950a9e77cda784f9e0c3..891d1752d48e80561c04dcf7415a699c3c46291d 100755 (executable)
@@ -1396,17 +1396,6 @@ function load_all_capabilities() {
 
         }
 
-        // when in "course login as" - load only course capabilitites (it may not always work as expected)
-        if (!empty($USER->realuser) and $USER->loginascontext->contextlevel != CONTEXT_SYSTEM) {
-            $children = array_keys(get_child_contexts($USER->loginascontext));
-            $children[] = $USER->loginascontext->id;
-            foreach ($USER->capabilities as $conid => $caps) {
-                if (!in_array($conid, $children)) {
-                    unset($USER->capabilities[$conid]);
-                }
-            }
-        }
-
         // handle role switching in courses
         if (!empty($USER->switchrole)) {
             foreach ($USER->switchrole as $contextid => $roleid) {
index 4b2ec8d140607a76d266ab6cdf9a9e30945ae993..76540e57834bcec0e821ca2c921606415f70cc02 100644 (file)
@@ -648,14 +648,22 @@ function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields=NUL
     // worthwhile...) and we may hit SQL parser limits
     // because we use IN()
     //
-    if ($userid === $USER->id 
-        && isset($USER->mycourses) 
-        && is_string($USER->mycourses)) {
-        if ($USER->mycourses === '') {
-            // empty str means: user has no courses
-            // ... so do the easy thing...
-            return array();
-        } else {
+    if ($userid === $USER->id) {
+        if (isset($USER->loginascontext)) {
+            // list _only_ this course
+            // anything else is asking for trouble...
+            $courseids = $USER->loginascontext->instanceid;
+        } elseif (isset($USER->mycourses) 
+                  && is_string($USER->mycourses)) {
+            if ($USER->mycourses === '') {
+                // empty str means: user has no courses
+                // ... so do the easy thing...
+                return array();
+            } else {
+                $courseids = $USER->mycourses;
+            }
+        }
+        if (isset($courseids)) {
             // The data massaging here MUST be kept in sync with 
             // get_user_courses_bycap() so we return
             // the same...
@@ -666,7 +674,7 @@ function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields=NUL
                     FROM {$CFG->prefix}course c
                     JOIN {$CFG->prefix}context ctx 
                       ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
-                    WHERE c.id IN ({$USER->mycourses})
+                    WHERE c.id IN ($courseids)
                     ORDER BY $sort";
             $rs = get_recordset_sql($sql);
             $courses = array();