Skip to content

Commit

Permalink
Merge branch 'MDL-63146-master' of git://github.com/rezaies/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Mar 14, 2019
2 parents bd9fb47 + 3f4d7f8 commit ca996c1
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 11 deletions.
20 changes: 13 additions & 7 deletions mod/quiz/lib.php
Expand Up @@ -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;
Expand Down
179 changes: 175 additions & 4 deletions mod/quiz/tests/lib_test.php
Expand Up @@ -505,15 +505,15 @@ public function test_quiz_core_calendar_provide_event_action_open() {

// 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,
'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.
// Now, log in as student.
$this->setUser($student);
// Create an action factory.
$factory = new \core_calendar\action_factory();
Expand All @@ -529,6 +529,36 @@ public function test_quiz_core_calendar_provide_event_action_open() {
$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();

Expand All @@ -551,22 +581,47 @@ public function test_quiz_core_calendar_provide_event_action_closed() {
$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();

$this->setAdminUser();

// 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,
'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.
// Now, log in as student.
$this->setUser($student);
// Create an action factory.
$factory = new \core_calendar\action_factory();
Expand All @@ -582,6 +637,36 @@ public function test_quiz_core_calendar_provide_event_action_open_in_future() {
$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;

Expand Down Expand Up @@ -619,6 +704,40 @@ public function test_quiz_core_calendar_provide_event_action_no_capability() {
$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;

Expand Down Expand Up @@ -674,6 +793,58 @@ public function test_quiz_core_calendar_provide_event_action_already_finished()
$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.
*
Expand Down

0 comments on commit ca996c1

Please sign in to comment.