From 68cfce9b48dc33420bea17f83f883c42ab870a5d Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Wed, 7 Sep 2016 09:51:48 +1000 Subject: [PATCH] Issue #9 - Fixed outagedb::getactive() to make better usage of Moodle DB API, removing hardcoded LIMIT from SQL Query. --- classes/outagedb.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/classes/outagedb.php b/classes/outagedb.php index f7ef64e..a731367 100644 --- a/classes/outagedb.php +++ b/classes/outagedb.php @@ -154,20 +154,21 @@ final class outagedb { throw new \InvalidArgumentException('$time must be null or an int.'); } - // TODO Is there a way to use Moodle API instead of writing SQL (conditions not equals)? // TODO Query not fully using indexes (starttime + 90) // Gets any active outage (already started or during warning period). // Gets only one record if available, the one that starts(ed) first and that stops last. - $data = $DB->get_record_sql(' - SELECT * - FROM {auth_outage} - WHERE (starttime - warningduration <= :datetime1 AND stoptime >= :datetime2) - ORDER BY starttime ASC, stoptime DESC, title ASC - LIMIT 1 - ', - ['datetime1' => $time, 'datetime2' => $time] + $data = $DB->get_records_select( + 'auth_outage', + '(starttime - warningduration <= :datetime1 AND stoptime >= :datetime2)', + ['datetime1' => $time, 'datetime2' => $time], + 'starttime ASC, stoptime DESC, title ASC', + '*', + 0, + 1 ); - return ($data === false) ? null : new \auth_outage\models\outage($data); + // Not using $DB->get_record_select instead because there is no 'limit' parameter. + // Allowing multiple records still raises an internal error. + return (count($data) == 0) ? null : new \auth_outage\models\outage(array_shift($data)); } }