MDL-52888 mod_quiz: New Web Service mod_quiz_view_attempt_review
authorJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 25 Jan 2016 14:19:26 +0000 (15:19 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Fri, 18 Mar 2016 08:50:06 +0000 (09:50 +0100)
mod/quiz/attemptlib.php
mod/quiz/classes/external.php
mod/quiz/db/services.php
mod/quiz/review.php
mod/quiz/tests/external_test.php
mod/quiz/version.php

index f45d721..c1fbf70 100644 (file)
@@ -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();
+    }
+
 }
 
 
index cbbfff7..dd487ba 100644 (file)
@@ -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(),
+            )
+        );
+    }
+
 }
index 5e569f6..af51f0e 100644 (file)
@@ -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)
+    ),
 );
index be91bd6..6e78ef3 100644 (file)
@@ -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();
index 4bb59de..3361d7b 100644 (file)
@@ -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());
+
+    }
+
 }
index 610b0e4..03f211d 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2015111614;
+$plugin->version   = 2015111615;
 $plugin->requires  = 2015111000;
 $plugin->component = 'mod_quiz';
 $plugin->cron      = 60;