get_my_courses() and get_user_courses_bycap() field handling and caching
- Field handling moves back to get_my_courses() and now we have
almost all the fields that the old get_my_courses() did
(except for summary, which is *huge*) so get_my_courses() asks
for a lot of fields, but the get_user_courses_bycap() defaults
are _much_ leaner now.
I think this makes sense ;-)
- get_my_courses() now caches the course ids for the currently logged in
user in $USER->mycourses -- as a _string_. This is magnitudes more efficient
than having it as an array.
The cache makes a difference, but it's not very visible on
normal pageloads (with my courses block, for example).
However, over 100 iterations, for a user with 50 enrolments in a site
with 6K courses, we go from 4.3s to 0.6s. And the DB queries are *cheap*.
$tt = microtime(true);
for($n=0;$n<100;$n++) {
get_my_courses($USER->id, 'sortorder ASC');
}
error_log("took " . (microtime(true) - $tt));
This commit is contained in:
+4
-9
@@ -683,11 +683,8 @@ function get_user_courses_bycap($userid, $cap, $sess, $doanything, $sort='c.sort
|
||||
|
||||
global $CFG;
|
||||
|
||||
// Note! id is added later to ensure it's first
|
||||
$basefields = array('id', 'category', 'sortorder',
|
||||
'shortname', 'fullname', 'idnumber',
|
||||
'teacher', 'teachers', 'student', 'students',
|
||||
'guest', 'startdate', 'visible');
|
||||
// Slim base fields, let callers ask for what they need...
|
||||
$basefields = array('id', 'sortorder', 'shortname', 'idnumber');
|
||||
|
||||
if (!is_null($fields)) {
|
||||
if (!is_array($fields)) {
|
||||
@@ -712,8 +709,7 @@ function get_user_courses_bycap($userid, $cap, $sess, $doanything, $sort='c.sort
|
||||
FROM {$CFG->prefix}course c
|
||||
JOIN {$CFG->prefix}context ctx
|
||||
ON (c.id=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
|
||||
ORDER BY $sort;
|
||||
";
|
||||
ORDER BY $sort ";
|
||||
$rs = get_recordset_sql($sql);
|
||||
} else {
|
||||
//
|
||||
@@ -768,8 +764,7 @@ function get_user_courses_bycap($userid, $cap, $sess, $doanything, $sort='c.sort
|
||||
WHERE ra.id IS NOT NULL
|
||||
OR rc.id IS NOT NULL
|
||||
$catclause
|
||||
ORDER BY $sort;
|
||||
";
|
||||
ORDER BY $sort ";
|
||||
$rs = get_recordset_sql($sql);
|
||||
}
|
||||
$courses = array();
|
||||
|
||||
Reference in New Issue
Block a user