adding some strings
[moodle-linuxchix.git] / mod / hotpot / template / v6 / jmatch6.js_
blob1922189d8e2bc3218f4d0337d7a7652a357e882a
2 [inclScorm1.2]
3 //JMATCH-SPECIFIC SCORM-RELATED JAVASCRIPT CODE
5 function SetScormScore(){
6 //Reports the current score and any other information back to the LMS
7         if (API != null){
8                 API.LMSSetValue('cmi.core.score.raw', Score);
9                 
10 //Now send a detailed reports on the item
11                 var ItemLabel = 'Matching';
12                 API.LMSSetValue('cmi.objectives.0.id', 'obj'+ItemLabel);
13                 API.LMSSetValue('cmi.interactions.0.id', 'int'+ItemLabel);      
14                 API.LMSSetValue('cmi.objectives.0.status', API.LMSGetValue('cmi.core.lesson_status'));  
15                 API.LMSSetValue('cmi.objectives.0.score.min', '0');
16                 API.LMSSetValue('cmi.objectives.0.score.max', '100');
17                 API.LMSSetValue('cmi.objectives.0.score.raw', Score);
18 //We can only use the performance type, because we're storing multiple responses of various types.
19                 API.LMSSetValue('cmi.interactions.0.type', 'performance');
20                 
21                 var AnswersTried = '';
22                 for (var i=0; i<Status[0][3].length; i++){
23                         if (i>0){AnswersTried += ' | ';}
24                         for (var j=0; j<Status.length; j++){
25                                 if (j>0){AnswersTried += ',';}
26                                 AnswersTried += j + '.' + Status[j][3][i];
27                         }
28                 }
29                 API.LMSSetValue('cmi.interactions.0.student_response', AnswersTried);
30                 API.LMSCommit('');
31         }
33 [/inclScorm1.2]
35 //JMATCH CORE JAVASCRIPT CODE
37 var CorrectIndicator = '[strCorrectIndicator]';
38 var IncorrectIndicator = '[strIncorrectIndicator]';
39 var YourScoreIs = '[strYourScoreIs]';
40 var CorrectResponse = '[strGuessCorrect]';
41 var IncorrectResponse = '[strGuessIncorrect]';
42 var TotalUnfixedLeftItems = 0;
43 var TotCorrectChoices = 0;
44 var Penalties = 0;
45 var Finished = false;
46 var TimeOver = false;
48 var Score = 0;
49 var Locked = false;
50 var ShuffleQs = [boolShuffleQs];
51 var QsToShow = [QsToShow];
54 function StartUp(){
55         RemoveBottomNavBarForIE();
57 [inclScorm1.2]
58         ScormStartUp();
59 [/inclScorm1.2]
60         
61 [inclSendResults]
62         GetUserName();
63 [/inclSendResults]
65 [inclPreloadImages]
66         PreloadImages([PreloadImageList]);
67 [/inclPreloadImages]
69         SetUpItems(ShuffleQs,QsToShow);
71         TotalUnfixedLeftItems = document.getElementById('MatchDiv').getElementsByTagName('select').length;
73 //Create arrays
74         CreateStatusArrays();
76 [inclTimer]
77         StartTimer();
78 [/inclTimer]
81 Status = new Array();
84 function CreateStatusArrays(){
85         var Selects = document.getElementById('Questions').getElementsByTagName('select');
86         for (var x=0; x<Selects.length; x++){
87                 Status[x] = new Array();
88                 Status[x][0] = 0; // Item not matched correctly yet
89                 Status[x][1] = 0; //Tries at this item so far
90                 Status[x][2] = Selects[x].id; //Store a ref to the original drop-down
91                 Status[x][3] = new Array(); //Sequence of guesses for this item
92         }
95 function GetKeyFromSelectContainer(Container){
96         var Result = -1;
97         if (Container.getElementsByTagName('select').length > 0){
98                 var Select = Container.getElementsByTagName('select')[0];
99                 if (Select != null){
100                         Result = parseInt(Select.id.substring(1, Select.id.length));
101                 }
102         }
103         return Result;
106 function GetKeyFromSelect(Select){
107         var Result = -1;
108         if (Select != null){
109                 Result = parseInt(Select.id.substring(1, Select.id.length));
110         }
111         return Result;
114 var OriginalKeys = new Array();
115 var ReducedKeys = new Array();
117 function GetUniqueKeys(Container, TargetArray){
118         TargetArray.length = 0;
119         var x = -1;
120         var SList = Container.getElementsByTagName('select');
121         if (SList.length > 0){
122                 for (var i=0; i<SList.length; i++){
123                         x = GetKeyFromSelect(SList[i]);
124                         if (TargetArray.indexOf(x) < 0){
125                                 TargetArray.push(x);
126                         }
127                 }
128         }
131 function SetUpItems(ShuffleQs, ReduceTo){
132         var QList = new Array();
133         var i, j, k, Selects, Options;
135 //Remove all the table rows and put them in an array for processing
136         var Qs = document.getElementById('Questions');
137         
138 //First, get a list of keys
139         GetUniqueKeys(Qs, OriginalKeys);
141 //Remove the table rows to an array
142         while (Qs.getElementsByTagName('tr').length > 0){
143                 QList.push(Qs.removeChild(Qs.getElementsByTagName('tr')[0]));
144         }
146         var Reducing = (QList.length > ReduceTo);
147         
148 //If required, select random rows to delete
149         if (Reducing == true){
150                 var DumpItem = 0;
151                 while (ReduceTo < QList.length){
152         
153 //Get a number to delete from the array
154                         DumpItem = Math.floor(QList.length*Math.random());
155                         for (i=DumpItem; i<(QList.length-1); i++){
156                                 QList[i] = QList[i+1];
157                         }
158                         QList.length = QList.length-1;
159                 }
160         }
161 //Shuffle the rows if necessary
162         if (ShuffleQs == true){
163                 QList = Shuffle(QList);
164         }
166         TotalUnfixedLeftItems = QList.length;
167         
168 //Write the rows back to the table body
169         for (i=0; i<QList.length; i++){
170                 Qs.appendChild(QList[i]);
171         }
172         
173 //Now we need to remove any drop-down options that no longer have associated select items
174         if (Reducing == true){
175                 GetUniqueKeys(Qs, ReducedKeys);
176                 
177                 Selects = Qs.getElementsByTagName('select');
178                 for (i=0; i<Selects.length; i++){
179                         Options = Selects[i].getElementsByTagName('option');
180                         for (j=Options.length-1; j>=0; j--){
181                                 if (OptionRequired(Options[j].value) == false){
182                                         Selects[i].removeChild(Options[j]);
183                                 }
184                         }
185                 }
186         }
189 function OptionRequired(Key){
190         if (ReducedKeys.indexOf(Key) > -1){
191                 return true;
192         }
193         else{
194                 if (OriginalKeys.indexOf(Key) > -1){
195                         return false;
196                 }
197                 else{
198                         return true;
199                 }
200         }
203 function CheckAnswers(){
204         if (Locked == true){return;}
205         var Select = null;
206         var Key = -1;
207         var Parent = null;
208         var Answer = null;
209         var AnsText = '';
210         var AllDone = true;
211         TotCorrectChoices = 0;
212         
213 //for each item not fixed or a distractor
214         for (var i=0; i<Status.length; i++){
215         
216 //if it hasn't been answered correctly yet
217                 if (Status[i][0] < 1){
218                 
219 //Add one to the number of tries for this item
220                         Status[i][1]++;
221                         
222 //Get a pointer to the drop-down
223                         Select = document.getElementById(Status[i][2]);
224                         Key = GetKeyFromSelect(Select);
225 //Save the answer given
226                         Status[i][3].push(Select.options[Select.selectedIndex].value);
227                         
228 //Check the answer
229                         if (Select.options[Select.selectedIndex].value == Key){
230                                         Status[i][0] = 1;
231                                         AnsText = Select.options[Select.selectedIndex].innerHTML;
232                                         Parent = Select.parentNode;
233                                         Parent.removeChild(Select);
234                                         Parent.innerHTML = AnsText;
235                                         Parent.nextSibling.innerHTML = CorrectIndicator;
236                         } 
237                         else{
238                                 AllDone = false;
239                                 Parent = Select.parentNode;
240                                 Parent.nextSibling.innerHTML = IncorrectIndicator;
241                         }
242                 }
243                 else{
244 //Add a copy of the last (correct) answer.
245                         Status[i][3].push(Status[i][3][Status[i][3].length-1]);
246                 }
247 //If it's correct, count it
248                 if (Status[i][0] == 1){
249                         TotCorrectChoices++;
250                 }
251         }
252 //Calculate the score
253         Score = Math.floor(((TotCorrectChoices-Penalties)/TotalUnfixedLeftItems)*100);
254         if (Score<0){Score = 0;}
255         var Feedback = '';
257 //Build the feedback
258         if (AllDone == true){
259                 Feedback = CorrectResponse + '<br />' + YourScoreIs + Score + '%.';
260         }
261         else{
262                 Feedback = IncorrectResponse + '<br />' + YourScoreIs + Score + '%.';
263 //Penalty for incorrect check
264                 Penalties++;
265         }
267 //If the exercise is over, deal with that
268         if ((AllDone == true)||(TimeOver == true)){
269 [inclSendResults]
270                 setTimeout('SendResults(' + Score + ')', 50);
271 [/inclSendResults]
272 [inclTimer]
273                 window.clearInterval(Interval);
274 [/inclTimer]
275                 TimeOver = true;
276                 Locked = true;
277                 Finished = true;
278                 setTimeout('Finish()', SubmissionTimeout);
279                 WriteToInstructions(Feedback);
280         }
282 //Show the feedback
283         ShowMessage(Feedback);
284         
285 [inclScorm1.2]
286         if (AllDone == true){
287                 SetScormComplete();
288         }
289         else{
290                 SetScormIncomplete();
291         }
292 [/inclScorm1.2]
295 [inclTimer]
296 function TimesUp() {
297         document.getElementById('Timer').innerHTML = '[strTimesUp]';
298 [inclPreloadImages]
299         RefreshImages();
300 [/inclPreloadImages]
301         TimeOver = true;
302         Finished = true;
303         CheckAnswers();
304         Locked = true;
305 [inclScorm1.2]
306         SetScormTimedOut();
307 [/inclScorm1.2]
309 [/inclTimer]