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

DetailController::generateSnapshotList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 15
c 5
b 0
f 0
dl 0
loc 22
rs 9.7666
cc 3
nc 3
nop 2
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\PluginBundle\ExerciseFocused\Traits\DetailControllerTrait;
10
use Display;
11
use Doctrine\ORM\EntityManager;
12
use Doctrine\ORM\EntityRepository;
13
use Exception;
14
use Exercise;
15
use ExerciseMonitoringPlugin;
16
use Symfony\Component\HttpFoundation\Request as HttpRequest;
17
use Symfony\Component\HttpFoundation\Response as HttpResponse;
18
19
class DetailController
20
{
21
    use DetailControllerTrait;
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
        $logs = $this->logRepository->findSnapshots($objExercise, $trackExe);
80
81
        $content = $this->generateHeader($objExercise, $user, $trackExe)
82
            .'<hr>'
83
            .$this->generateSnapshotList($logs, $trackExe->getExeUserId());
84
85
        return HttpResponse::create($content);
86
    }
87
88
    private function generateSnapshotList(array $logs, int $userId): string
89
    {
90
        $html = '';
91
92
        foreach ($logs as $i => $log) {
93
            $date = api_get_local_time($log['createdAt'], null, null, true, true, true);
94
95
            $html .= '<div class="col-xs-12 col-sm-6 col-md-3" style="clear: '.($i % 4 === 0 ? 'both' : 'none').';">';
96
            $html .= '<div class="thumbnail">';
97
            $html .= Display::img(
98
                ExerciseMonitoringPlugin::generateSnapshotUrl($userId, $log['imageFilename']),
99
                $date
100
            );
101
            $html .= '<div class="caption">';
102
            $html .= Display::tag('p', $date, ['class' => 'text-center']);
103
            $html .= Display::tag('div', $log['log_level'], ['class' => 'text-center']);
104
            $html .= '</div>';
105
            $html .= '</div>';
106
            $html .= '</div>';
107
        }
108
109
        return '<div class="row">'.$html.'</div>';
110
    }
111
}
112