Passed
Pull Request — 1.11.x (#4360)
by Angel Fernando Quiroz
08:44
created

ExerciseShowFunctions::display_free_answer()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 6
nop 6
dl 0
loc 22
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
/* See license terms in /license.txt */
4
5
class ExerciseShowFunctions
6
{
7
    /**
8
     * Shows the answer to a fill-in-the-blanks question, as HTML.
9
     *
10
     * @param Exercise $exercise
11
     * @param int      $feedbackType
12
     * @param string   $answer
13
     * @param int      $id                           Exercise ID
14
     * @param int      $questionId                   Question ID
15
     * @param int      $resultsDisabled
16
     * @param string   $originalStudentAnswer
17
     * @param bool     $showTotalScoreAndUserChoices
18
     */
19
    public static function display_fill_in_blanks_answer(
20
        $exercise,
21
        $feedbackType,
22
        $answer,
23
        $id,
24
        $questionId,
25
        $resultsDisabled,
26
        $originalStudentAnswer,
27
        $showTotalScoreAndUserChoices
28
    ) {
29
        $answerHTML = FillBlanks::getHtmlDisplayForAnswer(
30
            $answer,
31
            $feedbackType,
32
            $resultsDisabled,
33
            $showTotalScoreAndUserChoices
34
        );
35
36
        if (empty($id)) {
37
            echo '<tr><td>';
38
            echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
39
            echo '</td></tr>';
40
        } else {
41
            echo '<tr><td>';
42
            echo Security::remove_XSS($answerHTML, COURSEMANAGERLOWSECURITY);
43
            echo '</td>';
44
            echo '</tr>';
45
        }
46
    }
47
48
    /**
49
     * Shows the answer to a calculated question, as HTML.
50
     *
51
     *  @param Exercise $exercise
52
     * @param string    Answer text
53
     * @param int       Exercise ID
54
     * @param int       Question ID
55
     */
56
    public static function display_calculated_answer(
57
        $exercise,
58
        $feedback_type,
59
        $answer,
60
        $id,
61
        $questionId,
62
        $resultsDisabled,
63
        $showTotalScoreAndUserChoices,
64
        $expectedChoice = '',
65
        $choice = '',
66
        $status = ''
67
    ) {
68
        $answer = explode(':::', $answer);
69
        $answer = $answer[0];
70
        if ($exercise->showExpectedChoice()) {
71
            if (empty($id)) {
72
                echo '<tr><td>'.Security::remove_XSS($answer).'</td>';
73
                echo '<td>'.Security::remove_XSS($choice).'</td>';
74
                if ($exercise->showExpectedChoiceColumn()) {
75
                    echo '<td>'.Security::remove_XSS($expectedChoice).'</td>';
76
                }
77
78
                echo '<td>'.Security::remove_XSS($status).'</td>';
79
                echo '</tr>';
80
            } else {
81
                echo '<tr><td>';
82
                echo Security::remove_XSS($answer);
83
                echo '</td><td>';
84
                echo Security::remove_XSS($choice);
85
                echo '</td>';
86
                if ($exercise->showExpectedChoiceColumn()) {
87
                    echo '<td>';
88
                    echo Security::remove_XSS($expectedChoice);
89
                    echo '</td>';
90
                }
91
                echo '<td>';
92
                echo Security::remove_XSS($status);
93
                echo '</td>';
94
                echo '</tr>';
95
            }
96
        } else {
97
            if (empty($id)) {
98
                echo '<tr><td>'.Security::remove_XSS($answer).'</td></tr>';
99
            } else {
100
                echo '<tr><td>';
101
                echo Security::remove_XSS($answer);
102
                echo '</tr>';
103
            }
104
        }
105
    }
106
107
    /**
108
     * Shows the answer to an upload question.
109
     *
110
     * @param float|null $questionScore   Only used to check if > 0
111
     * @param int        $resultsDisabled Unused
112
     */
113
    public static function displayUploadAnswer(
114
        string $feedbackType,
115
        string $answer,
116
        int $exeId,
117
        int $questionId,
118
        $questionScore = null,
119
        $resultsDisabled = 0
120
    ) {
121
        if (!empty($answer)) {
122
            $exeInfo = Event::get_exercise_results_by_attempt($exeId);
0 ignored issues
show
Bug introduced by
The method get_exercise_results_by_attempt() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
            /** @scrutinizer ignore-call */ 
123
            $exeInfo = Event::get_exercise_results_by_attempt($exeId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
            if (empty($exeInfo)) {
124
                global $exercise_stat_info;
125
                $userId = $exercise_stat_info['exe_user_id'];
126
            } else {
127
                $userId = $exeInfo[$exeId]['exe_user_id'];
128
            }
129
            $userWebpath = UserManager::getUserPathById($userId, 'web').'my_files'.'/upload_answer/'.$exeId.'/'.$questionId.'/';
130
            $filesNames = explode('|', $answer);
131
            echo '<tr><td>';
132
            foreach ($filesNames as $filename) {
133
                $filename = Security::remove_XSS($filename);
134
                echo '<p><a href="'.$userWebpath.$filename.'" target="_blank">'.$filename.'</a></p>';
135
            }
136
            echo '</td></tr>';
137
        }
138
139
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
140
            $comments = Event::get_comments($exeId, $questionId);
0 ignored issues
show
Bug introduced by
The method get_comments() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
            /** @scrutinizer ignore-call */ 
141
            $comments = Event::get_comments($exeId, $questionId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
            if ($questionScore > 0 || !empty($comments)) {
142
            } else {
143
                echo '<tr>';
144
                echo Display::tag('td', ExerciseLib::getNotCorrectedYetText());
145
                echo '</tr>';
146
            }
147
        }
148
    }
149
150
    /**
151
     * Shows the answer to a free-answer question, as HTML.
152
     *
153
     * @param string    Answer text
154
     * @param int       Exercise ID
155
     * @param int       Question ID
156
     */
157
    public static function display_free_answer(
158
        $feedback_type,
159
        $answer,
160
        $exe_id,
161
        $questionId,
162
        $questionScore = null,
163
        $resultsDisabled = 0
164
    ) {
165
        $comments = Event::get_comments($exe_id, $questionId);
166
167
        if (!empty($answer)) {
168
            echo '<tr><td>';
169
            echo Security::remove_XSS($answer);
170
            echo '</td></tr>';
171
        }
172
173
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedback_type) {
174
            if ($questionScore > 0 || !empty($comments)) {
175
            } else {
176
                echo '<tr>';
177
                echo Display::tag('td', ExerciseLib::getNotCorrectedYetText());
178
                echo '</tr>';
179
            }
180
        }
181
    }
182
183
    /**
184
     * @param $feedback_type
185
     * @param $answer
186
     * @param $id
187
     * @param $questionId
188
     * @param null $fileUrl
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fileUrl is correct as it would always require null to be passed?
Loading history...
189
     * @param int  $resultsDisabled
190
     * @param int  $questionScore
191
     */
192
    public static function display_oral_expression_answer(
193
        $feedback_type,
194
        $answer,
195
        $id,
196
        $questionId,
197
        $fileUrl = null,
198
        $resultsDisabled = 0,
199
        $questionScore = 0
200
    ) {
201
        if (isset($fileUrl)) {
202
            echo '
203
                <tr>
204
                    <td><audio src="'.$fileUrl.'" controls></audio></td>
205
                </tr>
206
            ';
207
        }
208
209
        if (empty($id)) {
210
            echo '<tr>';
211
            if (!empty($answer) && ($answer != basename($fileUrl))) {
212
                echo Display::tag('td', Security::remove_XSS($answer), ['width' => '55%']);
213
            }
214
            echo '</tr>';
215
            if (!$questionScore && EXERCISE_FEEDBACK_TYPE_EXAM != $feedback_type) {
216
                echo '<tr>';
217
                echo Display::tag('td', ExerciseLib::getNotCorrectedYetText(), ['width' => '45%']);
218
                echo '</tr>';
219
            } else {
220
                echo '<tr><td>&nbsp;</td></tr>';
221
            }
222
        } else {
223
            echo '<tr>';
224
            echo '<td>';
225
            if (!empty($answer)) {
226
                echo Security::remove_XSS($answer);
227
            }
228
            echo '</td>';
229
            echo '</tr>';
230
        }
231
    }
232
233
    /**
234
     * Displays the answer to a hotspot question.
235
     *
236
     * @param int    $feedback_type
237
     * @param int    $answerId
238
     * @param string $answer
239
     * @param string $studentChoice
240
     * @param string $answerComment
241
     * @param int    $resultsDisabled
242
     * @param int    $orderColor
243
     * @param bool   $showTotalScoreAndUserChoices
244
     */
245
    public static function display_hotspot_answer(
246
        $exercise,
247
        $feedback_type,
248
        $answerId,
249
        $answer,
250
        $studentChoice,
251
        $answerComment,
252
        $resultsDisabled,
253
        $orderColor,
254
        $showTotalScoreAndUserChoices
255
    ) {
256
        $hide_expected_answer = false;
257
        switch ($resultsDisabled) {
258
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
259
                if (0 == $feedback_type) {
260
                    $hide_expected_answer = true;
261
                }
262
                break;
263
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
264
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
265
                $hide_expected_answer = true;
266
                if ($showTotalScoreAndUserChoices) {
267
                    $hide_expected_answer = false;
268
                }
269
                break;
270
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
271
                $hide_expected_answer = true;
272
                if ($showTotalScoreAndUserChoices) {
273
                    $hide_expected_answer = false;
274
                }
275
                if (false === $showTotalScoreAndUserChoices && empty($studentChoice)) {
276
                    return '';
277
                }
278
                break;
279
        }
280
281
        if (!$hide_expected_answer
282
            && !$studentChoice
283
            && in_array($resultsDisabled, [RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER])
284
        ) {
285
            return;
286
        }
287
288
        $hotspotColors = [
289
            '', // $i starts from 1 on next loop (ugly fix)
290
            '#4271B5',
291
            '#FE8E16',
292
            '#45C7F0',
293
            '#BCD631',
294
            '#D63173',
295
            '#D7D7D7',
296
            '#90AFDD',
297
            '#AF8640',
298
            '#4F9242',
299
            '#F4EB24',
300
            '#ED2024',
301
            '#3B3B3B',
302
            '#F7BDE2',
303
        ];
304
305
        $content = '<tr>';
306
        $content .= '<td class="text-center" width="5%">';
307
        $content .= '<span class="fa fa-square fa-fw fa-2x" aria-hidden="true" style="color:'.
308
            $hotspotColors[$orderColor].'"></span>';
309
        $content .= '</td>';
310
        $content .= '<td class="text-left" width="25%">';
311
        $content .= "$answerId - $answer";
312
        $content .= '</td>';
313
314
        if (false === $exercise->hideComment) {
315
            $content .= '<td class="text-left" width="10%">';
316
            if (!$hide_expected_answer) {
317
                $status = Display::label(get_lang('Incorrect'), 'danger');
318
                if ($studentChoice) {
319
                    $status = Display::label(get_lang('Correct'), 'success');
320
                }
321
                $content .= $status;
322
            } else {
323
                $content .= '&nbsp;';
324
            }
325
            $content .= '</td>';
326
327
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedback_type) {
328
                $content .= '<td class="text-left" width="60%">';
329
                if ($studentChoice) {
330
                    $content .= '<span style="font-weight: bold; color: #008000;">'.Security::remove_XSS(nl2br($answerComment)).'</span>';
331
                } else {
332
                    $content .= '&nbsp;';
333
                }
334
                $content .= '</td>';
335
            } else {
336
                $content .= '<td class="text-left" width="60%">&nbsp;</td>';
337
            }
338
        }
339
340
        $content .= '</tr>';
341
342
        echo $content;
343
    }
344
345
    public static function displayMultipleAnswerDropdown(
346
        Exercise $exercise,
347
        Answer $answer,
348
        array $quizQuestionOptions,
349
        array $studentChoices,
350
        bool $showTotalScoreAndUserChoices = true
351
    ): string {
352
        if (true === $exercise->hideNoAnswer && empty($studentChoices)) {
353
            return '';
354
        }
355
356
        sort($studentChoices);
357
        $rightAnswers = array_column($answer->getAnswers(), 'answer');
358
        sort($rightAnswers);
359
360
        $allChoices = array_unique(
361
            array_merge($rightAnswers, $studentChoices)
362
        );
363
364
        $checkboxOn = Display::return_icon('checkbox_on.png', null, null, ICON_SIZE_TINY);
365
        $checkboxOff = Display::return_icon('checkbox_off.png', null, null, ICON_SIZE_TINY);
366
367
        $labelSuccess = Display::label(get_lang('Correct'), 'success');
368
        $labelIncorrect = Display::label(get_lang('Incorrect'), 'danger');
369
370
        $html = '';
371
372
        foreach ($allChoices as $choice) {
373
            $isStudentChoice = in_array($choice, $studentChoices);
374
            $isRightAnswer = in_array($choice, $rightAnswers);
375
376
            $hideExpectedAnswer = false;
377
378
            switch ($exercise->selectResultsDisabled()) {
379
                case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
380
                    $hideExpectedAnswer = true;
381
382
                    if (!$isRightAnswer && empty($studentChoices)) {
383
                        continue 2;
384
                    }
385
                    break;
386
                case RESULT_DISABLE_SHOW_SCORE_ONLY:
387
                    if (0 == $exercise->getFeedbackType()) {
388
                        $hideExpectedAnswer = true;
389
                    }
390
                    break;
391
                case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
392
                case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
393
                    $hideExpectedAnswer = true;
394
                    if ($showTotalScoreAndUserChoices) {
395
                        $hideExpectedAnswer = false;
396
                    }
397
                    break;
398
                case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
399
                    if (false === $showTotalScoreAndUserChoices && empty($studentChoices)) {
400
                        continue 2;
401
                    }
402
                    break;
403
            }
404
405
            $studentChoiceClass = '';
406
407
            if ($isRightAnswer
408
                && in_array(
409
                    $exercise->selectResultsDisabled(),
410
                    [
411
                        RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
412
                        RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
413
                    ]
414
                )
415
            ) {
416
                $studentChoiceClass = 'success';
417
            }
418
419
            $html .= '<tr class="'.$studentChoiceClass.'">';
420
            $html .= '<td class="text-center">'.($isStudentChoice ? $checkboxOn : $checkboxOff).'</td>';
421
422
            if ($exercise->showExpectedChoiceColumn()) {
423
                $html .= '<td class="text-center">';
424
425
                if ($hideExpectedAnswer) {
426
                    $html .= '&mdash;';
427
                } else {
428
                    $html .= $isRightAnswer ? $checkboxOn : $checkboxOff;
429
                }
430
431
                $html .= '</td>';
432
            }
433
434
            $answerText = isset($quizQuestionOptions[$choice]) ? $quizQuestionOptions[$choice]['name'] : get_lang('None');
435
436
            if ($exercise->export) {
437
                $answerText = strip_tags_blacklist($answerText, ['title', 'head']);
438
                // Fix answers that contains this tags
439
                $tags = ['<html>', '</html>', '<body>', '</body>'];
440
                $answerText = str_replace($tags, '', $answerText);
441
            }
442
443
            $html .= '<td>'.Security::remove_XSS($answerText).'</td>';
444
445
            if ($exercise->showExpectedChoice()) {
446
                $html .= '<td class="text-center">'.($isRightAnswer ? $labelSuccess : $labelIncorrect).'</td>';
447
            }
448
449
            $html .= '</tr>';
450
        }
451
452
        return $html;
453
    }
454
455
    /**
456
     * Display the answers to a multiple choice question.
457
     *
458
     * @param Exercise $exercise
459
     * @param int      $feedbackType                 Feedback type
460
     * @param int      $answerType                   Answer type
461
     * @param int      $studentChoice                Student choice
462
     * @param string   $answer                       Textual answer
463
     * @param string   $answerComment                Comment on answer
464
     * @param string   $answerCorrect                Correct answer comment
465
     * @param int      $id                           Exercise ID
466
     * @param int      $questionId                   Question ID
467
     * @param bool     $ans                          Whether to show the answer comment or not
468
     * @param bool     $resultsDisabled
469
     * @param bool     $showTotalScoreAndUserChoices
470
     * @param bool     $export
471
     */
472
    public static function display_unique_or_multiple_answer(
473
        $exercise,
474
        $feedbackType,
475
        $answerType,
476
        $studentChoice,
477
        $answer,
478
        $answerComment,
479
        $answerCorrect,
480
        $id,
481
        $questionId,
482
        $ans,
483
        $resultsDisabled,
484
        $showTotalScoreAndUserChoices,
485
        $export = false
486
    ) {
487
        if (true === $exercise->hideNoAnswer && empty($studentChoice)) {
488
            return '';
489
        }
490
        if ($export) {
491
            $answer = strip_tags_blacklist($answer, ['title', 'head']);
492
            // Fix answers that contains this tags
493
            $tags = [
494
                '<html>',
495
                '</html>',
496
                '<body>',
497
                '</body>',
498
            ];
499
            $answer = str_replace($tags, '', $answer);
500
        }
501
502
        $studentChoiceInt = (int) $studentChoice;
503
        $answerCorrectChoice = (int) $answerCorrect;
504
505
        $hide_expected_answer = false;
506
        $showComment = false;
507
        switch ($resultsDisabled) {
508
            case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
509
                $hide_expected_answer = true;
510
                $showComment = true;
511
                if (!$answerCorrect && empty($studentChoice)) {
512
                    return '';
513
                }
514
                break;
515
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
516
                if (0 == $feedbackType) {
517
                    $hide_expected_answer = true;
518
                }
519
                break;
520
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
521
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
522
                $hide_expected_answer = true;
523
                if ($showTotalScoreAndUserChoices) {
524
                    $hide_expected_answer = false;
525
                }
526
                break;
527
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
528
                if (false === $showTotalScoreAndUserChoices && empty($studentChoiceInt)) {
529
                    return '';
530
                }
531
                break;
532
        }
533
534
        $icon = in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION]) ? 'radio' : 'checkbox';
