From bfc8fbea9782307763ad2837f1fe8dce2cd291ae Mon Sep 17 00:00:00 2001 From: Ryan Wyllie Date: Tue, 11 Apr 2017 03:22:51 +0000 Subject: [PATCH] MDL-58498 mod_lesson: no action after lesson submission Update the action event callback to not provide an action for the user if they have already attempted the lesson. --- mod/lesson/lib.php | 5 ++++ mod/lesson/tests/lib_test.php | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index 12c3b6beb3b..2bc36223c2f 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -1631,6 +1631,11 @@ function mod_lesson_core_calendar_provide_event_action(calendar_event $event, $cm = get_fast_modinfo($event->courseid)->instances['lesson'][$event->instance]; $lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST)); + if ($lesson->count_user_retries($USER->id)) { + // If the user has attempted the lesson then there is no further action for the user. + return null; + } + // Apply overrides. $lesson->update_effective_access($USER->id); diff --git a/mod/lesson/tests/lib_test.php b/mod/lesson/tests/lib_test.php index 7bd187d3852..1ca99dfa3c6 100644 --- a/mod/lesson/tests/lib_test.php +++ b/mod/lesson/tests/lib_test.php @@ -270,6 +270,56 @@ class mod_lesson_lib_testcase extends advanced_testcase { $this->assertTrue($actionevent->is_actionable()); } + public function test_lesson_core_calendar_provide_event_action_after_attempt() { + global $DB; + + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create user. + $student = self::getDataGenerator()->create_user(); + + // 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); + + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual'); + + $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson'); + $tfrecord = $generator->create_question_truefalse($lesson); + + // Now, do something in the lesson. + $this->setUser($student); + mod_lesson_external::launch_attempt($lesson->id); + $data = array( + array( + 'name' => 'answerid', + 'value' => $DB->get_field('lesson_answers', 'id', array('pageid' => $tfrecord->id, 'jumpto' => -1)), + ), + array( + 'name' => '_qf__lesson_display_answer_form_truefalse', + 'value' => 1, + ) + ); + mod_lesson_external::process_page($lesson->id, $tfrecord->id, $data); + mod_lesson_external::finish_attempt($lesson->id); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $action = mod_lesson_core_calendar_provide_event_action($event, $factory); + + // Confirm there was no action for the user. + $this->assertNull($action); + } + /** * Creates an action event. *