diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index d9439abe773..c3de6c68f94 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -1667,6 +1667,12 @@ function mod_lesson_core_calendar_provide_event_action(calendar_event $event, } $cm = get_fast_modinfo($event->courseid, $userid)->instances['lesson'][$event->instance]; + + if (!$cm->uservisible) { + // The module is not visible to the user for any reason. + return null; + } + $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST)); if ($lesson->count_user_retries($userid)) { diff --git a/mod/lesson/tests/lib_test.php b/mod/lesson/tests/lib_test.php index 31ed1931e3b..1555e61ad20 100644 --- a/mod/lesson/tests/lib_test.php +++ b/mod/lesson/tests/lib_test.php @@ -243,7 +243,39 @@ class mod_lesson_lib_testcase extends advanced_testcase { $this->assertTrue($actionevent->is_actionable()); } + public function test_lesson_core_calendar_provide_event_action_open_as_non_user() { + global $CFG; + + $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); + + // Now, log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. + $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); + + // Confirm the event is not shown at all. + $this->assertNull($actionevent); + } + public function test_lesson_core_calendar_provide_event_action_open_for_user() { + global $CFG; + $this->resetAfterTest(); $this->setAdminUser(); @@ -261,6 +293,7 @@ class mod_lesson_lib_testcase extends advanced_testcase { $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN); // Now, log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. $this->setUser(); // Create an action factory. @@ -277,6 +310,36 @@ class mod_lesson_lib_testcase extends advanced_testcase { $this->assertTrue($actionevent->is_actionable()); } + public function test_lesson_core_calendar_provide_event_action_open_in_hidden_section() { + $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); + + // Set sections 0 as hidden. + set_section_visible($course->id, 0, 0); + + // 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 is not shown at all. + $this->assertNull($actionevent); + } + public function test_lesson_core_calendar_provide_event_action_closed() { $this->resetAfterTest(); $this->setAdminUser(); @@ -306,6 +369,8 @@ class mod_lesson_lib_testcase extends advanced_testcase { } public function test_lesson_core_calendar_provide_event_action_closed_for_user() { + global $CFG; + $this->resetAfterTest(); $this->setAdminUser(); @@ -323,6 +388,7 @@ class mod_lesson_lib_testcase extends advanced_testcase { $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN); // Now, log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. $this->setUser(); // Create an action factory. @@ -368,6 +434,8 @@ class mod_lesson_lib_testcase extends advanced_testcase { } public function test_lesson_core_calendar_provide_event_action_open_in_future_for_user() { + global $CFG; + $this->resetAfterTest(); $this->setAdminUser(); @@ -385,6 +453,7 @@ class mod_lesson_lib_testcase extends advanced_testcase { $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN); // Now, log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. $this->setUser(); // Create an action factory. @@ -429,6 +498,8 @@ class mod_lesson_lib_testcase extends advanced_testcase { } public function test_lesson_core_calendar_provide_event_action_no_time_specified_for_user() { + global $CFG; + $this->resetAfterTest(); $this->setAdminUser(); @@ -445,6 +516,7 @@ class mod_lesson_lib_testcase extends advanced_testcase { $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN); // Now, log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. $this->setUser(); // Create an action factory.