Merge branch 'MDL-78974-402' of https://github.com/paulholden/moodle into MOODLE_402_...
[moodle.git] / question / lib.php
blob7104a08cad678da455d18b8da1d87ee903a4383a
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 $sql = "SELECT qc.*
61 FROM {question} q
62 JOIN {question_versions} qv ON qv.questionid = q.id
63 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
64 JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
65 WHERE q.id = :id";
66 $category = $DB->get_record_sql($sql, ['id' => $question->id]);
67 $questioncontext = \context::instance_by_id($category->contextid);
68 $contexts = new \core_question\local\bank\question_edit_contexts($editingcontext);
70 // Load the question tags and filter the course tags by the current course.
71 if (core_tag_tag::is_enabled('core_question', 'question')) {
72 $tagobjectsbyquestion = core_tag_tag::get_items_tags('core_question', 'question', [$question->id]);
73 if (!empty($tagobjectsbyquestion[$question->id])) {
74 $tagobjects = $tagobjectsbyquestion[$question->id];
75 $sortedtagobjects = question_sort_tags($tagobjects,
76 context::instance_by_id($category->contextid), $filtercourses);
79 $formoptions = [
80 'editingcontext' => $editingcontext,
81 'questioncontext' => $questioncontext,
82 'contexts' => $contexts->all()
84 $data = [
85 'id' => $question->id,
86 'questioncategory' => $category->name,
87 'questionname' => $question->name,
88 'categoryid' => $category->id,
89 'contextid' => $category->contextid,
90 'context' => $questioncontext->get_context_name(),
91 'tags' => $sortedtagobjects->tags ?? [],
92 'coursetags' => $sortedtagobjects->coursetags ?? [],
95 $cantag = question_has_capability_on($question, 'tag');
96 $mform = new \qbank_tagquestion\form\tags_form(null, $formoptions, 'post', '', null, $cantag, $data);
97 $mform->set_data($data);
99 return $mform->render();