Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * Column selector js.
18
 *
19
 * @module    qbank_comment/comment
20
 * @copyright 2021 Catalyst IT Australia Pty Ltd
21
 * @author    Safat Shahin <safatshahin@catalyst-au.net>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
import Fragment from 'core/fragment';
26
import {get_string as getString} from 'core/str';
27
import ModalEvents from 'core/modal_events';
28
import SaveCancelModal from 'core/modal_save_cancel';
29
 
30
/**
31
 * Event listeners for the module.
32
 *
33
 * @method clickEvent
34
 * @param {Number} questionId
35
 * @param {Number} courseID
36
 * @param {Number} contextId
37
 */
38
const commentEvent = async(questionId, courseID, contextId) => {
39
    const args = {
40
        questionid: questionId,
41
        courseid: courseID
42
    };
43
    const modal = await SaveCancelModal.create({
44
        title: getString('commentheader', 'qbank_comment'),
45
        body: Fragment.loadFragment('qbank_comment', 'question_comment', contextId, args),
46
        large: true,
47
        show: true,
48
        buttons: {
49
            save: getString('addcomment', 'qbank_comment'),
50
            cancel: getString('close', 'qbank_comment'),
51
        },
52
        removeOnClose: true,
53
    });
54
    const root = modal.getRoot();
55
 
56
    // Don't display the default add comment link in the modal.
57
    root.on(ModalEvents.bodyRendered, function() {
58
        const submitlink = document.querySelectorAll("div.comment-area a")[0];
59
        submitlink.style.display = 'none';
60
    });
61
 
62
    // Version selection event.
63
    root.on('change', '#question_comment_version_dropdown', (e) =>{
64
        args.questionid = e.target.value;
65
        modal.setBody(Fragment.loadFragment('qbank_comment', 'question_comment', contextId, args));
66
    });
67
 
68
    // Reload the page when the modal is closed.
69
    root.on(ModalEvents.hidden, () => location.reload());
70
 
71
    // Handle adding the comment when the button in the modal is clicked.
72
    root.on(ModalEvents.save, function(e) {
73
        e.preventDefault();
74
        const submitlink = document.querySelectorAll("div.comment-area a")[0];
75
        const textarea = document.querySelectorAll("div.comment-area textarea")[0];
76
 
77
        // Check there is a valid comment to add, and trigger adding if there is.
78
        if (textarea.value != textarea.getAttribute('aria-label') && textarea.value != '') {
79
            submitlink.click();
80
        }
81
 
82
    });
83
};
84
 
85
/**
86
 * Entrypoint of the js.
87
 *
88
 * @method init
89
 */
90
export const init = () => {
91
    const target = document.querySelector('#categoryquestions');
92
    if (target !== null) {
93
        target.addEventListener('click', (e) => {
94
            if (e.target.dataset.target && e.target.dataset.target.includes('questioncommentpreview')) {
95
                // Call for the event listener to listed for clicks in any comment count row.
96
                commentEvent(e.target.dataset.questionid, e.target.dataset.courseid, e.target.dataset.contextid);
97
            }
98
        });
99
    }
100
};