MDL-60062 calendar: remove validate_event_timestart callback

This commit is contained in:
Ryan Wyllie 2017-10-30 08:15:10 +00:00
parent 9aac9f7074
commit 478b1d194f
12 changed files with 459 additions and 1062 deletions

View file

@ -1258,15 +1258,9 @@ function mod_choice_core_calendar_provide_event_action(calendar_event $event,
* ]
*
* @param calendar_event $event The calendar event to get the time range for
* @param stdClass|null $instance The module instance to get the range from
* @param stdClass $choice The module instance to get the range from
*/
function mod_choice_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $choice = null) {
global $DB;
if (!$choice) {
$choice = $DB->get_record('choice', ['id' => $event->instance]);
}
function mod_choice_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $choice) {
$mindate = null;
$maxdate = null;
@ -1289,38 +1283,6 @@ function mod_choice_core_calendar_get_valid_event_timestart_range(\calendar_even
return [$mindate, $maxdate];
}
/**
* This function will check that the given event is valid for it's
* corresponding choice module.
*
* An exception is thrown if the event fails validation.
*
* @throws \moodle_exception
* @param \calendar_event $event
* @return bool
*/
function mod_choice_core_calendar_validate_event_timestart(\calendar_event $event) {
global $DB;
if (!isset($event->instance)) {
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.
$instance = $DB->get_record('choice', ['id' => $event->instance], '*', MUST_EXIST);
$timestart = $event->timestart;
list($min, $max) = mod_choice_core_calendar_get_valid_event_timestart_range($event, $instance);
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 choice module according to the
* event that has been modified.
@ -1330,10 +1292,15 @@ function mod_choice_core_calendar_validate_event_timestart(\calendar_event $even
*
* @throws \moodle_exception
* @param \calendar_event $event
* @param stdClass $choice The module instance to get the range from
*/
function mod_choice_core_calendar_event_timestart_updated(\calendar_event $event) {
function mod_choice_core_calendar_event_timestart_updated(\calendar_event $event, \stdClass $choice) {
global $DB;
if (!in_array($event->eventtype, [CHOICE_EVENT_TYPE_OPEN, CHOICE_EVENT_TYPE_CLOSE])) {
return;
}
$courseid = $event->courseid;
$modulename = $event->modulename;
$instanceid = $event->instance;
@ -1345,6 +1312,10 @@ function mod_choice_core_calendar_event_timestart_updated(\calendar_event $event
return;
}
if ($choice->id != $instanceid) {
return;
}
$coursemodule = get_fast_modinfo($courseid)->instances[$modulename][$instanceid];
$context = context_module::instance($coursemodule->id);
@ -1357,29 +1328,24 @@ function mod_choice_core_calendar_event_timestart_updated(\calendar_event $event
// If the event is for the choice activity opening then we should
// set the start time of the choice activity to be the new start
// time of the event.
$record = $DB->get_record('choice', ['id' => $instanceid], '*', MUST_EXIST);
if ($record->timeopen != $event->timestart) {
$record->timeopen = $event->timestart;
$record->timemodified = time();
if ($choice->timeopen != $event->timestart) {
$choice->timeopen = $event->timestart;
$modified = true;
}
} else if ($event->eventtype == CHOICE_EVENT_TYPE_CLOSE) {
// If the event is for the choice activity closing then we should
// set the end time of the choice activity to be the new start
// time of the event.
$record = $DB->get_record('choice', ['id' => $instanceid], '*', MUST_EXIST);
if ($record->timeclose != $event->timestart) {
$record->timeclose = $event->timestart;
$record->timemodified = time();
if ($choice->timeclose != $event->timestart) {
$choice->timeclose = $event->timestart;
$modified = true;
}
}
if ($modified) {
$choice->timemodified = time();
// Persist the instance changes.
$DB->update_record('choice', $record);
$DB->update_record('choice', $choice);
$event = \core\event\course_module_updated::create_from_cm($coursemodule, $context);
$event->trigger();
}

View file

@ -489,196 +489,6 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase {
$this->assertEquals(mod_choice_get_completion_active_rule_descriptions(new stdClass()), []);
}
/**
* You can't create a choice module event when the module doesn't exist.
*/
public function test_mod_choice_core_calendar_validate_event_timestart_no_activity() {
global $CFG;
require_once($CFG->dirroot . "/calendar/lib.php");
$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' => 'choice',
'instance' => 1234,
'eventtype' => CHOICE_EVENT_TYPE_OPEN,
'timestart' => time(),
'timeduration' => 86400,
'visible' => 1
]);
$this->expectException('moodle_exception');
mod_choice_core_calendar_validate_event_timestart($event);
}
/**
* A CHOICE_EVENT_TYPE_OPEN must be before the close time of the choice activity.
*/
public function test_mod_choice_core_calendar_validate_event_timestart_valid_open_event() {
global $CFG, $DB;
require_once($CFG->dirroot . "/calendar/lib.php");
$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$choicegenerator = $generator->get_plugin_generator('mod_choice');
$timeopen = time();
$timeclose = $timeopen + DAYSECS;
$choice = $choicegenerator->create_instance(['course' => $course->id]);
$choice->timeopen = $timeopen;
$choice->timeclose = $timeclose;
$DB->update_record('choice', $choice);
$event = new \calendar_event([
'name' => 'Test event',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'choice',
'instance' => $choice->id,
'eventtype' => CHOICE_EVENT_TYPE_OPEN,
'timestart' => $timeopen,
'timeduration' => 86400,
'visible' => 1
]);
mod_choice_core_calendar_validate_event_timestart($event);
// The function above will throw an exception if the event is
// invalid.
$this->assertTrue(true);
}
/**
* A CHOICE_EVENT_TYPE_OPEN can not have a start time set after the close time
* of the choice activity.
*/
public function test_mod_choice_core_calendar_validate_event_timestart_invalid_open_event() {
global $CFG, $DB;
require_once($CFG->dirroot . "/calendar/lib.php");
$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$choicegenerator = $generator->get_plugin_generator('mod_choice');
$timeopen = time();
$timeclose = $timeopen + DAYSECS;
$choice = $choicegenerator->create_instance(['course' => $course->id]);
$choice->timeopen = $timeopen;
$choice->timeclose = $timeclose;
$DB->update_record('choice', $choice);
$event = new \calendar_event([
'name' => 'Test event',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'choice',
'instance' => $choice->id,
'eventtype' => CHOICE_EVENT_TYPE_OPEN,
'timestart' => $timeclose + 1,
'timeduration' => 86400,
'visible' => 1
]);
$this->expectException('moodle_exception');
mod_choice_core_calendar_validate_event_timestart($event);
}
/**
* A CHOICE_EVENT_TYPE_CLOSE must be after the open time of the choice activity.
*/
public function test_mod_choice_core_calendar_validate_event_timestart_valid_close_event() {
global $CFG, $DB;
require_once($CFG->dirroot . "/calendar/lib.php");
$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$choicegenerator = $generator->get_plugin_generator('mod_choice');
$timeopen = time();
$timeclose = $timeopen + DAYSECS;
$choice = $choicegenerator->create_instance(['course' => $course->id]);
$choice->timeopen = $timeopen;
$choice->timeclose = $timeclose;
$DB->update_record('choice', $choice);
$event = new \calendar_event([
'name' => 'Test event',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'choice',
'instance' => $choice->id,
'eventtype' => CHOICE_EVENT_TYPE_CLOSE,
'timestart' => $timeclose,
'timeduration' => 86400,
'visible' => 1
]);
mod_choice_core_calendar_validate_event_timestart($event);
// The function above will throw an exception if the event isn't
// valid.
$this->assertTrue(true);
}
/**
* A CHOICE_EVENT_TYPE_CLOSE can not have a start time set before the open time
* of the choice activity.
*/
public function test_mod_choice_core_calendar_validate_event_timestart_invalid_close_event() {
global $CFG, $DB;
require_once($CFG->dirroot . "/calendar/lib.php");
$this->resetAfterTest(true);
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$choicegenerator = $generator->get_plugin_generator('mod_choice');
$timeopen = time();
$timeclose = $timeopen + DAYSECS;
$choice = $choicegenerator->create_instance(['course' => $course->id]);
$choice->timeopen = $timeopen;
$choice->timeclose = $timeclose;
$DB->update_record('choice', $choice);
// Create a valid event.
$event = new \calendar_event([
'name' => 'Test event',
'description' => '',
'format' => 1,
'courseid' => $course->id,
'groupid' => 0,
'userid' => 2,
'modulename' => 'choice',
'instance' => $choice->id,
'eventtype' => CHOICE_EVENT_TYPE_CLOSE,
'timestart' => $timeopen - 1,
'timeduration' => 86400,
'visible' => 1
]);
$this->expectException('moodle_exception');
mod_choice_core_calendar_validate_event_timestart($event);
}
/**
* An unkown event type should not change the choice instance.
*/
@ -714,7 +524,7 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase {
'visible' => 1
]);
mod_choice_core_calendar_event_timestart_updated($event);
mod_choice_core_calendar_event_timestart_updated($event, $choice);
$choice = $DB->get_record('choice', ['id' => $choice->id]);
$this->assertEquals($timeopen, $choice->timeopen);
@ -763,7 +573,7 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase {
// Trigger and capture the event when adding a contact.
$sink = $this->redirectEvents();
mod_choice_core_calendar_event_timestart_updated($event);
mod_choice_core_calendar_event_timestart_updated($event, $choice);
$triggeredevents = $sink->get_events();
$moduleupdatedevents = array_filter($triggeredevents, function($e) {
@ -824,7 +634,7 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase {
// Trigger and capture the event when adding a contact.
$sink = $this->redirectEvents();
mod_choice_core_calendar_event_timestart_updated($event);
mod_choice_core_calendar_event_timestart_updated($event, $choice);
$triggeredevents = $sink->get_events();
$moduleupdatedevents = array_filter($triggeredevents, function($e) {