forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotpot-full.js
3012 lines (3009 loc) · 104 KB
/
hotpot-full.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
// PLEASE NOTE that this version is more recent than the incorrectly
// numbered v6.1, dated 2003.11.17. From now on, version numbers will
// follow those of Hot Potatoes.
/* hot-potatoes.js (v6.0.4.0 - 2005.02.18)
* =======================================
* by Gordon Bateson, February 2003
* Copyright (c) 2003 Gordon Bateson. All Rights Reserved.
*
* You are hereby granted a royalty free license to use or modify this
* software provided that this copyright notice appears on all copies.
*
* This software is provided "AS IS" without a warranty of any kind.
*
* Documentation and downloads may be available from:
* http://www.kanazawa-gu.ac.jp/~gordon/research/hot-potatoes/
*/
// This JavaScript library modifies the SendResults and StartUp functions
// used by hotpot v5 and v6, so that more (or less!) details about the
// student can be input, and more details of a quiz's questions and answers
// can be submitted to the server when the quiz is finished
// If the arrays below (Login, DB, JBC, ...) are set BEFORE calling this
// script, they will NOT be overwritten. Any array that is not set, will
// use the defaults below. This is useful if you want to use different
// settings for different quizzes.
// ************
// Login Screen
// ************
if (window.Login==null) {
Login = new Array();
Login[0] = true; // Show prompt for user name
// This can also be a string of user names ...
// Login[0] = "Guest,Peter,Paul,Mary,Webmaster";
// or an array of user names (and on-screen texts) (and passwords) ...
// Login[0] = new Array("Guest", "001,Peter,xxxx", "002,Paul,yyyy", "003,Mary,zzzz", "Webmaster");
// and can also be written as ...
// Login[0] = new Array(
// new Array("Guest"),
// new Array("001", "Peter", "xxxx"),
// new Array("002", "Paul", "yyyy"),
// new Array("003", "Mary", "zzzz"),
// new Array("Webmaster")
// );
Login[1] = true; // Show prompt for student's UserID
// If there is no password prompt (i.e. Logon[3] is false), this value
// will be checked against the password information, if any, in Login[0]
Login[2] = false; // Show prompt for student's email
Login[3] = false; // Show prompt for quiz password, and check this value against
// the password information, if any, in Login[0]
// This can also be a string required to start the quiz ...
// Login[3] = "password";
Login[4] = true; // Show prompt for the cookie expiry date
// If false, cookies expire at the end of the current session
Login[5] = "guest,webmaster"
// guest user names (case insensitive) ...
// Login[5] = "guest,webmaster";
// These users do NOT need to fill in other login fields
// and their quiz results are NOT added to the database
// the Login prompts and error messages
// are defined in the MSG array (see below)
}
// *********
// Database (for use with BFormMail)
// *********
if (window.DB==null) {
DB = new Array();
DB[0] = true; // append form fields to database on server
// If you are NOT using BFormMail's database feature,
// set DB[0]=false, and you can then safely ignore DB[1 to 5]
DB[1] = "/home/gordon/public_html/cgi/hot-potatoes-data";
// append_db folder path (no trailing slash)
// Can be either an absolute path e.g. "/home/gordon/public_html/cgi/hot-potatoes-data"
// or a relative (to CGI bin) path e.g. "hot-potatoes-data"
DB[2] = "hot-potatoes";
// append_db file name (no extension)
// If left blank, the quiz file name, without extension, will be used
// i.e. each quiz will have its results stored in a different file.
// If filled in, this file will store the results for ALL quizzes.
// Database files and folders must be set up BEFORE running the quiz
// must have appropriate access privileges (on Unix, use "chmod 666").
DB[3] = ""; // append_db extension (if left blank, ".txt" will be used)
DB[4] = ""; // db_fields (if left blank, ALL quiz fields will be sent)
DB[5] = ""; // db_delimiter (if left blank, tab will be used)
DB[6] = "REMOTE_ADDR,HTTP_USER_AGENT";
// env_report ('REMOTE_ADDR','HTTP_USER_AGENT' and a few others)
// for a complete description of these fields are, see ...
// http://www.infosheet.com/stuff/BFormMail.readme
// Switches DB[7] and DB[8] force the settings in the ResultForm
// In v5 and v6 quizzes, these settings wil be override those in the original quiz
// If the quiz results are to be sent to an LMS (via the "store" form)
// then switches DB[7] and DB[8] are not used
DB[7] = ''; // URL of form processing script
// e.g. http://www.kanazawa-gu.ac.jp/~gordon/cgi/bformmail.cgi
DB[8] = ''; // email address to which results should be sent
// e.g. [email protected]
}
// By default the quiz's question's scores will be returned.
// If you want more detailed information, set the flags below:
// ********
// JBC
// ********
if (window.JBC==null) {
JBC = new Array();
JBC[0] = true; // show separator line between answers on email
JBC[1] = true; // show number of attempts to answer question
JBC[2] = true; // show question texts
JBC[3] = true; // show right answer(s)
JBC[4] = true; // show wrong answer(s)
JBC[5] = true; // show ignored answer(s)
JBC[6] = false; // show answer as text (false) or number (true)
}
// JBC quizzes use the global variables 'I' and 'Status'
// I : an array of JBC_QUESTIONs (one for each question)
// JBC_QUESTION :
// [0] : question text
// [1] : array of JBC_ANSWERs (one for each answer)
// [2] : single/multi flag
// 0 : single answer (using 'button')
// 1 : multiple answers (using 'checkbox')
// JBC_ANSWER :
// [0] : answer text
// [1] : answer feedback
// [2] : correct answer flag
// 0 : this is NOT the correct answer
// 1 : this is the correct answer
// Status : an array of JBC_QUESTION_STATUSes
// JBC_QUESTION_STATUS:
// [0] : correctly answered yet flag
// 0 : this question has NOT been correctly answered
// 1 : this question has been correctly answered
// [1] : array of JBC_ANSWER_STATUSes (one for each answer)
// '0' : initial value
// 'R' : single answer question was answered 'R'ight
// 'W' : single answer question was answered 'W'rong
// 'C' : multiple answer question's checkbox was 'C'hecked
// 'U' : multiple answer question's checkbox was 'U'nchecked
// [2] : number of times this question has been wrongly answered
// [3] : score (out of 1) for this question (maybe undefined on HP<5.5)
// 0 : not correct yet
// 0<[3]<1 : correct but only after [2] wrong attempts
// 1 : correct first time (bravo!)
// N.B. score = (numberOfAnswers - numberofWrongTries) / numberOfAnswers
// ********
// JCloze
// ********
if (window.JCloze==null) {
JCloze = new Array();
JCloze[0] = true; // show separator line between answers on email
JCloze[1] = true; // show student's correct answer
JCloze[2] = true; // show other correct answer(s), if any
JCloze[3] = true; // show wrong answer(s), if any (NOT available for v5)
JCloze[4] = false; // show number of hints + checks (legacy field, replaced by [7]+[9])
JCloze[5] = false; // show if clue was asked for or not (legacy field, replaced by [8])
JCloze[6] = true; // show clue
JCloze[7] = true; // show number of hints (=next letter requests)
JCloze[8] = true; // show number of clues
JCloze[9] = true; // show number of checks
}
// JCloze quizzes use the global variables 'I' and 'State'
// I : array of JCLOZE_ANSWERs
// JCLOZE_ANSWER :
// [0] : (unused)
// [1] : array of JCLOZE_ANSWER_TEXTs
// [2] : clue for this answer
// JCLOZE_ANSWER_TEXT :
// [0] : array (seems unnecessary, just the text would be enough?)
// [0] : text of possible answer
// State : array of JCLOZE_ANSWER_STATEs
// JCLOZE_ANSWER_STATE (v5) :
// [0] : clue asked for or not
// [1] : number of hints (show next letter) and penalties ('check' an incorrect answer)
// [2] : length of answer matched
// [3] : score for this item
// [4] : already answered correctly
// [5] : answer entered in text box (right or not)
// JCLOZE_ANSWER_STATE (v6)
// this.ClueGiven = false;
// this.HintsAndChecks = 0;
// this.MatchedAnswerLength = 0;
// this.ItemScore = 0;
// this.AnsweredCorrectly = false;
// this.Guesses = new Array(); last guess is correct answer
// ********
// JCross
// ********
if (window.JCross==null) {
JCross = new Array();
JCross[0] = true; // show separator line between answers on email
JCross[1] = true; // show number of penalties (hints or checks before complete)
JCross[2] = true; // show number of letters
JCross[3] = true; // show correct answers
JCross[4] = true; // show clues
JCross[5] = true; // show wrong answers
JCross[6] = true; // show if clue was asked for or not
JCross[7] = true; // show number of hints (=next letter requests)
JCross[8] = true; // show number of checks
// there are no "ignored" answers for JCross quizzes
}
// JCross quizzes use the following global variables:
// L : letters (of correct answers)
// C : clue numbers (CL in v6)
// G : guesses
// 'L', 'C' ('CL') and 'G' are all 2-dimensional arrays (rows x cols)
//
// v5 quizzes additionally use the following single-dimension arrays
// A : clues for across (horizontal) words
// D : clues for down (vertical) words
// N.B. form is only sent when all answers are correct so
// you can't find out what 'wrong' answers were entered
// ********
// JMatch
// ********
if (window.JMatch==null) {
JMatch = new Array();
JMatch[0] = true; // show separator line between answers on email
JMatch[1] = false; // show number of penalties (= total number of checks)
JMatch[2] = true; // show LHS texts (the question)
JMatch[3] = true; // show correct answers
JMatch[4] = true; // show wrong answers
JMatch[5] = true; // show checks (per match) [empty or unchanged RHS are not counted]
// JMatch has no "clue" or "hint" buttons
// there cannot be any "ignored" answers
}
// v5 JMatch quizzes use the global variables 'I' and 'Status' (and 'RItems')
// v6 JMatch quizzes use only 'Status'
// v6+ JMatch quizzes use 'F' and 'D' (see below)
// I : an array of JMATCH_PAIRs (one for each pair)
// JMATCH_PAIR :
// [0] : LHS text
// [1] : RHS text
// [2] : fixed (=not jumbled) flag
// 0 : not fixed
// 1 : fixed
// [3] : index in drop down list selection
// Status : an array of JMATCH_PAIR_STATUSes
// JMATCH_PAIR_STATUS:
// [0] : correctly matched yet flag
// 0 : this pair has NOT been correctly matched
// 1 : this pair has been correctly matched
// [1] : number of times this item has been wrongly matched
// v6 quizzes only
// [2] : id of original SELECT element containing possible matches
// Note that after matching, this SELECT is removed, so don't try looking for it :-)
// v6+ JMatch quizzes use the global variables 'F' and 'D'
// F : array of JMATCH_FIXED_ITEMs
// JMATCH_FIXED_ITEM:
// [0] : text
// [1] : tag
// D : array of JMATCH_DRAGGABLE_ITEMs
// JMATCH_DRAGGABLE_ITEM
// [0] : text
// [1] : tag of the F item to which it SHOULD be dragged
// [2] : tag of the F item to which it was dragged (initally 0)
// N.B. form is only sent when all answers are correct so
// you can't find out what 'wrong' answers were entered
// ********
// JMix
// ********
if (window.JMix==null) {
JMix = new Array();
JMix[0] = true; // show separator line between answers on email
JMix[1] = false; // show number of wrong guesses (replaced by JMix[5])
JMix[2] = true; // show right answer
JMix[3] = true; // show wrong answer, if any
JMix[4] = false; // show answer as text (false) or number (true)
JMix[5] = true; // show number of checks
JMix[6] = true; // show number of hints (=show next word)
}
// JMix quizzes use the global variables
// 'Segments', 'GuessSequence' and 'Penalties'
// Segments : array of JMix_QUESTIONs
// JMix_QUESTION:
// [0] : text
// [1] : order in sequence
// [2] : used flag
// GuessSequence : array of 'order in sequence' numbers
// Penalties : number of incorrect guesses
// ********
// JQuiz
// ********
if (window.JQuiz==null) {
JQuiz = new Array();
JQuiz[0] = true; // show separator line between answers on email
JQuiz[1] = true; // show question text
JQuiz[2] = true; // show student's correct answer(s)
JQuiz[3] = false; // show wrong and ignored answer(s) (legacy field superceded by [8] & [9])
JQuiz[4] = true; // show number of hints requested
JQuiz[5] = false; // show number of checks of incorrect answers (legacy field superceded by [12])
// HP6 v6 quizzes only
JQuiz[6] = false; // show answer value (false) or A,B,C... index (true)
JQuiz[7] = false; // show all students answers
JQuiz[8] = true; // show student's wrong answers
JQuiz[9] = true; // show ignored answers (not relevant for multi-select questions)
JQuiz[10] = true; // show score weightings
JQuiz[11] = true; // show question type
JQuiz[12] = true; // show number of checks (if true, then JQuiz[5] of will be ignored)
JQuiz[13] = true; // show number of times ShowAnswer button was pressed (usually 0 or 1)
}
// v5 JQuiz quizzes use the global variables 'I' and 'Status'
// I : array of JQUIZ_ANSWERs
// JQUIZ_ANSWER :
// [0] : question text
// [1] : array of JQUIZ_ANSWER_TEXTs (one for each answer)
// JQUIZ_ANSWER_TEXT :
// [0] : array (seems unnecessary, just the text would be enough?)
// [0] : text of possible answer
// Status : array of JQUIZ_ANSWER_STATEs
// JQUIZ_ANSWER_STATE :
// [0] : question done or not
// [1] : number of wrong checks
// [2] : number of hints asked for
// [3] : student's answer
// [4] : score for this question
// v6 JQuiz quizzes use the global variables 'I' and 'State'
// I : array of JQUIZ_QUESTIONs
// JQUIZ_QUESTION :
// [0] : weighting
// [1] : ?? (always set to '')
// [2] : question type
// '0'=multiple-choice, '1'=short-answer, '2'=hybrid, '3'=multi-select
// [3] : array of JQUIZ_ANSWERSs (one for each possible answer)
// JQUIZ_ANSWER :
// [0] : answer value
// [1] : feedback text
// [2] : correct answer flag (1=a correct answer, 0=a wrong answer)
// [3] : weighted score (as percentage) if correct
// [4] : flag (usually set to 1, but for hybrid answers that are not
// to be included in multiple choice options, it is set to 0)
// State : array of JQUIZ_QUESTION_STATEs
// JQUIZ_QUESTION_STATE :
// [0] : score (-1 shows not done yet)
// [1] : array showing on which number try each JQUIZ_ANSWER was selected
// [2] : number of attempts at this question
// [3] : total of weighted scores of correct answers that were selected
// i.e. each time a correct answer is selected,
// its JQUIZ_ANSWER[3] weighting is added to this total
// so when all the correct answers have been selected, this will be 100
// [4] : penalties incurred for hints (score is set to zero if >= 1)
// [5] : - for multiple choice, short-answer and hybrid questions, this is a
// comma-delimited list showing order in which answers were chosen
// - for multi-select fields, this is bar-delimted ('|') list of settings
// showing whether each checkbox was selected ('Y') on not ('N') when the
// 'Check' button was clicked. The final item in the list will be the
// settings for the correct answer.
// N.B. JBC, JMatch(v5) and JQuiz(v5) all use global variables 'I' and 'Status'
// JBC : I[0].length==3 && !window.RItems
// JQuiz(v5) : I[0].length==2
// JMatch(v5) : I[0].length==4 && window.RItems
// N.B. JCloze(v5+6) and JQuiz(v6) both use global variables 'I' and 'State'
// JCloze (v5) : I[0].length==3 && State[0].Guesses==null
// JCloze (v6) : I[0].length==3 && State[0].Guesses!=null
// JQuiz (v6) : I[0].length==4
// **********
// Rhubarb
// **********
if (window.Rhubarb==null) {
Rhubarb = new Array();
Rhubarb[0] = true; // show correct words (so far)
Rhubarb[1] = true; // show correct words as count (true) or list (false)
Rhubarb[2] = true; // show wrong words
Rhubarb[3] = false; // show wrong words as count (true) or list (false)
Rhubarb[4] = false; // show ignored words (not implemented yet)
Rhubarb[5] = true; // show hints
}
// **********
// Sequitur
// **********
if (window.Sequitur==null) {
Sequitur = new Array();
Sequitur[0] = true; // show count of correct button clicks
Sequitur[1] = true; // show count of wrong button clicks
}
// **********
// Messages
// **********
if (window.MSG==null) {
MSG = new Array();
// Login prompts
MSG[0] = 'Name';
MSG[1] = 'ID';
MSG[2] = 'Email';
MSG[3] = 'Password';
MSG[4] = 'Cookies';
// Login buttons
MSG[5] = 'Start the Quiz';
MSG[6] = 'Cancel';
// Cookie menu options (only used if Login[4] is true)
MSG[7] = 'keep for this session only';
MSG[8] = 'keep for one day';
MSG[9] = 'keep for one month';
MSG[10] = 'do NOT keep cookies';
// Login error messages
MSG[11] = 'Sorry, you were unable to login. Please try again later.';
MSG[12] = 'Please fill in all the information.';
MSG[13] = 'Incorrect Password. Please try again.';
MSG[14] = 'Incorrect ID. Please try again.';
MSG[15] = 'Email address does not appear to be valid.';
// day and month names (used in Start_Time and End_Time)
MSG[16] = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
MSG[17] = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
// enable popups
MSG[18] = 'Please enable pop-up windows on your browser.';
// browser specific instuctions on how to enable popup windows
var n = navigator;
var s = n.userAgent.toLowerCase();
if (n.appName=='Netscape' && s.indexOf('gecko')>=0) {
// Netscape 6 and 7
MSG[18] += '\n\n' + 'Edit->Preferences, ' + (s.indexOf('mac')>=0 ? 'Advanced->Scripts & Plugins' : 'Privacy & Security->Popup Window Controls');
} else if (s.indexOf('safari')>=0) {
// Safari
MSG[18] += '\n\n' + 'on Safari menu, uncheck "Block Pop-Up Windows"';
} else if (s.indexOf('firebird')>=0) {
// Firebird
MSG[18] += '\n\n' + 'Preferences->Web Features, uncheck "Block Pop-Up Windows"';
} else if (s.indexOf('msie 6')>=0) {
// IE 6 (WinXP.SP2)
MSG[18] += '\n\n' + 'Tools->Pop-up Blocker->Turn Off Pop-up Blocker';
}
}
//if (window.FEEDBACK==null) {
// FEEDBACK = new Array();
// FEEDBACK[0] = ''; // url of feedback page/script
// FEEDBACK[1] = ''; // array of array('teachername', 'value');
// FEEDBACK[2] = ''; // 'student name' [formmail only]
// FEEDBACK[3] = ''; // '[email protected]>' [formmail only]
// FEEDBACK[4] = ''; // window width
// FEEDBACK[5] = ''; // window height
// FEEDBACK[6] = ''; // 'Send a message to teacher' [prompt/button text]
// FEEDBACK[7] = ''; // 'Title'
// FEEDBACK[8] = ''; // 'Teacher'
// FEEDBACK[8] = ''; // 'Message'
// FEEDBACK[10] = ''; // 'Close this window' [formmail only]
//}
// **********
// HP array
// **********
HP = new Array();
for (var i=0; i<=8; i++) {
HP[i] = new Array();
}
// indexes for the HP array (makes the code further down easier to read)
_score = 0;
_weight = 1;
_correct = 2;
_wrong = 3;
_unused = 4;
_hints = 5;
_clues = 6;
_checks = 7;
_guesses = 8;
// *************
// Server Fields
// *************
if (window.ServerFields==null) {
ServerFields = new Array();
// these fields will be added to the ResultForm and submitted to the CGI script on the server.
// 'Sort', 'return_link_title', 'return_link_url' and 'print_blank_fields' are useful for formmail
// override the HP setting of sort fields (forces ALL fields to be displayed)
ServerFields[0] = new Array('sort', '');
// add link to close pop-up results window
ServerFields[1] = new Array('return_link_title', 'Close this window');
ServerFields[2] = new Array('return_link_url', 'javascript:self.close()');
// make sure zero values are printed
ServerFields[3] = new Array('print_blank_fields', 'yes');
// you can also set other fields for your customized CGI script
// e.g. adding a server defined start time (instead of a client defined start time)
// ServerFields[4] = new Array('serverStartTime', '<?php echo date("Y-m-d H:i:s") ?>');
}
// *********************
// Login screen
// (not required by LMS)
// *********************
function QuizLogin(LoginPrompt) {
if (!is_LMS() && (Login[0] || Login[1] || Login[2] || Login[3])) {
var html = ''
+ '<html>'
+ '<head></head>'
+ '<body bgColor="#cccccc" onLoad="opener.setFocus(self)">'
+ '<form onSubmit="'
+ 'self.ok=true;'
+ 'self.expiry=null;'
;
if (Login[4]) { // cookie expiry
html += "opener.checkOK(self,'CookieExpiry');";
}
if (Login[0]) { // user name
html += "opener.checkOK(self,'UserName');";
}
if (Login[1]) { // user ID
html += "opener.checkOK(self,'UserID');";
}
if (Login[2]) { // user email
html += "opener.checkOK(self,'UserEmail');";
}
if (Login[3]) { // quiz password
html += "opener.checkOK(self,'Password');";
}
html += 'if(ok){'
+ 'opener.StartQuiz();'
+ 'self.close();'
+ '}else{'
+ 'if(isNaN(self.tries))self.tries=0;'
+ 'self.tries++;'
+ 'if(self.tries<3){'
+ 'opener.setFocus(self);'
+ '}else{'
+ "alert(opener.MSG[11]);"
+ 'opener.goBack();'
+ 'self.close();'
+ '}'
+ '}'
+ 'return false;'
+ '">'
;
html += '<table>'
+ '<caption>' + LoginPrompt + '</caption>';
;
if (Login[0]) { // user name
var v = getCookie(self, 'UserName');
html += '<tr>'
+ '<th align=right nowrap>' + MSG[0] + ' :</th>'
+ '<td>'
;
if (typeof(Login[0])=='boolean') { // text box
html += '<input type=text name=UserName value="' + v + '">';
} else { // drop down menu of names
// pattern to match commas and white space
var comma = (window.RegExp) ? new RegExp('\\s*,\\s*') : ',';
// convert list of names to array, if necessary
if (typeof(Login[0])=='string') {
Login[0] = Login[0].split(comma);
}
html += '<select name=UserName size=1>'
+ '<option value=""></option>'
;
for(var i=0; i<Login[0].length; i++) {
// convert name details to array if nececount_cary
if (typeof(Login[0][i])=='string') {
Login[0][i] = Login[0][i].split(comma);
}
html += makeOption(Login[0][i][0], v, Login[0][i][1]);
}
html += '</select>';
}
html += '</td>'
+ '</tr>'
;
}
if (Login[1]) { // user ID
var v = getCookie(self, 'UserID');
html += '<tr><th align=right nowrap>' + MSG[1] + ' :</th><td><input type=text name=UserID value="' + v + '"></td></tr>';
}
if (Login[2]) { // user email
var v = getCookie(self, 'UserEmail');
html += '<tr><th align=right nowrap>' + MSG[2] +' :</th><td><input type=text name=UserEmail value="' + v + '"></td></tr>';
}
if (Login[3]) { // quiz password
var v = getCookie(self, 'Password');
html += '<tr><th align=right nowrap>' + MSG[3] + ' :</th><td><input type=password name=Password value="' + v + '"></td></tr>';
}
if (Login[4]) { // cookie lifespan
var v = getCookie(self, 'CookieExpiry');
html += '<tr>'
+ '<th align=right nowrap>' + MSG[4] + ' :</th>'
+ '<td>'
+ '<select name="CookieExpiry" size=1>'
+ makeOption('session', v, MSG[7])
+ makeOption('day', v, MSG[8])
+ makeOption('month', v, MSG[9])
+ makeOption('never', v, MSG[10])
+ '</select>'
+ '</td>'
+ '</tr>'
;
}
html += '<tr>'
+ '<th> </th>'
+ '<td nowrap>'
+ '<input type=submit value="' + MSG[5] + '"> '
+ '<input type=button value="' + MSG[6] + '" onClick="opener.goBack();self.close();">'
+ '</td>'
+ '</tr>'
+ '</table></form></body></html>'
;
// set height of Login Window
var m = navigator.userAgent.indexOf('Mac')>=0;
var h = (m ? 80 : 100);
for (var i=0; i<5; i++) h += (Login[i] ? (m ? 20 : 25) : 0);
// open up a new window
if (!openWindow('', '', (m ? 320 : 300), h, 'RESIZABLE', html)) {
alert(MSG[18]); // unable to open popup window
}
} else { // no Login required
window.UserName = window.UserID = window.UserEmail = window.Password = '';
window.StartQuiz();
}
return true;
}
function makeOption(value, v, txt) {
return '<option value="' + value + '"' + (value==v ? ' SELECTED' : '') + '>' + (txt ? txt : value) + '</option>';
}
function setFocus(w) {
w.focus(); // bring window to the front
var obj = w.document.forms[0].elements;
for(var i=0; i<obj.length; i++) {
var v = getValue(w, i);
if (v=='' || obj[i].type=='submit') {
obj[i].focus();
break;
}
}
}
function checkOK(w, n){
var v = getValue(w, n, true);
if (v || (n!='UserName' && isGuest())) {
if (n=='CookieExpiry') setCookieExpiry(w, v);
setCookie(self, n, v, w.expiry);
if (n!='CookieExpiry') eval('self.' + n + '=v');
} else {
if (w.ok) alert(MSG[12]);
w.ok = false;
}
}
function getValue(w, n, flag) {
var obj = w.document.forms[0].elements[n];
var TYPE = obj.type.toUpperCase(); // required for ns4 (win)
if (obj.options && TYPE.indexOf('SELECT')>=0){
var v = obj.options[obj.selectedIndex].value;
} else {
var v = obj.value;
}
if (flag) {
var msg = '';
if (n=='Password' || (n=='UserID' && !Login[3])) {
var pwd = getPassword(w);
if (pwd && v!=pwd) msg = MSG[n=='Password' ? 13 : 14];
}
if (n=='UserEmail' && window.RegExp) {
var r = '(\\w|-)+';
r = r + '(\\.' + r + ')';
r = new RegExp('^(' + r + '*)@(' + r + '+)$');
if (v.match(r)==null) msg = MSG[15];
}
if (msg) {
obj.value = v = '';
if (w.ok) alert(msg);
w.ok = false;
}
}
return v;
}
function getPassword(w) {
var pwd = '';
if (Login[3] && typeof(Login[3])=='string') {
pwd = Login[3];
} else if ((Login[3] || Login[1]) && typeof(Login[0])=='object') {
var username = getValue(w, 'UserName');
for(var i=0; i<Login[0].length; i++) {
if (username==Login[0][i][0]) {
pwd = Login[0][i][2];
break;
}
}
}
return pwd;
}
function setCookieExpiry(w, v) {
if (v=='never'){
w.expiry = new Date('Thu, 01-Jan-70 00:00:01 GMT');
} else if (v=='day' || v=='month') {
var ms = (v=='month' ? 31 : 1) * 60 * 60 * 24 * 1000;
w.expiry = new Date((new Date()).getTime() + ms);
}
}
function setCookie(w, name, value, expires, path, domain, secure) {
if (name) w.document.cookie = ''
+ 'HP_' + name + "=" + escape(value)
+ (expires ? "; expires=" + expires.toGMTString() : "")
+ (path ? "; path=" + path : "")
+ (domain ? "; domain=" + domain : "")
+ (secure ? "; secure" : "")
;
}
function getCookie(w, n) {
var c = w.document.cookie;
var i = c.indexOf('HP_' + n + '=');
var j = (i<0) ? -1 : c.indexOf(';', (i += n.length + 4));
return (i<0) ? '' : unescape(c.substring(i, ((j<0) ? c.length : j)));
}
function goBack(w) {
if (w==null) w = self; // default
if (w.history.length) w.history.back();
}
function openWindow(url, name, width, height, attributes, html) {
// set height, width and attributes
if (window.screen && width && height) {
var W = screen.availWidth;
var H = screen.availHeight;
width = Math.min(width, W);
height = Math.min(height, H);
attributes = ''
+ (attributes ? (attributes+',') : '')
+ 'WIDTH='+width+',HEIGHT='+height
;
}
// create global hpWindows object, if necessary
if (!window.hpWindows) window.hpWindows = new Array();
// initialize window object
var w = null;
// has a window with this name been opened before?
if (name && hpWindows[name]) {
// http://www.webreference.com/js/tutorial1/exist.html
if (hpWindows[name].open && !hpWindows[name].closed) {
w = hpWindows[name];
w.focus();
} else {
hpWindows[name] = null;
}
}
// check window is not already open
if (w==null) {
// workaround for "Access is denied" errors in IE when offline
// based on an idea seen at http://www.devshed.com/Client_Side/JavaScript/Mini_FAQ
var ie_offline = (document.all && location.protocol=='file:');
// try and open the new window
w = window.open((ie_offline ? '' : url), name, attributes);
// check window opened OK (user may have prevented popups)
if (w) {
// center the window
if (window.screen && width && height) {
w.moveTo((W-width)/2, (H-height)/2);
}
// add content, if required
if (html) {
with (w.document) {
clear();
open();
write(html);
close();
}
} else if (url && ie_offline) {
w.location = url;
}
if (name) hpWindows[name] = w;
}
}
return w;
}
// *********************
// Send results by email
// (not required by LMS)
// *********************
function SendAllResults(Score) {
// check this quiz is not generated by a LMS
if (!is_LMS()) {
// add flat file database details to the results form
AddDatabaseDetailsToResultForm();
// add student details to the results form
AddStudentDetailsToResultForm();
// add question details to the results form
AddQuestionDetailsToResultForm();
// add server fields, if any, to results form
AddServerFieldsToResultForm();
// change "method" of form, because "get" only allows 512 byts of data
ResultForm = replaceLast('method="get"', 'method="post"', ResultForm);
// create results window and form
var w = openWindow('', '', 500, 400, 'RESIZABLE,SCROLLBARS,LOCATION', ResultForm);
// check window opened OK (user may have prevented popups)
if (w) {
// get shortcut to form object
var form = w.document.forms[0];
// update some important field values
form.Score.value = Score + '%';
form.realname.value = UserName;
form.Start_Time.value = getTime(Start_Time);
form.End_Time.value = getTime();
// force email subject and Exercise title
form.subject.value = document.title;
form. Exercise.value = document.title;
// update DB fields, if required
if (DB[0] && !isGuest()) set_db_fields(form);
if (DB[7]) form.action = DB[7];
if (DB[8]) form.recipient.value = DB[8];
// if this is a Netscape browser, check if the referer will be set OK
if (navigator.appName=='Netscape' && (location.protocol=='file:' || navigator.userAgent.indexOf('Netscape6')>=0)) {
// ns4 and ns7 set referer to 'file:// ...' when running a quiz offline
// ns6.2 (at least) always sets referer to 'about:blank'
// Netscape's setting of referer can cause BFormMail
// to reject the form, so encode the form data as a URL
var url = form.action;
var obj = form.elements;
for (var i=0; i<obj.length; i++) {
var v = escape(obj[i].value);
v = v.replace((new RegExp('\\+', 'g')), '%2B');
url += (i==0 ? '?' : '&') + obj[i].name + '=' + v;
}
w.location.href = url;
} else { // browser can POST form ok
form.submit();
}
} else { // unable to open popup window
alert(MSG[18]);
}
} // end if LMS
}
function isGuest() {
// check username is not a "guest" user
var flag = false;
var n = getCookie(self, 'UserName').toLowerCase();
if (n) {
// convert list of user names to array, if necessary
if(typeof(Login[5])=='string') {
Login[5] = Login[5].split(',');
}
for(var i=0; i<Login[5].length; i++) {
if (n==Login[5][i].toLowerCase()) {
flag = true;
break;
}
}
}
return flag;
}
function set_db_fields(form) {
// update list of DB fields, if required
if (DB[4]=='' && window.RegExp) {
// add administration fields
var db_fields = ''
+ 'subject,realname'
+ (Login[1] ? ',ID' : '')
+ (Login[2] ? ',email' : '')
+ (Login[3] ? ',password' : '')
+ ',Score,Start_Time,End_Time'
;
// add answer fields (except separators)
var r = new RegExp('^[^_]+_q\\d\\d_\\w+$');
for(var i=0; i<form.elements.length; i++) {
var n = form.elements[i].name;
if (r.test(n)) db_fields += ',' + n;
}
form.db_fields.value = db_fields;
}
// make sure delimiter is set (NS6+ requires this be set here, not any earlier)
form.db_delimiter.value = (DB[5] ? DB[5] : '\t');
}
function AddStudentDetailsToResultForm() {
var sDetails = '';
if (Login[0]) { // user name
// use 'realname' instead of a separate 'Name' field
// sDetails += hpHiddenField('Name', window.UserName);
}
if (Login[1]) { // user ID
sDetails += hpHiddenField('ID', window.UserID);
}
if (Login[2]) { // user email
sDetails += hpHiddenField('email', window.UserEmail);
}
if (sDetails && window.RegExp) {
// insert sDetails before '<input...Score...></input>'
var r = new RegExp('<input[^>]*Score[^>]*><\\/input>', 'i');
var m = r.exec(ResultForm);
if (m) {
ResultForm = ResultForm.replace(m[0], sDetails + m[0] + makeSeparator('Time_'));
sDetails = '';
}
}
if (Login[3]) { // quiz password
sDetails += hpHiddenField('Password', window.Password);
ResultForm = replaceLast('</form>', sDetails + '</form>', ResultForm);
}
}
function AddQuestionDetailsToResultForm() {
var qDetails = GetQuestionDetails();
if (qDetails) {
// insert qDetails before the final </form> tag in the ResultForm
ResultForm = replaceLast('</form>', qDetails + '</form>', ResultForm);
}
}
function AddDatabaseDetailsToResultForm() {
if (window.DB && DB[0] && !isGuest()) {
var dbDetails = '';
var folder = DB[1];
if (folder && folder.charAt(folder.length-1)!='/') folder += '/';
var file = DB[2];
if (file=='') {
file = location.href;
file= file.substring(file.lastIndexOf('/')+1);
var i = file.indexOf('?');
if (i >= 0) file = file.substring(0, i);
var i = file.lastIndexOf('.');
if (i >= 0) file = file.substring(0, i);
}
var ext = (DB[3] ? DB[3] : 'txt');
if (ext.charAt(0)!='.') ext = '.' + ext;
dbDetails += hpHiddenField('append_db', folder + file + ext);
dbDetails += hpHiddenField('db_fields', DB[4]);
dbDetails += hpHiddenField('db_delimiter', ''); // NS6+ requires this be set later
if (DB[6]) dbDetails += hpHiddenField('env_report', DB[6]);
// insert dbDetails before the final </form> tag in the ResultForm
ResultForm = replaceLast('</form>', dbDetails + '</form>', ResultForm);
}
}
function AddServerFieldsToResultForm() {
if (window.ServerFields) {
var s = ''; // input tags for s(erver fields)
for (var i=0; i<ServerFields.length; i++) {
if (ServerFields[i][0] && window.RegExp) {
// remove previous field value, if any
var r = new RegExp('<input[^>]*name\\s*=\\s*["\']\\s*' + ServerFields[i][0] + '[^>]*>(\\s*<\\/input>)?', 'i');
if (r.test(ResultForm)) {
ResultForm = ResultForm.replace(r, '');
}
}
if (ServerFields[i][1]) {
s += hpHiddenField(ServerFields[i][0], ServerFields[i][1]);
}
} // end for
if (s) ResultForm = replaceLast('</form>', s + '</form>', ResultForm);
}
}
function replaceLast(a, b, c) {
// replace last occurrence of 'a' in 'c' with 'b'
var l = a.length;
var i = c.lastIndexOf(a);
return (i<0 || l==0) ? c : (c.substring(0, i) + b + c.substring(i+l));
}
// *************************
// Extract question details
// *************************
function GetQuestionDetails() {
var hp = hpVersion();
var t = hpQuizType();
var v = hpQuizVersion();
return (t==1) ? GetJbcQuestionDetails(hp, v) :
(t==2) ? GetJClozeQuestionDetails(hp, v) :
(t==3) ? GetJCrossQuestionDetails(hp, v) :
(t==4) ? GetJMatchQuestionDetails(hp, v) :
(t==5) ? GetJMixQuestionDetails(hp, v) :
(t==6) ? GetJQuizQuestionDetails(hp, v) :
(t==7) ? GetRhubarbDetails(hp, v) :
(t==8) ? GetSequiturDetails(hp, v) : '';
}
function GetJbcQuestionDetails(hp, v) {
qDetails = '';
// check the quiz version
if (hp==5 || hp==6) {
// get question details
for(var q=0; q<I.length; q++) {
// initialize strings to hold answer details
var aDetails = new Array();
aDetails[0] = new Array(); // right
aDetails[1] = new Array(); // wrong
aDetails[2] = new Array(); // ignored
// get answer details
for(var a=0; a<I[q][1].length; a++) {
var i = (Status[q][1][a]=='R') ? 0 : (Status[q][1][a]=='0') ? 2 : 1;
aDetails[i][aDetails[i].length] = (JBC[6] ? a : I[q][1][a][0]);
}
// format 'Q' (a padded, two-digit version of 'q')
var Q = getQ('JBC', q);
// add separator, if required
if (JBC[0]) qDetails += makeSeparator(Q);
if (JBC[1]) { // number of attempts to answer question
qDetails += hpHiddenField(Q+'attempts', Status[q][2] + (Status[q][0]==1 ? 1 : 0));
}
if (JBC[2]) { // question text
qDetails += hpHiddenField(Q+'text', I[q][0]);
}
if (JBC[3] && (DB[0] || aDetails[0].length>0)) { // right
qDetails += hpHiddenField(Q+'right', aDetails[0]);
}
if (JBC[4] && (DB[0] || aDetails[1].length>0)) { // wrong
qDetails += hpHiddenField(Q+'wrong', aDetails[1]);
}
if (JBC[5] && (DB[0] || aDetails[2].length>0)) { // ignored
qDetails += hpHiddenField(Q+'ignored', aDetails[2]);
}
// calculate score for this question, if required (for HP version < 5.5)
if (isNaN(Status[q][3])) {
var a1 = Status[q][1].length; // answers
var a2 = Status[q][2]; // attempts
Status[q][3] = (a1<1 || a1<(a2-1)) ? 0 : ((a1 - (a2-1)) / a1);
}
// add 'score' for this question
qDetails += hpHiddenField(Q+'score', Math.floor(Status[q][3]*100)+'%');
} // end for
}
return qDetails;
}
function GetJClozeQuestionDetails(hp, v) {
var qDetails = '';
// check the quiz version
if (hp==5 || hp==6) {
var r = hpRottmeier();
if (parseInt(r)==2) { // Rottmeier Find-It 3a+3b
qDetails += hpHiddenField('JCloze_penalties', window.TotWrongChoices);
}
// get details for each question
var q_max = (r==0) ? State.length : GapList.length; // could use I.length for both
for (var q=0; q<q_max; q++) {