[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * Handle selection changes on the competency tree. 18 * 19 * @module tool_lp/competencyselect 20 * @package tool_lp 21 * @copyright 2015 Damyon Wiese <damyon@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 define(['core/ajax', 'core/notification', 'core/templates', 'tool_lp/tree', 'tool_lp/competency_outcomes', 'jquery'], 25 function(ajax, notification, templates, Ariatree, CompOutcomes, $) { 26 27 // Private variables and functions. 28 /** @var {Object[]} competencies - Cached list of competencies */ 29 var competencies = {}; 30 31 /** @var {Number} competencyFrameworkId - The current framework id */ 32 var competencyFrameworkId = 0; 33 34 /** @var {String} competencyFrameworkShortName - The current framework short name */ 35 var competencyFrameworkShortName = ''; 36 37 /** @var {String} treeSelector - The selector for the root of the tree. */ 38 var treeSelector = ''; 39 40 /** @var {String} currentNodeId - The data-id of the current node in the tree. */ 41 var currentNodeId = ''; 42 43 /** @var {Boolean} competencyFramworkCanManage - Can manage the competencies framework */ 44 var competencyFramworkCanManage = false; 45 46 /** 47 * Build a tree from the flat list of competencies. 48 * @param {Object} parent The parent competency. 49 * @param {Array} all The list of all competencies. 50 */ 51 var addChildren = function(parent, all) { 52 var i = 0; 53 var current = false; 54 parent.haschildren = false; 55 parent.children = []; 56 for (i = 0; i < all.length; i++) { 57 current = all[i]; 58 if (current.parentid == parent.id) { 59 parent.haschildren = true; 60 parent.children.push(current); 61 addChildren(current, all); 62 } 63 } 64 }; 65 66 /** 67 * Load the list of competencies via ajax. Competencies are filtered by the searchtext. 68 * @param {String} searchtext The text to filter on. 69 * @return {promise} 70 */ 71 var loadCompetencies = function(searchtext) { 72 var deferred = $.Deferred(); 73 74 templates.render('tool_lp/loading', {}).done(function(loadinghtml, loadingjs) { 75 templates.replaceNodeContents($(treeSelector), loadinghtml, loadingjs); 76 77 var promises = ajax.call([{ 78 methodname: 'core_competency_search_competencies', 79 args: { 80 searchtext: searchtext, 81 competencyframeworkid: competencyFrameworkId 82 } 83 }]); 84 promises[0].done(function(result) { 85 competencies = {}; 86 var i = 0; 87 for (i = 0; i < result.length; i++) { 88 competencies[result[i].id] = result[i]; 89 } 90 91 var children = []; 92 var competency = false; 93 for (i = 0; i < result.length; i++) { 94 competency = result[i]; 95 if (parseInt(competency.parentid, 10) === 0) { 96 children.push(competency); 97 addChildren(competency, result); 98 } 99 } 100 var context = { 101 shortname: competencyFrameworkShortName, 102 canmanage: competencyFramworkCanManage, 103 competencies: children 104 }; 105 templates.render('tool_lp/competencies_tree_root', context).done(function(html, js) { 106 templates.replaceNodeContents($(treeSelector), $(html).html(), js); 107 var tree = new Ariatree(treeSelector, false); 108 109 if (currentNodeId) { 110 var node = $(treeSelector).find('[data-id=' + currentNodeId + ']'); 111 if (node.length) { 112 tree.selectItem(node); 113 tree.updateFocus(node); 114 } 115 } 116 deferred.resolve(competencies); 117 }).fail(deferred.reject); 118 }).fail(deferred.reject); 119 }); 120 121 return deferred.promise(); 122 }; 123 124 /** 125 * Whenever the current item in the tree is changed - remember the "id". 126 * @param {Event} evt 127 * @param {Object} params The parameters for the event (This is the selected node). 128 */ 129 var rememberCurrent = function(evt, params) { 130 var node = params.selected; 131 currentNodeId = node.attr('data-id'); 132 }; 133 134 return /** @alias module:tool_lp/competencytree */ { 135 // Public variables and functions. 136 /** 137 * Initialise the tree. 138 * 139 * @param {Number} id The competency id. 140 * @param {String} shortname The framework shortname 141 * @param {String} search The current search string 142 * @param {String} selector The selector for the tree div 143 * @param {Boolean} canmanage Can manage the competencies 144 */ 145 init: function(id, shortname, search, selector, canmanage) { 146 competencyFrameworkId = id; 147 competencyFrameworkShortName = shortname; 148 competencyFramworkCanManage = canmanage; 149 treeSelector = selector; 150 loadCompetencies(search).fail(notification.exception); 151 152 this.on('selectionchanged', rememberCurrent); 153 }, 154 155 /** 156 * Add an event handler for custom events emitted by the tree. 157 * 158 * @param {String} eventname The name of the event - only "selectionchanged" for now 159 * @param {Function} handler The handler for the event. 160 */ 161 on: function(eventname, handler) { 162 // We can't use the tree on function directly 163 // because the tree gets rebuilt whenever the search string changes, 164 // instead we attach the listner to the root node of the tree which never 165 // gets destroyed (same as "on()" code in the tree.js). 166 $(treeSelector).on(eventname, handler); 167 }, 168 169 /** 170 * Get the children of a competency. 171 * 172 * @param {Number} id The competency ID. 173 * @return {Array} 174 * @method getChildren 175 */ 176 getChildren: function(id) { 177 var children = []; 178 $.each(competencies, function(index, competency) { 179 if (competency.parentid == id) { 180 children.push(competency); 181 } 182 }); 183 return children; 184 }, 185 186 /** 187 * Get the competency framework id this model was initiliased with. 188 * 189 * @return {Number} 190 */ 191 getCompetencyFrameworkId: function() { 192 return competencyFrameworkId; 193 }, 194 195 /** 196 * Get a competency by id 197 * 198 * @param {Number} id The competency id 199 * @return {Object} 200 */ 201 getCompetency: function(id) { 202 return competencies[id]; 203 }, 204 205 /** 206 * Get the competency level. 207 * 208 * @param {Number} id The competency ID. 209 * @return {Number} 210 */ 211 getCompetencyLevel: function(id) { 212 var competency = this.getCompetency(id), 213 level = competency.path.replace(/^\/|\/$/g, '').split('/').length; 214 return level; 215 }, 216 217 /** 218 * Whether a competency has children. 219 * 220 * @param {Number} id The competency ID. 221 * @return {Boolean} 222 * @method hasChildren 223 */ 224 hasChildren: function(id) { 225 return this.getChildren(id).length > 0; 226 }, 227 228 /** 229 * Does the competency have a rule? 230 * 231 * @param {Number} id The competency ID. 232 * @return {Boolean} 233 */ 234 hasRule: function(id) { 235 var comp = this.getCompetency(id); 236 if (comp) { 237 return comp.ruleoutcome != CompOutcomes.OUTCOME_NONE 238 && comp.ruletype; 239 } 240 return false; 241 }, 242 243 /** 244 * Reload all the page competencies framework competencies. 245 * @method reloadCompetencies 246 * @return {Promise} 247 */ 248 reloadCompetencies: function() { 249 return loadCompetencies('').fail(notification.exception); 250 }, 251 252 /** 253 * Get all competencies for this framework. 254 * 255 * @return {Object[]} 256 */ 257 listCompetencies: function() { 258 return competencies; 259 }, 260 261 }; 262 });
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |