MDL-53451 competency: Removing sneaky whitespaces
[moodle.git] / admin / tool / lp / amd / src / competencies.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  * Handle add/remove competency links.
18  *
19  * @module     tool_lp/competencies
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         'core/ajax',
27         'core/templates',
28         'core/str',
29         'tool_lp/competencypicker',
30         'tool_lp/dragdrop-reorder'],
31        function($, notification, ajax, templates, str, Picker, dragdrop) {
33     /**
34      * Constructor
35      *
36      * @param {Number} itemid
37      * @param {String} itemtype
38      */
39     var competencies = function(itemid, itemtype, pagectxid) {
40         this.itemid = itemid;
41         this.itemtype = itemtype;
42         this.pageContextId = pagectxid;
43         this.pickerInstance = null;
45         $('[data-region="actions"] button').prop('disabled', false);
46         this.registerEvents();
47         this.registerDragDrop();
48     };
50     /**
51      * Initialise the drag/drop code.
52      * @method registerDragDrop
53      */
54     competencies.prototype.registerDragDrop = function() {
55         var localthis = this;
56         // Init this module.
57         str.get_string('movecompetency', 'tool_lp').done(
58             function(movestring) {
59                 dragdrop.dragdrop('movecompetency',
60                                   movestring,
61                                   { identifier: 'movecompetency', component: 'tool_lp'},
62                                   { identifier: 'movecompetencyafter', component: 'tool_lp'},
63                                   'drag-samenode',
64                                   'drag-parentnode',
65                                   'drag-handlecontainer',
66                                   function(drag, drop) {
67                                       localthis.handleDrop.call(localthis, drag, drop);
68                                   });
69             }
70         ).fail(notification.exception);
72     };
74     /**
75      * Handle a drop from a drag/drop operation.
76      *
77      * @method handleDrop
78      * @param {DOMNode} drag The dragged node.
79      * @param {DOMNode} drop The dropped on node.
80      */
81     competencies.prototype.handleDrop = function(drag, drop) {
82         var fromid = $(drag).data('id');
83         var toid = $(drop).data('id');
84         var localthis = this;
85         var requests = [];
87         if (localthis.itemtype == 'course') {
88             requests = ajax.call([
89                 {
90                     methodname: 'core_competency_reorder_course_competency',
91                     args: { courseid: localthis.itemid, competencyidfrom: fromid, competencyidto: toid }
92                 }
93             ]);
94         } else if (localthis.itemtype == 'template') {
95             requests = ajax.call([
96                 {
97                     methodname: 'core_competency_reorder_template_competency',
98                     args: { templateid: localthis.itemid, competencyidfrom: fromid, competencyidto: toid }
99                 }
100             ]);
101         } else if (localthis.itemtype == 'plan') {
102             requests = ajax.call([
103                 {
104                     methodname: 'core_competency_reorder_plan_competency',
105                     args: { planid: localthis.itemid, competencyidfrom: fromid, competencyidto: toid }
106                 }
107             ]);
108         } else {
109             return null;
110         }
112         requests[0].fail(notification.exception);
113     };
115     /**
116      * Pick a competency
117      *
118      * @method pickCompetency
119      */
120     competencies.prototype.pickCompetency = function() {
121         var self = this;
122         var requests;
123         var pagerender;
124         var pageregion;
125         var pageContextIncludes;
127         if (!self.pickerInstance) {
128             if (self.itemtype === 'template' || self.itemtype === 'course') {
129                 pageContextIncludes = 'parents';
130             }
131             self.pickerInstance = new Picker(self.pageContextId, false, pageContextIncludes);
132             self.pickerInstance.on('save', function(e, data) {
133                 var compIds = data.competencyIds;
135                 if (self.itemtype === "course") {
136                     requests = [];
138                     $.each(compIds, function(index, compId) {
139                         requests.push({
140                             methodname: 'core_competency_add_competency_to_course',
141                             args: { courseid: self.itemid, competencyid: compId }
142                         });
143                     });
144                     requests.push({
145                         methodname: 'tool_lp_data_for_course_competencies_page',
146                         args: { courseid: self.itemid }
147                     });
149                     pagerender = 'tool_lp/course_competencies_page';
150                     pageregion = 'coursecompetenciespage';
152                 } else if (self.itemtype === "template") {
153                     requests = [];
155                     $.each(compIds, function(index, compId) {
156                         requests.push({
157                             methodname: 'core_competency_add_competency_to_template',
158                             args: { templateid: self.itemid, competencyid: compId }
159                         });
160                     });
161                     requests.push({
162                         methodname: 'tool_lp_data_for_template_competencies_page',
163                         args: { templateid: self.itemid, pagecontext: { contextid: self.pageContextId }}
164                     });
165                     pagerender = 'tool_lp/template_competencies_page';
166                     pageregion = 'templatecompetenciespage';
167                 } else if (self.itemtype === "plan") {
168                     requests = [];
170                     $.each(compIds, function(index, compId) {
171                         requests.push({
172                             methodname: 'core_competency_add_competency_to_plan',
173                             args: { planid: self.itemid, competencyid: compId }
174                         });
175                     });
176                     requests.push({
177                          methodname: 'tool_lp_data_for_plan_page',
178                          args: { planid: self.itemid}
179                     });
180                     pagerender = 'tool_lp/plan_page';
181                     pageregion = 'plan-page';
182                 }
184                 ajax.call(requests)[requests.length - 1].then(function(context) {
185                     return templates.render(pagerender, context).done(function(html, js) {
186                         $('[data-region="' + pageregion + '"]').replaceWith(html);
187                         templates.runTemplateJS(js);
188                     });
189                 }, notification.exception);
190             });
191         }
193         self.pickerInstance.display();
194     };
196     /**
197      * Delete the link between competency and course, template or plan. Reload the page.
198      *
199      * @method doDelete
200      * @param {int} deleteid The id of record to delete.
201      */
202     competencies.prototype.doDelete = function(deleteid) {
203         var localthis = this;
204         var requests = [],
205             pagerender = '',
206             pageregion = '';
208         // Delete the link and reload the page template.
209         if (localthis.itemtype == 'course') {
210             requests = ajax.call([
211                 { methodname: 'core_competency_remove_competency_from_course',
212                     args: { courseid: localthis.itemid, competencyid: deleteid } },
213                 { methodname: 'tool_lp_data_for_course_competencies_page',
214                     args: { courseid: localthis.itemid } }
215             ]);
216             pagerender = 'tool_lp/course_competencies_page';
217             pageregion = 'coursecompetenciespage';
218         } else if (localthis.itemtype == 'template') {
219             requests = ajax.call([
220                 { methodname: 'core_competency_remove_competency_from_template',
221                     args: { templateid: localthis.itemid, competencyid: deleteid } },
222                 { methodname: 'tool_lp_data_for_template_competencies_page',
223                     args: { templateid: localthis.itemid, pagecontext: { contextid: localthis.pageContextId } } }
224             ]);
225             pagerender = 'tool_lp/template_competencies_page';
226             pageregion = 'templatecompetenciespage';
227         } else if (localthis.itemtype == 'plan') {
228             requests = ajax.call([
229                 { methodname: 'core_competency_remove_competency_from_plan',
230                     args: { planid: localthis.itemid, competencyid: deleteid } },
231                 { methodname: 'tool_lp_data_for_plan_page',
232                     args: { planid: localthis.itemid } }
233             ]);
234             pagerender = 'tool_lp/plan_page';
235             pageregion = 'plan-page';
236         }
238         requests[1].done(function(context) {
239             templates.render(pagerender, context).done(function(html, js) {
240                 $('[data-region="' + pageregion + '"]').replaceWith(html);
241                 templates.runTemplateJS(js);
242             }).fail(notification.exception);
243         }).fail(notification.exception);
245     };
247     /**
248      * Show a confirm dialogue before deleting a competency.
249      *
250      * @method deleteHandler
251      * @param {int} deleteid The id of record to delete.
252      */
253     competencies.prototype.deleteHandler = function(deleteid) {
254         var localthis = this;
255         var requests = [];
256         var message;
258         if (localthis.itemtype == 'course') {
259             message = 'unlinkcompetencycourse';
260         } else if (localthis.itemtype == 'template') {
261             message = 'unlinkcompetencytemplate';
262         } else if (localthis.itemtype == 'plan') {
263             message = 'unlinkcompetencyplan';
264         } else {
265             return;
266         }
268         requests = ajax.call([{
269             methodname: 'core_competency_read_competency',
270             args: { id: deleteid }
271         }]);
273         requests[0].done(function(competency) {
274             str.get_strings([
275                 { key: 'confirm', component: 'moodle' },
276                 { key: message, component: 'tool_lp', param: competency.shortname },
277                 { key: 'confirm', component: 'moodle' },
278                 { key: 'cancel', component: 'moodle' }
279             ]).done(function (strings) {
280                 notification.confirm(
281                     strings[0], // Confirm.
282                     strings[1], // Unlink the competency X from the course?
283                     strings[2], // Confirm.
284                     strings[3], // Cancel.
285                     function() {
286                         localthis.doDelete(deleteid);
287                     }
288                 );
289             }).fail(notification.exception);
290         }).fail(notification.exception);
291     };
293     /**
294      * Register the javascript event handlers for this page.
295      *
296      * @method registerEvents
297      */
298     competencies.prototype.registerEvents = function() {
299         var localthis = this;
301         if (localthis.itemtype == 'course') {
302             // Course completion rule handling.
303             $('[data-region="coursecompetenciespage"]').on('change', 'select[data-field="ruleoutcome"]', function(e){
304                 var requests = [];
305                 var pagerender = 'tool_lp/course_competencies_page';
306                 var pageregion = 'coursecompetenciespage';
307                 var coursecompetencyid = $(e.target).data('id');
308                 var ruleoutcome = $(e.target).val();
309                 requests = ajax.call([
310                     { methodname: 'core_competency_set_course_competency_ruleoutcome',
311                       args: { coursecompetencyid: coursecompetencyid, ruleoutcome: ruleoutcome } },
312                     { methodname: 'tool_lp_data_for_course_competencies_page',
313                       args: { courseid: localthis.itemid } }
314                 ]);
316                 requests[1].done(function(context) {
317                     templates.render(pagerender, context).done(function(html, js) {
318                         $('[data-region="' + pageregion + '"]').replaceWith(html);
319                         templates.runTemplateJS(js);
320                     }).fail(notification.exception);
321                 }).fail(notification.exception);
322             });
323         }
325         $('[data-region="actions"] button').click(function(e) {
326             e.preventDefault();
327             localthis.pickCompetency();
328         });
329         $('[data-action="delete-competency-link"]').click(function(e) {
330             e.preventDefault();
332             var deleteid = $(e.target).closest('[data-id]').data('id');
333             localthis.deleteHandler(deleteid);
334         });
335     };
337     return /** @alias module:tool_lp/competencies */ competencies;
338 });