Skip to content

Commit

Permalink
MDL-42020 course: performance improvement in course listings
Browse files Browse the repository at this point in the history
- better performance when get_course_count() OR search_course_count() are called by themselves
- better performance in displaying course contacts
  • Loading branch information
marinaglancy committed Oct 9, 2013
1 parent 56cc9b3 commit bf1405a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/coursecatlib.php
Expand Up @@ -1108,6 +1108,15 @@ public static function search_courses($search, $options = array()) {
if (!empty($ids)) {
list($sql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED, 'id');
$records = self::get_course_records("c.id ". $sql, $params, $options);
// Preload course contacts if necessary - saves DB queries later to do it for each course separately.
if (!empty($options['coursecontacts'])) {
self::preload_course_contacts($records);
}
// If option 'idonly' is specified no further action is needed, just return list of ids.
if (!empty($options['idonly'])) {
return array_keys($records);
}
// Prepare the list of course_in_list objects.
foreach ($ids as $id) {
$courses[$id] = new course_in_list($records[$id]);
}
Expand Down Expand Up @@ -1159,6 +1168,11 @@ public static function search_courses($search, $options = array()) {
if (!empty($preloadcoursecontacts)) {
self::preload_course_contacts($records);
}
// If option 'idonly' is specified no further action is needed, just return list of ids.
if (!empty($options['idonly'])) {
return array_keys($records);
}
// Prepare the list of course_in_list objects.
$courses = array();
foreach ($records as $record) {
$courses[$record->id] = new course_in_list($record);
Expand Down Expand Up @@ -1187,6 +1201,7 @@ public static function search_courses_count($search, $options = array()) {
unset($options['limit']);
unset($options['summary']);
unset($options['coursecontacts']);
$options['idonly'] = true;
$courses = self::search_courses($search, $options);
$cnt = count($courses);
}
Expand Down Expand Up @@ -1226,6 +1241,8 @@ public static function search_courses_count($search, $options = array()) {
* Only cached fields may be used for sorting!
* - offset
* - limit - maximum number of children to return, 0 or null for no limit
* - idonly - returns the array or course ids instead of array of objects
* used only in get_courses_count()
* @return course_in_list[]
*/
public function get_courses($options = array()) {
Expand Down Expand Up @@ -1255,6 +1272,15 @@ public function get_courses($options = array()) {
if (!empty($ids)) {
list($sql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED, 'id');
$records = self::get_course_records("c.id ". $sql, $params, $options);
// Preload course contacts if necessary - saves DB queries later to do it for each course separately.
if (!empty($options['coursecontacts'])) {
self::preload_course_contacts($records);
}
// If option 'idonly' is specified no further action is needed, just return list of ids.
if (!empty($options['idonly'])) {
return array_keys($records);
}
// Prepare the list of course_in_list objects.
foreach ($ids as $id) {
$courses[$id] = new course_in_list($records[$id]);
}
Expand Down Expand Up @@ -1293,6 +1319,11 @@ public function get_courses($options = array()) {
if (!empty($options['coursecontacts'])) {
self::preload_course_contacts($list);
}
// If option 'idonly' is specified no further action is needed, just return list of ids.
if (!empty($options['idonly'])) {
return array_keys($list);
}
// Prepare the list of course_in_list objects.
foreach ($list as $record) {
$courses[$record->id] = new course_in_list($record);
}
Expand All @@ -1316,6 +1347,7 @@ public function get_courses_count($options = array()) {
unset($options['limit']);
unset($options['summary']);
unset($options['coursecontacts']);
$options['idonly'] = true;
$courses = $this->get_courses($options);
$cnt = count($courses);
}
Expand Down

0 comments on commit bf1405a

Please sign in to comment.