535
        $icon .= $studentChoice ? '_on' : '_off';
536
        $icon .= '.png';
537
        $iconAnswer = in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION]) ? 'radio' : 'checkbox';
538
        $iconAnswer .= $answerCorrect ? '_on' : '_off';
539
        $iconAnswer .= '.png';
540
541
        $studentChoiceClass = '';
542
        if (in_array(
543
            $resultsDisabled,
544
            [
545
                RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
546
                RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
547
            ]
548
        )
549
        ) {
550
            if ($answerCorrect) {
551
                $studentChoiceClass = 'success';
552
            }
553
        }
554
555
        echo '<tr class="'.$studentChoiceClass.'">';
556
557
        echo '<td width="5%">';
558
        echo Display::return_icon($icon, null, null, ICON_SIZE_TINY);
559
        echo '</td>';
560
        if ($exercise->showExpectedChoiceColumn()) {
561
            if (false === $hide_expected_answer) {
562
                echo '<td width="5%">';
563
                echo Display::return_icon($iconAnswer, null, null, ICON_SIZE_TINY);
564
                echo '</td>';
565
            } else {
566
                echo '<td width="5%">';
567
                echo '-';
568
                echo '</td>';
569
            }
570
        }
571
572
        echo '<td width="40%">';
573
        echo Security::remove_XSS($answer);
