Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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
 * AMD code for the frequently used comments chooser for the marking guide grading form.
18
 *
19
 * @module     gradingform_guide/comment_chooser
20
 * @copyright  2015 Jun Pataleta <jun@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['jquery', 'core/templates', 'core/notification', 'core/yui'], function($, templates, notification) {
24
 
25
    // Private variables and functions.
26
 
27
    return /** @alias module:gradingform_guide/comment_chooser */ {
28
        // Public variables and functions.
29
        /**
30
         * Initialises the module.
31
         *
32
         * Basically, it performs the binding and handling of the button click event for
33
         * the 'Insert frequently used comment' button.
34
         *
35
         * @param {Integer} criterionId The criterion ID.
36
         * @param {String} buttonId The element ID of the button which the handler will be bound to.
37
         * @param {String} remarkId The element ID of the remark text area where the text of the selected comment will be copied to.
38
         * @param {Array} commentOptions The array of frequently used comments to be used as options.
39
         */
40
        initialise: function(criterionId, buttonId, remarkId, commentOptions) {
41
            /**
42
             * Display the chooser dialog using the compiled HTML from the mustache template
43
             * and binds onclick events for the generated comment options.
44
             *
45
             * @param {String} compiledSource The compiled HTML from the mustache template
46
             * @param {Array} comments Array containing comments.
47
             */
48
            function displayChooserDialog(compiledSource, comments) {
49
                var titleLabel = '<label>' + M.util.get_string('insertcomment', 'gradingform_guide') + '</label>';
50
                var cancelButtonId = 'comment-chooser-' + criterionId + '-cancel';
51
                var cancelButton = '<button id="' + cancelButtonId + '">' + M.util.get_string('cancel', 'moodle') + '</button>';
52
 
53
                // Set dialog's body content.
54
                var chooserDialog = new M.core.dialogue({
55
                    modal: true,
56
                    headerContent: titleLabel,
57
                    bodyContent: compiledSource,
58
                    footerContent: cancelButton,
59
                    focusAfterHide: '#' + remarkId,
60
                    id: "comments-chooser-dialog-" + criterionId
61
                });
62
 
63
                // Bind click event to the cancel button.
64
                $("#" + cancelButtonId).click(function() {
65
                    chooserDialog.hide();
66
                });
67
 
68
                // Loop over each comment item and bind click events.
69
                $.each(comments, function(index, comment) {
70
                    var commentOptionId = '#comment-option-' + criterionId + '-' + comment.id;
71
 
72
                    // Delegate click event for the generated option link.
73
                    $(commentOptionId).click(function() {
74
                        var remarkTextArea = $('#' + remarkId);
75
                        var remarkText = remarkTextArea.val();
76
 
77
                        // Add line break if the current value of the remark text is not empty.
78
                        if (remarkText.trim() !== '') {
79
                            remarkText += '\n';
80
                        }
81
                        remarkText += comment.description;
82
 
83
                        remarkTextArea.val(remarkText);
84
 
85
                        chooserDialog.hide();
86
                    });
87
 
88
                    // Handle keypress on list items.
89
                    $(document).off('keypress', commentOptionId).on('keypress', commentOptionId, function() {
90
                        var keyCode = event.which || event.keyCode;
91
 
92
                        // Enter or space key.
93
                        if (keyCode == 13 || keyCode == 32) {
94
                            // Trigger click event.
95
                            $(commentOptionId).click();
96
                        }
97
                    });
98
                });
99
 
100
                // Destroy the dialog when it is hidden to allow the grading section to
101
                // be loaded as a fragment multiple times within the same page.
102
                chooserDialog.after('visibleChange', function(e) {
103
                    // Going from visible to hidden.
104
                    if (e.prevVal && !e.newVal) {
105
                        this.destroy();
106
                    }
107
                }, chooserDialog);
108
 
109
                // Show dialog.
110
                chooserDialog.show();
111
            }
112
 
113
            /**
114
             * Generates the comments chooser dialog from the grading_form/comment_chooser mustache template.
115
             */
116
            function generateCommentsChooser() {
117
                // Template context.
118
                var context = {
119
                    criterionId: criterionId,
120
                    comments: commentOptions
121
                };
122
 
123
                // Render the template and display the comment chooser dialog.
124
                templates.render('gradingform_guide/comment_chooser', context)
125
                    .done(function(compiledSource) {
126
                        displayChooserDialog(compiledSource, commentOptions);
127
                    })
128
                    .fail(notification.exception);
129
            }
130
 
131
            // Bind click event for the comments chooser button.
132
            $("#" + buttonId).click(function(e) {
133
                e.preventDefault();
134
                generateCommentsChooser();
135
            });
136
        }
137
    };
138
});