[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/lp/amd/src/ -> templateactions.js (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1