Merge branch 'MDL-64173-master' of git://github.com/bmbrands/moodle
[moodle.git] / question / lib.php
blob209d06b733df9eafb7b7ad235ea91b67ce94823a
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.
37 function core_question_output_fragment_tags_form($args) {
39 if (!empty($args['id'])) {
40 global $CFG, $DB;
41 require_once($CFG->dirroot . '/question/type/tags_form.php');
42 require_once($CFG->libdir . '/questionlib.php');
43 $id = clean_param($args['id'], PARAM_INT);
44 $editingcontext = $args['context'];
46 $question = $DB->get_record('question', ['id' => $id]);
48 if ($coursecontext = $editingcontext->get_course_context(false)) {
49 $course = $DB->get_record('course', ['id' => $coursecontext->instanceid]);
50 $filtercourses = [$course];
51 } else {
52 $filtercourses = null;
55 // Load the question tags and filter the course tags by the current
56 // course.
57 get_question_options($question, true, $filtercourses);
59 $category = $question->categoryobject;
60 $questioncontext = \context::instance_by_id($category->contextid);
61 $contexts = new \question_edit_contexts($editingcontext);
63 $formoptions = [
64 'editingcontext' => $editingcontext,
65 'questioncontext' => $questioncontext,
66 'contexts' => $contexts->all()
68 $data = [
69 'id' => $question->id,
70 'questioncategory' => $category->name,
71 'questionname' => $question->name,
72 'categoryid' => $category->id,
73 'contextid' => $category->contextid,
74 'context' => $questioncontext->get_context_name(),
75 'tags' => isset($question->tags) ? $question->tags : [],
76 'coursetags' => isset($question->coursetags) ? $question->coursetags : [],
79 $cantag = question_has_capability_on($question, 'tag');
80 $mform = new \core_question\form\tags(null, $formoptions, 'post', '', null, $cantag, $data);
81 $mform->set_data($data);
83 return $mform->render();