MDL-57986 mod_lesson: Add action events

Part of MDL-55611 epic.
This commit is contained in:
Cameron Ball 2017-03-13 19:39:06 +08:00 committed by Damyon Wiese
parent 9a67798a5e
commit 8a8bd42528
3 changed files with 159 additions and 3 deletions

View File

@ -154,6 +154,7 @@ function lesson_update_events($lesson, $override = null) {
}
$event = new stdClass();
$event->type = !$deadline ? CALENDAR_EVENT_TYPE_ACTION : CALENDAR_EVENT_TYPE_STANDARD;
$event->description = format_module_intro('lesson', $lesson, $cmid);
// Events module won't show user events when the courseid is nonzero.
$event->courseid = ($userid) ? 0 : $lesson->course;
@ -163,8 +164,9 @@ function lesson_update_events($lesson, $override = null) {
$event->instance = $lesson->id;
$event->timestart = $available;
$event->timeduration = max($deadline - $available, 0);
$event->timesort = $available;
$event->visible = instance_is_visible('lesson', $lesson);
$event->eventtype = 'open';
$event->eventtype = LESSON_EVENT_TYPE_OPEN;
// Determine the event name and priority.
if ($groupid) {
@ -215,9 +217,11 @@ function lesson_update_events($lesson, $override = null) {
} else {
unset($event->id);
}
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->name = $eventname.' ('.get_string('lessoncloses', 'lesson').')';
$event->timestart = $deadline;
$event->eventtype = 'close';
$event->timesort = $deadline;
$event->eventtype = LESSON_EVENT_TYPE_CLOSE;
if ($groupid && $grouppriorities !== null) {
$closepriorities = $grouppriorities['close'];
if (isset($closepriorities[$deadline])) {
@ -722,7 +726,7 @@ function lesson_cron () {
* @global object
* @param int $lessonid id of lesson
* @param int $userid optional user id, 0 means all users
* @return array array of grades
* @return array array of grades, false if none
*/
function lesson_get_user_grades($lesson, $userid=0) {
global $CFG, $DB;
@ -1562,3 +1566,26 @@ function lesson_check_updates_since(cm_info $cm, $from, $filter = array()) {
return $updates;
}
/**
* Handles creating actions for events.
*
* @param \core_calendar\event $event
* @param \core_calendar\action_factory $factory
* @return \core_calendar\local\event\value_objects\action|\core_calendar\local\interfaces\action_interface|null
*/
function mod_lesson_core_calendar_provide_event_action(\core_calendar\event $event,
\core_calendar\action_factory $factory) {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/lesson/locallib.php');
$cm = get_fast_modinfo($event->courseid)->instances['lesson'][$event->instance];
$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
return $factory->create_instance(
get_string('startlesson', 'lesson'),
new \moodle_url('/mod/lesson/view.php', ['id' => $cm->id]),
1,
$lesson->is_accessible()
);
}

View File

@ -61,6 +61,10 @@ 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_

View File

@ -164,4 +164,129 @@ class mod_lesson_lib_testcase extends advanced_testcase {
$this->assertTrue($updates->timers->updated);
$this->assertCount(1, $updates->timers->itemids);
}
public function test_lesson_core_calendar_provide_event_action_open() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a lesson activity.
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id,
'available' => time() - DAYSECS, 'deadline' => time() + DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('startlesson', 'lesson'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
public function test_lesson_core_calendar_provide_event_action_closed() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a lesson activity.
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id,
'deadline' => time() - DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('startlesson', 'lesson'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
}
public function test_lesson_core_calendar_provide_event_action_open_in_future() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a lesson activity.
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id,
'available' => time() + DAYSECS));
// Create a calendar event.
$event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('startlesson', 'lesson'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
}
public function test_lesson_core_calendar_provide_event_action_no_time_specified() {
$this->resetAfterTest();
$this->setAdminUser();
// Create a course.
$course = $this->getDataGenerator()->create_course();
// Create a lesson activity.
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));
// Create a calendar event.
$event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
// Create an action factory.
$factory = new \core_calendar\action_factory();
// Decorate action event.
$actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('startlesson', 'lesson'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}
/**
* Creates an action event.
*
* @param int $courseid
* @param int $instanceid The lesson id.
* @param string $eventtype The event type. eg. LESSON_EVENT_TYPE_OPEN.
* @return bool|\core_calendar\event
*/
private function create_action_event($courseid, $instanceid, $eventtype) {
$event = new stdClass();
$event->name = 'Calendar event';
$event->modulename = 'lesson';
$event->courseid = $courseid;
$event->instance = $instanceid;
$event->type = CALENDAR_EVENT_TYPE_ACTION;
$event->eventtype = $eventtype;
$event->timestart = time();
return \core_calendar\event::create($event);
}
}