Passed
Pull Request — 1.11.x (#4900)
by Angel Fernando Quiroz
16:28
created

ReportingController::setBreadcrumb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\ExerciseFocused\Controller;
6
7
use Chamilo\CoreBundle\Entity\TrackEExercises;
8
use Chamilo\CourseBundle\Entity\CQuiz;
9
use Chamilo\PluginBundle\ExerciseFocused\Traits\ReportingFilterTrait;
10
use Display;
11
use Exception;
12
use Symfony\Component\HttpFoundation\Response as HttpResponse;
13
14
class ReportingController extends BaseController
15
{
16
    use ReportingFilterTrait;
17
18
    public function __invoke(): HttpResponse
19
    {
20
        parent::__invoke();
21
22
        $exercise = $this->em->find(
23
            CQuiz::class,
24
            $this->request->query->getInt('id')
25
        );
26
27
        if (!$exercise) {
28
            throw new Exception();
29
        }
30
31
        $courseCode = api_get_course_id();
32
        $sessionId = api_get_session_id();
33
34
        $tab1 = $this->generateTabResume($exercise);
35
36
        $tab2 = $this->generateTabSearch($exercise, $courseCode, $sessionId);
37
38
        $tab3 = $this->generateTabSampling($exercise);
39
40
        $content = Display::tabs(
41
            [
42
                $this->plugin->get_lang('ReportByAttempts'),
43
                get_lang('Search'),
44
                $this->plugin->get_lang('RandomSampling'),
45
            ],
46
            [$tab1, $tab2, $tab3],
47
            'exercise-focused-tabs',
48
            [],
49
            [],
50
            1
51
        );
52
53
        $this->setBreadcrumb($exercise->getId());
54
55
        return $this->renderView(
56
            $this->plugin->get_lang('ReportByAttempts'),
57
            $content,
58
            $exercise->getTitle()
59
        );
60
    }
61
62
    private function generateTabResume(CQuiz $exercise): string
63
    {
64
        $results = $this->findResultsInCourse($exercise->getId());
65
66
        return $this->createTable($results)->toHtml();
67
    }
68
69
    /**
70
     * @throws Exception
71
     */
72
    private function generateTabSearch(CQuiz $exercise, string $courseCode, int $sessionId): string
73
    {
74
        $form = $this->createForm();
75
        $form->updateAttributes(['action' => api_get_self().'?'.api_get_cidreq().'&id='.$exercise->getId()]);
76
        $form->addHidden('cidReq', $courseCode);
77
        $form->addHidden('id_session', $sessionId);
78
        $form->addHidden('gidReq', 0);
79
        $form->addHidden('gradebook', 0);
80
        $form->addHidden('origin', api_get_origin());
81
        $form->addHidden('id', $exercise->getId());
82
83
        $tableHtml = '';
84
        $actions = '';
85
86
        if ($form->validate()) {
87
            $formValues = $form->exportValues();
88
89
            $action = Display::url(
90
                Display::return_icon('export_excel.png', get_lang('ExportExcel'), [], ICON_SIZE_MEDIUM),
91
                api_get_path(WEB_PLUGIN_PATH).'exercisefocused/pages/export.php?'.http_build_query($formValues)
92
            );
93
94
            $actions = Display::toolbarAction(
95
                'em-actions',
96
                [$action]
97
            );
98
99
            $results = $this->findResults($formValues);
100
101
            $tableHtml = $this->createTable($results)->toHtml();
102
        }
103
104
        return $form->returnForm().$actions.$tableHtml;
105
    }
106
107
    private function generateTabSampling(CQuiz $exercise): string
108
    {
109
        $results = $this->findRandomResults($exercise->getId());
110
111
        return $this->createTable($results)->toHtml();
112
    }
113
114
    /**
115
     * @return array<int, TrackEExercises>
116
     */
117
    private function setBreadcrumb($exerciseId): void
118
    {
119
        $codePath = api_get_path('WEB_CODE_PATH');
120
        $cidReq = api_get_cidreq();
121
122
        $GLOBALS['interbreadcrumb'][] = [
123
            'url' => $codePath."exercise/exercise.php?$cidReq",
124
            'name' => get_lang('Exercises'),
125
        ];
126
        $GLOBALS['interbreadcrumb'][] = [
127
            'url' => $codePath."exercise/exercise_report.php?$cidReq&".http_build_query(['exerciseId' => $exerciseId]),
128
            'name' => get_lang('StudentScore'),
129
        ];
130
    }
131
}
132