MDL-57814 mod_feedback: New WS mod_feedback_get_current_completed_tmp
authorJuan Leyva <juanleyvadelgado@gmail.com>
Thu, 2 Feb 2017 11:07:09 +0000 (12:07 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 15 Mar 2017 14:31:38 +0000 (15:31 +0100)
mod/feedback/classes/external.php
mod/feedback/db/services.php
mod/feedback/tests/external_test.php
mod/feedback/version.php

index 07d4bde..e32c63d 100644 (file)
@@ -29,6 +29,7 @@ defined('MOODLE_INTERNAL') || die;
 require_once("$CFG->libdir/externallib.php");
 
 use mod_feedback\external\feedback_summary_exporter;
+use mod_feedback\external\feedback_completedtmp_exporter;
 
 /**
  * Feedback external functions
@@ -300,4 +301,61 @@ class mod_feedback_external extends external_api {
             )
         );
     }
+
+    /**
+     * Describes the parameters for get_current_completed_tmp.
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.3
+     */
+    public static function get_current_completed_tmp_parameters() {
+        return new external_function_parameters (
+            array(
+                'feedbackid' => new external_value(PARAM_INT, 'Feedback instance id'),
+            )
+        );
+    }
+
+    /**
+     * Returns the temporary completion record for the current user.
+     *
+     * @param int $feedbackid feedback instance id
+     * @return array of warnings and status result
+     * @since Moodle 3.3
+     * @throws moodle_exception
+     */
+    public static function get_current_completed_tmp($feedbackid) {
+        global $PAGE;
+
+        $params = array('feedbackid' => $feedbackid);
+        $params = self::validate_parameters(self::get_current_completed_tmp_parameters(), $params);
+        $warnings = array();
+
+        list($feedback, $course, $cm, $context) = self::validate_feedback($params['feedbackid']);
+        $feedbackcompletion = new mod_feedback_completion($feedback, $cm, $course->id);
+
+        if ($completed = $feedbackcompletion->get_current_completed_tmp()) {
+            $exporter = new feedback_completedtmp_exporter($completed);
+            return array(
+                'feedback' => $exporter->export($PAGE->get_renderer('core')),
+                'warnings' => $warnings,
+            );
+        }
+        throw new moodle_exception('not_started', 'feedback');
+    }
+
+    /**
+     * Describes the get_current_completed_tmp return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 3.3
+     */
+    public static function get_current_completed_tmp_returns() {
+        return new external_single_structure(
+            array(
+                'feedback' => feedback_completedtmp_exporter::get_read_structure(),
+                'warnings' => new external_warnings(),
+            )
+        );
+    }
 }
index 0535a51..5ae0cd7 100644 (file)
@@ -53,4 +53,12 @@ $functions = array(
         'capabilities'  => 'mod/feedback:view',
         'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
     ),
+    'mod_feedback_get_current_completed_tmp' => array(
+        'classname'     => 'mod_feedback_external',
+        'methodname'    => 'get_current_completed_tmp',
+        'description'   => 'Returns the temporary completion record for the current user.',
+        'type'          => 'read',
+        'capabilities'  => 'mod/feedback:view',
+        'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+    ),
 );
index 9613816..0fb7c06 100644 (file)
@@ -273,4 +273,32 @@ class mod_feedback_external_testcase extends externallib_advanced_testcase {
         $this->assertEventContextNotUsed($event);
         $this->assertNotEmpty($event->get_name());
     }
+
+    /**
+     * Test get_current_completed_tmp.
+     */
+    public function test_get_current_completed_tmp() {
+        global $DB;
+
+        // Force non anonymous.
+        $DB->set_field('feedback', 'anonymous', 0, array('id' => $this->feedback->id));
+        // Add a completed_tmp record.
+        $record = [
+            'feedback' => $this->feedback->id,
+            'userid' => $this->student->id,
+            'guestid' => '',
+            'timemodified' => time() - DAYSECS,
+            'random_response' => 0,
+            'anonymous_response' => 2,
+            'courseid' => $this->course->id,
+        ];
+        $record['id'] = $DB->insert_record('feedback_completedtmp', (object) $record);
+
+        // Test user with full capabilities.
+        $this->setUser($this->student);
+
+        $result = mod_feedback_external::get_current_completed_tmp($this->feedback->id);
+        $result = external_api::clean_returnvalue(mod_feedback_external::get_current_completed_tmp_returns(), $result);
+        $this->assertEquals($record['id'], $result['feedback']['id']);
+    }
 }
index b695790..b833fa5 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2016120503;       // The current module version (Date: YYYYMMDDXX)
+$plugin->version   = 2016120504;       // The current module version (Date: YYYYMMDDXX)
 $plugin->requires  = 2016112900;    // Requires this Moodle version
 $plugin->component = 'mod_feedback';   // Full name of the plugin (used for diagnostics)
 $plugin->cron      = 0;