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 qbank_editquestion\external;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  require_once($CFG->dirroot . '/question/engine/bank.php');
  22  
  23  use core_external\external_api;
  24  use core_external\external_function_parameters;
  25  use core_external\external_single_structure;
  26  use core_external\external_value;
  27  use qbank_editquestion\editquestion_helper;
  28  use question_bank;
  29  
  30  /**
  31   * Update question status external api.
  32   *
  33   * @package    qbank_editquestion
  34   * @copyright  2021 Catalyst IT Australia Pty Ltd
  35   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class update_question_version_status extends external_api {
  39  
  40      /**
  41       * Returns description of method parameters.
  42       *
  43       * @return external_function_parameters.
  44       */
  45      public static function execute_parameters() {
  46          return new external_function_parameters([
  47              'questionid' => new external_value(PARAM_INT, 'The question id'),
  48              'status' => new external_value(PARAM_TEXT, 'The updated question status')
  49          ]);
  50      }
  51  
  52      /**
  53       * Handles the status form submission.
  54       *
  55       * @param int $questionid The question id.
  56       * @param string $status The updated question status.
  57       * @return array The created or modified question tag
  58       */
  59      public static function execute($questionid, $status) {
  60          global $DB;
  61  
  62          $result = [
  63              'status' => false,
  64              'statusname' => '',
  65              'error' => ''
  66          ];
  67  
  68          // Parameter validation.
  69          $params = self::validate_parameters(self::execute_parameters(), [
  70              'questionid' => $questionid,
  71              'status' => $status
  72          ]);
  73  
  74          $statuslist = editquestion_helper::get_question_status_list();
  75          $statusexists = array_key_exists($status, $statuslist);
  76          if (!$statusexists) {
  77              return [
  78                  'status' => false,
  79                  'statusname' => '',
  80                  'error' => get_string('unrecognizedstatus', 'qbank_editquestion')
  81              ];
  82          }
  83          $question = question_bank::load_question($params['questionid']);
  84          $editingcontext = \context::instance_by_id($question->contextid);
  85          self::validate_context($editingcontext);
  86          $canedit = question_has_capability_on($question, 'edit');
  87          if ($canedit) {
  88              $versionrecord = $DB->get_record('question_versions', ['questionid' => $params['questionid']]);
  89              $versionrecord->status = $params['status'];
  90              $DB->update_record('question_versions', $versionrecord);
  91              question_bank::notify_question_edited($question->id);
  92              $result = [
  93                  'status' => true,
  94                  'statusname' => editquestion_helper::get_question_status_string($versionrecord->status),
  95                  'error' => ''
  96              ];
  97              $event = \core\event\question_updated::create_from_question_instance($question, $editingcontext);
  98              $event->trigger();
  99          }
 100  
 101          return $result;
 102      }
 103  
 104      /**
 105       * Returns description of method result value.
 106       */
 107      public static function  execute_returns() {
 108          return new external_single_structure([
 109              'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 110              'statusname' => new external_value(PARAM_RAW, 'statusname: name of the status'),
 111              'error' => new external_value(PARAM_TEXT, 'Error message if error exists')
 112          ]);
 113      }
 114  }