MDL-61133 core_question: new submit_tags_form webservice
authorSimey Lameze <simey@moodle.com>
Wed, 31 Jan 2018 05:40:46 +0000 (13:40 +0800)
committerSimey Lameze <simey@moodle.com>
Mon, 5 Feb 2018 02:58:04 +0000 (10:58 +0800)
lib/db/services.php
question/classes/external.php
version.php

index e8e4227..3a712b7 100644 (file)
@@ -1114,6 +1114,13 @@ $functions = array(
         'capabilities'  => 'moodle/question:flag',
         'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
     ),
+    'core_question_submit_tags_form' => array(
+        'classname'     => 'core_question_external',
+        'methodname'    => 'submit_tags_form',
+        'description'   => 'Update the question tags.',
+        'type'          => 'write',
+        'ajax' => true,
+    ),
     'core_rating_get_item_ratings' => array(
         'classname' => 'core_rating_external',
         'methodname' => 'get_item_ratings',
index dae3272..6210ff7 100644 (file)
@@ -114,4 +114,79 @@ class core_question_external extends external_api {
             )
         );
     }
+
+    /**
+     * Returns description of method parameters.
+     *
+     * @return external_function_parameters.
+     */
+    public static function submit_tags_form_parameters() {
+        return new external_function_parameters([
+                'formdata' => new external_value(PARAM_RAW, 'The data from the tag form'),
+        ]);
+    }
+
+    /**
+     * Handles the tags form submission.
+     *
+     * @param string $formdata The question tag form data in a URI encoded param string
+     * @return array The created or modified question tag
+     * @throws moodle_exception
+     */
+    public static function submit_tags_form($formdata) {
+        global $USER, $DB, $CFG;
+
+        $data = [];
+        $result = ['status' => false];
+
+        // Parameter validation.
+        $params = self::validate_parameters(self::submit_tags_form_parameters(), ['formdata' => $formdata]);
+        $context = \context_user::instance($USER->id);
+
+        self::validate_context($context);
+        parse_str($params['formdata'], $data);
+
+        if (!empty($data['id'])) {
+            $questionid = clean_param($data['id'], PARAM_INT);
+            $question = $DB->get_record('question', array('id' => $questionid));
+
+            require_once($CFG->libdir . '/questionlib.php');
+            $canedit = question_has_capability_on($question, 'edit');
+
+            require_once($CFG->dirroot . '/question/type/tags_form.php');
+            $mform = new \core_question\form\tags(null, null, 'post', '', null, $canedit, $data);
+
+            if ($validateddata = $mform->get_data()) {
+                // Due to a mform bug, if there's no tags set on the tag element, it submits the name as the value.
+                // The only way to discover is checking if the tag element is an array.
+                if ($canedit) {
+                    if (is_array($validateddata->tags)) {
+                        $categorycontext = context::instance_by_id($validateddata->contextid);
+
+                        core_tag_tag::set_item_tags('core_question', 'question', $validateddata->id,
+                            $categorycontext, $validateddata->tags);
+
+                        $result['status'] = true;
+                    } else {
+                        // If the tags element is not array, this means we don't have any tags to be set.
+                        // This is the only way to assume the user removed all tags from the question.
+                        core_tag_tag::remove_all_item_tags('core_question', 'question', $validateddata->id);
+
+                        $result['status'] = true;
+                    }
+                }
+            }
+        }
+
+        return $result;
+    }
+
+    /**
+     * Returns description of method result value.
+     */
+    public static function  submit_tags_form_returns() {
+        return new external_single_structure([
+                'status' => new external_value(PARAM_BOOL, 'status: true if success')
+        ]);
+    }
 }
index a882670..2bdfd57 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2018020100.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2018020100.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.