Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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