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

DetailController::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 22
rs 9.8333
cc 3
nc 3
nop 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\ExerciseFocused\Controller;
6
7
use Chamilo\CoreBundle\Entity\TrackEExercises;
8
use Chamilo\PluginBundle\ExerciseFocused\Entity\Log;
9
use Display;
10
use Doctrine\ORM\OptimisticLockException;
11
use Doctrine\ORM\ORMException;
12
use Doctrine\ORM\TransactionRequiredException;
13
use Exception;
14
use HTML_Table;
15
use Symfony\Component\HttpFoundation\Response as HttpResponse;
16
17
class DetailController extends BaseController
18
{
19
    /**
20
     * @throws OptimisticLockException
21
     * @throws TransactionRequiredException
22
     * @throws ORMException
23
     * @throws Exception
24
     */
25
    public function __invoke(): HttpResponse
26
    {
27
        parent::__invoke();
28
29
        $exeId = $this->request->query->getInt('id');
30
        $exe = $this->em->find(TrackEExercises::class, $exeId);
31
32
        if (!$exe) {
33
            throw new Exception();
34
        }
35
36
        $session = api_get_session_entity($exe->getSessionId());
37
        $user = api_get_user_entity($exe->getExeUserId());
38
39
        $subHeader = Display::page_subheader($user->getCompleteNameWithUsername());
40
        $subHeader2 = $session ? Display::page_subheader2($session->getName()) : '';
0 ignored issues
show
introduced by
$session is of type Chamilo\CoreBundle\Entity\Session, thus it always evaluated to true.
Loading history...
41
42
        $logs = $this->logRepository->findBy(['exe' => $exe], ['updatedAt' => 'ASC']);
43
        $table = $this->getTable($logs);
44
45
        return HttpResponse::create(
46
            $subHeader.$subHeader2.$table->toHtml()
47
        );
48
    }
49
50
    /**
51
     * @param array<int, Log> $logs
52
     *
53
     * @return void
54
     */
55
    private function getTable(array $logs): HTML_Table
56
    {
57
        $table = new HTML_Table(['class' => 'table table-hover table-striped data_table']);
58
        $table->setHeaderContents(0, 0, get_lang('Action'));
59
        $table->setHeaderContents(0, 1, get_lang('DateTime'));
60
61
        $row = 1;
62
63
        foreach ($logs as $log) {
64
            $table->setCellContents(
65
                $row,
66
                0,
67
                $this->plugin->getActionTitle($log->getAction())
68
            );
69
            $table->setCellContents(
70
                $row,
71
                1,
72
                api_get_local_time($log->getCreatedAt(), null, null, true, true, true)
73
            );
74
75
            $row++;
76
        }
77
78
        return $table;
79
    }
80
}
81