| 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 | |
| 29 | defined('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 | */ |
| 37 | function 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); |
| 44 | $editingcontext = $args['context']; |
| 45 | |
| 46 | // Load the question and some related information. |
| 47 | $question = $DB->get_record('question', ['id' => $id]); |
| 48 | |
| 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; |
| 54 | } |
| 55 | |
| 56 | $category = $DB->get_record('question_categories', ['id' => $question->category]); |
| 57 | $questioncontext = \context::instance_by_id($category->contextid); |
| 58 | $contexts = new \question_edit_contexts($editingcontext); |
| 59 | |
| 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 | } |
| 69 | $formoptions = [ |
| 70 | 'editingcontext' => $editingcontext, |
| 71 | 'questioncontext' => $questioncontext, |
| 72 | 'contexts' => $contexts->all() |
| 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(), |
| 81 | 'tags' => $sortedtagobjects->tags ?? [], |
| 82 | 'coursetags' => $sortedtagobjects->coursetags ?? [], |
| 83 | ]; |
| 84 | |
| 85 | $cantag = question_has_capability_on($question, 'tag'); |
| 86 | $mform = new \core_question\form\tags(null, $formoptions, 'post', '', null, $cantag, $data); |
| 87 | $mform->set_data($data); |
| 88 | |
| 89 | return $mform->render(); |
| 90 | } |
| 91 | } |