[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/lp/amd/src/ -> frameworkactions.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   * Competency frameworks actions via ajax.
  18   *
  19   * @module     tool_lp/frameworkactions
  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'], function($, templates, ajax, notification, str) {
  25      // Private variables and functions.
  26  
  27      /** @var {Number} pagecontextid The id of the context */
  28      var pagecontextid = 0;
  29  
  30      /** @var {Number} frameworkid The id of the framework */
  31      var frameworkid = 0;
  32  
  33      /**
  34       * Callback to replace the dom element with the rendered template.
  35       *
  36       * @param {String} newhtml The new html to insert.
  37       * @param {String} newjs The new js to run.
  38       */
  39      var updatePage = function(newhtml, newjs) {
  40          $('[data-region="managecompetencies"]').replaceWith(newhtml);
  41          templates.runTemplateJS(newjs);
  42      };
  43  
  44      /**
  45       * Callback to render the page template again and update the page.
  46       *
  47       * @param {Object} context The context for the template.
  48       */
  49      var reloadList = function(context) {
  50          templates.render('tool_lp/manage_competency_frameworks_page', context)
  51              .done(updatePage)
  52              .fail(notification.exception);
  53      };
  54  
  55      /**
  56       * Duplicate a framework and reload the page.
  57       * @method doDuplicate
  58       * @param {Event} e
  59       */
  60      var doDuplicate = function(e) {
  61          e.preventDefault();
  62  
  63          frameworkid = $(this).attr('data-frameworkid');
  64  
  65          // We are chaining ajax requests here.
  66          var requests = ajax.call([{
  67              methodname: 'core_competency_duplicate_competency_framework',
  68              args: {id: frameworkid}
  69          }, {
  70              methodname: 'tool_lp_data_for_competency_frameworks_manage_page',
  71              args: {
  72                  pagecontext: {
  73                      contextid: pagecontextid
  74                  }
  75              }
  76          }]);
  77          requests[1].done(reloadList).fail(notification.exception);
  78      };
  79      /**
  80       * Delete a framework and reload the page.
  81       */
  82      var doDelete = function() {
  83  
  84          // We are chaining ajax requests here.
  85          var requests = ajax.call([{
  86              methodname: 'core_competency_delete_competency_framework',
  87              args: {id: frameworkid}
  88          }, {
  89              methodname: 'tool_lp_data_for_competency_frameworks_manage_page',
  90              args: {
  91                  pagecontext: {
  92                      contextid: pagecontextid
  93                  }
  94              }
  95          }]);
  96          requests[0].done(function(success) {
  97              if (success === false) {
  98                  var req = ajax.call([{
  99                      methodname: 'core_competency_read_competency_framework',
 100                      args: {id: frameworkid}
 101                  }]);
 102                  req[0].done(function(framework) {
 103                      str.get_strings([
 104                          {key: 'frameworkcannotbedeleted', component: 'tool_lp', param: framework.shortname},
 105                          {key: 'cancel', component: 'moodle'}
 106                      ]).done(function(strings) {
 107                          notification.alert(
 108                              null,
 109                              strings[0]
 110                          );
 111                      }).fail(notification.exception);
 112                  });
 113              }
 114          }).fail(notification.exception);
 115          requests[1].done(reloadList).fail(notification.exception);
 116      };
 117  
 118      /**
 119       * Handler for "Delete competency framework" actions.
 120       * @param {Event} e
 121       */
 122      var confirmDelete = function(e) {
 123          e.preventDefault();
 124  
 125          var id = $(this).attr('data-frameworkid');
 126          frameworkid = id;
 127  
 128          var requests = ajax.call([{
 129              methodname: 'core_competency_read_competency_framework',
 130              args: {id: frameworkid}
 131          }]);
 132  
 133          requests[0].done(function(framework) {
 134              str.get_strings([
 135                  {key: 'confirm', component: 'moodle'},
 136                  {key: 'deletecompetencyframework', component: 'tool_lp', param: framework.shortname},
 137                  {key: 'delete', component: 'moodle'},
 138                  {key: 'cancel', component: 'moodle'}
 139              ]).done(function(strings) {
 140                  notification.confirm(
 141                      strings[0], // Confirm.
 142                      strings[1], // Delete competency framework X?
 143                      strings[2], // Delete.
 144                      strings[3], // Cancel.
 145                      doDelete
 146                  );
 147              }).fail(notification.exception);
 148          }).fail(notification.exception);
 149  
 150      };
 151  
 152  
 153      return /** @alias module:tool_lp/frameworkactions */ {
 154          // Public variables and functions.
 155  
 156          /**
 157           * Expose the event handler for delete.
 158           * @method deleteHandler
 159           * @param {Event} e
 160           */
 161          deleteHandler: confirmDelete,
 162  
 163          /**
 164           * Expose the event handler for duplicate.
 165           * @method duplicateHandler
 166           * @param {Event} e
 167           */
 168          duplicateHandler: doDuplicate,
 169  
 170          /**
 171           * Initialise the module.
 172           * @method init
 173           * @param {Number} contextid The context id of the page.
 174           */
 175          init: function(contextid) {
 176              pagecontextid = contextid;
 177          }
 178      };
 179  });


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