[ 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 * Competency picker from user plans. 18 * 19 * To handle 'save' events use: picker.on('save'). 20 * 21 * This will receive a object with either a single 'competencyId', or an array in 'competencyIds' 22 * depending on the value of multiSelect. 23 * 24 * @package tool_lp 25 * @copyright 2015 Frédéric Massart - FMCorz.net 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 29 define(['jquery', 30 'core/notification', 31 'core/ajax', 32 'core/templates', 33 'core/str', 34 'tool_lp/tree', 35 'tool_lp/competencypicker' 36 ], 37 function($, Notification, Ajax, Templates, Str, Tree, PickerBase) { 38 39 /** 40 * Competency picker in plan class. 41 * 42 * @param {Number} userId 43 * @param {Number|false} singlePlan The ID of the plan when limited to one. 44 * @param {Boolean} multiSelect Support multi-select in the tree. 45 */ 46 var Picker = function(userId, singlePlan, multiSelect) { 47 PickerBase.prototype.constructor.apply(this, [1, false, 'self', multiSelect]); 48 this._userId = userId; 49 this._plans = []; 50 51 if (singlePlan) { 52 this._planId = singlePlan; 53 this._singlePlan = true; 54 } 55 }; 56 Picker.prototype = Object.create(PickerBase.prototype); 57 58 /** @type {Array} The list of plans fetched. */ 59 Picker.prototype._plans = null; 60 /** @type {Number} The current plan ID. */ 61 Picker.prototype._planId = null; 62 /** @type {Boolean} Whether we can browse plans or not. */ 63 Picker.prototype._singlePlan = false; 64 /** @type {Number} The user the plans belongs to. */ 65 Picker.prototype._userId = null; 66 67 /** 68 * Hook to executed after the view is rendered. 69 * 70 * @method _afterRender 71 */ 72 Picker.prototype._afterRender = function() { 73 var self = this; 74 PickerBase.prototype._afterRender.apply(self, arguments); 75 76 // Add listener for framework change. 77 if (!self._singlePlan) { 78 self._find('[data-action="chooseplan"]').change(function(e) { 79 self._planId = $(e.target).val(); 80 self._loadCompetencies().then(self._refresh.bind(self)); 81 }); 82 } 83 }; 84 85 /** 86 * Fetch the competencies. 87 * 88 * @param {Number} planId The planId. 89 * @param {String} searchText Limit the competencies to those matching the text. 90 * @method _fetchCompetencies 91 * @return {Promise} The promise object. 92 */ 93 Picker.prototype._fetchCompetencies = function(planId, searchText) { 94 var self = this; 95 96 return Ajax.call([ 97 {methodname: 'core_competency_list_plan_competencies', args: { 98 id: planId 99 }} 100 ])[0].done(function(competencies) { 101 102 // Expand the list of competencies into a fake tree. 103 var i, comp; 104 var tree = []; 105 for (i = 0; i < competencies.length; i++) { 106 comp = competencies[i].competency; 107 if (comp.shortname.toLowerCase().indexOf(searchText.toLowerCase()) < 0) { 108 continue; 109 } 110 comp.children = []; 111 comp.haschildren = 0; 112 tree.push(comp); 113 } 114 115 self._competencies = tree; 116 117 }).fail(Notification.exception); 118 }; 119 120 /** 121 * Convenience method to get a plan object. 122 * 123 * @param {Number} id The plan ID. 124 * @return {Object|undefined} The plan. 125 * @method _getPlan 126 */ 127 Picker.prototype._getPlan = function(id) { 128 var plan; 129 $.each(this._plans, function(i, f) { 130 if (f.id == id) { 131 plan = f; 132 return; 133 } 134 }); 135 return plan; 136 }; 137 138 /** 139 * Load the competencies. 140 * 141 * @method _loadCompetencies 142 * @return {Promise} 143 */ 144 Picker.prototype._loadCompetencies = function() { 145 return this._fetchCompetencies(this._planId, this._searchText); 146 }; 147 148 /** 149 * Load the plans. 150 * 151 * @method _loadPlans 152 * @return {Promise} 153 */ 154 Picker.prototype._loadPlans = function() { 155 var promise, 156 self = this; 157 158 // Quit early because we already have the data. 159 if (self._plans.length > 0) { 160 return $.when(); 161 } 162 163 if (self._singlePlan) { 164 promise = Ajax.call([ 165 {methodname: 'core_competency_read_plan', args: { 166 id: this._planId 167 }} 168 ])[0].then(function(plan) { 169 return [plan]; 170 }); 171 } else { 172 promise = Ajax.call([ 173 {methodname: 'core_competency_list_user_plans', args: { 174 userid: self._userId 175 }} 176 ])[0]; 177 } 178 179 return promise.done(function(plans) { 180 self._plans = plans; 181 }).fail(Notification.exception); 182 }; 183 184 /** 185 * Hook to executed before render. 186 * 187 * @method _preRender 188 * @return {Promise} 189 */ 190 Picker.prototype._preRender = function() { 191 var self = this; 192 return self._loadPlans().then(function() { 193 if (!self._planId && self._plans.length > 0) { 194 self._planId = self._plans[0].id; 195 } 196 197 // We could not set a framework ID, that probably means there are no frameworks accessible. 198 if (!self._planId) { 199 self._plans = []; 200 return $.when(); 201 } 202 203 return self._loadCompetencies(); 204 }); 205 }; 206 207 /** 208 * Render the dialogue. 209 * 210 * @method _render 211 * @return {Promise} 212 */ 213 Picker.prototype._render = function() { 214 var self = this; 215 return self._preRender().then(function() { 216 217 if (!self._singlePlan) { 218 $.each(self._plans, function(i, plan) { 219 if (plan.id == self._planId) { 220 plan.selected = true; 221 } else { 222 plan.selected = false; 223 } 224 }); 225 } 226 227 var context = { 228 competencies: self._competencies, 229 plan: self._getPlan(self._planId), 230 plans: self._plans, 231 search: self._searchText, 232 singlePlan: self._singlePlan, 233 }; 234 235 return Templates.render('tool_lp/competency_picker_user_plans', context); 236 }); 237 }; 238 239 return /** @alias module:tool_lp/competencypicker_user_plans */ Picker; 240 241 });
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 |