MDL-64238 mod_quiz: Check enrollment on calendar_action generator

* Updated unit tests to account for participants
This commit is contained in:
Peter
2019-02-12 06:26:40 +08:00
parent 0cf0c2f339
commit e7f53ec0c8
5 changed files with 70 additions and 7 deletions
-1
View File
@@ -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.
+19
View File
@@ -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.
*/
+6
View File
@@ -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.
+37 -2
View File
@@ -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');
}
}
+8 -4
View File
@@ -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();