574
        echo '</td>';
575
576
        if ($exercise->showExpectedChoice()) {
577
            $status = Display::label(get_lang('Incorrect'), 'danger');
578
            if ($answerCorrect || ($answerCorrect && $studentChoiceInt === $answerCorrectChoice)) {
579
                $status = Display::label(get_lang('Correct'), 'success');
580
            }
581
            echo '<td width="20%">';
582
            // Show only status for the selected student answer BT#16256
583
            if ($studentChoice) {
584
                echo $status;
585
            }
586
587
            echo '</td>';
588
        }
589
590
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
591
            $showComment = true;
592
        }
593
594
        if (false === $exercise->hideComment) {
595
            if ($showComment) {
596
                echo '<td width="20%">';
597
                $color = 'black';
598
                if ($answerCorrect) {
599
                    $color = 'green';
600
                }
601
                if ($hide_expected_answer) {
602
                    $color = '';
603
                }
604
                $comment = '<span style="font-weight: bold; color: '.$color.';">'.
605
                    Security::remove_XSS($answerComment).
606
                    '</span>';
607
                echo $comment;
608
                echo '</td>';
609
            } else {
610
                echo '<td>&nbsp;</td>';
611
            }
612
        }
613
614
        echo '</tr>';
615
    }
616
617
    /**
618
     * Display the answers to a multiple choice question.
619
     *
620
     * @param Exercise $exercise
621
     * @param int Answer type
622
     * @param int Student choice
623
     * @param string  Textual answer
624
     * @param string  Comment on answer
625
     * @param string  Correct answer comment
626
     * @param int Exercise ID
627
     * @param int Question ID
628
     * @param bool Whether to show the answer comment or not
629
     */
