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.
This commit is contained in:
@@ -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) {
|
||||
|
||||
+17
-9
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user