mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +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
|
@ -3575,16 +3575,10 @@ function mod_feedback_get_completion_active_rule_descriptions($cm) {
|
|||
* ]
|
||||
*
|
||||
* @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 $instance The module instance to get the range from
|
||||
* @return array
|
||||
*/
|
||||
function mod_feedback_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $instance = null) {
|
||||
global $DB;
|
||||
|
||||
if (!$instance) {
|
||||
$instance = $DB->get_record('feedback', ['id' => $event->instance], '*', MUST_EXIST);
|
||||
}
|
||||
|
||||
function mod_feedback_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $instance) {
|
||||
$mindate = null;
|
||||
$maxdate = null;
|
||||
|
||||
|
@ -3611,32 +3605,6 @@ function mod_feedback_core_calendar_get_valid_event_timestart_range(\calendar_ev
|
|||
return [$mindate, $maxdate];
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will check that the given event is valid for it's
|
||||
* corresponding feedback module.
|
||||
*
|
||||
* An exception is thrown if the event fails validation.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
* @param \calendar_event $event
|
||||
*/
|
||||
function mod_feedback_core_calendar_validate_event_timestart(\calendar_event $event) {
|
||||
global $DB;
|
||||
|
||||
$record = $DB->get_record('feedback', ['id' => $event->instance], '*', MUST_EXIST);
|
||||
$timestart = $event->timestart;
|
||||
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event, $record);
|
||||
|
||||
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 feedback module according to the
|
||||
* event that has been modified.
|
||||
|
@ -3646,27 +3614,29 @@ function mod_feedback_core_calendar_validate_event_timestart(\calendar_event $ev
|
|||
*
|
||||
* @throws \moodle_exception
|
||||
* @param \calendar_event $event
|
||||
* @param stdClass $feedback The module instance to get the range from
|
||||
*/
|
||||
function mod_feedback_core_calendar_event_timestart_updated(\calendar_event $event) {
|
||||
function mod_feedback_core_calendar_event_timestart_updated(\calendar_event $event, \stdClass $feedback) {
|
||||
global $CFG, $DB;
|
||||
|
||||
if (empty($event->instance) || $event->modulename != 'feedback') {
|
||||
return;
|
||||
}
|
||||
|
||||
$coursemodule = get_coursemodule_from_instance('feedback',
|
||||
$event->instance,
|
||||
$event->courseid,
|
||||
false,
|
||||
MUST_EXIST);
|
||||
|
||||
if (empty($coursemodule)) {
|
||||
// If we don't have a course module yet then it likely means
|
||||
// the activity is still being set up. In this case there is
|
||||
// nothing for us to do anyway.
|
||||
if ($event->instance != $feedback->id) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!in_array($event->eventtype, [FEEDBACK_EVENT_TYPE_OPEN, FEEDBACK_EVENT_TYPE_CLOSE])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$courseid = $event->courseid;
|
||||
$modulename = $event->modulename;
|
||||
$instanceid = $event->instance;
|
||||
$modified = false;
|
||||
|
||||
$coursemodule = get_fast_modinfo($courseid)->instances[$modulename][$instanceid];
|
||||
$context = context_module::instance($coursemodule->id);
|
||||
|
||||
// The user does not have the capability to modify this activity.
|
||||
|
@ -3674,34 +3644,28 @@ function mod_feedback_core_calendar_event_timestart_updated(\calendar_event $eve
|
|||
return;
|
||||
}
|
||||
|
||||
$modified = false;
|
||||
|
||||
if ($event->eventtype == FEEDBACK_EVENT_TYPE_OPEN) {
|
||||
// If the event is for the feedback activity opening then we should
|
||||
// set the start time of the feedback activity to be the new start
|
||||
// time of the event.
|
||||
$record = $DB->get_record('feedback', ['id' => $event->instance], '*', MUST_EXIST);
|
||||
|
||||
if ($record->timeopen != $event->timestart) {
|
||||
$record->timeopen = $event->timestart;
|
||||
$record->timemodified = time();
|
||||
if ($feedback->timeopen != $event->timestart) {
|
||||
$feedback->timeopen = $event->timestart;
|
||||
$feedback->timemodified = time();
|
||||
$modified = true;
|
||||
}
|
||||
} else if ($event->eventtype == FEEDBACK_EVENT_TYPE_CLOSE) {
|
||||
// If the event is for the feedback activity closing then we should
|
||||
// set the end time of the feedback activity to be the new start
|
||||
// time of the event.
|
||||
$record = $DB->get_record('feedback', ['id' => $event->instance], '*', MUST_EXIST);
|
||||
|
||||
if ($record->timeclose != $event->timestart) {
|
||||
$record->timeclose = $event->timestart;
|
||||
$record->timemodified = time();
|
||||
if ($feedback->timeclose != $event->timestart) {
|
||||
$feedback->timeclose = $event->timestart;
|
||||
$modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($modified) {
|
||||
$DB->update_record('feedback', $record);
|
||||
$feedback->timemodified = time();
|
||||
$DB->update_record('feedback', $feedback);
|
||||
$event = \core\event\course_module_updated::create_from_cm($coursemodule, $context);
|
||||
$event->trigger();
|
||||
}
|
||||
|
|
|
@ -415,7 +415,7 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
'visible' => 1
|
||||
]);
|
||||
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event);
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event, $feedback);
|
||||
$this->assertNull($min);
|
||||
$this->assertNull($max);
|
||||
}
|
||||
|
@ -455,7 +455,7 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
'visible' => 1
|
||||
]);
|
||||
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event);
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event, $feedback);
|
||||
$this->assertNull($min);
|
||||
$this->assertEquals($timeclose, $max[0]);
|
||||
$this->assertNotEmpty($max[1]);
|
||||
|
@ -496,7 +496,7 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
'visible' => 1
|
||||
]);
|
||||
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event);
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event, $feedback);
|
||||
$this->assertNull($min);
|
||||
$this->assertNull($max);
|
||||
}
|
||||
|
@ -536,7 +536,7 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
'visible' => 1
|
||||
]);
|
||||
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event);
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event, $feedback);
|
||||
$this->assertEquals($timeopen, $min[0]);
|
||||
$this->assertNotEmpty($min[1]);
|
||||
$this->assertNull($max);
|
||||
|
@ -577,199 +577,11 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
'visible' => 1
|
||||
]);
|
||||
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event);
|
||||
list($min, $max) = mod_feedback_core_calendar_get_valid_event_timestart_range($event, $feedback);
|
||||
$this->assertNull($min);
|
||||
$this->assertNull($max);
|
||||
}
|
||||
|
||||
/**
|
||||
* You can't create a feedback module event when the module doesn't exist.
|
||||
*/
|
||||
public function test_mod_feedback_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' => 'feedback',
|
||||
'instance' => 1234,
|
||||
'eventtype' => FEEDBACK_EVENT_TYPE_OPEN,
|
||||
'timestart' => time(),
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
mod_feedback_core_calendar_validate_event_timestart($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* A FEEDBACK_EVENT_TYPE_OPEN must be before the close time of the feedback activity.
|
||||
*/
|
||||
public function test_mod_feedback_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();
|
||||
$feedbackgenerator = $generator->get_plugin_generator('mod_feedback');
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$feedback = $feedbackgenerator->create_instance(['course' => $course->id]);
|
||||
$feedback->timeopen = $timeopen;
|
||||
$feedback->timeclose = $timeclose;
|
||||
$DB->update_record('feedback', $feedback);
|
||||
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'feedback',
|
||||
'instance' => $feedback->id,
|
||||
'eventtype' => FEEDBACK_EVENT_TYPE_OPEN,
|
||||
'timestart' => $timeopen,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
// This will throw an exception if the event is invald.
|
||||
mod_feedback_core_calendar_validate_event_timestart($event);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* A FEEDBACK_EVENT_TYPE_OPEN can not have a start time set after the close time
|
||||
* of the feedback activity.
|
||||
*/
|
||||
public function test_mod_feedback_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();
|
||||
$feedbackgenerator = $generator->get_plugin_generator('mod_feedback');
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$feedback = $feedbackgenerator->create_instance(['course' => $course->id]);
|
||||
$feedback->timeopen = $timeopen;
|
||||
$feedback->timeclose = $timeclose;
|
||||
$DB->update_record('feedback', $feedback);
|
||||
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'feedback',
|
||||
'instance' => $feedback->id,
|
||||
'eventtype' => FEEDBACK_EVENT_TYPE_OPEN,
|
||||
'timestart' => $timeclose + 1,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
mod_feedback_core_calendar_validate_event_timestart($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* A FEEDBACK_EVENT_TYPE_CLOSE must be after the open time of the feedback activity.
|
||||
*/
|
||||
public function test_mod_feedback_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();
|
||||
$feedbackgenerator = $generator->get_plugin_generator('mod_feedback');
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$feedback = $feedbackgenerator->create_instance(['course' => $course->id]);
|
||||
$feedback->timeopen = $timeopen;
|
||||
$feedback->timeclose = $timeclose;
|
||||
$DB->update_record('feedback', $feedback);
|
||||
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'feedback',
|
||||
'instance' => $feedback->id,
|
||||
'eventtype' => FEEDBACK_EVENT_TYPE_CLOSE,
|
||||
'timestart' => $timeclose,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
// This will throw an exception if the event is invald.
|
||||
mod_feedback_core_calendar_validate_event_timestart($event);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* A FEEDBACK_EVENT_TYPE_CLOSE can not have a start time set before the open time
|
||||
* of the feedback activity.
|
||||
*/
|
||||
public function test_mod_feedback_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();
|
||||
$feedbackgenerator = $generator->get_plugin_generator('mod_feedback');
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$feedback = $feedbackgenerator->create_instance(['course' => $course->id]);
|
||||
$feedback->timeopen = $timeopen;
|
||||
$feedback->timeclose = $timeclose;
|
||||
$DB->update_record('feedback', $feedback);
|
||||
|
||||
// Create a valid event.
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'feedback',
|
||||
'instance' => $feedback->id,
|
||||
'eventtype' => FEEDBACK_EVENT_TYPE_CLOSE,
|
||||
'timestart' => $timeopen - 1,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
mod_feedback_core_calendar_validate_event_timestart($event);
|
||||
}
|
||||
|
||||
/**
|
||||
* An unkown event type should not change the feedback instance.
|
||||
*/
|
||||
|
@ -805,7 +617,7 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
'visible' => 1
|
||||
]);
|
||||
|
||||
mod_feedback_core_calendar_event_timestart_updated($event);
|
||||
mod_feedback_core_calendar_event_timestart_updated($event, $feedback);
|
||||
|
||||
$feedback = $DB->get_record('feedback', ['id' => $feedback->id]);
|
||||
$this->assertEquals($timeopen, $feedback->timeopen);
|
||||
|
@ -851,7 +663,7 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
'visible' => 1
|
||||
]);
|
||||
|
||||
mod_feedback_core_calendar_event_timestart_updated($event);
|
||||
mod_feedback_core_calendar_event_timestart_updated($event, $feedback);
|
||||
|
||||
$feedback = $DB->get_record('feedback', ['id' => $feedback->id]);
|
||||
// Ensure the timeopen property matches the event timestart.
|
||||
|
@ -901,7 +713,7 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
'visible' => 1
|
||||
]);
|
||||
|
||||
mod_feedback_core_calendar_event_timestart_updated($event);
|
||||
mod_feedback_core_calendar_event_timestart_updated($event, $feedback);
|
||||
|
||||
$feedback = $DB->get_record('feedback', ['id' => $feedback->id]);
|
||||
// Ensure the timeclose property matches the event timestart.
|
||||
|
@ -964,7 +776,7 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
|
||||
$this->setUser($user);
|
||||
|
||||
mod_feedback_core_calendar_event_timestart_updated($event);
|
||||
mod_feedback_core_calendar_event_timestart_updated($event, $feedback);
|
||||
|
||||
$newfeedback = $DB->get_record('feedback', ['id' => $feedback->id]);
|
||||
// The activity shouldn't have been updated because the user
|
||||
|
@ -1025,7 +837,7 @@ class mod_feedback_lib_testcase extends advanced_testcase {
|
|||
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
mod_feedback_core_calendar_event_timestart_updated($event);
|
||||
mod_feedback_core_calendar_event_timestart_updated($event, $feedback);
|
||||
|
||||
$triggeredevents = $sink->get_events();
|
||||
$moduleupdatedevents = array_filter($triggeredevents, function($e) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue