diff --git a/mod/quiz/attemptlib.php b/mod/quiz/attemptlib.php index b367917adad..f45d721a1f2 100644 --- a/mod/quiz/attemptlib.php +++ b/mod/quiz/attemptlib.php @@ -2225,6 +2225,27 @@ class quiz_attempt { $event->trigger(); } + /** + * Trigger the attempt_summary_viewed event. + * + * @since Moodle 3.1 + */ + public function fire_attempt_summary_viewed_event() { + + $params = array( + 'objectid' => $this->get_attemptid(), + 'relateduserid' => $this->get_userid(), + 'courseid' => $this->get_courseid(), + 'context' => context_module::instance($this->get_cmid()), + 'other' => array( + 'quizid' => $this->get_quizid() + ) + ); + $event = \mod_quiz\event\attempt_summary_viewed::create($params); + $event->add_record_snapshot('quiz_attempts', $this->get_attempt()); + $event->trigger(); + } + } diff --git a/mod/quiz/classes/external.php b/mod/quiz/classes/external.php index 9363a29fb64..cbbfff7ad2c 100644 --- a/mod/quiz/classes/external.php +++ b/mod/quiz/classes/external.php @@ -1445,4 +1445,59 @@ class mod_quiz_external extends external_api { ); } + /** + * Describes the parameters for view_attempt_summary. + * + * @return external_external_function_parameters + * @since Moodle 3.1 + */ + public static function view_attempt_summary_parameters() { + return new external_function_parameters ( + array( + 'attemptid' => new external_value(PARAM_INT, 'attempt id'), + ) + ); + } + + /** + * Trigger the attempt summary viewed event. + * + * @param int $attemptid attempt id + * @return array of warnings and status result + * @since Moodle 3.1 + */ + public static function view_attempt_summary($attemptid) { + + $warnings = array(); + + $params = array( + 'attemptid' => $attemptid, + ); + $params = self::validate_parameters(self::view_attempt_summary_parameters(), $params); + list($attemptobj, $messages) = self::validate_attempt($params); + + // Log action. + $attemptobj->fire_attempt_summary_viewed_event(); + + $result = array(); + $result['status'] = true; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Describes the view_attempt_summary return value. + * + * @return external_single_structure + * @since Moodle 3.1 + */ + public static function view_attempt_summary_returns() { + return new external_single_structure( + array( + 'status' => new external_value(PARAM_BOOL, 'status: true if success'), + 'warnings' => new external_warnings(), + ) + ); + } + } diff --git a/mod/quiz/db/services.php b/mod/quiz/db/services.php index 33c9095a642..5e569f619aa 100644 --- a/mod/quiz/db/services.php +++ b/mod/quiz/db/services.php @@ -137,4 +137,13 @@ $functions = array( 'capabilities' => 'mod/quiz:attempt', 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE) ), + + 'mod_quiz_view_attempt_summary' => array( + 'classname' => 'mod_quiz_external', + 'methodname' => 'view_attempt_summary', + 'description' => 'Trigger the attempt summary viewed event.', + 'type' => 'write', + 'capabilities' => 'mod/quiz:attempt', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE) + ), ); diff --git a/mod/quiz/summary.php b/mod/quiz/summary.php index b1ac720ff57..4ccec15d0b8 100644 --- a/mod/quiz/summary.php +++ b/mod/quiz/summary.php @@ -93,15 +93,4 @@ $PAGE->set_heading($attemptobj->get_course()->fullname); echo $output->summary_page($attemptobj, $displayoptions); // Log this page view. -$params = array( - 'objectid' => $attemptobj->get_attemptid(), - 'relateduserid' => $attemptobj->get_userid(), - 'courseid' => $attemptobj->get_courseid(), - 'context' => context_module::instance($attemptobj->get_cmid()), - 'other' => array( - 'quizid' => $attemptobj->get_quizid() - ) -); -$event = \mod_quiz\event\attempt_summary_viewed::create($params); -$event->add_record_snapshot('quiz_attempts', $attemptobj->get_attempt()); -$event->trigger(); +$attemptobj->fire_attempt_summary_viewed_event(); diff --git a/mod/quiz/tests/external_test.php b/mod/quiz/tests/external_test.php index 942dd8f629c..4bb59deb1b8 100644 --- a/mod/quiz/tests/external_test.php +++ b/mod/quiz/tests/external_test.php @@ -1296,4 +1296,37 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase { } + /** + * Test test_view_attempt_summary + */ + public function test_view_attempt_summary() { + global $DB; + + // Create a new quiz with two questions and one attempt started. + list($quiz, $context, $quizobj, $attempt, $attemptobj, $quba) = $this->create_quiz_with_questions(true, false); + + // Test user with full capabilities. + $this->setUser($this->student); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + $result = mod_quiz_external::view_attempt_summary($attempt->id, 0); + $result = external_api::clean_returnvalue(mod_quiz_external::view_attempt_summary_returns(), $result); + $this->assertTrue($result['status']); + + $events = $sink->get_events(); + $this->assertCount(1, $events); + $event = array_shift($events); + + // Checking that the event contains the expected values. + $this->assertInstanceOf('\mod_quiz\event\attempt_summary_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $moodlequiz = new \moodle_url('/mod/quiz/summary.php', array('attempt' => $attempt->id)); + $this->assertEquals($moodlequiz, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + + } + } diff --git a/mod/quiz/version.php b/mod/quiz/version.php index 8fa4cad4995..610b0e4b757 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015111613; +$plugin->version = 2015111614; $plugin->requires = 2015111000; $plugin->component = 'mod_quiz'; $plugin->cron = 60;