From 13f73be25b7e157863ed399727f9a3ccd054cfa2 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 31 May 2013 14:25:21 +0800 Subject: [PATCH] MDL-39536 completion: Prevent DB call when fetching activities --- lib/completionlib.php | 44 +++++++++++++++++++---------------------- report/progress/lib.php | 2 +- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/lib/completionlib.php b/lib/completionlib.php index 389312f601d..78f9eb8b62a 100644 --- a/lib/completionlib.php +++ b/lib/completionlib.php @@ -1045,40 +1045,36 @@ class completion_info { } } + /** + * Return whether or not the course has activities with completion enabled. + * + * @return boolean true when there is at least one activity with completion enabled. + */ + public function has_activities() { + $modinfo = get_fast_modinfo($this->course); + foreach ($modinfo->get_cms() as $cm) { + if ($cm->completion != COMPLETION_TRACKING_NONE) { + return true; + } + } + return false; + } + /** * Obtains a list of activities for which completion is enabled on the * course. The list is ordered by the section order of those activities. * - * @param array $modinfo For unit testing only, supply the value - * here. Otherwise the method calls get_fast_modinfo * @return array Array from $cmid => $cm of all activities with completion enabled, * empty array if none */ - public function get_activities($modinfo=null) { - global $DB; - - // Obtain those activities which have completion turned on - $withcompletion = $DB->get_records_select('course_modules', 'course='.$this->course->id. - ' AND completion<>'.COMPLETION_TRACKING_NONE); - if (!$withcompletion) { - return array(); - } - - // Use modinfo to get section order and also add in names - if (empty($modinfo)) { - $modinfo = get_fast_modinfo($this->course); - } + public function get_activities() { + $modinfo = get_fast_modinfo($this->course); $result = array(); - foreach ($modinfo->sections as $sectioncms) { - foreach ($sectioncms as $cmid) { - if (array_key_exists($cmid, $withcompletion)) { - $result[$cmid] = $withcompletion[$cmid]; - $result[$cmid]->modname = $modinfo->cms[$cmid]->modname; - $result[$cmid]->name = $modinfo->cms[$cmid]->name; - } + foreach ($modinfo->get_cms() as $cm) { + if ($cm->completion != COMPLETION_TRACKING_NONE) { + $result[$cm->id] = $cm; } } - return $result; } diff --git a/report/progress/lib.php b/report/progress/lib.php index c8ec1e43e61..3931e714b3b 100644 --- a/report/progress/lib.php +++ b/report/progress/lib.php @@ -44,7 +44,7 @@ function report_progress_extend_navigation_course($navigation, $course, $context } $completion = new completion_info($course); - $showonnavigation = ($showonnavigation && $completion->is_enabled() && count($completion->get_activities())>0); + $showonnavigation = ($showonnavigation && $completion->is_enabled() && $completion->has_activities()); if ($showonnavigation) { $url = new moodle_url('/report/progress/index.php', array('course'=>$course->id)); $navigation->add(get_string('pluginname','report_progress'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));