Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 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->libdir . '/externallib.php');
  22  require_once($CFG->dirroot . '/question/engine/bank.php');
  23  
  24  use external_api;
  25  use external_function_parameters;
  26  use external_single_structure;
  27  use external_value;
  28  use qbank_editquestion\editquestion_helper;
  29  use question_bank;
  30  
  31  /**
  32   * Update question status external api.
  33   *
  34   * @package    qbank_editquestion
  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 update_question_version_status extends \external_api {
  40  
  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              'status' => new external_value(PARAM_TEXT, 'The updated question status')
  50          ]);
  51      }
  52  
  53      /**
  54       * Handles the status form submission.
  55       *
  56       * @param int $questionid The question id.
  57       * @param string $status The updated question status.
  58       * @return array The created or modified question tag
  59       */
  60      public static function execute($questionid, $status) {
  61          global $DB;
  62  
  63          $result = [
  64              'status' => false,
  65              'statusname' => '',
  66              'error' => ''
  67          ];
  68  
  69          // Parameter validation.
  70          $params = self::validate_parameters(self::execute_parameters(), [
  71              'questionid' => $questionid,
  72              'status' => $status
  73          ]);
  74  
  75          $statuslist = editquestion_helper::get_question_status_list();
  76          $statusexists = array_key_exists($status, $statuslist);
  77          if (!$statusexists) {
  78              return [
  79                  'status' => false,
  80                  'statusname' => '',
  81                  'error' => get_string('unrecognizedstatus', 'qbank_editquestion')
  82              ];
  83          }
  84          $question = question_bank::load_question($params['questionid']);
  85          $editingcontext = \context::instance_by_id($question->contextid);
  86          self::validate_context($editingcontext);
  87          $canedit = question_has_capability_on($question, 'edit');
  88          if ($canedit) {
  89              $versionrecord = $DB->get_record('question_versions', ['questionid' => $params['questionid']]);
  90              $versionrecord->status = $params['status'];
  91              $DB->update_record('question_versions', $versionrecord);
  92              question_bank::notify_question_edited($question->id);
  93              $result = [
  94                  'status' => true,
  95                  'statusname' => editquestion_helper::get_question_status_string($versionrecord->status),
  96                  'error' => ''
  97              ];
  98              $event = \core\event\question_updated::create_from_question_instance($question, $editingcontext);
  99              $event->trigger();
 100          }
 101  
 102          return $result;
 103      }
 104  
 105      /**
 106       * Returns description of method result value.
 107       */
 108      public static function  execute_returns() {
 109          return new external_single_structure([
 110              'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 111              'statusname' => new external_value(PARAM_RAW, 'statusname: name of the status'),
 112              'error' => new external_value(PARAM_TEXT, 'Error message if error exists')
 113          ]);
 114      }
 115  }