Merge branch 'MDL-67514-310' of https://github.com/paulholden/moodle into MOODLE_310_...
[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 45
fa7431ce 46 // Load the question and some related information.
7f4b6dfe 47 $question = $DB->get_record('question', ['id' => $id]);
7f4b6dfe 48
e6890b11
SL
49 if ($coursecontext = $editingcontext->get_course_context(false)) {
50 $course = $DB->get_record('course', ['id' => $coursecontext->instanceid]);
51 $filtercourses = [$course];
52 } else {
53 $filtercourses = null;
7f4b6dfe
SL
54 }
55
fa7431ce 56 $category = $DB->get_record('question_categories', ['id' => $question->category]);
e6890b11 57 $questioncontext = \context::instance_by_id($category->contextid);
a9b34106 58 $contexts = new \question_edit_contexts($editingcontext);
e6890b11 59
fa7431ce
TH
60 // Load the question tags and filter the course tags by the current course.
61 if (core_tag_tag::is_enabled('core_question', 'question')) {
62 $tagobjectsbyquestion = core_tag_tag::get_items_tags('core_question', 'question', [$question->id]);
63 if (!empty($tagobjectsbyquestion[$question->id])) {
64 $tagobjects = $tagobjectsbyquestion[$question->id];
65 $sortedtagobjects = question_sort_tags($tagobjects,
66 context::instance_by_id($category->contextid), $filtercourses);
67 }
68 }
e6890b11
SL
69 $formoptions = [
70 'editingcontext' => $editingcontext,
a9b34106
SL
71 'questioncontext' => $questioncontext,
72 'contexts' => $contexts->all()
e6890b11
SL
73 ];
74 $data = [
75 'id' => $question->id,
76 'questioncategory' => $category->name,
77 'questionname' => $question->name,
78 'categoryid' => $category->id,
79 'contextid' => $category->contextid,
80 'context' => $questioncontext->get_context_name(),
fa7431ce
TH
81 'tags' => $sortedtagobjects->tags ?? [],
82 'coursetags' => $sortedtagobjects->coursetags ?? [],
e6890b11
SL
83 ];
84
e2795e86 85 $cantag = question_has_capability_on($question, 'tag');
e6890b11
SL
86 $mform = new \core_question\form\tags(null, $formoptions, 'post', '', null, $cantag, $data);
87 $mform->set_data($data);
7f4b6dfe
SL
88
89 return $mform->render();
90 }
91}