mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 08:56:36 +02:00
Fixed up get_my_courses().
It's not a tidy SQL statement any more but it's MUCH MUCH faster with large data sets.
This commit is contained in:
parent
98e9a035d7
commit
2f3499b70d
1 changed files with 32 additions and 9 deletions
|
@ -1185,17 +1185,40 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function get_my_courses($userid, $sort="c.fullname ASC") {
|
function get_my_courses($userid, $sort="fullname ASC") {
|
||||||
|
|
||||||
global $CFG;
|
global $CFG;
|
||||||
|
|
||||||
return get_records_sql("SELECT c.*
|
$course = array();
|
||||||
FROM {$CFG->prefix}course c,
|
|
||||||
{$CFG->prefix}user_students s,
|
if ($students = get_records("user_students", "userid", $userid, "", "id, course")) {
|
||||||
{$CFG->prefix}user_teachers t
|
foreach ($students as $student) {
|
||||||
WHERE (s.userid = '$userid' AND s.course = c.id)
|
$course[$student->course] = $student->course;
|
||||||
OR (t.userid = '$userid' AND t.course = c.id)
|
}
|
||||||
GROUP BY c.id
|
}
|
||||||
ORDER BY $sort");
|
if ($teachers = get_records("user_teachers", "userid", $userid, "", "id, course")) {
|
||||||
|
foreach ($teachers as $teacher) {
|
||||||
|
$course[$teacher->course] = $teacher->course;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (empty($course)) {
|
||||||
|
return $course;
|
||||||
|
}
|
||||||
|
|
||||||
|
$courseids = implode(',', $course);
|
||||||
|
|
||||||
|
return get_records_list("course", "id", $courseids, $sort);
|
||||||
|
|
||||||
|
// The following is correct but VERY slow with large datasets
|
||||||
|
//
|
||||||
|
// return get_records_sql("SELECT c.*
|
||||||
|
// FROM {$CFG->prefix}course c,
|
||||||
|
// {$CFG->prefix}user_students s,
|
||||||
|
// {$CFG->prefix}user_teachers t
|
||||||
|
// WHERE (s.userid = '$userid' AND s.course = c.id)
|
||||||
|
// OR (t.userid = '$userid' AND t.course = c.id)
|
||||||
|
// GROUP BY c.id
|
||||||
|
// ORDER BY $sort");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue