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 |
* Handle selecting parent competency in competency form.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_lp/parentcompetency_form
|
|
|
20 |
* @copyright 2015 Issam Taboubi <issam.taboubi@umontreal.ca>
|
|
|
21 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
22 |
*/
|
|
|
23 |
define(['jquery', 'core/ajax', 'core/str', 'tool_lp/competencypicker', 'core/templates', 'core/notification'],
|
|
|
24 |
function($, ajax, Str, Picker, Templates, Notification) {
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Parent Competency Form object.
|
|
|
28 |
* @param {String} buttonSelector The parent competency button selector.
|
|
|
29 |
* @param {String} inputHiddenSelector The hidden input field selector.
|
|
|
30 |
* @param {String} staticElementSelector The static element displaying the parent competency.
|
|
|
31 |
* @param {Number} frameworkId The competency framework ID.
|
|
|
32 |
* @param {Number} pageContextId The page context ID.
|
|
|
33 |
*/
|
|
|
34 |
var ParentCompetencyForm = function(buttonSelector,
|
|
|
35 |
inputHiddenSelector,
|
|
|
36 |
staticElementSelector,
|
|
|
37 |
frameworkId,
|
|
|
38 |
pageContextId) {
|
|
|
39 |
this.buttonSelector = buttonSelector;
|
|
|
40 |
this.inputHiddenSelector = inputHiddenSelector;
|
|
|
41 |
this.staticElementSelector = staticElementSelector;
|
|
|
42 |
this.frameworkId = frameworkId;
|
|
|
43 |
this.pageContextId = pageContextId;
|
|
|
44 |
|
|
|
45 |
// Register the events.
|
|
|
46 |
this.registerEvents();
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
/** @var {String} The parent competency button selector. */
|
|
|
50 |
ParentCompetencyForm.prototype.buttonSelector = null;
|
|
|
51 |
/** @var {String} The hidden input field selector. */
|
|
|
52 |
ParentCompetencyForm.prototype.inputHiddenSelector = null;
|
|
|
53 |
/** @var {String} The static element displaying the parent competency. */
|
|
|
54 |
ParentCompetencyForm.prototype.staticElementSelector = null;
|
|
|
55 |
/** @var {Number} The competency framework ID. */
|
|
|
56 |
ParentCompetencyForm.prototype.frameworkId = null;
|
|
|
57 |
/** @var {Number} The page context ID. */
|
|
|
58 |
ParentCompetencyForm.prototype.pageContextId = null;
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Set the parent competency in the competency form.
|
|
|
62 |
*
|
|
|
63 |
* @param {Object} data Data containing selected competency.
|
|
|
64 |
* @method setParent
|
|
|
65 |
*/
|
|
|
66 |
ParentCompetencyForm.prototype.setParent = function(data) {
|
|
|
67 |
var self = this;
|
|
|
68 |
|
|
|
69 |
if (data.competencyId !== 0) {
|
|
|
70 |
ajax.call([
|
|
|
71 |
{methodname: 'core_competency_read_competency', args: {
|
|
|
72 |
id: data.competencyId
|
|
|
73 |
}}
|
|
|
74 |
])[0].done(function(competency) {
|
|
|
75 |
$(self.staticElementSelector).html(competency.shortname);
|
|
|
76 |
$(self.inputHiddenSelector).val(competency.id);
|
|
|
77 |
}).fail(Notification.exception);
|
|
|
78 |
} else {
|
|
|
79 |
// Root of competency framework selected.
|
|
|
80 |
Str.get_string('competencyframeworkroot', 'tool_lp').then(function(rootframework) {
|
|
|
81 |
$(self.staticElementSelector).html(rootframework);
|
|
|
82 |
$(self.inputHiddenSelector).val(data.competencyId);
|
|
|
83 |
return;
|
|
|
84 |
}).fail(Notification.exception);
|
|
|
85 |
}
|
|
|
86 |
};
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Register the events of parent competency button click.
|
|
|
90 |
*
|
|
|
91 |
* @method registerEvents
|
|
|
92 |
*/
|
|
|
93 |
ParentCompetencyForm.prototype.registerEvents = function() {
|
|
|
94 |
var self = this;
|
|
|
95 |
|
|
|
96 |
// Event on edit parent button.
|
|
|
97 |
$(self.buttonSelector).on('click', function(e) {
|
|
|
98 |
e.preventDefault();
|
|
|
99 |
|
|
|
100 |
var picker = new Picker(self.pageContextId, self.frameworkId, 'self', false);
|
|
|
101 |
|
|
|
102 |
// Override the render method to make framework selectable.
|
|
|
103 |
picker._render = function() {
|
|
|
104 |
var self = this;
|
|
|
105 |
return self._preRender().then(function() {
|
|
|
106 |
var context = {
|
|
|
107 |
competencies: self._competencies,
|
|
|
108 |
framework: self._getFramework(self._frameworkId),
|
|
|
109 |
frameworks: self._frameworks,
|
|
|
110 |
search: self._searchText,
|
|
|
111 |
singleFramework: self._singleFramework,
|
|
|
112 |
};
|
|
|
113 |
|
|
|
114 |
return Templates.render('tool_lp/competency_picker_competencyform', context);
|
|
|
115 |
});
|
|
|
116 |
};
|
|
|
117 |
|
|
|
118 |
// On selected competency.
|
|
|
119 |
picker.on('save', function(e, data) {
|
|
|
120 |
self.setParent(data);
|
|
|
121 |
});
|
|
|
122 |
|
|
|
123 |
picker.display();
|
|
|
124 |
});
|
|
|
125 |
};
|
|
|
126 |
|
|
|
127 |
return {
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Main initialisation.
|
|
|
131 |
* @param {String} buttonSelector The parent competency button selector.
|
|
|
132 |
* @param {String} inputSelector The hidden input field selector.
|
|
|
133 |
* @param {String} staticElementSelector The static element displaying the parent competency.
|
|
|
134 |
* @param {Number} frameworkId The competency framework ID.
|
|
|
135 |
* @param {Number} pageContextId The page context ID.
|
|
|
136 |
* @method init
|
|
|
137 |
*/
|
|
|
138 |
init: function(buttonSelector,
|
|
|
139 |
inputSelector,
|
|
|
140 |
staticElementSelector,
|
|
|
141 |
frameworkId,
|
|
|
142 |
pageContextId) {
|
|
|
143 |
// Create instance.
|
|
|
144 |
new ParentCompetencyForm(buttonSelector,
|
|
|
145 |
inputSelector,
|
|
|
146 |
staticElementSelector,
|
|
|
147 |
frameworkId,
|
|
|
148 |
pageContextId);
|
|
|
149 |
}
|
|
|
150 |
};
|
|
|
151 |
});
|