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