Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * @package   qbank_tagquestion
23
 * @copyright 2018 Simey Lameze <simey@moodle.com>
24
 * @author    2021 Safat Shahin <safatshahin@catalyst-au.net>
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
use qbank_tagquestion\form\tags_form;
31
 
32
/**
33
 * Question tags fragment callback.
34
 *
35
 * @param array $args Arguments to the form.
36
 * @return null|string The rendered form.
37
 */
38
function qbank_tagquestion_output_fragment_tags_form($args) {
39
 
40
    if (!empty($args['id'])) {
41
        global $CFG, $DB;
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
        $sql = "SELECT qc.*
50
                  FROM {question} q
51
                  JOIN {question_versions} qv ON qv.questionid = q.id
52
                  JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
53
                  JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
54
                 WHERE q.id = :id";
55
        $category = $DB->get_record_sql($sql, ['id' => $question->id]);
56
        $questioncontext = \context::instance_by_id($category->contextid);
57
        $contexts = new \core_question\local\bank\question_edit_contexts($editingcontext);
58
 
59
        // Load the question tags and filter the course tags by the current course.
60
        if (core_tag_tag::is_enabled('core_question', 'question')) {
61
            $tagobjectsbyquestion = core_tag_tag::get_items_tags('core_question', 'question', [$question->id]);
62
            if (!empty($tagobjectsbyquestion[$question->id])) {
63
                $tagobjects = $tagobjectsbyquestion[$question->id];
1441 ariadna 64
                $sortedtagobjects = question_sort_tags($tagobjects, context::instance_by_id($category->contextid));
1 efrain 65
            }
66
        }
67
        $formoptions = [
68
                'editingcontext' => $editingcontext,
69
                'questioncontext' => $questioncontext,
70
                'contexts' => $contexts->all()
71
        ];
72
        $data = [
73
                'id' => $question->id,
74
                'questioncategory' => $category->name,
75
                'questionname' => $question->name,
76
                'categoryid' => $category->id,
77
                'contextid' => $category->contextid,
78
                'context' => $questioncontext->get_context_name(),
79
                'tags' => $sortedtagobjects->tags ?? [],
80
                'coursetags' => $sortedtagobjects->coursetags ?? [],
81
        ];
82
 
83
        $cantag = question_has_capability_on($question, 'tag');
84
        $mform = new tags_form(null, $formoptions, 'post', '', null, $cantag, $data);
85
        $mform->set_data($data);
86
 
87
        return $mform->render();
88
    }
89
 
90
}