report/competency/amd/src/grading_popup.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. * Module to enable inline editing of a comptency grade.
  17. *
  18. * @module report_competency/grading_popup
  19. * @copyright 2015 Damyon Wiese
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery', 'core/notification', 'core/str', 'core/ajax', 'core/log', 'core/templates', 'tool_lp/dialogue'],
  23. function($, notification, str, ajax, log, templates, Dialogue) {
  24. /**
  25. * GradingPopup
  26. *
  27. * @class report_competency/grading_popup
  28. * @param {String} regionSelector The regionSelector
  29. * @param {String} userCompetencySelector The userCompetencySelector
  30. */
  31. var GradingPopup = function(regionSelector, userCompetencySelector) {
  32. this._regionSelector = regionSelector;
  33. this._userCompetencySelector = userCompetencySelector;
  34. $(this._regionSelector).on('click', this._userCompetencySelector, this._handleClick.bind(this));
  35. };
  36. /**
  37. * Get the data from the clicked cell and open the popup.
  38. *
  39. * @method _handleClick
  40. * @param {Event} e The event
  41. */
  42. GradingPopup.prototype._handleClick = function(e) {
  43. var cell = $(e.target).closest(this._userCompetencySelector);
  44. var competencyId = $(cell).data('competencyid');
  45. var courseId = $(cell).data('courseid');
  46. var userId = $(cell).data('userid');
  47. log.debug('Clicked on cell: competencyId=' + competencyId + ', courseId=' + courseId + ', userId=' + userId);
  48. var requests = ajax.call([{
  49. methodname: 'tool_lp_data_for_user_competency_summary_in_course',
  50. args: {userid: userId, competencyid: competencyId, courseid: courseId},
  51. }, {
  52. methodname: 'core_competency_user_competency_viewed_in_course',
  53. args: {userid: userId, competencyid: competencyId, courseid: courseId},
  54. }]);
  55. $.when(requests[0], requests[1])
  56. .then(this._contextLoaded.bind(this))
  57. .catch(notification.exception);
  58. };
  59. /**
  60. * We loaded the context, now render the template.
  61. *
  62. * @method _contextLoaded
  63. * @param {Object} context
  64. * @returns {Promise}
  65. */
  66. GradingPopup.prototype._contextLoaded = function(context) {
  67. // We have to display user info in popup.
  68. context.displayuser = true;
  69. M.util.js_pending('report_competency/grading_popup:_contextLoaded');
  70. return $.when(
  71. str.get_string('usercompetencysummary', 'report_competency'),
  72. templates.render('tool_lp/user_competency_summary_in_course', context)
  73. )
  74. .then(function(title, templateData) {
  75. return new Dialogue(
  76. title,
  77. templateData[0],
  78. function() {
  79. templates.runTemplateJS(templateData[1]);
  80. M.util.js_complete('report_competency/grading_popup:_contextLoaded');
  81. },
  82. this._refresh.bind(this),
  83. true
  84. );
  85. }.bind(this));
  86. };
  87. /**
  88. * Refresh the page.
  89. *
  90. * @method _refresh
  91. * @returns {Promise}
  92. */
  93. GradingPopup.prototype._refresh = function() {
  94. var region = $(this._regionSelector);
  95. var courseId = region.data('courseid');
  96. var moduleId = region.data('moduleid');
  97. var userId = region.data('userid');
  98. // The module id is expected to be an integer, so don't pass empty string.
  99. if (moduleId === '') {
  100. moduleId = 0;
  101. }
  102. return ajax.call([{
  103. methodname: 'report_competency_data_for_report',
  104. args: {courseid: courseId, userid: userId, moduleid: moduleId},
  105. done: this._pageContextLoaded.bind(this),
  106. fail: notification.exception
  107. }]);
  108. };
  109. /**
  110. * We loaded the context, now render the template.
  111. *
  112. * @method _pageContextLoaded
  113. * @param {Object} context
  114. */
  115. GradingPopup.prototype._pageContextLoaded = function(context) {
  116. templates.render('report_competency/report', context)
  117. .then(function(html, js) {
  118. templates.replaceNode(this._regionSelector, html, js);
  119. return;
  120. }.bind(this))
  121. .catch(notification.exception);
  122. };
  123. /** @property {String} The selector for the region with the user competencies */
  124. GradingPopup.prototype._regionSelector = null;
  125. /** @property {String} The selector for the region with a single user competencies */
  126. GradingPopup.prototype._userCompetencySelector = null;
  127. return GradingPopup;
  128. });