adding some strings
[moodle-linuxchix.git] / mod / hotpot / template / v6 / djmix6.js_
blobe568c04660612b06bf1f5b9bebe82ab36c270cb5
1 [inclScorm1.2]
2 //JMMIX-SPECIFIC SCORM-RELATED JAVASCRIPT CODE
4 function SetScormScore(){
5 //Reports the current score and any other information back to the LMS
6         if (API != null){
7                 API.LMSSetValue('cmi.core.score.raw', Score);
8                 
9 //Now send a detailed reports on the item
10                 var ItemLabel = 'Item_1';
11                 API.LMSSetValue('cmi.objectives.0.id', 'obj'+ItemLabel);
12                 API.LMSSetValue('cmi.interactions.0.id', 'int'+ItemLabel);
13                 if (Finished == true){
14                         API.LMSSetValue('cmi.objectives.0.status', 'completed');
15                 }
16                 else{
17                         API.LMSSetValue('cmi.objectives.0.status', 'incomplete');
18                 }
19                                 
20                 API.LMSSetValue('cmi.objectives.0.score.min', '0');
21                 API.LMSSetValue('cmi.objectives.0.score.max', '100');
22                 API.LMSSetValue('cmi.objectives.0.score.raw', Score);
23 //We can only use the performance type, because we're storing multiple responses of various types.
24                 API.LMSSetValue('cmi.interactions.0.type', 'performance');
25                 API.LMSSetValue('cmi.interactions.0.student_response', AnswersTried);
26                 
27                 
28                 API.LMSCommit('');
29         }
31 [/inclScorm1.2]
33 //JMIX DRAG-DROP OUTPUT FORMAT CODE
35 var Punctuation = '[strPunctuation]';
37 var Openers = '[strOpenPunctuation]';
38 var CorrectResponse = '[strGuessCorrect]';
39 var IncorrectResponse = '[strGuessIncorrect]';
40 var ThisMuchCorrect = '[strThisMuch]';
41 var TheseAnswersToo = '[strTheseAnswersToo]';
42 var YourScoreIs = '[strYourScoreIs]';
43 var NextCorrect = '[strNextCorrect]';
44 var FeedbackWidth = 200; //default
45 var ExBGColor = '[strExBGColor]';
46 var PageBGColor = '[strPageBGColor]';
47 var TextColor = '[strTextColor]';
48 var TitleColor = '[strTitleColor]';
49 var DropTotal = 3; // number of lines that will be available for dropping on
50 var Gap = 4; //Gap between two segments when they're next to each other on a line
51 var DropHeight = 30;
52 var CapitalizeFirst = [boolCapitalizeFirst];
53 var CompiledOutput = '';
54 var TempSegment = '';
55 var FirstSegment = -1;
56 var FirstDiv = -1;
57 var Penalties = 0;
58 var Score = 0;
59 var TimeOver = false;
61 var CurrDrag = -1;
62 var topZ = 100;
63 var Cds = new Array();
64 var L = new Array();
65 var Finished = false;
67 var Locked = false;
68 var DivWidth = 600;
69 var LeftColPos = 100;
70 var DragTop = 120;
71 var DragNumber = -1;
72 var AnswersTried = '';
74 Lines = new Array();
76 function CapFirst(InString){
77         var i = 0;
78         if ((Openers.indexOf(InString.charAt(i))>-1)||(InString.charAt(i) == ' ')){
79                 i++;
80         }
81         if ((Openers.indexOf(InString.charAt(i))>-1)||(InString.charAt(i) == ' ')){
82                 i++;
83         }
84         var Temp = InString.charAt(i);
85         Temp = Temp.toUpperCase();
86         InString = InString.substring(0, i) + Temp + InString.substring(i+1, InString.length);
87         return InString;
90 function CheckResults(ChkType){
91 //Get sequence student has chosen
92         GetGuessSequence();
94 //Compile the answer
95         CompiledOutput = CompileString(GuessSequence);
97 //Check the answer
98         CheckAnswer(ChkType);
101 function GetGuessSequence(){
102 //Put pointers to draggables in arrays based on the lines they're sitting on
103         var Drops = new Array();
104         for (var i=0; i<L.length; i++){
105                 Drops[i] = new Array();
106         }
108         var CardPos = 0;
109         
110         for (i=0; i<Cds.length; i++){
111                 for (var j=0; j<L.length; j++){
112 //Slight modification for 6.0.4: allow some leeway for 1px inaccuracy in card placing by browser.
113                         CardPos = L[j].GetB() - (Cds[i].GetH()+2);
114                         if (((Cds[i].GetT() - CardPos) < 4)&&((Cds[i].GetT() - CardPos) > -4)){
115                                 Drops[j][Drops[j].length] = Cds[i];
116                         }
117                 }
118         }
120 //Sort the drop arrays based on the Left of each div
121         for (i=0; i<Drops.length; i++){
122                 Drops[i].sort(CompDrags);
123         }
125 //Put the tags into the GuessSequence array
126         GuessSequence.length = 0;
127         for (i=0; i<Drops.length; i++){
128                 for (j=0; j<Drops[i].length; j++){
129                         GuessSequence[GuessSequence.length] = Drops[i][j].tag;
130                 }
131         }
133 //Set the variable recording which div is first
134         var NewFirstDiv = -1;
135         for (i=0; i<Drops.length; i++){
136                 if (Drops[i].length > 0){
137                         NewFirstDiv = Drops[i][0].index;
138                         break;
139                 }
140         }
141         return NewFirstDiv;
144 function CompDrags(a,b){
145         return a.GetL() - b.GetL(); 
148 function FindSegment(SegID){
149         var Seg = '';
150         for (var i=0; i<Segments.length; i++){
151                 if (Segments[i][1] == SegID){
152                         Seg = Segments[i][0];
153                         break;
154                 }
155         }
156         return Seg;
159 function CompileString(InArray){
160         var OutString = '';
161         var i = 0;
162         OutArray = new Array();
164         for (i=0; i<InArray.length; i++){
165                 OutArray[OutArray.length] = FindSegment(InArray[i]);
166         }
168         if (OutArray.length > 0){
169                 OutString = OutArray[0];
170         }
171         else{
172                 OutString = '';
173         }
174         var Spacer = '';
176         for (i=1; i<OutArray.length; i++){
177                 Spacer = ' ';
178                 if ((Openers.indexOf(OutString.charAt(OutString.length-1)) > -1)||(Punctuation.indexOf(OutArray[i].charAt(0)) > -1)){
179                         Spacer = '';
180                 }
181                 OutString = OutString + Spacer + OutArray[i];           
182         }
184 //Capitalize the first letter if necessary
185         if (CapitalizeFirst == true){
186                 OutString = CapFirst(OutString);
187         }
188         return OutString;
191 function CheckAnswer(CheckType){
192         if (Locked == true){return;}
193         if (GuessSequence.length < 1){
194                 if (CheckType == 1){
195                         Penalties++;
196                         ShowMessage(NextCorrect + '<br /><br />' + FindSegment(Answers[0][0]));
197                 }
198                 return;
199         }
200         var i = 0;
201         var j = 0;
202         var k = 0;
203         var WellDone = '';
204         var WhichCorrect = -1;
205         var TryAgain = '';
206         var LongestCorrectBit = '';
207         TempCorrect = new Array();
208         LongestCorrect = new Array();
209         var TempHint = '';
210         var HintToReturn = 1;
211         var OtherAnswers = '';
212         var AllDone = false;
214         for (i=0; i<Answers.length; i++){
215                 TempCorrect.length = 0;
216                 for (j=0; j<Answers[i].length; j++){
217                         if (Answers[i][j] == GuessSequence[j]){
218                                 TempCorrect[j] = GuessSequence[j];
219                         }
220                         else{
221                                 TempHint = Answers[i][j];
222                                 break;
223                         }
224                 }
225                 if ((TempCorrect.length == GuessSequence.length)&&(TempCorrect.length == Answers[i].length)){
226                         WhichCorrect = i;
227                         break;
228                 }
229                 else{
230                         if (TempCorrect.length > LongestCorrect.length){
231                                 LongestCorrect.length = 0;
232                                 for (k=0; k<TempCorrect.length; k++){
233                                         LongestCorrect[k] = TempCorrect[k];
234                                 }
235                                 HintToReturn = TempHint;
236                         } 
237                 }       
238         }
239         if (WhichCorrect > -1){
240                 AllDone = true;
241                 for (i=0; i<Answers.length; i++){
242                         if (i!=WhichCorrect){
243                                 OtherAnswers += '<br />' + CompileString(Answers[i]);
244                         }
245                 }
246                 WellDone = '<span class="CorrectAnswer">' + CompiledOutput + '</span><br /><br />' + CorrectResponse + '<br />';
247                 
248                 if (AnswersTried.length > 0){AnswersTried += ' | ';}
249                 AnswersTried += CompiledOutput;
251 //Do score calculation here
252                 Score = Math.floor(((Segments.length-Penalties) * 100)/Segments.length);
253                 WellDone += YourScoreIs + ' ' + Score + '%.<br />';
255 [inclAlsoCorrect]
256                 if (OtherAnswers.length > 0){
257                         WellDone += TheseAnswersToo + '<span class="CorrectAnswer">' + OtherAnswers + '</span>';
258                 }
259 [/inclAlsoCorrect]
261                 ShowMessage(WellDone);
262                 WriteToInstructions(YourScoreIs + ' ' + Score + '%.');
263         }
265         else{
266                 var WrongGuess = CompileString(GuessSequence);
267                 if (AnswersTried.length > 0){AnswersTried += ' | ';}
268                 AnswersTried += WrongGuess;
269                 TryAgain = '<span class="Guess">' + WrongGuess + '</span><br /><br />';
270                 if ((CheckType == 0)||(LongestCorrect.length==0)){
271                         TryAgain += IncorrectResponse + '<br />';
272                 }
274                 if (LongestCorrect.length > 0){
275                         LongestCorrectBit = CompileString(LongestCorrect);
276                         GuessSequence.length = LongestCorrect.length;
277                         TryAgain += '<br />' + ThisMuchCorrect + '<br /><span class="Guess">' + LongestCorrectBit + '</span><br />';
278                 }
280                 if (CheckType == 1){
281                         TryAgain += '<br />' + NextCorrect + '<br />' + FindSegment(HintToReturn);
282                 }
283                 
284 [inclTimer]
285                 if (TimeOver == true){
286                         Score = Math.floor(((LongestCorrect.length-Penalties) * 100)/Segments.length);
287                         if (Score < 0){Score = 0;}
288                         TryAgain += YourScoreIs + ' ' + Score + '%.<br />';
289                 }
290 [/inclTimer]
291                 Penalties++; //Penalty for inaccurate check
292                 ShowMessage(TryAgain);
293         }
294         
295 //If the exercise is over, deal with that
296         if ((AllDone == true)||(TimeOver == true)){
297 [inclSendResults]
298                 setTimeout('SendResults(' + Score + ')', 50);
299 [/inclSendResults]
300 [inclTimer]
301                 window.clearInterval(Interval);
302 [/inclTimer]
303                 TimeOver = true;
304                 Locked = true;
305                 Finished = true;
306                 setTimeout('Finish()', SubmissionTimeout);
307                 WriteToInstructions(YourScoreIs + ' ' + Score + '%.'); 
308         }
309         
310 [inclScorm1.2]
311         if (AllDone == true){
312                 SetScormComplete();
313         }
314         else{
315                 SetScormIncomplete();
316         }
317 [/inclScorm1.2]
321 var Segments = new Array();
322 [SegmentArray]
324 var GuessSequence = new Array();
326 var Answers = new Array();
327 [AnswerArray]
329 function doDrag(e) {
330         if (CurrDrag == -1) {return};
331         if (C.ie){var Ev = window.event}else{var Ev = e}
332         var difX = Ev.clientX-window.lastX; 
333         var difY = Ev.clientY-window.lastY; 
334         var newX = Cds[CurrDrag].GetL()+difX; 
335         var newY = Cds[CurrDrag].GetT()+difY; 
336         Cds[CurrDrag].SetL(newX); 
337         Cds[CurrDrag].SetT(newY);
338         window.lastX = Ev.clientX; 
339         window.lastY = Ev.clientY; 
340         return false;
343 function beginDrag(e, DragNum) { 
344         CurrDrag = DragNum;
345         if (C.ie){
346                 var Ev = window.event;
347                 document.onmousemove=doDrag;
348                 document.onmouseup=endDrag;
349         }
350         else{
351                 var Ev = e;
352                 window.onmousemove=doDrag; 
353                 window.onmouseup=endDrag;
354         } 
355         Cds[CurrDrag].SwapColours();
356         topZ++;
357         Cds[CurrDrag].css.zIndex = topZ;
358         window.lastX=Ev.clientX; 
359         window.lastY=Ev.clientY;
360         return true;  
363 function endDrag(e) { 
364         if (CurrDrag == -1) {return};
365         Cds[CurrDrag].SwapColours();
366         if (C.ie){document.onmousemove=null}else{window.onmousemove=null;}
367         onEndDrag();    
368         CurrDrag = -1;
369         return true;
372 function onEndDrag(){
373 //Snap to lines
374         var i = 0;
375         var SnapLine = Cds[CurrDrag].GetT();
376         var BiggestOverlap = -1;
377         var OverlapRect = 0;
378         for (i=0; i<L.length; i++){
379                 if (Cds[CurrDrag].Overlap(L[i]) > OverlapRect){
380                         OverlapRect = Cds[CurrDrag].Overlap(L[i]);
381                         BiggestOverlap = i;
382                 }
383         }
384         if (BiggestOverlap > -1){
385                 SnapLine = L[BiggestOverlap].GetB() - (Cds[CurrDrag].GetH() + 2);
386                 Cds[CurrDrag].SetT(SnapLine);
387                 CheckOver(-1);
388         }
389         if (CapitalizeFirst==true){
390                 setTimeout('DoCapitalization()', 50);
391         }
394 function DoCapitalization(){
395 //Capitalize first segment if necessary
396         var FD = GetGuessSequence();
397         if ((FD == -1)&&(FirstDiv > -1)){
398                 Cds[FirstDiv].elm.innerHTML = Segments[FirstDiv][0];
399         }
400         if (((FD != FirstDiv)&&(CapitalizeFirst == true))&&(FD > -1)){
401                 if (FirstDiv > -1){
402                         Cds[FirstDiv].elm.innerHTML = Segments[FirstDiv][0];
403                 }
404         }
405         if ((FD > -1)&&(CapitalizeFirst == true)){
406                 var Temp = CapFirst(Segments[FD][0]);
407                 Cds[FD].elm.innerHTML = Temp;
408                 FirstDiv = FD;
409         }
412 function CheckOver(NoMove){
413 //This recursive function spreads out the Cards on a line if two of them are overlapping;
414 //if the spread operation moves one beyond the end of a line, it wraps it to the next line.
415         for (var i=0; i<Cds.length; i++){
416                 for (var j=0; j<Cds.length; j++){
417                         if (i!=j){
418                                 if (Cds[i].Overlap(Cds[j]) > 0){
419                                         if ((i==NoMove)||(Cds[i].GetL() < Cds[j].GetL())){
420                                                 Cds[j].DockToR(Cds[i]);
421                                                 if (Cds[j].GetR() > (LeftColPos + DivWidth)){
422                                                         Cds[j].SetL(LeftColPos);
423                                                         Cds[j].SetT(Cds[j].GetT() + DropHeight);
424                                                 }
425                                                 CheckOver(j);
426                                         }
427                                         else{
428                                                 Cds[i].DockToR(Cds[j]);
429                                                 if (Cds[i].GetR() > (LeftColPos + DivWidth)){
430                                                         Cds[i].SetL(LeftColPos);
431                                                         Cds[i].SetT(Cds[i].GetT() + DropHeight);
432                                                 }
433                                                 CheckOver(i);   
434                                         }
435                                 }
436                         }       
437                 }
438         }
441 function StartUp(){
443 [inclSendResults]
444         GetUserName();
445 [/inclSendResults]
447 [inclScorm1.2]
448         ScormStartUp();
449 [/inclScorm1.2]
451 [inclPreloadImages]
452         PreloadImages([PreloadImageList]);
453 [/inclPreloadImages]
455         Segments = Shuffle(Segments);
457 //Calculate page dimensions and positions
458         pg = new PageDim();
459         DivWidth = Math.floor((pg.W*4)/5);
460         LeftColPos = Math.floor(pg.W/10);
461         DragTop = parseInt(document.getElementById('CheckButtonDiv').offsetHeight) + parseInt(document.getElementById('CheckButtonDiv').offsetTop) + 10;
463         var CurrTop = DragTop + 10;
465 //Position the drop divs
466         for (var i=0; i<DropTotal; i++){
467                 L[i] = new Card('Drop' + i, 0);
468                 L[i].SetT(CurrTop)
469                 L[i].tag = CurrTop-5;
470                 L[i].SetL(LeftColPos);
471                 L[i].css.backgroundColor = '[strPageBGColor]';
472                 CurrTop += L[i].GetH();
473                 topZ++;
474                 L[i].css.zIndex = topZ;
475         }
476         DropHeight = L[0].GetH();
478         CurrTop = DragTop;
479         var TempInt = 0;
480         var DropHome = 0;
482         for (i=0; i<Segments.length; i++){
483 //Create a new pointer in the C array to ref the card div
484                 Cds[i] = new Card('D'+i, 0);
485                 Cds[i].elm.innerHTML = Segments[i][0];
486                 Cds[i].SetT(CurrTop);
487                 Cds[i].SetL(LeftColPos);
488                 Cds[i].css.cursor = 'move';
489                 TempInt = Cds[i].GetH();
490                 CurrTop = CurrTop + TempInt + 5;
491                 Cds[i].css.backgroundColor = '[strExBGColor]';
492                 Cds[i].css.color = '[strTextColor]';
493                 topZ++;
494                 Cds[i].css.zIndex = topZ;
495                 Cds[i].tag = Segments[i][1];
496                 Cds[i].index = i;
497         }
498 //Place them at the bottom of the page
499         SetInitialPositions();
501 [inclTimer]
502         StartTimer();
503 [/inclTimer]
506 function SetInitialPositions(){
507 //Places all the divs at the bottom of the page in centred rows
508 //First, get the vertical position of the first row
509         var RTop = L[L.length-1].GetB() + 10;
511 //Create an array to hold the numbers of Cards for each row
512         CRows = new Array();
513         CRows[0] = new Array();
514         Widths = new Array();
515         var i=0;
516         var r=0;
517         var RowWidth=0;
518 //Sort the Cards into rows, storing their numbers in the array
520         while (i<Cds.length){
521 //if it fits on this row, add it
522                 if ((RowWidth + Cds[i].GetW() + 5) < DivWidth){
523                         CRows[r][CRows[r].length] = i;
524                         RowWidth += Cds[i].GetW() + 5;
525 //Store the width in the Widths array for later
526                         Widths[r] = RowWidth;
527                 }
528 //if not, increment the row number, and add it to the next row
529                 else{
530                         r++;
531                         CRows[r] = new Array();
532                         CRows[r][CRows[r].length] = i;
533                         RowWidth = Cds[i].GetW() + 5;
534 //Store the width in the Widths array for later
535                         Widths[r] = RowWidth;
536                 }
537 //move to the next Card
538                 i++;
539         }
540 //Now we have the numbers in rows, set out each row
541         r=0;
542         var Indent=0;
543         for (r=0; r<CRows.length; r++){
544 //Get the required indent for this row
545                 Indent = Math.floor((DivWidth-Widths[r])/2);
546 //Set the first card in position
547                 Cds[CRows[r][0]].SetL(Indent + LeftColPos);
548                 Cds[CRows[r][0]].SetT(RTop);
549                 Cds[CRows[r][0]].SetHome();
550                 for (i=1; i<CRows[r].length; i++){
551                         Cds[CRows[r][i]].DockToR(Cds[CRows[r][i-1]]);
552                         Cds[CRows[r][i]].SetHome();
553                 }
554 //Increment the row height
555                 RTop += Cds[0].GetH() + 5;
556         }
559 function TimerStartUp(){
560         setTimeout('StartUp()', 300);
563 [inclTimer]
564 function TimesUp() {
565         document.getElementById('Timer').innerHTML = '[strTimesUp]';
566 [inclPreloadImages]
567         RefreshImages();
568 [/inclPreloadImages]
569         TimeOver = true;
570         CheckAnswer(0);
571         Locked = true;
572 [inclScorm1.2]
573         SetScormTimedOut();
574 [/inclScorm1.2]
576 [/inclTimer]