Merge branch 'issue9-fixsql'

This commit is contained in:
Daniel Thee Roperto
2016-09-07 10:25:38 +10:00

View File

@@ -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));
}
}