admin/tool/lp/amd/src/templateactions.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/>.
  15. /**
  16. * Handle actions on learning plan templates via ajax.
  17. *
  18. * @module tool_lp/templateactions
  19. * @copyright 2015 Damyon Wiese <damyon@moodle.com>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery', 'core/templates', 'core/ajax', 'core/notification', 'core/str', 'tool_lp/actionselector'],
  23. function($, templates, ajax, notification, str, Actionselector) {
  24. // Private variables and functions.
  25. /** @var {Number} pagecontextid The id of the context */
  26. var pagecontextid = 0;
  27. /** @var {Number} templateid The id of the template */
  28. var templateid = 0;
  29. /** @var {Boolean} Action to apply to plans when deleting a template */
  30. var deleteplans = true;
  31. /**
  32. * Callback to replace the dom element with the rendered template.
  33. *
  34. * @method updatePage
  35. * @param {String} newhtml The new html to insert.
  36. * @param {String} newjs The new js to run.
  37. */
  38. var updatePage = function(newhtml, newjs) {
  39. $('[data-region="managetemplates"]').replaceWith(newhtml);
  40. templates.runTemplateJS(newjs);
  41. };
  42. /**
  43. * Callback to render the page template again and update the page.
  44. *
  45. * @method reloadList
  46. * @param {Object} context The context for the template.
  47. */
  48. var reloadList = function(context) {
  49. templates.render('tool_lp/manage_templates_page', context)
  50. .done(updatePage)
  51. .fail(notification.exception);
  52. };
  53. /**
  54. * Delete a template and reload the page.
  55. * @method doDelete
  56. */
  57. var doDelete = function() {
  58. // We are chaining ajax requests here.
  59. var requests = ajax.call([{
  60. methodname: 'core_competency_delete_template',
  61. args: {id: templateid,
  62. deleteplans: deleteplans}
  63. }, {
  64. methodname: 'tool_lp_data_for_templates_manage_page',
  65. args: {
  66. pagecontext: {
  67. contextid: pagecontextid
  68. }
  69. }
  70. }]);
  71. requests[1].done(reloadList).fail(notification.exception);
  72. };
  73. /**
  74. * Duplicate a template and reload the page.
  75. * @method doDuplicate
  76. * @param {Event} e
  77. */
  78. var doDuplicate = function(e) {
  79. e.preventDefault();
  80. templateid = $(this).attr('data-templateid');
  81. // We are chaining ajax requests here.
  82. var requests = ajax.call([{
  83. methodname: 'core_competency_duplicate_template',
  84. args: {id: templateid}
  85. }, {
  86. methodname: 'tool_lp_data_for_templates_manage_page',
  87. args: {
  88. pagecontext: {
  89. contextid: pagecontextid
  90. }
  91. }
  92. }]);
  93. requests[1].done(reloadList).fail(notification.exception);
  94. };
  95. /**
  96. * Handler for "Delete learning plan template" actions.
  97. * @method confirmDelete
  98. * @param {Event} e
  99. */
  100. var confirmDelete = function(e) {
  101. e.preventDefault();
  102. var id = $(this).attr('data-templateid');
  103. templateid = id;
  104. deleteplans = true;
  105. var requests = ajax.call([{
  106. methodname: 'core_competency_read_template',
  107. args: {id: templateid}
  108. }, {
  109. methodname: 'core_competency_template_has_related_data',
  110. args: {id: templateid}
  111. }]);
  112. requests[0].done(function(template) {
  113. requests[1].done(function(templatehasrelateddata) {
  114. if (templatehasrelateddata) {
  115. str.get_strings([
  116. {key: 'deletetemplate', component: 'tool_lp', param: template.shortname},
  117. {key: 'deletetemplatewithplans', component: 'tool_lp'},
  118. {key: 'deleteplans', component: 'tool_lp'},
  119. {key: 'unlinkplanstemplate', component: 'tool_lp'},
  120. {key: 'confirm', component: 'moodle'},
  121. {key: 'cancel', component: 'moodle'}
  122. ]).done(function(strings) {
  123. var actions = [{'text': strings[2], 'value': 'delete'},
  124. {'text': strings[3], 'value': 'unlink'}];
  125. var actionselector = new Actionselector(
  126. strings[0], // Title.
  127. strings[1], // Message
  128. actions, // Radio button options.
  129. strings[4], // Confirm.
  130. strings[5]); // Cancel.
  131. actionselector.display();
  132. actionselector.on('save', function(e, data) {
  133. if (data.action != 'delete') {
  134. deleteplans = false;
  135. }
  136. doDelete();
  137. });
  138. }).fail(notification.exception);
  139. } else {
  140. str.get_strings([
  141. {key: 'confirm', component: 'moodle'},
  142. {key: 'deletetemplate', component: 'tool_lp', param: template.shortname},
  143. {key: 'delete', component: 'moodle'},
  144. {key: 'cancel', component: 'moodle'}
  145. ]).done(function(strings) {
  146. notification.confirm(
  147. strings[0], // Confirm.
  148. strings[1], // Delete learning plan template X?
  149. strings[2], // Delete.
  150. strings[3], // Cancel.
  151. doDelete
  152. );
  153. }).fail(notification.exception);
  154. }
  155. }).fail(notification.exception);
  156. }).fail(notification.exception);
  157. };
  158. return /** @alias module:tool_lp/templateactions */ {
  159. // Public variables and functions.
  160. /**
  161. * Expose the event handler for the delete.
  162. * @method deleteHandler
  163. * @param {Event} e
  164. */
  165. deleteHandler: confirmDelete,
  166. /**
  167. * Expose the event handler for the duplicate.
  168. * @method duplicateHandler
  169. * @param {Event} e
  170. */
  171. duplicateHandler: doDuplicate,
  172. /**
  173. * Initialise the module.
  174. * @method init
  175. * @param {Number} contextid The context id of the page.
  176. */
  177. init: function(contextid) {
  178. pagecontextid = contextid;
  179. }
  180. };
  181. });