From e56833ca06979dc38effc6836db2bde95e2076be Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Fri, 14 Sep 2018 20:05:02 +1000 Subject: [PATCH] MDL-63143 mod_lesson: Add userid param to mod_lesson calendar callbacks --- mod/lesson/lib.php | 14 ++- mod/lesson/tests/lib_test.php | 186 ++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+), 4 deletions(-) diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index fb554857107..d9439abe773 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -1653,23 +1653,29 @@ function lesson_check_updates_since(cm_info $cm, $from, $filter = array()) { * * @param calendar_event $event * @param \core_calendar\action_factory $factory + * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default). * @return \core_calendar\local\event\entities\action_interface|null */ function mod_lesson_core_calendar_provide_event_action(calendar_event $event, - \core_calendar\action_factory $factory) { + \core_calendar\action_factory $factory, + int $userid = 0) { global $DB, $CFG, $USER; require_once($CFG->dirroot . '/mod/lesson/locallib.php'); - $cm = get_fast_modinfo($event->courseid)->instances['lesson'][$event->instance]; + if (!$userid) { + $userid = $USER->id; + } + + $cm = get_fast_modinfo($event->courseid, $userid)->instances['lesson'][$event->instance]; $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST)); - if ($lesson->count_user_retries($USER->id)) { + if ($lesson->count_user_retries($userid)) { // If the user has attempted the lesson then there is no further action for the user. return null; } // Apply overrides. - $lesson->update_effective_access($USER->id); + $lesson->update_effective_access($userid); return $factory->create_instance( get_string('startlesson', 'lesson'), diff --git a/mod/lesson/tests/lib_test.php b/mod/lesson/tests/lib_test.php index a122c0fbc9e..31ed1931e3b 100644 --- a/mod/lesson/tests/lib_test.php +++ b/mod/lesson/tests/lib_test.php @@ -243,6 +243,40 @@ class mod_lesson_lib_testcase extends advanced_testcase { $this->assertTrue($actionevent->is_actionable()); } + public function test_lesson_core_calendar_provide_event_action_open_for_user() { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // 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); + + // Now, log out. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory, $student->id); + + // 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(); @@ -271,6 +305,40 @@ class mod_lesson_lib_testcase extends advanced_testcase { $this->assertFalse($actionevent->is_actionable()); } + public function test_lesson_core_calendar_provide_event_action_closed_for_user() { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // 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); + + // Now, log out. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory, $student->id); + + // 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(); @@ -299,6 +367,40 @@ class mod_lesson_lib_testcase extends advanced_testcase { $this->assertFalse($actionevent->is_actionable()); } + public function test_lesson_core_calendar_provide_event_action_open_in_future_for_user() { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // 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); + + // Now, log out. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory, $student->id); + + // 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(); @@ -326,6 +428,39 @@ class mod_lesson_lib_testcase extends advanced_testcase { $this->assertTrue($actionevent->is_actionable()); } + public function test_lesson_core_calendar_provide_event_action_no_time_specified_for_user() { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // 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); + + // Now, log out. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory, $student->id); + + // 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_after_attempt() { global $DB; @@ -376,6 +511,57 @@ class mod_lesson_lib_testcase extends advanced_testcase { $this->assertNull($action); } + public function test_lesson_core_calendar_provide_event_action_after_attempt_for_user() { + global $DB; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create 2 students in the course. + $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // 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); + + $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson'); + $tfrecord = $generator->create_question_truefalse($lesson); + + // Now, do something in the lesson as student1. + $this->setUser($student1); + mod_lesson_external::launch_attempt($lesson->id); + $data = array( + array( + 'name' => 'answerid', + 'value' => $DB->get_field('lesson_answers', 'id', array('pageid' => $tfrecord->id, 'jumpto' => -1)), + ), + array( + 'name' => '_qf__lesson_display_answer_form_truefalse', + 'value' => 1, + ) + ); + mod_lesson_external::process_page($lesson->id, $tfrecord->id, $data); + mod_lesson_external::finish_attempt($lesson->id); + + // Now, log in as the other student. + $this->setUser($student2); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $action = mod_lesson_core_calendar_provide_event_action($event, $factory, $student1->id); + + // Confirm there was no action for the user. + $this->assertNull($action); + } + /** * Creates an action event. *