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  /**
  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  /**
  27   * In place editing callback for question name.
  28   *
  29   * @param string $itemtype type of the item, questionname for this instance
  30   * @param int $itemid question id to change the title
  31   * @param string $newvalue the changed question title
  32   * @return \core\output\inplace_editable
  33   */
  34  function qbank_viewquestionname_inplace_editable ($itemtype, $itemid, $newvalue) : \core\output\inplace_editable {
  35      if ($itemtype === 'questionname') {
  36          global $CFG, $DB;
  37          require_once($CFG->libdir . '/questionlib.php');
  38          // Get the question data and to confirm any invalud itemid is not passed.
  39          $record = $DB->get_record('question', ['id' => $itemid], '*', MUST_EXIST);
  40  
  41          // Load question data from question engine.
  42          $question = question_bank::load_question($record->id);
  43          question_require_capability_on($question, 'edit');
  44  
  45          // Context validation.
  46          \external_api::validate_context(context::instance_by_id($question->contextid));
  47  
  48          // Now update the question data.
  49          $record->name = $newvalue;
  50          $DB->update_record('question', $record);
  51  
  52          // Trigger events.
  53          question_bank::notify_question_edited($record->id);
  54          $event = \core\event\question_updated::create_from_question_instance($question);
  55          $event->trigger();
  56  
  57          // Prepare the element for the output.
  58          return new \qbank_viewquestionname\output\questionname($record);
  59      }
  60  }