From d5cf76e3ff22705a0e49ae78a007be5fcb6f05df Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Tue, 10 Apr 2007 08:57:48 +0000 Subject: [PATCH] merged fix for MDL-9238 --- lib/datalib.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/datalib.php b/lib/datalib.php index 8b659aa6e5..ad0ea507df 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -700,7 +700,7 @@ function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields='*' // Check root permissions $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); - if(has_capability('moodle/course:view',$sitecontext,$userid,$doanything)) { + if (has_capability('moodle/course:view',$sitecontext,$userid,$doanything)) { // User can view all courses, although there might be exceptions // which we will filter later. $rs = get_recordset('course c', '', '', $sort, $fields); @@ -770,6 +770,56 @@ ORDER BY $sort"); } } + /// MDL-9238, course in sub categories are not shown + // if the user has course:view at system level, then he can view all course + // skip this part + if (!has_capability('moodle/course:view',$sitecontext,$userid,$doanything)) { + + // get all course categories with an assignment + $SQL = "SELECT a.id, a.id FROM {$CFG->prefix}role_assignments ra + INNER JOIN {$CFG->prefix}context x ON x.id=ra.contextid + INNER JOIN {$CFG->prefix}course_categories a ON x.instanceid=a.id AND x.contextlevel=40 + WHERE ra.userid=$userid"; + + if ($mcoursecats = get_records_sql($SQL)) { + foreach ($mcoursecats as $mcoursecat) { + + // run the sql to get the path, find all courses in each sub (sub) categories + $pathsql = "SELECT $fields + FROM {$CFG->prefix}course_categories cc, + {$CFG->prefix}course c + WHERE cc.path LIKE '%".$mcoursecat->id."/%' + AND c.category = cc.id"; + + if ($scourses = get_records_sql($pathsql)) { + + // add each course in sub category, if correct permissions are set + // and if the course is not added to my courses list yet + foreach ($scourses as $scourse) { + $context = get_context_instance(CONTEXT_COURSE, $scourse->id); + if (!isset($mycourses[$scourse->id]) && + has_capability('moodle/course:view', $context, $userid, $doanything) && + !has_capability('moodle/legacy:guest', $context, $userid, false) && + ($scourse->visible || + has_capability('moodle/course:viewhiddencourses', $context, $userid))) { + // add it to my course array + $mycourses[$scourse->id] = $scourse; + } + + // Only return a limited number of courses if limit is set + if($limit>0) { + $limit--; + if($limit==0) { + // breaks the 2 foreach loops + break 2; + } + } + } + } + } + } + } + if (!empty($USER->id) && ($USER->id == $userid)) { $USER->mycourses[$doanything] = $mycourses; } -- 2.39.5