From: sam_marshall Date: Fri, 12 Jan 2007 17:05:32 +0000 (+0000) Subject: MDL-8149 Added code to narrow down possibilities for get_my_courses, which reduces... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f8e1c7af386a2a0b8c44c4d7fb40d2ccb50890ee;p=moodle.git MDL-8149 Added code to narrow down possibilities for get_my_courses, which reduces number of db queries required for profile page when site has many courses. --- diff --git a/lib/datalib.php b/lib/datalib.php index d15891acaf..1c1e960db2 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -670,19 +670,74 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c /** - * List of courses that a user is a member of. + * List of courses that a user has access to view. Note that for admins, + * this usually includes every course on the system. * * @uses $CFG * @param int $userid The user of interest * @param string $sort the sortorder in the course table * @param string $fields the fields to return + * @param bool $doanything True if using the doanything flag + * @param int $limit Maximum number of records to return, or 0 for unlimited * @return array {@link $COURSE} of course objects */ -function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields='*', $doanything=false) { +function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields='*', $doanything=false,$limit=0) { $mycourses = array(); + + // Fix fields to refer to the course table c + $fields=preg_replace('/([a-z0-9*]+)/','c.$1',$fields); - $rs = get_recordset('course', '', '', $sort, $fields); + // Attempt to filter the list of courses in order to reduce the number + // of queries in the next part. + + // Check root permissions + $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); + 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); + } else { + // The only other context level above courses that applies to moodle/course:view + // is category. So we consider: + // 1. All courses in which the user is assigned a role + // 2. All courses in categories in which the user is assigned a role + // 3. All courses which have overrides for moodle/course:view + // Remember that this is just a filter. We check each individual course later. + // 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. + global $CFG; + $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 +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 +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"); + } if ($rs && $rs->RecordCount() > 0) { while (!$rs->EOF) { @@ -697,6 +752,14 @@ function get_my_courses($userid, $sort='visible DESC,sortorder ASC', $fields='*' !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; + } + } } } diff --git a/user/view.php b/user/view.php index fad1180e9b..8c31d7181f 100644 --- a/user/view.php +++ b/user/view.php @@ -290,7 +290,8 @@ print_row(get_string('msnid').':', s($user->msn)); } - if ($mycourses = get_my_courses($user->id)) { + if ($mycourses = get_my_courses($user->id,'visible DESC,sortorder ASC', '*', false, 21)) { + $shown=0; $courselisting = ''; foreach ($mycourses as $mycourse) { if ($mycourse->visible and $mycourse->category) { @@ -301,6 +302,11 @@ $courselisting .= "$mycourse->fullname, "; } } + $shown++; + if($shown==20) { + $courselisting.= "..."; + break; + } } print_row(get_string('courses').':', rtrim($courselisting,', ')); }