Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/inc/lib/exercise.lib.php (1 issue)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
6
use Chamilo\CoreBundle\Entity\TrackEExercises;
7
use ChamiloSession as Session;
8
9
/**
10
 * Class ExerciseLib
11
 * shows a question and its answers.
12
 *
13
 * @author Olivier Brouckaert <[email protected]> 2003-2004
14
 * @author Hubert Borderiou 2011-10-21
15
 * @author ivantcholakov2009-07-20
16
 * @author Julio Montoya
17
 */
18
class ExerciseLib
19
{
20
    /**
21
     * Shows a question.
22
     *
23
     * @param Exercise $exercise
24
     * @param int      $questionId     $questionId question id
25
     * @param bool     $only_questions if true only show the questions, no exercise title
26
     * @param bool     $origin         i.e = learnpath
27
     * @param string   $current_item   current item from the list of questions
28
     * @param bool     $show_title
29
     * @param bool     $freeze
30
     * @param array    $user_choice
31
     * @param bool     $show_comment
32
     * @param bool     $show_answers
33
     *
34
     * @throws \Exception
35
     *
36
     * @return bool|int
37
     */
38
    public static function showQuestion(
39
        $exercise,
40
        $questionId,
41
        $only_questions = false,
42
        $origin = false,
43
        $current_item = '',
44
        $show_title = true,
45
        $freeze = false,
46
        $user_choice = [],
47
        $show_comment = false,
48
        $show_answers = false,
49
        $show_icon = false
50
    ) {
51
        $course_id = $exercise->course_id;
52
        $exerciseId = $exercise->iid;
53
54
        if (empty($course_id)) {
55
            return '';
56
        }
57
        $course = $exercise->course;
58
59
        // Change false to true in the following line to enable answer hinting
60
        $debug_mark_answer = $show_answers;
61
        // Reads question information
62
        if (!$objQuestionTmp = Question::read($questionId, $course)) {
63
            // Question not found
64
            return false;
65
        }
66
67
        $questionRequireAuth = WhispeakAuthPlugin::questionRequireAuthentify($questionId);
68
69
        if ($exercise->getFeedbackType() != EXERCISE_FEEDBACK_TYPE_END) {
70
            $show_comment = false;
71
        }
72
73
        $answerType = $objQuestionTmp->selectType();
74
        $pictureName = $objQuestionTmp->getPictureFilename();
75
        $s = '';
76
        if ($answerType != HOT_SPOT &&
77
            $answerType != HOT_SPOT_DELINEATION &&
78
            $answerType != ANNOTATION
79
        ) {
80
            // Question is not a hotspot
81
            if (!$only_questions) {
82
                $questionDescription = $objQuestionTmp->selectDescription();
83
                if ($show_title) {
84
                    if ($exercise->display_category_name) {
85
                        TestCategory::displayCategoryAndTitle($objQuestionTmp->iid);
86
                    }
87
                    $titleToDisplay = Security::remove_XSS($objQuestionTmp->getTitleToDisplay($current_item));
88
                    if ($answerType == READING_COMPREHENSION) {
89
                        // In READING_COMPREHENSION, the title of the question
90
                        // contains the question itself, which can only be
91
                        // shown at the end of the given time, so hide for now
92
                        $titleToDisplay = Display::div(
93
                            $current_item.'. '.get_lang('ReadingComprehension'),
94
                            ['class' => 'question_title']
95
                        );
96
                    }
97
                    echo $titleToDisplay;
98
                }
99
100
                if ($questionRequireAuth) {
101
                    WhispeakAuthPlugin::quizQuestionAuthentify($questionId, $exercise);
102
103
                    return false;
104
                }
105
106
                if (!empty($questionDescription) && $answerType != READING_COMPREHENSION) {
107
                    echo Display::div(
108
                        $questionDescription,
109
                        ['class' => 'question_description']
110
                    );
111
                }
112
            }
113
114
            if (in_array($answerType, [FREE_ANSWER, ORAL_EXPRESSION, UPLOAD_ANSWER]) && $freeze) {
115
                return '';
116
            }
117
118
            echo '<div class="question_options">';
119
            // construction of the Answer object (also gets all answers details)
120
            $objAnswerTmp = new Answer($questionId, $course_id, $exercise);
121
            $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
122
            $quizQuestionOptions = Question::readQuestionOption($questionId, $course_id);
123
124
            // For "matching" type here, we need something a little bit special
125
            // because the match between the suggestions and the answers cannot be
126
            // done easily (suggestions and answers are in the same table), so we
127
            // have to go through answers first (elems with "correct" value to 0).
128
            $select_items = [];
129
            //This will contain the number of answers on the left side. We call them
130
            // suggestions here, for the sake of comprehensions, while the ones
131
            // on the right side are called answers
132
            $num_suggestions = 0;
133
            switch ($answerType) {
134
                case MATCHING:
135
                case DRAGGABLE:
136
                case MATCHING_DRAGGABLE:
137
                    if ($answerType == DRAGGABLE) {
138
                        $isVertical = $objQuestionTmp->extra == 'v';
139
                        $s .= '
140
                            <div class="row">
141
                                <div class="col-md-12">
142
                                    <p class="small">'.get_lang('DraggableQuestionIntro').'</p>
143
                                    <ul class="exercise-draggable-answer list-unstyled '
144
                            .($isVertical ? '' : 'list-inline').'" id="question-'.$questionId.'" data-question="'
145
                            .$questionId.'">
146
                        ';
147
                    } else {
148
                        $s .= '<div id="drag'.$questionId.'_question" class="drag_question">
149
                               <table class="table table-hover table-striped data_table">';
150
                    }
151
152
                    // Iterate through answers.
153
                    $x = 1;
154
                    // Mark letters for each answer.
155
                    $letter = 'A';
156
                    $answer_matching = [];
157
                    $cpt1 = [];
158
                    for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
159
                        $answerCorrect = $objAnswerTmp->isCorrect($answerId);
160
                        $numAnswer = $objAnswerTmp->selectId($answerId);
161
                        if ($answerCorrect == 0) {
162
                            // options (A, B, C, ...) that will be put into the list-box
163
                            // have the "correct" field set to 0 because they are answer
164
                            $cpt1[$x] = $letter;
165
                            $answer_matching[$x] = $objAnswerTmp->selectAnswerById($numAnswer);
166
                            $x++;
167
                            $letter++;
168
                        }
169
                    }
170
171
                    $i = 1;
172
                    $select_items[0]['id'] = 0;
173
                    $select_items[0]['letter'] = '--';
174
                    $select_items[0]['answer'] = '';
175
                    foreach ($answer_matching as $id => $value) {
176
                        $select_items[$i]['id'] = $value['iid'];
177
                        $select_items[$i]['letter'] = $cpt1[$id];
178
                        $select_items[$i]['answer'] = $value['answer'];
179
                        $i++;
180
                    }
181
182
                    $user_choice_array_position = [];
183
                    if (!empty($user_choice)) {
184
                        foreach ($user_choice as $item) {
185
                            $user_choice_array_position[$item['position']] = $item['answer'];
186
                        }
187
                    }
188
                    $num_suggestions = ($nbrAnswers - $x) + 1;
189
                    break;
190
                case FREE_ANSWER:
191
                    $fck_content = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer'] : null;
192
                    $form = new FormValidator('free_choice_'.$questionId);
193
                    $config = [
194
                        'ToolbarSet' => 'TestFreeAnswer',
195
                        'id' => 'choice['.$questionId.']',
196
                    ];
197
                    $form->addHtmlEditor(
198
                        'choice['.$questionId.']',
199
                        null,
200
                        false,
201
                        false,
202
                        $config
203
                    );
204
                    $form->setDefaults(["choice[".$questionId."]" => $fck_content]);
205
                    $s .= $form->returnForm();
206
                    break;
207
                case UPLOAD_ANSWER:
208
                    global $exe_id;
209
                    $answer = isset($user_choice[0]) && !empty($user_choice[0]['answer']) ? $user_choice[0]['answer'] : null;
210
                    $path = '/upload_answer/'.$exe_id.'/'.$questionId.'/';
211
                    $url = api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?'.api_get_cidreq().'&a=upload_answer&curdirpath='.$path;
212
                    $multipleForm = new FormValidator(
213
                        'drag_drop',
214
                        'post',
215
                        '#',
216
                        ['enctype' => 'multipart/form-data']
217
                    );
218
                    $multipleForm->addMultipleUpload($url);
219
                    $s .= '<script>
220
                        $(function() {
221
                            $("#input_file_upload").bind("fileuploaddone", function (e, data) {
222
                                $.each(data.result.files, function (index, file) {
223
                                    if (file.name) {
224
                                        var input = $("<input>", {
225
                                            type: "hidden",
226
                                            name: "uploadChoice['.$questionId.'][]",
227
                                            value: file.name
228
                                        })
229
                                        $(data.context.children()[index]).parent().append(input);
230
                                    }
231
                                });
232
                            });
233
                        });
234
                    </script>';
235
                    // Set default values
236
                    if (!empty($answer)) {
237
                        $userWebpath = UserManager::getUserPathById(api_get_user_id(), 'web').'my_files'.'/upload_answer/'.$exe_id.'/'.$questionId.'/';
238
                        $filesNames = explode('|', $answer);
239
                        $icon = Display::return_icon('file_txt.gif');
240
                        $default = '';
241
                        foreach ($filesNames as $fileName) {
242
                            $fileName = Security::remove_XSS($fileName);
243
                            $default .= '<a target="_blank" class="panel-image" href="'.$userWebpath.$fileName.'"><div class="row"><div class="col-sm-4">'.$icon.'</div><div class="col-sm-5 file_name">'.$fileName.'</div><input type="hidden" name="uploadChoice['.$questionId.'][]" value="'.$fileName.'"></div></a>';
244
                        }
245
                        $s .= '<script>
246
                            $(function() {
247
                                if ($("#files").length > 0) {
248
                                  $("#files").html("'.addslashes($default).'");
249
                                }
250
                            });
251
                        </script>';
252
                    }
253
                    $s .= $multipleForm->returnForm();
254
                    break;
255
                case ORAL_EXPRESSION:
256
                    // Add nanog
257
                    if (api_get_setting('enable_record_audio') === 'true') {
258
                        //@todo pass this as a parameter
259
                        global $exercise_stat_info;
260
                        if (!empty($exercise_stat_info)) {
261
                            $objQuestionTmp->initFile(
262
                                api_get_session_id(),
263
                                api_get_user_id(),
264
                                $exercise_stat_info['exe_exo_id'],
265
                                $exercise_stat_info['exe_id']
266
                            );
267
                        } else {
268
                            $objQuestionTmp->initFile(
269
                                api_get_session_id(),
270
                                api_get_user_id(),
271
                                $exerciseId,
272
                                'temp_exe'
273
                            );
274
                        }
275
276
                        echo $objQuestionTmp->returnRecorder();
277
                    }
278
279
                    $form = new FormValidator('free_choice_'.$questionId);
280
                    $config = ['ToolbarSet' => 'TestFreeAnswer'];
281
282
                    $form->addHtml('<div id="'.'hide_description_'.$questionId.'_options" style="display: none;">');
283
                    $form->addHtmlEditor(
284
                        "choice[$questionId]",
285
                        null,
286
                        false,
287
                        false,
288
                        $config
289
                    );
290
                    $form->addHtml('</div>');
291
                    $s .= $form->returnForm();
292
                    break;
293
            }
294
295
            // Now navigate through the possible answers, using the max number of
296
            // answers for the question as a limiter
297
            $lines_count = 1; // a counter for matching-type answers
298
            if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE ||
299
                $answerType == MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE
300
            ) {
301
                $header = Display::tag('th', get_lang('Options'));
302
                foreach ($objQuestionTmp->options as $item) {
303
                    if ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
304
                        if (in_array($item, $objQuestionTmp->options)) {
305
                            $header .= Display::tag('th', get_lang($item));
306
                        } else {
307
                            $header .= Display::tag('th', $item);
308
                        }
309
                    } else {
310
                        $header .= Display::tag('th', $item);
311
                    }
312
                }
313
                if ($show_comment) {
314
                    $header .= Display::tag('th', get_lang('Feedback'));
315
                }
316
                $s .= '<table class="table table-hover table-striped">';
317
                $s .= Display::tag(
318
                    'tr',
319
                    $header,
320
                    ['style' => 'text-align:left;']
321
                );
322
            } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
323
                $header = Display::tag('th', get_lang('Options'), ['width' => '50%']);
324
                echo "
325
                <script>
326
                    function RadioValidator(question_id, answer_id)
327
                    {
328
                        var ShowAlert = '';
329
                        var typeRadioB = '';
330
                        var AllFormElements = window.document.getElementById('exercise_form').elements;
331
332
                        for (i = 0; i < AllFormElements.length; i++) {
333
                            if (AllFormElements[i].type == 'radio') {
334
                                var ThisRadio = AllFormElements[i].name;
335
                                var ThisChecked = 'No';
336
                                var AllRadioOptions = document.getElementsByName(ThisRadio);
337
338
                                for (x = 0; x < AllRadioOptions.length; x++) {
339
                                     if (AllRadioOptions[x].checked && ThisChecked == 'No') {
340
                                         ThisChecked = 'Yes';
341
                                         break;
342
                                     }
343
                                }
344
345
                                var AlreadySearched = ShowAlert.indexOf(ThisRadio);
346
                                if (ThisChecked == 'No' && AlreadySearched == -1) {
347
                                    ShowAlert = ShowAlert + ThisRadio;
348
                                }
349
                            }
350
                        }
351
                        if (ShowAlert != '') {
352
353
                        } else {
354
                            $('.question-validate-btn').removeAttr('disabled');
355
                        }
356
                    }
357
358
                    function handleRadioRow(event, question_id, answer_id) {
359
                        var t = event.target;
360
                        if (t && t.tagName == 'INPUT')
361
                            return;
362
                        while (t && t.tagName != 'TD') {
363
                            t = t.parentElement;
364
                        }
365
                        var r = t.getElementsByTagName('INPUT')[0];
366
                        r.click();
367
                        RadioValidator(question_id, answer_id);
368
                    }
369
370
                    $(function() {
371
                        var ShowAlert = '';
372
                        var typeRadioB = '';
373
                        var question_id = $('input[name=question_id]').val();
374
                        var AllFormElements = window.document.getElementById('exercise_form').elements;
375
376
                        for (i = 0; i < AllFormElements.length; i++) {
377
                            if (AllFormElements[i].type == 'radio') {
378
                                var ThisRadio = AllFormElements[i].name;
379
                                var ThisChecked = 'No';
380
                                var AllRadioOptions = document.getElementsByName(ThisRadio);
381
382
                                for (x = 0; x < AllRadioOptions.length; x++) {
383
                                    if (AllRadioOptions[x].checked && ThisChecked == 'No') {
384
                                        ThisChecked = \"Yes\";
385
                                        break;
386
                                    }
387
                                }
388
389
                                var AlreadySearched = ShowAlert.indexOf(ThisRadio);
390
                                if (ThisChecked == 'No' && AlreadySearched == -1) {
391
                                    ShowAlert = ShowAlert + ThisRadio;
392
                                }
393
                            }
394
                        }
395
396
                        if (ShowAlert != '') {
397
                             $('.question-validate-btn').attr('disabled', 'disabled');
398
                        } else {
399
                            $('.question-validate-btn').removeAttr('disabled');
400
                        }
401
                    });
402
                </script>";
403
404
                foreach ($objQuestionTmp->optionsTitle as $item) {
405
                    if (in_array($item, $objQuestionTmp->optionsTitle)) {
406
                        $properties = [];
407
                        if ($item === 'Answers') {
408
                            $properties['colspan'] = 2;
409
                            $properties['style'] = 'background-color: #F56B2A; color: #ffffff;';
410
                        } elseif ($item == 'DegreeOfCertaintyThatMyAnswerIsCorrect') {
411
                            $properties['colspan'] = 6;
412
                            $properties['style'] = 'background-color: #330066; color: #ffffff;';
413
                        }
414
                        $header .= Display::tag('th', get_lang($item), $properties);
415
                    } else {
416
                        $header .= Display::tag('th', $item);
417
                    }
418
                }
419
420
                if ($show_comment) {
421
                    $header .= Display::tag('th', get_lang('Feedback'));
422
                }
423
424
                $s .= '<table class="table table-hover table-striped data_table">';
425
                $s .= Display::tag('tr', $header, ['style' => 'text-align:left;']);
426
427
                // ajout de la 2eme ligne d'entête pour true/falss et les pourcentages de certitude
428
                $header1 = Display::tag('th', '&nbsp;');
429
                $cpt1 = 0;
430
                foreach ($objQuestionTmp->options as $item) {
431
                    $colorBorder1 = $cpt1 == (count($objQuestionTmp->options) - 1)
432
                        ? '' : 'border-right: solid #FFFFFF 1px;';
433
                    if ($item === 'True' || $item === 'False') {
434
                        $header1 .= Display::tag(
435
                            'th',
436
                            get_lang($item),
437
                            ['style' => 'background-color: #F7C9B4; color: black;'.$colorBorder1]
438
                        );
439
                    } else {
440
                        $header1 .= Display::tag(
441
                            'th',
442
                            $item,
443
                            ['style' => 'background-color: #e6e6ff; color: black;padding:5px; '.$colorBorder1]
444
                        );
445
                    }
446
                    $cpt1++;
447
                }
448
                if ($show_comment) {
449
                    $header1 .= Display::tag('th', '&nbsp;');
450
                }
451
452
                $s .= Display::tag('tr', $header1);
453
454
                // add explanation
455
                $header2 = Display::tag('th', '&nbsp;');
456
                $descriptionList = [
457
                    get_lang('DegreeOfCertaintyIDeclareMyIgnorance'),
458
                    get_lang('DegreeOfCertaintyIAmVeryUnsure'),
459
                    get_lang('DegreeOfCertaintyIAmUnsure'),
460
                    get_lang('DegreeOfCertaintyIAmPrettySure'),
461
                    get_lang('DegreeOfCertaintyIAmSure'),
462
                    get_lang('DegreeOfCertaintyIAmVerySure'),
463
                ];
464
                $counter2 = 0;
465
                foreach ($objQuestionTmp->options as $item) {
466
                    if ($item === 'True' || $item === 'False') {
467
                        $header2 .= Display::tag('td',
468
                            '&nbsp;',
469
                            ['style' => 'background-color: #F7E1D7; color: black;border-right: solid #FFFFFF 1px;']);
470
                    } else {
471
                        $color_border2 = ($counter2 == (count($objQuestionTmp->options) - 1)) ?
472
                            '' : 'border-right: solid #FFFFFF 1px;font-size:11px;';
473
                        $header2 .= Display::tag(
474
                            'td',
475
                            nl2br($descriptionList[$counter2]),
476
                            ['style' => 'background-color: #EFEFFC; color: black; width: 110px; text-align:center;
477
                                vertical-align: top; padding:5px; '.$color_border2]);
478
                        $counter2++;
479
                    }
480
                }
481
                if ($show_comment) {
482
                    $header2 .= Display::tag('th', '&nbsp;');
483
                }
484
                $s .= Display::tag('tr', $header2);
485
            }
486
487
            if ($show_comment) {
488
                if (in_array(
489
                    $answerType,
490
                    [
491
                        MULTIPLE_ANSWER,
492
                        MULTIPLE_ANSWER_COMBINATION,
493
                        UNIQUE_ANSWER,
494
                        UNIQUE_ANSWER_IMAGE,
495
                        UNIQUE_ANSWER_NO_OPTION,
496
                        GLOBAL_MULTIPLE_ANSWER,
497
                    ]
498
                )) {
499
                    $header = Display::tag('th', get_lang('Options'));
500
                    if ($exercise->getFeedbackType() == EXERCISE_FEEDBACK_TYPE_END) {
501
                        $header .= Display::tag('th', get_lang('Feedback'));
502
                    }
503
                    $s .= '<table class="table table-hover table-striped">';
504
                    $s .= Display::tag(
505
                        'tr',
506
                        $header,
507
                        ['style' => 'text-align:left;']
508
                    );
509
                }
510
            }
511
512
            $matching_correct_answer = 0;
513
            $userChoiceList = [];
514
            if (!empty($user_choice)) {
515
                foreach ($user_choice as $item) {
516
                    $userChoiceList[] = $item['answer'];
517
                }
518
            }
519
520
            $hidingClass = '';
521
            if ($answerType == READING_COMPREHENSION) {
522
                $objQuestionTmp->setExerciseType($exercise->selectType());
523
                $objQuestionTmp->processText($objQuestionTmp->selectDescription());
524
                $hidingClass = 'hide-reading-answers';
525
                $s .= Display::div(
526
                    $objQuestionTmp->selectTitle(),
527
                    ['class' => 'question_title '.$hidingClass]
528
                );
529
            }
530
531
            $userStatus = STUDENT;
532
            // Allows to do a remove_XSS in question of exercise with user status COURSEMANAGER
533
            // see BT#18242
534
            if (api_get_configuration_value('question_exercise_html_strict_filtering')) {
535
                $userStatus = COURSEMANAGERLOWSECURITY;
536
            }
537
538
            for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
539
                $answer = $objAnswerTmp->selectAnswer($answerId);
540
                $answerCorrect = $objAnswerTmp->isCorrect($answerId);
541
                $numAnswer = $objAnswerTmp->selectId($answerId);
542
                $comment = Security::remove_XSS($objAnswerTmp->selectComment($answerId));
543
                $attributes = [];
544
545
                switch ($answerType) {
546
                    case UNIQUE_ANSWER:
547
                    case UNIQUE_ANSWER_NO_OPTION:
548
                    case UNIQUE_ANSWER_IMAGE:
549
                    case READING_COMPREHENSION:
550
                        $input_id = 'choice-'.$questionId.'-'.$answerId;
551
                        if (isset($user_choice[0]['answer']) && $user_choice[0]['answer'] == $numAnswer) {
552
                            $attributes = [
553
                                'id' => $input_id,
554
                                'checked' => 1,
555
                                'selected' => 1,
556
                            ];
557
                        } else {
558
                            $attributes = ['id' => $input_id];
559
                        }
560
561
                        if ($debug_mark_answer) {
562
                            if ($answerCorrect) {
563
                                $attributes['checked'] = 1;
564
                                $attributes['selected'] = 1;
565
                            }
566
                        }
567
568
                        if ($show_comment) {
569
                            $s .= '<tr><td>';
570
                        }
571
572
                        if ($answerType == UNIQUE_ANSWER_IMAGE) {
573
                            if ($show_comment) {
574
                                if (empty($comment)) {
575
                                    $s .= '<div id="answer'.$questionId.$numAnswer.'"
576
                                            class="exercise-unique-answer-image" style="text-align: center">';
577
                                } else {
578
                                    $s .= '<div id="answer'.$questionId.$numAnswer.'"
579
                                            class="exercise-unique-answer-image col-xs-6 col-sm-12"
580
                                            style="text-align: center">';
581
                                }
582
                            } else {
583
                                $s .= '<div id="answer'.$questionId.$numAnswer.'"
584
                                        class="exercise-unique-answer-image col-xs-6 col-md-3"
585
                                        style="text-align: center">';
586
                            }
587
                        }
588
589
                        if ($answerType != UNIQUE_ANSWER_IMAGE) {
590
                            $answer = Security::remove_XSS($answer, $userStatus);
591
                        }
592
                        $s .= Display::input(
593
                            'hidden',
594
                            'choice2['.$questionId.']',
595
                            '0'
596
                        );
597
598
                        $answer_input = null;
599
                        $attributes['class'] = 'checkradios';
600
                        if ($answerType == UNIQUE_ANSWER_IMAGE) {
601
                            $attributes['class'] = '';
602
                            $attributes['style'] = 'display: none;';
603
                            $answer = '<div class="thumbnail">'.$answer.'</div>';
604
                        }
605
606
                        $answer_input .= '<label class="radio '.$hidingClass.'">';
607
                        $answer_input .= Display::input(
608
                            'radio',
609
                            'choice['.$questionId.']',
610
                            $numAnswer,
611
                            $attributes
612
                        );
613
                        $answer_input .= $answer;
614
                        $answer_input .= '</label>';
615
616
                        if ($answerType == UNIQUE_ANSWER_IMAGE) {
617
                            $answer_input .= "</div>";
618
                        }
619
620
                        if ($show_comment) {
621
                            $s .= $answer_input;
622
                            $s .= '</td>';
623
                            $s .= '<td>';
624
                            $s .= $comment;
625
                            $s .= '</td>';
626
                            $s .= '</tr>';
627
                        } else {
628
                            $s .= $answer_input;
629
                        }
630
                        break;
631
                    case MULTIPLE_ANSWER:
632
                    case MULTIPLE_ANSWER_TRUE_FALSE:
633
                    case GLOBAL_MULTIPLE_ANSWER:
634
                    case MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY:
635
                        $input_id = 'choice-'.$questionId.'-'.$answerId;
636
                        $answer = Security::remove_XSS($answer, $userStatus);
637
638
                        if (in_array($numAnswer, $userChoiceList)) {
639
                            $attributes = [
640
                                'id' => $input_id,
641
                                'checked' => 1,
642
                                'selected' => 1,
643
                            ];
644
                        } else {
645
                            $attributes = ['id' => $input_id];
646
                        }
647
648
                        if ($debug_mark_answer) {
649
                            if ($answerCorrect) {
650
                                $attributes['checked'] = 1;
651
                                $attributes['selected'] = 1;
652
                            }
653
                        }
654
655
                        if ($answerType == MULTIPLE_ANSWER || $answerType == GLOBAL_MULTIPLE_ANSWER) {
656
                            $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
657
                            $attributes['class'] = 'checkradios';
658
                            $answer_input = '<label class="checkbox">';
659
                            $answer_input .= Display::input(
660
                                'checkbox',
661
                                'choice['.$questionId.']['.$numAnswer.']',
662
                                $numAnswer,
663
                                $attributes
664
                            );
665
                            $answer_input .= $answer;
666
                            $answer_input .= '</label>';
667
668
                            if ($show_comment) {
669
                                $s .= '<tr><td>';
670
                                $s .= $answer_input;
671
                                $s .= '</td>';
672
                                $s .= '<td>';
673
                                $s .= $comment;
674
                                $s .= '</td>';
675
                                $s .= '</tr>';
676
                            } else {
677
                                $s .= $answer_input;
678
                            }
679
                        } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE) {
680
                            $myChoice = [];
681
                            if (!empty($userChoiceList)) {
682
                                foreach ($userChoiceList as $item) {
683
                                    $item = explode(':', $item);
684
                                    if (!empty($item)) {
685
                                        $myChoice[$item[0]] = isset($item[1]) ? $item[1] : '';
686
                                    }
687
                                }
688
                            }
689
690
                            $s .= '<tr>';
691
                            $s .= Display::tag('td', $answer);
692
693
                            if (!empty($quizQuestionOptions)) {
694
                                foreach ($quizQuestionOptions as $id => $item) {
695
                                    if (isset($myChoice[$numAnswer]) && $id == $myChoice[$numAnswer]) {
696
                                        $attributes = [
697
                                            'checked' => 1,
698
                                            'selected' => 1,
699
                                        ];
700
                                    } else {
701
                                        $attributes = [];
702
                                    }
703
704
                                    if ($debug_mark_answer) {
705
                                        if ($id == $answerCorrect) {
706
                                            $attributes['checked'] = 1;
707
                                            $attributes['selected'] = 1;
708
                                        }
709
                                    }
710
                                    $s .= Display::tag(
711
                                        'td',
712
                                        Display::input(
713
                                            'radio',
714
                                            'choice['.$questionId.']['.$numAnswer.']',
715
                                            $id,
716
                                            $attributes
717
                                        ),
718
                                        ['style' => '']
719
                                    );
720
                                }
721
                            }
722
723
                            if ($show_comment) {
724
                                $s .= '<td>';
725
                                $s .= $comment;
726
                                $s .= '</td>';
727
                            }
728
                            $s .= '</tr>';
729
                        } elseif ($answerType == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
730
                            $myChoice = [];
731
                            if (!empty($userChoiceList)) {
732
                                foreach ($userChoiceList as $item) {
733
                                    $item = explode(':', $item);
734
                                    $myChoice[$item[0]] = $item[1];
735
                                }
736
                            }
737
                            $myChoiceDegreeCertainty = [];
738
                            if (!empty($userChoiceList)) {
739
                                foreach ($userChoiceList as $item) {
740
                                    $item = explode(':', $item);
741
                                    $myChoiceDegreeCertainty[$item[0]] = $item[2];
742
                                }
743
                            }
744
                            $s .= '<tr>';
745
                            $s .= Display::tag('td', $answer);
746
747
                            if (!empty($quizQuestionOptions)) {
748
                                foreach ($quizQuestionOptions as $id => $item) {
749
                                    if (isset($myChoice[$numAnswer]) && $id == $myChoice[$numAnswer]) {
750
                                        $attributes = ['checked' => 1, 'selected' => 1];
751
                                    } else {
752
                                        $attributes = [];
753
                                    }
754
                                    $attributes['onChange'] = 'RadioValidator('.$questionId.', '.$numAnswer.')';
755
756
                                    // radio button selection
757
                                    if (isset($myChoiceDegreeCertainty[$numAnswer]) &&
758
                                        $id == $myChoiceDegreeCertainty[$numAnswer]
759
                                    ) {
760
                                        $attributes1 = ['checked' => 1, 'selected' => 1];
761
                                    } else {
762
                                        $attributes1 = [];
763
                                    }
764
765
                                    $attributes1['onChange'] = 'RadioValidator('.$questionId.', '.$numAnswer.')';
766
767
                                    if ($debug_mark_answer) {
768
                                        if ($id == $answerCorrect) {
769
                                            $attributes['checked'] = 1;
770
                                            $attributes['selected'] = 1;
771
                                        }
772
                                    }
773
774
                                    if ($item['name'] == 'True' || $item['name'] == 'False') {
775
                                        $s .= Display::tag('td',
776
                                            Display::input('radio',
777
                                                'choice['.$questionId.']['.$numAnswer.']',
778
                                                $id,
779
                                                $attributes
780
                                            ),
781
                                            ['style' => 'text-align:center; background-color:#F7E1D7;',
782
                                                'onclick' => 'handleRadioRow(event, '.
783
                                                    $questionId.', '.
784
                                                    $numAnswer.')',
785
                                            ]
786
                                        );
787
                                    } else {
788
                                        $s .= Display::tag('td',
789
                                            Display::input('radio',
790
                                                'choiceDegreeCertainty['.$questionId.']['.$numAnswer.']',
791
                                                $id,
792
                                                $attributes1
793
                                            ),
794
                                            ['style' => 'text-align:center; background-color:#EFEFFC;',
795
                                                'onclick' => 'handleRadioRow(event, '.
796
                                                    $questionId.', '.
797
                                                    $numAnswer.')',
798
                                            ]
799
                                        );
800
                                    }
801
                                }
802
                            }
803
804
                            if ($show_comment) {
805
                                $s .= '<td>';
806
                                $s .= $comment;
807
                                $s .= '</td>';
808
                            }
809
                            $s .= '</tr>';
810
                        }
811
                        break;
812
                    case MULTIPLE_ANSWER_COMBINATION:
813
                        // multiple answers
814
                        $input_id = 'choice-'.$questionId.'-'.$answerId;
815
816
                        if (in_array($numAnswer, $userChoiceList)) {
817
                            $attributes = [
818
                                'id' => $input_id,
819
                                'checked' => 1,
820
                                'selected' => 1,
821
                            ];
822
                        } else {
823
                            $attributes = ['id' => $input_id];
824
                        }
825
826
                        if ($debug_mark_answer) {
827
                            if ($answerCorrect) {
828
                                $attributes['checked'] = 1;
829
                                $attributes['selected'] = 1;
830
                            }
831
                        }
832
                        $answer = Security::remove_XSS($answer, $userStatus);
833
                        $answer_input = '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
834
                        $answer_input .= '<label class="checkbox">';
835
                        $answer_input .= Display::input(
836
                            'checkbox',
837
                            'choice['.$questionId.']['.$numAnswer.']',
838
                            1,
839
                            $attributes
840
                        );
841
                        $answer_input .= $answer;
842
                        $answer_input .= '</label>';
843
844
                        if ($show_comment) {
845
                            $s .= '<tr>';
846
                            $s .= '<td>';
847
                            $s .= $answer_input;
848
                            $s .= '</td>';
849
                            $s .= '<td>';
850
                            $s .= $comment;
851
                            $s .= '</td>';
852
                            $s .= '</tr>';
853
                        } else {
854
                            $s .= $answer_input;
855
                        }
856
                        break;
857
                    case MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE:
858
                        $s .= '<input type="hidden" name="choice2['.$questionId.']" value="0" />';
859
                        $myChoice = [];
860
                        if (!empty($userChoiceList)) {
861
                            foreach ($userChoiceList as $item) {
862
                                $item = explode(':', $item);
863
                                if (isset($item[1]) && isset($item[0])) {
864
                                    $myChoice[$item[0]] = $item[1];
865
                                }
866
                            }
867
                        }
868
869
                        $answer = Security::remove_XSS($answer, $userStatus);
870
                        $s .= '<tr>';
871
                        $s .= Display::tag('td', $answer);
872
                        foreach ($objQuestionTmp->options as $key => $item) {
873
                            if (isset($myChoice[$numAnswer]) && $key == $myChoice[$numAnswer]) {
874
                                $attributes = [
875
                                    'checked' => 1,
876
                                    'selected' => 1,
877
                                ];
878
                            } else {
879
                                $attributes = [];
880
                            }
881
882
                            if ($debug_mark_answer) {
883
                                if ($key == $answerCorrect) {
884
                                    $attributes['checked'] = 1;
885
                                    $attributes['selected'] = 1;
886
                                }
887
                            }
888
                            $s .= Display::tag(
889
                                'td',
890
                                Display::input(
891
                                    'radio',
892
                                    'choice['.$questionId.']['.$numAnswer.']',
893
                                    $key,
894
                                    $attributes
895
                                )
896
                            );
897
                        }
898
899
                        if ($show_comment) {
900
                            $s .= '<td>';
901
                            $s .= $comment;
902
                            $s .= '</td>';
903
                        }
904
                        $s .= '</tr>';
905
                        break;
906
                    case FILL_IN_BLANKS:
907
                        // display the question, with field empty, for student to fill it,
908
                        // or filled to display the answer in the Question preview of the exercise/admin.php page
909
                        $displayForStudent = true;
910
                        $listAnswerInfo = FillBlanks::getAnswerInfo($answer);
911
                        // Correct answers
912
                        $correctAnswerList = $listAnswerInfo['words'];
913
                        // Student's answer
914
                        $studentAnswerList = [];
915
                        if (isset($user_choice[0]['answer'])) {
916
                            $arrayStudentAnswer = FillBlanks::getAnswerInfo(
917
                                $user_choice[0]['answer'],
918
                                true
919
                            );
920
                            $studentAnswerList = $arrayStudentAnswer['student_answer'];
921
                        }
922
923
                        // If the question must be shown with the answer (in page exercise/admin.php)
924
                        // for teacher preview set the student-answer to the correct answer
925
                        if ($debug_mark_answer) {
926
                            $studentAnswerList = $correctAnswerList;
927
                            $displayForStudent = false;
928
                        }
929
930
                        if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
931
                            $answer = '';
932
                            for ($i = 0; $i < count($listAnswerInfo['common_words']) - 1; $i++) {
933
                                // display the common word
934
                                $answer .= $listAnswerInfo['common_words'][$i];
935
                                // display the blank word
936
                                $correctItem = $listAnswerInfo['words'][$i];
937
                                if (isset($studentAnswerList[$i])) {
938
                                    // If student already started this test and answered this question,
939
                                    // fill the blank with his previous answers
940
                                    // may be "" if student viewed the question, but did not fill the blanks
941
                                    $correctItem = $studentAnswerList[$i];
942
                                }
943
                                $attributes['style'] = 'width:'.$listAnswerInfo['input_size'][$i].'px';
944
                                $answer .= FillBlanks::getFillTheBlankHtml(
945
                                    $current_item,
946
                                    $questionId,
947
                                    $correctItem,
948
                                    $attributes,
949
                                    $answer,
950
                                    $listAnswerInfo,
951
                                    $displayForStudent,
952
                                    $i
953
                                );
954
                            }
955
                            // display the last common word
956
                            $answer .= $listAnswerInfo['common_words'][$i];
957
                        } else {
958
                            // display empty [input] with the right width for student to fill it
959
                            $answer = '';
960
                            for ($i = 0; $i < count($listAnswerInfo['common_words']) - 1; $i++) {
961
                                // display the common words
962
                                $answer .= $listAnswerInfo['common_words'][$i];
963
                                // display the blank word
964
                                $attributes['style'] = 'width:'.$listAnswerInfo['input_size'][$i].'px';
965
                                $answer .= FillBlanks::getFillTheBlankHtml(
966
                                    $current_item,
967
                                    $questionId,
968
                                    '',
969
                                    $attributes,
970
                                    $answer,
971
                                    $listAnswerInfo,
972
                                    $displayForStudent,
973
                                    $i
974
                                );
975
                            }
976
                            // display the last common word
977
                            $answer .= $listAnswerInfo['common_words'][$i];
978
                        }
979
                        $s .= $answer;
980
                        break;
981
                    case CALCULATED_ANSWER:
982
                        /*
983
                         * In the CALCULATED_ANSWER test
984
                         * you mustn't have [ and ] in the textarea
985
                         * you mustn't have @@ in the textarea
986
                         * the text to find mustn't be empty or contains only spaces
987
                         * the text to find mustn't contains HTML tags
988
                         * the text to find mustn't contains char "
989
                         */
990
                        if (null !== $origin) {
991
                            global $exe_id;
992
                            $exe_id = (int) $exe_id;
993
                            $trackAttempts = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
994
                            $sql = "SELECT answer FROM $trackAttempts
995
                                    WHERE exe_id = $exe_id AND question_id = $questionId";
996
                            $rsLastAttempt = Database::query($sql);
997
                            $rowLastAttempt = Database::fetch_array($rsLastAttempt);
998
999
                            $answer = null;
1000
                            if (isset($rowLastAttempt['answer'])) {
1001
                                $answer = $rowLastAttempt['answer'];
1002
                                $answerParts = explode(':::', $answer);
1003
                                if (isset($answerParts[1])) {
1004
                                    $answer = $answerParts[0];
1005
                                    $calculatedAnswerList[$questionId] = $answerParts[1];
1006
                                    Session::write('calculatedAnswerId', $calculatedAnswerList);
1007
                                }
1008
                            } else {
1009
                                $calculatedAnswerList = Session::read('calculatedAnswerId');
1010
                                if (!isset($calculatedAnswerList[$questionId])) {
1011
                                    $calculatedAnswerList[$questionId] = mt_rand(1, $nbrAnswers);
1012
                                    Session::write('calculatedAnswerId', $calculatedAnswerList);
1013
                                }
1014
                                $answer = $objAnswerTmp->selectAnswer($calculatedAnswerList[$questionId]);
1015
                            }
1016
                        }
1017
1018
                        [$answer] = explode('@@', $answer);
1019
                        // $correctAnswerList array of array with correct answers 0=> [0=>[\p] 1=>[plop]]
1020
                        api_preg_match_all(
1021
                            '/\[[^]]+\]/',
1022
                            $answer,
1023
                            $correctAnswerList
1024
                        );
1025
1026
                        // get student answer to display it if student go back
1027
                        // to previous calculated answer question in a test
1028
                        if (isset($user_choice[0]['answer'])) {
1029
                            api_preg_match_all(
1030
                                '/\[[^]]+\]/',
1031
                                $answer,
1032
                                $studentAnswerList
1033
                            );
1034
                            $studentAnswerListToClean = $studentAnswerList[0];
1035
                            $studentAnswerList = [];
1036
1037
                            $maxStudents = count($studentAnswerListToClean);
1038
                            for ($i = 0; $i < $maxStudents; $i++) {
1039
                                $answerCorrected = $studentAnswerListToClean[$i];
1040
                                $answerCorrected = api_preg_replace(
1041
                                    '| / <font color="green"><b>.*$|',
1042
                                    '',
1043
                                    $answerCorrected
1044
                                );
1045
                                $answerCorrected = api_preg_replace(
1046
                                    '/^\[/',
1047
                                    '',
1048
                                    $answerCorrected
1049
                                );
1050
                                $answerCorrected = api_preg_replace(
1051
                                    '|^<font color="red"><s>|',
1052
                                    '',
1053
                                    $answerCorrected
1054
                                );
1055
                                $answerCorrected = api_preg_replace(
1056
                                    '|</s></font>$|',
1057
                                    '',
1058
                                    $answerCorrected
1059
                                );
1060
                                $answerCorrected = '['.$answerCorrected.']';
1061
                                $studentAnswerList[] = $answerCorrected;
1062
                            }
1063
                        }
1064
1065
                        // If display preview of answer in test view for exemple,
1066
                        // set the student answer to the correct answers
1067
                        if ($debug_mark_answer) {
1068
                            // contain the rights answers surronded with brackets
1069
                            $studentAnswerList = $correctAnswerList[0];
1070
                        }
1071
1072
                        /*
1073
                        Split the response by bracket
1074
                        tabComments is an array with text surrounding the text to find
1075
                        we add a space before and after the answerQuestion to be sure to
1076
                        have a block of text before and after [xxx] patterns
1077
                        so we have n text to find ([xxx]) and n+1 block of texts before,
1078
                        between and after the text to find
1079
                        */
1080
                        $tabComments = api_preg_split(
1081
                            '/\[[^]]+\]/',
1082
                            ' '.$answer.' '
1083
                        );
1084
                        if (!empty($correctAnswerList) && !empty($studentAnswerList)) {
1085
                            $answer = '';
1086
                            $i = 0;
1087
                            foreach ($studentAnswerList as $studentItem) {
1088
                                // Remove surrounding brackets
1089
                                $studentResponse = api_substr(
1090
                                    $studentItem,
1091
                                    1,
1092
                                    api_strlen($studentItem) - 2
1093
                                );
1094
                                $size = strlen($studentItem);
1095
                                $attributes['class'] = self::detectInputAppropriateClass($size);
1096
                                $answer .= $tabComments[$i].
1097
                                    Display::input(
1098
                                        'text',
1099
                                        "choice[$questionId][]",
1100
                                        $studentResponse,
1101
                                        $attributes
1102
                                    );
1103
                                $i++;
1104
                            }
1105
                            $answer .= $tabComments[$i];
1106
                        } else {
1107
                            // display exercise with empty input fields
1108
                            // every [xxx] are replaced with an empty input field
1109
                            foreach ($correctAnswerList[0] as $item) {
1110
                                $size = strlen($item);
1111
                                $attributes['class'] = self::detectInputAppropriateClass($size);
1112
                                if (EXERCISE_FEEDBACK_TYPE_POPUP == $exercise->getFeedbackType()) {
1113
                                    $attributes['id'] = "question_$questionId";
1114
                                    $attributes['class'] .= ' checkCalculatedQuestionOnEnter ';
1115
                                }
1116
1117
                                $answer = str_replace(
1118
                                    $item,
1119
                                    Display::input(
1120
                                        'text',
1121
                                        "choice[$questionId][]",
1122
                                        '',
1123
                                        $attributes
1124
                                    ),
1125
                                    $answer
1126
                                );
1127
                            }
1128
                        }
1129
                        if (null !== $origin) {
1130
                            $s = $answer;
1131
                            break;
1132
                        } else {
1133
                            $s .= $answer;
1134
                        }
1135
                        break;
1136
                    case MATCHING:
1137
                        // matching type, showing suggestions and answers
1138
                        // TODO: replace $answerId by $numAnswer
1139
                        if ($answerCorrect != 0) {
1140
                            // only show elements to be answered (not the contents of
1141
                            // the select boxes, who are correct = 0)
1142
                            $s .= '<tr><td width="45%" valign="top">';
1143
                            $parsed_answer = $answer;
1144
                            // Left part questions
1145
                            $s .= '<p class="indent">'.$lines_count.'.&nbsp;'.$parsed_answer.'</p></td>';
1146
                            // Middle part (matches selects)
1147
                            // Id of select is # question + # of option
1148
                            $s .= '<td width="10%" valign="top" align="center">
1149
                                <div class="select-matching">
1150
                                <select
1151
                                    id="choice_id_'.$current_item.'_'.$lines_count.'"
1152
                                    name="choice['.$questionId.']['.$numAnswer.']">';
1153
1154
                            // fills the list-box
1155
                            foreach ($select_items as $key => $val) {
1156
                                // set $debug_mark_answer to true at function start to
1157
                                // show the correct answer with a suffix '-x'
1158
                                $selected = '';
1159
                                if ($debug_mark_answer) {
1160
                                    if ($val['id'] == $answerCorrect) {
1161
                                        $selected = 'selected="selected"';
1162
                                    }
1163
                                }
1164
                                //$user_choice_array_position
1165
                                if (isset($user_choice_array_position[$numAnswer]) &&
1166
                                    $val['id'] == $user_choice_array_position[$numAnswer]
1167
                                ) {
1168
                                    $selected = 'selected="selected"';
1169
                                }
1170
                                $s .= '<option value="'.$val['id'].'" '.$selected.'>'.$val['letter'].'</option>';
1171
                            }
1172
1173
                            $s .= '</select></div></td><td width="5%" class="separate">&nbsp;</td>';
1174
                            $s .= '<td width="40%" valign="top" >';
1175
                            if (isset($select_items[$lines_count])) {
1176
                                $s .= '<div class="text-right">
1177
                                        <p class="indent">'.
1178
                                    $select_items[$lines_count]['letter'].'.&nbsp; '.
1179
                                    $select_items[$lines_count]['answer'].'
1180
                                        </p>
1181
                                        </div>';
1182
                            } else {
1183
                                $s .= '&nbsp;';
1184
                            }
1185
                            $s .= '</td>';
1186
                            $s .= '</tr>';
1187
                            $lines_count++;
1188
                            // If the left side of the "matching" has been completely
1189
                            // shown but the right side still has values to show...
1190
                            if (($lines_count - 1) == $num_suggestions) {
1191
                                // if it remains answers to shown at the right side
1192
                                while (isset($select_items[$lines_count])) {
1193
                                    $s .= '<tr>
1194
                                      <td colspan="2"></td>
1195
                                      <td valign="top">';
1196
                                    $s .= '<b>'.$select_items[$lines_count]['letter'].'.</b> '.
1197
                                        $select_items[$lines_count]['answer'];
1198
                                    $s .= "</td>
1199
                                </tr>";
1200
                                    $lines_count++;
1201
                                }
1202
                            }
1203
                            $matching_correct_answer++;
1204
                        }
1205
                        break;
1206
                    case DRAGGABLE:
1207
                        if ($answerCorrect) {
1208
                            $windowId = $questionId.'_'.$lines_count;
1209
                            $s .= '<li class="touch-items" id="'.$windowId.'">';
1210
                            $s .= Display::div(
1211
                                $answer,
1212
                                [
1213
                                    'id' => "window_$windowId",
1214
                                    'class' => "window{$questionId}_question_draggable exercise-draggable-answer-option",
1215
                                ]
1216
                            );
1217
1218
                            $draggableSelectOptions = [];
1219
                            $selectedValue = 0;
1220
                            $selectedIndex = 0;
1221
                            if ($user_choice) {
1222
                                foreach ($user_choice as $userChoiceKey => $chosen) {
1223
                                    $userChoiceKey++;
1224
                                    if ($lines_count != $userChoiceKey) {
1225
                                        continue;
1226
                                    }
1227
                                    /*if ($answerCorrect != $chosen['answer']) {
1228
                                        continue;
1229
                                    }*/
1230
                                    $selectedValue = $chosen['answer'];
1231
                                }
1232
                            }
1233
                            foreach ($select_items as $key => $select_item) {
1234
                                $draggableSelectOptions[$select_item['id']] = $select_item['letter'];
1235
                            }
1236
1237
                            foreach ($draggableSelectOptions as $value => $text) {
1238
                                if ($value == $selectedValue) {
1239
                                    break;
1240
                                }
1241
                                $selectedIndex++;
1242
                            }
1243
1244
                            $s .= Display::select(
1245
                                "choice[$questionId][$numAnswer]",
1246
                                $draggableSelectOptions,
1247
                                $selectedValue,
1248
                                [
1249
                                    'id' => "window_{$windowId}_select",
1250
                                    'class' => 'select_option hidden',
1251
                                ],
1252
                                false
1253
                            );
1254
1255
                            if ($selectedValue && $selectedIndex) {
1256
                                $s .= "
1257
                                    <script>
1258
                                        $(function() {
1259
                                            DraggableAnswer.deleteItem(
1260
                                                $('#{$questionId}_$lines_count'),
1261
                                                $('#drop_{$questionId}_{$selectedIndex}')
1262
                                            );
1263
                                        });
1264
                                    </script>
1265
                                ";
1266
                            }
1267
1268
                            if (isset($select_items[$lines_count])) {
1269
                                $s .= Display::div(
1270
                                    Display::tag(
1271
                                        'b',
1272
                                        $select_items[$lines_count]['letter']
1273
                                    ).$select_items[$lines_count]['answer'],
1274
                                    [
1275
                                        'id' => "window_{$windowId}_answer",
1276
                                        'class' => 'hidden',
1277
                                    ]
1278
                                );
1279
                            } else {
1280
                                $s .= '&nbsp;';
1281
                            }
1282
1283
                            $lines_count++;
1284
                            if (($lines_count - 1) == $num_suggestions) {
1285
                                while (isset($select_items[$lines_count])) {
1286
                                    $s .= Display::tag('b', $select_items[$lines_count]['letter']);
1287
                                    $s .= $select_items[$lines_count]['answer'];
1288
                                    $lines_count++;
1289
                                }
1290
                            }
1291
1292
                            $matching_correct_answer++;
1293
                            $s .= '</li>';
1294
                        }
1295
                        break;
1296
                    case MATCHING_DRAGGABLE:
1297
                        if ($answerId == 1) {
1298
                            echo $objAnswerTmp->getJs();
1299
                        }
1300
                        if ($answerCorrect != 0) {
1301
                            $windowId = "{$questionId}_{$lines_count}";
1302
                            $s .= <<<HTML
1303
                            <tr>
1304
                                <td width="45%">
1305
                                    <div id="window_{$windowId}"
1306
                                        class="window window_left_question window{$questionId}_question">
1307
                                        <strong>$lines_count.</strong>
1308
                                        $answer
1309
                                    </div>
1310
                                </td>
1311
                                <td width="10%">
1312
HTML;
1313
1314
                            $draggableSelectOptions = [];
1315
                            $selectedValue = 0;
1316
                            $selectedIndex = 0;
1317
1318
                            if ($user_choice) {
1319
                                foreach ($user_choice as $chosen) {
1320
                                    if ($numAnswer == $chosen['position']) {
1321
                                        $selectedValue = $chosen['answer'];
1322
                                        break;
1323
                                    }
1324
                                }
1325
                            }
1326
1327
                            foreach ($select_items as $key => $selectItem) {
1328
                                $draggableSelectOptions[$selectItem['id']] = $selectItem['letter'];
1329
                            }
1330
1331
                            foreach ($draggableSelectOptions as $value => $text) {
1332
                                if ($value == $selectedValue) {
1333
                                    break;
1334
                                }
1335
                                $selectedIndex++;
1336
                            }
1337
1338
                            $s .= Display::select(
1339
                                "choice[$questionId][$numAnswer]",
1340
                                $draggableSelectOptions,
1341
                                $selectedValue,
1342
                                [
1343
                                    'id' => "window_{$windowId}_select",
1344
                                    'class' => 'hidden',
1345
                                ],
1346
                                false
1347
                            );
1348
1349
                            if (!empty($answerCorrect) && !empty($selectedValue)) {
1350
                                // Show connect if is not freeze (question preview)
1351
                                if (!$freeze) {
1352
                                    $s .= "
1353
                                        <script>
1354
                                            $(function() {
1355
                                                $(window).on('load', function() {
1356
                                                    jsPlumb.connect({
1357
                                                        source: 'window_$windowId',
1358
                                                        target: 'window_{$questionId}_{$selectedIndex}_answer',
1359
                                                        endpoint: ['Blank', {radius: 15}],
1360
                                                        anchors: ['RightMiddle', 'LeftMiddle'],
1361
                                                        paintStyle: {strokeStyle: '#8A8888', lineWidth: 8},
1362
                                                        connector: [
1363
                                                            MatchingDraggable.connectorType,
1364
                                                            {curvines: MatchingDraggable.curviness}
1365
                                                        ]
1366
                                                    });
1367
                                                });
1368
                                            });
1369
                                        </script>
1370
                                    ";
1371
                                }
1372
                            }
1373
1374
                            $s .= '</td><td width="45%">';
1375
                            if (isset($select_items[$lines_count])) {
1376
                                $s .= <<<HTML
1377
                                <div id="window_{$windowId}_answer" class="window window_right_question">
1378
                                    <strong>{$select_items[$lines_count]['letter']}.</strong>
1379
                                    {$select_items[$lines_count]['answer']}
1380
                                </div>
1381
HTML;
1382
                            } else {
1383
                                $s .= '&nbsp;';
1384
                            }
1385
1386
                            $s .= '</td></tr>';
1387
                            $lines_count++;
1388
                            if (($lines_count - 1) == $num_suggestions) {
1389
                                while (isset($select_items[$lines_count])) {
1390
                                    $s .= <<<HTML
1391
                                    <tr>
1392
                                        <td colspan="2"></td>
1393
                                        <td>
1394
                                            <strong>{$select_items[$lines_count]['letter']}</strong>
1395
                                            {$select_items[$lines_count]['answer']}
1396
                                        </td>
1397
                                    </tr>
1398
HTML;
1399
                                    $lines_count++;
1400
                                }
1401
                            }
1402
                            $matching_correct_answer++;
1403
                        }
1404
                        break;
1405
                }
1406
            }
1407
1408
            if ($show_comment) {
1409
                $s .= '</table>';
1410
            } elseif (in_array(
1411
                $answerType,
1412
                [
1413
                    MATCHING,
1414
                    MATCHING_DRAGGABLE,
1415
                    UNIQUE_ANSWER_NO_OPTION,
1416
                    MULTIPLE_ANSWER_TRUE_FALSE,
1417
                    MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE,
1418
                    MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY,
1419
                ]
1420
            )) {
1421
                $s .= '</table>';
1422
            }
1423
1424
            if ($answerType == DRAGGABLE) {
1425
                $isVertical = $objQuestionTmp->extra == 'v';
1426
                $s .= "
1427
                           </ul>
1428
                        </div><!-- .col-md-12 -->
1429
                    </div><!-- .row -->
1430
                ";
1431
                $counterAnswer = 1;
1432
                $s .= $isVertical ? '' : '<div class="row">';
1433
                for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
1434
                    $answerCorrect = $objAnswerTmp->isCorrect($answerId);
1435
                    $windowId = $questionId.'_'.$counterAnswer;
1436
                    if ($answerCorrect) {
1437
                        $s .= $isVertical ? '<div class="row">' : '';
1438
                        $s .= '
1439
                            <div class="'.($isVertical ? 'col-md-12' : 'col-xs-12 col-sm-4 col-md-3 col-lg-2').'">
1440
                                <div class="droppable-item">
1441
                                    <span class="number">'.$counterAnswer.'.</span>
1442
                                    <div id="drop_'.$windowId.'" class="droppable">
1443
                                    </div>
1444
                                 </div>
1445
                            </div>
1446
                        ';
1447
                        $s .= $isVertical ? '</div>' : '';
1448
                        $counterAnswer++;
1449
                    }
1450
                }
1451
1452
                $s .= $isVertical ? '' : '</div>'; // row
1453
//                $s .= '</div>';
1454
            }
1455
1456
            if (in_array($answerType, [MATCHING, MATCHING_DRAGGABLE])) {
1457
                $s .= '</div>'; //drag_question
1458
            }
1459
1460
            $s .= '</div>'; //question_options row
1461
1462
            // destruction of the Answer object
1463
            unset($objAnswerTmp);
1464
            // destruction of the Question object
1465
            unset($objQuestionTmp);
1466
            if ('export' == $origin) {
1467
                return $s;
1468
            }
1469
            echo $s;
1470
        } elseif ($answerType == HOT_SPOT || $answerType == HOT_SPOT_DELINEATION) {
1471
            global $exe_id;
1472
            // Question is a HOT_SPOT
1473
            // Checking document/images visibility
1474
            if (api_is_platform_admin() || api_is_course_admin()) {
1475
                $doc_id = $objQuestionTmp->getPictureId();
1476
                if (is_numeric($doc_id)) {
1477
                    $images_folder_visibility = api_get_item_visibility(
1478
                        $course,
1479
                        'document',
1480
                        $doc_id,
1481
                        api_get_session_id()
1482
                    );
1483
                    if (!$images_folder_visibility) {
1484
                        // Show only to the course/platform admin if the image is set to visibility = false
1485
                        echo Display::return_message(
1486
                            get_lang('ChangeTheVisibilityOfTheCurrentImage'),
1487
                            'warning'
1488
                        );
1489
                    }
1490
                }
1491
            }
1492
            $questionDescription = $objQuestionTmp->selectDescription();
1493
1494
            // Get the answers, make a list
1495
            $objAnswerTmp = new Answer($questionId, $course_id);
1496
            $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
1497
1498
            // get answers of hotpost
1499
            $answers_hotspot = [];
1500
            for ($answerId = 1; $answerId <= $nbrAnswers; $answerId++) {
1501
                $answers = $objAnswerTmp->selectAnswerByAutoId(
1502
                    $objAnswerTmp->selectAutoId($answerId)
1503
                );
1504
                $answers_hotspot[$answers['iid']] = $objAnswerTmp->selectAnswer(
1505
                    $answerId
1506
                );
1507
            }
1508
1509
            $answerList = '';
1510
            $hotspotColor = 0;
1511
            if ($answerType != HOT_SPOT_DELINEATION) {
1512
                $answerList = '
1513
                    <div class="well well-sm">
1514
                        <h5 class="page-header">'.get_lang('HotspotZones').'</h5>
1515
                        <ol>
1516
                ';
1517
1518
                if (!empty($answers_hotspot)) {
1519
                    Session::write("hotspot_ordered$questionId", array_keys($answers_hotspot));
1520
                    foreach ($answers_hotspot as $value) {
1521
                        $answerList .= '<li>';
1522
                        if ($freeze) {
1523
                            $answerList .= '<span class="hotspot-color-'.$hotspotColor
1524
                                .' fa fa-square" aria-hidden="true"></span>'.PHP_EOL;
1525
                        }
1526
                        $answerList .= $value;
1527
                        $answerList .= '</li>';
1528
                        $hotspotColor++;
1529
                    }
1530
                }
1531
1532
                $answerList .= '
1533
                        </ol>
1534
                    </div>
1535
                ';
1536
            }
1537
1538
            if ($freeze) {
1539
                $relPath = api_get_path(WEB_CODE_PATH);
1540
                echo "
1541
                    <div class=\"row\">
1542
                        <div class=\"col-sm-9\">
1543
                            <div id=\"hotspot-preview-$questionId\"></div>
1544
                        </div>
1545
                        <div class=\"col-sm-3\">
1546
                            $answerList
1547
                        </div>
1548
                    </div>
1549
                    <script>
1550
                        new ".($answerType == HOT_SPOT ? "HotspotQuestion" : "DelineationQuestion")."({
1551
                            questionId: $questionId,
1552
                            exerciseId: {$exercise->iid},
1553
                            exeId: 0,
1554
                            selector: '#hotspot-preview-$questionId',
1555
                            'for': 'preview',
1556
                            relPath: '$relPath'
1557
                        });
1558
                    </script>
1559
                ";
1560
1561
                return;
1562
            }
1563
1564
            if (!$only_questions) {
1565
                if ($show_title) {
1566
                    if ($exercise->display_category_name) {
1567
                        TestCategory::displayCategoryAndTitle($objQuestionTmp->iid);
1568
                    }
1569
                    echo $objQuestionTmp->getTitleToDisplay($current_item);
1570
                }
1571
1572
                if ($questionRequireAuth) {
1573
                    WhispeakAuthPlugin::quizQuestionAuthentify($questionId, $exercise);
1574
1575
                    return false;
1576
                }
1577
1578
                //@todo I need to the get the feedback type
1579
                echo <<<HOTSPOT
1580
                    <input type="hidden" name="hidden_hotspot_id" value="$questionId" />
1581
                    <div class="exercise_questions">
1582
                        $questionDescription
1583
                        <div class="row">
1584
HOTSPOT;
1585
            }
1586
1587
            $relPath = api_get_path(WEB_CODE_PATH);
1588
            $s .= "<div class=\"col-sm-8 col-md-9\">
1589
                   <div class=\"hotspot-image\"></div>
1590
                    <script>
1591
                        $(function() {
1592
                            new ".($answerType == HOT_SPOT_DELINEATION ? 'DelineationQuestion' : 'HotspotQuestion')."({
1593
                                questionId: $questionId,
1594
                                exerciseId: {$exercise->iid},
1595
                                exeId: 0,
1596
                                selector: '#question_div_' + $questionId + ' .hotspot-image',
1597
                                'for': 'user',
1598
                                relPath: '$relPath'
1599
                            });
1600
                        });
1601
                    </script>
1602
                </div>
1603
                <div class=\"col-sm-4 col-md-3\">
1604
                    $answerList
1605
                </div>
1606
            ";
1607
1608
            echo <<<HOTSPOT
1609
                            $s
1610
                        </div>
1611
                    </div>
1612
HOTSPOT;
1613
        } elseif ($answerType == ANNOTATION) {
1614
            global $exe_id;
1615
            $relPath = api_get_path(WEB_CODE_PATH);
1616
            if (api_is_platform_admin() || api_is_course_admin()) {
1617
                $docId = DocumentManager::get_document_id($course, '/images/'.$pictureName);
1618
                if ($docId) {
1619
                    $images_folder_visibility = api_get_item_visibility(
1620
                        $course,
1621
                        'document',
1622
                        $docId,
1623
                        api_get_session_id()
1624
                    );
1625
1626
                    if (!$images_folder_visibility) {
1627
                        echo Display::return_message(get_lang('ChangeTheVisibilityOfTheCurrentImage'), 'warning');
1628
                    }
1629
                }
1630
1631
                if ($freeze) {
1632
                    echo Display::img(
1633
                        api_get_path(WEB_COURSE_PATH).$course['path'].'/document/images/'.$pictureName,
1634
                        $objQuestionTmp->selectTitle(),
1635
                        ['width' => '600px']
1636
                    );
1637
1638
                    return 0;
1639
                }
1640
            }
1641
1642
            if (!$only_questions) {
1643
                if ($show_title) {
1644
                    if ($exercise->display_category_name) {
1645
                        TestCategory::displayCategoryAndTitle($objQuestionTmp->iid);
1646
                    }
1647
                    echo $objQuestionTmp->getTitleToDisplay($current_item);
1648
                }
1649
1650
                if ($questionRequireAuth) {
1651
                    WhispeakAuthPlugin::quizQuestionAuthentify($questionId, $exercise);
1652
1653
                    return false;
1654
                }
1655
1656
                echo '
1657
                    <input type="hidden" name="hidden_hotspot_id" value="'.$questionId.'" />
1658
                    <div class="exercise_questions">
1659
                        '.$objQuestionTmp->selectDescription().'
1660
                        <div class="row">
1661
                            <div class="col-sm-8 col-md-9">
1662
                                <div id="annotation-canvas-'.$questionId.'" class="annotation-canvas center-block">
1663
                                </div>
1664
                                <script>
1665
                                    AnnotationQuestion({
1666
                                        questionId: '.$questionId.',
1667
                                        exerciseId: '.$exerciseId.',
1668
                                        relPath: \''.$relPath.'\',
1669
                                        courseId: '.$course_id.',
1670
                                    });
1671
                                </script>
1672
                            </div>
1673
                            <div class="col-sm-4 col-md-3">
1674
                                <div class="well well-sm" id="annotation-toolbar-'.$questionId.'">
1675
                                    <div class="btn-toolbar">
1676
                                        <div class="btn-group" data-toggle="buttons">
1677
                                            <label class="btn btn-default active"
1678
                                                aria-label="'.get_lang('AddAnnotationPath').'">
1679
                                                <input
1680
                                                    type="radio" value="0"
1681
                                                    name="'.$questionId.'-options" autocomplete="off" checked>
1682
                                                <span class="fa fa-pencil" aria-hidden="true"></span>
1683
                                            </label>
1684
                                            <label class="btn btn-default"
1685
                                                aria-label="'.get_lang('AddAnnotationText').'">
1686
                                                <input
1687
                                                    type="radio" value="1"
1688
                                                    name="'.$questionId.'-options" autocomplete="off">
1689
                                                <span class="fa fa-font fa-fw" aria-hidden="true"></span>
1690
                                            </label>
1691
                                        </div>
1692
                                    </div>
1693
                                    <ul class="list-unstyled"></ul>
1694
                                </div>
1695
                            </div>
1696
                        </div>
1697
                    </div>
1698
                ';
1699
            }
1700
            $objAnswerTmp = new Answer($questionId);
1701
            $nbrAnswers = $objAnswerTmp->selectNbrAnswers();
1702
            unset($objAnswerTmp, $objQuestionTmp);
1703
        }
1704
1705
        return $nbrAnswers;
1706
    }
1707
1708
    /**
1709
     * Get an HTML string with the list of exercises where the given question
1710
     * is being used.
1711
     *
1712
     * @param int $questionId    The iid of the question being observed
1713
     * @param int $excludeTestId If defined, exclude this (current) test from the list of results
1714
     *
1715
     * @return string An HTML string containing a div and a table
1716
     */
1717
    public static function showTestsWhereQuestionIsUsed(int $questionId, int $excludeTestId = 0)
1718
    {
1719
        $questionId = (int) $questionId;
1720
        $sql = "SELECT qz.title quiz_title,
1721
                        c.title course_title,
1722
                        s.name session_name,
1723
                        qz.iid as quiz_id,
1724
                        qz.c_id,
1725
                        qz.session_id
1726
                FROM c_quiz qz,
1727
                    c_quiz_rel_question qq,
1728
                    course c,
1729
                    session s
1730
                WHERE qz.c_id = c.id AND
1731
                    (qz.session_id = s.id OR qz.session_id = 0) AND
1732
                    qq.exercice_id = qz.iid AND ";
1733
        if (!empty($excludeTestId)) {
1734
            $excludeTestId = (int) $excludeTestId;
1735
            $sql .= " qz.iid != $excludeTestId AND ";
1736
        }
1737
        $sql .= "     qq.question_id = $questionId
1738
                GROUP BY qq.iid";
1739
1740
        $result = [];
1741
        $html = "";
1742
1743
        $sqlResult = Database::query($sql);
1744
1745
        if (Database::num_rows($sqlResult) != 0) {
1746
            while ($row = Database::fetch_array($sqlResult, 'ASSOC')) {
1747
                $tmp = [];
1748
                $tmp[0] = $row['course_title'];
1749
                $tmp[1] = $row['session_name'];
1750
                $tmp[2] = $row['quiz_title'];
1751
                $courseDetails = api_get_course_info_by_id($row['c_id']);
1752
                $courseCode = $courseDetails['code'];
1753
                // Send do other test with r=1 to reset current test session variables
1754
                $urlToQuiz = api_get_path(WEB_CODE_PATH).'exercise/admin.php?'.api_get_cidreq_params($courseCode, $row['session_id']).'&exerciseId='.$row['quiz_id'].'&r=1';
1755
                $tmp[3] = '<a href="'.$urlToQuiz.'">'.Display::return_icon('quiz.png', get_lang('Edit')).'</a>';
1756
                if ((int) $row['session_id'] == 0) {
1757
                    $tmp[1] = '-';
1758
                }
1759
1760
                $result[] = $tmp;
1761
            }
1762
1763
            $headers = [
1764
                get_lang('Course'),
1765
                get_lang('Session'),
1766
                get_lang('Quiz'),
1767
                get_lang('LinkToTestEdition'),
1768
            ];
1769
1770
            $title = Display::div(
1771
                get_lang('QuestionAlsoUsedInTheFollowingTests'),
1772
                [
1773
                    'class' => 'section-title',
1774
                    'style' => 'margin-top: 25px; border-bottom: none',
1775
                ]
1776
            );
1777
1778
            $html = $title.Display::table($headers, $result);
1779
        }
1780
1781
        echo $html;
1782
    }
1783
1784
    /**
1785
     * @param int $exeId
1786
     *
1787
     * @return array
1788
     */
1789
    public static function get_exercise_track_exercise_info($exeId)
1790
    {
1791
        $quizTable = Database::get_course_table(TABLE_QUIZ_TEST);
1792
        $trackExerciseTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
1793
        $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
1794
        $exeId = (int) $exeId;
1795
        $result = [];
1796
        if (!empty($exeId)) {
1797
            $sql = " SELECT q.*, tee.*
1798
                FROM $quizTable as q
1799
                INNER JOIN $trackExerciseTable as tee
1800
                ON q.iid = tee.exe_exo_id
1801
                INNER JOIN $courseTable c
1802
                ON c.id = tee.c_id
1803
                WHERE tee.exe_id = $exeId
1804
                AND q.c_id = c.id";
1805
1806
            $sqlResult = Database::query($sql);
1807
            if (Database::num_rows($sqlResult)) {
1808
                $result = Database::fetch_array($sqlResult, 'ASSOC');
1809
                $result['duration_formatted'] = '';
1810
                if (!empty($result['exe_duration'])) {
1811
                    $time = api_format_time($result['exe_duration'], 'js');
1812
                    $result['duration_formatted'] = $time;
1813
                }
1814
            }
1815
        }
1816
1817
        return $result;
1818
    }
1819
1820
    /**
1821
     * Validates the time control key.
1822
     *
1823
     * @param int $exercise_id
1824
     * @param int $lp_id
1825
     * @param int $lp_item_id
1826
     *
1827
     * @return bool
1828
     */
1829
    public static function exercise_time_control_is_valid(
1830
        $exercise_id,
1831
        $lp_id = 0,
1832
        $lp_item_id = 0
1833
    ) {
1834
        $course_id = api_get_course_int_id();
1835
        $exercise_id = (int) $exercise_id;
1836
        $table = Database::get_course_table(TABLE_QUIZ_TEST);
1837
        $sql = "SELECT expired_time FROM $table
1838
                WHERE iid = $exercise_id";
1839
        $result = Database::query($sql);
1840
        $row = Database::fetch_array($result, 'ASSOC');
1841
        if (!empty($row['expired_time'])) {
1842
            $current_expired_time_key = self::get_time_control_key(
1843
                $exercise_id,
1844
                $lp_id,
1845
                $lp_item_id
1846
            );
1847
            if (isset($_SESSION['expired_time'][$current_expired_time_key])) {
1848
                $current_time = time();
1849
                $expired_time = api_strtotime(
1850
                    $_SESSION['expired_time'][$current_expired_time_key],
1851
                    'UTC'
1852
                );
1853
                $total_time_allowed = $expired_time + 30;
1854
                if ($total_time_allowed < $current_time) {
1855
                    return false;
1856
                }
1857
1858
                return true;
1859
            }
1860
1861
            return false;
1862
        }
1863
1864
        return true;
1865
    }
1866
1867
    /**
1868
     * Deletes the time control token.
1869
     *
1870
     * @param int $exercise_id
1871
     * @param int $lp_id
1872
     * @param int $lp_item_id
1873
     */
1874
    public static function exercise_time_control_delete(
1875
        $exercise_id,
1876
        $lp_id = 0,
1877
        $lp_item_id = 0
1878
    ) {
1879
        $current_expired_time_key = self::get_time_control_key(
1880
            $exercise_id,
1881
            $lp_id,
1882
            $lp_item_id
1883
        );
1884
        unset($_SESSION['expired_time'][$current_expired_time_key]);
1885
    }
1886
1887
    /**
1888
     * Generates the time control key.
1889
     *
1890
     * @param int $exercise_id
1891
     * @param int $lp_id
1892
     * @param int $lp_item_id
1893
     *
1894
     * @return string
1895
     */
1896
    public static function get_time_control_key(
1897
        $exercise_id,
1898
        $lp_id = 0,
1899
        $lp_item_id = 0
1900
    ) {
1901
        $exercise_id = (int) $exercise_id;
1902
        $lp_id = (int) $lp_id;
1903
        $lp_item_id = (int) $lp_item_id;
1904
1905
        return
1906
            api_get_course_int_id().'_'.
1907
            api_get_session_id().'_'.
1908
            $exercise_id.'_'.
1909
            api_get_user_id().'_'.
1910
            $lp_id.'_'.
1911
            $lp_item_id;
1912
    }
1913
1914
    /**
1915
     * Get session time control.
1916
     *
1917
     * @param int $exercise_id
1918
     * @param int $lp_id
1919
     * @param int $lp_item_id
1920
     *
1921
     * @return int
1922
     */
1923
    public static function get_session_time_control_key(
1924
        $exercise_id,
1925
        $lp_id = 0,
1926
        $lp_item_id = 0
1927
    ) {
1928
        $return_value = 0;
1929
        $time_control_key = self::get_time_control_key(
1930
            $exercise_id,
1931
            $lp_id,
1932
            $lp_item_id
1933
        );
1934
        if (isset($_SESSION['expired_time']) && isset($_SESSION['expired_time'][$time_control_key])) {
1935
            $return_value = $_SESSION['expired_time'][$time_control_key];
1936
        }
1937
1938
        return $return_value;
1939
    }
1940
1941
    /**
1942
     * Gets count of exam results.
1943
     *
1944
     * @param int    $exerciseId
1945
     * @param array  $conditions
1946
     * @param string $courseCode
1947
     * @param bool   $showSession
1948
     * @param bool   $searchAllTeacherCourses
1949
     * @param int    $status
1950
     *
1951
     * @return array
1952
     */
1953
    public static function get_count_exam_results(
1954
        $exerciseId,
1955
        $conditions,
1956
        $courseCode = '',
1957
        $showSession = false,
1958
        $searchAllTeacherCourses = false,
1959
        $status = 0
1960
    ) {
1961
        return self::get_exam_results_data(
1962
            null,
1963
            null,
1964
            null,
1965
            null,
1966
            $exerciseId,
1967
            $conditions,
1968
            true,
1969
            $courseCode,
1970
            $showSession,
1971
            false,
1972
            [],
1973
            false,
1974
            false,
1975
            false,
1976
            $searchAllTeacherCourses,
1977
            $status
1978
        );
1979
    }
1980
1981
    /**
1982
     * @param string $path
1983
     *
1984
     * @return int
1985
     */
1986
    public static function get_count_exam_hotpotatoes_results($path)
1987
    {
1988
        return self::get_exam_results_hotpotatoes_data(
1989
            0,
1990
            0,
1991
            '',
1992
            '',
1993
            $path,
1994
            true,
1995
            ''
1996
        );
1997
    }
1998
1999
    /**
2000
     * @param int    $in_from
2001
     * @param int    $in_number_of_items
2002
     * @param int    $in_column
2003
     * @param int    $in_direction
2004
     * @param string $in_hotpot_path
2005
     * @param bool   $in_get_count
2006
     * @param null   $where_condition
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $where_condition is correct as it would always require null to be passed?
Loading history...
2007
     *
2008
     * @return array|int
2009
     */
2010
    public static function get_exam_results_hotpotatoes_data(
2011
        $in_from,
2012
        $in_number_of_items,
2013
        $in_column,
2014
        $in_direction,
2015
        $in_hotpot_path,
2016
        $in_get_count = false,
2017
        $where_condition = null
2018
    ) {
2019
        $courseId = api_get_course_int_id();
2020
        // by default in_column = 1 If parameters given, it is the name of the column witch is the bdd field name
2021
        if ($in_column == 1) {
2022
            $in_column = 'firstname';
2023
        }
2024
        $in_hotpot_path = Database::escape_string($in_hotpot_path);
2025
        $in_direction = Database::escape_string($in_direction);
2026
        $in_direction = !in_array(strtolower(trim($in_direction)), ['asc', 'desc']) ? 'asc' : $in_direction;
2027
        $in_column = Database::escape_string($in_column);
2028
        $in_number_of_items = (int) $in_number_of_items;
2029
        $in_from = (int) $in_from;
2030
2031
        $TBL_TRACK_HOTPOTATOES = Database::get_main_table(
2032
            TABLE_STATISTIC_TRACK_E_HOTPOTATOES
2033
        );
2034
        $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
2035
2036
        $sql = "SELECT *, thp.id AS thp_id
2037
                FROM $TBL_TRACK_HOTPOTATOES thp
2038
                JOIN $TBL_USER u
2039
                ON thp.exe_user_id = u.user_id
2040
                WHERE
2041
                    thp.c_id = $courseId AND
2042
                    exe_name LIKE '$in_hotpot_path%'";
2043
2044
        // just count how many answers
2045
        if ($in_get_count) {
2046
            $res = Database::query($sql);
2047
2048
            return Database::num_rows($res);
2049
        }
2050
        // get a number of sorted results
2051
        $sql .= " $where_condition
2052
            ORDER BY `$in_column` $in_direction
2053
            LIMIT $in_from, $in_number_of_items";
2054
2055
        $res = Database::query($sql);
2056
        $result = [];
2057
        $apiIsAllowedToEdit = api_is_allowed_to_edit();
2058
        $urlBase = api_get_path(WEB_CODE_PATH).
2059
            'exercise/hotpotatoes_exercise_report.php?action=delete&'.
2060
            api_get_cidreq().'&id=';
2061
        while ($data = Database::fetch_array($res)) {
2062
            $actions = null;
2063
2064
            if ($apiIsAllowedToEdit) {
2065
                $url = $urlBase.$data['thp_id'].'&path='.$data['exe_name'];
2066
                $actions = Display::url(
2067
                    Display::return_icon('delete.png', get_lang('Delete')),
2068
                    $url
2069
                );
2070
            }
2071
2072
            $result[] = [
2073
                'firstname' => $data['firstname'],
2074
                'lastname' => $data['lastname'],
2075
                'username' => $data['username'],
2076
                'group_name' => implode(
2077
                    '<br/>',
2078
                    GroupManager::get_user_group_name($data['user_id'])
2079
                ),
2080
                'exe_date' => $data['exe_date'],
2081
                'score' => $data['exe_result'].' / '.$data['exe_weighting'],
2082
                'actions' => $actions,
2083
            ];
2084
        }
2085
2086
        return $result;
2087
    }
2088
2089
    /**
2090
     * @param string $exercisePath
2091
     * @param int    $userId
2092
     * @param int    $courseId
2093
     * @param int    $sessionId
2094
     *
2095
     * @return array
2096
     */
2097
    public static function getLatestHotPotatoResult(
2098
        $exercisePath,
2099
        $userId,
2100
        $courseId,
2101
        $sessionId
2102
    ) {
2103
        $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
2104
        $exercisePath = Database::escape_string($exercisePath);
2105
        $userId = (int) $userId;
2106
        $courseId = (int) $courseId;
2107
2108
        $sql = "SELECT * FROM $table
2109
                WHERE
2110
                    c_id = $courseId AND
2111
                    exe_name LIKE '$exercisePath%' AND
2112
                    exe_user_id = $userId
2113
                ORDER BY id
2114
                LIMIT 1";
2115
        $result = Database::query($sql);
2116
        $attempt = [];
2117
        if (Database::num_rows($result)) {
2118
            $attempt = Database::fetch_array($result, 'ASSOC');
2119
        }
2120
2121
        return $attempt;
2122
    }
2123
2124
    /**
2125
     * Gets exercise results.
2126
     *
2127
     * @todo this function should be moved in a library  + no global calls
2128
     *
2129
     * @param int    $from
2130
     * @param int    $number_of_items
2131
     * @param int    $column
2132
     * @param string $direction
2133
     * @param int    $exercise_id
2134
     * @param null   $extra_where_conditions
2135
     * @param bool   $get_count
2136
     * @param string $courseCode
2137
     * @param bool   $showSessionField
2138
     * @param bool   $showExerciseCategories
2139
     * @param array  $userExtraFieldsToAdd
2140
     * @param bool   $useCommaAsDecimalPoint
2141
     * @param bool   $roundValues
2142
     * @param bool   $getOnlyIds
2143
     *
2144
     * @return array
2145
     */
2146
    public static function get_exam_results_data(
2147
        $from,
2148
        $number_of_items,
2149
        $column,
2150
        $direction,
2151
        $exercise_id,
2152
        $extra_where_conditions = null,
2153
        $get_count = false,
2154
        $courseCode = null,
2155
        $showSessionField = false,
2156
        $showExerciseCategories = false,
2157
        $userExtraFieldsToAdd = [],
2158
        $useCommaAsDecimalPoint = false,
2159
        $roundValues = false,
2160
        $getOnlyIds = false,
2161
        $searchAllTeacherCourses = false,
2162
        $status = 0
2163
    ) {
2164
        //@todo replace all this globals
2165
        global $filter;
2166
        $courseCode = empty($courseCode) ? api_get_course_id() : $courseCode;
2167
        $courseInfo = api_get_course_info($courseCode);
2168
        $documentPath = '';
2169
        $sessionId = api_get_session_id();
2170
        $courseId = 0;
2171
        if (!empty($courseInfo)) {
2172
            $courseId = $courseInfo['real_id'];
2173
            $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document';
2174
        }
2175
2176
        $is_allowedToEdit =
2177
            api_is_allowed_to_edit(null, true) ||
2178
            api_is_allowed_to_edit(true) ||
2179
            api_is_drh() ||
2180
            api_is_student_boss() ||
2181
            api_is_session_admin();
2182
2183
        $courseCondition = "c_id = $courseId";
2184
        $statusCondition = '';
2185
2186
        if (!empty($status)) {
2187
            switch ($status) {
2188
                case 2:
2189
                    // validated
2190
                    $statusCondition = ' AND revised = 1 ';
2191
                    break;
2192
                case 3:
2193
                    // not validated
2194
                    $statusCondition = ' AND revised = 0 ';
2195
                    break;
2196
            }
2197
        }
2198
2199
        if (false === $searchAllTeacherCourses) {
2200
            if (empty($courseInfo)) {
2201
                return [];
2202
            }
2203
        } else {
2204
            $courses = CourseManager::get_courses_list_by_user_id(api_get_user_id(), false, false, false);
2205
2206
            if (empty($courses)) {
2207
                return [];
2208
            }
2209
2210
            $courses = array_column($courses, 'real_id');
2211
            $is_allowedToEdit = true;
2212
            $courseCondition = "c_id IN ('".implode("', '", $courses)."') ";
2213
        }
2214
2215
        $exercise_id = (int) $exercise_id;
2216
2217
        $TBL_USER = Database::get_main_table(TABLE_MAIN_USER);
2218
        $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
2219
        $TBL_GROUP_REL_USER = Database::get_course_table(TABLE_GROUP_USER);
2220
        $TBL_GROUP = Database::get_course_table(TABLE_GROUP);
2221
        $TBL_TRACK_EXERCICES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
2222
        $TBL_TRACK_HOTPOTATOES = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTPOTATOES);
2223
        $TBL_TRACK_ATTEMPT_RECORDING = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING);
2224
2225
        $session_id_and = '';
2226
        $sessionCondition = '';
2227
        if (!$showSessionField) {
2228
            $session_id_and = " AND te.session_id = $sessionId ";
2229
            $sessionCondition = " AND ttte.session_id = $sessionId";
2230
        }
2231
2232
        if ($searchAllTeacherCourses) {
2233
            $session_id_and = " AND te.session_id = 0 ";
2234
            $sessionCondition = " AND ttte.session_id = 0";
2235
        }
2236
2237
        if (empty($sessionId) &&
2238
            api_get_configuration_value('show_exercise_session_attempts_in_base_course')
2239
        ) {
2240
            $session_id_and = '';
2241
            $sessionCondition = '';
2242
        }
2243
2244
        $exercise_where = '';
2245
        $exerciseFilter = '';
2246
        if (!empty($exercise_id)) {
2247
            $exercise_where .= ' AND te.exe_exo_id = '.$exercise_id.' ';
2248
            $exerciseFilter = " AND exe_exo_id = $exercise_id ";
2249
        }
2250
2251
        $hotpotatoe_where = '';
2252
        if (!empty($_GET['path'])) {
2253
            $hotpotatoe_path = Database::escape_string($_GET['path']);
2254
            $hotpotatoe_where .= ' AND exe_name = "'.$hotpotatoe_path.'"  ';
2255
        }
2256
2257
        // sql for chamilo-type tests for teacher / tutor view
2258
        $sql_inner_join_tbl_track_exercices = "
2259
        (
2260
            SELECT DISTINCT ttte.*, if(tr.exe_id,1, 0) as revised
2261
            FROM $TBL_TRACK_EXERCICES ttte
2262
            LEFT JOIN $TBL_TRACK_ATTEMPT_RECORDING tr
2263
            ON (ttte.exe_id = tr.exe_id)
2264
            WHERE
2265
                $courseCondition
2266
                $exerciseFilter
2267
                $sessionCondition
2268
        )";
2269
2270
        if ($is_allowedToEdit) {
2271
            //@todo fix to work with COURSE_RELATION_TYPE_RRHH in both queries
2272
            // Hack in order to filter groups
2273
            $sql_inner_join_tbl_user = '';
2274
            if (strpos($extra_where_conditions, 'group_id')) {
2275
                $sql_inner_join_tbl_user = "
2276
                (
2277
                    SELECT
2278
                        u.user_id,
2279
                        firstname,
2280
                        lastname,
2281
                        official_code,
2282
                        email,
2283
                        username,
2284
                        g.name as group_name,
2285
                        g.id as group_id
2286
                    FROM $TBL_USER u
2287
                    INNER JOIN $TBL_GROUP_REL_USER gru
2288
                    ON (gru.user_id = u.user_id AND gru.c_id= $courseId )
2289
                    INNER JOIN $TBL_GROUP g
2290
                    ON (gru.group_id = g.id AND g.c_id= $courseId )
2291
                )";
2292
            }
2293
2294
            if (strpos($extra_where_conditions, 'group_all')) {
2295
                $extra_where_conditions = str_replace(
2296
                    "AND (  group_id = 'group_all'  )",
2297
                    '',
2298
                    $extra_where_conditions
2299
                );
2300
                $extra_where_conditions = str_replace(
2301
                    "AND group_id = 'group_all'",
2302
                    '',
2303
                    $extra_where_conditions
2304
                );
2305
                $extra_where_conditions = str_replace(
2306
                    "group_id = 'group_all' AND",
2307
                    '',
2308
                    $extra_where_conditions
2309
                );
2310
2311
                $sql_inner_join_tbl_user = "
2312
                (
2313
                    SELECT
2314
                        u.user_id,
2315
                        firstname,
2316
                        lastname,
2317
                        official_code,
2318
                        email,
2319
                        username,
2320
                        '' as group_name,
2321
                        '' as group_id
2322
                    FROM $TBL_USER u
2323
                )";
2324
                $sql_inner_join_tbl_user = null;
2325
            }
2326
2327
            if (strpos($extra_where_conditions, 'group_none')) {
2328
                $extra_where_conditions = str_replace(
2329
                    "AND (  group_id = 'group_none'  )",
2330
                    "AND (  group_id is null  )",
2331
                    $extra_where_conditions
2332
                );
2333
                $extra_where_conditions = str_replace(
2334
                    "AND group_id = 'group_none'",
2335
                    "AND (  group_id is null  )",
2336
                    $extra_where_conditions
2337
                );
2338
                $sql_inner_join_tbl_user = "
2339
            (
2340
                SELECT
2341
                    u.user_id,
2342
                    firstname,
2343
                    lastname,
2344
                    official_code,
2345
                    email,
2346
                    username,
2347
                    g.name as group_name,
2348
                    g.id as group_id
2349
                FROM $TBL_USER u
2350
                LEFT OUTER JOIN $TBL_GROUP_REL_USER gru
2351
                ON ( gru.user_id = u.user_id AND gru.c_id = $courseId )
2352
                LEFT OUTER JOIN $TBL_GROUP g
2353
                ON (gru.group_id = g.id AND g.c_id = $courseId )
2354
            )";
2355
            }
2356
2357
            // All
2358
            $is_empty_sql_inner_join_tbl_user = false;
2359
            if (empty($sql_inner_join_tbl_user)) {
2360
                $is_empty_sql_inner_join_tbl_user = true;
2361
                $sql_inner_join_tbl_user = "
2362
            (
2363
                SELECT u.user_id, firstname, lastname, email, username, ' ' as group_name, '' as group_id, official_code
2364
                FROM $TBL_USER u
2365
                WHERE u.status NOT IN(".api_get_users_status_ignored_in_reports('string').")
2366
            )";
2367
            }
2368
2369
            $sqlFromOption = '';
2370
            $sqlWhereOption = '';
2371
            if (false === $searchAllTeacherCourses) {
2372
                $sqlFromOption = " , $TBL_GROUP_REL_USER AS gru ";
2373
                $sqlWhereOption = "  AND gru.c_id = $courseId AND gru.user_id = user.user_id ";
2374
            }
2375
2376
            $first_and_last_name = api_is_western_name_order() ? "firstname, lastname" : "lastname, firstname";
2377
2378
            if ($get_count) {
2379
                $sql_select = 'SELECT count(te.exe_id) ';
2380
            } else {
2381
                $sql_select = "SELECT DISTINCT
2382
                    user_id,
2383
                    $first_and_last_name,
2384
                    official_code,
2385
                    ce.title,
2386
                    username,
2387
                    te.exe_result,
2388
                    te.exe_weighting,
2389
                    te.exe_date,
2390
                    te.exe_id,
2391
                    te.c_id,
2392
                    te.session_id,
2393
                    email as exemail,
2394
                    te.start_date,
2395
                    ce.expired_time,
2396
                    steps_counter,
2397
                    exe_user_id,
2398
                    te.exe_duration,
2399
                    te.status as completion_status,
2400
                    propagate_neg,
2401
                    revised,
2402
                    group_name,
2403
                    group_id,
2404
                    orig_lp_id,
2405
                    te.user_ip";
2406
            }
2407
2408
            $sql = " $sql_select
2409
                FROM $TBL_EXERCICES AS ce
2410
                INNER JOIN $sql_inner_join_tbl_track_exercices AS te
2411
                ON (te.exe_exo_id = ce.iid)
2412
                INNER JOIN $sql_inner_join_tbl_user AS user
2413
                ON (user.user_id = exe_user_id)
2414
                WHERE
2415
                    te.$courseCondition
2416
                    $session_id_and AND
2417
                    ce.active <> -1 AND
2418
                    ce.$courseCondition
2419
                    $exercise_where
2420
                    $extra_where_conditions
2421
                    $statusCondition
2422
                ";
2423
2424
            // sql for hotpotatoes tests for teacher / tutor view
2425
            if ($get_count) {
2426
                $hpsql_select = ' SELECT count(username) ';
2427
            } else {
2428
                $hpsql_select = " SELECT
2429
                    $first_and_last_name ,
2430
                    username,
2431
                    official_code,
2432
                    tth.exe_name,
2433
                    tth.exe_result ,
2434
                    tth.exe_weighting,
2435
                    tth.exe_date";
2436
            }
2437
2438
            $hpsql = " $hpsql_select
2439
                FROM
2440
                    $TBL_TRACK_HOTPOTATOES tth,
2441
                    $TBL_USER user
2442
                    $sqlFromOption
2443
                WHERE
2444
                    user.user_id=tth.exe_user_id AND
2445
                    tth.$courseCondition
2446
                    $hotpotatoe_where
2447
                    $sqlWhereOption AND
2448
                     user.status NOT IN (".api_get_users_status_ignored_in_reports('string').")
2449
                ORDER BY tth.c_id ASC, tth.exe_date DESC ";
2450
        }
