diff --git a/mod/quiz/attemptlib.php b/mod/quiz/attemptlib.php index f45d721a1f2..c1fbf700500 100644 --- a/mod/quiz/attemptlib.php +++ b/mod/quiz/attemptlib.php @@ -2246,6 +2246,27 @@ class quiz_attempt { $event->trigger(); } + /** + * Trigger the attempt_reviewed event. + * + * @since Moodle 3.1 + */ + public function fire_attempt_reviewed_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_reviewed::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 cbbfff7ad2c..dd487ba7f64 100644 --- a/mod/quiz/classes/external.php +++ b/mod/quiz/classes/external.php @@ -1500,4 +1500,59 @@ class mod_quiz_external extends external_api { ); } + /** + * Describes the parameters for view_attempt_review. + * + * @return external_external_function_parameters + * @since Moodle 3.1 + */ + public static function view_attempt_review_parameters() { + return new external_function_parameters ( + array( + 'attemptid' => new external_value(PARAM_INT, 'attempt id'), + ) + ); + } + + /** + * Trigger the attempt reviewed event. + * + * @param int $attemptid attempt id + * @return array of warnings and status result + * @since Moodle 3.1 + */ + public static function view_attempt_review($attemptid) { + + $warnings = array(); + + $params = array( + 'attemptid' => $attemptid, + ); + $params = self::validate_parameters(self::view_attempt_review_parameters(), $params); + list($attemptobj, $displayoptions) = self::validate_attempt_review($params); + + // Log action. + $attemptobj->fire_attempt_reviewed_event(); + + $result = array(); + $result['status'] = true; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Describes the view_attempt_review return value. + * + * @return external_single_structure + * @since Moodle 3.1 + */ + public static function view_attempt_review_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 5e569f619aa..af51f0e6a8c 100644 --- a/mod/quiz/db/services.php +++ b/mod/quiz/db/services.php @@ -146,4 +146,13 @@ $functions = array( 'capabilities' => 'mod/quiz:attempt', 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE) ), + + 'mod_quiz_view_attempt_review' => array( + 'classname' => 'mod_quiz_external', + 'methodname' => 'view_attempt_review', + 'description' => 'Trigger the attempt reviewed event.', + 'type' => 'write', + 'capabilities' => 'mod/quiz:reviewmyattempts', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE) + ), ); diff --git a/mod/quiz/review.php b/mod/quiz/review.php index be91bd64b6a..6e78ef3542c 100644 --- a/mod/quiz/review.php +++ b/mod/quiz/review.php @@ -259,15 +259,4 @@ $PAGE->blocks->add_fake_block($navbc, reset($regions)); echo $output->review_page($attemptobj, $slots, $page, $showall, $lastpage, $options, $summarydata); // Trigger an event for this review. -$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_reviewed::create($params); -$event->add_record_snapshot('quiz_attempts', $attemptobj->get_attempt()); -$event->trigger(); +$attemptobj->fire_attempt_reviewed_event(); diff --git a/mod/quiz/tests/external_test.php b/mod/quiz/tests/external_test.php index 4bb59deb1b8..3361d7b2a7d 100644 --- a/mod/quiz/tests/external_test.php +++ b/mod/quiz/tests/external_test.php @@ -1329,4 +1329,37 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase { } + /** + * Test test_view_attempt_summary + */ + public function test_view_attempt_review() { + global $DB; + + // Create a new quiz with two questions and one attempt finished. + list($quiz, $context, $quizobj, $attempt, $attemptobj, $quba) = $this->create_quiz_with_questions(true, true); + + // Test user with full capabilities. + $this->setUser($this->student); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + $result = mod_quiz_external::view_attempt_review($attempt->id, 0); + $result = external_api::clean_returnvalue(mod_quiz_external::view_attempt_review_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_reviewed', $event); + $this->assertEquals($context, $event->get_context()); + $moodlequiz = new \moodle_url('/mod/quiz/review.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 610b0e4b757..03f211d4aa6 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015111614; +$plugin->version = 2015111615; $plugin->requires = 2015111000; $plugin->component = 'mod_quiz'; $plugin->cron = 60;