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

displayMultipleAnswerDropdown()   F

Complexity

Conditions 25
Paths 1734

Size

Total Lines 114
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 66
c 2
b 0
f 0
dl 0
loc 114
rs 0
cc 25
nop 5
nc 1734

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 $correctAnswers,
349
        array $studentChoices,
350
        bool $showTotalScoreAndUserChoices = true
351
    ): string {
352
        if (true === $exercise->hideNoAnswer && empty($studentChoices)) {
353
            return '';
354
        }
355
356
        $studentChoices = array_filter(
357
            $studentChoices,
358
            function ($studentAnswerId) {
359
                return -1 !== (int) $studentAnswerId;
360
            }
361
        );
362
363
        $allChoices = array_unique(
364
            array_merge($correctAnswers, $studentChoices)
365
        );
366
        sort($allChoices);
367
368
        $checkboxOn = Display::return_icon('checkbox_on.png', null, null, ICON_SIZE_TINY);
369
        $checkboxOff = Display::return_icon('checkbox_off.png', null, null, ICON_SIZE_TINY);
370
371
        $labelSuccess = Display::label(get_lang('Correct'), 'success');
372
        $labelIncorrect = Display::label(get_lang('Incorrect'), 'danger');
373
374
        $html = '';
375
376
        foreach ($allChoices as $choice) {
377
            $isStudentAnswer = in_array($choice, $studentChoices);
378
            $isExpectedAnswer = in_array($choice, $correctAnswers);
379
            $isCorrectAnswer = $isStudentAnswer && $isExpectedAnswer;
380
            $answerPosition = array_search($choice, $answer->iid);
381
382
            $hideExpectedAnswer = false;
383
384
            switch ($exercise->selectResultsDisabled()) {
385
                case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
386
                    $hideExpectedAnswer = true;
387
388
                    if (!$isCorrectAnswer && empty($studentChoices)) {
389
                        continue 2;
390
                    }
391
                    break;
392
                case RESULT_DISABLE_SHOW_SCORE_ONLY:
393
                    if (0 == $exercise->getFeedbackType()) {
394
                        $hideExpectedAnswer = true;
395
                    }
396
                    break;
397
                case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
398
                case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
399
                    $hideExpectedAnswer = true;
400
                    if ($showTotalScoreAndUserChoices) {
401
                        $hideExpectedAnswer = false;
402
                    }
403
                    break;
404
                case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
405
                    if (false === $showTotalScoreAndUserChoices && empty($studentChoices)) {
406
                        continue 2;
407
                    }
408
                    break;
409
            }
410
411
            $studentAnswerClass = '';
412
413
            if ($isCorrectAnswer
414
                && in_array(
415
                    $exercise->selectResultsDisabled(),
416
                    [
417
                        RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
418
                        RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
419
                    ]
420
                )
421
            ) {
422
                $studentAnswerClass = 'success';
423
            }
424
425
            $html .= '<tr class="'.$studentAnswerClass.'">';
426
            $html .= '<td class="text-center">'.($isStudentAnswer ? $checkboxOn : $checkboxOff).'</td>';
427
428
            if ($exercise->showExpectedChoiceColumn()) {
429
                $html .= '<td class="text-center">';
430
431
                if ($hideExpectedAnswer) {
432
                    $html .= '<span class="text-muted">&mdash;</span>';
433
                } else {
434
                    $html .= $isExpectedAnswer ? $checkboxOn : $checkboxOff;
435
                }
436
437
                $html .= '</td>';
438
            }
439
440
            $answerText = $answer->answer[$answerPosition] ?? get_lang('None');
441
442
            if ($exercise->export) {
443
                $answerText = strip_tags_blacklist($answerText, ['title', 'head']);
444
                // Fix answers that contains this tags
445
                $tags = ['<html>', '</html>', '<body>', '</body>'];
446
                $answerText = str_replace($tags, '', $answerText);
447
            }
448
449
            $html .= '<td>'.Security::remove_XSS($answerText).'</td>';
450
451
            if ($exercise->showExpectedChoice()) {
452
                $html .= '<td class="text-center">'.($isCorrectAnswer ? $labelSuccess : $labelIncorrect).'</td>';
453
            }
454
455
            $html .= '</tr>';
456
        }
457
458
        return $html;
459
    }
