Differences Between: [Versions 400 and 402] [Versions 401 and 402]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace qbank_tagquestion\external; 18 19 defined('MOODLE_INTERNAL') || die(); 20 21 require_once($CFG->dirroot . '/question/engine/lib.php'); 22 require_once($CFG->dirroot . '/question/engine/datalib.php'); 23 require_once($CFG->libdir . '/questionlib.php'); 24 25 use core_tag_tag; 26 use core_external\external_api; 27 use core_external\external_function_parameters; 28 use core_external\external_single_structure; 29 use core_external\external_value; 30 use qbank_tagquestion\form\tags_form; 31 32 /** 33 * External qbank_tagquestion API. 34 * 35 * @package qbank_tagquestion 36 * @copyright 2016 Pau Ferrer <pau@moodle.com> 37 * @author 2021 Safat Shahin <safatshahin@catalyst-au.net> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class submit_tags extends external_api { 41 /** 42 * Returns description of method parameters. 43 * 44 * @return external_function_parameters. 45 */ 46 public static function execute_parameters() { 47 return new external_function_parameters([ 48 'questionid' => new external_value(PARAM_INT, 'The question id'), 49 'contextid' => new external_value(PARAM_INT, 'The editing context id'), 50 'formdata' => new external_value(PARAM_RAW, 'The data from the tag form'), 51 ]); 52 } 53 54 /** 55 * Handles the tags form submission. 56 * 57 * @param int $questionid The question id. 58 * @param int $contextid The editing context id. 59 * @param string $formdata The question tag form data in a URI encoded param string 60 * @return array The created or modified question tag 61 */ 62 public static function execute($questionid, $contextid, $formdata) { 63 global $DB, $CFG; 64 65 $data = []; 66 $result = ['status' => false]; 67 68 // Parameter validation. 69 $params = self::validate_parameters(self::execute_parameters(), [ 70 'questionid' => $questionid, 71 'contextid' => $contextid, 72 'formdata' => $formdata 73 ]); 74 75 $editingcontext = \context::instance_by_id($params['contextid']); 76 self::validate_context($editingcontext); 77 parse_str($params['formdata'], $data); 78 79 if (!$question = $DB->get_record_sql(' 80 SELECT q.*, qc.contextid 81 FROM {question} q 82 JOIN {question_versions} qv ON qv.questionid = q.id 83 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid 84 JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid 85 WHERE q.id = ?', [$questionid])) { 86 throw new \moodle_exception('questiondoesnotexist', 'question'); 87 } 88 89 $cantag = question_has_capability_on($question, 'tag'); 90 $questioncontext = \context::instance_by_id($question->contextid); 91 $contexts = new \core_question\local\bank\question_edit_contexts($editingcontext); 92 93 $formoptions = [ 94 'editingcontext' => $editingcontext, 95 'questioncontext' => $questioncontext, 96 'contexts' => $contexts->all() 97 ]; 98 99 $mform = new tags_form(null, $formoptions, 'post', '', null, $cantag, $data); 100 101 if ($validateddata = $mform->get_data()) { 102 if ($cantag) { 103 if (isset($validateddata->tags)) { 104 // Due to a mform bug, if there's no tags set on the tag element, it submits the name as the value. 105 // The only way to discover is checking if the tag element is an array. 106 $tags = is_array($validateddata->tags) ? $validateddata->tags : []; 107 108 core_tag_tag::set_item_tags('core_question', 'question', $validateddata->id, 109 $questioncontext, $tags); 110 111 $result['status'] = true; 112 } 113 114 if (isset($validateddata->coursetags)) { 115 $coursetags = is_array($validateddata->coursetags) ? $validateddata->coursetags : []; 116 core_tag_tag::set_item_tags('core_question', 'question', $validateddata->id, 117 $editingcontext->get_course_context(false), $coursetags); 118 119 $result['status'] = true; 120 } 121 } 122 } 123 124 return $result; 125 } 126 127 /** 128 * Returns description of method result value. 129 */ 130 public static function execute_returns() { 131 return new external_single_structure([ 132 'status' => new external_value(PARAM_BOOL, 'status: true if success') 133 ]); 134 } 135 136 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body