admin/tool/lp/amd/src/competency_rule.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 base module.
  17. *
  18. * @module tool_lp/competency_rule
  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'], function($) {
  23. /**
  24. * Competency rule abstract class.
  25. *
  26. * Any competency rule should extend this object. The event 'change' should be
  27. * triggered on the instance when the configuration has changed. This will allow
  28. * the components using the rule to gather the config, or check its validity.
  29. *
  30. * this._triggerChange();
  31. *
  32. * @param {Tree} tree The competency tree.
  33. */
  34. var Rule = function(tree) {
  35. this._eventNode = $('<div>');
  36. this._ready = $.Deferred();
  37. this._tree = tree;
  38. };
  39. /** @property {Object} The current competency. */
  40. Rule.prototype._competency = null;
  41. /** @property {Node} The node we attach the events to. */
  42. Rule.prototype._eventNode = null;
  43. /** @property {Promise} Resolved when the object is ready. */
  44. Rule.prototype._ready = null;
  45. /** @property {Tree} The competency tree. */
  46. Rule.prototype._tree = null;
  47. /**
  48. * Whether or not the current competency can be configured using this rule.
  49. *
  50. * @return {Boolean}
  51. * @method canConfig
  52. */
  53. Rule.prototype.canConfig = function() {
  54. return this._tree.hasChildren(this._competency.id);
  55. };
  56. /**
  57. * The config established by this rule.
  58. *
  59. * To override in subclasses when relevant.
  60. *
  61. * @return {String|null}
  62. * @method getConfig
  63. */
  64. Rule.prototype.getConfig = function() {
  65. return null;
  66. };
  67. /**
  68. * Return the type of the module.
  69. *
  70. * @return {String}
  71. * @method getType
  72. */
  73. Rule.prototype.getType = function() {
  74. throw new Error('Not implemented');
  75. };
  76. /**
  77. * The init process.
  78. *
  79. * Do not override this, instead override _load.
  80. *
  81. * @return {Promise} Revoled when the plugin is initialised.
  82. * @method init
  83. */
  84. Rule.prototype.init = function() {
  85. return this._load();
  86. };
  87. /**
  88. * Callback to inject the template.
  89. *
  90. * @returns {Promise} Resolved when done.
  91. * @method injectTemplate
  92. */
  93. Rule.prototype.injectTemplate = function() {
  94. return $.Deferred().reject().promise();
  95. };
  96. /**
  97. * Whether or not the current config is valid.
  98. *
  99. * Plugins should override this.
  100. *
  101. * @return {Boolean}
  102. * @method _isValid
  103. */
  104. Rule.prototype.isValid = function() {
  105. return false;
  106. };
  107. /**
  108. * Load the class.
  109. *
  110. * @return {Promise}
  111. * @method _load
  112. * @protected
  113. */
  114. Rule.prototype._load = function() {
  115. return $.when();
  116. };
  117. /**
  118. * Register an event listener.
  119. *
  120. * @param {String} type The event type.
  121. * @param {Function} handler The event listener.
  122. * @method on
  123. */
  124. Rule.prototype.on = function(type, handler) {
  125. this._eventNode.on(type, handler);
  126. };
  127. /**
  128. * Sets the current competency.
  129. *
  130. * @param {Competency} competency
  131. * @method setTargetCompetency
  132. */
  133. Rule.prototype.setTargetCompetency = function(competency) {
  134. this._competency = competency;
  135. };
  136. /**
  137. * Trigger an event.
  138. *
  139. * @param {String} type The type of event.
  140. * @param {Object} data The data to pass to the listeners.
  141. * @method _trigger
  142. * @protected
  143. */
  144. Rule.prototype._trigger = function(type, data) {
  145. this._eventNode.trigger(type, [data]);
  146. };
  147. /**
  148. * Trigger the change event.
  149. *
  150. * @method _triggerChange
  151. * @protected
  152. */
  153. Rule.prototype._triggerChange = function() {
  154. this._trigger('change', this);
  155. };
  156. return /** @alias module:tool_lp/competency_rule */ Rule;
  157. });