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.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Callback and other methods for viewquestionname plugin.
 *
 * @package    qbank_viewquestionname
 * @copyright  2022 Catalyst IT Australia Pty Ltd
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

> use core_external\external_api; /** > use core_external\external_description; * In place editing callback for question name. > use core_external\external_files; * > use core_external\external_format_value; * @param string $itemtype type of the item, questionname for this instance > use core_external\external_function_parameters; * @param int $itemid question id to change the title > use core_external\external_multiple_structure; * @param string $newvalue the changed question title > use core_external\external_settings; * @return \core\output\inplace_editable > use core_external\external_single_structure; */ > use core_external\external_value; function qbank_viewquestionname_inplace_editable ($itemtype, $itemid, $newvalue) : \core\output\inplace_editable { > use core_external\external_warnings; if ($itemtype === 'questionname') { > use core_external\restricted_context_exception; global $CFG, $DB; > use core_external\util; require_once($CFG->libdir . '/questionlib.php'); > use core_external\util as external_util; // Get the question data and to confirm any invalud itemid is not passed. >
$record = $DB->get_record('question', ['id' => $itemid], '*', MUST_EXIST); // Load question data from question engine. $question = question_bank::load_question($record->id); question_require_capability_on($question, 'edit'); // Context validation.
< \external_api::validate_context(context::instance_by_id($question->contextid));
> external_api::validate_context(context::instance_by_id($question->contextid));
// Now update the question data. $record->name = $newvalue; $DB->update_record('question', $record); // Trigger events. question_bank::notify_question_edited($record->id); $event = \core\event\question_updated::create_from_question_instance($question); $event->trigger(); // Prepare the element for the output. return new \qbank_viewquestionname\output\questionname($record); } }