MDL-61837 core_question: replace tag fields on tag modal
[moodle.git] / question / lib.php
CommitLineData
7f4b6dfe
SL
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 * Question related functions.
19 *
20 * This file was created just because Fragment API expects callbacks to be defined on lib.php.
21 *
22 * Please, do not add new functions to this file.
23 *
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
27 */
28
29defined('MOODLE_INTERNAL') || die();
30
31/**
32 * Question tags fragment callback.
33 *
34 * @param array $args Arguments to the form.
35 * @return null|string The rendered form.
36 */
37function core_question_output_fragment_tags_form($args) {
38
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);
e6890b11 44 $editingcontext = $args['context'];
7f4b6dfe
SL
45
46 $question = $DB->get_record('question', ['id' => $id]);
7f4b6dfe 47
e6890b11
SL
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;
7f4b6dfe
SL
53 }
54
e6890b11
SL
55 // Load the question tags and filter the course tags by the current
56 // course.
57 get_question_options($question, true, $filtercourses);
58
59 $category = $question->categoryobject;
60 $questioncontext = \context::instance_by_id($category->contextid);
a9b34106 61 $contexts = new \question_edit_contexts($editingcontext);
e6890b11
SL
62
63 $formoptions = [
64 'editingcontext' => $editingcontext,
a9b34106
SL
65 'questioncontext' => $questioncontext,
66 'contexts' => $contexts->all()
e6890b11
SL
67 ];
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 : [],
77 ];
78
e2795e86 79 $cantag = question_has_capability_on($question, 'tag');
e6890b11
SL
80 $mform = new \core_question\form\tags(null, $formoptions, 'post', '', null, $cantag, $data);
81 $mform->set_data($data);
7f4b6dfe
SL
82
83 return $mform->render();
84 }
85}