460
461
    /**
462
     * Display the answers to a multiple choice question.
463
     *
464
     * @param Exercise $exercise
465
     * @param int      $feedbackType                 Feedback type
466
     * @param int      $answerType                   Answer type
467
     * @param int      $studentChoice                Student choice
468
     * @param string   $answer                       Textual answer
469
     * @param string   $answerComment                Comment on answer
470
     * @param string   $answerCorrect                Correct answer comment
471
     * @param int      $id                           Exercise ID
472
     * @param int      $questionId                   Question ID
473
     * @param bool     $ans                          Whether to show the answer comment or not
474
     * @param bool     $resultsDisabled
475
     * @param bool     $showTotalScoreAndUserChoices
476
     * @param bool     $export
477
     */
478
    public static function display_unique_or_multiple_answer(
479
        $exercise,
480
        $feedbackType,
481
        $answerType,
482
        $studentChoice,
483
        $answer,
484
        $answerComment,
485
        $answerCorrect,
486
        $id,
487
        $questionId,
488
        $ans,
489
        $resultsDisabled,
490
        $showTotalScoreAndUserChoices,
491
        $export = false
492
    ) {
493
        if (true === $exercise->hideNoAnswer && empty($studentChoice)) {
494
            return '';
495
        }
496
        if ($export) {
497
            $answer = strip_tags_blacklist($answer, ['title', 'head']);
498
            // Fix answers that contains this tags
499
            $tags = [
500
                '<html>',
501
                '</html>',
502
                '<body>',
503
                '</body>',
504
            ];
505
            $answer = str_replace($tags, '', $answer);
506
        }
507
508
        $studentChoiceInt = (int) $studentChoice;
509
        $answerCorrectChoice = (int) $answerCorrect;
510
511
        $hide_expected_answer = false;
512
        $showComment = false;
513
        switch ($resultsDisabled) {
514
            case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
515
                $hide_expected_answer = true;
516
                $showComment = true;
517
                if (!$answerCorrect && empty($studentChoice)) {
518
                    return '';
519
                }
520
                break;
521
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
522
                if (0 == $feedbackType) {
523
                    $hide_expected_answer = true;
524
                }
525
                break;
526
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
527
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
528
                $hide_expected_answer = true;
529
                if ($showTotalScoreAndUserChoices) {
530
                    $hide_expected_answer = false;
531
                }
532
                break;
533
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
534
                if (false === $showTotalScoreAndUserChoices && empty($studentChoiceInt)) {
535
                    return '';
536
                }
537
                break;
538
        }
539
540
        $icon = in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION]) ? 'radio' : 'checkbox';
541
        $icon .= $studentChoice ? '_on' : '_off';
542
        $icon .= '.png';
543
        $iconAnswer = in_array($answerType, [UNIQUE_ANSWER, UNIQUE_ANSWER_NO_OPTION]) ? 'radio' : 'checkbox';
544
        $iconAnswer .= $answerCorrect ? '_on' : '_off';
545
        $iconAnswer .= '.png';
546
547
        $studentChoiceClass = '';
548
        if (in_array(
549
            $resultsDisabled,
550
            [
551
                RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
552
                RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
553
            ]
554
        )
555
        ) {
556
            if ($answerCorrect) {
557
                $studentChoiceClass = 'success';
558
            }
559
        }
560
561
        echo '<tr class="'.$studentChoiceClass.'">';
562
563
        echo '<td width="5%">';
564
        echo Display::return_icon($icon, null, null, ICON_SIZE_TINY);
565
        echo '</td>';
566
        if ($exercise->showExpectedChoiceColumn()) {
567
            if (false === $hide_expected_answer) {
568
                echo '<td width="5%">';
569
                echo Display::return_icon($iconAnswer, null, null, ICON_SIZE_TINY);
570
                echo '</td>';
571
            } else {
572
                echo '<td width="5%">';
573
                echo '-';
574
                echo '</td>';
575
            }
576
        }
577
578
        echo '<td width="40%">';
579
        echo Security::remove_XSS($answer);
580
        echo '</td>';
581
582
        if ($exercise->showExpectedChoice()) {
583
            $status = Display::label(get_lang('Incorrect'), 'danger');
584
            if ($answerCorrect || ($answerCorrect && $studentChoiceInt === $answerCorrectChoice)) {
585
                $status = Display::label(get_lang('Correct'), 'success');
586
            }
587
            echo '<td width="20%">';
588
            // Show only status for the selected student answer BT#16256
589
            if ($studentChoice) {
590
                echo $status;
591
            }
592
593
            echo '</td>';
594
        }
