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