Passed
Pull Request — 1.11.x (#4900)
by Angel Fernando Quiroz
16:10 queued 05:39
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\PluginBundle\ExerciseFocused\Traits\DetailControllerTrait;
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
    use DetailControllerTrait;
24
25
    /**
26
     * @var ExerciseMonitoringPlugin
27
     */
28
    private $plugin;
29
30
    /**
31
     * @var HttpRequest
32
     */
33
    private $request;
34
35
    /**
36
     * @var EntityManager
37
     */
38
    private $em;
39
40
    /**
41
     * @var EntityRepository
42
     */
43
    private $logRepository;
44
45
    public function __construct(
46
        ExerciseMonitoringPlugin $plugin,
47
        HttpRequest $request,
48
        EntityManager $em,
49
        EntityRepository $logRepository
50
    ) {
51
        $this->plugin = $plugin;
52
        $this->request = $request;
53
        $this->em = $em;
54
        $this->logRepository = $logRepository;
55
    }
56
57
    /**
58
     * @throws Exception
59
     */
60
    public function __invoke(): HttpResponse
61
    {
62
        if (!$this->plugin->isEnabled(true)) {
63
            throw new Exception();
64
        }
65
66
        $trackExe = $this->em->find(
67
            TrackEExercises::class,
68
            $this->request->query->getInt('id')
69
        );
70
71
        if (!$trackExe) {
72
            throw new Exception();
73
        }
74
75
        $exercise = $this->em->find(CQuiz::class, $trackExe->getExeExoId());
76
        $user = api_get_user_entity($trackExe->getExeUserId());
77
78
        $objExercise = new Exercise();
79
        $objExercise->read($trackExe->getExeExoId());
80
81
        $qb = $this->logRepository->createQueryBuilder('l');
82
        $qb
83
            ->select(['l.imageFilename', 'l.createdAt']);
84
85
        if (ONE_PER_PAGE == $objExercise->selectType()) {
86
            $qb
87
                ->addSelect(['qq.question AS log_level'])
88
                ->innerJoin(CQuizQuestion::class, 'qq', Join::WITH, 'l.level = qq.iid');
89
        }
90
91
        $logs = $qb
92
            ->andWhere(
93
                $qb->expr()->eq('l.exe', $trackExe->getExeId())
94
            )
95
            ->addOrderBy('l.createdAt')
96
            ->getQuery()
97
            ->getResult();
98
99
        $content = $this->generateHeader($objExercise, $user, $trackExe)
100
            .'<hr>'
101
            .$this->generateSnapshotList($logs, $trackExe->getExeUserId());
102
103
        return HttpResponse::create($content);
104
    }
105
106
    private function generateSnapshotList(array $logs, int $userId): string
107
    {
108
        $html = '';
109
110
        foreach ($logs as $i => $log) {
111
            $date = api_get_local_time($log['createdAt'], null, null, true, true, true);
112
113
            $html .= '<div class="col-xs-12 col-sm-6 col-md-3" style="clear: '.($i % 4 === 0 ? 'both' : 'none').';">';
114
            $html .= '<div class="thumbnail">';
115
            $html .= Display::img(
116
                ExerciseMonitoringPlugin::generateSnapshotUrl($userId, $log['imageFilename']),
117
                $date
118
            );
119
            $html .= '<div class="caption">';
120
            $html .= Display::tag('p', $date, ['class' => 'text-center']);
121
            $html .= Display::tag('div', $log['log_level'], ['class' => 'text-center']);
122
            $html .= '</div>';
123
            $html .= '</div>';
124
            $html .= '</div>';
125
        }
126
127
        return '<div class="row">'.$html.'</div>';
128
    }
129
}
130