From: martinlanghoff <martinlanghoff>
Date: Wed, 19 Sep 2007 07:07:21 +0000 (+0000)
Subject: get_my_courses(): Support course-level login as
X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=ae1555ae2e266d3f5add559ad7039cd3ed9fd36a;p=moodle.git

get_my_courses(): Support course-level login as

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.
---

diff --git a/lib/accesslib.php b/lib/accesslib.php
index af6386780c..891d1752d4 100755
--- a/lib/accesslib.php
+++ b/lib/accesslib.php
@@ -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) {
diff --git a/lib/datalib.php b/lib/datalib.php
index 4b2ec8d140..76540e5783 100644
--- a/lib/datalib.php
+++ b/lib/datalib.php
@@ -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();