v6.php 57.3 KB
Newer Older
1
<?PHP
2
3

class hotpot_xml_quiz_template extends hotpot_xml_template_default {
4
5
6
    // left and right items for JMatch
    var $l_items = array();
    var $r_items = array();
7
8
9
10
11

    // constructor function for this class
    function hotpot_xml_quiz_template(&$parent) {

        $this->parent = &$parent;
12

13
14
15
16
17
        $get_js = optional_param('js', false);
        $get_css = optional_param('css', false);

        if (!empty($get_css)) {
            // set $this->css
18
            $this->v6_expand_StyleSheet();
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

        } else if (!empty($get_js)) {
            // set $this->js
            $this->read_template($this->parent->draganddrop.$this->parent->quiztype.'6.js_', 'js');

        } else {
            // set $this->html
            $this->read_template($this->parent->draganddrop.$this->parent->quiztype.'6.ht_', 'html');
        }

        // expand special strings, if any
        $pattern = '';
        switch ($this->parent->quiztype) {
            case 'jcloze':
                $pattern = '/\[(PreloadImageList)\]/';
                break;
            case 'jcross':
                $pattern = '/\[(PreloadImageList|ShowHideClueList)\]/';
                break;
            case 'jmatch':
                $pattern = '/\[(PreloadImageList|QsToShow|FixedArray|DragArray)\]/';
                break;
            case 'jmix':
                $pattern = '/\[(PreloadImageList|SegmentArray|AnswerArray)\]/';
                break;
            case 'jquiz':
                $pattern = '/\[(PreloadImageList|QsToShow)\]/';
                break;
        }
        if (!empty($pattern)) {
            $this->expand_strings('html', $pattern);
        }
51
52
53
54
55
56
        // fix doctype (convert short dtd to long dtd)
        $this->html = preg_replace(
            '/<!DOCTYPE[^>]*>/',
            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
            $this->html, 1
        );
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    }

    // captions and messages

    function v6_expand_AlsoCorrect() {
        return $this->parent->xml_value('hotpot-config-file,'.$this->parent->quiztype.',also-correct');
    }
    function v6_expand_CapitalizeFirst() {
        return $this->bool_value('hotpot-config-file,'.$this->parent->quiztype.',capitalize-first-letter');
    }
    function v6_expand_CheckCaption() {
        return $this->parent->xml_value('hotpot-config-file,global,check-caption');
    }
    function v6_expand_CorrectIndicator() {
        return $this->js_value('hotpot-config-file,global,correct-indicator');
    }
    function v6_expand_Back() {
        return $this->int_value('hotpot-config-file,global,include-back');
    }
    function v6_expand_BackCaption() {
77
        return str_replace('<=', '&lt;=', $this->parent->xml_value('hotpot-config-file,global,back-caption'));
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
    }
    function v6_expand_ClickToAdd() {
        return $this->parent->xml_value('hotpot-config-file,'.$this->parent->quiztype.',click-to-add');
    }
    function v6_expand_ClueCaption() {
        return $this->parent->xml_value('hotpot-config-file,global,clue-caption');
    }
    function v6_expand_Clues() {
        return $this->int_value('hotpot-config-file,'.$this->parent->quiztype.',include-clues');
    }
    function v6_expand_Contents() {
        return $this->int_value('hotpot-config-file,global,include-contents');
    }
    function v6_expand_ContentsCaption() {
        return $this->parent->xml_value('hotpot-config-file,global,contents-caption');
    }
    function v6_expand_GuessCorrect() {
        return $this->js_value('hotpot-config-file,'.$this->parent->quiztype.',guess-correct');
    }
    function v6_expand_GuessIncorrect() {
        return $this->js_value('hotpot-config-file,'.$this->parent->quiztype.',guess-incorrect');
    }
    function v6_expand_Hint() {
        return $this->int_value('hotpot-config-file,'.$this->parent->quiztype.',include-hint');
    }
    function v6_expand_HintCaption() {
        return $this->parent->xml_value('hotpot-config-file,global,hint-caption');
    }
    function v6_expand_IncorrectIndicator() {
        return $this->js_value('hotpot-config-file,global,incorrect-indicator');
    }
    function v6_expand_LastQCaption() {
110
111
        $caption = $this->parent->xml_value('hotpot-config-file,global,last-q-caption');
        return ($caption=='<=' ? '&lt;=' : $caption);
112
113
114
115
116
117
118
119
120
121
122
123
    }
    function v6_expand_NextCorrect() {
        $value = $this->js_value('hotpot-config-file,'.$this->parent->quiztype.',next-correct-part');
        if (empty($value)) { // jquiz
            $value = $this->js_value('hotpot-config-file,'.$this->parent->quiztype.',next-correct-letter');
        }
        return $value;
    }
    function v6_expand_NextEx() {
        return $this->int_value('hotpot-config-file,global,include-next-ex');
    }
    function v6_expand_NextExCaption() {
124
        return str_replace('=>', '=&gt;', $this->parent->xml_value('hotpot-config-file,global,next-ex-caption'));
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
    }
    function v6_expand_NextQCaption() {
        return $this->parent->xml_value('hotpot-config-file,global,next-q-caption');
    }
    function v6_expand_OKCaption() {
        return $this->parent->xml_value('hotpot-config-file,global,ok-caption');
    }
    function v6_expand_Restart() {
        return $this->int_value('hotpot-config-file,'.$this->parent->quiztype.',include-restart');
    }
    function v6_expand_RestartCaption() {
        return $this->parent->xml_value('hotpot-config-file,global,restart-caption');
    }
    function v6_expand_ShowAllQuestionsCaption() {
        return $this->js_value('hotpot-config-file,global,show-all-questions-caption');
    }
    function v6_expand_ShowOneByOneCaption() {
        return $this->js_value('hotpot-config-file,global,show-one-by-one-caption');
    }
    function v6_expand_TheseAnswersToo() {
        return $this->js_value('hotpot-config-file,'.$this->parent->quiztype.',also-correct');
    }
    function v6_expand_ThisMuch() {
        return $this->js_value('hotpot-config-file,'.$this->parent->quiztype.',this-much-correct');
    }
    function v6_expand_Undo() {
        return $this->int_value('hotpot-config-file,'.$this->parent->quiztype.',include-undo');
    }
    function v6_expand_UndoCaption() {
        return $this->parent->xml_value('hotpot-config-file,global,undo-caption');
    }
    function v6_expand_YourScoreIs() {
        return $this->js_value('hotpot-config-file,global,your-score-is');
    }