2451
2452
        if (empty($sql)) {
2453
            return false;
2454
        }
2455
2456
        if ($get_count) {
2457
            $resx = Database::query($sql);
2458
            $rowx = Database::fetch_row($resx, 'ASSOC');
2459
2460
            return $rowx[0];
2461
        }
2462
2463
        $teacher_id_list = [];
2464
        if (!empty($courseCode)) {
2465
            $teacher_list = CourseManager::get_teacher_list_from_course_code($courseCode);
2466
            if (!empty($teacher_list)) {
2467
                foreach ($teacher_list as $teacher) {
2468
                    $teacher_id_list[] = $teacher['user_id'];
2469
                }
2470
            }
2471
        }
2472
2473
        $scoreDisplay = new ScoreDisplay();
2474
        $decimalSeparator = '.';
2475
        $thousandSeparator = ',';
2476
2477
        if ($useCommaAsDecimalPoint) {
2478
            $decimalSeparator = ',';
2479
            $thousandSeparator = '';
2480
        }
2481
2482
        $listInfo = [];
2483
        // Simple exercises
2484
        if (empty($hotpotatoe_where)) {
2485
            $column = !empty($column) ? Database::escape_string($column) : null;
2486
            $from = (int) $from;
2487
            $number_of_items = (int) $number_of_items;
2488
            $direction = !in_array(strtolower(trim($direction)), ['asc', 'desc']) ? 'asc' : $direction;
2489
2490
            if (!empty($column)) {
2491
                $sql .= " ORDER BY `$column` $direction ";
2492
            }
2493
2494
            if (!$getOnlyIds) {
2495
                $sql .= " LIMIT $from, $number_of_items";
2496
            }
2497
2498
            $results = [];
2499
            $resx = Database::query($sql);
2500
            while ($rowx = Database::fetch_array($resx, 'ASSOC')) {
2501
                $results[] = $rowx;
2502
            }
2503
2504
            $clean_group_list = [];
2505
            $lp_list = [];
2506
2507
            if (!empty($courseInfo)) {
2508
                $group_list = GroupManager::get_group_list(null, $courseInfo);
2509
                if (!empty($group_list)) {
2510
                    foreach ($group_list as $group) {
2511
                        $clean_group_list[$group['id']] = $group['name'];
2512
                    }
2513
                }
2514
2515
                $lp_list_obj = new LearnpathList(api_get_user_id());
2516
                $lp_list = $lp_list_obj->get_flat_list();
2517
                $oldIds = array_column($lp_list, 'lp_old_id', 'iid');
2518
            }
2519
2520
            if (is_array($results)) {
2521
                $users_array_id = [];
2522
                $from_gradebook = false;
2523
                if (isset($_GET['gradebook']) && $_GET['gradebook'] === 'view') {
2524
                    $from_gradebook = true;
2525
                }
2526
                $sizeof = count($results);
2527
                $locked = api_resource_is_locked_by_gradebook($exercise_id, LINK_EXERCISE);
2528
                $timeNow = strtotime(api_get_utc_datetime());
2529
                $courseItemList = [];
2530
                // Looping results
2531
                for ($i = 0; $i < $sizeof; $i++) {
2532
                    $attempt = $results[$i];
2533
                    $revised = $attempt['revised'];
2534
                    $attemptSessionId = (int) $attempt['session_id'];
2535
                    if (false === $searchAllTeacherCourses) {
2536
                        $courseItemInfo = api_get_course_info();
2537
                        $cidReq = api_get_cidreq(false).'&id_session='.$attemptSessionId;
2538
                    } else {
2539
                        if (isset($courseItemList[$attempt['c_id']])) {
2540
                            $courseItemInfo = $courseItemList[$attempt['c_id']];
2541
                        } else {
2542
                            $courseItemInfo = api_get_course_info_by_id($attempt['c_id']);
2543
                            $courseItemList[$attempt['c_id']] = $courseItemInfo;
2544
                        }
2545
                        $cidReq = 'cidReq='.$courseItemInfo['code'].'&id_session='.$attemptSessionId;
2546
                    }
2547
2548
                    if ('incomplete' === $attempt['completion_status']) {
2549
                        // If the exercise was incomplete, we need to determine
2550
                        // if it is still into the time allowed, or if its
2551
                        // allowed time has expired and it can be closed
2552
                        // (it's "unclosed")
2553
                        $minutes = $attempt['expired_time'];
2554
                        if ($minutes == 0) {
2555
                            // There's no time limit, so obviously the attempt
2556
                            // can still be "ongoing", but the teacher should
2557
                            // be able to choose to close it, so mark it as
2558
                            // "unclosed" instead of "ongoing"
2559
                            $revised = 2;
2560
                        } else {
2561
                            $allowedSeconds = $minutes * 60;
2562
                            $timeAttemptStarted = strtotime($attempt['start_date']);
2563
                            $secondsSinceStart = $timeNow - $timeAttemptStarted;
2564
                            $revised = 3; // mark as "ongoing"
2565
                            if ($secondsSinceStart > $allowedSeconds) {
2566
                                $revised = 2; // mark as "unclosed"
2567
                            }
2568
                        }
2569
                    }
2570
2571
                    if ($from_gradebook && ($is_allowedToEdit)) {
2572
                        if (in_array(
2573
                            $attempt['username'].$attempt['firstname'].$attempt['lastname'],
2574
                            $users_array_id
2575
                        )) {
2576
                            continue;
2577
                        }
2578
                        $users_array_id[] = $attempt['username'].$attempt['firstname'].$attempt['lastname'];
2579
                    }
2580
2581
                    $lp_obj = isset($attempt['orig_lp_id']) &&
2582
                        isset($lp_list[$attempt['orig_lp_id']]) ? $lp_list[$attempt['orig_lp_id']] : null;
2583
                    if (empty($lp_obj)) {
2584
                        // Try to get the old id (id instead of iid)
2585
                        $lpNewId = isset($attempt['orig_lp_id']) &&
2586
                        isset($oldIds[$attempt['orig_lp_id']]) ? $oldIds[$attempt['orig_lp_id']] : null;
2587
                        if ($lpNewId) {
2588
                            $lp_obj = isset($lp_list[$lpNewId]) ? $lp_list[$lpNewId] : null;
2589
                        }
2590
                    }
2591
                    $lp_name = null;
2592
                    if ($lp_obj) {
2593
                        $url = api_get_path(WEB_CODE_PATH).
2594
                            'lp/lp_controller.php?'.$cidReq.'&action=view&lp_id='.$attempt['orig_lp_id'];
2595
                        $lp_name = Display::url(
2596
                            $lp_obj['lp_name'],
2597
                            $url,
2598
                            ['target' => '_blank']
2599
                        );
2600
                    }
2601
2602
                    // Add all groups by user
2603
                    $group_name_list = '';
2604
                    if ($is_empty_sql_inner_join_tbl_user) {
2605
                        $group_list = GroupManager::get_group_ids(
2606
                            api_get_course_int_id(),
2607
                            $attempt['user_id']
2608
                        );
2609
2610
                        foreach ($group_list as $id) {
2611
                            if (isset($clean_group_list[$id])) {
2612
                                $group_name_list .= $clean_group_list[$id].'<br/>';
2613
                            }
2614
                        }
2615
                        $attempt['group_name'] = $group_name_list;
2616
                    }
2617
2618
                    $attempt['exe_duration'] = !empty($attempt['exe_duration']) ? round($attempt['exe_duration'] / 60) : 0;
2619
                    $id = $attempt['exe_id'];
2620
                    $dt = api_convert_and_format_date($attempt['exe_weighting']);
2621
2622
                    // we filter the results if we have the permission to
2623
                    $result_disabled = 0;
2624
                    if (isset($attempt['results_disabled'])) {
2625
                        $result_disabled = (int) $attempt['results_disabled'];
2626
                    }
2627
                    if ($result_disabled == 0) {
2628
                        $my_res = $attempt['exe_result'];
2629
                        $my_total = $attempt['exe_weighting'];
2630
                        $attempt['start_date'] = api_get_local_time($attempt['start_date']);
2631
                        $attempt['exe_date'] = api_get_local_time($attempt['exe_date']);
2632
2633
                        if (!$attempt['propagate_neg'] && $my_res < 0) {
2634
                            $my_res = 0;
2635
                        }
2636
2637
                        $score = self::show_score(
2638
                            $my_res,
2639
                            $my_total,
2640
                            true,
2641
                            true,
2642
                            false,
2643
                            false,
2644
                            $decimalSeparator,
2645
                            $thousandSeparator,
2646
                            $roundValues
2647
                        );
2648
2649
                        $actions = '<div class="pull-right">';
2650
                        if ($is_allowedToEdit) {
2651
                            if (isset($teacher_id_list)) {
2652
                                if (in_array(
2653
                                    $attempt['exe_user_id'],
2654
                                    $teacher_id_list
2655
                                )) {
2656
                                    $actions .= Display::return_icon('teacher.png', get_lang('Teacher'));
2657
                                }
2658
                            }
2659
                            $revisedLabel = '';
2660
                            switch ($revised) {
2661
                                case 0:
2662
                                    $actions .= "<a href='exercise_show.php?".$cidReq."&action=qualify&id=$id'>".
2663
                                        Display:: return_icon(
2664
                                            'quiz.png',
2665
                                            get_lang('Qualify')
2666
                                        );
2667
                                    $actions .= '</a>';
2668
                                    $revisedLabel = Display::label(
2669
                                        get_lang('NotValidated'),
2670
                                        'info'
2671
                                    );
2672
                                    break;
2673
                                case 1:
2674
                                    $actions .= "<a href='exercise_show.php?".$cidReq."&action=edit&id=$id'>".
2675
                                        Display:: return_icon(
2676
                                            'edit.png',
2677
                                            get_lang('Edit'),
2678
                                            [],
2679
                                            ICON_SIZE_SMALL
2680
                                        );
2681
                                    $actions .= '</a>';
2682
                                    $revisedLabel = Display::label(
2683
                                        get_lang('Validated'),
2684
                                        'success'
2685
                                    );
2686
                                    break;
2687
                                case 2: //finished but not marked as such
2688
                                    $actions .= '<a href="exercise_report.php?'
2689
                                        .$cidReq
2690
                                        .'&exerciseId='.$exercise_id
2691
                                        .'&a=close&id='.$id
2692
                                        .'">'.
2693
                                        Display:: return_icon(
2694
                                            'lock.png',
2695
                                            get_lang('MarkAttemptAsClosed'),
2696
                                            [],
2697
                                            ICON_SIZE_SMALL
2698
                                        );
2699
                                    $actions .= '</a>';
2700
                                    $revisedLabel = Display::label(
2701
                                        get_lang('Unclosed'),
2702
                                        'warning'
2703
                                    );
2704
                                    break;
2705
                                case 3: //still ongoing
2706
                                    $actions .= Display:: return_icon(
2707
                                        'clock.png',
2708
                                        get_lang('AttemptStillOngoingPleaseWait'),
2709
                                        [],
2710
                                        ICON_SIZE_SMALL
2711
                                    );
2712
                                    $actions .= '';
2713
                                    $revisedLabel = Display::label(
2714
                                        get_lang('Ongoing'),
2715
                                        'danger'
2716
                                    );
2717
                                    break;
2718
                            }
2719
2720
                            if ($filter == 2) {
2721
                                $actions .= ' <a href="exercise_history.php?'.$cidReq.'&exe_id='.$id.'">'.
2722
                                    Display:: return_icon(
2723
                                        'history.png',
2724
                                        get_lang('ViewHistoryChange')
2725
                                    ).'</a>';
2726
                            }
2727
2728
                            // Admin can always delete the attempt
2729
                            if (($locked == false || api_is_platform_admin()) && !api_is_student_boss()) {
2730
                                $ip = Tracking::get_ip_from_user_event(
2731
                                    $attempt['exe_user_id'],
2732
                                    api_get_utc_datetime(),
2733
                                    false
2734
                                );
2735
                                $actions .= '<a href="http://www.whatsmyip.org/ip-geo-location/?ip='.$ip.'" target="_blank">'
2736
                                    .Display::return_icon('info.png', $ip)
2737
                                    .'</a>';
2738
2739
                                $recalculateUrl = api_get_path(WEB_CODE_PATH).'exercise/recalculate.php?'.
2740
                                    $cidReq.'&'.
2741
                                    http_build_query([
2742
                                        'id' => $id,
2743
                                        'exercise' => $exercise_id,
2744
                                        'user' => $attempt['exe_user_id'],
2745
                                    ]);
2746
                                $actions .= Display::url(
2747
                                    Display::return_icon('reload.png', get_lang('RecalculateResults')),
2748
                                    $recalculateUrl,
2749
                                    [
2750
                                        'data-exercise' => $exercise_id,
2751
                                        'data-user' => $attempt['exe_user_id'],
2752
                                        'data-id' => $id,
2753
                                        'class' => 'exercise-recalculate',
2754
                                    ]
2755
                                );
2756
2757
                                $filterByUser = isset($_GET['filter_by_user']) ? (int) $_GET['filter_by_user'] : 0;
2758
                                $delete_link = '<a
2759
                                    href="exercise_report.php?'.$cidReq.'&filter_by_user='.$filterByUser.'&filter='.$filter.'&exerciseId='.$exercise_id.'&delete=delete&did='.$id.'"
2760
                                    onclick="javascript:if(!confirm(\''.sprintf(
2761
                                        addslashes(get_lang('DeleteAttempt')),
2762
                                        $attempt['username'],
2763
                                        $dt
2764
                                    ).'\')) return false;">';
2765
                                $delete_link .= Display::return_icon(
2766
                                        'delete.png',
2767
                                        addslashes(get_lang('Delete'))
2768
                                    ).'</a>';
2769
2770
                                if (api_is_drh() && !api_is_platform_admin()) {
2771
                                    $delete_link = null;
2772
                                }
2773
                                if (api_is_session_admin()) {
2774
                                    $delete_link = '';
2775
                                }
2776
                                if ($revised == 3) {
2777
                                    $delete_link = null;
2778
                                }
2779
                                $actions .= $delete_link;
2780
                            }
2781
                        } else {
2782
                            $attempt_url = api_get_path(WEB_CODE_PATH).'exercise/result.php?'.$cidReq.'&id='.$attempt['exe_id'];
2783
                            $attempt_link = Display::url(
2784
                                get_lang('Show'),
2785
                                $attempt_url,
2786
                                [
2787
                                    'class' => 'ajax btn btn-default',
2788
                                    'data-title' => get_lang('Show'),
2789
                                ]
2790
                            );
2791
                            $actions .= $attempt_link;
2792
                        }
2793
                        $actions .= '</div>';
2794
2795
                        if (!empty($userExtraFieldsToAdd)) {
2796
                            foreach ($userExtraFieldsToAdd as $variable) {
2797
                                $extraFieldValue = new ExtraFieldValue('user');
2798
                                $values = $extraFieldValue->get_values_by_handler_and_field_variable(
2799
                                    $attempt['user_id'],
2800
                                    $variable
2801
                                );
2802
                                if (isset($values['value'])) {
2803
                                    $attempt[$variable] = $values['value'];
2804
                                }
2805
                            }
2806
                        }
2807
2808
                        $exeId = $attempt['exe_id'];
2809
                        $attempt['id'] = $exeId;
2810
                        $category_list = [];
2811
                        if ($is_allowedToEdit) {
2812
                            $sessionName = '';
2813
                            $sessionStartAccessDate = '';
2814
                            if (!empty($attemptSessionId)) {
2815
                                $sessionInfo = api_get_session_info($attemptSessionId);
2816
                                if (!empty($sessionInfo)) {
2817
                                    $sessionName = $sessionInfo['name'];
2818
                                    $sessionStartAccessDate = api_get_local_time($sessionInfo['access_start_date']);
2819
                                }
2820
                            }
2821
2822
                            $courseId = $courseItemInfo['real_id'];
2823
2824
                            if ($searchAllTeacherCourses) {
2825
                                $attempt['course'] = $courseItemInfo['title'];
2826
                                $attempt['exercise'] = $attempt['title'];
2827
                            }
2828
2829
                            $objExercise = new Exercise($courseId);
2830
                            if ($showExerciseCategories) {
2831
                                // Getting attempt info
2832
                                $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
2833
                                if (!empty($exercise_stat_info['data_tracking'])) {
2834
                                    $question_list = explode(',', $exercise_stat_info['data_tracking']);
2835
                                    if (!empty($question_list)) {
2836
                                        foreach ($question_list as $questionId) {
2837
                                            $objQuestionTmp = Question::read($questionId, $objExercise->course);
2838
                                            // We're inside *one* question.
2839
                                            // Go through each possible answer for this question.
2840
                                            $result = $objExercise->manage_answer(
2841
                                                $exeId,
2842
                                                $questionId,
2843
                                                null,
2844
                                                'exercise_result',
2845
                                                false,
2846
                                                false,
2847
                                                true,
2848
                                                false,
2849
                                                $objExercise->selectPropagateNeg(),
2850
                                                null,
2851
                                                true
2852
                                            );
2853
2854
                                            $my_total_score = $result['score'];
2855
                                            $my_total_weight = $result['weight'];
2856
2857
                                            // Category report
2858
                                            $category_was_added_for_this_test = false;
2859
                                            if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
2860
                                                if (!isset($category_list[$objQuestionTmp->category]['score'])) {
2861
                                                    $category_list[$objQuestionTmp->category]['score'] = 0;
2862
                                                }
2863
                                                if (!isset($category_list[$objQuestionTmp->category]['total'])) {
2864
                                                    $category_list[$objQuestionTmp->category]['total'] = 0;
2865
                                                }
2866
                                                $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
2867
                                                $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
2868
                                                $category_was_added_for_this_test = true;
2869
                                            }
2870
2871
                                            if (isset($objQuestionTmp->category_list) &&
2872
                                                !empty($objQuestionTmp->category_list)
2873
                                            ) {
2874
                                                foreach ($objQuestionTmp->category_list as $category_id) {
2875
                                                    $category_list[$category_id]['score'] += $my_total_score;
2876
                                                    $category_list[$category_id]['total'] += $my_total_weight;
2877
                                                    $category_was_added_for_this_test = true;
2878
                                                }
2879
                                            }
2880
2881
                                            // No category for this question!
2882
                                            if ($category_was_added_for_this_test == false) {
2883
                                                if (!isset($category_list['none']['score'])) {
2884
                                                    $category_list['none']['score'] = 0;
2885
                                                }
2886
                                                if (!isset($category_list['none']['total'])) {
2887
                                                    $category_list['none']['total'] = 0;
2888
                                                }
2889
2890
                                                $category_list['none']['score'] += $my_total_score;
2891
                                                $category_list['none']['total'] += $my_total_weight;
2892
                                            }
2893
                                        }
2894
                                    }
2895
                                }
2896
                            }
2897
2898
                            foreach ($category_list as $categoryId => $result) {
2899
                                $scoreToDisplay = self::show_score(
2900
                                    $result['score'],
2901
                                    $result['total'],
2902
                                    true,
2903
                                    true,
2904
                                    false,
2905
                                    false,
2906
                                    $decimalSeparator,
2907
                                    $thousandSeparator,
2908
                                    $roundValues
2909
                                );
2910
                                $attempt['category_'.$categoryId] = $scoreToDisplay;
2911
                                $attempt['category_'.$categoryId.'_score_percentage'] = self::show_score(
2912
                                    $result['score'],
2913
                                    $result['total'],
2914
                                    true,
2915
                                    true,
2916
                                    true,
2917
                                    true,
2918
                                    $decimalSeparator,
2919
                                    $thousandSeparator,
2920
                                    $roundValues
2921
                                );
2922
                                $attempt['category_'.$categoryId.'_only_score'] = $result['score'];
2923
                                $attempt['category_'.$categoryId.'_total'] = $result['total'];
2924
                            }
2925
                            $attempt['session'] = $sessionName;
2926
                            $attempt['session_access_start_date'] = $sessionStartAccessDate;
2927
                            $attempt['status'] = $revisedLabel;
2928
                            $attempt['score'] = $score;
2929
                            $attempt['score_percentage'] = self::show_score(
2930
                                $my_res,
2931
                                $my_total,
2932
                                true,
2933
                                true,
2934
                                true,
2935
                                true,
2936
                                $decimalSeparator,
2937
                                $thousandSeparator,
2938
                                $roundValues
2939
                            );
2940
2941
                            if ($roundValues) {
2942
                                $whole = floor($my_res); // 1
2943
                                $fraction = $my_res - $whole; // .25
2944
                                if ($fraction >= 0.5) {
2945
                                    $onlyScore = ceil($my_res);
2946
                                } else {
2947
                                    $onlyScore = round($my_res);
2948
                                }
2949
                            } else {
2950
                                $onlyScore = $scoreDisplay->format_score(
2951
                                    $my_res,
2952
                                    false,
2953
                                    $decimalSeparator,
2954
                                    $thousandSeparator
2955
                                );
2956
                            }
2957
2958
                            $attempt['only_score'] = $onlyScore;
2959
2960
                            if ($roundValues) {
2961
                                $whole = floor($my_total); // 1
2962
                                $fraction = $my_total - $whole; // .25
2963
                                if ($fraction >= 0.5) {
2964
                                    $onlyTotal = ceil($my_total);
2965
                                } else {
2966
                                    $onlyTotal = round($my_total);
2967
                                }
2968
                            } else {
2969
                                $onlyTotal = $scoreDisplay->format_score(
2970
                                    $my_total,
2971
                                    false,
2972
                                    $decimalSeparator,
2973
                                    $thousandSeparator
2974
                                );
2975
                            }
2976
                            $attempt['total'] = $onlyTotal;
2977
                            $attempt['lp'] = $lp_name;
2978
                            $attempt['actions'] = $actions;
2979
                            $listInfo[] = $attempt;
2980
                        } else {
2981
                            $attempt['status'] = $revisedLabel;
2982
                            $attempt['score'] = $score;
2983
                            $attempt['actions'] = $actions;
2984
                            $listInfo[] = $attempt;
2985
                        }
2986
                    }
2987
                }
2988
            }
2989
        } else {
2990
            $hpresults = [];
2991
            $res = Database::query($hpsql);
2992
            if ($res !== false) {
2993
                $i = 0;
2994
                while ($resA = Database::fetch_array($res, 'NUM')) {
2995
                    for ($j = 0; $j < 6; $j++) {
2996
                        $hpresults[$i][$j] = $resA[$j];
2997
                    }
2998
                    $i++;
2999
                }
3000
            }
3001
3002
            // Print HotPotatoes test results.
3003
            if (is_array($hpresults)) {
3004
                for ($i = 0; $i < count($hpresults); $i++) {
3005
                    $hp_title = GetQuizName($hpresults[$i][3], $documentPath);
3006
                    if ($hp_title == '') {
3007
                        $hp_title = basename($hpresults[$i][3]);
3008
                    }
3009
3010
                    $hp_date = api_get_local_time(
3011
                        $hpresults[$i][6],
3012
                        null,
3013
                        date_default_timezone_get()
3014
                    );
3015
                    $hp_result = round(($hpresults[$i][4] / ($hpresults[$i][5] != 0 ? $hpresults[$i][5] : 1)) * 100, 2);
3016
                    $hp_result .= '% ('.$hpresults[$i][4].' / '.$hpresults[$i][5].')';
3017
3018
                    if ($is_allowedToEdit) {
3019
                        $listInfo[] = [
3020
                            $hpresults[$i][0],
3021
                            $hpresults[$i][1],
3022
                            $hpresults[$i][2],
3023
                            '',
3024
                            $hp_title,
3025
                            '-',
3026
                            $hp_date,
3027
                            $hp_result,
3028
                            '-',
3029
                        ];
3030
                    } else {
3031
                        $listInfo[] = [
3032
                            $hp_title,
3033
                            '-',
3034
                            $hp_date,
3035
                            $hp_result,
3036
                            '-',
3037
                        ];
3038
                    }
3039
                }
3040
            }
3041
        }
3042
3043
        return $listInfo;
3044
    }
3045
3046
    /**
3047
     * @param $score
3048
     * @param $weight
3049
     *
3050
     * @return array
3051
     */
3052
    public static function convertScoreToPlatformSetting($score, $weight)
3053
    {
3054
        $maxNote = api_get_setting('exercise_max_score');
3055
        $minNote = api_get_setting('exercise_min_score');
3056
3057
        if ($maxNote != '' && $minNote != '') {
3058
            if (!empty($weight) && (float) $weight !== (float) 0) {
3059
                $score = $minNote + ($maxNote - $minNote) * $score / $weight;
3060
            } else {
3061
                $score = $minNote;
3062
            }
3063
            $weight = $maxNote;
3064
        }
3065
3066
        return ['score' => $score, 'weight' => $weight];
3067
    }
3068
3069
    /**
3070
     * Converts the score with the exercise_max_note and exercise_min_score
3071
     * the platform settings + formats the results using the float_format function.
3072
     *
3073
     * @param float  $score
3074
     * @param float  $weight
3075
     * @param bool   $show_percentage       show percentage or not
3076
     * @param bool   $use_platform_settings use or not the platform settings
3077
     * @param bool   $show_only_percentage
3078
     * @param bool   $hidePercentageSign    hide "%" sign
3079
     * @param string $decimalSeparator
3080
     * @param string $thousandSeparator
3081
     * @param bool   $roundValues           This option rounds the float values into a int using ceil()
3082
     * @param bool   $removeEmptyDecimals
3083
     *
3084
     * @return string an html with the score modified
3085
     */
3086
    public static function show_score(
3087
        $score,
3088
        $weight,
3089
        $show_percentage = true,
3090
        $use_platform_settings = true,
3091
        $show_only_percentage = false,
3092
        $hidePercentageSign = false,
3093
        $decimalSeparator = '.',
3094
        $thousandSeparator = ',',
3095
        $roundValues = false,
3096
        $removeEmptyDecimals = false
3097
    ) {
3098
        if (is_null($score) && is_null($weight)) {
3099
            return '-';
3100
        }
3101
3102
        $decimalSeparator = empty($decimalSeparator) ? '.' : $decimalSeparator;
3103
        $thousandSeparator = empty($thousandSeparator) ? ',' : $thousandSeparator;
3104
3105
        if ($use_platform_settings) {
3106
            $result = self::convertScoreToPlatformSetting($score, $weight);
3107
            $score = $result['score'];
3108
            $weight = $result['weight'];
3109
        }
3110
3111
        $percentage = (100 * $score) / ($weight != 0 ? $weight : 1);
3112
3113
        // Formats values
3114
        $percentage = float_format($percentage, 1);
3115
        $score = float_format($score, 1);
3116
        $weight = float_format($weight, 1);
3117
3118
        if ($roundValues) {
3119
            $whole = floor($percentage); // 1
3120
            $fraction = $percentage - $whole; // .25
3121
3122
            // Formats values
3123
            if ($fraction >= 0.5) {
3124
                $percentage = ceil($percentage);
3125
            } else {
3126
                $percentage = round($percentage);
3127
            }
3128
3129
            $whole = floor($score); // 1
3130
            $fraction = $score - $whole; // .25
3131
            if ($fraction >= 0.5) {
3132
                $score = ceil($score);
3133
            } else {
3134
                $score = round($score);
3135
            }
3136
3137
            $whole = floor($weight); // 1
3138
            $fraction = $weight - $whole; // .25
3139
            if ($fraction >= 0.5) {
3140
                $weight = ceil($weight);
3141
            } else {
3142
                $weight = round($weight);
3143
            }
3144
        } else {
3145
            // Formats values
3146
            $percentage = float_format($percentage, 1, $decimalSeparator, $thousandSeparator);
3147
            $score = float_format($score, 1, $decimalSeparator, $thousandSeparator);
3148
            $weight = float_format($weight, 1, $decimalSeparator, $thousandSeparator);
3149
        }
3150
3151
        if ($show_percentage) {
3152
            $percentageSign = ' %';
3153
            if ($hidePercentageSign) {
3154
                $percentageSign = '';
3155
            }
3156
            $html = $percentage."$percentageSign ($score / $weight)";
3157
            if ($show_only_percentage) {
3158
                $html = $percentage.$percentageSign;
3159
            }
3160
        } else {
3161
            if ($removeEmptyDecimals) {
3162
                if (ScoreDisplay::hasEmptyDecimals($weight)) {
3163
                    $weight = round($weight);
3164
                }
3165
            }
3166
            $html = $score.' / '.$weight;
3167
        }
3168
3169
        // Over write score
3170
        $scoreBasedInModel = self::convertScoreToModel($percentage);
3171
        if (!empty($scoreBasedInModel)) {
3172
            $html = $scoreBasedInModel;
3173
        }
3174
3175
        // Ignore other formats and use the configuration['exercise_score_format'] value
3176
        // But also keep the round values settings.
3177
        $format = api_get_configuration_value('exercise_score_format');
3178
        if (!empty($format)) {
3179
            $html = ScoreDisplay::instance()->display_score([$score, $weight], $format);
3180
        }
3181
3182
        return Display::span($html, ['class' => 'score_exercise']);
3183
    }
