';
+ echo '
';
$this->start_table();
$rowcount = 0;
foreach ($questions as $question) {
diff --git a/question/classes/external.php b/question/classes/external.php
index dae3272acc2..6210ff7f3a7 100644
--- a/question/classes/external.php
+++ b/question/classes/external.php
@@ -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')
+ ]);
+ }
}
diff --git a/question/lib.php b/question/lib.php
new file mode 100644
index 00000000000..5c04b33831d
--- /dev/null
+++ b/question/lib.php
@@ -0,0 +1,67 @@
+.
+
+/**
+ * Question related functions.
+ *
+ * This file was created just because Fragment API expects callbacks to be defined on lib.php.
+ *
+ * Please, do not add new functions to this file.
+ *
+ * @package core_question
+ * @copyright 2018 Simey Lameze
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Question tags fragment callback.
+ *
+ * @param array $args Arguments to the form.
+ * @return null|string The rendered form.
+ */
+function core_question_output_fragment_tags_form($args) {
+
+ if (!empty($args['id'])) {
+ global $CFG, $DB;
+ require_once($CFG->dirroot . '/question/type/tags_form.php');
+ require_once($CFG->libdir . '/questionlib.php');
+ $id = clean_param($args['id'], PARAM_INT);
+
+ $question = $DB->get_record('question', ['id' => $id]);
+ $category = $DB->get_record('question_categories', array('id' => $question->category));
+ $context = \context::instance_by_id($category->contextid);
+
+ $toform = new stdClass();
+ $toform->id = $question->id;
+ $toform->questioncategory = $category->name;
+ $toform->questionname = $question->name;
+ $toform->categoryid = $category->id;
+ $toform->contextid = $category->contextid;
+ $toform->context = $context->get_context_name();
+
+ if (core_tag_tag::is_enabled('core_question', 'question')) {
+ $toform->tags = core_tag_tag::get_item_tags_array('core_question', 'question', $question->id);
+ }
+
+ $canedit = question_has_capability_on($question, 'edit');
+ $mform = new \core_question\form\tags(null, null, 'post', '', null, $canedit, $toform);
+ $mform->set_data($toform);
+
+ return $mform->render();
+ }
+}
diff --git a/question/type/tags_form.php b/question/type/tags_form.php
new file mode 100644
index 00000000000..f2a59c66df7
--- /dev/null
+++ b/question/type/tags_form.php
@@ -0,0 +1,61 @@
+.
+
+/**
+ * The mform to manage question tags.
+ *
+ * @package core_question
+ * @copyright 2018 Simey Lameze
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core_question\form;
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->dirroot . '/lib/formslib.php');
+
+/**
+ * The mform class for manage question tags.
+ *
+ * @copyright 2018 Simey Lameze
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tags extends \moodleform {
+
+ /**
+ * The form definition
+ */
+ public function definition() {
+ $mform = $this->_form;
+
+ $mform->addElement('hidden', 'id');
+ $mform->setType('id', PARAM_INT);
+
+ $mform->addElement('hidden', 'categoryid');
+ $mform->setType('categoryid', PARAM_INT);
+
+ $mform->addElement('hidden', 'contextid');
+ $mform->setType('contextid', PARAM_INT);
+
+ $mform->addElement('static', 'questionname', get_string('questionname', 'question'));
+ $mform->addElement('static', 'questioncategory', get_string('categorycurrent', 'question'));
+ $mform->addElement('static', 'context', '');
+
+ $mform->addElement('tags', 'tags', get_string('tags'),
+ ['itemtype' => 'question', 'component' => 'core_question']);
+ }
+}
diff --git a/version.php b/version.php
index a8826708737..2bdfd579954 100644
--- a/version.php
+++ b/version.php
@@ -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.