mirror of
https://github.com/moodle/moodle.git
synced 2025-03-13 20:26:32 +01:00
Merge branch 'MDL-60057-master' of git://github.com/lameze/moodle
This commit is contained in:
commit
67c0e18ca2
@ -391,6 +391,7 @@ $string['ongoing_help'] = 'If enabled, each page will display the student\'s cur
|
||||
$string['ongoingcustom'] = 'You have earned {$a->score} point(s) out of {$a->currenthigh} point(s) thus far.';
|
||||
$string['ongoingnormal'] = 'You have answered {$a->correct} correctly out of {$a->viewed} attempts.';
|
||||
$string['onpostperpage'] = 'Only one posting per grade';
|
||||
$string['openafterclose'] = 'You have specified an open date after the close date';
|
||||
$string['options'] = 'Options';
|
||||
$string['or'] = 'OR';
|
||||
$string['ordered'] = 'Ordered';
|
||||
|
@ -25,6 +25,10 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
// Event types.
|
||||
define('LESSON_EVENT_TYPE_OPEN', 'open');
|
||||
define('LESSON_EVENT_TYPE_CLOSE', 'close');
|
||||
|
||||
/* Do not include any libraries here! */
|
||||
|
||||
/**
|
||||
@ -1746,3 +1750,117 @@ function mod_lesson_get_completion_active_rule_descriptions($cm) {
|
||||
}
|
||||
return $descriptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function calculates the minimum and maximum cutoff values for the timestart of
|
||||
* the given event.
|
||||
*
|
||||
* It will return an array with two values, the first being the minimum cutoff value and
|
||||
* the second being the maximum cutoff value. Either or both values can be null, which
|
||||
* indicates there is no minimum or maximum, respectively.
|
||||
*
|
||||
* If a cutoff is required then the function must return an array containing the cutoff
|
||||
* timestamp and error string to display to the user if the cutoff value is violated.
|
||||
*
|
||||
* A minimum and maximum cutoff return value will look like:
|
||||
* [
|
||||
* [1505704373, 'The due date must be after the start date'],
|
||||
* [1506741172, 'The due date must be before the cutoff date']
|
||||
* ]
|
||||
*
|
||||
* @param calendar_event $event The calendar event to get the time range for
|
||||
* @param stdClass $instance The module instance to get the range from
|
||||
* @return array
|
||||
*/
|
||||
function mod_lesson_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $instance) {
|
||||
$mindate = null;
|
||||
$maxdate = null;
|
||||
|
||||
if ($event->eventtype == LESSON_EVENT_TYPE_OPEN) {
|
||||
// The start time of the open event can't be equal to or after the
|
||||
// close time of the lesson activity.
|
||||
if (!empty($instance->deadline)) {
|
||||
$maxdate = [
|
||||
$instance->deadline,
|
||||
get_string('openafterclose', 'lesson')
|
||||
];
|
||||
}
|
||||
} else if ($event->eventtype == LESSON_EVENT_TYPE_CLOSE) {
|
||||
// The start time of the close event can't be equal to or earlier than the
|
||||
// open time of the lesson activity.
|
||||
if (!empty($instance->available)) {
|
||||
$mindate = [
|
||||
$instance->available,
|
||||
get_string('closebeforeopen', 'lesson')
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return [$mindate, $maxdate];
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will update the lesson module according to the
|
||||
* event that has been modified.
|
||||
*
|
||||
* It will set the available or deadline value of the lesson instance
|
||||
* according to the type of event provided.
|
||||
*
|
||||
* @throws \moodle_exception
|
||||
* @param \calendar_event $event
|
||||
* @param stdClass $lesson The module instance to get the range from
|
||||
*/
|
||||
function mod_lesson_core_calendar_event_timestart_updated(\calendar_event $event, \stdClass $lesson) {
|
||||
global $DB;
|
||||
|
||||
if (empty($event->instance) || $event->modulename != 'lesson') {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($event->instance != $lesson->id) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!in_array($event->eventtype, [LESSON_EVENT_TYPE_OPEN, LESSON_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.
|
||||
if (!has_capability('moodle/course:manageactivities', $context)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($event->eventtype == LESSON_EVENT_TYPE_OPEN) {
|
||||
// If the event is for the lesson activity opening then we should
|
||||
// set the start time of the lesson activity to be the new start
|
||||
// time of the event.
|
||||
if ($lesson->available != $event->timestart) {
|
||||
$lesson->available = $event->timestart;
|
||||
$lesson->timemodified = time();
|
||||
$modified = true;
|
||||
}
|
||||
} else if ($event->eventtype == LESSON_EVENT_TYPE_CLOSE) {
|
||||
// If the event is for the lesson activity closing then we should
|
||||
// set the end time of the lesson activity to be the new start
|
||||
// time of the event.
|
||||
if ($lesson->deadline != $event->timestart) {
|
||||
$lesson->deadline = $event->timestart;
|
||||
$modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($modified) {
|
||||
$lesson->timemodified = time();
|
||||
$DB->update_record('lesson', $lesson);
|
||||
$event = \core\event\course_module_updated::create_from_cm($coursemodule, $context);
|
||||
$event->trigger();
|
||||
}
|
||||
}
|
||||
|
@ -61,10 +61,6 @@ define("LESSON_MAX_EVENT_LENGTH", "432000");
|
||||
/** Answer format is HTML */
|
||||
define("LESSON_ANSWER_HTML", "HTML");
|
||||
|
||||
// Event types.
|
||||
define('LESSON_EVENT_TYPE_OPEN', 'open');
|
||||
define('LESSON_EVENT_TYPE_CLOSE', 'close');
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
/// Any other lesson functions go here. Each of them must have a name that
|
||||
/// starts with lesson_
|
||||
|
@ -441,4 +441,286 @@ class mod_lesson_lib_testcase extends advanced_testcase {
|
||||
$this->assertEquals(mod_lesson_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
|
||||
$this->assertEquals(mod_lesson_get_completion_active_rule_descriptions(new stdClass()), []);
|
||||
}
|
||||
|
||||
/**
|
||||
* An unknown event type should not change the lesson instance.
|
||||
*/
|
||||
public function test_mod_lesson_core_calendar_event_timestart_updated_unknown_event() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . "/calendar/lib.php");
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
$lessongenerator = $generator->get_plugin_generator('mod_lesson');
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$lesson = $lessongenerator->create_instance(['course' => $course->id]);
|
||||
$lesson->available = $timeopen;
|
||||
$lesson->deadline = $timeclose;
|
||||
$DB->update_record('lesson', $lesson);
|
||||
|
||||
// Create a valid event.
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'lesson',
|
||||
'instance' => $lesson->id,
|
||||
'eventtype' => LESSON_EVENT_TYPE_OPEN . "SOMETHING ELSE",
|
||||
'timestart' => 1,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
mod_lesson_core_calendar_event_timestart_updated($event, $lesson);
|
||||
$lesson = $DB->get_record('lesson', ['id' => $lesson->id]);
|
||||
$this->assertEquals($timeopen, $lesson->available);
|
||||
$this->assertEquals($timeclose, $lesson->deadline);
|
||||
}
|
||||
|
||||
/**
|
||||
* A LESSON_EVENT_TYPE_OPEN event should update the available property of the lesson activity.
|
||||
*/
|
||||
public function test_mod_lesson_core_calendar_event_timestart_updated_open_event() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . "/calendar/lib.php");
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
$lessongenerator = $generator->get_plugin_generator('mod_lesson');
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$timemodified = 1;
|
||||
$newtimeopen = $timeopen - DAYSECS;
|
||||
$lesson = $lessongenerator->create_instance(['course' => $course->id]);
|
||||
$lesson->available = $timeopen;
|
||||
$lesson->deadline = $timeclose;
|
||||
$lesson->timemodified = $timemodified;
|
||||
$DB->update_record('lesson', $lesson);
|
||||
|
||||
// Create a valid event.
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'lesson',
|
||||
'instance' => $lesson->id,
|
||||
'eventtype' => LESSON_EVENT_TYPE_OPEN,
|
||||
'timestart' => $newtimeopen,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
// Trigger and capture the event when adding a contact.
|
||||
$sink = $this->redirectEvents();
|
||||
mod_lesson_core_calendar_event_timestart_updated($event, $lesson);
|
||||
$triggeredevents = $sink->get_events();
|
||||
$moduleupdatedevents = array_filter($triggeredevents, function($e) {
|
||||
return is_a($e, 'core\event\course_module_updated');
|
||||
});
|
||||
$lesson = $DB->get_record('lesson', ['id' => $lesson->id]);
|
||||
|
||||
// Ensure the available property matches the event timestart.
|
||||
$this->assertEquals($newtimeopen, $lesson->available);
|
||||
|
||||
// Ensure the deadline isn't changed.
|
||||
$this->assertEquals($timeclose, $lesson->deadline);
|
||||
|
||||
// Ensure the timemodified property has been changed.
|
||||
$this->assertNotEquals($timemodified, $lesson->timemodified);
|
||||
|
||||
// Confirm that a module updated event is fired when the module is changed.
|
||||
$this->assertNotEmpty($moduleupdatedevents);
|
||||
}
|
||||
|
||||
/**
|
||||
* A LESSON_EVENT_TYPE_CLOSE event should update the deadline property of the lesson activity.
|
||||
*/
|
||||
public function test_mod_lesson_core_calendar_event_timestart_updated_close_event() {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . "/calendar/lib.php");
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
$lessongenerator = $generator->get_plugin_generator('mod_lesson');
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$timemodified = 1;
|
||||
$newtimeclose = $timeclose + DAYSECS;
|
||||
$lesson = $lessongenerator->create_instance(['course' => $course->id]);
|
||||
$lesson->available = $timeopen;
|
||||
$lesson->deadline = $timeclose;
|
||||
$lesson->timemodified = $timemodified;
|
||||
$DB->update_record('lesson', $lesson);
|
||||
// Create a valid event.
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'lesson',
|
||||
'instance' => $lesson->id,
|
||||
'eventtype' => LESSON_EVENT_TYPE_CLOSE,
|
||||
'timestart' => $newtimeclose,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
// Trigger and capture the event when adding a contact.
|
||||
$sink = $this->redirectEvents();
|
||||
mod_lesson_core_calendar_event_timestart_updated($event, $lesson);
|
||||
$triggeredevents = $sink->get_events();
|
||||
$moduleupdatedevents = array_filter($triggeredevents, function($e) {
|
||||
return is_a($e, 'core\event\course_module_updated');
|
||||
});
|
||||
$lesson = $DB->get_record('lesson', ['id' => $lesson->id]);
|
||||
// Ensure the deadline property matches the event timestart.
|
||||
$this->assertEquals($newtimeclose, $lesson->deadline);
|
||||
// Ensure the available isn't changed.
|
||||
$this->assertEquals($timeopen, $lesson->available);
|
||||
// Ensure the timemodified property has been changed.
|
||||
$this->assertNotEquals($timemodified, $lesson->timemodified);
|
||||
// Confirm that a module updated event is fired when the module is changed.
|
||||
$this->assertNotEmpty($moduleupdatedevents);
|
||||
}
|
||||
|
||||
/**
|
||||
* An unknown event type should not have any limits.
|
||||
*/
|
||||
public function test_mod_lesson_core_calendar_get_valid_event_timestart_range_unknown_event() {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . "/calendar/lib.php");
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$lesson = new \stdClass();
|
||||
$lesson->available = $timeopen;
|
||||
$lesson->deadline = $timeclose;
|
||||
|
||||
// Create a valid event.
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'lesson',
|
||||
'instance' => 1,
|
||||
'eventtype' => LESSON_EVENT_TYPE_OPEN . "SOMETHING ELSE",
|
||||
'timestart' => 1,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
list ($min, $max) = mod_lesson_core_calendar_get_valid_event_timestart_range($event, $lesson);
|
||||
$this->assertNull($min);
|
||||
$this->assertNull($max);
|
||||
}
|
||||
|
||||
/**
|
||||
* The open event should be limited by the lesson's deadline property, if it's set.
|
||||
*/
|
||||
public function test_mod_lesson_core_calendar_get_valid_event_timestart_range_open_event() {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . "/calendar/lib.php");
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$lesson = new \stdClass();
|
||||
$lesson->available = $timeopen;
|
||||
$lesson->deadline = $timeclose;
|
||||
|
||||
// Create a valid event.
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'lesson',
|
||||
'instance' => 1,
|
||||
'eventtype' => LESSON_EVENT_TYPE_OPEN,
|
||||
'timestart' => 1,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
// The max limit should be bounded by the timeclose value.
|
||||
list ($min, $max) = mod_lesson_core_calendar_get_valid_event_timestart_range($event, $lesson);
|
||||
$this->assertNull($min);
|
||||
$this->assertEquals($timeclose, $max[0]);
|
||||
|
||||
// No timeclose value should result in no upper limit.
|
||||
$lesson->deadline = 0;
|
||||
list ($min, $max) = mod_lesson_core_calendar_get_valid_event_timestart_range($event, $lesson);
|
||||
$this->assertNull($min);
|
||||
$this->assertNull($max);
|
||||
}
|
||||
|
||||
/**
|
||||
* The close event should be limited by the lesson's available property, if it's set.
|
||||
*/
|
||||
public function test_mod_lesson_core_calendar_get_valid_event_timestart_range_close_event() {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . "/calendar/lib.php");
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->setAdminUser();
|
||||
$generator = $this->getDataGenerator();
|
||||
$course = $generator->create_course();
|
||||
$timeopen = time();
|
||||
$timeclose = $timeopen + DAYSECS;
|
||||
$lesson = new \stdClass();
|
||||
$lesson->available = $timeopen;
|
||||
$lesson->deadline = $timeclose;
|
||||
|
||||
// Create a valid event.
|
||||
$event = new \calendar_event([
|
||||
'name' => 'Test event',
|
||||
'description' => '',
|
||||
'format' => 1,
|
||||
'courseid' => $course->id,
|
||||
'groupid' => 0,
|
||||
'userid' => 2,
|
||||
'modulename' => 'lesson',
|
||||
'instance' => 1,
|
||||
'eventtype' => LESSON_EVENT_TYPE_CLOSE,
|
||||
'timestart' => 1,
|
||||
'timeduration' => 86400,
|
||||
'visible' => 1
|
||||
]);
|
||||
|
||||
// The max limit should be bounded by the timeclose value.
|
||||
list ($min, $max) = mod_lesson_core_calendar_get_valid_event_timestart_range($event, $lesson);
|
||||
$this->assertEquals($timeopen, $min[0]);
|
||||
$this->assertNull($max);
|
||||
|
||||
// No deadline value should result in no upper limit.
|
||||
$lesson->available = 0;
|
||||
list ($min, $max) = mod_lesson_core_calendar_get_valid_event_timestart_range($event, $lesson);
|
||||
$this->assertNull($min);
|
||||
$this->assertNull($max);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user