    // reading

    function v6_expand_Reading() {
        return $this->int_value('data,reading,include-reading');
    }
    function v6_expand_ReadingText() {
        $title = $this->v6_expand_ReadingTitle();
        $value = $this->parent->xml_value('data,reading,reading-text');
        $value = empty($value) ? '' : ('<div class="ReadingText">'.$value.'</div>');
        return $title.$value;
    }
    function v6_expand_ReadingTitle() {
        $value = $this->parent->xml_value('data,reading,reading-title');
        return empty($value) ? '' : ('<h3 class="ExerciseSubtitle">'.$value.'</h3>');
    }

176
    // timer
177
178
179
180
181
182
183
184
185
186
187

    function v6_expand_Timer() {
        return $this->int_value('data,timer,include-timer');
    }
    function v6_expand_JSTimer() {
        return $this->read_template('hp6timer.js_');
    }
    function v6_expand_Seconds() {
        return $this->parent->xml_value('data,timer,seconds');
    }

188
    // send results
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

    function v6_expand_SendResults() {
        return $this->parent->xml_value('hotpot-config-file,'.$this->parent->quiztype.',send-email');
    }
    function v6_expand_JSSendResults() {
        return $this->read_template('hp6sendresults.js_');
    }
    function v6_expand_FormMailURL() {
        return $this->parent->xml_value('hotpot-config-file,global,formmail-url');
    }
    function v6_expand_EMail() {
        return $this->parent->xml_value('hotpot-config-file,global,email');
    }
    function v6_expand_NamePlease() {
        return $this->js_value('hotpot-config-file,global,name-please');
    }

    // preload images

    function v6_expand_PreloadImages() {
        $value = $this->v6_expand_PreloadImageList();
        return empty($value) ? false : true;
    }
    function v6_expand_PreloadImageList() {

        // check it has not been set already
        if (!isset($this->PreloadImageList)) {

            // the list of image urls
            $list = array();

            // extract <img> tags
            $img_tag = htmlspecialchars('|&#x003C;img.*?src="(.*?)".*?&#x003E;|is');
            if (preg_match_all($img_tag, $this->parent->source, $matches)) {
                $list = $matches[1];

                // remove duplicates
                $list = array_unique($list);
            }

            // convert to comma delimited string
230
            $this->PreloadImageList = empty($list) ? '' : "'".implode("','", $list)."'";
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
        }
        return $this->PreloadImageList;
    }

    // html files (all quiz types)

