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