630
    public static function display_multiple_answer_true_false(
631
        $exercise,
632
        $feedbackType,
633
        $answerType,
634
        $studentChoice,
635
        $answer,
636
        $answerComment,
637
        $answerCorrect,
638
        $id,
639
        $questionId,
640
        $ans,
641
        $resultsDisabled,
642
        $showTotalScoreAndUserChoices
643
    ) {
644
        $hide_expected_answer = false;
645
        $hideStudentChoice = false;
646
        switch ($resultsDisabled) {
647
            //case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
648
            case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
649
                $hideStudentChoice = false;
650
                $hide_expected_answer = true;
651
                break;
652
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
653
                if (0 == $feedbackType) {
654
                    $hide_expected_answer = true;
655
                }
656
                break;
657
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
658
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
659
                $hide_expected_answer = true;
660
                if ($showTotalScoreAndUserChoices) {
661
                    $hide_expected_answer = false;
662
                }
663
                break;
664
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
665
                if (false === $showTotalScoreAndUserChoices && empty($studentChoice)) {
666
                    return '';
667
                }
668
                break;
669
        }
670
671
        $content = '<tr>';
672
        if (false === $hideStudentChoice) {
673
            $content .= '<td width="5%">';
674
            $course_id = api_get_course_int_id();
675
            $new_options = Question::readQuestionOption($questionId, $course_id);
676
            // Your choice
677
            if (isset($new_options[$studentChoice])) {
678
                $content .= get_lang($new_options[$studentChoice]['name']);
679
            } else {
680
                $content .= '-';
681
            }
682
            $content .= '</td>';
683
        }
684
685
        // Expected choice
686
        if ($exercise->showExpectedChoiceColumn()) {
687
            if (!$hide_expected_answer) {
688
                $content .= '<td width="5%">';
689
                if (isset($new_options[$answerCorrect])) {
690
                    $content .= get_lang($new_options[$answerCorrect]['name']);
691
                } else {
692
                    $content .= '-';
693
                }
694
                $content .= '</td>';
695
            }
696
        }
697
698
        $content .= '<td width="40%">';
699
        $content .= Security::remove_XSS($answer);
700
        $content .= '</td>';
701
702
        if ($exercise->showExpectedChoice()) {
703
            $status = Display::label(get_lang('Incorrect'), 'danger');
704
            if (isset($new_options[$studentChoice])) {
705
                if ($studentChoice == $answerCorrect) {
706
                    $status = Display::label(get_lang('Correct'), 'success');
707
                }
708
            }
709
            $content .= '<td width="20%">';
710
            $content .= $status;
711
            $content .= '</td>';
712
        }
713
714
        if (false === $exercise->hideComment) {
715
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
716
                $content .= '<td width="20%">';
717
                $color = 'black';
718
                if (isset($new_options[$studentChoice]) || in_array(
719
                        $exercise->results_disabled,
720
                        [
721
                            RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
722
                            RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
723
                        ]
724
                    )
725
                ) {
726
                    if ($studentChoice == $answerCorrect) {
727
                        $color = 'green';
728
                    }
729
730
                    if ($hide_expected_answer) {
731
                        $color = '';
732
                    }
733
                    $content .= '<span style="font-weight: bold; color: '.$color.';">'.Security::remove_XSS(nl2br($answerComment)).'</span>';
734
                }
735
                $content .= '</td>';
736
            }
737
        }
