admin/tool/lp/amd/src/competency_rule_points.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. * Competency rule points module.
  17. *
  18. * @module tool_lp/competency_rule_points
  19. * @copyright 2015 Frédéric Massart - FMCorz.net
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery',
  23. 'core/str',
  24. 'core/templates',
  25. 'tool_lp/competency_rule',
  26. ],
  27. function($, Str, Templates, RuleBase) {
  28. /**
  29. * Competency rule points class.
  30. */
  31. var Rule = function() {
  32. RuleBase.apply(this, arguments);
  33. };
  34. Rule.prototype = Object.create(RuleBase.prototype);
  35. /** @property {Node} Reference to the container in which the template was included. */
  36. Rule.prototype._container = null;
  37. /** @property {Boolean} Whether or not the template was included. */
  38. Rule.prototype._templateLoaded = false;
  39. /**
  40. * The config established by this rule.
  41. *
  42. * @return {String}
  43. * @method getConfig
  44. */
  45. Rule.prototype.getConfig = function() {
  46. return JSON.stringify({
  47. base: {
  48. points: this._getRequiredPoints(),
  49. },
  50. competencies: this._getCompetenciesConfig()
  51. });
  52. };
  53. /**
  54. * Gathers the input provided by the user for competencies.
  55. *
  56. * @return {Array} Containing id, points and required.
  57. * @method _getCompetenciesConfig
  58. * @protected
  59. */
  60. Rule.prototype._getCompetenciesConfig = function() {
  61. var competencies = [];
  62. this._container.find('[data-competency]').each(function() {
  63. var node = $(this),
  64. id = node.data('competency'),
  65. points = parseInt(node.find('[name="points"]').val(), 10),
  66. required = node.find('[name="required"]').prop('checked');
  67. competencies.push({
  68. id: id,
  69. points: points,
  70. required: required ? 1 : 0
  71. });
  72. });
  73. return competencies;
  74. };
  75. /**
  76. * Fetches the required points set by the user.
  77. *
  78. * @return {Number}
  79. * @method _getRequiredPoints
  80. * @protected
  81. */
  82. Rule.prototype._getRequiredPoints = function() {
  83. return parseInt(this._container.find('[name="requiredpoints"]').val() || 1, 10);
  84. };
  85. /**
  86. * Return the type of the module.
  87. *
  88. * @return {String}
  89. * @method getType
  90. */
  91. Rule.prototype.getType = function() {
  92. return 'core_competency\\competency_rule_points';
  93. };
  94. /**
  95. * Callback to inject the template.
  96. *
  97. * @param {Node} container Node to inject in.
  98. * @return {Promise} Resolved when done.
  99. * @method injectTemplate
  100. */
  101. Rule.prototype.injectTemplate = function(container) {
  102. var self = this,
  103. children = this._tree.getChildren(this._competency.id),
  104. context,
  105. config = {
  106. base: {points: 2},
  107. competencies: []
  108. };
  109. this._templateLoaded = false;
  110. // Only pre-load the configuration when the competency is using this rule.
  111. if (self._competency.ruletype == self.getType()) {
  112. try {
  113. config = JSON.parse(self._competency.ruleconfig);
  114. } catch (e) {
  115. // eslint-disable-line no-empty
  116. }
  117. }
  118. context = {
  119. requiredpoints: (config && config.base) ? config.base.points : 2,
  120. competency: self._competency,
  121. children: []
  122. };
  123. $.each(children, function(index, child) {
  124. var competency = {
  125. id: child.id,
  126. shortname: child.shortname,
  127. required: false,
  128. points: 0
  129. };
  130. if (config) {
  131. $.each(config.competencies, function(index, comp) {
  132. if (comp.id == competency.id) {
  133. competency.required = comp.required ? true : false;
  134. competency.points = comp.points;
  135. }
  136. });
  137. }
  138. context.children.push(competency);
  139. });
  140. return Templates.render('tool_lp/competency_rule_points', context).then(function(html) {
  141. self._container = container;
  142. container.html(html);
  143. container.find('input').change(function() {
  144. self._triggerChange();
  145. });
  146. // We're done, let's trigger a change.
  147. self._templateLoaded = true;
  148. self._triggerChange();
  149. return;
  150. });
  151. };
  152. /**
  153. * Whether or not the current config is valid.
  154. *
  155. * @return {Boolean}
  156. * @method isValid
  157. */
  158. Rule.prototype.isValid = function() {
  159. if (!this._templateLoaded) {
  160. return false;
  161. }
  162. var required = this._getRequiredPoints(),
  163. max = 0,
  164. valid = true;
  165. $.each(this._getCompetenciesConfig(), function(index, competency) {
  166. if (competency.points < 0) {
  167. valid = false;
  168. }
  169. max += competency.points;
  170. });
  171. valid = valid && max >= required;
  172. return valid;
  173. };
  174. return Rule;
  175. });