admin/tool/lp/amd/src/grade_user_competency_inline.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 tool_lp/grade_user_competency_inline
  19. * @copyright 2015 Damyon Wiese
  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/log',
  26. 'tool_lp/grade_dialogue',
  27. 'tool_lp/event_base',
  28. 'tool_lp/scalevalues',
  29. ], function($, notification, ajax, log, GradeDialogue, EventBase, ScaleValues) {
  30. /**
  31. * InlineEditor
  32. *
  33. * @class tool_lp/grade_user_competency_inline
  34. * @param {String} selector The selector to trigger the grading.
  35. * @param {Number} scaleId The id of the scale for this competency.
  36. * @param {Number} competencyId The id of the competency.
  37. * @param {Number} userId The id of the user.
  38. * @param {Number} planId The id of the plan.
  39. * @param {Number} courseId The id of the course.
  40. * @param {String} chooseStr Language string for choose a rating.
  41. */
  42. var InlineEditor = function(selector, scaleId, competencyId, userId, planId, courseId, chooseStr) {
  43. EventBase.prototype.constructor.apply(this, []);
  44. var trigger = $(selector);
  45. if (!trigger.length) {
  46. throw new Error('Could not find the trigger');
  47. }
  48. this._scaleId = scaleId;
  49. this._competencyId = competencyId;
  50. this._userId = userId;
  51. this._planId = planId;
  52. this._courseId = courseId;
  53. this._chooseStr = chooseStr;
  54. this._setUp();
  55. trigger.click(function(e) {
  56. e.preventDefault();
  57. this._dialogue.display();
  58. }.bind(this));
  59. if (this._planId) {
  60. this._methodName = 'core_competency_grade_competency_in_plan';
  61. this._args = {
  62. competencyid: this._competencyId,
  63. planid: this._planId
  64. };
  65. } else if (this._courseId) {
  66. this._methodName = 'core_competency_grade_competency_in_course';
  67. this._args = {
  68. competencyid: this._competencyId,
  69. courseid: this._courseId,
  70. userid: this._userId
  71. };
  72. } else {
  73. this._methodName = 'core_competency_grade_competency';
  74. this._args = {
  75. userid: this._userId,
  76. competencyid: this._competencyId
  77. };
  78. }
  79. };
  80. InlineEditor.prototype = Object.create(EventBase.prototype);
  81. /**
  82. * Setup.
  83. *
  84. * @method _setUp
  85. */
  86. InlineEditor.prototype._setUp = function() {
  87. var options = [],
  88. self = this;
  89. M.util.js_pending('tool_lp/grade_user_competency_inline:_setUp');
  90. var promise = ScaleValues.get_values(self._scaleId);
  91. promise.then(function(scalevalues) {
  92. options.push({
  93. value: '',
  94. name: self._chooseStr
  95. });
  96. for (var i = 0; i < scalevalues.length; i++) {
  97. var optionConfig = scalevalues[i];
  98. options.push({
  99. value: optionConfig.id,
  100. name: optionConfig.name
  101. });
  102. }
  103. return options;
  104. })
  105. .then(function(options) {
  106. return new GradeDialogue(options);
  107. })
  108. .then(function(dialogue) {
  109. dialogue.on('rated', function(e, data) {
  110. var args = self._args;
  111. args.grade = data.rating;
  112. args.note = data.note;
  113. ajax.call([{
  114. methodname: self._methodName,
  115. args: args,
  116. done: function(evidence) {
  117. self._trigger('competencyupdated', {args: args, evidence: evidence});
  118. },
  119. fail: notification.exception
  120. }]);
  121. });
  122. return dialogue;
  123. })
  124. .then(function(dialogue) {
  125. self._dialogue = dialogue;
  126. M.util.js_complete('tool_lp/grade_user_competency_inline:_setUp');
  127. return;
  128. })
  129. .fail(notification.exception);
  130. };
  131. /** @property {Number} The scale id for this competency. */
  132. InlineEditor.prototype._scaleId = null;
  133. /** @property {Number} The id of the competency. */
  134. InlineEditor.prototype._competencyId = null;
  135. /** @property {Number} The id of the user. */
  136. InlineEditor.prototype._userId = null;
  137. /** @property {Number} The id of the plan. */
  138. InlineEditor.prototype._planId = null;
  139. /** @property {Number} The id of the course. */
  140. InlineEditor.prototype._courseId = null;
  141. /** @property {String} The text for Choose rating. */
  142. InlineEditor.prototype._chooseStr = null;
  143. /** @property {GradeDialogue} The grading dialogue. */
  144. InlineEditor.prototype._dialogue = null;
  145. return InlineEditor;
  146. });