3184
3185
    /**
3186
     * @param array $model
3187
     * @param float $percentage
3188
     *
3189
     * @return string
3190
     */
3191
    public static function getModelStyle($model, $percentage)
3192
    {
3193
        return '<span class="'.$model['css_class'].'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
3194
    }
3195
3196
    /**
3197
     * @param float $percentage value between 0 and 100
3198
     *
3199
     * @return string
3200
     */
3201
    public static function convertScoreToModel($percentage)
3202
    {
3203
        $model = self::getCourseScoreModel();
3204
        if (!empty($model)) {
3205
            $scoreWithGrade = [];
3206
            foreach ($model['score_list'] as $item) {
3207
                if ($percentage >= $item['min'] && $percentage <= $item['max']) {
3208
                    $scoreWithGrade = $item;
3209
                    break;
3210
                }
3211
            }
3212
3213
            if (!empty($scoreWithGrade)) {
3214
                return self::getModelStyle($scoreWithGrade, $percentage);
3215
            }
3216
        }
3217
3218
        return '';
3219
    }
3220
3221
    /**
3222
     * @return array
3223
     */
3224
    public static function getCourseScoreModel()
3225
    {
3226
        $modelList = self::getScoreModels();
3227
        if (empty($modelList)) {
3228
            return [];
3229
        }
3230
3231
        $courseInfo = api_get_course_info();
3232
        if (!empty($courseInfo)) {
3233
            $scoreModelId = api_get_course_setting('score_model_id');
3234
            if (-1 != $scoreModelId) {
3235
                $modelIdList = array_column($modelList['models'], 'id');
3236
                if (in_array($scoreModelId, $modelIdList)) {
3237
                    foreach ($modelList['models'] as $item) {
3238
                        if ($item['id'] == $scoreModelId) {
3239
                            return $item;
3240
                        }
3241
                    }
3242
                }
3243
            }
3244
        }
3245
3246
        return [];
3247
    }
3248
3249
    /**
3250
     * @return array
3251
     */
3252
    public static function getScoreModels()
3253
    {
3254
        return api_get_configuration_value('score_grade_model');
3255
    }
3256
3257
    /**
3258
     * @param float  $score
3259
     * @param float  $weight
3260
     * @param string $passPercentage
3261
     *
3262
     * @return bool
3263
     */
3264
    public static function isSuccessExerciseResult($score, $weight, $passPercentage)
3265
    {
3266
        $percentage = float_format(
3267
            ($score / (0 != $weight ? $weight : 1)) * 100,
3268
            1
3269
        );
3270
        if (isset($passPercentage) && !empty($passPercentage)) {
3271
            if ($percentage >= $passPercentage) {
3272
                return true;
3273
            }
3274
        }
3275
3276
        return false;
3277
    }
3278
3279
    /**
3280
     * @param string $name
3281
     * @param $weight
3282
     * @param $selected
3283
     *
3284
     * @return bool
3285
     */
3286
    public static function addScoreModelInput(
3287
        FormValidator $form,
3288
        $name,
3289
        $weight,
3290
        $selected
3291
    ) {
3292
        $model = self::getCourseScoreModel();
3293
        if (empty($model)) {
3294
            return false;
3295
        }
3296
3297
        /** @var HTML_QuickForm_select $element */
3298
        $element = $form->createElement(
3299
            'select',
3300
            $name,
3301
            get_lang('Qualification'),
3302
            [],
3303
            ['class' => 'exercise_mark_select']
3304
        );
3305
3306
        foreach ($model['score_list'] as $item) {
3307
            $i = api_number_format($item['score_to_qualify'] / 100 * $weight, 2);
3308
            $label = self::getModelStyle($item, $i);
3309
            $attributes = [
3310
                'class' => $item['css_class'],
3311
            ];
3312
            if ($selected == $i) {
3313
                $attributes['selected'] = 'selected';
3314
            }
3315
            $element->addOption($label, $i, $attributes);
3316
        }
3317
        $form->addElement($element);
3318
    }
3319
3320
    /**
3321
     * @return string
3322
     */
3323
    public static function getJsCode()
3324
    {
3325
        // Filling the scores with the right colors.
3326
        $models = self::getCourseScoreModel();
3327
        $cssListToString = '';
3328
        if (!empty($models)) {
3329
            $cssList = array_column($models['score_list'], 'css_class');
3330
            $cssListToString = implode(' ', $cssList);
3331
        }
3332
3333
        if (empty($cssListToString)) {
3334
            return '';
3335
        }
3336
        $js = <<<EOT
3337
3338
        function updateSelect(element) {
3339
            var spanTag = element.parent().find('span.filter-option');
3340
            var value = element.val();
3341
            var selectId = element.attr('id');
3342
            var optionClass = $('#' + selectId + ' option[value="'+value+'"]').attr('class');
3343
            spanTag.removeClass('$cssListToString');
3344
            spanTag.addClass(optionClass);
3345
        }
3346
3347
        $(function() {
3348
            // Loading values
3349
            $('.exercise_mark_select').on('loaded.bs.select', function() {
3350
                updateSelect($(this));
3351
            });
3352
            // On change
3353
            $('.exercise_mark_select').on('changed.bs.select', function() {
3354
                updateSelect($(this));
3355
            });
3356
        });
3357
EOT;
3358
3359
        return $js;
3360
    }
3361
3362
    /**
3363
     * @param float  $score
3364
     * @param float  $weight
3365
     * @param string $pass_percentage
3366
     *
3367
     * @return string
3368
     */
3369
    public static function showSuccessMessage($score, $weight, $pass_percentage)
3370
    {
3371
        $res = '';
3372
        if (self::isPassPercentageEnabled($pass_percentage)) {
3373
            $isSuccess = self::isSuccessExerciseResult(
3374
                $score,
3375
                $weight,
3376
                $pass_percentage
3377
            );
3378
3379
            if ($isSuccess) {
3380
                $html = get_lang('CongratulationsYouPassedTheTest');
3381
                $icon = Display::return_icon(
3382
                    'completed.png',
3383
                    get_lang('Correct'),
3384
                    [],
3385
                    ICON_SIZE_MEDIUM
3386
                );
3387
            } else {
3388
                $html = get_lang('YouDidNotReachTheMinimumScore');
3389
                $icon = Display::return_icon(
3390
                    'warning.png',
3391
                    get_lang('Wrong'),
3392
                    [],
3393
                    ICON_SIZE_MEDIUM
3394
                );
3395
            }
3396
            $html = Display::tag('h4', $html);
3397
            $html .= Display::tag(
3398
                'h5',
3399
                $icon,
3400
                ['style' => 'width:40px; padding:2px 10px 0px 0px']
3401
            );
3402
            $res = $html;
3403
        }
3404
3405
        return $res;
3406
    }
3407
3408
    /**
3409
     * Return true if pass_pourcentage activated (we use the pass pourcentage feature
3410
     * return false if pass_percentage = 0 (we don't use the pass pourcentage feature.
3411
     *
3412
     * @param $value
3413
     *
3414
     * @return bool
3415
     *              In this version, pass_percentage and show_success_message are disabled if
3416
     *              pass_percentage is set to 0
3417
     */
3418
    public static function isPassPercentageEnabled($value)
3419
    {
3420
        return $value > 0;
3421
    }
3422
3423
    /**
3424
     * Converts a numeric value in a percentage example 0.66666 to 66.67 %.
3425
     *
3426
     * @param $value
3427
     *
3428
     * @return float Converted number
3429
     */
3430
    public static function convert_to_percentage($value)
3431
    {
3432
        $return = '-';
3433
        if ($value != '') {
3434
            $return = float_format($value * 100, 1).' %';
3435
        }
3436
3437
        return $return;
3438
    }
3439
3440
    /**
3441
     * Getting all active exercises from a course from a session
3442
     * (if a session_id is provided we will show all the exercises in the course +
3443
     * all exercises in the session).
3444
     *
3445
     * @param array  $course_info
3446
     * @param int    $session_id
3447
     * @param bool   $check_publication_dates
3448
     * @param string $search                  Search exercise name
3449
     * @param bool   $search_all_sessions     Search exercises in all sessions
3450
     * @param   int 0 = only inactive exercises
3451
     *                  1 = only active exercises,
3452
     *                  2 = all exercises
3453
     *                  3 = active <> -1
3454
     *
3455
     * @return array array with exercise data
3456
     */
3457
    public static function get_all_exercises(
3458
        $course_info = null,
3459
        $session_id = 0,
3460
        $check_publication_dates = false,
3461
        $search = '',
3462
        $search_all_sessions = false,
3463
        $active = 2
3464
    ) {
3465
        $course_id = api_get_course_int_id();
3466
3467
        if (!empty($course_info) && !empty($course_info['real_id'])) {
3468
            $course_id = $course_info['real_id'];
3469
        }
3470
3471
        if ($session_id == -1) {
3472
            $session_id = 0;
3473
        }
3474
3475
        $now = api_get_utc_datetime();
3476
        $timeConditions = '';
3477
        if ($check_publication_dates) {
3478
            // Start and end are set
3479
            $timeConditions = " AND ((start_time <> '' AND start_time < '$now' AND end_time <> '' AND end_time > '$now' )  OR ";
3480
            // only start is set
3481
            $timeConditions .= " (start_time <> '' AND start_time < '$now' AND end_time is NULL) OR ";
3482
            // only end is set
3483
            $timeConditions .= " (start_time IS NULL AND end_time <> '' AND end_time > '$now') OR ";
3484
            // nothing is set
3485
            $timeConditions .= ' (start_time IS NULL AND end_time IS NULL)) ';
3486
        }
3487
3488
        $needle_where = !empty($search) ? " AND title LIKE '?' " : '';
3489
        $needle = !empty($search) ? "%".$search."%" : '';
3490
3491
        // Show courses by active status
3492
        $active_sql = '';
3493
        if ($active == 3) {
3494
            $active_sql = ' active <> -1 AND';
3495
        } else {
3496
            if ($active != 2) {
3497
                $active_sql = sprintf(' active = %d AND', $active);
3498
            }
3499
        }
3500
3501
        if ($search_all_sessions == true) {
3502
            $conditions = [
3503
                'where' => [
3504
                    $active_sql.' c_id = ? '.$needle_where.$timeConditions => [
3505
                        $course_id,
3506
                        $needle,
3507
                    ],
3508
                ],
3509
                'order' => 'title',
3510
            ];
3511
        } else {
3512
            if (empty($session_id)) {
3513
                $conditions = [
3514
                    'where' => [
3515
                        $active_sql.' (session_id = 0 OR session_id IS NULL) AND c_id = ? '.$needle_where.$timeConditions => [
3516
                            $course_id,
3517
                            $needle,
3518
                        ],
3519
                    ],
3520
                    'order' => 'title',
3521
                ];
3522
            } else {
3523
                $conditions = [
3524
                    'where' => [
3525
                        $active_sql.' (session_id = 0 OR session_id IS NULL OR session_id = ? ) AND c_id = ? '.$needle_where.$timeConditions => [
3526
                            $session_id,
3527
                            $course_id,
3528
                            $needle,
3529
                        ],
3530
                    ],
3531
                    'order' => 'title',
3532
                ];
3533
            }
3534
        }
3535
3536
        $table = Database::get_course_table(TABLE_QUIZ_TEST);
3537
3538
        return Database::select('*', $table, $conditions);
3539
    }
3540
3541
    /**
3542
     * Getting all exercises (active only or all)
3543
     * from a course from a session
3544
     * (if a session_id is provided we will show all the exercises in the
3545
     * course + all exercises in the session).
3546
     *
3547
     * @param   array   course data
3548
     * @param   int     session id
3549
     * @param    int        course c_id
3550
     * @param bool $only_active_exercises
3551
     *
3552
     * @return array array with exercise data
3553
     *               modified by Hubert Borderiou
3554
     */
3555
    public static function get_all_exercises_for_course_id(
3556
        $course_info = null,
3557
        $session_id = 0,
3558
        $course_id = 0,
3559
        $only_active_exercises = true
3560
    ) {
3561
        $table = Database::get_course_table(TABLE_QUIZ_TEST);
3562
3563
        if ($only_active_exercises) {
3564
            // Only active exercises.
3565
            $sql_active_exercises = "active = 1 AND ";
3566
        } else {
3567
            // Not only active means visible and invisible NOT deleted (-2)
3568
            $sql_active_exercises = "active IN (1, 0) AND ";
3569
        }
3570
3571
        if ($session_id == -1) {
3572
            $session_id = 0;
3573
        }
3574
3575
        $params = [
3576
            $session_id,
3577
            $course_id,
3578
        ];
3579
3580
        if (empty($session_id)) {
3581
            $conditions = [
3582
                'where' => ["$sql_active_exercises (session_id = 0 OR session_id IS NULL) AND c_id = ?" => [$course_id]],
3583
                'order' => 'title',
3584
            ];
3585
        } else {
3586
            // All exercises
3587
            $conditions = [
3588
                'where' => ["$sql_active_exercises (session_id = 0 OR session_id IS NULL OR session_id = ? ) AND c_id = ?" => $params],
3589
                'order' => 'title',
3590
            ];
3591
        }
3592
3593
        return Database::select('*', $table, $conditions);
3594
    }
3595
3596
    /**
3597
     * Gets the position of the score based in a given score (result/weight)
3598
     * and the exe_id based in the user list
3599
     * (NO Exercises in LPs ).
3600
     *
3601
     * @param float  $my_score      user score to be compared *attention*
3602
     *                              $my_score = score/weight and not just the score
3603
     * @param int    $my_exe_id     exe id of the exercise
3604
     *                              (this is necessary because if 2 students have the same score the one
3605
     *                              with the minor exe_id will have a best position, just to be fair and FIFO)
3606
     * @param int    $exercise_id
3607
     * @param string $course_code
3608
     * @param int    $session_id
3609
     * @param array  $user_list
3610
     * @param bool   $return_string
3611
     *
3612
     * @return int the position of the user between his friends in a course
3613
     *             (or course within a session)
3614
     */
3615
    public static function get_exercise_result_ranking(
3616
        $my_score,
3617
        $my_exe_id,
3618
        $exercise_id,
3619
        $course_code,
3620
        $session_id = 0,
3621
        $user_list = [],
3622
        $return_string = true
3623
    ) {
3624
        //No score given we return
3625
        if (is_null($my_score)) {
3626
            return '-';
3627
        }
3628
        if (empty($user_list)) {
3629
            return '-';
3630
        }
3631
3632
        $best_attempts = [];
3633
        foreach ($user_list as $user_data) {
3634
            $user_id = $user_data['user_id'];
3635
            $best_attempts[$user_id] = self::get_best_attempt_by_user(
3636
                $user_id,
3637
                $exercise_id,
3638
                $course_code,
3639
                $session_id
3640
            );
3641
        }
3642
3643
        if (empty($best_attempts)) {
3644
            return 1;
3645
        } else {
3646
            $position = 1;
3647
            $my_ranking = [];
3648
            foreach ($best_attempts as $user_id => $result) {
3649
                if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
3650
                    $my_ranking[$user_id] = $result['exe_result'] / $result['exe_weighting'];
3651
                } else {
3652
                    $my_ranking[$user_id] = 0;
3653
                }
3654
            }
3655
            //if (!empty($my_ranking)) {
3656
            asort($my_ranking);
3657
            $position = count($my_ranking);
3658
            if (!empty($my_ranking)) {
3659
                foreach ($my_ranking as $user_id => $ranking) {
3660
                    if ($my_score >= $ranking) {
3661
                        if ($my_score == $ranking && isset($best_attempts[$user_id]['exe_id'])) {
3662
                            $exe_id = $best_attempts[$user_id]['exe_id'];
3663
                            if ($my_exe_id < $exe_id) {
3664
                                $position--;
3665
                            }
3666
                        } else {
3667
                            $position--;
3668
                        }
3669
                    }
3670
                }
3671
            }
3672
            //}
3673
            $return_value = [
3674
                'position' => $position,
3675
                'count' => count($my_ranking),
3676
            ];
3677
3678
            if ($return_string) {
3679
                if (!empty($position) && !empty($my_ranking)) {
3680
                    $return_value = $position.'/'.count($my_ranking);
3681
                } else {
3682
                    $return_value = '-';
3683
                }
3684
            }
3685
3686
            return $return_value;
3687
        }
3688
    }
3689
3690
    /**
3691
     * Gets the position of the score based in a given score (result/weight) and the exe_id based in all attempts
3692
     * (NO Exercises in LPs ) old functionality by attempt.
3693
     *
3694
     * @param   float   user score to be compared attention => score/weight
3695
     * @param   int     exe id of the exercise
3696
     * (this is necessary because if 2 students have the same score the one
3697
     * with the minor exe_id will have a best position, just to be fair and FIFO)
3698
     * @param   int     exercise id
3699
     * @param   string  course code
3700
     * @param   int     session id
3701
     * @param bool $return_string
3702
     *
3703
     * @return int the position of the user between his friends in a course (or course within a session)
3704
     */
3705
    public static function get_exercise_result_ranking_by_attempt(
3706
        $my_score,
3707
        $my_exe_id,
3708
        $exercise_id,
3709
        $courseId,
3710
        $session_id = 0,
3711
        $return_string = true
3712
    ) {
3713
        if (empty($session_id)) {
3714
            $session_id = 0;
3715
        }
3716
        if (is_null($my_score)) {
3717
            return '-';
3718
        }
3719
        $user_results = Event::get_all_exercise_results(
3720
            $exercise_id,
3721
            $courseId,
3722
            $session_id,
3723
            false
3724
        );
3725
        $position_data = [];
3726
        if (empty($user_results)) {
3727
            return 1;
3728
        } else {
3729
            $position = 1;
3730
            $my_ranking = [];
3731
            foreach ($user_results as $result) {
3732
                if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
3733
                    $my_ranking[$result['exe_id']] = $result['exe_result'] / $result['exe_weighting'];
3734
                } else {
3735
                    $my_ranking[$result['exe_id']] = 0;
3736
                }
3737
            }
3738
            asort($my_ranking);
3739
            $position = count($my_ranking);
3740
            if (!empty($my_ranking)) {
3741
                foreach ($my_ranking as $exe_id => $ranking) {
3742
                    if ($my_score >= $ranking) {
3743
                        if ($my_score == $ranking) {
3744
                            if ($my_exe_id < $exe_id) {
3745
                                $position--;
3746
                            }
3747
                        } else {
3748
                            $position--;
3749
                        }
3750
                    }
3751
                }
3752
            }
3753
            $return_value = [
3754
                'position' => $position,
3755
                'count' => count($my_ranking),
3756
            ];
3757
3758
            if ($return_string) {
3759
                if (!empty($position) && !empty($my_ranking)) {
3760
                    return $position.'/'.count($my_ranking);
3761
                }
3762
            }
3763
3764
            return $return_value;
3765
        }
3766
    }
3767
3768
    /**
3769
     * Get the best attempt in a exercise (NO Exercises in LPs ).
3770
     *
3771
     * @param int $exercise_id
3772
     * @param int $courseId
3773
     * @param int $session_id
3774
     *
3775
     * @return array
3776
     */
3777
    public static function get_best_attempt_in_course($exercise_id, $courseId, $session_id)
3778
    {
3779
        $user_results = Event::get_all_exercise_results(
3780
            $exercise_id,
3781
            $courseId,
3782
            $session_id,
3783
            false
3784
        );
3785
3786
        $best_score_data = [];
3787
        $best_score = 0;
3788
        if (!empty($user_results)) {
3789
            foreach ($user_results as $result) {
3790
                if (!empty($result['exe_weighting']) &&
3791
                    intval($result['exe_weighting']) != 0
3792
                ) {
3793
                    $score = $result['exe_result'] / $result['exe_weighting'];
3794
                    if ($score >= $best_score) {
3795
                        $best_score = $score;
3796
                        $best_score_data = $result;
3797
                    }
3798
                }
3799
            }
3800
        }
3801
3802
        return $best_score_data;
3803
    }
3804
3805
    /**
3806
     * Get the best score in a exercise (NO Exercises in LPs ).
3807
     *
3808
     * @param int $user_id
3809
     * @param int $exercise_id
3810
     * @param int $courseId
3811
     * @param int $session_id
3812
     *
3813
     * @return array
3814
     */
3815
    public static function get_best_attempt_by_user(
3816
        $user_id,
3817
        $exercise_id,
3818
        $courseId,
3819
        $session_id
3820
    ) {
3821
        $user_results = Event::get_all_exercise_results(
3822
            $exercise_id,
3823
            $courseId,
3824
            $session_id,
3825
            false,
3826
            $user_id
3827
        );
3828
        $best_score_data = [];
3829
        $best_score = 0;
3830
        if (!empty($user_results)) {
3831
            foreach ($user_results as $result) {
3832
                if (!empty($result['exe_weighting']) && (float) $result['exe_weighting'] != 0) {
3833
                    $score = $result['exe_result'] / $result['exe_weighting'];
3834
                    if ($score >= $best_score) {
3835
                        $best_score = $score;
3836
                        $best_score_data = $result;
3837
                    }
3838
                }
3839
            }
3840
        }
3841
3842
        return $best_score_data;
3843
    }
3844
3845
    /**
3846
     * Get average score (NO Exercises in LPs ).
3847
     *
3848
     * @param int $exerciseId
3849
     * @param int $courseId
3850
     * @param int $sessionId
3851
     *
3852
     * @return float Average score
3853
     */
3854
    public static function get_average_score($exerciseId, $courseId, $sessionId, $groupId = 0)
3855
    {
3856
        $user_results = Event::get_all_exercise_results(
3857
            $exerciseId,
3858
            $courseId,
3859
            $sessionId,
3860
            true,
3861
            null,
3862
            $groupId
3863
        );
3864
        $avg_score = 0;
3865
        if (!empty($user_results)) {
3866
            foreach ($user_results as $result) {
3867
                if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
3868
                    $score = $result['exe_result'] / $result['exe_weighting'];
3869
                    $avg_score += $score;
3870
                }
3871
            }
3872
            $avg_score = float_format($avg_score / count($user_results), 1);
3873
        }
3874
3875
        return $avg_score;
3876
    }
3877
3878
    /**
3879
     * Get average quiz score by course (Only exercises not added in a LP).
3880
     *
3881
     * @param int $courseId
3882
     * @param int $sessionId
3883
     *
3884
     * @return float Average score
3885
     */
3886
    public static function get_average_score_by_course($courseId, $sessionId)
3887
    {
3888
        $user_results = Event::get_all_exercise_results_by_course(
3889
            $courseId,
3890
            $sessionId,
3891
            false
3892
        );
3893
        $avg_score = 0;
3894
        if (!empty($user_results)) {
3895
            foreach ($user_results as $result) {
3896
                if (!empty($result['exe_weighting']) && intval(
3897
                        $result['exe_weighting']
3898
                    ) != 0
3899
                ) {
3900
                    $score = $result['exe_result'] / $result['exe_weighting'];
3901
                    $avg_score += $score;
3902
                }
3903
            }
3904
            // We assume that all exe_weighting
3905
            $avg_score = $avg_score / count($user_results);
3906
        }
3907
3908
        return $avg_score;
3909
    }
3910
3911
    /**
3912
     * @param int $user_id
3913
     * @param int $courseId
3914
     * @param int $session_id
3915
     *
3916
     * @return float|int
3917
     */
3918
    public static function get_average_score_by_course_by_user(
3919
        $user_id,
3920
        $courseId,
3921
        $session_id
3922
    ) {
3923
        $user_results = Event::get_all_exercise_results_by_user(
3924
            $user_id,
3925
            $courseId,
3926
            $session_id
3927
        );
3928
        $avg_score = 0;
3929
        if (!empty($user_results)) {
3930
            foreach ($user_results as $result) {
3931
                if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
3932
                    $score = $result['exe_result'] / $result['exe_weighting'];
3933
                    $avg_score += $score;
3934
                }
3935
            }
3936
            // We assume that all exe_weighting
3937
            $avg_score = ($avg_score / count($user_results));
3938
        }
3939
3940
        return $avg_score;
3941
    }
3942
3943
    /**
3944
     * Get average score by score (NO Exercises in LPs ).
3945
     *
3946
     * @param int $exercise_id
3947
     * @param int $courseId
3948
     * @param int $session_id
3949
     * @param int $user_count
3950
     *
3951
     * @return float Best average score
3952
     */
3953
    public static function get_best_average_score_by_exercise(
3954
        $exercise_id,
3955
        $courseId,
3956
        $session_id,
3957
        $user_count
3958
    ) {
3959
        $user_results = Event::get_best_exercise_results_by_user(
3960
            $exercise_id,
3961
            $courseId,
3962
            $session_id
3963
        );
3964
        $avg_score = 0;
3965
        if (!empty($user_results)) {
3966
            foreach ($user_results as $result) {
3967
                if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
3968
                    $score = $result['exe_result'] / $result['exe_weighting'];
3969
                    $avg_score += $score;
3970
                }
3971
            }
3972
            // We asumme that all exe_weighting
3973
            if (!empty($user_count)) {
3974
                $avg_score = float_format($avg_score / $user_count, 1) * 100;
3975
            } else {
3976
                $avg_score = 0;
3977
            }
3978
        }
3979
3980
        return $avg_score;
3981
    }
3982
3983
    /**
3984
     * Get average score by score (NO Exercises in LPs ).
3985
     *
3986
     * @param int $exercise_id
3987
     * @param int $courseId
3988
     * @param int $session_id
3989
     *
3990
     * @return float Best average score
3991
     */
3992
    public static function getBestScoreByExercise(
3993
        $exercise_id,
3994
        $courseId,
3995
        $session_id
3996
    ) {
3997
        $user_results = Event::get_best_exercise_results_by_user(
3998
            $exercise_id,
3999
            $courseId,
4000
            $session_id
4001
        );
4002
        $avg_score = 0;
4003
        if (!empty($user_results)) {
4004
            foreach ($user_results as $result) {
4005
                if (!empty($result['exe_weighting']) && intval($result['exe_weighting']) != 0) {
4006
                    $score = $result['exe_result'] / $result['exe_weighting'];
4007
                    $avg_score += $score;
4008
                }
4009
            }
4010
        }
4011
4012
        return $avg_score;
4013
    }
4014
4015
    /**
4016
     * @param string $course_code
4017
     * @param int    $session_id
4018
     *
4019
     * @return array
4020
     */
4021
    public static function get_exercises_to_be_taken($course_code, $session_id)
4022
    {
4023
        $course_info = api_get_course_info($course_code);
4024
        $exercises = self::get_all_exercises($course_info, $session_id);
4025
        $result = [];
4026
        $now = time() + 15 * 24 * 60 * 60;
4027
        foreach ($exercises as $exercise_item) {
4028
            if (isset($exercise_item['end_time']) &&
4029
                !empty($exercise_item['end_time']) &&
4030
                api_strtotime($exercise_item['end_time'], 'UTC') < $now
4031
            ) {
4032
                $result[] = $exercise_item;
4033
            }
4034
        }
4035
4036
        return $result;
4037
    }
4038
4039
    /**
4040
     * Get student results (only in completed exercises) stats by question.
4041
     *
4042
     * @param int  $question_id
4043
     * @param int  $exercise_id
4044
     * @param int  $courseId
4045
     * @param int  $session_id
4046
     * @param bool $onlyStudent Filter only enrolled students
4047
     *
4048
     * @return array
4049
     */
4050
    public static function get_student_stats_by_question(
4051
        $question_id,
4052
        $exercise_id,
4053
        $courseId,
4054
        $session_id,
4055
        $onlyStudent = false
4056
    ) {
4057
        $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
4058
        $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
4059
        $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
4060
4061
        $question_id = (int) $question_id;
4062
        $exercise_id = (int) $exercise_id;
4063
        $session_id = (int) $session_id;
4064
        $courseId = (int) $courseId;
4065
4066
        $sql = "SELECT MAX(marks) as max, MIN(marks) as min, AVG(marks) as average
4067
                FROM $track_exercises e ";
4068
        if ($onlyStudent) {
4069
            if (empty($session_id)) {
4070
                $courseCondition = "
4071
                    INNER JOIN $courseUser c
4072
                    ON (
4073
                        e.exe_user_id = c.user_id AND
4074
                        e.c_id = c.c_id AND
4075
                        c.status = ".STUDENT."
4076
                        AND relation_type <> 2
4077
                    )";
4078
            } else {
4079
                $sessionRelCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
4080
                $courseCondition = "
4081
                    INNER JOIN $sessionRelCourse sc
4082
                    ON (
4083
                        e.exe_user_id = sc.user_id AND
4084
                        e.c_id = sc.c_id AND
4085
                        e.session_id = sc.session_id AND
4086
                        sc.status = 0
4087
                    ) ";
4088
            }
4089
            $sql .= $courseCondition;
4090
        }
4091
4092
        $sql .= "
4093
            INNER JOIN $track_attempt a
4094
    		ON (
4095
    		    a.exe_id = e.exe_id AND
4096
    		    e.c_id = a.c_id AND
4097
    		    e.session_id  = a.session_id
4098
            )
4099
    		WHERE
4100
    		    exe_exo_id 	= $exercise_id AND
4101
                a.c_id = $courseId AND
4102
                e.session_id = $session_id AND
4103
                question_id = $question_id AND
4104
                e.status = ''
4105
            LIMIT 1";
4106
        $result = Database::query($sql);
4107
        $return = [];
4108
        if ($result) {
4109
            $return = Database::fetch_array($result, 'ASSOC');
4110
        }
4111
4112
        return $return;
4113
    }
4114
4115
    /**
4116
     * Get the correct answer count for a fill blanks question.
4117
     *
4118
     * @param int $question_id
4119
     * @param int $exercise_id
4120
     *
4121
     * @return array
4122
     */
4123
    public static function getNumberStudentsFillBlanksAnswerCount(
4124
        $question_id,
4125
        $exercise_id
4126
    ) {
4127
        $listStudentsId = [];
4128
        $listAllStudentInfo = CourseManager::get_student_list_from_course_code(
4129
            api_get_course_id(),
4130
            true
4131
        );
4132
        foreach ($listAllStudentInfo as $i => $listStudentInfo) {
4133
            $listStudentsId[] = $listStudentInfo['user_id'];
4134
        }
4135
4136
        $listFillTheBlankResult = FillBlanks::getFillTheBlankResult(
4137
            $exercise_id,
4138
            $question_id,
4139
            $listStudentsId,
4140
            '1970-01-01',
4141
            '3000-01-01'
4142
        );
4143
4144
        $arrayCount = [];
4145
4146
        foreach ($listFillTheBlankResult as $resultCount) {
4147
            foreach ($resultCount as $index => $count) {
4148
                //this is only for declare the array index per answer
4149
                $arrayCount[$index] = 0;
4150
            }
4151
        }
4152
4153
        foreach ($listFillTheBlankResult as $resultCount) {
4154
            foreach ($resultCount as $index => $count) {
4155
                $count = ($count === 0) ? 1 : 0;
4156
                $arrayCount[$index] += $count;
4157
            }
4158
        }
4159
4160
        return $arrayCount;
4161
    }
4162
4163
    /**
4164
     * Get the number of questions with answers.
4165
     *
4166
     * @param int    $question_id
4167
     * @param int    $exercise_id
4168
     * @param string $course_code
4169
     * @param int    $session_id
4170
     * @param string $questionType
4171
     *
4172
     * @return int
4173
     */
4174
    public static function get_number_students_question_with_answer_count(
4175
        $question_id,
4176
        $exercise_id,
4177
        $course_code,
4178
        $session_id,
4179
        $questionType = ''
4180
    ) {
4181
        $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
4182
        $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
4183
        $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
4184
        $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
4185
        $courseUserSession = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
4186
4187
        $question_id = intval($question_id);
4188
        $exercise_id = intval($exercise_id);
4189
        $courseId = api_get_course_int_id($course_code);
4190
        $session_id = intval($session_id);
4191
4192
        if ($questionType == FILL_IN_BLANKS) {
4193
            $listStudentsId = [];
4194
            $listAllStudentInfo = CourseManager::get_student_list_from_course_code(
4195
                api_get_course_id(),
4196
                true
4197
            );
4198
            foreach ($listAllStudentInfo as $i => $listStudentInfo) {
4199
                $listStudentsId[] = $listStudentInfo['user_id'];
4200
            }
4201
4202
            $listFillTheBlankResult = FillBlanks::getFillTheBlankResult(
4203
                $exercise_id,
4204
                $question_id,
4205
                $listStudentsId,
4206
                '1970-01-01',
4207
                '3000-01-01'
4208
            );
4209
4210
            return FillBlanks::getNbResultFillBlankAll($listFillTheBlankResult);
4211
        }
4212
4213
        if (empty($session_id)) {
4214
            $courseCondition = "
4215
            INNER JOIN $courseUser cu
4216
            ON cu.c_id = c.id AND cu.user_id  = exe_user_id";
4217
            $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
4218
        } else {
4219
            $courseCondition = "
4220
            INNER JOIN $courseUserSession cu
4221
            ON cu.c_id = c.id AND cu.user_id = exe_user_id";
4222
            $courseConditionWhere = " AND cu.status = 0 ";
4223
        }
4224
4225
        $sql = "SELECT DISTINCT exe_user_id
4226
    		FROM $track_exercises e
4227
    		INNER JOIN $track_attempt a
4228
    		ON (
4229
    		    a.exe_id = e.exe_id AND
4230
    		    e.c_id = a.c_id AND
4231
    		    e.session_id  = a.session_id
4232
            )
4233
            INNER JOIN $courseTable c
4234
            ON (c.id = a.c_id)
4235
    		$courseCondition
4236
    		WHERE
4237
    		    exe_exo_id = $exercise_id AND
4238
                a.c_id = $courseId AND
4239
                e.session_id = $session_id AND
4240
                question_id = $question_id AND
4241
                answer <> '0' AND
4242
                e.status = ''
4243
                $courseConditionWhere
4244
            ";
4245
        $result = Database::query($sql);
4246
        $return = 0;
4247
        if ($result) {
4248
            $return = Database::num_rows($result);
4249
        }
4250
4251
        return $return;
4252
    }
4253
4254
    /**
4255
     * Get number of answers to hotspot questions.
4256
     *
4257
     * @param int    $answer_id
4258
     * @param int    $question_id
4259
     * @param int    $exercise_id
4260
     * @param string $course_code
4261
     * @param int    $session_id
4262
     *
4263
     * @return int
4264
     */
4265
    public static function get_number_students_answer_hotspot_count(
4266
        $answer_id,
4267
        $question_id,
4268
        $exercise_id,
4269
        $course_code,
4270
        $session_id
4271
    ) {
4272
        $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
4273
        $track_hotspot = Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT);
4274
        $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
4275
        $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
4276
        $courseUserSession = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
4277
4278
        $question_id = (int) $question_id;
4279
        $answer_id = (int) $answer_id;
4280
        $exercise_id = (int) $exercise_id;
4281
        $course_code = Database::escape_string($course_code);
4282
        $session_id = (int) $session_id;
4283
4284
        if (empty($session_id)) {
4285
            $courseCondition = "
4286
            INNER JOIN $courseUser cu
4287
            ON cu.c_id = c.id AND cu.user_id  = exe_user_id";
4288
            $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
4289
        } else {
4290
            $courseCondition = "
4291
            INNER JOIN $courseUserSession cu
4292
            ON cu.c_id = c.id AND cu.user_id = exe_user_id";
4293
            $courseConditionWhere = ' AND cu.status = 0 ';
4294
        }
4295
4296
        $sql = "SELECT DISTINCT exe_user_id
4297
    		FROM $track_exercises e
4298
    		INNER JOIN $track_hotspot a
4299
    		ON (a.hotspot_exe_id = e.exe_id)
4300
    		INNER JOIN $courseTable c
4301
    		ON (hotspot_course_code = c.code)
4302
    		$courseCondition
4303
    		WHERE
4304
    		    exe_exo_id              = $exercise_id AND
4305
                a.hotspot_course_code 	= '$course_code' AND
4306
                e.session_id            = $session_id AND
4307
                hotspot_answer_id       = $answer_id AND
4308
                hotspot_question_id     = $question_id AND
4309
                hotspot_correct         =  1 AND
4310
                e.status                = ''
4311
                $courseConditionWhere
4312
            ";
4313
4314
        $result = Database::query($sql);
4315
        $return = 0;
4316
        if ($result) {
4317
            $return = Database::num_rows($result);
4318
        }
4319
4320
        return $return;
4321
    }
4322
4323
    /**
4324
     * @param int    $answer_id
4325
     * @param int    $question_id
4326
     * @param int    $exercise_id
4327
     * @param int    $courseId
4328
     * @param int    $session_id
4329
     * @param string $question_type
4330
     * @param string $correct_answer
4331
     * @param string $current_answer
4332
     *
4333
     * @return int
4334
     */
4335
    public static function get_number_students_answer_count(
4336
        $answer_id,
4337
        $question_id,
4338
        $exercise_id,
4339
        $courseId,
4340
        $session_id,
4341
        $question_type = null,
4342
        $correct_answer = null,
4343
        $current_answer = null
4344
    ) {
4345
        $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
4346
        $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
4347
        $courseTable = Database::get_main_table(TABLE_MAIN_COURSE);
4348
        $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER);
4349
        $courseUserSession = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
4350
4351
        $question_id = (int) $question_id;
4352
        $answer_id = (int) $answer_id;
4353
        $exercise_id = (int) $exercise_id;
4354
        $courseId = (int) $courseId;
4355
        $session_id = (int) $session_id;
4356
4357
        switch ($question_type) {
4358
            case FILL_IN_BLANKS:
4359
                $answer_condition = '';
4360
                $select_condition = ' e.exe_id, answer ';
4361
                break;
4362
            case MATCHING:
4363
            case MATCHING_DRAGGABLE:
4364
            default:
4365
                $answer_condition = " answer = $answer_id AND ";
4366
                $select_condition = ' DISTINCT exe_user_id ';
4367
        }
4368
4369
        if (empty($session_id)) {
4370
            $courseCondition = "
4371
            INNER JOIN $courseUser cu
4372
            ON cu.c_id = c.id AND cu.user_id  = exe_user_id";
4373
            $courseConditionWhere = " AND relation_type <> 2 AND cu.status = ".STUDENT;
4374
        } else {
4375
            $courseCondition = "
4376
            INNER JOIN $courseUserSession cu
4377
            ON cu.c_id = a.c_id AND cu.user_id = exe_user_id";
4378
            $courseConditionWhere = ' AND cu.status = 0 ';
4379
        }
4380
4381
        $sql = "SELECT $select_condition
4382
    		FROM $track_exercises e
4383
    		INNER JOIN $track_attempt a
4384
    		ON (
4385
    		    a.exe_id = e.exe_id AND
4386
    		    e.c_id = a.c_id AND
4387
    		    e.session_id  = a.session_id
4388
            )
4389
            INNER JOIN $courseTable c
4390
            ON c.id = a.c_id
4391
    		$courseCondition
4392
    		WHERE
4393
    		    exe_exo_id = $exercise_id AND
4394
                a.c_id = $courseId AND
4395
                e.session_id = $session_id AND
4396
                $answer_condition
4397
                question_id = $question_id AND
4398
                e.status = ''
4399
                $courseConditionWhere
4400
            ";
4401
        $result = Database::query($sql);
4402
        $return = 0;
4403
        if ($result) {
4404
            $good_answers = 0;
4405
            switch ($question_type) {
4406
                case FILL_IN_BLANKS:
4407
                    while ($row = Database::fetch_array($result, 'ASSOC')) {
4408
                        $fill_blank = self::check_fill_in_blanks(
4409
                            $correct_answer,
4410
                            $row['answer'],
4411
                            $current_answer
4412
                        );
4413
                        if (isset($fill_blank[$current_answer]) && $fill_blank[$current_answer] == 1) {
4414
                            $good_answers++;
4415
                        }
4416
                    }
4417
4418
                    return $good_answers;
4419
                    break;
4420
                case MATCHING:
4421
                case MATCHING_DRAGGABLE:
4422
                default:
4423
                    $return = Database::num_rows($result);
4424
            }
4425
        }
4426
4427
        return $return;
4428
    }
4429
4430
    /**
4431
     * @param array  $answer
4432
     * @param string $user_answer
4433
     *
4434
     * @return array
4435
     */
4436
    public static function check_fill_in_blanks($answer, $user_answer, $current_answer)
4437
    {
4438
        // the question is encoded like this
4439
        // [A] B [C] D [E] F::10,10,10@1
4440
        // number 1 before the "@" means that is a switchable fill in blank question
4441
        // [A] B [C] D [E] F::10,10,10@ or  [A] B [C] D [E] F::10,10,10
4442
        // means that is a normal fill blank question
4443
        // first we explode the "::"
4444
        $pre_array = explode('::', $answer);
4445
        // is switchable fill blank or not
4446
        $last = count($pre_array) - 1;
4447
        $is_set_switchable = explode('@', $pre_array[$last]);
4448
        $switchable_answer_set = false;
4449
        if (isset($is_set_switchable[1]) && $is_set_switchable[1] == 1) {
4450
            $switchable_answer_set = true;
4451
        }
4452
        $answer = '';
4453
        for ($k = 0; $k < $last; $k++) {
4454
            $answer .= $pre_array[$k];
4455
        }
4456
        // splits weightings that are joined with a comma
4457
        $answerWeighting = explode(',', $is_set_switchable[0]);
4458
4459
        // we save the answer because it will be modified
4460
        //$temp = $answer;
4461
        $temp = $answer;
4462
4463
        $answer = '';
4464
        $j = 0;
4465
        //initialise answer tags
4466
        $user_tags = $correct_tags = $real_text = [];
4467
        // the loop will stop at the end of the text
4468
        while (1) {
4469
            // quits the loop if there are no more blanks (detect '[')
4470
            if (($pos = api_strpos($temp, '[')) === false) {
4471
                // adds the end of the text
4472
                $answer = $temp;
4473
                $real_text[] = $answer;
4474
                break; //no more "blanks", quit the loop
4475
            }
4476
            // adds the piece of text that is before the blank
4477
            //and ends with '[' into a general storage array
4478
            $real_text[] = api_substr($temp, 0, $pos + 1);
4479
            $answer .= api_substr($temp, 0, $pos + 1);
4480
            //take the string remaining (after the last "[" we found)
4481
            $temp = api_substr($temp, $pos + 1);
4482
            // quit the loop if there are no more blanks, and update $pos to the position of next ']'
4483
            if (($pos = api_strpos($temp, ']')) === false) {
4484
                // adds the end of the text
4485
                $answer .= $temp;
4486
                break;
4487
            }
4488
4489
            $str = $user_answer;
4490
4491
            preg_match_all('#\[([^[]*)\]#', $str, $arr);
4492
            $str = str_replace('\r\n', '', $str);
4493
            $choices = $arr[1];
4494
            $choice = [];
4495
            $check = false;
4496
            $i = 0;
4497
            foreach ($choices as $item) {
4498
                if ($current_answer === $item) {
4499
                    $check = true;
4500
                }
4501
                if ($check) {
4502
                    $choice[] = $item;
4503
                    $i++;
4504
                }
4505
                if ($i == 3) {
4506
                    break;
4507
                }
4508
            }
4509
            $tmp = api_strrpos($choice[$j], ' / ');
4510
4511
            if ($tmp !== false) {
4512
                $choice[$j] = api_substr($choice[$j], 0, $tmp);
4513
            }
4514
4515
            $choice[$j] = trim($choice[$j]);
4516
4517
            //Needed to let characters ' and " to work as part of an answer
4518
            $choice[$j] = stripslashes($choice[$j]);
4519
4520
            $user_tags[] = api_strtolower($choice[$j]);
4521
            //put the contents of the [] answer tag into correct_tags[]
4522
            $correct_tags[] = api_strtolower(api_substr($temp, 0, $pos));
4523
            $j++;
4524
            $temp = api_substr($temp, $pos + 1);
4525
        }
4526
4527
        $answer = '';
4528
        $real_correct_tags = $correct_tags;
4529
        $chosen_list = [];
4530
        $good_answer = [];
4531
4532
        for ($i = 0; $i < count($real_correct_tags); $i++) {
4533
            if (!$switchable_answer_set) {
4534
                //needed to parse ' and " characters
4535
                $user_tags[$i] = stripslashes($user_tags[$i]);
4536
                if ($correct_tags[$i] == $user_tags[$i]) {
4537
                    $good_answer[$correct_tags[$i]] = 1;
4538
                } elseif (!empty($user_tags[$i])) {
4539
                    $good_answer[$correct_tags[$i]] = 0;
4540
                } else {
4541
                    $good_answer[$correct_tags[$i]] = 0;
4542
                }
4543
            } else {
4544
                // switchable fill in the blanks
4545
                if (in_array($user_tags[$i], $correct_tags)) {
4546
                    $correct_tags = array_diff($correct_tags, $chosen_list);
4547
                    $good_answer[$correct_tags[$i]] = 1;
4548
                } elseif (!empty($user_tags[$i])) {
4549
                    $good_answer[$correct_tags[$i]] = 0;
4550
                } else {
4551
                    $good_answer[$correct_tags[$i]] = 0;
4552
                }
4553
            }
4554
            // adds the correct word, followed by ] to close the blank
4555
            $answer .= ' / <font color="green"><b>'.$real_correct_tags[$i].'</b></font>]';
4556
            if (isset($real_text[$i + 1])) {
4557
                $answer .= $real_text[$i + 1];
4558
            }
4559
        }
4560
4561
        return $good_answer;
4562
    }
4563
4564
    /**
4565
     * @param int    $exercise_id
4566
     * @param string $course_code
4567
     * @param int    $session_id
4568
     *
4569
     * @return int
4570
     */
4571
    public static function get_number_students_finish_exercise(
4572
        $exercise_id,
4573
        $course_code,
4574
        $session_id
4575
    ) {
4576
        $track_exercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
4577
        $track_attempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
4578
4579
        $exercise_id = (int) $exercise_id;
4580
        $course_code = Database::escape_string($course_code);
4581
        $session_id = (int) $session_id;
4582
4583
        $sql = "SELECT DISTINCT exe_user_id
4584
                FROM $track_exercises e
4585
                INNER JOIN $track_attempt a
4586
                ON (a.exe_id = e.exe_id)
4587
                WHERE
4588
                    exe_exo_id 	 = $exercise_id AND
4589
                    course_code  = '$course_code' AND
4590
                    e.session_id = $session_id AND
4591
                    status = ''";
4592
        $result = Database::query($sql);
4593
        $return = 0;
4594
        if ($result) {
4595
            $return = Database::num_rows($result);
4596
        }
4597
4598
        return $return;
4599
    }
4600
4601
    /**
4602
     * Return an HTML select menu with the student groups.
4603
     *
4604
     * @param string $name     is the name and the id of the <select>
4605
     * @param string $default  default value for option
4606
     * @param string $onchange
4607
     *
4608
     * @return string the html code of the <select>
4609
     */
4610
    public static function displayGroupMenu($name, $default, $onchange = "")
4611
    {
4612
        // check the default value of option
4613
        $tabSelected = [$default => " selected='selected' "];
4614
        $res = "";
4615
        $res .= "<select name='$name' id='$name' onchange='".$onchange."' >";
4616
        $res .= "<option value='-1'".$tabSelected["-1"].">-- ".get_lang(
4617
                'AllGroups'
4618
            )." --</option>";
4619
        $res .= "<option value='0'".$tabSelected["0"].">- ".get_lang(
4620
                'NotInAGroup'
4621
            )." -</option>";
4622
        $tabGroups = GroupManager::get_group_list();
4623
        $currentCatId = 0;
4624
        $countGroups = count($tabGroups);
4625
        for ($i = 0; $i < $countGroups; $i++) {
4626
            $tabCategory = GroupManager::get_category_from_group(
4627
                $tabGroups[$i]['iid']
4628
            );
4629
            if ($tabCategory['iid'] != $currentCatId) {
4630
                $res .= "<option value='-1' disabled='disabled'>".$tabCategory['title']."</option>";
4631
                $currentCatId = $tabCategory['iid'];
4632
            }
4633
            $res .= "<option ".$tabSelected[$tabGroups[$i]['iid']]."style='margin-left:40px' value='".
4634
                $tabGroups[$i]['iid']."'>".
4635
                $tabGroups[$i]['name'].
4636
                "</option>";
4637
        }
4638
        $res .= "</select>";
4639
4640
        return $res;
4641
    }
4642
4643
    /**
4644
     * @param int $exe_id
4645
     */
4646
    public static function create_chat_exercise_session($exe_id)
4647
    {
4648
        if (!isset($_SESSION['current_exercises'])) {
4649
            $_SESSION['current_exercises'] = [];
4650
        }
4651
        $_SESSION['current_exercises'][$exe_id] = true;
4652
    }
4653
4654
    /**
4655
     * @param int $exe_id
4656
     */
4657
    public static function delete_chat_exercise_session($exe_id)
4658
    {
4659
        if (isset($_SESSION['current_exercises'])) {
4660
            $_SESSION['current_exercises'][$exe_id] = false;
4661
        }
4662
    }
4663
4664
    /**
4665
     * Display the exercise results.
4666
     *
4667
     * @param Exercise $objExercise
4668
     * @param int      $exeId
4669
     * @param bool     $save_user_result save users results (true) or just show the results (false)
4670
     * @param string   $remainingMessage
4671
     * @param bool     $allowSignature
4672
     * @param bool     $allowExportPdf
4673
     * @param bool     $isExport
4674
     */
4675
    public static function displayQuestionListByAttempt(
4676
        $objExercise,
4677
        $exeId,
4678
        $save_user_result = false,
4679
        $remainingMessage = '',
4680
        $allowSignature = false,
4681
        $allowExportPdf = false,
4682
        $isExport = false
4683
    ) {
4684
        $origin = api_get_origin();
4685
        $courseId = api_get_course_int_id();
4686
        $courseCode = api_get_course_id();
4687
        $sessionId = api_get_session_id();
4688
4689
        // Getting attempt info
4690
        $exercise_stat_info = $objExercise->get_stat_track_exercise_info_by_exe_id($exeId);
4691
4692
        // Getting question list
4693
        $question_list = [];
4694
        $studentInfo = [];
4695
        if (!empty($exercise_stat_info['data_tracking'])) {
4696
            $studentInfo = api_get_user_info($exercise_stat_info['exe_user_id']);
4697
            $question_list = explode(',', $exercise_stat_info['data_tracking']);
4698
        } else {
4699
            // Try getting the question list only if save result is off
4700
            if ($save_user_result == false) {
4701
                $question_list = $objExercise->get_validated_question_list();
4702
            }
4703
            if (in_array(
4704
                $objExercise->getFeedbackType(),
4705
                [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP]
4706
            )) {
4707
                $question_list = $objExercise->get_validated_question_list();
4708
            }
4709
        }
4710
4711
        if ($objExercise->getResultAccess()) {
4712
            if ($objExercise->hasResultsAccess($exercise_stat_info) === false) {
4713
                echo Display::return_message(
4714
                    sprintf(get_lang('YouPassedTheLimitOfXMinutesToSeeTheResults'), $objExercise->getResultsAccess())
4715
                );
4716
4717
                return false;
4718
            }
4719
4720
            if (!empty($objExercise->getResultAccess())) {
4721
                $url = api_get_path(WEB_CODE_PATH).'exercise/overview.php?'.api_get_cidreq().'&exerciseId='.$objExercise->iid;
4722
                echo $objExercise->returnTimeLeftDiv();
4723
                echo $objExercise->showSimpleTimeControl(
4724
                    $objExercise->getResultAccessTimeDiff($exercise_stat_info),
4725
                    $url
4726
                );
4727
            }
4728
        }
4729
4730
        $counter = 1;
4731
        $total_score = $total_weight = 0;
4732
        $exercise_content = null;
4733
        // Hide results
4734
        $show_results = false;
4735
        $show_only_score = false;
4736
        if (in_array($objExercise->results_disabled,
4737
            [
4738
                RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER,
4739
                RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS,
4740
                RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING,
4741
            ]
4742
        )) {
4743
            $show_results = true;
4744
        }
4745
4746
        if (in_array(
4747
            $objExercise->results_disabled,
4748
            [
4749
                RESULT_DISABLE_SHOW_SCORE_ONLY,
4750
                RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES,
4751
                RESULT_DISABLE_RANKING,
4752
            ]
4753
        )
4754
        ) {
4755
            $show_only_score = true;
4756
        }
4757
4758
        // Not display expected answer, but score, and feedback
4759
        $show_all_but_expected_answer = false;
4760
        if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ONLY &&
4761
            $objExercise->getFeedbackType() == EXERCISE_FEEDBACK_TYPE_END
4762
        ) {
4763
            $show_all_but_expected_answer = true;
4764
            $show_results = true;
4765
            $show_only_score = false;
4766
        }
4767
4768
        $showTotalScoreAndUserChoicesInLastAttempt = true;
4769
        $showTotalScore = true;
4770
        $showQuestionScore = true;
4771
        $attemptResult = [];
4772
4773
        if (in_array(
4774
            $objExercise->results_disabled,
4775
            [
4776
                RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT,
4777
                RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK,
4778
                RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK,
4779
            ])
4780
        ) {
4781
            $show_only_score = true;
4782
            $show_results = true;
4783
            $numberAttempts = 0;
4784
            if ($objExercise->attempts > 0) {
4785
                $attempts = Event::getExerciseResultsByUser(
4786
                    api_get_user_id(),
4787
                    $objExercise->iid,
4788
                    $courseId,
4789
                    $sessionId,
4790
                    $exercise_stat_info['orig_lp_id'],
4791
                    $exercise_stat_info['orig_lp_item_id'],
4792
                    'desc'
4793
                );
4794
                if ($attempts) {
4795
                    $numberAttempts = count($attempts);
4796
                }
4797
4798
                if ($save_user_result) {
4799
                    $numberAttempts++;
4800
                }
4801
4802
                $showTotalScore = false;
4803
                if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT) {
4804
                    $showTotalScore = true;
4805
                }
4806
                $showTotalScoreAndUserChoicesInLastAttempt = false;
4807
                if ($numberAttempts >= $objExercise->attempts) {
4808
                    $showTotalScore = true;
4809
                    $show_results = true;
4810
                    $show_only_score = false;
4811
                    $showTotalScoreAndUserChoicesInLastAttempt = true;
4812
                }
4813
4814
                if ($objExercise->results_disabled == RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT_NO_FEEDBACK) {
4815
                    $showTotalScore = true;
4816
                    $show_results = true;
4817
                    $show_only_score = false;
4818
                    $showTotalScoreAndUserChoicesInLastAttempt = false;
4819
                    if ($numberAttempts >= $objExercise->attempts) {
4820
                        $showTotalScoreAndUserChoicesInLastAttempt = true;
4821
                    }
4822
4823
                    // Check if the current attempt is the last.
4824
                    /*if (false === $save_user_result && !empty($attempts)) {
4825
                        $showTotalScoreAndUserChoicesInLastAttempt = false;
4826
                        $position = 1;
4827
                        foreach ($attempts as $attempt) {
4828
                            if ($exeId == $attempt['exe_id']) {
4829
                                break;
4830
                            }
4831
                            $position++;
4832
                        }
4833
4834
                        if ($position == $objExercise->attempts) {
4835
                            $showTotalScoreAndUserChoicesInLastAttempt = true;
4836
                        }
4837
                    }*/
4838
                }
4839
            }
4840
4841
            if ($objExercise->results_disabled ==
4842
                RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK
4843
            ) {
4844
                $show_only_score = false;
4845
                $show_results = true;
4846
                $show_all_but_expected_answer = false;
4847
                $showTotalScore = false;
4848
                $showQuestionScore = false;
4849
                if ($numberAttempts >= $objExercise->attempts) {
4850
                    $showTotalScore = true;
4851
                    $showQuestionScore = true;
4852
                }
4853
            }
4854
        }
4855
4856
        // When exporting to PDF hide feedback/comment/score show warning in hotspot.
4857
        if ($allowExportPdf && $isExport) {
4858
            $showTotalScore = false;
4859
            $showQuestionScore = false;
4860
            $objExercise->feedback_type = 2;
4861
            $objExercise->hideComment = true;
4862
            $objExercise->hideNoAnswer = true;
4863
            $objExercise->results_disabled = 0;
4864
            $objExercise->hideExpectedAnswer = true;
4865
            $show_results = true;
4866
        }
4867
4868
        if ('embeddable' !== $origin &&
4869
            !empty($exercise_stat_info['exe_user_id']) &&
4870
            !empty($studentInfo)
4871
        ) {
4872
            // Shows exercise header.
4873
            echo $objExercise->showExerciseResultHeader(
4874
                $studentInfo,
4875
                $exercise_stat_info,
4876
                $save_user_result,
4877
                $allowSignature,
4878
                $allowExportPdf
4879
            );
4880
        }
4881
4882
        // Display text when test is finished #4074 and for LP #4227
4883
        $endOfMessage = Security::remove_XSS($objExercise->getTextWhenFinished());
4884
        if (!empty($endOfMessage)) {
4885
            echo Display::div(
4886
                $endOfMessage,
4887
                ['id' => 'quiz_end_message']
4888
            );
4889
        }
4890
4891
        $question_list_answers = [];
4892
        $category_list = [];
4893
        $loadChoiceFromSession = false;
4894
        $fromDatabase = true;
4895
        $exerciseResult = null;
4896
        $exerciseResultCoordinates = null;
4897
        $delineationResults = null;
4898
        if (true === $save_user_result && in_array(
4899
            $objExercise->getFeedbackType(),
4900
            [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP]
4901
        )) {
4902
            $loadChoiceFromSession = true;
4903
            $fromDatabase = false;
4904
            $exerciseResult = Session::read('exerciseResult');
4905
            $exerciseResultCoordinates = Session::read('exerciseResultCoordinates');
4906
            $delineationResults = Session::read('hotspot_delineation_result');
4907
            $delineationResults = isset($delineationResults[$objExercise->iid]) ? $delineationResults[$objExercise->iid] : null;
4908
        }
4909
4910
        $countPendingQuestions = 0;
4911
        $result = [];
4912
        // Loop over all question to show results for each of them, one by one
4913
        if (!empty($question_list)) {
4914
            foreach ($question_list as $questionId) {
4915
                // Creates a temporary Question object
4916
                $objQuestionTmp = Question::read($questionId, $objExercise->course);
4917
                // This variable came from exercise_submit_modal.php
4918
                ob_start();
4919
                $choice = null;
4920
                $delineationChoice = null;
4921
                if ($loadChoiceFromSession) {
4922
                    $choice = isset($exerciseResult[$questionId]) ? $exerciseResult[$questionId] : null;
4923
                    $delineationChoice = isset($delineationResults[$questionId]) ? $delineationResults[$questionId] : null;
4924
                }
4925
4926
                // We're inside *one* question. Go through each possible answer for this question
4927
                $result = $objExercise->manage_answer(
4928
                    $exeId,
4929
                    $questionId,
4930
                    $choice,
4931
                    'exercise_result',
4932
                    $exerciseResultCoordinates,
4933
                    $save_user_result,
4934
                    $fromDatabase,
4935
                    $show_results,
4936
                    $objExercise->selectPropagateNeg(),
4937
                    $delineationChoice,
4938
                    $showTotalScoreAndUserChoicesInLastAttempt
4939
                );
4940
4941
                if (empty($result)) {
4942
                    continue;
4943
                }
4944
4945
                $total_score += $result['score'];
4946
                $total_weight += $result['weight'];
4947
4948
                $question_list_answers[] = [
4949
                    'question' => $result['open_question'],
4950
                    'answer' => $result['open_answer'],
4951
                    'answer_type' => $result['answer_type'],
4952
                    'generated_oral_file' => $result['generated_oral_file'],
4953
                ];
4954
4955
                $my_total_score = $result['score'];
4956
                $my_total_weight = $result['weight'];
4957
                $scorePassed = self::scorePassed($my_total_score, $my_total_weight);
4958
4959
                // Category report
4960
                $category_was_added_for_this_test = false;
4961
                if (isset($objQuestionTmp->category) && !empty($objQuestionTmp->category)) {
4962
                    if (!isset($category_list[$objQuestionTmp->category]['score'])) {
4963
                        $category_list[$objQuestionTmp->category]['score'] = 0;
4964
                    }
4965
                    if (!isset($category_list[$objQuestionTmp->category]['total'])) {
4966
                        $category_list[$objQuestionTmp->category]['total'] = 0;
4967
                    }
4968
                    if (!isset($category_list[$objQuestionTmp->category]['total_questions'])) {
4969
                        $category_list[$objQuestionTmp->category]['total_questions'] = 0;
4970
                    }
4971
                    if (!isset($category_list[$objQuestionTmp->category]['passed'])) {
4972
                        $category_list[$objQuestionTmp->category]['passed'] = 0;
4973
                    }
4974
                    if (!isset($category_list[$objQuestionTmp->category]['wrong'])) {
4975
                        $category_list[$objQuestionTmp->category]['wrong'] = 0;
4976
                    }
4977
                    if (!isset($category_list[$objQuestionTmp->category]['no_answer'])) {
4978
                        $category_list[$objQuestionTmp->category]['no_answer'] = 0;
4979
                    }
4980
4981
                    $category_list[$objQuestionTmp->category]['score'] += $my_total_score;
4982
                    $category_list[$objQuestionTmp->category]['total'] += $my_total_weight;
4983
                    if ($scorePassed) {
4984
                        // Only count passed if score is not empty
4985
                        if (!empty($my_total_score)) {
4986
                            $category_list[$objQuestionTmp->category]['passed']++;
4987
                        }
4988
                    } else {
4989
                        if ($result['user_answered']) {
4990
                            $category_list[$objQuestionTmp->category]['wrong']++;
4991
                        } else {
4992
                            $category_list[$objQuestionTmp->category]['no_answer']++;
4993
                        }
4994
                    }
4995
4996
                    $category_list[$objQuestionTmp->category]['total_questions']++;
4997
                    $category_was_added_for_this_test = true;
4998
                }
4999
                if (isset($objQuestionTmp->category_list) && !empty($objQuestionTmp->category_list)) {
5000
                    foreach ($objQuestionTmp->category_list as $category_id) {
5001
                        $category_list[$category_id]['score'] += $my_total_score;
5002
                        $category_list[$category_id]['total'] += $my_total_weight;
5003
                        $category_was_added_for_this_test = true;
5004
                    }
5005
                }
5006
5007
                // No category for this question!
5008
                if ($category_was_added_for_this_test == false) {
5009
                    if (!isset($category_list['none']['score'])) {
5010
                        $category_list['none']['score'] = 0;
5011
                    }
5012
                    if (!isset($category_list['none']['total'])) {
5013
                        $category_list['none']['total'] = 0;
5014
                    }
5015
5016
                    $category_list['none']['score'] += $my_total_score;
5017
                    $category_list['none']['total'] += $my_total_weight;
5018
                }
5019
5020
                if ($objExercise->selectPropagateNeg() == 0 && $my_total_score < 0) {
5021
                    $my_total_score = 0;
5022
                }
5023
5024
                $comnt = null;
5025
                if ($show_results) {
5026
                    $comnt = Event::get_comments($exeId, $questionId);
5027
                    $teacherAudio = self::getOralFeedbackAudio(
5028
                        $exeId,
5029
                        $questionId,
5030
                        api_get_user_id()
5031
                    );
5032
5033
                    if (!empty($comnt) || $teacherAudio) {
5034
                        echo '<b>'.get_lang('Feedback').'</b>';
5035
                    }
5036
5037
                    if (!empty($comnt)) {
5038
                        echo self::getFeedbackText($comnt);
5039
                    }
5040
5041
                    if ($teacherAudio) {
5042
                        echo $teacherAudio;
5043
                    }
5044
                }
5045
5046
                $calculatedScore = [
5047
                    'result' => self::show_score(
5048
                        $my_total_score,
5049
                        $my_total_weight,
5050
                        false
5051
                    ),
5052
                    'pass' => $scorePassed,
5053
                    'score' => $my_total_score,
5054
                    'weight' => $my_total_weight,
5055
                    'comments' => $comnt,
5056
                    'user_answered' => $result['user_answered'],
5057
                ];
5058
5059
                $score = [];
5060
                if ($show_results) {
5061
                    $score = $calculatedScore;
5062
                }
5063
                if (in_array($objQuestionTmp->type, [FREE_ANSWER, ORAL_EXPRESSION, ANNOTATION, UPLOAD_ANSWER])) {
5064
                    $reviewScore = [
5065
                        'score' => $my_total_score,
5066
                        'comments' => Event::get_comments($exeId, $questionId),
5067
                    ];
5068
                    $check = $objQuestionTmp->isQuestionWaitingReview($reviewScore);
5069
                    if (false === $check) {
5070
                        $countPendingQuestions++;
5071
                    }
5072
                }
5073
5074
                $contents = ob_get_clean();
5075
5076
                // Hide correct answers.
5077
                if ($scorePassed && false === $objExercise->disableHideCorrectAnsweredQuestions) {
5078
                    // Skip correct answers.
5079
                    $hide = (int) $objExercise->getPageConfigurationAttribute('hide_correct_answered_questions');
5080
                    if (1 === $hide) {
5081
                        continue;
5082
                    }
5083
                }
5084
5085
                $question_content = '';
5086
                if ($show_results) {
5087
                    $question_content = '<div class="question_row_answer">';
5088
                    if (false === $showQuestionScore) {
5089
                        $score = [];
5090
                    }
5091
5092
                    // Shows question title an description
5093
                    $question_content .= $objQuestionTmp->return_header(
5094
                        $objExercise,
5095
                        $counter,
5096
                        $score
5097
                    );
5098
                }
5099
                $counter++;
5100
                $question_content .= $contents;
5101
                if ($show_results) {
5102
                    $question_content .= '</div>';
5103
                }
5104
5105
                $calculatedScore['question_content'] = $question_content;
5106
                $attemptResult[] = $calculatedScore;
5107
5108
                if ($objExercise->showExpectedChoice()) {
5109
                    $exercise_content .= Display::div(
5110
                        Display::panel($question_content),
5111
                        ['class' => 'question-panel']
5112
                    );
5113
                } else {
5114
                    // $show_all_but_expected_answer should not happen at
5115
                    // the same time as $show_results
5116
                    if ($show_results && !$show_only_score) {
5117
                        $exercise_content .= Display::div(
5118
                            Display::panel($question_content),
5119
                            ['class' => 'question-panel']
5120
                        );
5121
                    }
5122
                }
5123
            }
5124
        }
5125
5126
        $totalScoreText = null;
5127
        $certificateBlock = '';
5128
        if (($show_results || $show_only_score) && $showTotalScore) {
5129
            if ($result['answer_type'] == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
5130
                echo '<h1 style="text-align : center; margin : 20px 0;">'.get_lang('YourResults').'</h1><br />';
5131
            }
5132
            $totalScoreText .= '<div class="question_row_score">';
5133
            if ($result['answer_type'] == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
5134
                $totalScoreText .= self::getQuestionDiagnosisRibbon(
5135
                    $objExercise,
5136
                    $total_score,
5137
                    $total_weight,
5138
                    true
5139
                );
5140
            } else {
5141
                $pluginEvaluation = QuestionOptionsEvaluationPlugin::create();
5142
                if ('true' === $pluginEvaluation->get(QuestionOptionsEvaluationPlugin::SETTING_ENABLE)) {
5143
                    $formula = $pluginEvaluation->getFormulaForExercise($objExercise->selectId());
5144
5145
                    if (!empty($formula)) {
5146
                        $total_score = $pluginEvaluation->getResultWithFormula($exeId, $formula);
5147
                        $total_weight = $pluginEvaluation->getMaxScore();
5148
                    }
5149
                }
5150
5151
                $totalScoreText .= self::getTotalScoreRibbon(
5152
                    $objExercise,
5153
                    $total_score,
5154
                    $total_weight,
5155
                    true,
5156
                    $countPendingQuestions
5157
                );
5158
            }
5159
            $totalScoreText .= '</div>';
5160
5161
            if (!empty($studentInfo)) {
5162
                $certificateBlock = self::generateAndShowCertificateBlock(
5163
                    $total_score,
5164
                    $total_weight,
5165
                    $objExercise,
5166
                    $studentInfo['id'],
5167
                    $courseCode,
5168
                    $sessionId
5169
                );
5170
            }
5171
        }
5172
5173
        if ($result['answer_type'] == MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY) {
5174
            $chartMultiAnswer = MultipleAnswerTrueFalseDegreeCertainty::displayStudentsChartResults(
5175
                $exeId,
5176
                $objExercise
5177
            );
5178
            echo $chartMultiAnswer;
5179
        }
5180
5181
        if (!empty($category_list) &&
5182
            ($show_results || $show_only_score || RESULT_DISABLE_RADAR == $objExercise->results_disabled)
5183
        ) {
5184
            // Adding total
5185
            $category_list['total'] = [
5186
                'score' => $total_score,
5187
                'total' => $total_weight,
5188
            ];
5189
            echo TestCategory::get_stats_table_by_attempt($objExercise, $category_list);
5190
        }
5191
5192
        if ($show_all_but_expected_answer) {
5193
            $exercise_content .= Display::return_message(get_lang('ExerciseWithFeedbackWithoutCorrectionComment'));
5194
        }
5195
5196
        // Remove audio auto play from questions on results page - refs BT#7939
5197
        $exercise_content = preg_replace(
5198
            ['/autoplay[\=\".+\"]+/', '/autostart[\=\".+\"]+/'],
5199
            '',
5200
            $exercise_content
5201
        );
5202
5203
        echo $totalScoreText;
5204
        echo $certificateBlock;
5205
5206
        // Ofaj change BT#11784
5207
        if (api_get_configuration_value('quiz_show_description_on_results_page') &&
5208
            !empty($objExercise->description)
5209
        ) {
5210
            echo Display::div(Security::remove_XSS($objExercise->description), ['class' => 'exercise_description']);
5211
        }
5212
5213
        echo $exercise_content;
5214
        if (!$show_only_score) {
5215
            echo $totalScoreText;
5216
        }
5217
5218
        if ($save_user_result) {
5219
            // Tracking of results
5220
            if ($exercise_stat_info) {
5221
                $learnpath_id = $exercise_stat_info['orig_lp_id'];
5222
                $learnpath_item_id = $exercise_stat_info['orig_lp_item_id'];
5223
                $learnpath_item_view_id = $exercise_stat_info['orig_lp_item_view_id'];
5224
5225
                if (api_is_allowed_to_session_edit()) {
5226
                    Event::updateEventExercise(
5227
                        $exercise_stat_info['exe_id'],
5228
                        $objExercise->selectId(),
5229
                        $total_score,
5230
                        $total_weight,
5231
                        $sessionId,
5232
                        $learnpath_id,
5233
                        $learnpath_item_id,
5234
                        $learnpath_item_view_id,
5235
                        $exercise_stat_info['exe_duration'],
5236
                        $question_list
5237
                    );
5238
5239
                    $allowStats = api_get_configuration_value('allow_gradebook_stats');
5240
                    if ($allowStats) {
5241
                        $objExercise->generateStats(
5242
                            $objExercise->selectId(),
5243
                            api_get_course_info(),
5244
                            $sessionId
5245
                        );
5246
                    }
5247
                }
5248
            }
5249
5250
            // Send notification at the end
5251
            if (!api_is_allowed_to_edit(null, true) &&
5252
                !api_is_excluded_user_type()
5253
            ) {
5254
                $objExercise->send_mail_notification_for_exam(
5255
                    'end',
5256
                    $question_list_answers,
5257
                    $origin,
5258
                    $exeId,
5259
                    $total_score,
5260
                    $total_weight
5261
                );
5262
            }
5263
        }
5264
5265
        if (in_array(
5266
            $objExercise->selectResultsDisabled(),
5267
            [RESULT_DISABLE_RANKING, RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING]
5268
        )) {
5269
            echo Display::page_header(get_lang('Ranking'), null, 'h4');
5270
            echo self::displayResultsInRanking(
5271
                $objExercise,
5272
                api_get_user_id(),
5273
                $courseId,
5274
                $sessionId
5275
            );
5276
        }
5277
5278
        if (!empty($remainingMessage)) {
5279
            echo Display::return_message($remainingMessage, 'normal', false);
5280
        }
5281
5282
        $failedAnswersCount = 0;
5283
        $wrongQuestionHtml = '';
5284
        $all = '';
5285
        foreach ($attemptResult as $item) {
5286
            if (false === $item['pass']) {
5287
                $failedAnswersCount++;
5288
                $wrongQuestionHtml .= $item['question_content'].'<br />';
5289
            }
5290
            $all .= $item['question_content'].'<br />';
5291
        }
5292
5293
        $passed = self::isPassPercentageAttemptPassed(
5294
            $objExercise,
5295
            $total_score,
5296
            $total_weight
5297
        );
5298
5299
        $percentage = 0;
5300
        if (!empty($total_weight)) {
5301
            $percentage = ($total_score / $total_weight) * 100;
5302
        }
5303
5304
        return [
5305
            'category_list' => $category_list,
5306
            'attempts_result_list' => $attemptResult, // array of results
5307
            'exercise_passed' => $passed, // boolean
5308
            'total_answers_count' => count($attemptResult), // int
5309
            'failed_answers_count' => $failedAnswersCount, // int
5310
            'failed_answers_html' => $wrongQuestionHtml,
5311
            'all_answers_html' => $all,
5312
            'total_score' => $total_score,
5313
            'total_weight' => $total_weight,
5314
            'total_percentage' => $percentage,
5315
            'count_pending_questions' => $countPendingQuestions,
5316
        ];
5317
    }
5318
5319
    /**
5320
     * Display the ranking of results in a exercise.
5321
     *
5322
     * @param Exercise $exercise
5323
     * @param int      $currentUserId
5324
     * @param int      $courseId
5325
     * @param int      $sessionId
5326
     *
5327
     * @return string
5328
     */
5329
    public static function displayResultsInRanking($exercise, $currentUserId, $courseId, $sessionId = 0)
5330
    {
5331
        $exerciseId = $exercise->iid;
5332
        $data = self::exerciseResultsInRanking($exerciseId, $courseId, $sessionId);
5333
5334
        $table = new HTML_Table(['class' => 'table table-hover table-striped table-bordered']);
5335
        $table->setHeaderContents(0, 0, get_lang('Position'), ['class' => 'text-right']);
5336
        $table->setHeaderContents(0, 1, get_lang('Username'));
5337
        $table->setHeaderContents(0, 2, get_lang('Score'), ['class' => 'text-right']);
5338
        $table->setHeaderContents(0, 3, get_lang('Date'), ['class' => 'text-center']);
5339
5340
        foreach ($data as $r => $item) {
5341
            if (!isset($item[1])) {
5342
                continue;
5343
            }
5344
            $selected = $item[1]->getId() == $currentUserId;
5345
5346
            foreach ($item as $c => $value) {
5347
                $table->setCellContents($r + 1, $c, $value);
5348
5349
                $attrClass = '';
5350
5351
                if (in_array($c, [0, 2])) {
5352
                    $attrClass = 'text-right';
5353
                } elseif (3 == $c) {
5354
                    $attrClass = 'text-center';
5355
                }
5356
5357
                if ($selected) {
5358
                    $attrClass .= ' warning';
5359
                }
5360
5361
                $table->setCellAttributes($r + 1, $c, ['class' => $attrClass]);
5362
            }
5363
        }
5364
5365
        return $table->toHtml();
5366
    }
5367
5368
    /**
5369
     * Get the ranking for results in a exercise.
5370
     * Function used internally by ExerciseLib::displayResultsInRanking.
5371
     *
5372
     * @param int $exerciseId
5373
     * @param int $courseId
5374
     * @param int $sessionId
5375
     *
5376
     * @return array
5377
     */
5378
    public static function exerciseResultsInRanking($exerciseId, $courseId, $sessionId = 0)
5379
    {
5380
        $em = Database::getManager();
5381
5382
        $dql = 'SELECT DISTINCT te.exeUserId FROM ChamiloCoreBundle:TrackEExercises te WHERE te.exeExoId = :id AND te.cId = :cId';
5383
        $dql .= api_get_session_condition($sessionId, true, false, 'te.sessionId');
5384
5385
        $result = $em
5386
            ->createQuery($dql)
5387
            ->setParameters(['id' => $exerciseId, 'cId' => $courseId])
5388
            ->getScalarResult();
5389
5390
        $data = [];
5391
        /** @var TrackEExercises $item */
5392
        foreach ($result as $item) {
5393
            $data[] = self::get_best_attempt_by_user($item['exeUserId'], $exerciseId, $courseId, $sessionId);
5394
        }
5395
5396
        usort(
5397
            $data,
5398
            function ($a, $b) {
5399
                if ($a['exe_result'] != $b['exe_result']) {
5400
                    return $a['exe_result'] > $b['exe_result'] ? -1 : 1;
5401
                }
5402
5403
                if ($a['exe_date'] != $b['exe_date']) {
5404
                    return $a['exe_date'] < $b['exe_date'] ? -1 : 1;
5405
                }
5406
5407
                return 0;
5408
            }
5409
        );
5410
5411
        // flags to display the same position in case of tie
5412
        $lastScore = $data[0]['exe_result'];
5413
        $position = 1;
5414
        $data = array_map(
5415
            function ($item) use (&$lastScore, &$position) {
5416
                if ($item['exe_result'] < $lastScore) {
5417
                    $position++;
5418
                }
5419
5420
                $lastScore = $item['exe_result'];
5421
5422
                return [
5423
                    $position,
5424
                    api_get_user_entity($item['exe_user_id']),
5425
                    self::show_score($item['exe_result'], $item['exe_weighting'], true, true, true),
5426
                    api_convert_and_format_date($item['exe_date'], DATE_TIME_FORMAT_SHORT),
5427
                ];
5428
            },
5429
            $data
5430
        );
5431
5432
        return $data;
5433
    }
5434
5435
    /**
5436
     * Get a special ribbon on top of "degree of certainty" questions (
5437
     * variation from getTotalScoreRibbon() for other question types).
5438
     *
5439
     * @param Exercise $objExercise
5440
     * @param float    $score
5441
     * @param float    $weight
5442
     * @param bool     $checkPassPercentage
5443
     *
5444
     * @return string
5445
     */
5446
    public static function getQuestionDiagnosisRibbon($objExercise, $score, $weight, $checkPassPercentage = false)
5447
    {
5448
        $displayChartDegree = true;
5449
        $ribbon = $displayChartDegree ? '<div class="ribbon">' : '';
5450
5451
        if ($checkPassPercentage) {
5452
            $passPercentage = $objExercise->selectPassPercentage();
5453
            $isSuccess = self::isSuccessExerciseResult($score, $weight, $passPercentage);
5454
            // Color the final test score if pass_percentage activated
5455
            $ribbonTotalSuccessOrError = '';
5456
            if (self::isPassPercentageEnabled($passPercentage)) {
5457
                if ($isSuccess) {
5458
                    $ribbonTotalSuccessOrError = ' ribbon-total-success';
5459
                } else {
5460
                    $ribbonTotalSuccessOrError = ' ribbon-total-error';
5461
                }
5462
            }
5463
            $ribbon .= $displayChartDegree ? '<div class="rib rib-total '.$ribbonTotalSuccessOrError.'">' : '';
5464
        } else {
5465
            $ribbon .= $displayChartDegree ? '<div class="rib rib-total">' : '';
5466
        }
5467
5468
        if ($displayChartDegree) {
5469
            $ribbon .= '<h3>'.get_lang('YourTotalScore').':&nbsp;';
5470
            $ribbon .= self::show_score($score, $weight, false, true);
5471
            $ribbon .= '</h3>';
5472
            $ribbon .= '</div>';
5473
        }
5474
5475
        if ($checkPassPercentage) {
5476
            $ribbon .= self::showSuccessMessage(
5477
                $score,
5478
                $weight,
5479
                $objExercise->selectPassPercentage()
5480
            );
5481
        }
5482
5483
        $ribbon .= $displayChartDegree ? '</div>' : '';
5484
5485
        return $ribbon;
5486
    }
5487
5488
    public static function isPassPercentageAttemptPassed($objExercise, $score, $weight)
5489
    {
5490
        $passPercentage = $objExercise->selectPassPercentage();
5491
5492
        return self::isSuccessExerciseResult($score, $weight, $passPercentage);
5493
    }
5494
5495
    /**
5496
     * @param float $score
5497
     * @param float $weight
5498
     * @param bool  $checkPassPercentage
5499
     * @param int   $countPendingQuestions
5500
     *
5501
     * @return string
5502
     */
5503
    public static function getTotalScoreRibbon(
5504
        Exercise $objExercise,
5505
        $score,
5506
        $weight,
5507
        $checkPassPercentage = false,
5508
        $countPendingQuestions = 0
5509
    ) {
5510
        $hide = (int) $objExercise->getPageConfigurationAttribute('hide_total_score');
5511
        if (1 === $hide) {
5512
            return '';
5513
        }
5514
5515
        $passPercentage = $objExercise->selectPassPercentage();
5516
        $ribbon = '<div class="title-score">';
5517
        if ($checkPassPercentage) {
5518
            $isSuccess = self::isSuccessExerciseResult(
5519
                $score,
5520
                $weight,
5521
                $passPercentage
5522
            );
5523
            // Color the final test score if pass_percentage activated
5524
            $class = '';
5525
            if (self::isPassPercentageEnabled($passPercentage)) {
5526
                if ($isSuccess) {
5527
                    $class = ' ribbon-total-success';
5528
                } else {
5529
                    $class = ' ribbon-total-error';
5530
                }
5531
            }
5532
            $ribbon .= '<div class="total '.$class.'">';
5533
        } else {
5534
            $ribbon .= '<div class="total">';
5535
        }
5536
        $ribbon .= '<h3>'.get_lang('YourTotalScore').':&nbsp;';
5537
        $ribbon .= self::show_score($score, $weight, false, true);
5538
        $ribbon .= '</h3>';
5539
        $ribbon .= '</div>';
5540
        if ($checkPassPercentage) {
5541
            $ribbon .= self::showSuccessMessage(
5542
                $score,
5543
                $weight,
5544
                $passPercentage
5545
            );
5546
        }
5547
        $ribbon .= '</div>';
5548
5549
        if (!empty($countPendingQuestions)) {
5550
            $ribbon .= '<br />';
5551
            $ribbon .= Display::return_message(
5552
                sprintf(
5553
                    get_lang('TempScoreXQuestionsNotCorrectedYet'),
5554
                    $countPendingQuestions
5555
                ),
5556
                'warning'
5557
            );
5558
        }
5559
5560
        return $ribbon;
5561
    }
5562
5563
    /**
5564
     * @param int $countLetter
5565
     *
5566
     * @return mixed
5567
     */
5568
    public static function detectInputAppropriateClass($countLetter)
5569
    {
5570
        $limits = [
5571
            0 => 'input-mini',
5572
            10 => 'input-mini',
5573
            15 => 'input-medium',
5574
            20 => 'input-xlarge',
5575
            40 => 'input-xlarge',
5576
            60 => 'input-xxlarge',
5577
            100 => 'input-xxlarge',
5578
            200 => 'input-xxlarge',
5579
        ];
5580
5581
        foreach ($limits as $size => $item) {
5582
            if ($countLetter <= $size) {
5583
                return $item;
5584
            }
5585
        }
5586
5587
        return $limits[0];
5588
    }
5589
5590
    /**
5591
     * @param int    $senderId
5592
     * @param array  $course_info
5593
     * @param string $test
5594
     * @param string $url
5595
     *
5596
     * @return string
5597
     */
5598
    public static function getEmailNotification($senderId, $course_info, $test, $url)
5599
    {
5600
        $teacher_info = api_get_user_info($senderId);
5601
        $from_name = api_get_person_name(
5602
            $teacher_info['firstname'],
5603
            $teacher_info['lastname'],
5604
            null,
5605
            PERSON_NAME_EMAIL_ADDRESS
5606
        );
5607
5608
        $view = new Template('', false, false, false, false, false, false);
5609
        $view->assign('course_title', Security::remove_XSS($course_info['name']));
5610
        $view->assign('test_title', Security::remove_XSS($test));
5611
        $view->assign('url', $url);
5612
        $view->assign('teacher_name', $from_name);
5613
        $template = $view->get_template('mail/exercise_result_alert_body.tpl');
5614
5615
        return $view->fetch($template);
5616
    }
5617
5618
    /**
5619
     * @return string
5620
     */
5621
    public static function getNotCorrectedYetText()
5622
    {
5623
        return Display::return_message(get_lang('notCorrectedYet'), 'warning');
5624
    }
5625
5626
    /**
5627
     * @param string $message
5628
     *
5629
     * @return string
5630
     */
5631
    public static function getFeedbackText($message)
5632
    {
5633
        return Display::return_message($message, 'warning', false);
5634
    }
5635
5636
    /**
5637
     * Get the recorder audio component for save a teacher audio feedback.
5638
     *
5639
     * @param Template $template
5640
     * @param int      $attemptId
5641
     * @param int      $questionId
5642
     * @param int      $userId
5643
     *
5644
     * @return string
5645
     */
5646
    public static function getOralFeedbackForm($template, $attemptId, $questionId, $userId)
5647
    {
5648
        $template->assign('user_id', $userId);
5649
        $template->assign('question_id', $questionId);
5650
        $template->assign('directory', "/../exercises/teacher_audio/$attemptId/");
5651
        $template->assign('file_name', "{$questionId}_{$userId}");
5652
5653
        return $template->fetch($template->get_template('exercise/oral_expression.tpl'));
5654
    }
5655
5656
    /**
5657
     * Get the audio componen for a teacher audio feedback.
5658
     *
5659
     * @param int $attemptId
5660
     * @param int $questionId
5661
     * @param int $userId
5662
     *
5663
     * @return string
5664
     */
5665
    public static function getOralFeedbackAudio($attemptId, $questionId, $userId)
5666
    {
5667
        $courseInfo = api_get_course_info();
5668
        $sessionId = api_get_session_id();
5669
        $groupId = api_get_group_id();
5670
        $sysCourseDir = api_get_path(SYS_COURSE_PATH).$courseInfo['path'];
5671
        $webCourseDir = api_get_path(WEB_COURSE_PATH).$courseInfo['path'];
5672
        $fileName = "{$questionId}_{$userId}".DocumentManager::getDocumentSuffix($courseInfo, $sessionId, $groupId);
5673
        $filePath = null;
5674
5675
        $relFilePath = "/exercises/teacher_audio/$attemptId/$fileName";
5676
5677
        if (file_exists($sysCourseDir.$relFilePath.'.ogg')) {
5678
            $filePath = $webCourseDir.$relFilePath.'.ogg';
5679
        } elseif (file_exists($sysCourseDir.$relFilePath.'.wav.wav')) {
5680
            $filePath = $webCourseDir.$relFilePath.'.wav.wav';
5681
        } elseif (file_exists($sysCourseDir.$relFilePath.'.wav')) {
5682
            $filePath = $webCourseDir.$relFilePath.'.wav';
5683
        }
5684
5685
        if (!$filePath) {
5686
            return '';
5687
        }
5688
5689
        return Display::tag(
5690
            'audio',
5691
            null,
5692
            ['src' => $filePath]
5693
        );
5694
    }
5695
5696
    /**
5697
     * @return array
5698
     */
5699
    public static function getNotificationSettings()
5700
    {
5701
        $emailAlerts = [
5702
            2 => get_lang('SendEmailToTeacherWhenStudentStartQuiz'),
5703
            1 => get_lang('SendEmailToTeacherWhenStudentEndQuiz'), // default
5704
            3 => get_lang('SendEmailToTeacherWhenStudentEndQuizOnlyIfOpenQuestion'),
5705
            4 => get_lang('SendEmailToTeacherWhenStudentEndQuizOnlyIfOralQuestion'),
5706
        ];
5707
5708
        return $emailAlerts;
5709
    }
5710
5711
    /**
5712
     * Get the additional actions added in exercise_additional_teacher_modify_actions configuration.
5713
     *
5714
     * @param int $exerciseId
5715
     * @param int $iconSize
5716
     *
5717
     * @return string
5718
     */
5719
    public static function getAdditionalTeacherActions($exerciseId, $iconSize = ICON_SIZE_SMALL)
5720
    {
5721
        $additionalActions = api_get_configuration_value('exercise_additional_teacher_modify_actions') ?: [];
5722
        $actions = [];
5723
5724
        foreach ($additionalActions as $additionalAction) {
5725
            $actions[] = call_user_func(
5726
                $additionalAction,
5727
                $exerciseId,
5728
                $iconSize
5729
            );
5730
        }
5731
5732
        return implode(PHP_EOL, $actions);
5733
    }
5734
5735
    /**
5736
     * @param int $userId
5737
     * @param int $courseId
5738
     * @param int $sessionId
5739
     *
5740
     * @throws \Doctrine\ORM\Query\QueryException
5741
     *
5742
     * @return int
5743
     */
5744
    public static function countAnsweredQuestionsByUserAfterTime(DateTime $time, $userId, $courseId, $sessionId)
5745
    {
5746
        $em = Database::getManager();
5747
5748
        $time = api_get_utc_datetime($time->format('Y-m-d H:i:s'), false, true);
5749
5750
        $result = $em
5751
            ->createQuery('
5752
                SELECT COUNT(ea) FROM ChamiloCoreBundle:TrackEAttempt ea
5753
                WHERE ea.userId = :user AND ea.cId = :course AND ea.sessionId = :session
5754
                    AND ea.tms > :time
5755
            ')
5756
            ->setParameters(['user' => $userId, 'course' => $courseId, 'session' => $sessionId, 'time' => $time])
5757
            ->getSingleScalarResult();
5758
5759
        return $result;
5760
    }
5761
5762
    /**
5763
     * @param int $userId
5764
     * @param int $numberOfQuestions
5765
     * @param int $courseId
5766
     * @param int $sessionId
5767
     *
5768
     * @throws \Doctrine\ORM\Query\QueryException
5769
     *
5770
     * @return bool
5771
     */
5772
    public static function isQuestionsLimitPerDayReached($userId, $numberOfQuestions, $courseId, $sessionId)
5773
    {
5774
        $questionsLimitPerDay = (int) api_get_course_setting('quiz_question_limit_per_day');
5775
5776
        if ($questionsLimitPerDay <= 0) {
5777
            return false;
5778
        }
5779
5780
        $midnightTime = ChamiloApi::getServerMidnightTime();
5781
5782
        $answeredQuestionsCount = self::countAnsweredQuestionsByUserAfterTime(
5783
            $midnightTime,
5784
            $userId,
5785
            $courseId,
5786
            $sessionId
5787
        );
5788
5789
        return ($answeredQuestionsCount + $numberOfQuestions) > $questionsLimitPerDay;
5790
    }
5791
5792
    /**
5793
     * Check if an exercise complies with the requirements to be embedded in the mobile app or a video.
5794
     * By making sure it is set on one question per page and it only contains unique-answer or multiple-answer questions
5795
     * or unique-answer image. And that the exam does not have immediate feedback.
5796
     *
5797
     * @param array $exercise Exercise info
5798
     *
5799
     * @throws \Doctrine\ORM\Query\QueryException
5800
     *
5801
     * @return bool
5802
     */
5803
    public static function isQuizEmbeddable(array $exercise)
5804
    {
5805
        $em = Database::getManager();
5806
5807
        if (ONE_PER_PAGE != $exercise['type'] ||
5808
            in_array($exercise['feedback_type'], [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP])
5809
        ) {
5810
            return false;
5811
        }
5812
5813
        $countAll = $em
5814
            ->createQuery('SELECT COUNT(qq)
5815
                FROM ChamiloCourseBundle:CQuizQuestion qq
5816
                INNER JOIN ChamiloCourseBundle:CQuizRelQuestion qrq
5817
                   WITH qq.iid = qrq.questionId
5818
                WHERE qrq.exerciceId = :id'
5819
            )
5820
            ->setParameter('id', $exercise['iid'])
5821
            ->getSingleScalarResult();
5822
5823
        $countOfAllowed = $em
5824
            ->createQuery('SELECT COUNT(qq)
5825
                FROM ChamiloCourseBundle:CQuizQuestion qq
5826
                INNER JOIN ChamiloCourseBundle:CQuizRelQuestion qrq
5827
                   WITH qq.iid = qrq.questionId
5828
                WHERE qrq.exerciceId = :id AND qq.type IN (:types)'
5829
            )
5830
            ->setParameters(
5831
                [
5832
                    'id' => $exercise['iid'],
5833
                    'types' => [UNIQUE_ANSWER, MULTIPLE_ANSWER, UNIQUE_ANSWER_IMAGE],
5834
                ]
5835
            )
5836
            ->getSingleScalarResult();
5837
5838
        return $countAll === $countOfAllowed;
5839
    }
5840
5841
    /**
5842
     * Generate a certificate linked to current quiz and.
5843
     * Return the HTML block with links to download and view the certificate.
5844
     *
5845
     * @param float  $totalScore
5846
     * @param float  $totalWeight
5847
     * @param int    $studentId
5848
     * @param string $courseCode
5849
     * @param int    $sessionId
5850
     *
5851
     * @return string
5852
     */
5853
    public static function generateAndShowCertificateBlock(
5854
        $totalScore,
5855
        $totalWeight,
5856
        Exercise $objExercise,
5857
        $studentId,
5858
        $courseCode,
5859
        $sessionId = 0
5860
    ) {
5861
        if (!api_get_configuration_value('quiz_generate_certificate_ending') ||
5862
            !self::isSuccessExerciseResult($totalScore, $totalWeight, $objExercise->selectPassPercentage())
5863
        ) {
5864
            return '';
5865
        }
5866
5867
        /** @var Category $category */
5868
        $category = Category::load(null, null, $courseCode, null, null, $sessionId, 'ORDER By id');
5869
5870
        if (empty($category)) {
5871
            return '';
5872
        }
5873
5874
        /** @var Category $category */
5875
        $category = $category[0];
5876
        $categoryId = $category->get_id();
5877
        $link = LinkFactory::load(
5878
            null,
5879
            null,
5880
            $objExercise->selectId(),
5881
            null,
5882
            $courseCode,
5883
            $categoryId
5884
        );
5885
5886
        if (empty($link)) {
5887
            return '';
5888
        }
5889
5890
        $resourceDeletedMessage = $category->show_message_resource_delete($courseCode);
5891
5892
        if (false !== $resourceDeletedMessage || api_is_allowed_to_edit() || api_is_excluded_user_type()) {
5893
            return '';
5894
        }
5895
5896
        $certificate = Category::generateUserCertificate($categoryId, $studentId);
5897
5898
        if (!is_array($certificate)) {
5899
            return '';
5900
        }
5901
5902
        return Category::getDownloadCertificateBlock($certificate);
5903
    }
5904
5905
    /**
5906
     * @param int $exerciseId
5907
     */
5908
    public static function getExerciseTitleById($exerciseId)
5909
    {
5910
        $em = Database::getManager();
5911
5912
        return $em
5913
            ->createQuery('SELECT cq.title
5914
                FROM ChamiloCourseBundle:CQuiz cq
5915
                WHERE cq.iid = :iid'
5916
            )
5917
            ->setParameter('iid', $exerciseId)
5918
            ->getSingleScalarResult();
5919
    }
5920
5921
    /**
5922
     * @param int $exeId      ID from track_e_exercises
5923
     * @param int $userId     User ID
5924
     * @param int $exerciseId Exercise ID
5925
     * @param int $courseId   Optional. Coure ID.
5926
     *
5927
     * @return TrackEExercises|null
5928
     */
5929
    public static function recalculateResult($exeId, $userId, $exerciseId, $courseId = 0)
5930
    {
5931
        if (empty($userId) || empty($exerciseId)) {
5932
            return null;
5933
        }
5934
5935
        $em = Database::getManager();
5936
        /** @var TrackEExercises $trackedExercise */
5937
        $trackedExercise = $em->getRepository('ChamiloCoreBundle:TrackEExercises')->find($exeId);
5938
5939
        if (empty($trackedExercise)) {
5940
            return null;
5941
        }
5942
5943
        if ($trackedExercise->getExeUserId() != $userId ||
5944
            $trackedExercise->getExeExoId() != $exerciseId
5945
        ) {
5946
            return null;
5947
        }
5948
5949
        $questionList = $trackedExercise->getDataTracking();
5950
5951
        if (empty($questionList)) {
5952
            return null;
5953
        }
5954
5955
        $questionList = explode(',', $questionList);
5956
5957
        $exercise = new Exercise($courseId);
5958
        $courseInfo = $courseId ? api_get_course_info_by_id($courseId) : [];
5959
5960
        if ($exercise->read($exerciseId) === false) {
5961
            return null;
5962
        }
5963
5964
        $totalScore = 0;
5965
        $totalWeight = 0;
5966
5967
        $pluginEvaluation = QuestionOptionsEvaluationPlugin::create();
5968
5969
        $formula = 'true' === $pluginEvaluation->get(QuestionOptionsEvaluationPlugin::SETTING_ENABLE)
5970
            ? $pluginEvaluation->getFormulaForExercise($exerciseId)
5971
            : 0;
5972
5973
        if (empty($formula)) {
5974
            foreach ($questionList as $questionId) {
5975
                $question = Question::read($questionId, $courseInfo);
5976
5977
                if (false === $question) {
5978
                    continue;
5979
                }
5980
5981
                $totalWeight += $question->selectWeighting();
5982
5983
                // We're inside *one* question. Go through each possible answer for this question
5984
                $result = $exercise->manage_answer(
5985
                    $exeId,
5986
                    $questionId,
5987
                    [],
5988
                    'exercise_result',
5989
                    [],
5990
                    false,
5991
                    true,
5992
                    false,
5993
                    $exercise->selectPropagateNeg(),
5994
                    [],
5995
                    [],
5996
                    true
5997
                );
5998
5999
                //  Adding the new score.
6000
                $totalScore += $result['score'];
6001
            }
6002
        } else {
6003
            $totalScore = $pluginEvaluation->getResultWithFormula($exeId, $formula);
6004
            $totalWeight = $pluginEvaluation->getMaxScore();
6005
        }
6006
6007
        $trackedExercise
6008
            ->setExeResult($totalScore)
6009
            ->setExeWeighting($totalWeight);
6010
6011
        $em->persist($trackedExercise);
6012
        $em->flush();
6013
6014
        return $trackedExercise;
6015
    }
6016
6017
    public static function getTotalQuestionAnswered($courseId, $exerciseId, $questionId, $sessionId = 0, $groups = [], $users = [])
6018
    {
6019
        $courseId = (int) $courseId;
6020
        $exerciseId = (int) $exerciseId;
6021
        $questionId = (int) $questionId;
6022
        $sessionId = (int) $sessionId;
6023
6024
        $attemptTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
6025
        $trackTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
6026
6027
        $userCondition = '';
6028
        $allUsers = [];
6029
        if (!empty($groups)) {
6030
            foreach ($groups as $groupId) {
6031
                $groupUsers = GroupManager::get_users($groupId, null, null, null, false, $courseId);
6032
                if (!empty($groupUsers)) {
6033
                    $allUsers = array_merge($allUsers, $groupUsers);
6034
                }
6035
            }
6036
        }
6037
6038
        if (!empty($users)) {
6039
            $allUsers = array_merge($allUsers, $users);
6040
        }
6041
6042
        if (!empty($allUsers)) {
6043
            $allUsers = array_map('intval', $allUsers);
6044
            $usersToString = implode("', '", $allUsers);
6045
            $userCondition = " AND user_id IN ('$usersToString') ";
6046
        }
6047
6048
        $sessionCondition = '';
6049
        if (!empty($sessionId)) {
6050
            $sessionCondition = api_get_session_condition($sessionId, true, false, 'te.session_id');
6051
        }
6052
6053
        $sql = "SELECT count(te.exe_id) total
6054
                FROM $attemptTable t
6055
                INNER JOIN $trackTable te
6056
                ON (te.c_id = t.c_id AND t.exe_id = te.exe_id)
6057
                WHERE
6058
                    t.c_id = $courseId AND
6059
                    exe_exo_id = $exerciseId AND
6060
                    t.question_id = $questionId AND
6061
                    status != 'incomplete'
6062
                    $sessionCondition
6063
                    $userCondition
6064
        ";
6065
        $queryTotal = Database::query($sql);
6066
        $totalRow = Database::fetch_array($queryTotal, 'ASSOC');
6067
        $total = 0;
6068
        if ($totalRow) {
6069
            $total = (int) $totalRow['total'];
6070
        }
6071
6072
        return $total;
6073
    }
6074
6075
    public static function getWrongQuestionResults($courseId, $exerciseId, $sessionId = 0, $groups = [], $users = [], $limit = 10)
6076
    {
6077
        $courseId = (int) $courseId;
6078
        $exerciseId = (int) $exerciseId;
6079
        $limit = (int) $limit;
6080
6081
        $questionTable = Database::get_course_table(TABLE_QUIZ_QUESTION);
6082
        $attemptTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
6083
        $trackTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
6084
6085
        $sessionCondition = '';
6086
        if (!empty($sessionId)) {
6087
            $sessionCondition = api_get_session_condition($sessionId, true, false, 'te.session_id');
6088
        }
6089
6090
        $userCondition = '';
6091
        $allUsers = [];
6092
        if (!empty($groups)) {
6093
            foreach ($groups as $groupId) {
6094
                $groupUsers = GroupManager::get_users($groupId, null, null, null, false, $courseId);
6095
                if (!empty($groupUsers)) {
6096
                    $allUsers = array_merge($allUsers, $groupUsers);
6097
                }
6098
            }
6099
        }
6100
6101
        if (!empty($users)) {
6102
            $allUsers = array_merge($allUsers, $users);
6103
        }
6104
6105
        if (!empty($allUsers)) {
6106
            $allUsers = array_map('intval', $allUsers);
6107
            $usersToString = implode("', '", $allUsers);
6108
            $userCondition .= " AND user_id IN ('$usersToString') ";
6109
        }
6110
6111
        $sql = "SELECT q.question, question_id, count(q.iid) count
6112
                FROM $attemptTable t
6113
                INNER JOIN $questionTable q
6114
                ON q.iid = t.question_id
6115
                INNER JOIN $trackTable te
6116
                ON t.exe_id = te.exe_id
6117
                WHERE
6118
                    t.c_id = $courseId AND
6119
                    t.marks != q.ponderation AND
6120
                    exe_exo_id = $exerciseId AND
6121
                    status != 'incomplete'
6122
                    $sessionCondition
6123
                    $userCondition
6124
                GROUP BY q.iid
6125
                ORDER BY count DESC
6126
                LIMIT $limit
6127
        ";
6128
6129
        $result = Database::query($sql);
6130
6131
        return Database::store_result($result, 'ASSOC');
6132
    }
6133
6134
    public static function getExerciseResultsCount($type, $courseId, Exercise $exercise, $sessionId = 0)
6135
    {
6136
        $courseId = (int) $courseId;
6137
        $exerciseId = (int) $exercise->iid;
6138
6139
        $trackTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
6140
6141
        $sessionCondition = '';
6142
        if (!empty($sessionId)) {
6143
            $sessionCondition = api_get_session_condition($sessionId, true, false, 'te.session_id');
6144
        }
6145
6146
        $passPercentage = $exercise->selectPassPercentage();
6147
        $minPercentage = 100;
6148
        if (!empty($passPercentage)) {
6149
            $minPercentage = $passPercentage;
6150
        }
6151
6152
        $selectCount = 'count(DISTINCT te.exe_id)';
6153
        $scoreCondition = '';
6154
        switch ($type) {
6155
            case 'correct_student':
6156
                $selectCount = 'count(DISTINCT te.exe_user_id)';
6157
                $scoreCondition = " AND (exe_result/exe_weighting*100) >= $minPercentage ";
6158
                break;
6159
            case 'wrong_student':
6160
                $selectCount = 'count(DISTINCT te.exe_user_id)';
6161
                $scoreCondition = " AND (exe_result/exe_weighting*100) < $minPercentage ";
6162
                break;
6163
            case 'correct':
6164
                $scoreCondition = " AND (exe_result/exe_weighting*100) >= $minPercentage ";
6165
                break;
6166
            case 'wrong':
6167
                $scoreCondition = " AND (exe_result/exe_weighting*100) < $minPercentage ";
6168
                break;
6169
        }
6170
6171
        $sql = "SELECT $selectCount count
6172
                FROM $trackTable te
6173
                WHERE
6174
                    c_id = $courseId AND
6175
                    exe_exo_id = $exerciseId AND
6176
                    status != 'incomplete'
6177
                    $scoreCondition
6178
                    $sessionCondition
6179
        ";
6180
        $result = Database::query($sql);
6181
        $totalRow = Database::fetch_array($result, 'ASSOC');
6182
        $total = 0;
6183
        if ($totalRow) {
6184
            $total = (int) $totalRow['count'];
6185
        }
6186
6187
        return $total;
6188
    }
6189
6190
    public static function parseContent($content, $stats, Exercise $exercise, $trackInfo, $currentUserId = 0)
6191
    {
6192
        $wrongAnswersCount = $stats['failed_answers_count'];
6193
        $attemptDate = substr($trackInfo['exe_date'], 0, 10);
6194
        $exeId = $trackInfo['exe_id'];
6195
        $resultsStudentUrl = api_get_path(WEB_CODE_PATH).
6196
            'exercise/result.php?id='.$exeId.'&'.api_get_cidreq();
6197
        $resultsTeacherUrl = api_get_path(WEB_CODE_PATH).
6198
            'exercise/exercise_show.php?action=edit&id='.$exeId.'&'.api_get_cidreq(true, true, 'teacher');
6199
6200
        $content = str_replace(
6201
            [
6202
                '((exercise_error_count))',
6203
                '((all_answers_html))',
6204
                '((all_answers_teacher_html))',
6205
                '((exercise_title))',
6206
                '((exercise_attempt_date))',
6207
                '((link_to_test_result_page_student))',
6208
                '((link_to_test_result_page_teacher))',
6209
            ],
6210
            [
6211
                $wrongAnswersCount,
6212
                $stats['all_answers_html'],
6213
                $stats['all_answers_teacher_html'],
6214
                $exercise->get_formated_title(),
6215
                $attemptDate,
6216
                $resultsStudentUrl,
6217
                $resultsTeacherUrl,
6218
            ],
6219
            $content
6220
        );
6221
6222
        $currentUserId = empty($currentUserId) ? api_get_user_id() : (int) $currentUserId;
6223
6224
        $content = AnnouncementManager::parseContent(
6225
            $currentUserId,
6226
            $content,
6227
            api_get_course_id(),
6228
            api_get_session_id()
6229
        );
6230
6231
        return $content;
6232
    }
6233
6234
    public static function sendNotification(
6235
        $currentUserId,
6236
        $objExercise,
6237
        $exercise_stat_info,
6238
        $courseInfo,
6239
        $attemptCountToSend,
6240
        $stats,
6241
        $statsTeacher
6242
    ) {
6243
        $notifications = api_get_configuration_value('exercise_finished_notification_settings');
6244
        if (empty($notifications)) {
6245
            return false;
6246
        }
6247
6248
        $studentId = $exercise_stat_info['exe_user_id'];
6249
        $exerciseExtraFieldValue = new ExtraFieldValue('exercise');
6250
        $wrongAnswersCount = $stats['failed_answers_count'];
6251
        $exercisePassed = $stats['exercise_passed'];
6252
        $countPendingQuestions = $stats['count_pending_questions'];
6253
        $stats['all_answers_teacher_html'] = $statsTeacher['all_answers_html'];
6254
6255
        // If there are no pending questions (Open questions).
6256
        if (0 === $countPendingQuestions) {
6257
            /*$extraFieldData = $exerciseExtraFieldValue->get_values_by_handler_and_field_variable(
6258
                $objExercise->iid,
6259
                'signature_mandatory'
6260
            );
6261
6262
            if ($extraFieldData && isset($extraFieldData['value']) && 1 === (int) $extraFieldData['value']) {
6263
                if (ExerciseSignaturePlugin::exerciseHasSignatureActivated($objExercise)) {
6264
                    $signature = ExerciseSignaturePlugin::getSignature($studentId, $exercise_stat_info);
6265
                    if (false !== $signature) {
6266
                        //return false;
6267
                    }
6268
                }
6269
            }*/
6270
6271
            // Notifications.
6272
            $extraFieldData = $exerciseExtraFieldValue->get_values_by_handler_and_field_variable(
6273
                $objExercise->iid,
6274
                'notifications'
6275
            );
6276
            $exerciseNotification = '';
6277
            if ($extraFieldData && isset($extraFieldData['value'])) {
6278
                $exerciseNotification = $extraFieldData['value'];
6279
            }
6280
6281
            $subject = sprintf(get_lang('WrongAttemptXInCourseX'), $attemptCountToSend, $courseInfo['title']);
6282
            if ($exercisePassed) {
6283
                $subject = sprintf(get_lang('ExerciseValidationInCourseX'), $courseInfo['title']);
6284
            }
6285
6286
            if ($exercisePassed) {
6287
                $extraFieldData = $exerciseExtraFieldValue->get_values_by_handler_and_field_variable(
6288
                    $objExercise->iid,
6289
                    'MailSuccess'
6290
                );
6291
            } else {
6292
                $extraFieldData = $exerciseExtraFieldValue->get_values_by_handler_and_field_variable(
6293
                    $objExercise->iid,
6294
                    'MailAttempt'.$attemptCountToSend
6295
                );
6296
            }
6297
6298
            // Blocking exercise.
6299
            $blockPercentageExtra = $exerciseExtraFieldValue->get_values_by_handler_and_field_variable(
6300
                $objExercise->iid,
6301
                'blocking_percentage'
6302
            );
6303
            $blockPercentage = false;
6304
            if ($blockPercentageExtra && isset($blockPercentageExtra['value']) && $blockPercentageExtra['value']) {
6305
                $blockPercentage = $blockPercentageExtra['value'];
6306
            }
6307
            if ($blockPercentage) {
6308
                $passBlock = $stats['total_percentage'] > $blockPercentage;
6309
                if (false === $passBlock) {
6310
                    $extraFieldData = $exerciseExtraFieldValue->get_values_by_handler_and_field_variable(
6311
                        $objExercise->iid,
6312
                        'MailIsBlockByPercentage'
6313
                    );
6314
                }
6315
            }
6316
6317
            $extraFieldValueUser = new ExtraFieldValue('user');
6318
6319
            if ($extraFieldData && isset($extraFieldData['value'])) {
6320
                $content = $extraFieldData['value'];
6321
                $content = self::parseContent($content, $stats, $objExercise, $exercise_stat_info, $studentId);
6322
                //if (false === $exercisePassed) {
6323
                if (0 !== $wrongAnswersCount) {
6324
                    $content .= $stats['failed_answers_html'];
6325
                }
6326
6327
                $sendMessage = true;
6328
                if (!empty($exerciseNotification)) {
6329
                    foreach ($notifications as $name => $notificationList) {
6330
                        if ($exerciseNotification !== $name) {
6331
                            continue;
6332
                        }
6333
                        foreach ($notificationList as $notificationName => $attemptData) {
6334
                            if ('student_check' === $notificationName) {
6335
                                $sendMsgIfInList = isset($attemptData['send_notification_if_user_in_extra_field']) ? $attemptData['send_notification_if_user_in_extra_field'] : '';
6336
                                if (!empty($sendMsgIfInList)) {
6337
                                    foreach ($sendMsgIfInList as $skipVariable => $skipValues) {
6338
                                        $userExtraFieldValue = $extraFieldValueUser->get_values_by_handler_and_field_variable(
6339
                                            $studentId,
6340
                                            $skipVariable
6341
                                        );
6342
6343
                                        if (empty($userExtraFieldValue)) {
6344
                                            $sendMessage = false;
6345
                                            break;
6346
                                        } else {
6347
                                            $sendMessage = false;
6348
                                            if (isset($userExtraFieldValue['value']) &&
6349
                                                in_array($userExtraFieldValue['value'], $skipValues)
6350
                                            ) {
6351
                                                $sendMessage = true;
6352
                                                break;
6353
                                            }
6354
                                        }
6355
                                    }
6356
                                }
6357
                                break;
6358
                            }
6359
                        }
6360
                    }
6361
                }
6362
6363
                // Send to student.
6364
                if ($sendMessage) {
6365
                    MessageManager::send_message($currentUserId, $subject, $content);
6366
                }
6367
            }
6368
6369
            if (!empty($exerciseNotification)) {
6370
                foreach ($notifications as $name => $notificationList) {
6371
                    if ($exerciseNotification !== $name) {
6372
                        continue;
6373
                    }
6374
                    foreach ($notificationList as $attemptData) {
6375
                        $skipNotification = false;
6376
                        $skipNotificationList = isset($attemptData['send_notification_if_user_in_extra_field']) ? $attemptData['send_notification_if_user_in_extra_field'] : [];
6377
                        if (!empty($skipNotificationList)) {
6378
                            foreach ($skipNotificationList as $skipVariable => $skipValues) {
6379
                                $userExtraFieldValue = $extraFieldValueUser->get_values_by_handler_and_field_variable(
6380
                                    $studentId,
6381
                                    $skipVariable
6382
                                );
6383
6384
                                if (empty($userExtraFieldValue)) {
6385
                                    $skipNotification = true;
6386
                                    break;
6387
                                } else {
6388
                                    if (isset($userExtraFieldValue['value'])) {
6389
                                        if (!in_array($userExtraFieldValue['value'], $skipValues)) {
6390
                                            $skipNotification = true;
6391
                                            break;
6392
                                        }
6393
                                    } else {
6394
                                        $skipNotification = true;
6395
                                        break;
6396
                                    }
6397
                                }
6398
                            }
6399
                        }
6400
6401
                        if ($skipNotification) {
6402
                            continue;
6403
                        }
6404
6405
                        $email = isset($attemptData['email']) ? $attemptData['email'] : '';
6406
                        $emailList = explode(',', $email);
6407
                        if (empty($emailList)) {
6408
                            continue;
6409
                        }
6410
                        $attempts = isset($attemptData['attempts']) ? $attemptData['attempts'] : [];
6411
                        foreach ($attempts as $attempt) {
6412
                            $sendMessage = false;
6413
                            if (isset($attempt['attempt']) && $attemptCountToSend !== (int) $attempt['attempt']) {
6414
                                continue;
6415
                            }
6416
6417
                            if (!isset($attempt['status'])) {
6418
                                continue;
6419
                            }
6420
6421
                            if ($blockPercentage && isset($attempt['is_block_by_percentage'])) {
6422
                                if ($attempt['is_block_by_percentage']) {
6423
                                    if ($passBlock) {
6424
                                        continue;
6425
                                    }
6426
                                } else {
6427
                                    if (false === $passBlock) {
6428
                                        continue;
6429
                                    }
6430
                                }
6431
                            }
6432
6433
                            switch ($attempt['status']) {
6434
                                case 'passed':
6435
                                    if ($exercisePassed) {
6436
                                        $sendMessage = true;
6437
                                    }
6438
                                    break;
6439
                                case 'failed':
6440
                                    if (false === $exercisePassed) {
6441
                                        $sendMessage = true;
6442
                                    }
6443
                                    break;
6444
                                case 'all':
6445
                                    $sendMessage = true;
6446
                                    break;
6447
                            }
6448
6449
                            if ($sendMessage) {
6450
                                $attachments = [];
6451
                                if (isset($attempt['add_pdf']) && $attempt['add_pdf']) {
6452
                                    // Get pdf content
6453
                                    $pdfExtraData = $exerciseExtraFieldValue->get_values_by_handler_and_field_variable(
6454
                                        $objExercise->iid,
6455
                                        $attempt['add_pdf']
6456
                                    );
6457
6458
                                    if ($pdfExtraData && isset($pdfExtraData['value'])) {
6459
                                        $pdfContent = self::parseContent(
6460
                                            $pdfExtraData['value'],
6461
                                            $stats,
6462
                                            $objExercise,
6463
                                            $exercise_stat_info,
6464
                                            $studentId
6465
                                        );
6466
6467
                                        @$pdf = new PDF();
6468
                                        $filename = get_lang('Exercise');
6469
                                        $cssFile = api_get_path(SYS_CSS_PATH).'themes/chamilo/default.css';
6470
                                        $pdfPath = @$pdf->content_to_pdf(
6471
                                            "<html><body>$pdfContent</body></html>",
6472
                                            file_get_contents($cssFile),
6473
                                            $filename,
6474
                                            api_get_course_id(),
6475
                                            'F',
6476
                                            false,
6477
                                            null,
6478
                                            false,
6479
                                            true
6480
                                        );
6481
                                        $attachments[] = ['filename' => $filename, 'path' => $pdfPath];
6482
                                    }
6483
                                }
6484
6485
                                $content = isset($attempt['content_default']) ? $attempt['content_default'] : '';
6486
                                if (isset($attempt['content'])) {
6487
                                    $extraFieldData = $exerciseExtraFieldValue->get_values_by_handler_and_field_variable(
6488
                                        $objExercise->iid,
6489
                                        $attempt['content']
6490
                                    );
6491
                                    if ($extraFieldData && isset($extraFieldData['value']) && !empty($extraFieldData['value'])) {
6492
                                        $content = $extraFieldData['value'];
6493
                                    }
6494
                                }
6495
6496
                                if (!empty($content)) {
6497
                                    $content = self::parseContent(
6498
                                        $content,
6499
                                        $stats,
6500
                                        $objExercise,
6501
                                        $exercise_stat_info,
6502
                                        $studentId
6503
                                    );
6504
                                    foreach ($emailList as $email) {
6505
                                        if (empty($email)) {
6506
                                            continue;
6507
                                        }
6508
                                        api_mail_html(
6509
                                            null,
6510
                                            $email,
6511
                                            $subject,
6512
                                            $content,
6513
                                            null,
6514
                                            null,
6515
                                            [],
6516
                                            $attachments
6517
                                        );
6518
                                    }
6519
                                }
6520
6521
                                if (isset($attempt['post_actions'])) {
6522
                                    foreach ($attempt['post_actions'] as $action => $params) {
6523
                                        switch ($action) {
6524
                                            case 'subscribe_student_to_courses':
6525
                                                foreach ($params as $code) {
6526
                                                    CourseManager::subscribeUser($currentUserId, $code);
6527
                                                    break;
6528
                                                }
6529
                                                break;
6530
                                        }
6531
                                    }
6532
                                }
6533
                            }
6534
                        }
6535
                    }
6536
                }
6537
            }
6538
        }
6539
    }
6540
6541
    /**
6542
     * Delete an exercise attempt.
6543
     *
6544
     * Log the exe_id deleted with the exe_user_id related.
6545
     *
6546
     * @param int $exeId
6547
     */
6548
    public static function deleteExerciseAttempt($exeId)
6549
    {
6550
        $exeId = (int) $exeId;
6551
6552
        $trackExerciseInfo = self::get_exercise_track_exercise_info($exeId);
6553
6554
        if (empty($trackExerciseInfo)) {
6555
            return;
6556
        }
6557
6558
        $tblTrackExercises = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCISES);
6559
        $tblTrackAttempt = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
6560
6561
        Database::query("DELETE FROM $tblTrackExercises WHERE exe_id = $exeId");
6562
        Database::query("DELETE FROM $tblTrackAttempt WHERE exe_id = $exeId");
6563
6564
        Event::addEvent(
6565
            LOG_EXERCISE_ATTEMPT_DELETE,
6566
            LOG_EXERCISE_ATTEMPT,
6567
            $exeId,
6568
            api_get_utc_datetime()
6569
        );
6570
        Event::addEvent(
6571
            LOG_EXERCISE_ATTEMPT_DELETE,
6572
            LOG_EXERCISE_AND_USER_ID,
6573
            $exeId.'-'.$trackExerciseInfo['exe_user_id'],
6574
            api_get_utc_datetime()
6575
        );
6576
    }
6577
6578
    public static function scorePassed($score, $total)
6579
    {
6580
        $compareResult = bccomp($score, $total, 3);
6581
        $scorePassed = 1 === $compareResult || 0 === $compareResult;
6582
        if (false === $scorePassed) {
6583
            $epsilon = 0.00001;
6584
            if (abs($score - $total) < $epsilon) {
6585
                $scorePassed = true;
6586
            }
6587
        }
6588
6589
        return $scorePassed;
6590
    }
6591
}
6592