Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / hotpot / template / v6 / jcloze6.js_
blob1dca255e67f32e6b6bf282c607d1d58f71370295
2 [inclScorm1.2]
3 //JCLOZE-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 //Now send detailed reports about each item
10                 for (var i=0; i<State.length; i++){
11                         var ThisItemGuesses = '';
12                         var GapLabel = 'Gap_' + (i+1).toString();
13                         var ThisItemScore = Math.floor(State[i].ItemScore * 100) + '';
14                         API.LMSSetValue('cmi.objectives.' + i + '.id', 'obj'+GapLabel);
15                         API.LMSSetValue('cmi.interactions.' + i + '.id', 'int'+GapLabel);
16                         API.LMSSetValue('cmi.objectives.' + i + '.score.raw', ThisItemScore);
17                         API.LMSSetValue('cmi.objectives.' + i + '.score.min', '0');
18                         API.LMSSetValue('cmi.objectives.' + i + '.score.max', '100');
19                         if (State[i].AnsweredCorrectly == true){
20                                 API.LMSSetValue('cmi.objectives.' + i + '.status', 'completed');
21                         }
22                         else{
23                                 API.LMSSetValue('cmi.objectives.' + i + '.status', 'incomplete');
24                         }
25                         for (var j=0; j<State[i].Guesses.length; j++){
26                                 if (j>0){ThisItemGuesses += ' | ';}
27                                 ThisItemGuesses += State[i].Guesses[j];
28                         }
29                         API.LMSSetValue('cmi.interactions.' + i + '.type', 'fill-in');
30                         API.LMSSetValue('cmi.interactions.' + i + '.student_response', ThisItemGuesses);
31                 }
32                 API.LMSCommit('');
33         }
35 [/inclScorm1.2]
37 //JCLOZE CORE JAVASCRIPT CODE
39 function ItemState(){
40         this.ClueGiven = false;
41         this.HintsAndChecks = 0;
42         this.MatchedAnswerLength = 0;
43         this.ItemScore = 0;
44         this.AnsweredCorrectly = false;
45         this.Guesses = new Array();
46         return this;
49 var Feedback = '';
50 var Correct = '[strCorrect]';
51 var Incorrect = '[strIncorrect]';
52 var GiveHint = '[strGiveHint]';
53 var CaseSensitive = [boolCaseSensitive];
54 var YourScoreIs = '[strYourScoreIs]';
55 var Finished = false;
56 var Locked = false;
57 var Score = 0;
58 var CurrentWord = 0;
59 var Guesses = '';
60 var TimeOver = false;
62 I = new Array();
63 [strItemArray]
65 State = new Array();
67 function StartUp(){
68         RemoveBottomNavBarForIE();
69 //Show a keypad if there is one (added bugfix for 6.0.4.12)
70         if (document.getElementById('CharacterKeypad') != null){
71                 document.getElementById('CharacterKeypad').style.display = 'block';
72         }
74 [inclScorm1.2]
75         ScormStartUp();
76 [/inclScorm1.2]
78 [inclSendResults]
79         GetUserName();
80 [/inclSendResults]
82 [inclPreloadImages]
83         PreloadImages([PreloadImageList]);
84 [/inclPreloadImages]
86         var i = 0;
88         State.length = 0;
89         for (i=0; i<I.length; i++){
90                 State[i] = new ItemState();
91         }
93         ClearTextBoxes();
95 [inclTimer]
96         StartTimer();
97 [/inclTimer]
101 function ShowClue(ItemNum){
102         if (Locked == true){return;}
103         State[ItemNum].ClueGiven = true;
104         ShowMessage(I[ItemNum][2]);
107 function SaveCurrentAnswers(){
108         var Ans = '';
109         for (var i=0; i<I.length; i++){
110                 Ans = GetGapValue(i);
111                 if ((Ans.length > 0)&&(Ans != State[i].Guesses[State[i].Guesses.length-1])){
112                         State[i].Guesses[State[i].Guesses.length] = Ans;
113                 }
114         }
117 function CompileGuesses(){
118         var F = document.getElementById('store');
119         if (F != null){
120                 var Temp = '<?xml version="1.0"?><hpnetresult><fields>';
121                 var GapLabel = '';
122                 for (var i=0; i<State.length; i++){
123                         GapLabel = 'Gap ' + (i+1).toString();
124                         Temp += '<field><fieldname>' + GapLabel + '</fieldname>';
125                         Temp += '<fieldtype>student-responses</fieldtype><fieldlabel>' + GapLabel + '</fieldlabel>';
126                         Temp += '<fieldlabelid>JClozeStudentResponses</fieldlabelid><fielddata>';
127                         for (var j=0; j<State[i].Guesses.length; j++){
128                                 if (j>0){Temp += '| ';}
129                                 Temp += State[i].Guesses[j] + ' ';
130                         }
131                 Temp += '</fielddata></field>';
132                 }
133                 Temp += '</fields></hpnetresult>';
134                 Detail = Temp;
135         }
138 function CheckAnswers(){
139         if (Locked == true){return;}
140         SaveCurrentAnswers();
141         var AllCorrect = true;
143 //Check each answer
144         for (var i = 0; i<I.length; i++){
146                 if (State[i].AnsweredCorrectly == false){
147 //If it's right, calculate its score
148                         if (CheckAnswer(i, true) > -1){
149                                 var TotalChars = GetGapValue(i).length;
150                                 State[i].ItemScore = (TotalChars-State[i].HintsAndChecks)/TotalChars;
151                                 if (State[i].ClueGiven == true){State[i].ItemScore /= 2;}
152                                 if (State[i].ItemScore <0 ){State[i].ItemScore = 0;}
153                                 State[i].AnsweredCorrectly = true;
154 //Drop the correct answer into the page, replacing the text box
155                                 SetCorrectAnswer(i, GetGapValue(i));
156                         }
157                         else{
158 //Otherwise, increment the hints for this item, as a penalty
159                                 State[i].HintsAndChecks++;
161 //then set the flag
162                                 AllCorrect = false;
163                         }
164                 }
165         }
167 //Calculate the total score
168         var TotalScore = 0;
169         for (i=0; i<State.length; i++){
170                 TotalScore += State[i].ItemScore;
171         }
172         TotalScore = Math.floor((TotalScore * 100)/I.length);
174 //Compile the output
175         Output = '';
177         if (AllCorrect == true){
178                 Output = Correct + '<br />';
179         }
181         Output += YourScoreIs + ' ' + TotalScore + '%.<br />';
182         if (AllCorrect == false){
183                 Output += '<br />' + Incorrect;
184         }
185         ShowMessage(Output);
186         setTimeout('WriteToInstructions(Output)', 50);
188         Score = TotalScore;
189         CompileGuesses();
191         if ((AllCorrect == true)||(Finished == true)){
193 [inclSendResults]
194                 setTimeout('SendResults(' + TotalScore + ')', 50);
195 [/inclSendResults]
196 [inclTimer]
197                 window.clearInterval(Interval);
198 [/inclTimer]
199                 TimeOver = true;
200                 Locked = true;
201                 Finished = true;
202                 setTimeout('Finish()', SubmissionTimeout);
203         }
204 [inclScorm1.2]
205         if (AllCorrect == true){
206                 SetScormComplete();
207         }
208         else{
209                 SetScormIncomplete();
210         }
211 [/inclScorm1.2]
214 function TrackFocus(BoxNumber){
215         CurrentWord = BoxNumber;
216         InTextBox = true;
219 function LeaveGap(){
220         InTextBox = false;
223 function CheckBeginning(Guess, Answer){
224         var OutString = '';
225         var i = 0;
226         var UpperGuess = '';
227         var UpperAnswer = '';
229         if (CaseSensitive == false) {
230                 UpperGuess = Guess.toUpperCase();
231                 UpperAnswer = Answer.toUpperCase();
232         }
233         else {
234                 UpperGuess = Guess;
235                 UpperAnswer = Answer;
236         }
238         while (UpperGuess.charAt(i) == UpperAnswer.charAt(i)) {
239                 OutString += Guess.charAt(i);
240                 i++;
241         }
242         OutString += Answer.charAt(i);
243         return OutString;
246 function GetGapValue(GNum){
247         var RetVal = '';
248         if ((GNum<0)||(GNum>=I.length)){return RetVal;}
249         if (document.getElementById('Gap' + GNum) != null){
250                 RetVal = document.getElementById('Gap' + GNum).value;
251                 RetVal = TrimString(RetVal);
252         }
253         else{
254                 RetVal = State[GNum].Guesses[State[GNum].Guesses.length-1];
255         }
256         return RetVal;
259 function SetGapValue(GNum, Val){
260         if ((GNum<0)||(GNum>=I.length)){return;}
261         if (document.getElementById('Gap' + GNum) != null){
262                 document.getElementById('Gap' + GNum).value = Val;
263                 document.getElementById('Gap' + GNum).focus();
264         }
267 function SetCorrectAnswer(GNum, Val){
268         if ((GNum<0)||(GNum>=I.length)){return;}
269         if (document.getElementById('GapSpan' + GNum) != null){
270                 document.getElementById('GapSpan' + GNum).innerHTML = Val;
271         }
274 function FindCurrent() {
275         var x = 0;
276         FoundCurrent = -1;
278 //Test the current word:
279 //If its state is not set to already correct, check the word.
280         if (State[CurrentWord].AnsweredCorrectly == false){
281                 if (CheckAnswer(CurrentWord, false) < 0){
282                         return CurrentWord;
283                 }
284         }
286         x=CurrentWord + 1;
287         while (x<I.length){
288                 if (State[x].AnsweredCorrectly == false){
289                         if (CheckAnswer(x, false) < 0){
290                                 return x;
291                         }
292                 }
293         x++;
294         }
296         x = 0;
297         while (x<CurrentWord){
298                 if (State[x].AnsweredCorrectly == false){
299                         if (CheckAnswer(x, false) < 0){
300                                 return x;
301                         }
302                 }
303         x++;
304         }
305         return FoundCurrent;
308 function CheckAnswer(GapNum, MarkAnswer){
309         var Guess = GetGapValue(GapNum);
310         var UpperGuess = '';
311         var UpperAnswer = '';
312         if (CaseSensitive == false){
313                 UpperGuess = Guess.toUpperCase();
314         }
315         else{
316                 UpperGuess = Guess;
317         }
318         var Match = -1;
319         for (var i = 0; i<I[GapNum][1].length; i++){
320                 if (CaseSensitive == false){
321                         UpperAnswer = I[GapNum][1][i][0].toUpperCase();
322                 }
323                 else{
324                         UpperAnswer = I[GapNum][1][i][0];
325                 }
326                 if (TrimString(UpperGuess) == UpperAnswer){
327                         Match = i;
328                         if (MarkAnswer == true){
329                                 State[GapNum].AnsweredCorrectly = true;
330                         }
331                 }
332         }
333         return Match;
336 function GetHint(GapNum){
337         Guess = GetGapValue(GapNum);
339         if (CheckAnswer(GapNum, false) > -1){return ''}
340         RightBits = new Array();
341         for (var i=0; i<I[GapNum][1].length; i++){
342                 RightBits[i] = CheckBeginning(Guess, I[GapNum][1][i][0]);
343         }
344         var RightOne = FindLongest(RightBits);
345         var Result = I[GapNum][1][RightOne][0].substring(0,RightBits[RightOne].length);
346 //Add another char if the last one is a space
347         if (Result.charAt(Result.length-1) == ' '){
348                 Result = I[GapNum][1][RightOne][0].substring(0,RightBits[RightOne].length+1);
349         }
350         return Result;
353 function ShowHint(){
354         if (document.getElementById('FeedbackDiv').style.display == 'block'){return;}
355         if (Locked == true){return;}
356         var CurrGap = FindCurrent();
357         if (CurrGap < 0){return;}
359         var HintString = GetHint(CurrGap);
361         if (HintString.length > 0){
362                 SetGapValue(CurrGap, HintString);
363                 State[CurrGap].HintsAndChecks += 1;
364         }
365         ShowMessage(GiveHint);
368 function TypeChars(Chars){
369         var CurrGap = FindCurrent();
370         if (CurrGap < 0){return;}
371         if (document.getElementById('Gap' + CurrGap) != null){
372                 SetGapValue(CurrGap, document.getElementById('Gap' + CurrGap).value + Chars);
373         }
376 [inclTimer]
377 function TimesUp() {
378         document.getElementById('Timer').innerHTML = '[strTimesUp]';
379 [inclPreloadImages]
380         RefreshImages();
381 [/inclPreloadImages]
382         TimeOver = true;
383         Finished = true;
384         CheckAnswers();
385         Locked = true;
386 [inclScorm1.2]
387         SetScormTimedOut();
388 [/inclScorm1.2]
390 [/inclTimer]