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