Merge branch 'w17_MDL-45214_m27_idsortlog' of https://github.com/skodak/moodle

This commit is contained in:
Sam Hemelryk
2014-04-24 11:10:46 +12:00
6 changed files with 30 additions and 2 deletions
+22
View File
@@ -61,4 +61,26 @@ trait reader {
}
return $this->store;
}
/**
* Adds ID column to $sort to make sure events from one request
* within 1 second are returned in the same order.
*
* @param string $sort
* @return string sort string
*/
protected static function tweak_sort_by_id($sort) {
if (empty($sort)) {
// Mysql does this - unlikely to be used in real life because $sort is always expected.
$sort = "id ASC";
} else if (stripos($sort, 'timecreated') === false) {
$sort .= ", id ASC";
} else if (stripos($sort, 'timecreated DESC') !== false) {
$sort .= ", id DESC";
} else {
$sort .= ", id ASC";
}
return $sort;
}
}
@@ -167,6 +167,8 @@ class store implements \tool_log\log\writer, \core\log\sql_select_reader {
return array();
}
$sort = self::tweak_sort_by_id($sort);
$events = array();
$records = $this->extdb->get_records_select($dbtable, $selectwhere, $params, $sort, '*', $limitfrom, $limitnum);
@@ -152,7 +152,7 @@ class logstore_database_store_testcase extends advanced_testcase {
// Test reading.
$this->assertSame(3, $store->get_events_select_count('', array()));
$events = $store->get_events_select('', array(), 'id', 0, 0);
$events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC".
$this->assertCount(3, $events);
$resev1 = array_shift($events);
array_shift($events);
@@ -86,6 +86,8 @@ class store implements \tool_log\log\store, \core\log\sql_select_reader {
public function get_events_select($selectwhere, array $params, $sort, $limitfrom, $limitnum) {
global $DB;
$sort = self::tweak_sort_by_id($sort);
// Replace the query with hardcoded mappings required for core.
list($selectwhere, $params, $sort) = self::replace_sql_legacy($selectwhere, $params, $sort);
@@ -69,6 +69,8 @@ class store implements \tool_log\log\writer, \core\log\sql_internal_reader {
public function get_events_select($selectwhere, array $params, $sort, $limitfrom, $limitnum) {
global $DB;
$sort = self::tweak_sort_by_id($sort);
$events = array();
$records = $DB->get_records_select('logstore_standard_log', $selectwhere, $params, $sort, '*', $limitfrom, $limitnum);
@@ -122,7 +122,7 @@ class logstore_standard_store_testcase extends advanced_testcase {
// Test reading.
$this->assertSame(3, $store->get_events_select_count('', array()));
$events = $store->get_events_select('', array(), 'id', 0, 0);
$events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC".
$this->assertCount(3, $events);
$resev1 = array_shift($events);
array_shift($events);