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
 * Change the course competency settings in a popup.
18
 *
19
 * @module     tool_lp/course_competency_settings
20
 * @copyright  2015 Damyon Wiese <damyon@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['jquery',
24
        'core/notification',
25
        'tool_lp/dialogue',
26
        'core/str',
27
        'core/ajax',
28
        'core/templates',
29
        'core/pending'
30
        ],
31
       function($, notification, Dialogue, str, ajax, templates, Pending) {
32
 
33
    /**
34
     * Constructor
35
     *
36
     * @param {String} selector - selector for the links to open the dialogue.
37
     */
38
    var settingsMod = function(selector) {
39
        $(selector).on('click', this.configureSettings.bind(this));
40
    };
41
 
42
    /** @property {Dialogue} Reference to the dialogue that we opened. */
43
    settingsMod.prototype._dialogue = null;
44
 
45
    /**
46
     * Open the configure settings dialogue.
47
     *
48
     * @param {Event} e
49
     * @method configureSettings
50
     */
51
    settingsMod.prototype.configureSettings = function(e) {
52
        var pendingPromise = new Pending();
53
        var courseid = $(e.target).closest('a').data('courseid');
54
        var currentValue = $(e.target).closest('a').data('pushratingstouserplans');
55
        var context = {
56
            courseid: courseid,
57
            settings: {pushratingstouserplans: currentValue}
58
        };
59
        e.preventDefault();
60
 
61
        $.when(
62
            str.get_string('configurecoursecompetencysettings', 'tool_lp'),
63
            templates.render('tool_lp/course_competency_settings', context),
64
        )
65
        .then(function(title, templateResult) {
66
            this._dialogue = new Dialogue(
67
                title,
68
                templateResult[0],
69
                this.addListeners.bind(this)
70
            );
71
 
72
            return this._dialogue;
73
        }.bind(this))
74
        .then(pendingPromise.resolve)
75
        .catch(notification.exception);
76
    };
77
 
78
    /**
79
     * Add the save listener to the form.
80
     *
81
     * @method addSaveListener
82
     */
83
    settingsMod.prototype.addListeners = function() {
84
        var save = this._find('[data-action="save"]');
85
        save.on('click', this.saveSettings.bind(this));
86
        var cancel = this._find('[data-action="cancel"]');
87
        cancel.on('click', this.cancelChanges.bind(this));
88
    };
89
 
90
    /**
91
     * Cancel the changes.
92
     *
93
     * @param {Event} e
94
     * @method cancelChanges
95
     */
96
    settingsMod.prototype.cancelChanges = function(e) {
97
        e.preventDefault();
98
        this._dialogue.close();
99
    };
100
 
101
    /**
102
     * Cancel the changes.
103
     *
104
     * @param {String} selector
105
     * @return {JQuery}
106
     */
107
    settingsMod.prototype._find = function(selector) {
108
        return $('[data-region="coursecompetencysettings"]').find(selector);
109
    };
110
 
111
    /**
112
     * Save the settings.
113
     *
114
     * @param {Event} e
115
     * @method saveSettings
116
     */
117
    settingsMod.prototype.saveSettings = function(e) {
118
        var pendingPromise = new Pending();
119
        e.preventDefault();
120
 
121
        var newValue = this._find('input[name="pushratingstouserplans"]:checked').val();
122
        var courseId = this._find('input[name="courseid"]').val();
123
        var settings = {pushratingstouserplans: newValue};
124
 
125
        ajax.call([
126
            {methodname: 'core_competency_update_course_competency_settings',
127
              args: {courseid: courseId, settings: settings}}
128
        ])[0]
129
        .then(function() {
130
            return this.refreshCourseCompetenciesPage();
131
        }.bind(this))
132
        .then(pendingPromise.resolve)
133
        .catch(notification.exception);
134
 
135
    };
136
 
137
    /**
138
     * Refresh the course competencies page.
139
     *
140
     * @method saveSettings
141
     */
142
    settingsMod.prototype.refreshCourseCompetenciesPage = function() {
143
        var courseId = this._find('input[name="courseid"]').val();
144
        var pendingPromise = new Pending();
145
 
146
        ajax.call([
147
            {methodname: 'tool_lp_data_for_course_competencies_page',
148
              args: {courseid: courseId, moduleid: 0}}
149
        ])[0]
150
        .then(function(context) {
151
            return templates.render('tool_lp/course_competencies_page', context);
152
        })
153
        .then(function(html, js) {
154
            templates.replaceNode($('[data-region="coursecompetenciespage"]'), html, js);
155
            this._dialogue.close();
156
 
157
            return;
158
        }.bind(this))
159
        .then(pendingPromise.resolve)
160
        .catch(notification.exception);
161
    };
162
 
163
    return /** @alias module:tool_lp/configurecoursecompetencysettings */ settingsMod;
164
});