Index: datalib.php =================================================================== --- datalib.php (revision 103) +++ datalib.php (revision 102) @@ -732,7 +732,7 @@ if ($candoanything || has_capability('moodle/course:view',$sitecontext,$userid,$doanything)) { // User can view all courses, although there might be exceptions // which we will filter later. - $records = get_records('course c', '', '', $sort, $fields);###patch + $rs = get_recordset('course c', '', '', $sort, $fields); } else { // The only other context level above courses that applies to moodle/course:view // is category. So we consider: @@ -743,66 +743,68 @@ // However for a typical student on a large system this can reduce the // number of courses considered from around 2,000 to around 2, with corresponding // reduction in the number of queries needed. - ### start patch global $CFG; - $statements = array("SELECT $fields + $rs=get_recordset_sql(" +SELECT + $fields FROM {$CFG->prefix}role_assignments ra INNER JOIN {$CFG->prefix}context x ON x.id=ra.contextid INNER JOIN {$CFG->prefix}course c ON x.instanceid=c.id AND x.contextlevel=50 WHERE - ra.userid=$userid", -"SELECT $fields + ra.userid=$userid +UNION +SELECT + $fields 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 INNER JOIN {$CFG->prefix}course c ON c.category=a.id WHERE - ra.userid=$userid", -"SELECT $fields + ra.userid=$userid +UNION +SELECT + $fields FROM {$CFG->prefix}role_capabilities ca INNER JOIN {$CFG->prefix}context x ON x.id=ca.contextid AND x.contextlevel=50 INNER JOIN {$CFG->prefix}course c ON c.id=x.instanceid WHERE ca.contextid <> {$sitecontext->id} AND ca.capability='moodle/course:view' -ORDER BY $sort"); - $records = array(); - foreach ($statements as $s) { - $r = get_records_sql($s); - foreach ($r as $i) { - $records[$i->id] = $i; - } - } +ORDER BY $sort"); } - foreach ($records as $course) { - if ($course->id != SITEID) { - if ($candoanything) { // no need for further checks... - $mycourses[$course->id] = $course; - continue; - } - // users with moodle/course:view are considered course participants - // the course needs to be visible, or user must have moodle/course:viewhiddencourses - // capability set to view hidden courses - $context = get_context_instance(CONTEXT_COURSE, $course->id); - if ( has_capability('moodle/course:view', $context, $userid, $doanything) && - !has_capability('moodle/legacy:guest', $context, $userid, false) && - ($course->visible || has_capability('moodle/course:viewhiddencourses', $context, $userid))) { - $mycourses[$course->id] = $course; + if ($rs && $rs->RecordCount() > 0) { + while ($course = rs_fetch_next_record($rs)) { + if ($course->id != SITEID) { - // Only return a limited number of courses if limit is set - if($limit>0) { - $limit--; - if($limit==0) { - break; + if ($candoanything) { // no need for further checks... + $mycourses[$course->id] = $course; + continue; + } + + + // users with moodle/course:view are considered course participants + // the course needs to be visible, or user must have moodle/course:viewhiddencourses + // capability set to view hidden courses + $context = get_context_instance(CONTEXT_COURSE, $course->id); + if ( has_capability('moodle/course:view', $context, $userid, $doanything) && + !has_capability('moodle/legacy:guest', $context, $userid, false) && + ($course->visible || has_capability('moodle/course:viewhiddencourses', $context, $userid))) { + $mycourses[$course->id] = $course; + + // Only return a limited number of courses if limit is set + if($limit>0) { + $limit--; + if($limit==0) { + break; + } } } } } } - ### end patch /// MDL-9238, course in sub categories are not shown // if the user has course:view at system level, then he can view all course