1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * Handle actions on learning plan templates via ajax.
19 * @module tool_lp/templateactions
21 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 define(['jquery', 'core/templates', 'core/ajax', 'core/notification', 'core/str','tool_lp/actionselector'],
25 function($, templates, ajax, notification, str, Actionselector) {
26 // Private variables and functions.
28 /** @var {Number} pagecontextid The id of the context */
29 var pagecontextid = 0;
31 /** @var {Number} templateid The id of the template */
34 /** @var {Boolean} Action to apply to plans when deleting a template */
35 var deleteplans = true;
38 * Callback to replace the dom element with the rendered template.
41 * @param {String} newhtml The new html to insert.
42 * @param {String} newjs The new js to run.
44 var updatePage = function(newhtml, newjs) {
45 $('[data-region="managetemplates"]').replaceWith(newhtml);
46 templates.runTemplateJS(newjs);
50 * Callback to render the page template again and update the page.
53 * @param {Object} context The context for the template.
55 var reloadList = function(context) {
56 templates.render('tool_lp/manage_templates_page', context)
58 .fail(notification.exception);
62 * Delete a template and reload the page.
65 var doDelete = function() {
67 // We are chaining ajax requests here.
68 var requests = ajax.call([{
69 methodname: 'core_competency_delete_template',
70 args: { id: templateid,
71 deleteplans: deleteplans }
73 methodname: 'tool_lp_data_for_templates_manage_page',
76 contextid: pagecontextid
80 requests[1].done(reloadList).fail(notification.exception);
84 * Duplicate a template and reload the page.
88 var doDuplicate = function(e) {
91 templateid = $(this).attr('data-templateid');
93 // We are chaining ajax requests here.
94 var requests = ajax.call([{
95 methodname: 'core_competency_duplicate_template',
96 args: { id: templateid }
98 methodname: 'tool_lp_data_for_templates_manage_page',
101 contextid: pagecontextid
105 requests[1].done(reloadList).fail(notification.exception);
109 * Handler for "Delete learning plan template" actions.
110 * @method confirmDelete
113 var confirmDelete = function(e) {
116 var id = $(this).attr('data-templateid');
120 var requests = ajax.call([{
121 methodname: 'core_competency_read_template',
122 args: { id: templateid }
124 methodname: 'core_competency_template_has_related_data',
125 args: { id: templateid }
128 requests[0].done(function(template) {
129 requests[1].done(function(templatehasrelateddata) {
130 if (templatehasrelateddata) {
132 { key: 'deletetemplate', component: 'tool_lp', param: template.shortname },
133 { key: 'deletetemplatewithplans', component: 'tool_lp' },
134 { key: 'deleteplans', component: 'tool_lp' },
135 { key: 'unlinkplanstemplate', component: 'tool_lp' },
136 { key: 'confirm', component: 'moodle' },
137 { key: 'cancel', component: 'moodle' }
138 ]).done(function (strings) {
139 var actions = [{'text': strings[2], 'value' : 'delete'},
140 {'text': strings[3], 'value' : 'unlink'}];
141 var actionselector = new Actionselector(
142 strings[0], // Title.
143 strings[1], // Message
144 actions, // Radio button options.
145 strings[4], // Confirm.
146 strings[5]); // Cancel.
147 actionselector.display();
148 actionselector.on('save', function(e, data) {
149 if (data.action != 'delete') {
154 }).fail(notification.exception);
158 { key: 'confirm', component: 'moodle' },
159 { key: 'deletetemplate', component: 'tool_lp', param: template.shortname },
160 { key: 'delete', component: 'moodle' },
161 { key: 'cancel', component: 'moodle' }
162 ]).done(function (strings) {
163 notification.confirm(
164 strings[0], // Confirm.
165 strings[1], // Delete learning plan template X?
166 strings[2], // Delete.
167 strings[3], // Cancel.
170 }).fail(notification.exception);
172 }).fail(notification.exception);
173 }).fail(notification.exception);
177 return /** @alias module:tool_lp/templateactions */ {
178 // Public variables and functions.
180 * Expose the event handler for the delete.
181 * @method deleteHandler
184 deleteHandler: confirmDelete,
187 * Expose the event handler for the duplicate.
188 * @method duplicateHandler
191 duplicateHandler: doDuplicate,
194 * Initialise the module.
196 * @param {Number} contextid The context id of the page.
198 init: function(contextid) {
199 pagecontextid = contextid;