738
        $content .= '</tr>';
739
740
        echo $content;
741
    }
742
743
    /**
744
     * Display the answers to a multiple choice question.
745
     *
746
     * @param Exercise $exercise
747
     * @param int      $feedbackType
748
     * @param int      $studentChoice
749
     * @param int      $studentChoiceDegree
750
     * @param string   $answer
751
     * @param string   $answerComment
752
     * @param int      $answerCorrect
753
     * @param int      $questionId
754
     * @param bool     $inResultsDisabled
755
     */
756
    public static function displayMultipleAnswerTrueFalseDegreeCertainty(
757
        $exercise,
758
        $feedbackType,
759
        $studentChoice,
760
        $studentChoiceDegree,
761
        $answer,
762
        $answerComment,
763
        $answerCorrect,
764
        $questionId,
765
        $inResultsDisabled
766
    ) {
767
        $hideExpectedAnswer = false;
768
        if (0 == $feedbackType && 2 == $inResultsDisabled) {
769
            $hideExpectedAnswer = true;
770
        }
771
772
        echo '<tr><td width="5%">';
773
        $question = new MultipleAnswerTrueFalseDegreeCertainty();
774
        $courseId = api_get_course_int_id();
775
        $newOptions = Question::readQuestionOption($questionId, $courseId);
776
777
        // Your choice
778
        if (isset($newOptions[$studentChoice])) {
779
            echo get_lang($newOptions[$studentChoice]['name']);
780
        } else {
781
            echo '-';
782
        }
783
        echo '</td>';
784
785
        // Expected choice
786
        if ($exercise->showExpectedChoiceColumn()) {
787
            echo '<td width="5%">';
788
            if (!$hideExpectedAnswer) {
789
                if (isset($newOptions[$answerCorrect])) {
790
                    echo get_lang($newOptions[$answerCorrect]['name']);
791
                } else {
792
                    echo '-';
793
                }
794
            } else {
795
                echo '-';
796
            }
797
            echo '</td>';
798
        }
799
800
        echo '<td width="20%">';
801
        echo Security::remove_XSS($answer);
802
        echo '</td><td width="5%" style="text-align:center;">';
803
        if (isset($newOptions[$studentChoiceDegree])) {
804
            echo $newOptions[$studentChoiceDegree]['name'];
805
        }
806
        echo '</td>';
807
808
        $position = isset($newOptions[$studentChoiceDegree]) ? $newOptions[$studentChoiceDegree]['position'] : '';
809
        $degreeInfo = $question->getResponseDegreeInfo(
810
            $studentChoice,
811
            $answerCorrect,
812
            $position
813
        );
814
815
        $degreeInfo['color'] = isset($degreeInfo['color']) ? $degreeInfo['color'] : '';
816
        $degreeInfo['background-color'] = isset($degreeInfo['background-color']) ? $degreeInfo['background-color'] : '';
817
        $degreeInfo['description'] = isset($degreeInfo['description']) ? $degreeInfo['description'] : '';
818
        $degreeInfo['label'] = isset($degreeInfo['label']) ? $degreeInfo['label'] : '';
819
820
        echo '
821
            <td width="15%">
822
                <div style="text-align:center;color: '.$degreeInfo['color'].';
823
                    background-color: '.$degreeInfo['background-color'].';
824
                    line-height:30px;height:30px;width: 100%;margin:auto;"
825
                    title="'.$degreeInfo['description'].'">'.
826
                    nl2br($degreeInfo['label']).
827
                '</div>
828
            </td>';
829
830
        if (false === $exercise->hideComment) {
831
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
832
                echo '<td width="20%">';
833
                if (isset($newOptions[$studentChoice])) {
834
                    echo '<span style="font-weight: bold; color: black;">'.nl2br($answerComment).'</span>';
835
                }
836
                echo '</td>';
837
            } else {
838
                echo '<td>&nbsp;</td>';
839
            }
840
        }