595
596
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
597
            $showComment = true;
598
        }
599
600
        if (false === $exercise->hideComment) {
601
            if ($showComment) {
602
                echo '<td width="20%">';
603
                $color = 'black';
604
                if ($answerCorrect) {
605
                    $color = 'green';
606
                }
607
                if ($hide_expected_answer) {
608
                    $color = '';
609
                }
610
                $comment = '<span style="font-weight: bold; color: '.$color.';">'.
611
                    Security::remove_XSS($answerComment).
612
                    '</span>';
613
                echo $comment;
614
                echo '</td>';
615
            } else {
616
                echo '<td>&nbsp;</td>';
617
            }
618
        }
619
620
        echo '</tr>';
621
    }
622
623
    /**
624
     * Display the answers to a multiple choice question.
625
     *
626
     * @param Exercise $exercise
627
     * @param int Answer type
628
     * @param int Student choice
629
     * @param string  Textual answer
630
     * @param string  Comment on answer
631
     * @param string  Correct answer comment
632
     * @param int Exercise ID
633
     * @param int Question ID
634
     * @param bool Whether to show the answer comment or not
635
     */
636
    public static function display_multiple_answer_true_false(
637
        $exercise,
638
        $feedbackType,
639
        $answerType,
640
        $studentChoice,
641
        $answer,
642
        $answerComment,
643
        $answerCorrect,
644
        $id,
645
        $questionId,
646
        $ans,
647
        $resultsDisabled,
648
        $showTotalScoreAndUserChoices
649
    ) {
650
        $hide_expected_answer = false;
651
        $hideStudentChoice = false;
652
        switch ($resultsDisabled) {
653
            //case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
654
            case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
655
                $hideStudentChoice = false;
656
                $hide_expected_answer = true;
657
                break;
658
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
659
                if (0 == $feedbackType) {
660
                    $hide_expected_answer = true;
661
                }
662
                break;
663
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
664
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
665
                $hide_expected_answer = true;
666
                if ($showTotalScoreAndUserChoices) {
667
                    $hide_expected_answer = false;
668
                }
669
                break;
670
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
671
                if (false === $showTotalScoreAndUserChoices && empty($studentChoice)) {
672
                    return '';
673
                }
674
                break;
675
        }
676
677
        $content = '<tr>';
678
        if (false === $hideStudentChoice) {
679
            $content .= '<td width="5%">';
680
            $course_id = api_get_course_int_id();
681
            $new_options = Question::readQuestionOption($questionId, $course_id);
682
            // Your choice
683
            if (isset($new_options[$studentChoice])) {
684
                $content .= get_lang($new_options[$studentChoice]['name']);
685
            } else {
686
                $content .= '-';
687
            }
688
            $content .= '</td>';
689
        }
690
691
        // Expected choice
692
        if ($exercise->showExpectedChoiceColumn()) {
693
            if (!$hide_expected_answer) {
694
                $content .= '<td width="5%">';
695
                if (isset($new_options[$answerCorrect])) {
696
                    $content .= get_lang($new_options[$answerCorrect]['name']);
697
                } else {
698
                    $content .= '-';
699
                }
700
                $content .= '</td>';
701
            }
702
        }
703
704
        $content .= '<td width="40%">';
705
        $content .= Security::remove_XSS($answer);
706
        $content .= '</td>';
707
708
        if ($exercise->showExpectedChoice()) {
709
            $status = Display::label(get_lang('Incorrect'), 'danger');
710
            if (isset($new_options[$studentChoice])) {
711
                if ($studentChoice == $answerCorrect) {
712
                    $status = Display::label(get_lang('Correct'), 'success');
713
                }
714
            }
715
            $content .= '<td width="20%">';
716
            $content .= $status;
717
            $content .= '</td>';
718
        }
719
720
        if (false === $exercise->hideComment) {
721
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
722
                $content .= '<td width="20%">';
723
                $color = 'black';
724
                if (isset($new_options[$studentChoice]) || in_array(
725
                        $exercise->results_disabled,
726
                        [
727
                            RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
728
                            RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
729
                        ]
730
                    )
731
                ) {
732
                    if ($studentChoice == $answerCorrect) {
733
                        $color = 'green';
734
                    }
735
736
                    if ($hide_expected_answer) {
737
                        $color = '';
738
                    }
739
                    $content .= '<span style="font-weight: bold; color: '.$color.';">'.Security::remove_XSS(nl2br($answerComment)).'</span>';
740
                }
741
                $content .= '</td>';
742
            }
743
        }
