From 0cf0c2f3390f57b119e86865a3e8bf71abfd20a5 Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 9 Jan 2019 14:35:41 +0800 Subject: [PATCH 1/2] MDL-64238 mod_lesson: Check enrollment calendar_action generator * Updated unit tests to account for participants --- mod/lesson/lib.php | 7 ++++++ mod/lesson/locallib.php | 19 ++++++++++++++++ mod/lesson/tests/lib_test.php | 20 ++++++++++++++--- mod/lesson/tests/locallib_test.php | 35 ++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) 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'); + } } From e7f53ec0c8a371f54c483452ae2626174fbf388a Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 10 Jan 2019 08:42:40 +0800 Subject: [PATCH 2/2] MDL-64238 mod_quiz: Check enrollment on calendar_action generator * Updated unit tests to account for participants --- mod/lesson/lib.php | 1 - mod/quiz/attemptlib.php | 19 ++++++++++++++++ mod/quiz/lib.php | 6 +++++ mod/quiz/tests/attempt_test.php | 39 +++++++++++++++++++++++++++++++-- mod/quiz/tests/lib_test.php | 12 ++++++---- 5 files changed, 70 insertions(+), 7 deletions(-) diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index b3ce5985a3a..d91b50be225 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -1675,7 +1675,6 @@ 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. diff --git a/mod/quiz/attemptlib.php b/mod/quiz/attemptlib.php index 00e1eb1b010..1f6674a9a08 100644 --- a/mod/quiz/attemptlib.php +++ b/mod/quiz/attemptlib.php @@ -234,6 +234,25 @@ class quiz { return $this->ispreviewuser; } + /** + * 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/quiz:attempt', $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()); + } + /** * @return whether any questions have been added to this quiz. */ diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index 3ebe2fde951..3ffd67e8e8c 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -2184,6 +2184,12 @@ function mod_quiz_core_calendar_provide_event_action(calendar_event $event, return null; } + if (!$quizobj->is_participant($USER->id)) { + // If the user is not a participant then they have + // no action to take. This will filter out the events for teachers. + return null; + } + $attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id); if (!empty($attempts)) { // The student's last attempt is finished. diff --git a/mod/quiz/tests/attempt_test.php b/mod/quiz/tests/attempt_test.php index 0b31121d992..b9c2415f19d 100644 --- a/mod/quiz/tests/attempt_test.php +++ b/mod/quiz/tests/attempt_test.php @@ -95,7 +95,7 @@ class mod_quiz_attempt_testable extends quiz_attempt { * @copyright 2014 Tim Hunt * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class mod_quiz_attempt_testcase extends basic_testcase { +class mod_quiz_attempt_testcase extends advanced_testcase { /** * Test the functions quiz_update_open_attempts() and get_list_of_overdue_attempts() */ @@ -304,4 +304,39 @@ class mod_quiz_attempt_testcase extends basic_testcase { '/mod/quiz/review.php?attempt=124&page=1&cmid=0#'), $attempt->review_url(11, -1, false, 0)); } -} + + public function test_is_participant() { + global $USER; + $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); + $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); + $quizobj = quiz::create($quiz->id); + + // Login as student. + $this->setUser($student); + // Convert to a lesson object. + $this->assertEquals(true, $quizobj->is_participant($student->id), + 'Student is enrolled, active and can participate'); + + // Login as student2. + $this->setUser($student2); + $this->assertEquals(false, $quizobj->is_participant($student2->id), + 'Student is enrolled, suspended and can NOT participate'); + + // Login as an admin. + $this->setAdminUser(); + $this->assertEquals(false, $quizobj->is_participant($USER->id), + 'Admin is not enrolled and can NOT participate'); + + $this->getDataGenerator()->enrol_user(2, $course->id); + $this->assertEquals(true, $quizobj->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, $quizobj->is_participant($USER->id), + 'Admin is enrolled, suspended and can participate'); + } +} \ No newline at end of file diff --git a/mod/quiz/tests/lib_test.php b/mod/quiz/tests/lib_test.php index 65d8b56b111..32eadeb5646 100644 --- a/mod/quiz/tests/lib_test.php +++ b/mod/quiz/tests/lib_test.php @@ -505,14 +505,16 @@ class mod_quiz_lib_testcase extends advanced_testcase { // Create a course. $course = $this->getDataGenerator()->create_course(); - + // Create a teacher and enrol into the course. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); // Create a quiz. $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id, 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS)); // Create a calendar event. $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN); - + // Now, log in as teacher. + $this->setUser($student); // Create an action factory. $factory = new \core_calendar\action_factory(); @@ -556,14 +558,16 @@ class mod_quiz_lib_testcase extends advanced_testcase { // Create a course. $course = $this->getDataGenerator()->create_course(); - + // Create a teacher and enrol into the course. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); // Create a quiz. $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id, 'timeopen' => time() + DAYSECS)); // Create a calendar event. $event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE); - + // Now, log in as teacher. + $this->setUser($student); // Create an action factory. $factory = new \core_calendar\action_factory();