841
842
        echo '</tr>';
843
    }
844
845
    /**
846
     * Display the answers to a multiple choice question.
847
     *
848
     * @param Exercise $exercise
849
     * @param int Answer type
850
     * @param int Student choice
851
     * @param string  Textual answer
852
     * @param string  Comment on answer
853
     * @param string  Correct answer comment
854
     * @param int Exercise ID
855
     * @param int Question ID
856
     * @param bool Whether to show the answer comment or not
857
     */
858
    public static function display_multiple_answer_combination_true_false(
859
        $exercise,
860
        $feedbackType,
861
        $answerType,
862
        $studentChoice,
863
        $answer,
864
        $answerComment,
865
        $answerCorrect,
866
        $id,
867
        $questionId,
868
        $ans,
869
        $resultsDisabled,
870
        $showTotalScoreAndUserChoices
871
    ) {
872
        $hide_expected_answer = false;
873
        $hideStudentChoice = false;
874
        switch ($resultsDisabled) {
875
            case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
876
            case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
877
                $hideStudentChoice = true;
878
                $hide_expected_answer = true;
879
                break;
880
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
881
                if (0 == $feedbackType) {
882
                    $hide_expected_answer = true;
883
                }
884
                break;
885
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
886
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
887
                $hide_expected_answer = true;
888
                if ($showTotalScoreAndUserChoices) {
889
                    $hide_expected_answer = false;
890
                }
891
                break;
892
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
893
                if (false === $showTotalScoreAndUserChoices && empty($studentChoice)) {
894
                    return '';
895
                }
896
                break;
897
        }
898
899
        echo '<tr>';
900
        if (false === $hideStudentChoice) {
901
            echo '<td width="5%">';
902
            // Your choice
903
            $question = new MultipleAnswerCombinationTrueFalse();
904
            if (isset($question->options[$studentChoice])) {
905
                echo $question->options[$studentChoice];
906
            } else {
907
                echo $question->options[2];
908
            }
909
            echo '</td>';
910
        }
911
912
        // Expected choice
913
        if ($exercise->showExpectedChoiceColumn()) {
914
            if (!$hide_expected_answer) {
915
                echo '<td width="5%">';
916
                if (isset($question->options[$answerCorrect])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $question does not seem to be defined for all execution paths leading up to this point.
Loading history...
917
                    echo $question->options[$answerCorrect];
918
                } else {
919
                    echo $question->options[2];
920
                }
921
                echo '</td>';
922
            }
923
        }
924
925
        echo '<td width="40%">';
926
        echo Security::remove_XSS($answer);
927
        echo '</td>';
928
929
        if ($exercise->showExpectedChoice()) {
930
            $status = '';
931
            if (isset($studentChoice)) {
932
                $status = Display::label(get_lang('Incorrect'), 'danger');
933
                if ($studentChoice == $answerCorrect) {
934
                    $status = Display::label(get_lang('Correct'), 'success');
935
                }
936
            }
937
            echo '<td width="20%">';
938
            echo $status;
939
            echo '</td>';
940
        }
941
942
        if (false === $exercise->hideComment) {
943
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
944
                echo '<td width="20%">';
945
                //@todo replace this harcoded value
946
                if ($studentChoice || in_array(
947
                        $resultsDisabled,
948
                        [
949
                            RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
950
                            RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
951
                        ]
952
                    )
953
                ) {
954
                    $color = 'black';
955
                    if ($studentChoice == $answerCorrect) {
956
                        $color = 'green';
957
                    }
958
                    if ($hide_expected_answer) {
959
                        $color = '';
960
                    }
961
                    echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
962
                }
963
                echo '</td>';
964
            } else {
965
                echo '<td>&nbsp;</td>';
966
            }
967
        }
968
        echo '</tr>';
969
    }
970
971
    /**
972
     * @param int  $feedbackType
973
     * @param int  $exeId
974
     * @param int  $questionId
975
     * @param null $questionScore
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $questionScore is correct as it would always require null to be passed?
Loading history...
976
     * @param int  $resultsDisabled
977
     */
978
    public static function displayAnnotationAnswer(
979
        $feedbackType,
980
        $exeId,
981
        $questionId,
982
        $questionScore = null,
983
        $resultsDisabled = 0
984
    ) {
985
        $comments = Event::get_comments($exeId, $questionId);
986
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
987
            if ($questionScore <= 0 && empty($comments)) {
988
                echo '<br />'.ExerciseLib::getNotCorrectedYetText();
989
            }
990
        }
991
    }
992
}
993