From 5c18a8364935ca3a58e603c2db071f00f321b70e Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Tue, 12 Mar 2013 17:04:53 +0800 Subject: [PATCH] MDL-36872 reports: Fixing incorrect sorting of recent activites by timestamp --- course/recent.php | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/course/recent.php b/course/recent.php index df0f0d515d0..86404196d90 100644 --- a/course/recent.php +++ b/course/recent.php @@ -274,21 +274,37 @@ echo $OUTPUT->footer(); function compare_activities_by_time_desc($a, $b) { // make sure the activities actually have a timestamp property - if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) { - return 0; - } - if ($a->timestamp == $b->timestamp) + if ((!array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) { return 0; + } + // We treat instances without timestamp as if they have a timestamp of 0. + if ((!array_key_exists('timestamp', $a)) && (array_key_exists('timestamp', $b))) { + return 1; + } + if ((array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) { + return -1; + } + if ($a->timestamp == $b->timestamp) { + return 0; + } return ($a->timestamp > $b->timestamp) ? -1 : 1; } function compare_activities_by_time_asc($a, $b) { // make sure the activities actually have a timestamp property - if ((!array_key_exists('timestamp', $a)) or (!array_key_exists('timestamp', $b))) { + if ((!array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) { return 0; } - if ($a->timestamp == $b->timestamp) + // We treat instances without timestamp as if they have a timestamp of 0. + if ((!array_key_exists('timestamp', $a)) && (array_key_exists('timestamp', $b))) { + return -1; + } + if ((array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) { + return 1; + } + if ($a->timestamp == $b->timestamp) { return 0; + } return ($a->timestamp < $b->timestamp) ? -1 : 1; }