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

DetailController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 64
c 1
b 0
f 0
dl 0
loc 134
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generateSnapshotList() 0 30 3
A __construct() 0 10 1
A __invoke() 0 44 4
A generateHeader() 0 12 1
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\ExerciseMonitoring\Controller;
6
7
use Chamilo\CoreBundle\Entity\TrackEAttempt;
8
use Chamilo\CoreBundle\Entity\TrackEExercises;
9
use Chamilo\CourseBundle\Entity\CQuiz;
10
use Chamilo\CourseBundle\Entity\CQuizQuestion;
11
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log;
12
use Chamilo\UserBundle\Entity\User;
13
use Display;
14
use Doctrine\ORM\EntityManager;
15
use Doctrine\ORM\EntityRepository;
16
use Doctrine\ORM\Query\Expr\Join;
17
use Exception;
18
use Exercise;
19
use ExerciseMonitoringPlugin;
20
use HTML_Table;
21
use Symfony\Component\Filesystem\Filesystem;
22
use Symfony\Component\HttpFoundation\File\UploadedFile;
23
use Symfony\Component\HttpFoundation\Request as HttpRequest;
24
use Symfony\Component\HttpFoundation\Response as HttpResponse;
25
26
class DetailController
27
{
28
    /**
29
     * @var ExerciseMonitoringPlugin
30
     */
31
    private $plugin;
32
33
    /**
34
     * @var HttpRequest
35
     */
36
    private $request;
37
38
    /**
39
     * @var EntityManager
40
     */
41
    private $em;
42
43
    /**
44
     * @var EntityRepository
45
     */
46
    private $logRepository;
47
48
    public function __construct(
49
        ExerciseMonitoringPlugin $plugin,
50
        HttpRequest $request,
51
        EntityManager $em,
52
        EntityRepository $logRepository
53
    ) {
54
        $this->plugin = $plugin;
55
        $this->request = $request;
56
        $this->em = $em;
57
        $this->logRepository = $logRepository;
58
    }
59
60
    /**
61
     * @throws Exception
62
     */
63
    public function __invoke(): HttpResponse
64
    {
65
        if (!$this->plugin->isEnabled(true)) {
66
            throw new Exception();
67
        }
68
69
        $trackExe = $this->em->find(
70
            TrackEExercises::class,
71
            $this->request->query->getInt('id')
72
        );
73
74
        if (!$trackExe) {
75
            throw new Exception();
76
        }
77
78
        $exercise = $this->em->find(CQuiz::class, $trackExe->getExeExoId());
79
        $user = api_get_user_entity($trackExe->getExeUserId());
80
81
        $objExercise = new Exercise();
82
        $objExercise->read($trackExe->getExeExoId());
83
84
        $qb = $this->logRepository->createQueryBuilder('l');
85
        $qb
86
            ->select(['l.imageFilename', 'l.createdAt']);
87
88
        if (ONE_PER_PAGE == $objExercise->selectType()) {
89
            $qb
90
                ->addSelect(['qq.question'])
91
                ->innerJoin(CQuizQuestion::class, 'qq', Join::WITH, 'l.level = qq.iid');
92
        }
93
94
        $logs = $qb
95
            ->andWhere(
96
                $qb->expr()->eq('l.exe', $trackExe->getExeId())
97
            )
98
            ->addOrderBy('l.createdAt')
99
            ->getQuery()
100
            ->getResult();
101
102
        $content = $this->generateHeader($objExercise, $user, $trackExe)
103
            .'<hr>'
104
            .$this->generateSnapshotList($logs, $trackExe->getExeUserId());
105
106
        return HttpResponse::create($content);
107
    }
108
109
    private function generateHeader(Exercise $objExercise, User $student, TrackEExercises $trackExe): string
110
    {
111
        $startDate = api_get_local_time($trackExe->getStartDate(), null, null, true, true, true);
112
        $endDate = api_get_local_time($trackExe->getExeDate(), null, null, true, true, true);
113
114
        return Display::page_subheader2($objExercise->selectTitle())
115
            .Display::tag('p', $student->getCompleteNameWithUsername(), ['class' => 'lead'])
116
            .Display::tag(
117
                'p',
118
                sprintf(get_lang('QuizRemindStartDate'), $startDate)
119
                    .sprintf(get_lang('QuizRemindEndDate'), $endDate)
120
                    .sprintf(get_lang('QuizRemindDuration'), api_format_time($trackExe->getExeDuration()))
121
            );
122
    }
123
124
    /**
125
     * @param array $logs
126
     * @param int $userId
127
     *
128
     * @return string
129
     */
130
    private function generateSnapshotList(array $logs, int $userId): string
131
    {
132
        $pluginDirName = api_get_path(WEB_UPLOAD_PATH).'plugins/exercisemonitoring';
133
134
        $html = '';
135
136
        foreach ($logs as $log) {
137
            $userDirName = $pluginDirName.'/'.$userId;
138
139
            $date = api_get_local_time($log['createdAt'], null, null, true, true, true);
140
141
            $html .= '<div class="col-xs-12 col-sm-6 col-md-3">';
142
            $html .= '<div class="thumbnail">';
143
            $html .= Display::img(
144
                $userDirName.'/'.$log['imageFilename'],
145
                $date
146
            );
147
            $html .= '<div class="caption">';
148
            $html .= Display::tag('p', $date, ['class' => 'text-center']);
149
150
            if (!empty($log['question'])) {
151
                $html .= Display::tag('div', $log['question'], ['class' => 'text-center']);
152
            }
153
154
            $html .= '</div>';
155
            $html .= '</div>';
156
            $html .= '</div>';
157
        }
158
159
        return '<div class="row">'.$html.'</div>';
160
    }
161
}
162