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
 * Helper functions and callbacks.
19
 *
20
 * @package    qbank_comment
21
 * @copyright  2021 Catalyst IT Australia Pty Ltd
22
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
require_once($CFG->dirroot. '/comment/lib.php');
29
 
30
/**
31
 * Validate comment parameter before perform other comments actions.
32
 *
33
 * @param stdClass $commentparam
34
 * {
35
 * context     => context the context object
36
 * courseid    => int course id
37
 * cm          => stdClass course module object
38
 * commentarea => string comment area
39
 * itemid      => int itemid
40
 * }
41
 * @return boolean
42
 */
43
function qbank_comment_comment_validate($commentparam): bool {
44
    if ($commentparam->commentarea != 'question' && $commentparam->component != 'qbank_comment') {
45
        throw new comment_exception('invalidcommentarea');
46
    }
47
    return true;
48
}
49
 
50
/**
51
 * Running additional permission check on plugins.
52
 *
53
 * @param stdClass $args
54
 * @return array
55
 */
56
function qbank_comment_comment_permissions($args): array {
57
    return ['post' => true, 'view' => true];
58
}
59
 
60
/**
61
 * Validate comment data before displaying comments.
62
 *
63
 * @param array $comments
64
 * @param stdClass $args
65
 * @return array $comments
66
 */
67
function qbank_comment_comment_display($comments, $args): array {
68
    if ($args->commentarea != 'question' && $args->component != 'qbank_comment') {
69
        throw new comment_exception('core_question');
70
    }
71
    return $comments;
72
}
73
 
74
/**
75
 * Comment content for callbacks.
76
 *
77
 * @param question_definition $question
78
 * @param int $courseid
79
 * @return string
80
 */
81
function qbank_comment_preview_display($question, $courseid): string {
82
    global $CFG, $PAGE;
83
    if (question_has_capability_on($question, 'comment') && $CFG->usecomments
84
            && core\plugininfo\qbank::is_plugin_enabled('qbank_comment')) {
85
        \comment::init($PAGE);
86
        $args = new \stdClass;
87
        $args->contextid = context_system::instance()->id; // Static data to bypass comment sql as context is not needed.
88
        $args->courseid  = $courseid;
89
        $args->area      = 'question';
90
        $args->itemid    = $question->id;
91
        $args->component = 'qbank_comment';
92
        $args->notoggle  = true;
93
        $args->autostart = true;
94
        $args->displaycancel = false;
95
        $args->linktext = get_string('commentheader', 'qbank_comment');
96
        $comment = new \comment($args);
97
        $comment->set_view_permission(true);
98
        $comment->set_fullwidth();
99
        return $comment->output();
100
    } else {
101
        return '';
102
    }
103
}
104
 
105
/**
106
 * Question comment fragment callback.
107
 *
108
 * @param array $args
109
 * @return string rendered output
110
 */
111
function qbank_comment_output_fragment_question_comment($args): string {
1441 ariadna 112
    global $PAGE, $CFG;
1 efrain 113
    $displaydata = [];
114
    require_once($CFG->dirroot . '/question/engine/bank.php');
115
    $question = question_bank::load_question($args['questionid']);
1441 ariadna 116
    $displaydata['question'] = question_bank::render_preview_of_question($question);
1 efrain 117
 
118
    $displaydata['comment'] = qbank_comment_preview_display($question, $args['courseid']);
119
    $displaydata['commenstdisabled'] = false;
120
    if (empty($displaydata['comment']) && !$CFG->usecomments) {
121
        $displaydata['commenstdisabled'] = true;
122
    }
123
 
124
    $selector = \core_question\output\question_version_selection::make_for_question('question_comment_version_dropdown',
125
        $args['questionid']);
126
    $qbankrenderer = $PAGE->get_renderer('core_question', 'bank');
127
    $displaydata['versionselection'] = $selector->export_for_template($qbankrenderer);
128
 
129
    return $PAGE->get_renderer('qbank_comment')->render_comment_fragment($displaydata);
130
}