mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 02:16:41 +02:00
MDL-60062 calendar: remove validate_event_timestart callback
This commit is contained in:
parent
9aac9f7074
commit
478b1d194f
12 changed files with 459 additions and 1062 deletions
|
@ -2270,11 +2270,12 @@ function mod_quiz_get_completion_active_rule_descriptions($cm) {
|
|||
* [1506741172, 'The date must be before this date']
|
||||
* ]
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
* @param \calendar_event $event The calendar event to get the time range for
|
||||
* @param stdClass|null $quiz The module instance to get the range from
|
||||
* @param stdClass $quiz The module instance to get the range from
|
||||
* @return array
|
||||
*/
|
||||
function mod_quiz_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $quiz = null) {
|
||||
function mod_quiz_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $quiz) {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
||||
|
||||
|
@ -2283,10 +2284,6 @@ function mod_quiz_core_calendar_get_valid_event_timestart_range(\calendar_event
|
|||
return [null, null];
|
||||
}
|
||||
|
||||
if (!$quiz) {
|
||||
$quiz = $DB->get_record('quiz', ['id' => $event->instance]);
|
||||
}
|
||||
|
||||
$mindate = null;
|
||||
$maxdate = null;
|
||||
|
||||
|
@ -2309,44 +2306,6 @@ function mod_quiz_core_calendar_get_valid_event_timestart_range(\calendar_event
|
|||
return [$mindate, $maxdate];
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will check that the given event is valid for it's
|
||||
* corresponding quiz module.
|
||||
*
|
||||
* An exception is thrown if the event fails validation.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
* @param \calendar_event $event
|
||||
* @return bool
|
||||
*/
|
||||
function mod_quiz_core_calendar_validate_event_timestart(\calendar_event $event) {
|
||||
global $DB;
|
||||
|
||||
if (!isset($event->instance)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Something weird going on. The event is for a different module so
|
||||
// we should ignore it.
|
||||
if ($event->modulename != 'quiz') {
|
||||
return;
|
||||
}
|
||||
|
||||
// We need to read from the DB directly because course module may
|
||||
// currently be getting created so it won't be in mod info yet.
|
||||
$quiz = $DB->get_record('quiz', ['id' => $event->instance], '*', MUST_EXIST);
|
||||
$timestart = $event->timestart;
|
||||
list($min, $max) = mod_quiz_core_calendar_get_valid_event_timestart_range($event, $quiz);
|
||||
|
||||
if ($min && $timestart < $min[0]) {
|
||||
throw new \moodle_exception($min[1]);
|
||||
}
|
||||
|
||||
if ($max && $timestart > $max[0]) {
|
||||
throw new \moodle_exception($max[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will update the quiz module according to the
|
||||
* event that has been modified.
|
||||
|
@ -2355,15 +2314,15 @@ function mod_quiz_core_calendar_validate_event_timestart(\calendar_event $event)
|
|||
* according to the type of event provided.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
* @param \calendar_event $event
|
||||
* @param \calendar_event $event A quiz activity calendar event
|
||||
* @param \stdClass $quiz A quiz activity instance
|
||||
*/
|
||||
function mod_quiz_core_calendar_event_timestart_updated(\calendar_event $event) {
|
||||
function mod_quiz_core_calendar_event_timestart_updated(\calendar_event $event, \stdClass $quiz) {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
||||
|
||||
// We don't update the activity if it's an override event that has
|
||||
// been modified.
|
||||
if (quiz_is_overriden_calendar_event($event)) {
|
||||
if (!in_array($event->eventtype, [QUIZ_EVENT_TYPE_OPEN, QUIZ_EVENT_TYPE_CLOSE])) {
|
||||
// This isn't an event that we care about so we can ignore it.
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -2379,6 +2338,18 @@ function mod_quiz_core_calendar_event_timestart_updated(\calendar_event $event)
|
|||
return;
|
||||
}
|
||||
|
||||
if ($quiz->id != $instanceid) {
|
||||
// The provided quiz instance doesn't match the event so
|
||||
// there is nothing to do here.
|
||||
return;
|
||||
}
|
||||
|
||||
// We don't update the activity if it's an override event that has
|
||||
// been modified.
|
||||
if (quiz_is_overriden_calendar_event($event)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$coursemodule = get_fast_modinfo($courseid)->instances[$modulename][$instanceid];
|
||||
$context = context_module::instance($coursemodule->id);
|
||||
|
||||
|
@ -2391,8 +2362,6 @@ function mod_quiz_core_calendar_event_timestart_updated(\calendar_event $event)
|
|||
// If the event is for the quiz activity opening then we should
|
||||
// set the start time of the quiz activity to be the new start
|
||||
// time of the event.
|
||||
$quiz = $DB->get_record('quiz', ['id' => $instanceid], '*', MUST_EXIST);
|
||||
|
||||
if ($quiz->timeopen != $event->timestart) {
|
||||
$quiz->timeopen = $event->timestart;
|
||||
$modified = true;
|
||||
|
@ -2401,8 +2370,6 @@ function mod_quiz_core_calendar_event_timestart_updated(\calendar_event $event)
|
|||
// If the event is for the quiz activity closing then we should
|
||||
// set the end time of the quiz activity to be the new start
|
||||
// time of the event.
|
||||
$quiz = $DB->get_record('quiz', ['id' => $instanceid], '*', MUST_EXIST);
|
||||
|
||||
if ($quiz->timeclose != $event->timestart) {
|
||||
$quiz->timeclose = $event->timestart;
|
||||
$modified = true;
|
||||
|
|
|
@ -94,122 +94,6 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
|
|||
return new \calendar_event(array_merge($defaultproperties, $eventproperties));
|
||||
}
|
||||
|
||||
/**
|
||||
* You can't create a quiz module event when the module doesn't exist.
|
||||
*/
|
||||
public function test_mod_quiz_core_calendar_validate_event_timestart_no_activity() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'quiz',
|
||||
'instance' => 1234,
|
||||
'eventtype' => QUIZ_EVENT_TYPE_OPEN,
|
||||
'timestart' => time(),
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
mod_quiz_core_calendar_validate_event_timestart($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* A QUIZ_EVENT_TYPE_OPEN must be before the close time of the quiz activity.
|
||||
*/
|
||||
public function test_mod_quiz_core_calendar_validate_event_timestart_valid_open_event() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$quiz = $this->create_quiz_instance(['timeopen' => $timeopen, 'timeclose' => $timeclose]);
|
||||
$event = $this->create_quiz_calendar_event($quiz, [
|
||||
'eventtype' => QUIZ_EVENT_TYPE_OPEN,
|
||||
'timestart' => $timeopen
|
||||
]);
|
||||
|
||||
mod_quiz_core_calendar_validate_event_timestart($event);
|
||||
// The function above will throw an exception if the event is
|
||||
// invalid.
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* A QUIZ_EVENT_TYPE_OPEN can not have a start time set after the close time
|
||||
* of the quiz activity.
|
||||
*/
|
||||
public function test_mod_quiz_core_calendar_validate_event_timestart_invalid_open_event() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$quiz = $this->create_quiz_instance(['timeopen' => $timeopen, 'timeclose' => $timeclose]);
|
||||
$event = $this->create_quiz_calendar_event($quiz, [
|
||||
'eventtype' => QUIZ_EVENT_TYPE_OPEN,
|
||||
'timestart' => $timeclose + 1
|
||||
]);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
mod_quiz_core_calendar_validate_event_timestart($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* A QUIZ_EVENT_TYPE_CLOSE must be after the open time of the quiz activity.
|
||||
*/
|
||||
public function test_mod_quiz_core_calendar_validate_event_timestart_valid_close_event() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$quiz = $this->create_quiz_instance(['timeopen' => $timeopen, 'timeclose' => $timeclose]);
|
||||
$event = $this->create_quiz_calendar_event($quiz, [
|
||||
'eventtype' => QUIZ_EVENT_TYPE_OPEN,
|
||||
'timestart' => $timeclose
|
||||
]);
|
||||
|
||||
mod_quiz_core_calendar_validate_event_timestart($event);
|
||||
// The function above will throw an exception if the event isn't
|
||||
// valid.
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* A QUIZ_EVENT_TYPE_CLOSE can not have a start time set before the open time
|
||||
* of the quiz activity.
|
||||
*/
|
||||
public function test_mod_quiz_core_calendar_validate_event_timestart_invalid_close_event() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$quiz = $this->create_quiz_instance(['timeopen' => $timeopen, 'timeclose' => $timeclose]);
|
||||
$event = $this->create_quiz_calendar_event($quiz, [
|
||||
'eventtype' => QUIZ_EVENT_TYPE_CLOSE,
|
||||
'timestart' => $timeopen - 1
|
||||
]);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
mod_quiz_core_calendar_validate_event_timestart($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* An unkown event type should not change the quiz instance.
|
||||
*/
|
||||
|
@ -226,7 +110,7 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
|
|||
'timestart' => 1
|
||||
]);
|
||||
|
||||
mod_quiz_core_calendar_event_timestart_updated($event);
|
||||
mod_quiz_core_calendar_event_timestart_updated($event, $quiz);
|
||||
|
||||
$quiz = $DB->get_record('quiz', ['id' => $quiz->id]);
|
||||
$this->assertEquals($timeopen, $quiz->timeopen);
|
||||
|
@ -256,7 +140,7 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
|
|||
'timestart' => $newtimeopen
|
||||
]);
|
||||
|
||||
mod_quiz_core_calendar_event_timestart_updated($event);
|
||||
mod_quiz_core_calendar_event_timestart_updated($event, $quiz);
|
||||
|
||||
$quiz = $DB->get_record('quiz', ['id' => $quiz->id]);
|
||||
// Ensure the timeopen property matches the event timestart.
|
||||
|
@ -290,7 +174,7 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
|
|||
'timestart' => $newtimeclose
|
||||
]);
|
||||
|
||||
mod_quiz_core_calendar_event_timestart_updated($event);
|
||||
mod_quiz_core_calendar_event_timestart_updated($event, $quiz);
|
||||
|
||||
$quiz = $DB->get_record('quiz', ['id' => $quiz->id]);
|
||||
// Ensure the timeclose property matches the event timestart.
|
||||
|
@ -332,7 +216,7 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
|
|||
|
||||
$DB->insert_record('quiz_overrides', $record);
|
||||
|
||||
mod_quiz_core_calendar_event_timestart_updated($event);
|
||||
mod_quiz_core_calendar_event_timestart_updated($event, $quiz);
|
||||
|
||||
$quiz = $DB->get_record('quiz', ['id' => $quiz->id]);
|
||||
// Ensure the timeopen property doesn't change.
|
||||
|
@ -379,7 +263,7 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
|
|||
|
||||
$this->setUser($user);
|
||||
|
||||
mod_quiz_core_calendar_event_timestart_updated($event);
|
||||
mod_quiz_core_calendar_event_timestart_updated($event, $quiz);
|
||||
|
||||
$newquiz = $DB->get_record('quiz', ['id' => $quiz->id]);
|
||||
// The time open shouldn't have changed even though we updated the calendar
|
||||
|
@ -426,7 +310,7 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
|
|||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
mod_quiz_core_calendar_event_timestart_updated($event);
|
||||
mod_quiz_core_calendar_event_timestart_updated($event, $quiz);
|
||||
|
||||
$triggeredevents = $sink->get_events();
|
||||
$moduleupdatedevents = array_filter($triggeredevents, function($e) {
|
||||
|
@ -625,7 +509,7 @@ class mod_quiz_calendar_event_modified_testcase extends advanced_testcase {
|
|||
|
||||
$this->setUser($teacher);
|
||||
|
||||
mod_quiz_core_calendar_event_timestart_updated($event);
|
||||
mod_quiz_core_calendar_event_timestart_updated($event, $quiz);
|
||||
|
||||
$quiz = $DB->get_record('quiz', ['id' => $quiz->id]);
|
||||
$attempt = $DB->get_record('quiz_attempts', ['id' => $attemptid]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue