diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index 520840e3d390e..0d37b67d6b5b1 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -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]); } @@ -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); @@ -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); } @@ -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()) { @@ -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]); } @@ -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); } @@ -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); }