    function v6_expand_PlainTitle() {
        return $this->parent->xml_value('data,title');
    }
    function v6_expand_ExerciseSubtitle() {
        return $this->parent->xml_value('hotpot-config-file,'.$this->parent->quiztype.',exercise-subtitle');
    }
    function v6_expand_Instructions() {
        return $this->parent->xml_value('hotpot-config-file,'.$this->parent->quiztype.',instructions');
    }
    function v6_expand_DublinCoreMetadata() {
        $dc = '<link rel="schema.DC" href="'.$this->parent->xml_value('', "['rdf:RDF'][0]['@']['xmlns:dc']").'" />'."\n";
        if (is_string($this->parent->xml_value('rdf:RDF,rdf:Description'))) {
            // do nothing (there is no more dc info)
        } else {
            $dc .= '<meta name="DC:Creator" content="'.$this->parent->xml_value('rdf:RDF,rdf:Description,dc:creator').'" />'."\n";
            $dc .= '<meta name="DC:Title" content="'.strip_tags($this->parent->xml_value('rdf:RDF,rdf:Description,dc:title')).'" />'."\n";
        }
        return $dc;
    }
    function v6_expand_FullVersionInfo() {
        global $CFG;
        require_once($CFG->hotpotroot.DIRECTORY_SEPARATOR.'version.php'); // set $module
        return $this->parent->xml_value('version').'.x (Moodle '.$CFG->release.', hotpot-module '.$this->parent->obj_value($module, 'release').')';
    }
    function v6_expand_HeaderCode() {
        return $this->parent->xml_value('hotpot-config-file,global,header-code');
    }
    function v6_expand_StyleSheet() {
        $this->read_template('hp6.cs_', 'css');
        $this->css = hotpot_convert_stylesheets_urls($this->parent->get_baseurl(), $this->parent->reference, $this->css);
        return $this->css;
    }

    // stylesheet (hp6.cs_)

    function v6_expand_PageBGColor() {
        return $this->parent->xml_value('hotpot-config-file,global,page-bg-color');
    }
    function v6_expand_GraphicURL() {
        return $this->parent->xml_value('hotpot-config-file,global,graphic-url');
    }
    function v6_expand_ExBGColor() {
        return $this->parent->xml_value('hotpot-config-file,global,ex-bg-color');
    }

    function v6_expand_FontFace() {
        return $this->parent->xml_value('hotpot-config-file,global,font-face');
    }
    function v6_expand_FontSize() {
286
287
        $value = $this->parent->xml_value('hotpot-config-file,global,font-size');
        return (empty($value) ? 'small' : $value);
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    }
    function v6_expand_TextColor() {
        return $this->parent->xml_value('hotpot-config-file,global,text-color');
    }
    function v6_expand_TitleColor() {
        return $this->parent->xml_value('hotpot-config-file,global,title-color');
    }
    function v6_expand_LinkColor() {
        return $this->parent->xml_value('hotpot-config-file,global,link-color');
    }
    function v6_expand_VLinkColor() {
        return $this->parent->xml_value('hotpot-config-file,global,vlink-color');
    }

    function v6_expand_NavTextColor() {
        return $this->parent->xml_value('hotpot-config-file,global,page-bg-color');
    }
    function v6_expand_NavBarColor() {
        return $this->parent->xml_value('hotpot-config-file,global,nav-bar-color');
    }
    function v6_expand_NavLightColor() {
        $color = $this->parent->xml_value('hotpot-config-file,global,nav-bar-color');
310
        return $this->get_halfway_color($color, '#ffffff');
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
    }
    function v6_expand_NavShadeColor() {
        $color = $this->parent->xml_value('hotpot-config-file,global,nav-bar-color');
        return $this->get_halfway_color($color, '#000000');
    }

    function v6_expand_FuncLightColor() { // top-left of buttons
        $color = $this->parent->xml_value('hotpot-config-file,global,ex-bg-color');
        return $this->get_halfway_color($color, '#ffffff');
    }
    function v6_expand_FuncShadeColor() { // bottom right of buttons
        $color = $this->parent->xml_value('hotpot-config-file,global,ex-bg-color');
        return $this->get_halfway_color($color, '#000000');
    }

    // navigation buttons

    function v6_expand_NavButtons() {
        $back = $this->v6_expand_Back();
        $next_ex = $this->v6_expand_NextEx();
        $contents = $this->v6_expand_Contents();
        return (empty($back) && empty($next_ex) && empty($contents) ? false : true);
    }
    function v6_expand_NavBarJS() {
        return $this->v6_expand_NavButtons();
    }
337
338

    // switch off scorm
339
340
341
    function v6_expand_Scorm12() {
        return false;
    }
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

    // js files (all quiz types)

    function v6_expand_JSBrowserCheck() {
        return $this->read_template('hp6browsercheck.js_');
    }
    function v6_expand_JSButtons() {
        return $this->read_template('hp6buttons.js_');
    }
    function v6_expand_JSCard() {
        return $this->read_template('hp6card.js_');
    }
    function v6_expand_JSCheckShortAnswer() {
        return $this->read_template('hp6checkshortanswer.js_');
    }
    function v6_expand_JSHotPotNet() {
        return $this->read_template('hp6hotpotnet.js_');
    }
    function v6_expand_JSShowMessage() {
        return $this->read_template('hp6showmessage.js_');
    }
    function v6_expand_JSUtilities() {
        return $this->read_template('hp6utilities.js_');
    }

