diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index 5898fc3b86f..b3ce5985a3a 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -1675,6 +1675,13 @@ function mod_lesson_core_calendar_provide_event_action(calendar_event $event, // Apply overrides. $lesson->update_effective_access($userid); + // Mimics get_participant checks from mod_assign. + if (!$lesson->is_participant($userid)) { + // If the user is not a participant then they have + // no action to take. This will filter out the events for teachers. + return null; + } + return $factory->create_instance( get_string('startlesson', 'lesson'), new \moodle_url('/mod/lesson/view.php', ['id' => $cm->id]), diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index c65a6cafb14..5b847da7294 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -1728,6 +1728,25 @@ class lesson extends lesson_base { } } + /** + * Checks user enrollment in the current course. + * + * @param int $userid + * @return null|stdClass user record + */ + public function is_participant($userid) { + return is_enrolled($this->get_context(), $userid, 'mod/lesson:view', $this->show_only_active_users()); + } + + /** + * Check is only active users in course should be shown. + * + * @return bool true if only active users should be shown. + */ + public function show_only_active_users() { + return !has_capability('moodle/course:viewsuspendedusers', $this->get_context()); + } + /** * Updates the lesson properties with override information for a user. * diff --git a/mod/lesson/tests/lib_test.php b/mod/lesson/tests/lib_test.php index 74641d093bf..ed6a7db532a 100644 --- a/mod/lesson/tests/lib_test.php +++ b/mod/lesson/tests/lib_test.php @@ -226,11 +226,16 @@ class mod_lesson_lib_testcase extends advanced_testcase { $this->setAdminUser(); // Create a course. $course = $this->getDataGenerator()->create_course(); + // Create a teacher and enrol into the course. + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); // 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); + + // Log in as the teacher. + $this->setUser($teacher); // Create an action factory. $factory = new \core_calendar\action_factory(); // Decorate action event. @@ -346,6 +351,8 @@ class mod_lesson_lib_testcase extends advanced_testcase { // Create a course. $course = $this->getDataGenerator()->create_course(); + // Create a teacher and enrol. + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); // Create a lesson activity. $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id, @@ -354,6 +361,8 @@ class mod_lesson_lib_testcase extends advanced_testcase { // Create a calendar event. $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN); + // Now, log in as teacher. + $this->setUser($teacher); // Create an action factory. $factory = new \core_calendar\action_factory(); @@ -411,7 +420,8 @@ class mod_lesson_lib_testcase extends advanced_testcase { // Create a course. $course = $this->getDataGenerator()->create_course(); - + // Create a teacher and enrol into the course. + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); // Create a lesson activity. $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id, 'available' => time() + DAYSECS)); @@ -419,6 +429,8 @@ class mod_lesson_lib_testcase extends advanced_testcase { // Create a calendar event. $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN); + // Now, log in as teacher. + $this->setUser($teacher); // Create an action factory. $factory = new \core_calendar\action_factory(); @@ -476,13 +488,15 @@ class mod_lesson_lib_testcase extends advanced_testcase { // Create a course. $course = $this->getDataGenerator()->create_course(); - + // Create a teacher and enrol into the course. + $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); // 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 in as teacher. + $this->setUser($teacher); // Create an action factory. $factory = new \core_calendar\action_factory(); diff --git a/mod/lesson/tests/locallib_test.php b/mod/lesson/tests/locallib_test.php index e12e6e80e61..0ea349b11fa 100644 --- a/mod/lesson/tests/locallib_test.php +++ b/mod/lesson/tests/locallib_test.php @@ -216,4 +216,39 @@ class mod_lesson_locallib_testcase extends advanced_testcase { $this->assertEquals($comparearray, lesson_get_user_deadline($course->id)); } + + public function test_is_participant() { + global $USER, $DB; + $this->resetAfterTest(); + $this->setAdminUser(); + $course = $this->getDataGenerator()->create_course(); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student', [], 'manual', 0, 0, ENROL_USER_SUSPENDED); + $lessonmodule = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id)); + + // Login as student. + $this->setUser($student); + // Convert to a lesson object. + $lesson = new lesson($lessonmodule); + $this->assertEquals(true, $lesson->is_participant($student->id), + 'Student is enrolled, active and can participate'); + + // Login as student2. + $this->setUser($student2); + $this->assertEquals(false, $lesson->is_participant($student2->id), + 'Student is enrolled, suspended and can NOT participate'); + + // Login as an admin. + $this->setAdminUser(); + $this->assertEquals(false, $lesson->is_participant($USER->id), + 'Admin is not enrolled and can NOT participate'); + + $this->getDataGenerator()->enrol_user(2, $course->id); + $this->assertEquals(true, $lesson->is_participant($USER->id), + 'Admin is enrolled and can participate'); + + $this->getDataGenerator()->enrol_user(2, $course->id, [], 'manual', 0, 0, ENROL_USER_SUSPENDED); + $this->assertEquals(true, $lesson->is_participant($USER->id), + 'Admin is enrolled, suspended and can participate'); + } }