forked from bdaloukas/moodle-mod_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporthtml_millionaire.php
executable file
·528 lines (434 loc) · 17.5 KB
/
exporthtml_millionaire.php
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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This page export the game millionaire to html
*
* @author bdaloukas
* @version $Id: exporthtml_millionaire.php,v 1.14 2012/07/25 11:16:03 bdaloukas Exp $
* @package game
**/
function game_millionaire_html_getquestions( $game, $context, &$maxanswers, &$countofquestions, &$retfeedback, $destdir, &$files) {
global $CFG, $DB, $USER;
$maxanswers = 0;
$countofquestions = 0;
$files = array();
if ( ($game->sourcemodule != 'quiz') and ($game->sourcemodule != 'question')) {
print_error( get_string('millionaire_sourcemodule_must_quiz_question', 'game', get_string( 'modulename', 'quiz')).
' '.get_string( 'modulename', $game->sourcemodule));
}
if ( $game->sourcemodule == 'quiz') {
if ( $game->quizid == 0) {
print_error( get_string( 'must_select_quiz', 'game'));
}
$select = "qtype='multichoice' AND quiz='$game->quizid' ".
" AND qqi.question=q.id";
$table = "{question} q,{quiz_question_instances} qqi";
} else {
if ( $game->questioncategoryid == 0) {
print_error( get_string( 'must_select_questioncategory', 'game'));
}
// Include subcategories.
$select = 'category='.$game->questioncategoryid;
if ( $game->subcategories) {
$cats = question_categorylist( $game->questioncategoryid);
if (strpos( $cats, ',') > 0) {
$select = 'category in ('.$cats.')';
}
}
$select .= " AND qtype='multichoice'";
$table = "{question} q";
}
$select .= " AND q.hidden=0";
$sql = "SELECT q.id as id, q.questiontext FROM $table WHERE $select";
$recs = $DB->get_records_sql( $sql);
$ret = '';
$retfeedback = '';
foreach ($recs as $rec) {
$recs2 = $DB->get_records( 'question_answers', array( 'question' => $rec->id), 'fraction DESC', 'id,answer,feedback');
// Must parse the questiontext and get the name of files.
$line = $rec->questiontext;
$line = game_export_split_files( $game->course, $context, 'questiontext', $rec->id, $rec->questiontext, $destdir, $files);
$linefeedback = '';
foreach ($recs2 as $rec2) {
$line .= '#'.str_replace( array( '"', '#'), array( "'", ' '),
game_export_split_files( $game->course, $context, 'answer', $rec2->id, $rec2->answer, $destdir, $files));
$linefeedback .= '#'.str_replace( array( '"', '#'), array( "'", ' '), $rec2->feedback);
}
if ( $ret != '') {
$ret .= ",\r";
}
$ret .= '"'.base64_encode( $line).'"';
if ( $retfeedback != '') {
$retfeedback .= ",\r";
}
$retfeedback .= '"'.base64_encode( $linefeedback).'"';
if ( count( $recs2) > $maxanswers) {
$maxanswers = count( $recs2);
}
$countofquestions++;
}
return $ret;
}
function game_millionaire_html_print( $game, $questions, $maxquestions) {
$color1 = 'black';
$color2 = 'DarkOrange';
$colorback = "white";
$stylequestion = "background:$colorback;color:$color1";
$stylequestionselected = "background:$colorback;color:$color2";
?>
<body onload="Reset();">
<script type="text/javascript">
// Millionaire for Moodle by Vasilis Daloukas.
<?php echo 'var questions = new Array('.$questions.");\r"; ?>
var current_question = 0;
var level = 0;
var posCorrect = 0;
var infoCorrect = "";
var flag5050 = 0;
var flagTelephone = 0;
var flagPeople = 0;
var countQuestions = 0;
var maxQuestions = <?php echo $maxquestions;?>;
function Highlite( ans) {
document.getElementById( "btAnswer" + ans).style.backgroundColor = '<?php echo $color2;?>';
}
function Restore( ans) {
document.getElementById( "btAnswer" + ans).style.backgroundColor = '<?php echo $colorback;?>';
}
function OnSelectAnswer( ans) {
if ( posCorrect == ans) {
if( level+1 > 15) {
alert( "<?php echo get_string( 'win', 'game');?>");
Reset();
} else {
UpdateLevel( level+1);
SelectNextQuestion();
}
} else {
OnGameOver( ans);
}
}
function OnGameOver( ans) {
document.getElementById( "info").innerHTML = "<?php echo get_string( 'millionaire_info_wrong_answer', 'game');?> " +
document.getElementById( "lblAnswer" + posCorrect).innerHTML;
Highlite( posCorrect);
Restore( ans);
document.getElementById( "lblAnswer" + posCorrect).style.backgroundColor = '<?php echo $color2;?>';
alert( "<?php echo strip_tags( get_string( 'hangman_loose', 'game')); ?>");
Restore( posCorrect);
document.getElementById( "lblAnswer" + posCorrect).style.backgroundColor = '<?php echo $colorback;?>';
Reset();
}
function UpdateLevel( newlevel) {
if ( level > 0) {
document.getElementById( "levela" + level).bgColor = "<?php echo $colorback;?>";
document.getElementById( "levelb" + level).bgColor = "<?php echo $colorback;?>";
document.getElementById( "levelc" + level).bgColor = "<?php echo $colorback;?>";
document.getElementById( "levela" + level).style.color = "<?php echo $color1;?>";
document.getElementById( "levelb" + level).style.color = "<?php echo $color1;?>";
document.getElementById( "levelc" + level).style.color = "<?php echo $color1;?>";
}
level = newlevel;
document.getElementById( "levela" + level).bgColor = "<?php echo $color2;?>";
document.getElementById( "levelb" + level).bgColor = "<?php echo $color2;?>";
document.getElementById( "levelc" + level).bgColor = "<?php echo $color2;?>";
document.getElementById( "levela" + level).style.color = "<?php echo $colorback;?>";
document.getElementById( "levelb" + level).style.color = "<?php echo $colorback;?>";
document.getElementById( "levelc" + level).style.color = "<?php echo $colorback;?>";
}
function OnHelp5050( ans) {
if (flag5050) {
return;
}
document.getElementById( "Help5050").src = "5050x.png";
flag5050 = 1;
for (pos = posCorrect;pos == posCorrect;pos = 1+Math.floor(Math.random()*countQuestions));
for (i=1; i <= countQuestions; i++) {
if( (i != pos) && (i != posCorrect)) {
document.getElementById( "lblAnswer" + i).style.visibility = 'hidden';
document.getElementById( "btAnswer" + i).style.visibility = 'hidden';
}
}
}
function OnHelpTelephone( ans) {
if( flagTelephone) {
return;
}
flagTelephone = 1;
document.getElementById( "HelpTelephone").src = "telephonex.png";
if (countQuestions < 2) {
wrong = posCorrect;
} else {
for(;;) {
wrong = 1 + Math.floor(Math.random() * countQuestions);
if ( wrong != posCorrect) {
break;
}
}
}
// With 80% gives the correct answer.
if (Math.random() <= 0.8) {
pos = posCorrect;
} else {
pos = wrong;
}
info = "<?php echo get_string( 'millionaire_info_telephone', 'game').'<br><b>';?> ";
info += document.getElementById( "lblAnswer" + pos).innerHTML;
document.getElementById( "info").innerHTML = info;
}
function OnHelpPeople( ans) {
if( flagPeople) {
return;
}
flagPeople = 1;
document.getElementById( "HelpPeople").src = "peoplex.png";
sum = 0;
var aPercent = new Array();
for( i = 0; i < countQuestions-1; i++) {
percent = Math.floor(Math.random()*(100-sum));
aPercent[ i] = percent;
sum += percent;
}
aPercent[ countQuestions - 1] = 100 - sum;
if( Math.random() <= 0.8) {
//with percent 80% sets in the correct answer the biggest percent
max_pos = 0;
for( i=1; i < countQuestions; i++) {
if( aPercent[ i] >= aPercent[ max_pos])
max_pos = i;
}
temp = aPercent[ max_pos];
aPercent[ max_pos] = aPercent[ posCorrect-1];
aPercent[ posCorrect-1] = temp;
}
var letters = "<?php echo get_string( 'lettersall', 'game');?>";
info = "<?php echo '<br>'.get_string( 'millionaire_info_people', 'game').':<br>';?>";
for( i=0; i < countQuestions; i++) {
info += "<br>" + letters.charAt( i) + " : " + aPercent[ i] + " %";
}
document.getElementById( "info").innerHTML = info;
}
function OnQuit( ans) {
Reset();
}
function Reset() {
for(i=1; i <= 15; i++) {
document.getElementById( "levela" + i).bgColor = "<?php echo $colorback;?>";
document.getElementById( "levelb" + i).bgColor = "<?php echo $colorback;?>";
document.getElementById( "levelc" + i).bgColor = "<?php echo $colorback;?>";
document.getElementById( "levela" + i).style.color = "<?php echo $color1;?>";
document.getElementById( "levelb" + i).style.color = "<?php echo $color1;?>";
document.getElementById( "levelc" + i).style.color = "<?php echo $color1;?>";
}
flag5050 = 0;
flagTelephone = 0;
flagPeople = 0;
document.getElementById( "Help5050").src = "5050.png";
document.getElementById( "HelpPeople").src = "people.png";
document.getElementById( "HelpTelephone").src = "telephone.png";
document.getElementById( "info").innerHTML = "";
UpdateLevel( 1);
SelectNextQuestion();
}
function RandomizeAnswers( elements) {
posCorrect = 1;
countQuestions = elements.length-1;
for( i=1; i <= countQuestions; i++) {
pos = 1+Math.floor(Math.random()*countQuestions);
if( posCorrect == i) {
posCorrect = pos;
} else if ( posCorrect == pos)
posCorrect = i;
var temp = elements[ i];
elements[ i] = elements[ pos];
elements[ pos] = temp;
}
}
function SelectNextQuestion()
{
current_question = Math.floor(Math.random()*questions.length);
question = Base64.decode( questions[ current_question]);
var elements = new Array();
elements = question.split('#');
RandomizeAnswers( elements);
document.getElementById( "question").innerHTML = elements[ 0];
for( i=1; i < elements.length; i++)
{
document.getElementById( "lblAnswer" + i).innerHTML = elements[ i];
document.getElementById( "lblAnswer" + i).style.visibility = 'visible';
document.getElementById( "btAnswer" + i).style.visibility = 'visible';
}
for( i=elements.length; i<= maxQuestions; i++)
{
document.getElementById( "lblAnswer" + i).style.visibility = 'hidden';
document.getElementById( "btAnswer" + i).style.visibility = 'hidden';
}
document.getElementById( "info").innerHTML = "";
}
/**
*
* Base64 encode / decode
* http://www.webtoolkit.info/
*
**/
var Base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for decoding
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
</script>
<table cellpadding=0 cellspacing=0 border=0>
<tr style='background:#408080'>
<td rowspan=<?php echo 17 + $maxquestions;?>> </td>
<td colspan=6> </td>
<td rowspan=<?php echo 17 + $maxquestions;?>> </td>
</tr>
<tr height=10%>
<td style='background:#408080' rowspan=3 colspan=2>
<input type="image" name="Help5050" id="Help5050" Title="50 50" src="5050.png" alt="" border="0" onmousedown=OnHelp5050();>
<input type="image" name="HelpTelephone" id="HelpTelephone" Title="<?php echo get_string( 'millionaire_telephone', 'game');?>"
src="telephone.png" alt="" border="0" onmousedown="OnHelpTelephone();">
<input type="image" name="HelpPeople" id="HelpPeople" Title="<?php echo get_string( 'millionaire_helppeople', 'game');?>"
src="people.png" alt="" border="0" onmousedown="OnHelpPeople();">
<input type="image" name="Quit" id="Quit" Title="<?php echo get_string( 'millionaire_quit', 'game');?>"
src="x.png" alt="" border="0" onmousedown=OnQuit();>
</td>
<td rowspan=<?php echo 16 + $maxquestions;?> style='background:#408080'> </td>
<td id="levela15" align=right>15</td>
<td id="levelb15"> </td>
<td id="levelc15" align=right> 150000</td>
</tr>
<tr><td id="levela14" align=right>14</td>
<td id="levelb14"></td><td id="levelc14" align=right> 800000</td>
</tr>
<tr><td id="levela13" align=right>13</td>
<td id="levelb13"></td><td id="levelc13" align=right> 400000</td>
</tr>
<tr><td rowspan=12 colspan=2 valign=top style='background:<?php echo $colorback;?>;color:<?php echo $color1;?>'>
<div id="question">aa</div></td>
<td id="levela12" align=r0ight>12</div></td>
<td id="levelb12"></td><td id="levelc12" align=right> 200000</td>
</tr>
<tr><td id="levela11" align=right>11</td>
<td id="levelb11"></td><td id="levelc11" align=right> 10000</td>
</tr>
<tr><td id="levela10" align=right>10</td>
<td id="levelb10"></td><td id="levelc10" align=right> 5000</td>
</tr>
<tr><td id="levela9" align=right>9</td>
<td id="levelb9"></td><td id="levelc9" align=right> 4000</td>
</tr>
<tr><td id="levela8" align=right>8</td>
<td id="levelb8"></td><td id="levelc8" align=right> 2000</td>
</tr>
<tr><td id="levela7" align=right>7</td>
<td id="levelb7"></td><td id="levelc7" align=right> 1500</td>
</tr>
<tr><td id="levela6" align=right>6</td>
<td id="levelb6"></td><td id="levelc6" align=right> 1000</td>
</tr>
<tr><td id="levela5" align=right>5</td>
<td id="levelb5"></td><td id="levelc5" align=right> 500</td>
</tr>
<tr><td id="levela4" align=right>4</td>
<td id="levelb4"></td><td id="levelc4" align=right> 400</td>
</tr>
<tr><td id="levela3" align=right>3</td>
<td id="levelb3"></td><td id="levelc3" align=right> 300</td>
</tr>
<tr><td id="levela2" align=right>2</td>
<td id="levelb2"></td><td id="levelc2" align=right> 200</td>
</tr>
<tr><td id="levela1" align=right>1</td>
<td id="levelb1"></td><td id="levelc1" align=right> 100</td>
</tr>
<tr style='background:#408080'><td colspan=10> </td>
</tr>
<?php
$letters = get_string( 'lettersall', 'game');
for ($i = 1; $i <= $maxquestions; $i++) {
$s = game_substr( $letters, $i - 1, 1);
echo "<tr>\n";
echo "<td style='background:$colorback;color:$color1'>";
echo "<input style=\"background:$colorback;color:$color1;\"
type=\"submit\" name=\"btAnswer$i\" value=\"$s\" id=\"btAnswer$i\"";
echo " onmouseover=\"Highlite( $i);\" onmouseout=\"Restore( $i);\" onmousedown=\"OnSelectAnswer( $i);\">";
echo "</td>\n";
echo "<td style=\"background:$colorback;color:$color1;\" width=100%> <span id=lblAnswer$i
style=\"background:$colorback;color:$color1\"
onmouseover=\"Highlite($i);\r \n\" onmouseout=\"Restore( $i);\" onmousedown=\"OnSelectAnswer( $i);\"></span></td>\n";
if ( $i == 1) {
echo "<td style='background:#408080' rowspan=".$maxquestions." colspan=3><div id=\"info\"></div></td>\n";
}
echo "</tr>\n";
}
?>
<tr><td colspan=10 style='background:#408080'> </td></tr>
</table>
</body>
</html>
<?php
}