744
        $content .= '</tr>';
745
746
        echo $content;
747
    }
748
749
    /**
750
     * Display the answers to a multiple choice question.
751
     *
752
     * @param Exercise $exercise
753
     * @param int      $feedbackType
754
     * @param int      $studentChoice
755
     * @param int      $studentChoiceDegree
756
     * @param string   $answer
757
     * @param string   $answerComment
758
     * @param int      $answerCorrect
759
     * @param int      $questionId
760
     * @param bool     $inResultsDisabled
761
     */
762
    public static function displayMultipleAnswerTrueFalseDegreeCertainty(
763
        $exercise,
764
        $feedbackType,
765
        $studentChoice,
766
        $studentChoiceDegree,
767
        $answer,
768
        $answerComment,
769
        $answerCorrect,
770
        $questionId,
771
        $inResultsDisabled
772
    ) {
773
        $hideExpectedAnswer = false;
774
        if (0 == $feedbackType && 2 == $inResultsDisabled) {
775
            $hideExpectedAnswer = true;
776
        }
777
778
        echo '<tr><td width="5%">';
779
        $question = new MultipleAnswerTrueFalseDegreeCertainty();
780
        $courseId = api_get_course_int_id();
781
        $newOptions = Question::readQuestionOption($questionId, $courseId);
782
783
        // Your choice
784
        if (isset($newOptions[$studentChoice])) {
785
            echo get_lang($newOptions[$studentChoice]['name']);
786
        } else {
787
            echo '-';
788
        }
789
        echo '</td>';
790
791
        // Expected choice
792
        if ($exercise->showExpectedChoiceColumn()) {
793
            echo '<td width="5%">';
794
            if (!$hideExpectedAnswer) {
795
                if (isset($newOptions[$answerCorrect])) {
796
                    echo get_lang($newOptions[$answerCorrect]['name']);
797
                } else {
798
                    echo '-';
799
                }
800
            } else {
801
                echo '-';
802
            }
803
            echo '</td>';
804
        }
805
806
        echo '<td width="20%">';
807
        echo Security::remove_XSS($answer);
808
        echo '</td><td width="5%" style="text-align:center;">';
809
        if (isset($newOptions[$studentChoiceDegree])) {
810
            echo $newOptions[$studentChoiceDegree]['name'];
811
        }
812
        echo '</td>';
813
814
        $position = isset($newOptions[$studentChoiceDegree]) ? $newOptions[$studentChoiceDegree]['position'] : '';
815
        $degreeInfo = $question->getResponseDegreeInfo(
816
            $studentChoice,
817
            $answerCorrect,
818
            $position
819
        );
820
821
        $degreeInfo['color'] = isset($degreeInfo['color']) ? $degreeInfo['color'] : '';
822
        $degreeInfo['background-color'] = isset($degreeInfo['background-color']) ? $degreeInfo['background-color'] : '';
823
        $degreeInfo['description'] = isset($degreeInfo['description']) ? $degreeInfo['description'] : '';
824
        $degreeInfo['label'] = isset($degreeInfo['label']) ? $degreeInfo['label'] : '';
825
826
        echo '
827
            <td width="15%">
828
                <div style="text-align:center;color: '.$degreeInfo['color'].';
829
                    background-color: '.$degreeInfo['background-color'].';
830
                    line-height:30px;height:30px;width: 100%;margin:auto;"
831
                    title="'.$degreeInfo['description'].'">'.
832
                    nl2br($degreeInfo['label']).
833
                '</div>
834
            </td>';
835
836
        if (false === $exercise->hideComment) {
837
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
838
                echo '<td width="20%">';
839
                if (isset($newOptions[$studentChoice])) {
840
                    echo '<span style="font-weight: bold; color: black;">'.nl2br($answerComment).'</span>';
841
                }
842
                echo '</td>';
843
            } else {
844
                echo '<td>&nbsp;</td>';
845
            }
846
        }
847
848
        echo '</tr>';
849
    }
