Passed
Pull Request — 1.11.x (#4900)
by Angel Fernando Quiroz
13:48 queued 05:26
created

DetailController::__invoke()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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