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 mod_quiz\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 external_api;
  27  use external_description;
  28  use external_function_parameters;
  29  use external_single_structure;
  30  use external_value;
  31  use stdClass;
  32  
  33  /**
  34   * External api for changing the question version in the quiz.
  35   *
  36   * @package    mod_quiz
  37   * @copyright  2021 Catalyst IT Australia Pty Ltd
  38   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class submit_question_version extends external_api {
  42  
  43      /**
  44       * Parameters for the submit_question_version.
  45       *
  46       * @return \external_function_parameters
  47       */
  48      public static function execute_parameters(): external_function_parameters {
  49          return new external_function_parameters (
  50              [
  51                  'slotid' => new external_value(PARAM_INT, ''),
  52                  'newversion' => new external_value(PARAM_INT, '')
  53              ]
  54          );
  55      }
  56  
  57      /**
  58       * Set the questions slot parameters to display the question template.
  59       *
  60       * @param int $slotid Slot id to display.
  61       * @param int $newversion the version to set. 0 means 'always latest'.
  62       * @return array
  63       */
  64      public static function execute(int $slotid, int $newversion): array {
  65          global $DB;
  66          $params = [
  67              'slotid' => $slotid,
  68              'newversion' => $newversion
  69          ];
  70          $params = self::validate_parameters(self::execute_parameters(), $params);
  71          $response = ['result' => false];
  72          // Get the required data.
  73          $referencedata = $DB->get_record('question_references',
  74              ['itemid' => $params['slotid'], 'component' => 'mod_quiz', 'questionarea' => 'slot']);
  75          $slotdata = $DB->get_record('quiz_slots', ['id' => $slotid]);
  76  
  77          // Capability check.
  78          list($course, $cm) = get_course_and_cm_from_instance($slotdata->quizid, 'quiz');
  79          $context = \context_module::instance($cm->id);
  80          self::validate_context($context);
  81          require_capability('mod/quiz:manage', $context);
  82  
  83          $reference = new stdClass();
  84          $reference->id = $referencedata->id;
  85          if ($params['newversion'] === 0) {
  86              $reference->version = null;
  87          } else {
  88              $reference->version = $params['newversion'];
  89          }
  90          $response['result'] = $DB->update_record('question_references', $reference);
  91          return $response;
  92      }
  93  
  94      /**
  95       * Define the webservice response.
  96       *
  97       * @return external_description
  98       */
  99      public static function execute_returns() {
 100          return new external_single_structure(
 101              [
 102                  'result' => new external_value(PARAM_BOOL, '')
 103              ]
 104          );
 105      }
 106  }