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

DetailController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
eloc 62
dl 0
loc 124
rs 10
c 3
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A __invoke() 0 44 4
A generateHeader() 0 12 1
A generateSnapshotList() 0 26 4
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
        $html = '';
122
123
        foreach ($logs as $i => $log) {
124
            $date = api_get_local_time($log['createdAt'], null, null, true, true, true);
125
126
            $html .= '<div class="col-xs-12 col-sm-6 col-md-3" style="clear: '.($i % 4 === 0 ? 'both' : 'none').';">';
127
            $html .= '<div class="thumbnail">';
128
            $html .= Display::img(
129
                ExerciseMonitoringPlugin::generateSnapshotUrl($userId, $log['imageFilename']),
130
                $date
131
            );
132
            $html .= '<div class="caption">';
133
            $html .= Display::tag('p', $date, ['class' => 'text-center']);
134
135
            if (!empty($log['question'])) {
136
                $html .= Display::tag('div', $log['question'], ['class' => 'text-center']);
137
            }
138
139
            $html .= '</div>';
140
            $html .= '</div>';
141
            $html .= '</div>';
142
        }
143
144
        return '<div class="row">'.$html.'</div>';
145
    }
146
}
147