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 |
* Display Competency in dialogue box.
|
|
|
18 |
*
|
|
|
19 |
* @module tool_lp/competencydialogue
|
|
|
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',
|
|
|
24 |
'core/notification',
|
|
|
25 |
'core/ajax',
|
|
|
26 |
'core/templates',
|
|
|
27 |
'core/str',
|
|
|
28 |
'tool_lp/dialogue'],
|
|
|
29 |
function($, notification, ajax, templates, str, Dialogue) {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* The main instance we'll be working with.
|
|
|
33 |
*
|
|
|
34 |
* @type {Competencydialogue}
|
|
|
35 |
*/
|
|
|
36 |
var instance;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Constructor for CompetencyDialogue.
|
|
|
40 |
*/
|
|
|
41 |
var Competencydialogue = function() {
|
|
|
42 |
// Intentionally left empty.
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Log the competency viewed event.
|
|
|
47 |
*
|
|
|
48 |
* @param {Number} competencyId The competency ID.
|
|
|
49 |
* @method triggerCompetencyViewedEvent
|
|
|
50 |
*/
|
|
|
51 |
Competencydialogue.prototype.triggerCompetencyViewedEvent = function(competencyId) {
|
|
|
52 |
ajax.call([{
|
|
|
53 |
methodname: 'core_competency_competency_viewed',
|
|
|
54 |
args: {id: competencyId}
|
|
|
55 |
}]);
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Display a dialogue box by competencyid.
|
|
|
60 |
*
|
|
|
61 |
* @param {Number} competencyid The competency ID.
|
|
|
62 |
* @param {Object} options The options.
|
|
|
63 |
* @method showDialogue
|
|
|
64 |
*/
|
|
|
65 |
Competencydialogue.prototype.showDialogue = function(competencyid, options) {
|
|
|
66 |
|
|
|
67 |
var datapromise = this.getCompetencyDataPromise(competencyid, options);
|
|
|
68 |
var localthis = this;
|
|
|
69 |
datapromise.done(function(data) {
|
|
|
70 |
// Inner Html in the dialogue content.
|
|
|
71 |
templates.render('tool_lp/competency_summary', data)
|
|
|
72 |
.done(function(html) {
|
|
|
73 |
// Log competency viewed event.
|
|
|
74 |
localthis.triggerCompetencyViewedEvent(competencyid);
|
|
|
75 |
|
|
|
76 |
// Show the dialogue.
|
|
|
77 |
new Dialogue(
|
|
|
78 |
data.competency.shortname,
|
|
|
79 |
html
|
|
|
80 |
);
|
|
|
81 |
}).fail(notification.exception);
|
|
|
82 |
}).fail(notification.exception);
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Display a dialogue box from data.
|
|
|
87 |
*
|
|
|
88 |
* @param {Object} dataSource data to be used to display dialogue box
|
|
|
89 |
* @method showDialogueFromData
|
|
|
90 |
*/
|
|
|
91 |
Competencydialogue.prototype.showDialogueFromData = function(dataSource) {
|
|
|
92 |
|
|
|
93 |
var localthis = this;
|
|
|
94 |
// Inner Html in the dialogue content.
|
|
|
95 |
templates.render('tool_lp/competency_summary', dataSource)
|
|
|
96 |
.done(function(html) {
|
|
|
97 |
// Log competency viewed event.
|
|
|
98 |
localthis.triggerCompetencyViewedEvent(dataSource.id);
|
|
|
99 |
|
|
|
100 |
// Show the dialogue.
|
|
|
101 |
new Dialogue(
|
|
|
102 |
dataSource.shortname,
|
|
|
103 |
html,
|
|
|
104 |
localthis.enhanceDialogue
|
|
|
105 |
);
|
|
|
106 |
}).fail(notification.exception);
|
|
|
107 |
};
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* The action on the click event.
|
|
|
111 |
*
|
|
|
112 |
* @param {Event} e event click
|
|
|
113 |
* @method clickEventHandler
|
|
|
114 |
*/
|
|
|
115 |
Competencydialogue.prototype.clickEventHandler = function(e) {
|
|
|
116 |
|
|
|
117 |
var compdialogue = e.data.compdialogue;
|
|
|
118 |
var currentTarget = $(e.currentTarget);
|
|
|
119 |
var competencyid = currentTarget.data('id');
|
|
|
120 |
var includerelated = !(currentTarget.data('excluderelated'));
|
|
|
121 |
var includecourses = currentTarget.data('includecourses');
|
|
|
122 |
|
|
|
123 |
// Show the dialogue box.
|
|
|
124 |
compdialogue.showDialogue(competencyid, {
|
|
|
125 |
includerelated: includerelated,
|
|
|
126 |
includecourses: includecourses
|
|
|
127 |
});
|
|
|
128 |
e.preventDefault();
|
|
|
129 |
};
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Get a promise on data competency.
|
|
|
133 |
*
|
|
|
134 |
* @param {Number} competencyid
|
|
|
135 |
* @param {Object} options
|
|
|
136 |
* @return {Promise} return promise on data request
|
|
|
137 |
* @method getCompetencyDataPromise
|
|
|
138 |
*/
|
|
|
139 |
Competencydialogue.prototype.getCompetencyDataPromise = function(competencyid, options) {
|
|
|
140 |
|
|
|
141 |
var requests = ajax.call([
|
|
|
142 |
{methodname: 'tool_lp_data_for_competency_summary',
|
|
|
143 |
args: {competencyid: competencyid,
|
|
|
144 |
includerelated: options.includerelated || false,
|
|
|
145 |
includecourses: options.includecourses || false
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
]);
|
|
|
149 |
|
|
|
150 |
return requests[0].then(function(context) {
|
|
|
151 |
return context;
|
|
|
152 |
}).fail(notification.exception);
|
|
|
153 |
};
|
|
|
154 |
|
|
|
155 |
return /** @alias module:tool_lp/competencydialogue */ {
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Initialise the competency dialogue module.
|
|
|
159 |
*
|
|
|
160 |
* Only the first call matters.
|
|
|
161 |
*/
|
|
|
162 |
init: function() {
|
|
|
163 |
if (typeof instance !== 'undefined') {
|
|
|
164 |
return;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
// Instantiate the one instance and delegate event on the body.
|
|
|
168 |
instance = new Competencydialogue();
|
|
|
169 |
$('body').delegate('[data-action="competency-dialogue"]', 'click', {compdialogue: instance},
|
|
|
170 |
instance.clickEventHandler.bind(instance));
|
|
|
171 |
}
|
|
|
172 |
};
|
|
|
173 |
});
|