Merge branch 'MDL-64238-master' of git://github.com/peterRd/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2019-02-11 23:56:44 +01:00
commit 095d42c297
8 changed files with 147 additions and 9 deletions

View File

@ -1684,6 +1684,12 @@ function mod_lesson_core_calendar_provide_event_action(calendar_event $event,
// Apply overrides.
$lesson->update_effective_access($userid);
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]),

View File

@ -1733,6 +1733,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.
*

View File

@ -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();

View File

@ -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');
}
}

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.
*/

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.

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');
}
}

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();