admin/tool/lp/amd/src/course_competency_settings.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. * Change the course competency settings in a popup.
  17. *
  18. * @module tool_lp/course_competency_settings
  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',
  23. 'core/notification',
  24. 'tool_lp/dialogue',
  25. 'core/str',
  26. 'core/ajax',
  27. 'core/templates',
  28. 'core/pending'
  29. ],
  30. function($, notification, Dialogue, str, ajax, templates, Pending) {
  31. /**
  32. * Constructor
  33. *
  34. * @param {String} selector - selector for the links to open the dialogue.
  35. */
  36. var settingsMod = function(selector) {
  37. $(selector).on('click', this.configureSettings.bind(this));
  38. };
  39. /** @property {Dialogue} Reference to the dialogue that we opened. */
  40. settingsMod.prototype._dialogue = null;
  41. /**
  42. * Open the configure settings dialogue.
  43. *
  44. * @param {Event} e
  45. * @method configureSettings
  46. */
  47. settingsMod.prototype.configureSettings = function(e) {
  48. var pendingPromise = new Pending();
  49. var courseid = $(e.target).closest('a').data('courseid');
  50. var currentValue = $(e.target).closest('a').data('pushratingstouserplans');
  51. var context = {
  52. courseid: courseid,
  53. settings: {pushratingstouserplans: currentValue}
  54. };
  55. e.preventDefault();
  56. $.when(
  57. str.get_string('configurecoursecompetencysettings', 'tool_lp'),
  58. templates.render('tool_lp/course_competency_settings', context),
  59. )
  60. .then(function(title, templateResult) {
  61. this._dialogue = new Dialogue(
  62. title,
  63. templateResult[0],
  64. this.addListeners.bind(this)
  65. );
  66. return this._dialogue;
  67. }.bind(this))
  68. .then(pendingPromise.resolve)
  69. .catch(notification.exception);
  70. };
  71. /**
  72. * Add the save listener to the form.
  73. *
  74. * @method addSaveListener
  75. */
  76. settingsMod.prototype.addListeners = function() {
  77. var save = this._find('[data-action="save"]');
  78. save.on('click', this.saveSettings.bind(this));
  79. var cancel = this._find('[data-action="cancel"]');
  80. cancel.on('click', this.cancelChanges.bind(this));
  81. };
  82. /**
  83. * Cancel the changes.
  84. *
  85. * @param {Event} e
  86. * @method cancelChanges
  87. */
  88. settingsMod.prototype.cancelChanges = function(e) {
  89. e.preventDefault();
  90. this._dialogue.close();
  91. };
  92. /**
  93. * Cancel the changes.
  94. *
  95. * @param {String} selector
  96. * @return {JQuery}
  97. */
  98. settingsMod.prototype._find = function(selector) {
  99. return $('[data-region="coursecompetencysettings"]').find(selector);
  100. };
  101. /**
  102. * Save the settings.
  103. *
  104. * @param {Event} e
  105. * @method saveSettings
  106. */
  107. settingsMod.prototype.saveSettings = function(e) {
  108. var pendingPromise = new Pending();
  109. e.preventDefault();
  110. var newValue = this._find('input[name="pushratingstouserplans"]:checked').val();
  111. var courseId = this._find('input[name="courseid"]').val();
  112. var settings = {pushratingstouserplans: newValue};
  113. ajax.call([
  114. {methodname: 'core_competency_update_course_competency_settings',
  115. args: {courseid: courseId, settings: settings}}
  116. ])[0]
  117. .then(function() {
  118. return this.refreshCourseCompetenciesPage();
  119. }.bind(this))
  120. .then(pendingPromise.resolve)
  121. .catch(notification.exception);
  122. };
  123. /**
  124. * Refresh the course competencies page.
  125. *
  126. * @method saveSettings
  127. */
  128. settingsMod.prototype.refreshCourseCompetenciesPage = function() {
  129. var courseId = this._find('input[name="courseid"]').val();
  130. var pendingPromise = new Pending();
  131. ajax.call([
  132. {methodname: 'tool_lp_data_for_course_competencies_page',
  133. args: {courseid: courseId, moduleid: 0}}
  134. ])[0]
  135. .then(function(context) {
  136. return templates.render('tool_lp/course_competencies_page', context);
  137. })
  138. .then(function(html, js) {
  139. templates.replaceNode($('[data-region="coursecompetenciespage"]'), html, js);
  140. this._dialogue.close();
  141. return;
  142. }.bind(this))
  143. .then(pendingPromise.resolve)
  144. .catch(notification.exception);
  145. };
  146. return /** @alias module:tool_lp/configurecoursecompetencysettings */ settingsMod;
  147. });