MDL-58498 mod_lesson: no action after lesson submission
authorRyan Wyllie <ryan@moodle.com>
Tue, 11 Apr 2017 03:22:51 +0000 (03:22 +0000)
committerRyan Wyllie <ryan@moodle.com>
Tue, 11 Apr 2017 03:26:08 +0000 (03:26 +0000)
Update the action event callback to not provide an action for the user
if they have already attempted the lesson.

mod/lesson/lib.php
mod/lesson/tests/lib_test.php

index 12c3b6b..2bc3622 100644 (file)
@@ -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);
 
index 7bd187d..1ca99df 100644 (file)
@@ -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.
      *