MDL-42892 logging: Remove usage of logtable from can_delete_course
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+18
-4
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user