From 536c0865efcf0cf3412ad443d2829139d67092df Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Thu, 27 Feb 2014 12:12:54 +0800 Subject: [PATCH] MDL-42892 logging: Remove usage of logtable from can_delete_course --- .../log/store/legacy/classes/log/store.php | 24 +++++++++++++++++++ .../log/store/legacy/tests/fixtures/store.php | 12 ++++++++++ .../log/store/legacy/tests/store_test.php | 15 ++++++++++++ course/lib.php | 22 +++++++++++++---- 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/admin/tool/log/store/legacy/classes/log/store.php b/admin/tool/log/store/legacy/classes/log/store.php index e46dd502568..058fda13d7d 100644 --- a/admin/tool/log/store/legacy/classes/log/store.php +++ b/admin/tool/log/store/legacy/classes/log/store.php @@ -44,10 +44,31 @@ class store implements \tool_log\log\store, \core\log\sql_select_reader { /** @var string Regex to replace the crud params */ const CRUD_REGEX = "/(crud).*?(<>|=|!=).*?'(.*?)'/s"; + /** + * This method contains hacks required for Moodle core to make legacy store compatible with other sql_select_reader based + * queries. + * + * @param string $select Select statment + * @param array $params params for the sql + * + * @return array returns an array containing the sql predicate and an array of params. + */ + protected static function replace_sql_hacks($select, array $params) { + if ($select == "userid = :userid AND courseid = :courseid AND eventname = :eventname AND timecreated > :since") { + $replace = "module = 'course' AND action = 'new' AND userid = :userid AND url = :url AND time > :since"; + $params += array('url' => "view.php?id={$params['courseid']}"); + return array($replace, $params); + } + + return array($select, $params); + } public function get_events_select($selectwhere, array $params, $sort, $limitfrom, $limitnum) { global $DB; + // Replace the query with hardcoded hacks required for core. + list($selectwhere, $params) = self::replace_sql_hacks($selectwhere, $params); + // Replace db field names to make it compatible with legacy log. foreach ($this->standardtolegacyfields as $from => $to) { $selectwhere = str_replace($from, $to, $selectwhere); @@ -77,6 +98,9 @@ class store implements \tool_log\log\store, \core\log\sql_select_reader { public function get_events_select_count($selectwhere, array $params) { global $DB; + // Replace the query with hardcoded hacks required for core. + list($selectwhere, $params) = self::replace_sql_hacks($selectwhere, $params); + // Replace db field names to make it compatible with legacy log. foreach ($this->standardtolegacyfields as $from => $to) { $selectwhere = str_replace($from, $to, $selectwhere); diff --git a/admin/tool/log/store/legacy/tests/fixtures/store.php b/admin/tool/log/store/legacy/tests/fixtures/store.php index e588f2cf586..73b8fdd13b7 100644 --- a/admin/tool/log/store/legacy/tests/fixtures/store.php +++ b/admin/tool/log/store/legacy/tests/fixtures/store.php @@ -38,4 +38,16 @@ class unittest_logstore_legacy extends \logstore_legacy\log\store { public static function replace_crud($match) { return parent::replace_crud($match); } + + /** + * Wrapper to make protected method accessible during testing. + * + * @param string $select sql predicate. + * @param array $params sql params. + * + * @return array returns array of sql predicate and params. + */ + public static function replace_sql_hack($select, array $params) { + return parent::replace_sql_hacks($select, $params); + } } diff --git a/admin/tool/log/store/legacy/tests/store_test.php b/admin/tool/log/store/legacy/tests/store_test.php index 649b0e6e109..3c59ad214e4 100644 --- a/admin/tool/log/store/legacy/tests/store_test.php +++ b/admin/tool/log/store/legacy/tests/store_test.php @@ -214,5 +214,20 @@ class logstore_legacy_store_testcase extends advanced_testcase { $updatewhere = preg_replace_callback($crudregex, 'logstore_legacy\test\unittest_logstore_legacy::replace_crud', $selectwhere); $this->assertEquals("edulevel = 0 and action LIKE '%update%' OR action NOT LIKE '%view%' AND action NOT LIKE '%report%' or action NOT LIKE '%delete%'", $updatewhere); + + } + + /** + * Test replace_sql_hacks() + */ + public function test_replace_sql_hacks() { + $select = "userid = :userid AND courseid = :courseid AND eventname = :eventname AND timecreated > :since"; + $params = array('userid' => 2, 'since' => 3, 'courseid' => 4, 'eventname' => '\core\event\course_created'); + $expectedselect = "module = 'course' AND action = 'new' AND userid = :userid AND url = :url AND time > :since"; + $expectedparams = $params + array('url' => "view.php?id=4"); + + list($replaceselect, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_hack($select, $params); + $this->assertEquals($replaceselect, $expectedselect); + $this->assertEquals($replaceparams, $expectedparams); } } diff --git a/course/lib.php b/course/lib.php index 973537c4243..562ac03de9e 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2353,7 +2353,7 @@ function course_format_ajax_support($format) { * @return boolean */ function can_delete_course($courseid) { - global $USER, $DB; + global $USER; $context = context_course::instance($courseid); @@ -2367,11 +2367,25 @@ function can_delete_course($courseid) { } $since = time() - 60*60*24; + $course = get_course($courseid); - $params = array('userid'=>$USER->id, 'url'=>"view.php?id=$courseid", 'since'=>$since); - $select = "module = 'course' AND action = 'new' AND userid = :userid AND url = :url AND time > :since"; + if ($course->timecreated < $since) { + return false; // Return if the course was not created in last 24 hours. + } - return $DB->record_exists_select('log', $select, $params); + $logmanger = get_log_manager(); + $readers = $logmanger->get_readers('\core\log\sql_select_reader'); + $reader = reset($readers); + + if (empty($reader)) { + return false; // No log reader found. + } + + // A proper reader. + $select = "userid = :userid AND courseid = :courseid AND eventname = :eventname AND timecreated > :since"; + $params = array('userid' => $USER->id, 'since' => $since, 'courseid' => $course->id, 'eventname' => '\core\event\course_created'); + + return (bool)$reader->get_events_select_count($select, $params); } /**