mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 00:12:56 +02:00
Merge branch 'MDL-63146-master' of git://github.com/rezaies/moodle
This commit is contained in:
commit
ca996c1f48
@ -2135,37 +2135,43 @@ function mod_quiz_get_fontawesome_icon_map() {
|
||||
*
|
||||
* @param calendar_event $event
|
||||
* @param \core_calendar\action_factory $factory
|
||||
* @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
|
||||
* @return \core_calendar\local\event\entities\action_interface|null
|
||||
*/
|
||||
function mod_quiz_core_calendar_provide_event_action(calendar_event $event,
|
||||
\core_calendar\action_factory $factory) {
|
||||
\core_calendar\action_factory $factory,
|
||||
int $userid = 0) {
|
||||
global $CFG, $USER;
|
||||
|
||||
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
||||
|
||||
$cm = get_fast_modinfo($event->courseid)->instances['quiz'][$event->instance];
|
||||
$quizobj = quiz::create($cm->instance, $USER->id);
|
||||
if (empty($userid)) {
|
||||
$userid = $USER->id;
|
||||
}
|
||||
|
||||
$cm = get_fast_modinfo($event->courseid, $userid)->instances['quiz'][$event->instance];
|
||||
$quizobj = quiz::create($cm->instance, $userid);
|
||||
$quiz = $quizobj->get_quiz();
|
||||
|
||||
// Check they have capabilities allowing them to view the quiz.
|
||||
if (!has_any_capability(array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'), $quizobj->get_context())) {
|
||||
if (!has_any_capability(['mod/quiz:reviewmyattempts', 'mod/quiz:attempt'], $quizobj->get_context(), $userid)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
quiz_update_effective_access($quiz, $USER->id);
|
||||
quiz_update_effective_access($quiz, $userid);
|
||||
|
||||
// Check if quiz is closed, if so don't display it.
|
||||
if (!empty($quiz->timeclose) && $quiz->timeclose <= time()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$quizobj->is_participant($USER->id)) {
|
||||
if (!$quizobj->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;
|
||||
}
|
||||
|
||||
$attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id);
|
||||
$attempts = quiz_get_user_attempts($quizobj->get_quizid(), $userid);
|
||||
if (!empty($attempts)) {
|
||||
// The student's last attempt is finished.
|
||||
return null;
|
||||
|
@ -505,7 +505,7 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
// Create a teacher and enrol into the course.
|
||||
// Create a student 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,
|
||||
@ -513,7 +513,7 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
||||
|
||||
// Create a calendar event.
|
||||
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
|
||||
// Now, log in as teacher.
|
||||
// Now, log in as student.
|
||||
$this->setUser($student);
|
||||
// Create an action factory.
|
||||
$factory = new \core_calendar\action_factory();
|
||||
@ -529,6 +529,36 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
||||
$this->assertTrue($actionevent->is_actionable());
|
||||
}
|
||||
|
||||
public function test_quiz_core_calendar_provide_event_action_open_for_user() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
// Create a student 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);
|
||||
|
||||
// Create an action factory.
|
||||
$factory = new \core_calendar\action_factory();
|
||||
|
||||
// Decorate action event for the student.
|
||||
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
|
||||
|
||||
// Confirm the event was decorated.
|
||||
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
||||
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
|
||||
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
||||
$this->assertEquals(1, $actionevent->get_item_count());
|
||||
$this->assertTrue($actionevent->is_actionable());
|
||||
}
|
||||
|
||||
public function test_quiz_core_calendar_provide_event_action_closed() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
@ -551,6 +581,31 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
||||
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
|
||||
}
|
||||
|
||||
public function test_quiz_core_calendar_provide_event_action_closed_for_user() {
|
||||
$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 quiz.
|
||||
$quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
|
||||
'timeclose' => time() - DAYSECS));
|
||||
|
||||
// Create a calendar event.
|
||||
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
|
||||
|
||||
// Create an action factory.
|
||||
$factory = new \core_calendar\action_factory();
|
||||
|
||||
// Confirm the result was null.
|
||||
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
|
||||
}
|
||||
|
||||
public function test_quiz_core_calendar_provide_event_action_open_in_future() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
@ -558,7 +613,7 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
// Create a teacher and enrol into the course.
|
||||
// Create a student 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,
|
||||
@ -566,7 +621,7 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
||||
|
||||
// Create a calendar event.
|
||||
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_CLOSE);
|
||||
// Now, log in as teacher.
|
||||
// Now, log in as student.
|
||||
$this->setUser($student);
|
||||
// Create an action factory.
|
||||
$factory = new \core_calendar\action_factory();
|
||||
@ -582,6 +637,36 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
||||
$this->assertFalse($actionevent->is_actionable());
|
||||
}
|
||||
|
||||
public function test_quiz_core_calendar_provide_event_action_open_in_future_for_user() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
// Create a student 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);
|
||||
|
||||
// Create an action factory.
|
||||
$factory = new \core_calendar\action_factory();
|
||||
|
||||
// Decorate action event for the student.
|
||||
$actionevent = mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id);
|
||||
|
||||
// Confirm the event was decorated.
|
||||
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
||||
$this->assertEquals(get_string('attemptquiznow', 'quiz'), $actionevent->get_name());
|
||||
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
||||
$this->assertEquals(1, $actionevent->get_item_count());
|
||||
$this->assertFalse($actionevent->is_actionable());
|
||||
}
|
||||
|
||||
public function test_quiz_core_calendar_provide_event_action_no_capability() {
|
||||
global $DB;
|
||||
|
||||
@ -619,6 +704,40 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
||||
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
|
||||
}
|
||||
|
||||
public function test_quiz_core_calendar_provide_event_action_no_capability_for_user() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
// Create a student.
|
||||
$student = $this->getDataGenerator()->create_user();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
|
||||
// Enrol student.
|
||||
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
|
||||
|
||||
// Create a quiz.
|
||||
$quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
|
||||
|
||||
// Remove the permission to attempt or review the quiz for the student role.
|
||||
$coursecontext = context_course::instance($course->id);
|
||||
assign_capability('mod/quiz:reviewmyattempts', CAP_PROHIBIT, $studentrole->id, $coursecontext);
|
||||
assign_capability('mod/quiz:attempt', CAP_PROHIBIT, $studentrole->id, $coursecontext);
|
||||
|
||||
// Create a calendar event.
|
||||
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
|
||||
|
||||
// Create an action factory.
|
||||
$factory = new \core_calendar\action_factory();
|
||||
|
||||
// Confirm null is returned.
|
||||
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
|
||||
}
|
||||
|
||||
public function test_quiz_core_calendar_provide_event_action_already_finished() {
|
||||
global $DB;
|
||||
|
||||
@ -674,6 +793,58 @@ class mod_quiz_lib_testcase extends advanced_testcase {
|
||||
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory));
|
||||
}
|
||||
|
||||
public function test_quiz_core_calendar_provide_event_action_already_finished_for_user() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
// Create a student.
|
||||
$student = $this->getDataGenerator()->create_user();
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
|
||||
// Enrol student.
|
||||
$this->assertTrue($this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id));
|
||||
|
||||
// Create a quiz.
|
||||
$quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id,
|
||||
'sumgrades' => 1));
|
||||
|
||||
// Add a question to the quiz.
|
||||
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
||||
$cat = $questiongenerator->create_question_category();
|
||||
$question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
|
||||
quiz_add_quiz_question($question->id, $quiz);
|
||||
|
||||
// Get the quiz object.
|
||||
$quizobj = quiz::create($quiz->id, $student->id);
|
||||
|
||||
// Create an attempt for the student in the quiz.
|
||||
$timenow = time();
|
||||
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $student->id);
|
||||
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
||||
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
||||
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
|
||||
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
||||
|
||||
// Finish the attempt.
|
||||
$attemptobj = quiz_attempt::create($attempt->id);
|
||||
$attemptobj->process_finish($timenow, false);
|
||||
|
||||
// Create a calendar event.
|
||||
$event = $this->create_action_event($course->id, $quiz->id, QUIZ_EVENT_TYPE_OPEN);
|
||||
|
||||
// Create an action factory.
|
||||
$factory = new \core_calendar\action_factory();
|
||||
|
||||
// Confirm null is returned.
|
||||
$this->assertNull(mod_quiz_core_calendar_provide_event_action($event, $factory, $student->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an action event.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user