Merge branch 'MDL-73468' of git://github.com/paulholden/moodle
[moodle.git] / question / lib.php
blob158bba6cd2456a3b144f66e60a5d43143bf0c978
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.
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/>.
17 /**
18 * Question related functions.
20 * This file was created just because Fragment API expects callbacks to be defined on lib.php.
22 * Please, do not add new functions to this file.
24 * @package core_question
25 * @copyright 2018 Simey Lameze <simey@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
31 /**
32 * Question tags fragment callback.
34 * @param array $args Arguments to the form.
35 * @return null|string The rendered form.
36 * @deprecated since Moodle 4.0
37 * @see /question/bank/qbank_tagquestion/lib.php
38 * @todo Final deprecation on Moodle 4.4 MDL-72438
40 function core_question_output_fragment_tags_form($args) {
41 debugging('Function core_question_output_fragment_tags_form() is deprecated,
42 please use core_question_output_fragment_tags_form() from qbank_tagquestion instead.', DEBUG_DEVELOPER);
44 if (!empty($args['id'])) {
45 global $CFG, $DB;
46 require_once($CFG->libdir . '/questionlib.php');
47 $id = clean_param($args['id'], PARAM_INT);
48 $editingcontext = $args['context'];
50 // Load the question and some related information.
51 $question = $DB->get_record('question', ['id' => $id]);
53 if ($coursecontext = $editingcontext->get_course_context(false)) {
54 $course = $DB->get_record('course', ['id' => $coursecontext->instanceid]);
55 $filtercourses = [$course];
56 } else {
57 $filtercourses = null;
60 $category = $DB->get_record('question_categories', ['id' => $question->category]);
61 $questioncontext = \context::instance_by_id($category->contextid);
62 $contexts = new \question_edit_contexts($editingcontext);
64 // Load the question tags and filter the course tags by the current course.
65 if (core_tag_tag::is_enabled('core_question', 'question')) {
66 $tagobjectsbyquestion = core_tag_tag::get_items_tags('core_question', 'question', [$question->id]);
67 if (!empty($tagobjectsbyquestion[$question->id])) {
68 $tagobjects = $tagobjectsbyquestion[$question->id];
69 $sortedtagobjects = question_sort_tags($tagobjects,
70 context::instance_by_id($category->contextid), $filtercourses);
73 $formoptions = [
74 'editingcontext' => $editingcontext,
75 'questioncontext' => $questioncontext,
76 'contexts' => $contexts->all()
78 $data = [
79 'id' => $question->id,
80 'questioncategory' => $category->name,
81 'questionname' => $question->name,
82 'categoryid' => $category->id,
83 'contextid' => $category->contextid,
84 'context' => $questioncontext->get_context_name(),
85 'tags' => $sortedtagobjects->tags ?? [],
86 'coursetags' => $sortedtagobjects->coursetags ?? [],
89 $cantag = question_has_capability_on($question, 'tag');
90 $mform = new \qbank_tagquestion\form\tags_form(null, $formoptions, 'post', '', null, $cantag, $data);
91 $mform->set_data($data);
93 return $mform->render();