/
testcontroller.js
290 lines (236 loc) · 10.4 KB
/
testcontroller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* jshint ignore:start */
define(['jquery','jqueryui', 'core/log','filter_poodll/utils_amd','filter_poodll/poodll_mediarecorder'], function($, jqui, log, utils,pmr) {
"use strict"; // jshint ;_;
log.debug('Test controller: initialising');
return {
currentitemid: 0,
currentitem: 0,
itemcount: null,
cmid: null,
testdata: null,
holderid: null,
recorderid: null,
playerid: null,
startbuttonid: null,
sorryboxid: null,
controls: null,
fbrecorder: null,
//for making multiple instances
clone: function(){
return $.extend(true,{},this);
},
//pass in config, the jquery video/audio object, and a function to be called when conversion has finshed
init: function(props){
var dd = this.clone();
//pick up opts from html
var theid='#amdopts_' + props.widgetid;
var configcontrol = $(theid).get(0);
if(configcontrol){
dd.testdata = JSON.parse(configcontrol.value);
$(theid).remove();
}else{
//if there is no config we might as well give up
log.debug('FB Test Controller: No config found on page. Giving up.');
return;
}
dd.cmid = props.cmid;
dd.holderid = props.widgetid + '_holder';
dd.recorderid = props.widgetid + '_recorder';
dd.playerid = props.widgetid + '_player';
dd.startbuttonid = props.widgetid + '_startbutton';
dd.sorryboxid = props.widgetid + '_sorrybox';
dd.itemcount = dd.testdata.length;
//if the browser doesn't support html5 recording.
//then warn and do not go any further
if(!dd.is_browser_ok()){
$('#' + dd.sorryboxid).show();
return;
}
dd.setup_recorder();
dd.process_html();
dd.register_events();
},
start_item: function(){
var dd = this;
dd.currentitem = dd.currentitem +1;
if(dd.currentitem > dd.itemcount){
//do ending
}
//get item data
var item = dd.testdata[dd.currentitem-1];
//to make sure we send the correct evaluation
dd.currentitemid = item.itemid;
//set controls with item data
dd.controls.progress.text(item.itemprogress);
dd.controls.header.text(item.itemheader);
dd.controls.othertext.html(item.itemtext);
//set recorder with item data
var ip = dd.fbrecorder.fetch_instanceprops();
ip.config.timelimit=item.timetarget;
ip.timer.initseconds=parseInt(item.timetarget);
ip.timer.seconds=parseInt(item.timetarget);
ip.config.resource = item.resourceurl;
ip.config.resource2 = item.modelurl;
var controlbar = ip.controlbar;
//commence playback
controlbar.resourcebutton.click();
},
process_html: function(){
var controls ={
//the feedback dialog
holder: $('#' + this.holderid),
the_dialog: $('#' + this.holderid + ' .mod_fluencybuilder_dialogcontentbox'),
me_play: $('#' + this.holderid + ' .mod_fluencybuilder_me_play'),
me_ok: $('#' + this.holderid + ' .mod_fluencybuilder_me_ok'),
me_ng: $('#' + this.holderid + ' .mod_fluencybuilder_me_ng'),
player: $('#' + this.playerid),
startbutton: $('#' + this.startbuttonid),
//the text items
progress: $('#' + this.holderid + ' .mod_fluencybuilder_itemprogress'),
header: $('#' + this.holderid + ' .mod_fluencybuilder_itemheader'),
othertext: $('#' + this.holderid + ' .mod_fluencybuilder_itemothertext'),
};
this.controls = controls;
},
warmup_player: function(){
var checkplayer = this.controls.player[0];
try{
checkplayer.play();
checkplayer.pause();
}catch(e){
//d nothing
}
},
is_browser_ok: function(){
return (navigator && navigator.mediaDevices
&& navigator.mediaDevices.getUserMedia);
},
setup_recorder: function(){
var params=[];
var dingurl = M.cfg.wwwroot + '/filter/poodll/ding.mp3';
params['widgetid'] = this.recorderid;
params['media_timeinterval'] = 2000;
params['media_audiomimetype'] = 'audio/webm';//or audio/wav
params['media_videorecordertype'] = 'auto';//or mediarec or webp
params['media_videocapturewidth'] = 320;
params['media_videocaptureheight'] = 240;
params['mediatype'] = 'audio';
params['media_skin'] = 'fluencybuilder';
params['media_skin_style'] = '';
params['timelimit'] =5;
params['hideupload'] =true;
params['resource']=dingurl;//just for now we use ding ..need a blank
params['resource2']=dingurl;//just for now we use ding ..need a blank
var fbrecorder = pmr.embed('#' + this.recorderid,params);
this.fbrecorder =fbrecorder;
},
register_events: function() {
var dd = this;
//ip is the internal props of the fb recorder
var ip = dd.fbrecorder.fetch_instanceprops();
//these are the correct and incorrect buttons
dd.controls.me_ok.click(function () {
dd.send_evaluation('ok');
});
dd.controls.me_ng.click(function () {
dd.send_evaluation('ng');
});
//this is the test start button
//we change display to enabled after we have attached our click event
dd.controls.startbutton.click(function(){
$(this).hide();
//we need to warm up the player
dd.warmup_player();
//debugger;
dd.controls.holder.show();
dd.start_item();
}),
dd.controls.startbutton.removeClass('mod_fluencybuilder_startbutton_disabled'),
//this is the playback recording check player
dd.controls.me_play.click(function () {
var checkplayer = dd.controls.player[0];
pmr.do_play_audio(ip,checkplayer);
});
//set the dialog popup function to show after the audio model player has ended
var endedfunction = function () {
//prepare next button
var nexttext = M.util.get_string('recui_next', 'mod_fluencybuilder');
var buttons = {};
buttons[nexttext] = function () {
if (dd.currentitem == dd.itemcount) {
window.location.replace(M.cfg.wwwroot + '/mod/fluencybuilder/view.php?id=' + dd.cmid);
$(this).dialog("close");
}else{
$(this).dialog("close");
//do next item
dd.start_item();
}
}
//prepare and show dialog
dd.controls.the_dialog.dialog({
dialogClass: 'mod_fluencybuilder_no-close',
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: buttons
});
};
//when the model player finishes show our dialog
//we need to do some dumb setInterval here because of javascript load race condtions YUKKY
var model_player = ip.controlbar.modelplayer;
if (model_player.length < 1) {
var interval_handle = setInterval(function(){
var model_player = $('#' + dd.holderid + ' .poodll_modelplayer_fluencybuilder');
if(model_player.length >0){
clearInterval(interval_handle);
model_player.on('ended', endedfunction);
}
}, 500);
} else {
model_player.on('ended', endedfunction);
}
},
send_evaluation: function(evaluation){
//set up our ajax request
var xhr = new XMLHttpRequest();
var that = this;
//set up our handler for the response
xhr.onreadystatechange = function(e){
if(this.readyState===4){
if(xhr.status==200){
log.debug('ok we got an attempt update response');
//get a yes or forgetit or tryagain
var payload = xhr.responseText;
var payloadobject = JSON.parse(payload);
if(payloadobject){
switch(payloadobject.message) {
case 'noted':
log.debug('attempted item evaluation accepted');
if(payloadobject.attemptid != window.attemptid){
window.attemptid=payloadobject.attemptid;
}
break;
case 'problem':
default:
log.debug('attempted item evaluation failure');
}
}
}else{
log.debug('Not 200 response:' + xhr.status);
}
}
};
//use already created attemptid if we have one
var attemptid=0;
if(window.attemptid) {
attemptid = window.attemptid;
}
var params = "itemid=" + that.currentitemid + "&cmid=" + that.cmid + "&eval=" + evaluation + '&attemptid=' + attemptid;
xhr.open("POST",M.cfg.wwwroot + '/mod/fluencybuilder/jsonresults.php', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.send(params);
}
};//end of returned object
});//total end