MDL-61133 core_question: tags form and fragment callback
[moodle.git] / question / lib.php
blob5c04b33831d5979da4f7dcbd23e17c767f15cce4
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);
45 $question = $DB->get_record('question', ['id' => $id]);
46 $category = $DB->get_record('question_categories', array('id' => $question->category));
47 $context = \context::instance_by_id($category->contextid);
49 $toform = new stdClass();
50 $toform->id = $question->id;
51 $toform->questioncategory = $category->name;
52 $toform->questionname = $question->name;
53 $toform->categoryid = $category->id;
54 $toform->contextid = $category->contextid;
55 $toform->context = $context->get_context_name();
57 if (core_tag_tag::is_enabled('core_question', 'question')) {
58 $toform->tags = core_tag_tag::get_item_tags_array('core_question', 'question', $question->id);
61 $canedit = question_has_capability_on($question, 'edit');
62 $mform = new \core_question\form\tags(null, null, 'post', '', null, $canedit, $toform);
63 $mform->set_data($toform);
65 return $mform->render();