850
851
    /**
852
     * Display the answers to a multiple choice question.
853
     *
854
     * @param Exercise $exercise
855
     * @param int Answer type
856
     * @param int Student choice
857
     * @param string  Textual answer
858
     * @param string  Comment on answer
859
     * @param string  Correct answer comment
860
     * @param int Exercise ID
861
     * @param int Question ID
862
     * @param bool Whether to show the answer comment or not
863
     */
864
    public static function display_multiple_answer_combination_true_false(
865
        $exercise,
866
        $feedbackType,
867
        $answerType,
868
        $studentChoice,
869
        $answer,
870
        $answerComment,
871
        $answerCorrect,
872
        $id,
873
        $questionId,
874
        $ans,
875
        $resultsDisabled,
876
        $showTotalScoreAndUserChoices
877
    ) {
878
        $hide_expected_answer = false;
879
        $hideStudentChoice = false;
880
        switch ($resultsDisabled) {
881
            case RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING:
882
            case RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER:
883
                $hideStudentChoice = true;
884
                $hide_expected_answer = true;
885
                break;
886
            case RESULT_DISABLE_SHOW_SCORE_ONLY:
887
                if (0 == $feedbackType) {
888
                    $hide_expected_answer = true;
889
                }
890
                break;
891
            case RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK:
892
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT:
893
                $hide_expected_answer = true;
894
                if ($showTotalScoreAndUserChoices) {
895
                    $hide_expected_answer = false;
896
                }
897
                break;
898
            case RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK:
899
                if (false === $showTotalScoreAndUserChoices && empty($studentChoice)) {
900
                    return '';
901
                }
902
                break;
903
        }
904
905
        echo '<tr>';
906
        if (false === $hideStudentChoice) {
907
            echo '<td width="5%">';
908
            // Your choice
909
            $question = new MultipleAnswerCombinationTrueFalse();
910
            if (isset($question->options[$studentChoice])) {
911
                echo $question->options[$studentChoice];
912
            } else {
913
                echo $question->options[2];
914
            }
915
            echo '</td>';
916
        }
917
918
        // Expected choice
919
        if ($exercise->showExpectedChoiceColumn()) {
920
            if (!$hide_expected_answer) {
921
                echo '<td width="5%">';
922
                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...
923
                    echo $question->options[$answerCorrect];
924
                } else {
925
                    echo $question->options[2];
926
                }
927
                echo '</td>';
928
            }
929
        }
930
931
        echo '<td width="40%">';
932
        echo Security::remove_XSS($answer);
933
        echo '</td>';
934
935
        if ($exercise->showExpectedChoice()) {
936
            $status = '';
937
            if (isset($studentChoice)) {
938
                $status = Display::label(get_lang('Incorrect'), 'danger');
939
                if ($studentChoice == $answerCorrect) {
940
                    $status = Display::label(get_lang('Correct'), 'success');
941
                }
942
            }
943
            echo '<td width="20%">';
944
            echo $status;
945
            echo '</td>';
946
        }
947
948
        if (false === $exercise->hideComment) {
949
            if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
950
                echo '<td width="20%">';
951
                //@todo replace this harcoded value
952
                if ($studentChoice || in_array(
953
                        $resultsDisabled,
954
                        [
955
                            RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
956
                            RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
957
                        ]
958
                    )
959
                ) {
960
                    $color = 'black';
961
                    if ($studentChoice == $answerCorrect) {
962
                        $color = 'green';
963
                    }
964
                    if ($hide_expected_answer) {
965
                        $color = '';
966
                    }
967
                    echo '<span style="font-weight: bold; color: '.$color.';">'.nl2br($answerComment).'</span>';
968
                }
969
                echo '</td>';
970
            } else {
971
                echo '<td>&nbsp;</td>';
972
            }
973
        }
974
        echo '</tr>';
975
    }
976
977
    /**
978
     * @param int  $feedbackType
979
     * @param int  $exeId
980
     * @param int  $questionId
981
     * @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...
982
     * @param int  $resultsDisabled
983
     */
984
    public static function displayAnnotationAnswer(
985
        $feedbackType,
986
        $exeId,
987
        $questionId,
988
        $questionScore = null,
989
        $resultsDisabled = 0
990
    ) {
991
        $comments = Event::get_comments($exeId, $questionId);
992
        if (EXERCISE_FEEDBACK_TYPE_EXAM != $feedbackType) {
993
            if ($questionScore <= 0 && empty($comments)) {
994
                echo '<br />'.ExerciseLib::getNotCorrectedYetText();
995
            }
996
        }
997
    }
998
}
999