MDL-64437 assignment: Fix element overlaping in low screen resolution
[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        function($, notification, Dialogue, str, ajax, templates) {
32     /**
33      * Constructor
34      *
35      * @param {String} selector - selector for the links to open the dialogue.
36      */
37     var settingsMod = function(selector) {
38         $(selector).on('click', this.configureSettings.bind(this));
39     };
41     /** @type {Dialogue} Reference to the dialogue that we opened. */
42     settingsMod.prototype._dialogue = null;
44     /**
45      * Open the configure settings dialogue.
46      *
47      * @param {Event} e
48      * @method configureSettings
49      */
50     settingsMod.prototype.configureSettings = function(e) {
51         var courseid = $(e.target).closest('a').data('courseid');
52         var currentValue = $(e.target).closest('a').data('pushratingstouserplans');
53         var context = {
54             courseid: courseid,
55             settings: {pushratingstouserplans: currentValue}
56         };
57         e.preventDefault();
59         templates.render('tool_lp/course_competency_settings', context).done(function(html) {
60             str.get_string('configurecoursecompetencysettings', 'tool_lp').done(function(title) {
61                 this._dialogue = new Dialogue(
62                     title,
63                     html,
64                     this.addListeners.bind(this)
65                 );
66             }.bind(this)).fail(notification.exception);
67         }.bind(this)).fail(notification.exception);
69     };
71     /**
72      * Add the save listener to the form.
73      *
74      * @method addSaveListener
75      */
76     settingsMod.prototype.addListeners = function() {
77         var save = this._find('[data-action="save"]');
78         save.on('click', this.saveSettings.bind(this));
79         var cancel = this._find('[data-action="cancel"]');
80         cancel.on('click', this.cancelChanges.bind(this));
81     };
83     /**
84      * Cancel the changes.
85      *
86      * @param {Event} e
87      * @method cancelChanges
88      */
89     settingsMod.prototype.cancelChanges = function(e) {
90         e.preventDefault();
91         this._dialogue.close();
92     };
94     /**
95      * Cancel the changes.
96      *
97      * @param {String} selector
98      * @return {JQuery}
99      */
100     settingsMod.prototype._find = function(selector) {
101         return $('[data-region="coursecompetencysettings"]').find(selector);
102     };
104     /**
105      * Save the settings.
106      *
107      * @param {Event} e
108      * @method saveSettings
109      */
110     settingsMod.prototype.saveSettings = function(e) {
111         e.preventDefault();
113         var newValue = this._find('input[name="pushratingstouserplans"]:checked').val();
114         var courseId = this._find('input[name="courseid"]').val();
115         var settings = {pushratingstouserplans: newValue};
117         ajax.call([
118             {methodname: 'core_competency_update_course_competency_settings',
119               args: {courseid: courseId, settings: settings}}
120         ])[0].done(function() {
121             this.refreshCourseCompetenciesPage();
122         }.bind(this)).fail(notification.exception);
124     };
126     /**
127      * Refresh the course competencies page.
128      *
129      * @param {Event} e
130      * @method saveSettings
131      */
132     settingsMod.prototype.refreshCourseCompetenciesPage = function() {
133         var courseId = this._find('input[name="courseid"]').val();
135         ajax.call([
136             {methodname: 'tool_lp_data_for_course_competencies_page',
137               args: {courseid: courseId, moduleid: 0}}
138         ])[0].done(function(context) {
139             templates.render('tool_lp/course_competencies_page', context).done(function(html, js) {
140                 $('[data-region="coursecompetenciespage"]').replaceWith(html);
141                 templates.runTemplateJS(js);
142                 this._dialogue.close();
143             }.bind(this)).fail(notification.exception);
144         }.bind(this)).fail(notification.exception);
146     };
148     return /** @alias module:tool_lp/configurecoursecompetencysettings */ settingsMod;
149 });