admin/tool/lp/amd/src/competencydialogue.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. * Display Competency in dialogue box.
  17. *
  18. * @module tool_lp/competencydialogue
  19. * @copyright 2015 Issam Taboubi <issam.taboubi@umontreal.ca>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery',
  23. 'core/notification',
  24. 'core/ajax',
  25. 'core/templates',
  26. 'core/str',
  27. 'tool_lp/dialogue'],
  28. function($, notification, ajax, templates, str, Dialogue) {
  29. /**
  30. * The main instance we'll be working with.
  31. *
  32. * @type {Competencydialogue}
  33. */
  34. var instance;
  35. /**
  36. * Constructor for CompetencyDialogue.
  37. */
  38. var Competencydialogue = function() {
  39. // Intentionally left empty.
  40. };
  41. /**
  42. * Log the competency viewed event.
  43. *
  44. * @param {Number} competencyId The competency ID.
  45. * @method triggerCompetencyViewedEvent
  46. */
  47. Competencydialogue.prototype.triggerCompetencyViewedEvent = function(competencyId) {
  48. ajax.call([{
  49. methodname: 'core_competency_competency_viewed',
  50. args: {id: competencyId}
  51. }]);
  52. };
  53. /**
  54. * Display a dialogue box by competencyid.
  55. *
  56. * @param {Number} competencyid The competency ID.
  57. * @param {Object} options The options.
  58. * @method showDialogue
  59. */
  60. Competencydialogue.prototype.showDialogue = function(competencyid, options) {
  61. var datapromise = this.getCompetencyDataPromise(competencyid, options);
  62. var localthis = this;
  63. datapromise.done(function(data) {
  64. // Inner Html in the dialogue content.
  65. templates.render('tool_lp/competency_summary', data)
  66. .done(function(html) {
  67. // Log competency viewed event.
  68. localthis.triggerCompetencyViewedEvent(competencyid);
  69. // Show the dialogue.
  70. new Dialogue(
  71. data.competency.shortname,
  72. html
  73. );
  74. }).fail(notification.exception);
  75. }).fail(notification.exception);
  76. };
  77. /**
  78. * Display a dialogue box from data.
  79. *
  80. * @param {Object} dataSource data to be used to display dialogue box
  81. * @method showDialogueFromData
  82. */
  83. Competencydialogue.prototype.showDialogueFromData = function(dataSource) {
  84. var localthis = this;
  85. // Inner Html in the dialogue content.
  86. templates.render('tool_lp/competency_summary', dataSource)
  87. .done(function(html) {
  88. // Log competency viewed event.
  89. localthis.triggerCompetencyViewedEvent(dataSource.id);
  90. // Show the dialogue.
  91. new Dialogue(
  92. dataSource.shortname,
  93. html,
  94. localthis.enhanceDialogue
  95. );
  96. }).fail(notification.exception);
  97. };
  98. /**
  99. * The action on the click event.
  100. *
  101. * @param {Event} e event click
  102. * @method clickEventHandler
  103. */
  104. Competencydialogue.prototype.clickEventHandler = function(e) {
  105. var compdialogue = e.data.compdialogue;
  106. var currentTarget = $(e.currentTarget);
  107. var competencyid = currentTarget.data('id');
  108. var includerelated = !(currentTarget.data('excluderelated'));
  109. var includecourses = currentTarget.data('includecourses');
  110. // Show the dialogue box.
  111. compdialogue.showDialogue(competencyid, {
  112. includerelated: includerelated,
  113. includecourses: includecourses
  114. });
  115. e.preventDefault();
  116. };
  117. /**
  118. * Get a promise on data competency.
  119. *
  120. * @param {Number} competencyid
  121. * @param {Object} options
  122. * @return {Promise} return promise on data request
  123. * @method getCompetencyDataPromise
  124. */
  125. Competencydialogue.prototype.getCompetencyDataPromise = function(competencyid, options) {
  126. var requests = ajax.call([
  127. {methodname: 'tool_lp_data_for_competency_summary',
  128. args: {competencyid: competencyid,
  129. includerelated: options.includerelated || false,
  130. includecourses: options.includecourses || false
  131. }
  132. }
  133. ]);
  134. return requests[0].then(function(context) {
  135. return context;
  136. }).fail(notification.exception);
  137. };
  138. return /** @alias module:tool_lp/competencydialogue */ {
  139. /**
  140. * Initialise the competency dialogue module.
  141. *
  142. * Only the first call matters.
  143. */
  144. init: function() {
  145. if (typeof instance !== 'undefined') {
  146. return;
  147. }
  148. // Instantiate the one instance and delegate event on the body.
  149. instance = new Competencydialogue();
  150. $('body').delegate('[data-action="competency-dialogue"]', 'click', {compdialogue: instance},
  151. instance.clickEventHandler.bind(instance));
  152. }
  153. };
  154. });