    // js files

    function v6_expand_JSJCloze6() {
        return $this->read_template('jcloze6.js_');
    }
    function v6_expand_JSJCross6() {
        return $this->read_template('jcross6.js_');
    }
    function v6_expand_JSJMatch6() {
        return $this->read_template('jmatch6.js_');
    }
    function v6_expand_JSJMix6() {
        return $this->read_template('jmix6.js_');
    }
    function v6_expand_JSJQuiz6() {
        return $this->read_template('jquiz6.js_');
    }

    // drag and drop

    function v6_expand_JSDJMatch6() {
        return $this->read_template('djmatch6.js_');
    }
    function v6_expand_JSDJMix6() {
        return $this->read_template('djmix6.js_');
    }

    // what are these for?

    function v6_expand_JSFJMatch6() {
        return $this->read_template('fjmatch6.js_');
    }
    function v6_expand_JSFJMix6() {
        return $this->read_template('fjmix6.js_');
    }

    // jmatch6.js_

    function v6_expand_ShuffleQs() {
        return $this->bool_value('hotpot-config-file,'.$this->parent->quiztype.',shuffle-questions');
    }
    function v6_expand_QsToShow() {
        $i = $this->parent->xml_value('hotpot-config-file,'.$this->parent->quiztype.',show-limited-questions');
        if ($i) {
            $i = $this->parent->xml_value('hotpot-config-file,'.$this->parent->quiztype.',questions-to-show');
        }
        if (empty($i)) {
            $i = 0;
            switch ($this->parent->quiztype) {
                case 'jmatch':
                    $values = $this->parent->xml_values('data,matching-exercise,pair');
                    $i = count($values);
                    break;
                case 'jquiz':
421
422
                    $tags = 'data,questions,question-record';
                    while (($question="[$i]['#']") && $this->parent->xml_value($tags, $question)) {
423
424
425
426
427
428
429
430
                        $i++;
                    }
                    break;
            } // end switch
        }
        return $i;
    }
    function v6_expand_MatchDivItems() {
431
        $this->set_jmatch_items();
432

433
434
        $l_keys = $this->shuffle_jmatch_items($this->l_items);
        $r_keys = $this->shuffle_jmatch_items($this->r_items);
435
436
437

        $options = '<option value="x">'.$this->parent->xml_value('data,matching-exercise,default-right-item').'</option>';
        foreach ($r_keys as $key) {
438
439
            if (! $this->r_items[$key]['fixed']) {
                $options .= '<option value="'.$key.'">'.$this->r_items[$key]['text'].'</option>'."\n";
440
441
442
443
444
            }
        }

        $str = '';
        foreach ($l_keys as $key) {
445
            $str .= '<tr><td class="LeftItem">'.$this->l_items[$key]['text'].'</td>';
446
            $str .= '<td class="RightItem">';
447
448
            if ($this->r_items[$key]['fixed']) {
                $str .= $this->r_items[$key]['text'];
449
            }  else {
450
                $str .= '<select id="s'.$key.'_'.$key.'">'.$options.'</select>';
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
            }
            $str .= '</td><td></td></tr>';
        }
        return $str;
    }

    // jmix6.js_

