Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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->libdir . '/externallib.php');
  22  require_once($CFG->dirroot . '/question/engine/lib.php');
  23  require_once($CFG->dirroot . '/question/engine/datalib.php');
  24  require_once($CFG->libdir . '/questionlib.php');
  25  
  26  use core_tag_tag;
  27  use external_api;
  28  use external_function_parameters;
  29  use external_single_structure;
  30  use external_value;
  31  use qbank_tagquestion\form\tags_form;
  32  
  33  /**
  34   * External qbank_tagquestion API.
  35   *
  36   * @package    qbank_tagquestion
  37   * @copyright  2016 Pau Ferrer <pau@moodle.com>
  38   * @author     2021 Safat Shahin <safatshahin@catalyst-au.net>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class submit_tags extends external_api {
  42      /**
  43       * Returns description of method parameters.
  44       *
  45       * @return external_function_parameters.
  46       */
  47      public static function execute_parameters() {
  48          return new external_function_parameters([
  49                  'questionid' => new external_value(PARAM_INT, 'The question id'),
  50                  'contextid' => new external_value(PARAM_INT, 'The editing context id'),
  51                  'formdata' => new external_value(PARAM_RAW, 'The data from the tag form'),
  52          ]);
  53      }
  54  
  55      /**
  56       * Handles the tags form submission.
  57       *
  58       * @param int $questionid The question id.
  59       * @param int $contextid The editing context id.
  60       * @param string $formdata The question tag form data in a URI encoded param string
  61       * @return array The created or modified question tag
  62       */
  63      public static function execute($questionid, $contextid, $formdata) {
  64          global $DB, $CFG;
  65  
  66          $data = [];
  67          $result = ['status' => false];
  68  
  69          // Parameter validation.
  70          $params = self::validate_parameters(self::execute_parameters(), [
  71                  'questionid' => $questionid,
  72                  'contextid' => $contextid,
  73                  'formdata' => $formdata
  74          ]);
  75  
  76          $editingcontext = \context::instance_by_id($params['contextid']);
  77          self::validate_context($editingcontext);
  78          parse_str($params['formdata'], $data);
  79  
  80          if (!$question = $DB->get_record_sql('
  81                  SELECT q.*, qc.contextid
  82                    FROM {question} q
  83                    JOIN {question_versions} qv ON qv.questionid = q.id
  84                    JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
  85                    JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
  86                   WHERE q.id = ?', [$questionid])) {
  87              throw new \moodle_exception('questiondoesnotexist', 'question');
  88          }
  89  
  90          $cantag = question_has_capability_on($question, 'tag');
  91          $questioncontext = \context::instance_by_id($question->contextid);
  92          $contexts = new \core_question\local\bank\question_edit_contexts($editingcontext);
  93  
  94          $formoptions = [
  95                  'editingcontext' => $editingcontext,
  96                  'questioncontext' => $questioncontext,
  97                  'contexts' => $contexts->all()
  98          ];
  99  
 100          $mform = new tags_form(null, $formoptions, 'post', '', null, $cantag, $data);
 101  
 102          if ($validateddata = $mform->get_data()) {
 103              if ($cantag) {
 104                  if (isset($validateddata->tags)) {
 105                      // Due to a mform bug, if there's no tags set on the tag element, it submits the name as the value.
 106                      // The only way to discover is checking if the tag element is an array.
 107                      $tags = is_array($validateddata->tags) ? $validateddata->tags : [];
 108  
 109                      core_tag_tag::set_item_tags('core_question', 'question', $validateddata->id,
 110                              $questioncontext, $tags);
 111  
 112                      $result['status'] = true;
 113                  }
 114  
 115                  if (isset($validateddata->coursetags)) {
 116                      $coursetags = is_array($validateddata->coursetags) ? $validateddata->coursetags : [];
 117                      core_tag_tag::set_item_tags('core_question', 'question', $validateddata->id,
 118                              $editingcontext->get_course_context(false), $coursetags);
 119  
 120                      $result['status'] = true;
 121                  }
 122              }
 123          }
 124  
 125          return $result;
 126      }
 127  
 128      /**
 129       * Returns description of method result value.
 130       */
 131      public static function  execute_returns() {
 132          return new external_single_structure([
 133                  'status' => new external_value(PARAM_BOOL, 'status: true if success')
 134          ]);
 135      }
 136  
 137  }