-
Notifications
You must be signed in to change notification settings - Fork 465
/
fill_blanks.class.php
executable file
·1447 lines (1312 loc) · 53.9 KB
/
fill_blanks.class.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
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
<?php
/* For licensing terms, see /license.txt */
/**
* Class FillBlanks.
*
* @author Eric Marguin
* @author Julio Montoya multiple fill in blank option added
*
* @package chamilo.exercise
*/
class FillBlanks extends Question
{
const FILL_THE_BLANK_STANDARD = 0;
const FILL_THE_BLANK_MENU = 1;
const FILL_THE_BLANK_SEVERAL_ANSWER = 2;
public static $typePicture = 'fill_in_blanks.png';
public static $explanationLangVar = 'FillBlanks';
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
$this->type = FILL_IN_BLANKS;
$this->isContent = $this->getIsContent();
}
/**
* {@inheritdoc}
*/
public function createAnswersForm($form)
{
$defaults = [];
$defaults['answer'] = get_lang('DefaultTextInBlanks');
$defaults['select_separator'] = 0;
$blankSeparatorNumber = 0;
if (!empty($this->id)) {
$objectAnswer = new Answer($this->id);
$answer = $objectAnswer->selectAnswer(1);
$listAnswersInfo = self::getAnswerInfo($answer);
$defaults['multiple_answer'] = 0;
if ($listAnswersInfo['switchable']) {
$defaults['multiple_answer'] = 1;
}
// Take the complete string except after the last '::'
$defaults['answer'] = $listAnswersInfo['text'];
$defaults['select_separator'] = $listAnswersInfo['blank_separator_number'];
$blankSeparatorNumber = $listAnswersInfo['blank_separator_number'];
}
$blankSeparatorStart = self::getStartSeparator($blankSeparatorNumber);
$blankSeparatorEnd = self::getEndSeparator($blankSeparatorNumber);
$setWeightAndSize = '';
if (isset($listAnswersInfo) && count($listAnswersInfo['weighting']) > 0) {
foreach ($listAnswersInfo['weighting'] as $i => $weighting) {
$setWeightAndSize .= 'document.getElementById("weighting['.$i.']").value = "'.$weighting.'";';
}
foreach ($listAnswersInfo['input_size'] as $i => $sizeOfInput) {
$setWeightAndSize .= 'document.getElementById("sizeofinput['.$i.']").value = "'.$sizeOfInput.'";';
$setWeightAndSize .= 'document.getElementById("samplesize['.$i.']").style.width = "'.$sizeOfInput.'px";';
}
}
echo '<script>
var firstTime = true;
var originalOrder = new Array();
var blankSeparatorStart = "'.$blankSeparatorStart.'";
var blankSeparatorEnd = "'.$blankSeparatorEnd.'";
var blankSeparatorStartRegexp = getBlankSeparatorRegexp(blankSeparatorStart);
var blankSeparatorEndRegexp = getBlankSeparatorRegexp(blankSeparatorEnd);
var blanksRegexp = "/"+blankSeparatorStartRegexp+"[^"+blankSeparatorStartRegexp+"]*"+blankSeparatorEndRegexp+"/g";
CKEDITOR.on("instanceCreated", function(e) {
if (e.editor.name === "answer") {
//e.editor.on("change", updateBlanks);
e.editor.on("change", function(){
updateBlanks();
});
}
});
function updateBlanks()
{
var answer;
if (firstTime) {
var field = document.getElementById("answer");
answer = field.value;
} else {
answer = CKEDITOR.instances["answer"].getData();
}
// disable the save button, if not blanks have been created
$("button").attr("disabled", "disabled");
$("#defineoneblank").show();
var blanks = answer.match(eval(blanksRegexp));
var fields = "<div class=\"form-group \">";
fields += "<label class=\"col-sm-2 control-label\"></label>";
fields += "<div class=\"col-sm-8\">";
fields += "<table class=\"data_table\">";
fields += "<tr><th style=\"width:220px\">'.get_lang('WordTofind').'</th>";
fields += "<th style=\"width:50px\">'.get_lang('QuestionWeighting').'</th>";
fields += "<th>'.get_lang('BlankInputSize').'</th></tr>";
if (blanks != null) {
for (var i=0; i < blanks.length; i++) {
// remove forbidden characters that causes bugs
blanks[i] = removeForbiddenChars(blanks[i]);
// trim blanks between brackets
blanks[i] = trimBlanksBetweenSeparator(blanks[i], blankSeparatorStart, blankSeparatorEnd);
// if the word is empty []
if (blanks[i] == blankSeparatorStartRegexp+blankSeparatorEndRegexp) {
break;
}
// get input size
var inputSize = 100;
var textValue = blanks[i].substr(1, blanks[i].length - 2);
var btoaValue = textValue.hashCode();
if (firstTime == false) {
var element = document.getElementById("samplesize["+i+"]");
if (element) {
inputSize = document.getElementById("sizeofinput["+i+"]").value;
}
}
if (document.getElementById("weighting["+i+"]")) {
var value = document.getElementById("weighting["+i+"]").value;
} else {
var value = "1";
}
var blanksWithColor = trimBlanksBetweenSeparator(blanks[i], blankSeparatorStart, blankSeparatorEnd, 1);
fields += "<tr>";
fields += "<td>"+blanksWithColor+"</td>";
fields += "<td><input class=\"form-control\" style=\"width:60px\" value=\""+value+"\" type=\"text\" id=\"weighting["+i+"]\" name=\"weighting["+i+"]\" /></td>";
fields += "<td>";
fields += "<input class=\"btn btn-default\" type=\"button\" value=\"-\" onclick=\"changeInputSize(-1, "+i+")\"> ";
fields += "<input class=\"btn btn-default\" type=\"button\" value=\"+\" onclick=\"changeInputSize(1, "+i+")\"> ";
fields += " <input class=\"sample\" id=\"samplesize["+i+"]\" data-btoa=\""+btoaValue+"\" type=\"text\" value=\""+textValue+"\" style=\"width:"+inputSize+"px\" disabled=disabled />";
fields += "<input id=\"sizeofinput["+i+"]\" type=\"hidden\" value=\""+inputSize+"\" name=\"sizeofinput["+i+"]\" />";
fields += "</td>";
fields += "</tr>";
// enable the save button
$("button").removeAttr("disabled");
$("#defineoneblank").hide();
}
}
document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
$(originalOrder).each(function(i, data) {
if (firstTime == false) {
value = data.value;
var d = $("input.sample[data-btoa=\'"+value+"\']");
var id = d.attr("id");
if (id) {
var sizeInputId = id.replace("samplesize", "sizeofinput");
var sizeInputId = sizeInputId.replace("[", "\\\[");
var sizeInputId = sizeInputId.replace("]", "\\\]");
$("#"+sizeInputId).val(data.width);
d.outerWidth(data.width+"px");
}
}
});
updateOrder(blanks);
if (firstTime) {
firstTime = false;
'.$setWeightAndSize.'
}
}
window.onload = updateBlanks;
String.prototype.hashCode = function() {
var hash = 0, i, chr, len;
if (this.length === 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
function updateOrder(blanks)
{
originalOrder = new Array();
if (blanks != null) {
for (var i=0; i < blanks.length; i++) {
// remove forbidden characters that causes bugs
blanks[i] = removeForbiddenChars(blanks[i]);
// trim blanks between brackets
blanks[i] = trimBlanksBetweenSeparator(blanks[i], blankSeparatorStart, blankSeparatorEnd);
// if the word is empty []
if (blanks[i] == blankSeparatorStartRegexp+blankSeparatorEndRegexp) {
break;
}
var textValue = blanks[i].substr(1, blanks[i].length - 2);
var btoaValue = textValue.hashCode();
if (firstTime == false) {
var element = document.getElementById("samplesize["+i+"]");
if (element) {
inputSize = document.getElementById("sizeofinput["+i+"]").value;
originalOrder.push({ "width" : inputSize, "value": btoaValue });
}
}
}
}
}
function changeInputSize(coef, inIdNum)
{
if (firstTime) {
var field = document.getElementById("answer");
answer = field.value;
} else {
answer = CKEDITOR.instances["answer"].getData();
}
var blanks = answer.match(eval(blanksRegexp));
var currentWidth = $("#samplesize\\\["+inIdNum+"\\\]").width();
var newWidth = currentWidth + coef * 20;
newWidth = Math.max(20, newWidth);
newWidth = Math.min(newWidth, 600);
$("#samplesize\\\["+inIdNum+"\\\]").outerWidth(newWidth);
$("#sizeofinput\\\["+inIdNum+"\\\]").attr("value", newWidth);
updateOrder(blanks);
}
function removeForbiddenChars(inTxt)
{
outTxt = inTxt;
outTxt = outTxt.replace(/"/g, ""); // remove the char
outTxt = outTxt.replace(/\x22/g, ""); // remove the char
outTxt = outTxt.replace(/"/g, ""); // remove the char
outTxt = outTxt.replace(/\\\\/g, ""); // remove the \ char
outTxt = outTxt.replace(/ /g, " ");
outTxt = outTxt.replace(/^ +/, "");
outTxt = outTxt.replace(/ +$/, "");
return outTxt;
}
function changeBlankSeparator()
{
var separatorNumber = $("#select_separator").val();
var tabSeparator = getSeparatorFromNumber(separatorNumber);
blankSeparatorStart = tabSeparator[0];
blankSeparatorEnd = tabSeparator[1];
blankSeparatorStartRegexp = getBlankSeparatorRegexp(blankSeparatorStart);
blankSeparatorEndRegexp = getBlankSeparatorRegexp(blankSeparatorEnd);
blanksRegexp = "/"+blankSeparatorStartRegexp+"[^"+blankSeparatorStartRegexp+"]*"+blankSeparatorEndRegexp+"/g";
updateBlanks();
}
// this function is the same than the PHP one
// if modify it modify the php one escapeForRegexp
function getBlankSeparatorRegexp(inTxt)
{
var tabSpecialChar = new Array(".", "+", "*", "?", "[", "^", "]", "$", "(", ")",
"{", "}", "=", "!", "<", ">", "|", ":", "-", ")");
for (var i=0; i < tabSpecialChar.length; i++) {
if (inTxt == tabSpecialChar[i]) {
return "\\\"+inTxt;
}
}
return inTxt;
}
// this function is the same than the PHP one
// if modify it modify the php one getAllowedSeparator
function getSeparatorFromNumber(number)
{
var separator = new Array();
separator[0] = new Array("[", "]");
separator[1] = new Array("{", "}");
separator[2] = new Array("(", ")");
separator[3] = new Array("*", "*");
separator[4] = new Array("#", "#");
separator[5] = new Array("%", "%");
separator[6] = new Array("$", "$");
return separator[number];
}
function trimBlanksBetweenSeparator(inTxt, inSeparatorStart, inSeparatorEnd, addColor)
{
var result = inTxt
result = result.replace(inSeparatorStart, "");
result = result.replace(inSeparatorEnd, "");
result = result.trim();
if (addColor == 1) {
var resultParts = result.split("|");
var partsToString = "";
resultParts.forEach(function(item, index) {
if (index == 0) {
item = "<b><font style=\"color:green\"> " + item +"</font></b>";
}
if (index < resultParts.length - 1) {
item = item + " | ";
}
partsToString += item;
});
result = partsToString;
}
return inSeparatorStart+result+inSeparatorEnd;
}
</script>';
// answer
$form->addLabel(
null,
get_lang('TypeTextBelow').', '.get_lang('And').' '.get_lang('UseTagForBlank')
);
$form->addElement(
'html_editor',
'answer',
Display::return_icon('fill_field.png'),
['id' => 'answer'],
['ToolbarSet' => 'TestQuestionDescription']
);
$form->addRule('answer', get_lang('GiveText'), 'required');
//added multiple answers
$form->addElement('checkbox', 'multiple_answer', '', get_lang('FillInBlankSwitchable'));
$form->addElement(
'select',
'select_separator',
get_lang('SelectFillTheBlankSeparator'),
self::getAllowedSeparatorForSelect(),
' id="select_separator" style="width:150px" class="selectpicker" onchange="changeBlankSeparator()" '
);
$form->addLabel(
null,
'<input type="button" onclick="updateBlanks()" value="'.get_lang('RefreshBlanks').'" class="btn btn-default" />'
);
$form->addHtml('<div id="blanks_weighting"></div>');
global $text;
// setting the save button here and not in the question class.php
$form->addHtml('<div id="defineoneblank" style="color:#D04A66; margin-left:160px">'.get_lang('DefineBlanks').'</div>');
$form->addButtonSave($text, 'submitQuestion');
if (!empty($this->id)) {
$form->setDefaults($defaults);
} else {
if ($this->isContent == 1) {
$form->setDefaults($defaults);
}
}
}
/**
* {@inheritdoc}
*/
public function processAnswersCreation($form, $exercise)
{
$answer = $form->getSubmitValue('answer');
// Due the ckeditor transform the elements to their HTML value
//$answer = api_html_entity_decode($answer, ENT_QUOTES, $charset);
//$answer = htmlentities(api_utf8_encode($answer));
// remove the "::" eventually written by the user
$answer = str_replace('::', '', $answer);
// remove starting and ending space and
$answer = api_preg_replace("/\xc2\xa0/", " ", $answer);
// start and end separator
$blankStartSeparator = self::getStartSeparator($form->getSubmitValue('select_separator'));
$blankEndSeparator = self::getEndSeparator($form->getSubmitValue('select_separator'));
$blankStartSeparatorRegexp = self::escapeForRegexp($blankStartSeparator);
$blankEndSeparatorRegexp = self::escapeForRegexp($blankEndSeparator);
// remove spaces at the beginning and the end of text in square brackets
$answer = preg_replace_callback(
"/".$blankStartSeparatorRegexp."[^]]+".$blankEndSeparatorRegexp."/",
function ($matches) use ($blankStartSeparator, $blankEndSeparator) {
$matchingResult = $matches[0];
$matchingResult = trim($matchingResult, $blankStartSeparator);
$matchingResult = trim($matchingResult, $blankEndSeparator);
$matchingResult = trim($matchingResult);
// remove forbidden chars
$matchingResult = str_replace("/\\/", "", $matchingResult);
$matchingResult = str_replace('/"/', "", $matchingResult);
return $blankStartSeparator.$matchingResult.$blankEndSeparator;
},
$answer
);
// get the blanks weightings
$nb = preg_match_all(
'/'.$blankStartSeparatorRegexp.'[^'.$blankStartSeparatorRegexp.']*'.$blankEndSeparatorRegexp.'/',
$answer,
$blanks
);
if (isset($_GET['editQuestion'])) {
$this->weighting = 0;
}
/* if we have some [tobefound] in the text
build the string to save the following in the answers table
<p>I use a [computer] and a [pen].</p>
becomes
<p>I use a [computer] and a [pen].</p>::100,50:100,50@1
++++++++-------**
--- -- --- -- -
A B (C) (D)(E)
+++++++ : required, weighting of each words
------- : optional, input width to display, 200 if not present
** : equal @1 if "Allow answers order switches" has been checked, @ otherwise
A : weighting for the word [computer]
B : weighting for the word [pen]
C : input width for the word [computer]
D : input width for the word [pen]
E : equal @1 if "Allow answers order switches" has been checked, @ otherwise
*/
if ($nb > 0) {
$answer .= '::';
// weighting
for ($i = 0; $i < $nb; $i++) {
// enter the weighting of word $i
$answer .= $form->getSubmitValue('weighting['.$i.']');
// not the last word, add ","
if ($i != $nb - 1) {
$answer .= ',';
}
// calculate the global weighting for the question
$this->weighting += (float) $form->getSubmitValue('weighting['.$i.']');
}
// input width
$answer .= ":";
for ($i = 0; $i < $nb; $i++) {
// enter the width of input for word $i
$answer .= $form->getSubmitValue('sizeofinput['.$i.']');
// not the last word, add ","
if ($i != $nb - 1) {
$answer .= ',';
}
}
}
// write the blank separator code number
// see function getAllowedSeparator
/*
0 [...]
1 {...}
2 (...)
3 *...*
4 #...#
5 %...%
6 $...$
*/
$answer .= ':'.$form->getSubmitValue('select_separator');
// Allow answers order switches
$is_multiple = $form->getSubmitValue('multiple_answer');
$answer .= '@'.$is_multiple;
$this->save($exercise);
$objAnswer = new Answer($this->id);
$objAnswer->createAnswer($answer, 0, '', 0, 1);
$objAnswer->save();
}
/**
* {@inheritdoc}
*/
public function return_header(Exercise $exercise, $counter = null, $score = [])
{
$header = parent::return_header($exercise, $counter, $score);
$header .= '<table class="'.$this->question_table_class.'">
<tr>
<th>'.get_lang('Answer').'</th>
</tr>';
return $header;
}
/**
* @param int $currentQuestion
* @param int $questionId
* @param string $correctItem
* @param array $attributes
* @param string $answer
* @param array $listAnswersInfo
* @param bool $displayForStudent
* @param int $inBlankNumber
* @param string $labelId
*
* @return string
*/
public static function getFillTheBlankHtml(
$currentQuestion,
$questionId,
$correctItem,
$attributes,
$answer,
$listAnswersInfo,
$displayForStudent,
$inBlankNumber,
$labelId = ''
) {
$inTabTeacherSolution = $listAnswersInfo['words'];
$inTeacherSolution = $inTabTeacherSolution[$inBlankNumber];
if (empty($labelId)) {
$labelId = 'choice_id_'.$currentQuestion.'_'.$inBlankNumber;
}
switch (self::getFillTheBlankAnswerType($inTeacherSolution)) {
case self::FILL_THE_BLANK_MENU:
$selected = '';
// the blank menu
// display a menu from answer separated with |
// if display for student, shuffle the correct answer menu
$listMenu = self::getFillTheBlankMenuAnswers(
$inTeacherSolution,
$displayForStudent
);
$resultOptions = ['' => '--'];
foreach ($listMenu as $item) {
$resultOptions[sha1($item)] = $item;
}
foreach ($resultOptions as $key => $value) {
if ($correctItem == $value) {
$selected = $key;
break;
}
}
$width = '';
if (!empty($attributes['style'])) {
$width = str_replace('width:', '', $attributes['style']);
}
$result = Display::select(
"choice[$questionId][]",
$resultOptions,
$selected,
[
'class' => 'selectpicker',
'data-width' => $width,
'id' => $labelId,
],
false
);
break;
case self::FILL_THE_BLANK_SEVERAL_ANSWER:
case self::FILL_THE_BLANK_STANDARD:
default:
$attributes['id'] = $labelId;
$result = Display::input(
'text',
"choice[$questionId][]",
$correctItem,
$attributes
);
break;
}
return $result;
}
/**
* Return an array with the different choices available
* when the answers between bracket show as a menu.
*
* @param string $correctAnswer
* @param bool $displayForStudent true if we want to shuffle the choices of the menu for students
*
* @return array
*/
public static function getFillTheBlankMenuAnswers($correctAnswer, $displayForStudent)
{
$list = api_preg_split("/\|/", $correctAnswer);
foreach ($list as &$item) {
$item = self::trimOption($item);
$item = api_html_entity_decode($item);
}
// The list is always in the same order, there's no option to allow or disable shuffle options.
if ($displayForStudent) {
shuffle_assoc($list);
}
return $list;
}
/**
* Return the array index of the student answer.
*
* @param string $correctAnswer the menu Choice1|Choice2|Choice3
* @param string $studentAnswer the student answer must be Choice1 or Choice2 or Choice3
*
* @return int in the example 0 1 or 2 depending of the choice of the student
*/
public static function getFillTheBlankMenuAnswerNum($correctAnswer, $studentAnswer)
{
$listChoices = self::getFillTheBlankMenuAnswers($correctAnswer, false);
foreach ($listChoices as $num => $value) {
if ($value == $studentAnswer) {
return $num;
}
}
// should not happened, because student choose the answer in a menu of possible answers
return -1;
}
/**
* Return the possible answer if the answer between brackets is a multiple choice menu.
*
* @param string $correctAnswer
*
* @return array
*/
public static function getFillTheBlankSeveralAnswers($correctAnswer)
{
// is answer||Answer||response||Response , mean answer or Answer ...
$listSeveral = api_preg_split("/\|\|/", $correctAnswer);
return $listSeveral;
}
/**
* Return true if student answer is right according to the correctAnswer
* it is not as simple as equality, because of the type of Fill The Blank question
* eg : studentAnswer = 'Un' and correctAnswer = 'Un||1||un'.
*
* @param string $studentAnswer [student_answer] of the info array of the answer field
* @param string $correctAnswer [words] of the info array of the answer field
* @param bool $fromDatabase
*
* @return bool
*/
public static function isStudentAnswerGood($studentAnswer, $correctAnswer, $fromDatabase = false)
{
$result = false;
switch (self::getFillTheBlankAnswerType($correctAnswer)) {
case self::FILL_THE_BLANK_MENU:
$listMenu = self::getFillTheBlankMenuAnswers($correctAnswer, false);
if ($studentAnswer != '' && isset($listMenu[0])) {
// First item is always the correct one.
$item = $listMenu[0];
if (!$fromDatabase) {
$item = sha1($item);
$studentAnswer = sha1($studentAnswer);
}
if ($item === $studentAnswer) {
$result = true;
}
}
break;
case self::FILL_THE_BLANK_SEVERAL_ANSWER:
// the answer must be one of the choice made
$listSeveral = self::getFillTheBlankSeveralAnswers($correctAnswer);
$listSeveral = array_map(
function ($item) {
return self::trimOption(api_html_entity_decode($item));
},
$listSeveral
);
//$studentAnswer = htmlspecialchars($studentAnswer);
$result = in_array($studentAnswer, $listSeveral);
break;
case self::FILL_THE_BLANK_STANDARD:
default:
$correctAnswer = api_html_entity_decode($correctAnswer);
//$studentAnswer = htmlspecialchars($studentAnswer);
$result = $studentAnswer == self::trimOption($correctAnswer);
break;
}
return $result;
}
/**
* @param string $correctAnswer
*
* @return int
*/
public static function getFillTheBlankAnswerType($correctAnswer)
{
$type = self::FILL_THE_BLANK_STANDARD;
if (api_strpos($correctAnswer, '|') && !api_strpos($correctAnswer, '||')) {
$type = self::FILL_THE_BLANK_MENU;
} elseif (api_strpos($correctAnswer, '||')) {
$type = self::FILL_THE_BLANK_SEVERAL_ANSWER;
}
return $type;
}
/**
* Return information about the answer.
*
* @param string $userAnswer the text of the answer of the question
* @param bool $isStudentAnswer true if it's a student answer false the empty question model
*
* @return array of information about the answer
*/
public static function getAnswerInfo($userAnswer = '', $isStudentAnswer = false)
{
$listAnswerResults = [];
$listAnswerResults['text'] = '';
$listAnswerResults['words_count'] = 0;
$listAnswerResults['words_with_bracket'] = [];
$listAnswerResults['words'] = [];
$listAnswerResults['weighting'] = [];
$listAnswerResults['input_size'] = [];
$listAnswerResults['switchable'] = '';
$listAnswerResults['student_answer'] = [];
$listAnswerResults['student_score'] = [];
$listAnswerResults['blank_separator_number'] = 0;
$listDoubleColon = [];
api_preg_match("/(.*)::(.*)$/s", $userAnswer, $listResult);
if (count($listResult) < 2) {
$listDoubleColon[] = '';
$listDoubleColon[] = '';
} else {
$listDoubleColon[] = $listResult[1];
$listDoubleColon[] = $listResult[2];
}
$listAnswerResults['system_string'] = $listDoubleColon[1];
// Make sure we only take the last bit to find special marks
$listArobaseSplit = explode('@', $listDoubleColon[1]);
if (count($listArobaseSplit) < 2) {
$listArobaseSplit[1] = '';
}
// Take the complete string except after the last '::'
$listDetails = explode(':', $listArobaseSplit[0]);
// < number of item after the ::[score]:[size]:[separator_id]@ , here there are 3
if (count($listDetails) < 3) {
$listWeightings = explode(',', $listDetails[0]);
$listSizeOfInput = [];
for ($i = 0; $i < count($listWeightings); $i++) {
$listSizeOfInput[] = 200;
}
$blankSeparatorNumber = 0; // 0 is [...]
} else {
$listWeightings = explode(',', $listDetails[0]);
$listSizeOfInput = explode(',', $listDetails[1]);
$blankSeparatorNumber = $listDetails[2];
}
$listAnswerResults['text'] = $listDoubleColon[0];
$listAnswerResults['weighting'] = $listWeightings;
$listAnswerResults['input_size'] = $listSizeOfInput;
$listAnswerResults['switchable'] = $listArobaseSplit[1];
$listAnswerResults['blank_separator_start'] = self::getStartSeparator($blankSeparatorNumber);
$listAnswerResults['blank_separator_end'] = self::getEndSeparator($blankSeparatorNumber);
$listAnswerResults['blank_separator_number'] = $blankSeparatorNumber;
$blankCharStart = self::getStartSeparator($blankSeparatorNumber);
$blankCharEnd = self::getEndSeparator($blankSeparatorNumber);
$blankCharStartForRegexp = self::escapeForRegexp($blankCharStart);
$blankCharEndForRegexp = self::escapeForRegexp($blankCharEnd);
// Get all blanks words
$listAnswerResults['words_count'] = api_preg_match_all(
'/'.$blankCharStartForRegexp.'[^'.$blankCharEndForRegexp.']*'.$blankCharEndForRegexp.'/',
$listDoubleColon[0],
$listWords
);
if ($listAnswerResults['words_count'] > 0) {
$listAnswerResults['words_with_bracket'] = $listWords[0];
// remove [ and ] in string
array_walk(
$listWords[0],
function (&$value, $key, $tabBlankChar) {
$trimChars = '';
for ($i = 0; $i < count($tabBlankChar); $i++) {
$trimChars .= $tabBlankChar[$i];
}
$value = trim($value, $trimChars);
},
[$blankCharStart, $blankCharEnd]
);
$listAnswerResults['words'] = $listWords[0];
}
// Get all common words
$commonWords = api_preg_replace(
'/'.$blankCharStartForRegexp.'[^'.$blankCharEndForRegexp.']*'.$blankCharEndForRegexp.'/',
"::",
$listDoubleColon[0]
);
// if student answer, the second [] is the student answer,
// the third is if student scored or not
$listBrackets = [];
$listWords = [];
if ($isStudentAnswer) {
for ($i = 0; $i < count($listAnswerResults['words']); $i++) {
$listBrackets[] = $listAnswerResults['words_with_bracket'][$i];
$listWords[] = $listAnswerResults['words'][$i];
if ($i + 1 < count($listAnswerResults['words'])) {
// should always be
$i++;
}
$listAnswerResults['student_answer'][] = $listAnswerResults['words'][$i];
if ($i + 1 < count($listAnswerResults['words'])) {
// should always be
$i++;
}
$listAnswerResults['student_score'][] = $listAnswerResults['words'][$i];
}
$listAnswerResults['words'] = $listWords;
$listAnswerResults['words_with_bracket'] = $listBrackets;
// if we are in student view, we've got 3 times :::::: for common words
$commonWords = api_preg_replace("/::::::/", '::', $commonWords);
}
$listAnswerResults['common_words'] = explode('::', $commonWords);
return $listAnswerResults;
}
/**
* Return an array of student state answers for fill the blank questions
* for each students that answered the question
* -2 : didn't answer
* -1 : student answer is wrong
* 0 : student answer is correct
* >0 : fill the blank question with choice menu, is the index of the student answer (right answer index is 0).
*
* @param int $testId
* @param int $questionId
* @param $studentsIdList
* @param string $startDate
* @param string $endDate
* @param bool $useLastAnsweredAttempt
*
* @return array
* (
* [student_id] => Array
* (
* [first fill the blank for question] => -1
* [second fill the blank for question] => 2
* [third fill the blank for question] => -1
* )
* )
*/
public static function getFillTheBlankResult(
$testId,
$questionId,
$studentsIdList,
$startDate,
$endDate,
$useLastAnsweredAttempt = true
) {
$tblTrackEAttempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
$tblTrackEExercise = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
$courseId = api_get_course_int_id();
// If no user has answered questions, no need to go further. Return empty array.
if (empty($studentsIdList)) {
return [];
}
// request to have all the answers of student for this question
// student may have doing it several time
// student may have not answered the bracket id, in this case, is result of the answer is empty
// we got the less recent attempt first
$sql = 'SELECT * FROM '.$tblTrackEAttempt.' tea
LEFT JOIN '.$tblTrackEExercise.' tee
ON
tee.exe_id = tea.exe_id AND
tea.c_id = '.$courseId.' AND
exe_exo_id = '.$testId.'
WHERE
tee.c_id = '.$courseId.' AND
question_id = '.$questionId.' AND
tea.user_id IN ('.implode(',', $studentsIdList).') AND
tea.tms >= "'.$startDate.'" AND
tea.tms <= "'.$endDate.'"
ORDER BY user_id, tea.exe_id;
';
$res = Database::query($sql);
$userResult = [];
// foreach attempts for all students starting with his older attempt
while ($data = Database::fetch_array($res)) {
$answer = self::getAnswerInfo($data['answer'], true);
// for each bracket to find in this question
foreach ($answer['student_answer'] as $bracketNumber => $studentAnswer) {
if ($answer['student_answer'][$bracketNumber] != '') {
// student has answered this bracket, cool
switch (self::getFillTheBlankAnswerType($answer['words'][$bracketNumber])) {
case self::FILL_THE_BLANK_MENU:
// get the indice of the choosen answer in the menu
// we know that the right answer is the first entry of the menu, ie 0
// (remember, menu entries are shuffled when taking the test)
$userResult[$data['user_id']][$bracketNumber] = self::getFillTheBlankMenuAnswerNum(
$answer['words'][$bracketNumber],
$answer['student_answer'][$bracketNumber]
);
break;
default:
if (self::isStudentAnswerGood(
$answer['student_answer'][$bracketNumber],
$answer['words'][$bracketNumber]
)
) {
$userResult[$data['user_id']][$bracketNumber] = 0; // right answer
} else {
$userResult[$data['user_id']][$bracketNumber] = -1; // wrong answer
}
}
} else {
// student didn't answer this bracket
if ($useLastAnsweredAttempt) {
// if we take into account the last answered attempt
if (!isset($userResult[$data['user_id']][$bracketNumber])) {
$userResult[$data['user_id']][$bracketNumber] = -2; // not answered
}
} else {
// we take the last attempt, even if the student answer the question before
$userResult[$data['user_id']][$bracketNumber] = -2; // not answered
}
}
}
}
return $userResult;
}
/**
* Return the number of student that give at leat an answer in the fill the blank test.
*
* @param array $resultList
*
* @return int
*/
public static function getNbResultFillBlankAll($resultList)
{
$outRes = 0;
// for each student in group
foreach ($resultList as $list) {
$found = false;
// for each bracket, if student has at least one answer ( choice > -2) then he pass the question
foreach ($list as $choice) {
if ($choice > -2 && !$found) {
$outRes++;
$found = true;
}
}
}
return $outRes;
}
/**
* Replace the occurrence of blank word with [correct answer][student answer][answer is correct].
*
* @param array $listWithStudentAnswer
*
* @return string
*/
public static function getAnswerInStudentAttempt($listWithStudentAnswer)
{
$separatorStart = $listWithStudentAnswer['blank_separator_start'];
$separatorEnd = $listWithStudentAnswer['blank_separator_end'];
// lets rebuild the sentence with [correct answer][student answer][answer is correct]
$result = '';
for ($i = 0; $i < count($listWithStudentAnswer['common_words']) - 1; $i++) {
$answerValue = null;
if (isset($listWithStudentAnswer['student_answer'][$i])) {
$answerValue = $listWithStudentAnswer['student_answer'][$i];
}
$scoreValue = null;
if (isset($listWithStudentAnswer['student_score'][$i])) {
$scoreValue = $listWithStudentAnswer['student_score'][$i];