From c9192ffccba4cb9a71d2c4088eb485ece333d708 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 21 Mar 2016 10:20:11 +0800 Subject: [PATCH] MDL-51917 course completion: order activities in reports Thanks to Nick Phillips for initial patch --- lib/completionlib.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/completionlib.php b/lib/completionlib.php index 80c854242db..816c7d1ca2e 100644 --- a/lib/completionlib.php +++ b/lib/completionlib.php @@ -423,6 +423,21 @@ class completion_info { // Load criteria from database $records = (array)$DB->get_records('course_completion_criteria', $params); + // Order records so activities are in the same order as they appear on the course view page. + if ($records) { + $activitiesorder = array_keys(get_fast_modinfo($this->course)->get_cms()); + usort($records, function ($a, $b) use ($activitiesorder) { + $aidx = ($a->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) ? + array_search($a->moduleinstance, $activitiesorder) : false; + $bidx = ($b->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) ? + array_search($b->moduleinstance, $activitiesorder) : false; + if ($aidx === false || $bidx === false || $aidx == $bidx) { + return 0; + } + return ($aidx < $bidx) ? -1 : 1; + }); + } + // Build array of criteria objects $this->criteria = array(); foreach ($records as $record) {