    function v6_expand_Punctuation() {
        $tags = 'data,jumbled-order-exercise';
        $chars = array_merge(
            $this->jmix_Punctuation("$tags,main-order,segment"),
            $this->jmix_Punctuation("$tags,alternate")
        );
        $chars = array_unique($chars);
        $chars = implode('', $chars);
        $chars = $this->js_safe($chars, true);
        return $chars;
    }
    function jmix_Punctuation($tags) {
        $chars = array();

        // all punctutation except '&#;' (because they are used in html entities)
        $ENTITIES = $this->jmix_encode_punctuation('!"$%'."'".'()*+,-./:<=>?@[\]^_`{|}~');
        $pattern = "/&#x([0-9A-F]+);/i";
        $i = 0;

        // get next segment (or alternate answer)
        while ($value = $this->parent->xml_value($tags, "[$i]['#']")) {

            // convert low-ascii punctuation to entities
            $value = strtr($value, $ENTITIES);

            // extract all hex HTML entities
            if (preg_match_all($pattern, $value, $matches)) {

                // loop through hex entities
                $m_max = count($matches[0]);
                for ($m=0; $m<$m_max; $m++) {

                    // convert to hex number
                    eval('$hex=0x'.$matches[1][$m].';');

                    // is this a punctuation character?
                    if (
                        ($hex>=0x0020 && $hex<=0x00BF) || // ascii punctuation
                        ($hex>=0x2000 && $hex<=0x206F) || // general punctuation
                        ($hex>=0x3000 && $hex<=0x303F) || // CJK punctuation
                        ($hex>=0xFE30 && $hex<=0xFE4F) || // CJK compatability
                        ($hex>=0xFE50 && $hex<=0xFE6F) || // small form variants
                        ($hex>=0xFF00 && $hex<=0xFF40) || // halfwidth and fullwidth forms (1)
                        ($hex>=0xFF5B && $hex<=0xFF65) || // halfwidth and fullwidth forms (2)
                        ($hex>=0xFFE0 && $hex<=0xFFEE)    // halfwidth and fullwidth forms (3)
                    ) {
                        // add this character
                        $chars[] = $matches[0][$m];
                    }
                }
            }
            $i++;
        }

        return $chars;
    }
    function v6_expand_OpenPunctuation() {
        $tags = 'data,jumbled-order-exercise';
        $chars = array_merge(
            $this->jmix_OpenPunctuation("$tags,main-order,segment"),
            $this->jmix_OpenPunctuation("$tags,alternate")
        );
        $chars = array_unique($chars);
        $chars = implode('', $chars);
        $chars = $this->js_safe($chars, true);
        return $chars;
    }
    function jmix_OpenPunctuation($tags) {
        $chars = array();

        // unicode punctuation designations (pi="initial quote", ps="open")
530
531
        //  http://www.sql-und-xml.de/unicode-database/pi.html
        //  http://www.sql-und-xml.de/unicode-database/ps.html
532
533
534
535
536
537
538
539
540
541
542
        $pi = '0022|0027|00AB|2018|201B|201C|201F|2039';
        $ps = '0028|005B|007B|0F3A|0F3C|169B|201A|201E|2045|207D|208D|2329|23B4|2768|276A|276C|276E|2770|2772|2774|27E6|27E8|27EA|2983|2985|2987|2989|298B|298D|298F|2991|2993|2995|2997|29D8|29DA|29FC|3008|300A|300C|300E|3010|3014|3016|3018|301A|301D|FD3E|FE35|FE37|FE39|FE3B|FE3D|FE3F|FE41|FE43|FE47|FE59|FE5B|FE5D|FF08|FF3B|FF5B|FF5F|FF62';
        $pattern = "/(&#x($pi|$ps);)/i";

        $ENTITIES = $this->jmix_encode_punctuation('"'."'".'(<[{');

        $i = 0;
        while ($value = $this->parent->xml_value($tags, "[$i]['#']")) {
            $value = strtr($value, $ENTITIES);
            if (preg_match_all($pattern, $value, $matches)) {
                $chars = array_merge($chars, $matches[0]);
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
            $i++;
        }

        return $chars;
    }
    function jmix_encode_punctuation($str) {
        $ENTITIES = array();
        $i_max = strlen($str);
        for ($i=0; $i<$i_max; $i++) {
            $ENTITIES[$str{$i}] = '&#x'.sprintf('%04X', ord($str{$i})).';';
        }
        return $ENTITIES;
    }
    function v6_expand_ExerciseTitle() {
        return $this->parent->xml_value('data,title');
    }

    // Jmix specials

    function v6_expand_SegmentArray() {

        $segments = array();
        $values = array();
        $VALUES = array();

        // XML tags to the start of a segment
        $tags = 'data,jumbled-order-exercise,main-order,segment';

        $i = 0;
        while ($value = $this->parent->xml_value($tags, "[$i]['#']")) {
            $VALUE = strtoupper($value);
            $key = array_search($VALUE, $VALUES);
            if (is_numeric($key)) {
                $segments[] = $key;
            } else {
                $segments[] = $i;
                $values[$i] = $value;
                $VALUES[$i] = $VALUE;
            }
            $i++;
        }

        $this->seed_random_number_generator();
        $keys = array_keys($segments);
        shuffle($keys);

        $str = '';
        for($i=0; $i<count($keys); $i++) {
            $key = $segments[$keys[$i]];
            $str .= "Segments[$i] = new Array();\n";
            $str .= "Segments[$i][0] = '".$this->js_safe($values[$key], true)."';\n";
            $str .= "Segments[$i][1] = ".($key+1).";\n";
            $str .= "Segments[$i][2] = 0;\n";
        }
        return $str;
    }
    function v6_expand_AnswerArray() {

        $segments = array();
        $values = array();
        $VALUES = array();
        $escapedvalues = array();

        // XML tags to the start of a segment
        $tags = 'data,jumbled-order-exercise,main-order,segment';

        $i = 0;
        while ($value = $this->parent->xml_value($tags, "[$i]['#']")) {
            $VALUE = strtoupper($value);
            $key = array_search($VALUE, $VALUES);
            if (is_numeric($key)) {
                $segments[] = $key+1;
            } else {
                $segments[] = $i+1;
                $values[$i] = $value;
                $VALUES[$i] = $VALUE;
                $escapedvalues[] = preg_quote($value, '/');
            }
            $i++;
        }

        // start the answers array
        $a = 0;
        $str = 'Answers['.($a++).'] = new Array('.implode(',', $segments).");\n";

        // pattern to match the next part of an alternate answer
        $pattern = '/^('.implode('|', $escapedvalues).')\\s*/i';

        // XML tags to the start of an alternate answer
        $tags = 'data,jumbled-order-exercise,alternate';

        $i = 0;
        while ($value = $this->parent->xml_value($tags, "[$i]['#']")) {
            $segments = array();
            while (strlen($value) && preg_match($pattern, $value, $matches)) {
                $key = array_search($matches[1], $values);
                if (is_numeric($key)) {
                    $segments[] = $key+1;
                    $value = substr($value, strlen($matches[0]));
                } else {
                    // invalid alternate sequence
                    $segments = array();
                    break;
                }
            }
            if (count($segments)) {
                $str .= 'Answers['.($a++).'] = new Array('.implode(',', $segments).");\n";
            }
            $i++;
        }
        return $str;
    }

    // ===============================================================

    // JMix (jmix6.js_)

    function v6_expand_RemainingWords() {
        return $this->parent->xml_value('hotpot-config-file,'.$this->parent->quiztype.',remaining-words');
    }
    function v6_expand_TimesUp() {
665
        return $this->js_value('hotpot-config-file,global,times-up');
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
    }

    // nav bar

    function v6_expand_NavBar($navbarid='') {
        $this->navbarid = $navbarid;

        $tag = 'navbar';
        $this->read_template('hp6navbar.ht_', $tag);

        unset($this->navbarid);

        return $this->$tag;
    }
    function v6_expand_TopNavBar() {
        return $this->v6_expand_NavBar('TopNavBar');
    }
    function v6_expand_BottomNavBar() {
        return $this->v6_expand_NavBar('BottomNavBar');
    }

    // hp6navbar.ht_

    function v6_expand_NavBarID() {
        // $this->navbarid is set in "$this->v6_expand_NavBar"
        return empty($this->navbarid) ? '' : $this->navbarid;
    }
    function v6_expand_ContentsURL() {
        $url = $this->parent->xml_value('hotpot-config-file,global,contents-url');
        if ($url) {
            $url = hotpot_convert_navbutton_url($this->parent->get_baseurl(), $this->parent->reference, $url, $this->parent->course);
        }
        return $url;
    }
    function v6_expand_NextExURL() {
        $url = $this->parent->xml_value('hotpot-config-file,'.$this->parent->quiztype.',next-ex-url');
        if ($url) {
            $url = hotpot_convert_navbutton_url($this->parent->get_baseurl(), $this->parent->reference, $url, $this->parent->course);
        }
        return $url;
    }

    // conditional blocks

    function v6_expand_ShowAnswer() {
        return $this->int_value('hotpot-config-file,'.$this->parent->quiztype.',include-show-answer');
    }
    function v6_expand_Slide() {
        return true; // whats's this (JMatch drag and drop)
    }

    // specials (JMatch)

    function v6_expand_FixedArray() {
720
        $this->set_jmatch_items();
721
        $str = '';
722
        foreach ($this->l_items as $i=>$item) {
723
724
725
726
727
            for ($ii=0; $ii<$i; $ii++) {
                if ($this->r_items[$ii]['text']==$this->r_items[$i]['text']) {
                    break;
                }
            }
728
            $str .= "F[$i] = new Array();\n";
729
            $str .= "F[$i][0] = '".$this->js_safe($item['text'], true)."';\n";
730
            $str .= "F[$i][1] = ".($ii+1).";\n";
731
732
733
734
        }
        return $str;
    }
    function v6_expand_DragArray() {
735
        $this->set_jmatch_items();
736
        $str = '';
737
        foreach ($this->r_items as $i=>$item) {
738
739
740
741
742
            for ($ii=0; $ii<$i; $ii++) {
                if ($this->r_items[$ii]['text']==$this->r_items[$i]['text']) {
                    break;
                }
            }
743
            $str .= "D[$i] = new Array();\n";
744
            $str .= "D[$i][0] = '".$this->js_safe($item['text'], true)."';\n";
745
746
            $str .= "D[$i][1] = ".($ii+1).";\n";
            $str .= "D[$i][2] = ".$item['fixed'].";\n";
747
        }
748
749
750
        return $str;
    }

751
    function set_jmatch_items() {
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
        if (count($this->l_items)) {
            return;
        }
        $tags = 'data,matching-exercise,pair';
        $i = 0;
        while (($item = "[$i]['#']") && $this->parent->xml_value($tags, $item)) {
            $leftitem = $item."['left-item'][0]['#']";
            $lefttext = $this->parent->xml_value($tags, $leftitem."['text'][0]['#']");

            $rightitem = $item."['right-item'][0]['#']";
            $righttext = $this->parent->xml_value($tags, $rightitem."['text'][0]['#']");

            if (strlen($righttext)) {
                $addright = true;
            } else {
                $addright = false;
            }
            if (strlen($lefttext)) {
                $this->l_items[] = array(
                    'text' => $lefttext,
                    'fixed' => $this->int_value($tags, $leftitem."['fixed'][0]['#']")
                );
                $addright = true; // force right item to be added
            }
            if ($addright) {
                $this->r_items[] = array(
                    'text' => $righttext,
                    'fixed' => $this->int_value($tags, $rightitem."['fixed'][0]['#']")
                );
781
            }
782
            $i++;
783
784
785
786
787
788
        }
    }
    function shuffle_jmatch_items(&$items) {
        // get moveable items
        $moveable_keys = array();
        for($i=0; $i<count($items); $i++) {
789
            if(empty($items[$i]['fixed'])) {
790
791
792
793
794
795
796
797
798
                $moveable_keys[] = $i;
            }
        }
        // shuffle moveable items
        $this->seed_random_number_generator();
        shuffle($moveable_keys);

        $keys = array();
        for($i=0, $ii=0; $i<count($items); $i++) {
799
800
            if(empty($items[$i]['fixed'])) {
                //  moveable items are inserted in a shuffled order
801
802
                $keys[] = $moveable_keys[$ii++];
            } else {
803
                //  fixed items stay where they are
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
                $keys[] = $i;
            }
        }
        return $keys;
    }
    function seed_random_number_generator() {
        static $seeded_RNG = FALSE;
        if (!$seeded_RNG) {
            srand((double) microtime() * 1000000);
            $seeded_RNG = TRUE;
        }
    }

    // specials (JMix)


    // specials (JCloze)

    function v6_expand_ItemArray() {
        $q = 0;
        $str = '';
        switch ($this->parent->quiztype) {
            case 'jcloze':
                $tags = 'data,gap-fill,question-record';
                while (($question="[$q]['#']") && $this->parent->xml_value($tags, $question)) {
                    $a = 0;
                    $aa = 0;
                    while (($answer=$question."['answer'][$a]['#']") && $this->parent->xml_value($tags, $answer)) {
832
                        $text = $this->js_value($tags,  $answer."['text'][0]['#']");
833
834
835
836
837
838
839
840
841
842
843
844
845
846
                        if (strlen($text)) {
                            if ($aa==0) { // first time only
                                $str .= "\n";
                                $str .= "I[$q] = new Array();\n";
                                $str .= "I[$q][1] = new Array();\n";
                            }
                            $str .= "I[$q][1][$aa] = new Array();\n";
                            $str .= "I[$q][1][$aa][0] = '$text';\n";
                            $aa++;
                        }
                        $a++;
                    }
                    // add clue, if any answers were found
                    if ($aa) {
847
                        $clue = $this->js_value($tags, $question."['clue'][0]['#']");
848
849
850
851
852
853
854
855
856
857
858
859
                        $str .= "I[$q][2] = '$clue';\n";
                    }
                    $q++;
                }
                break;
            case 'jquiz':
                $str .= "I=new Array();\n";
                $tags = 'data,questions,question-record';
                while (($question="[$q]['#']") && $this->parent->xml_value($tags, $question)) {

                    $question_type = $this->int_value($tags, $question."['question-type'][0]['#']");
                    $weighting = $this->int_value($tags, $question."['weighting'][0]['#']");
860
                    $clue = $this->js_value($tags, $question."['clue'][0]['#']");
861
862
863
864
865
866

                    $answers = $question."['answers'][0]['#']";

                    $a = 0;
                    $aa = 0;
                    while (($answer = $answers."['answer'][$a]['#']") && $this->parent->xml_value($tags, $answer)) {
867
868
                        $text =     $this->js_value($tags,  $answer."['text'][0]['#']");
                        $feedback = $this->js_value($tags,  $answer."['feedback'][0]['#']");
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
                        $correct =  $this->int_value($tags, $answer."['correct'][0]['#']");
                        $percent =  $this->int_value($tags, $answer."['percent-correct'][0]['#']");
                        $include =  $this->int_value($tags, $answer."['include-in-mc-options'][0]['#']");
                        if (strlen($text)) {
                            if ($aa==0) { // first time only
                                $str .= "\n";
                                $str .= "I[$q] = new Array();\n";
                                $str .= "I[$q][0] = $weighting;\n";
                                $str .= "I[$q][1] = '$clue';\n";
                                $str .= "I[$q][2] = '".($question_type-1)."';\n";
                                $str .= "I[$q][3] = new Array();\n";
                            }
                            $str .= "I[$q][3][$aa] = new Array('$text','$feedback',$correct,$percent,$include);\n";
                            $aa++;
                        }
                        $a++;
                    }
                    $q++;
                }
                break;
        }
        return $str;
    }

    function v6_expand_ClozeBody() {
        $str = '';

        // get drop down list of words, if required
        $dropdownlist = '';
        if ($this->v6_use_DropDownList()) {
            $this->v6_set_WordList();
            foreach ($this->wordlist as $word) {
                $dropdownlist .= '<option value="'.$word.'">'.$word.'</option>';
            }
        }

        // cache clues flag and caption
        $includeclues = $this->v6_expand_Clues();
        $cluecaption = $this->v6_expand_ClueCaption();

        // detect if cloze starts with gap
        $strpos = strpos($this->parent->source, '<gap-fill><question-record>');
        if (is_numeric($strpos)) {
            $startwithgap = true;
        } else {
            $startwithgap = false;
        }

        // initialize loop values
        $q = 0;
        $tags = 'data,gap-fill';
        $question_record = "$tags,question-record";

        // loop through text and gaps
        do {
            $text = $this->parent->xml_value($tags, "[0]['#'][$q]");
            $gap = '';
            if (($question="[$q]['#']") && $this->parent->xml_value($question_record, $question)) {
                $gap .= '<span class="GapSpan" id="GapSpan'.$q.'">';
                if ($this->v6_use_DropDownList()) {
                    $gap .= '<select id="Gap'.$q.'"><option value=""></option>'.$dropdownlist.'</select>';
                } else {
                    // minimum gap size
932
                    if (! $gapsize = $this->int_value('hotpot-config-file,'.$this->parent->quiztype.',minimum-gap-size')) {
933
934
                        $gapsize = 6;
                    }
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

                    // increase gap size to length of longest answer for this gap
                    $a = 0;
                    while (($answer=$question."['answer'][$a]['#']") && $this->parent->xml_value($question_record, $answer)) {
                        $answertext = $this->parent->xml_value($question_record,  $answer."['text'][0]['#']");
                        $answertext = preg_replace('|&[#a-zA-Z0-9]+;|', 'x', $answertext);
                        $gapsize = max($gapsize, strlen($answertext));
                        $a++;
                    }

                    $gap .= '<input type="text" id="Gap'.$q.'" onfocus="TrackFocus('.$q.')" onblur="LeaveGap()" class="GapBox" size="'.$gapsize.'"></input>';
                }
                if ($includeclues) {
                    $clue = $this->parent->xml_value($question_record, $question."['clue'][0]['#']");
                    if (strlen($clue)) {
                        $gap .= '<button style="line-height: 1.0" class="FuncButton" onfocus="FuncBtnOver(this)" onmouseover="FuncBtnOver(this)" onblur="FuncBtnOut(this)" onmouseout="FuncBtnOut(this)" onmousedown="FuncBtnDown(this)" onmouseup="FuncBtnOut(this)" onclick="ShowClue('.$q.')">'.$cluecaption.'</button>';
                    }
                }
                $gap .= '</span>';
            }
            if ($startwithgap) {
                $str .= "$gap$text";
            } else {
                $str .= "$text$gap";
            }
            $q++;
        } while (strlen($text) || strlen($gap));

        return $str;
    }

    // JCloze quiztype

    function v6_expand_WordList() {
        $str = '';
        if ($this->v6_include_WordList()) {
            $this->v6_set_WordList();
            $str = implode(' &#160;&#160; ', $this->wordlist);
        }
        return $str;
    }
    function v6_include_WordList() {
        return $this->int_value('hotpot-config-file,'.$this->parent->quiztype.',include-word-list');
    }
    function v6_use_DropDownList() {
        return $this->int_value('hotpot-config-file,'.$this->parent->quiztype.',use-drop-down-list');
    }
    function v6_set_WordList() {

        if (isset($this->wordlist)) {
            // do nothing
        } else {
            $this->wordlist = array();

            // is the wordlist required
            if ($this->v6_include_WordList() || $this->v6_use_DropDownList()) {

                $q = 0;
                $tags = 'data,gap-fill,question-record';
                while (($question="[$q]['#']") && $this->parent->xml_value($tags, $question)) {
                    $a = 0;
                    $aa = 0;
                    while (($answer=$question."['answer'][$a]['#']") && $this->parent->xml_value($tags, $answer)) {
                        $text = $this->parent->xml_value($tags,  $answer."['text'][0]['#']");
                        $correct =  $this->int_value($tags, $answer."['correct'][0]['#']");
                        if ($text && $correct) { // $correct is always true
For faster browsing, not all history is shown. View entire blame