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
 * Grade dialogue.
18
 *
19
 * @module     tool_lp/grade_dialogue
20
 * @copyright  2016 Frédéric Massart - FMCorz.net
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
define(['jquery',
25
        'core/notification',
26
        'core/templates',
27
        'tool_lp/dialogue',
28
        'tool_lp/event_base',
29
        'core/str'],
30
        function($, Notification, Templates, Dialogue, EventBase, Str) {
31
 
32
    /**
33
     * Grade dialogue class.
34
     *
35
     * @class tool_lp/grade_dialogue
36
     * @param {Array} ratingOptions
37
     */
38
    var Grade = function(ratingOptions) {
39
        EventBase.prototype.constructor.apply(this, []);
40
        this._ratingOptions = ratingOptions;
41
    };
42
    Grade.prototype = Object.create(EventBase.prototype);
43
 
44
    /** @property {Dialogue} The dialogue. */
45
    Grade.prototype._popup = null;
46
    /** @property {Array} Array of objects containing, 'value', 'name' and optionally 'selected'. */
47
    Grade.prototype._ratingOptions = null;
48
 
49
    /**
50
     * After render hook.
51
     *
52
     * @method _afterRender
53
     * @protected
54
     */
55
    Grade.prototype._afterRender = function() {
56
        var btnRate = this._find('[data-action="rate"]'),
57
            lstRating = this._find('[name="rating"]'),
58
            txtComment = this._find('[name="comment"]');
59
 
60
        this._find('[data-action="cancel"]').click(function(e) {
61
            e.preventDefault();
62
            this._trigger('cancelled');
63
            this.close();
64
        }.bind(this));
65
 
66
        lstRating.change(function() {
67
            var node = $(this);
68
            if (!node.val()) {
69
                btnRate.prop('disabled', true);
70
            } else {
71
                btnRate.prop('disabled', false);
72
            }
73
        }).change();
74
 
75
        btnRate.click(function(e) {
76
            e.preventDefault();
77
            var val = lstRating.val();
78
            if (!val) {
79
                return;
80
            }
81
            this._trigger('rated', {
82
                'rating': val,
83
                'note': txtComment.val()
84
            });
85
            this.close();
86
        }.bind(this));
87
    };
88
 
89
    /**
90
     * Close the dialogue.
91
     *
92
     * @method close
93
     */
94
    Grade.prototype.close = function() {
95
        this._popup.close();
96
        this._popup = null;
97
    };
98
 
99
    /**
100
     * Opens the picker.
101
     *
102
     * @method display
103
     * @return {Promise}
104
     */
105
    Grade.prototype.display = function() {
106
        M.util.js_pending('tool_lp/grade_dialogue:display');
107
        return $.when(
108
            Str.get_string('rate', 'tool_lp'),
109
            this._render()
110
        )
111
        .then(function(title, templateResult) {
112
            this._popup = new Dialogue(
113
                title,
114
                templateResult[0],
115
                function() {
116
                    this._afterRender();
117
                    M.util.js_complete('tool_lp/grade_dialogue:display');
118
                }.bind(this)
119
            );
120
 
121
            return this._popup;
122
        }.bind(this))
123
        .catch(Notification.exception);
124
    };
125
 
126
    /**
127
     * Find a node in the dialogue.
128
     *
129
     * @param {String} selector
130
     * @method _find
131
     * @returns {node} The node
132
     * @protected
133
     */
134
    Grade.prototype._find = function(selector) {
135
        return $(this._popup.getContent()).find(selector);
136
    };
137
 
138
    /**
139
     * Render the dialogue.
140
     *
141
     * @method _render
142
     * @protected
143
     * @return {Promise}
144
     */
145
    Grade.prototype._render = function() {
146
        var context = {
147
            cangrade: this._canGrade,
148
            ratings: this._ratingOptions
149
        };
150
        return Templates.render('tool_lp/competency_grader', context);
151
    };
152
 
153
    return Grade;
154
});