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
 * Module to enable inline editing of a comptency grade.
18
 *
19
 * @module     tool_lp/grade_user_competency_inline
20
 * @copyright  2015 Damyon Wiese
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
define(['jquery',
25
        'core/notification',
26
        'core/ajax',
27
        'core/log',
28
        'tool_lp/grade_dialogue',
29
        'tool_lp/event_base',
30
        'tool_lp/scalevalues',
31
    ], function($, notification, ajax, log, GradeDialogue, EventBase, ScaleValues) {
32
 
33
    /**
34
     * InlineEditor
35
     *
36
     * @class tool_lp/grade_user_competency_inline
37
     * @param {String} selector The selector to trigger the grading.
38
     * @param {Number} scaleId The id of the scale for this competency.
39
     * @param {Number} competencyId The id of the competency.
40
     * @param {Number} userId The id of the user.
41
     * @param {Number} planId The id of the plan.
42
     * @param {Number} courseId The id of the course.
43
     * @param {String} chooseStr Language string for choose a rating.
44
     */
45
    var InlineEditor = function(selector, scaleId, competencyId, userId, planId, courseId, chooseStr) {
46
        EventBase.prototype.constructor.apply(this, []);
47
 
48
        var trigger = $(selector);
49
        if (!trigger.length) {
50
            throw new Error('Could not find the trigger');
51
        }
52
 
53
        this._scaleId = scaleId;
54
        this._competencyId = competencyId;
55
        this._userId = userId;
56
        this._planId = planId;
57
        this._courseId = courseId;
58
        this._chooseStr = chooseStr;
59
        this._setUp();
60
 
61
        trigger.click(function(e) {
62
            e.preventDefault();
63
            this._dialogue.display();
64
        }.bind(this));
65
 
66
        if (this._planId) {
67
            this._methodName = 'core_competency_grade_competency_in_plan';
68
            this._args = {
69
                competencyid: this._competencyId,
70
                planid: this._planId
71
            };
72
        } else if (this._courseId) {
73
            this._methodName = 'core_competency_grade_competency_in_course';
74
            this._args = {
75
                competencyid: this._competencyId,
76
                courseid: this._courseId,
77
                userid: this._userId
78
            };
79
        } else {
80
            this._methodName = 'core_competency_grade_competency';
81
            this._args = {
82
                userid: this._userId,
83
                competencyid: this._competencyId
84
            };
85
        }
86
    };
87
    InlineEditor.prototype = Object.create(EventBase.prototype);
88
 
89
    /**
90
     * Setup.
91
     *
92
     * @method _setUp
93
     */
94
    InlineEditor.prototype._setUp = function() {
95
        var options = [],
96
            self = this;
97
 
98
        M.util.js_pending('tool_lp/grade_user_competency_inline:_setUp');
99
        var promise = ScaleValues.get_values(self._scaleId);
100
        promise.then(function(scalevalues) {
101
            options.push({
102
                value: '',
103
                name: self._chooseStr
104
            });
105
 
106
            for (var i = 0; i < scalevalues.length; i++) {
107
                var optionConfig = scalevalues[i];
108
                options.push({
109
                    value: optionConfig.id,
110
                    name: optionConfig.name
111
                });
112
            }
113
 
114
            return options;
115
        })
116
        .then(function(options) {
117
            return new GradeDialogue(options);
118
        })
119
        .then(function(dialogue) {
120
            dialogue.on('rated', function(e, data) {
121
                var args = self._args;
122
                args.grade = data.rating;
123
                args.note = data.note;
124
                ajax.call([{
125
                    methodname: self._methodName,
126
                    args: args,
127
                    done: function(evidence) {
128
                        self._trigger('competencyupdated', {args: args, evidence: evidence});
129
                    },
130
                    fail: notification.exception
131
                }]);
132
            });
133
 
134
            return dialogue;
135
        })
136
        .then(function(dialogue) {
137
            self._dialogue = dialogue;
138
 
139
            M.util.js_complete('tool_lp/grade_user_competency_inline:_setUp');
140
            return;
141
        })
142
        .fail(notification.exception);
143
    };
144
 
145
    /** @property {Number} The scale id for this competency. */
146
    InlineEditor.prototype._scaleId = null;
147
    /** @property {Number} The id of the competency. */
148
    InlineEditor.prototype._competencyId = null;
149
    /** @property {Number} The id of the user. */
150
    InlineEditor.prototype._userId = null;
151
    /** @property {Number} The id of the plan. */
152
    InlineEditor.prototype._planId = null;
153
    /** @property {Number} The id of the course. */
154
    InlineEditor.prototype._courseId = null;
155
    /** @property {String} The text for Choose rating. */
156
    InlineEditor.prototype._chooseStr = null;
157
    /** @property {GradeDialogue} The grading dialogue. */
158
    InlineEditor.prototype._dialogue = null;
159
 
160
    return InlineEditor;
161
});