Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 401 and 402]

   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  /**
  18   * Callback and other methods for viewquestionname plugin.
  19   *
  20   * @package    qbank_viewquestionname
  21   * @copyright  2022 Catalyst IT Australia Pty Ltd
  22   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  use core_external\external_api;
  27  use core_external\external_description;
  28  use core_external\external_files;
  29  use core_external\external_format_value;
  30  use core_external\external_function_parameters;
  31  use core_external\external_multiple_structure;
  32  use core_external\external_settings;
  33  use core_external\external_single_structure;
  34  use core_external\external_value;
  35  use core_external\external_warnings;
  36  use core_external\restricted_context_exception;
  37  use core_external\util;
  38  use core_external\util as external_util;
  39  
  40  /**
  41   * In place editing callback for question name.
  42   *
  43   * @param string $itemtype type of the item, questionname for this instance
  44   * @param int $itemid question id to change the title
  45   * @param string $newvalue the changed question title
  46   * @return \core\output\inplace_editable
  47   */
  48  function qbank_viewquestionname_inplace_editable ($itemtype, $itemid, $newvalue) : \core\output\inplace_editable {
  49      if ($itemtype === 'questionname') {
  50          global $CFG, $DB;
  51          require_once($CFG->libdir . '/questionlib.php');
  52          // Get the question data and to confirm any invalud itemid is not passed.
  53          $record = $DB->get_record('question', ['id' => $itemid], '*', MUST_EXIST);
  54  
  55          // Load question data from question engine.
  56          $question = question_bank::load_question($record->id);
  57          question_require_capability_on($question, 'edit');
  58  
  59          // Context validation.
  60          external_api::validate_context(context::instance_by_id($question->contextid));
  61  
  62          // Now update the question data.
  63          $record->name = $newvalue;
  64          $DB->update_record('question', $record);
  65  
  66          // Trigger events.
  67          question_bank::notify_question_edited($record->id);
  68          $event = \core\event\question_updated::create_from_question_instance($question);
  69          $event->trigger();
  70  
  71          // Prepare the element for the output.
  72          return new \qbank_viewquestionname\output\questionname($record);
  73      }
  74  }