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

ExerciseShowFunctions::display_hotspot_answer()   D

Complexity

Conditions 18
Paths 101

Size

Total Lines 98
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 67
nc 101
nop 9
dl 0
loc 98
rs 4.8583
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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