Passed
Pull Request — 1.11.x (#4890)
by Angel Fernando Quiroz
08:15
created

LogController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 34
c 1
b 0
f 0
dl 0
loc 61
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 49 5
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 ChamiloSession;
10
use Exception;
11
use Exercise;
12
use Security;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class LogController extends BaseController
17
{
18
    public const VALID_ACTIONS = [
19
        Log::TYPE_OUTFOCUSED,
20
        Log::TYPE_RETURN,
21
        Log::TYPE_ABANDONMENT_LIMIT,
22
        Log::TYPE_TIME_LIMIT,
23
    ];
24
25
    /**
26
     * @throws Exception
27
     */
28
    public function __invoke(): Response
29
    {
30
        parent::__invoke();
31
32
        $tokenIsValid = Security::check_token('get', null, 'exercisefocused');
33
34
        if (!$tokenIsValid) {
35
            throw new Exception();
36
        }
37
38
        $action = $this->request->query->get('action');
39
        $exeId = (int) ChamiloSession::read('exe_id');
40
41
        if (!in_array($action, self::VALID_ACTIONS)) {
42
            throw new Exception();
43
        }
44
45
        $trackingExercise = $this->em->find(TrackEExercises::class, $exeId);
46
47
        if (!$trackingExercise) {
48
            throw new Exception();
49
        }
50
51
        $log = new Log();
52
        $log
53
            ->setAction($action)
54
            ->setExe($trackingExercise);
55
56
        $this->em->persist($log);
57
        $this->em->flush();
58
59
        $remainingAbandonments = -1;
60
61
        if ('true' === $this->plugin->get('enable_abandonment_limit')) {
62
            $countAbandonments = $this->logRepository->countByActionInExe($trackingExercise, Log::TYPE_OUTFOCUSED);
63
64
            $remainingAbandonments = (int) $this->plugin->get('abandonment_limit') - $countAbandonments;
65
        }
66
67
        $exercise = new Exercise(api_get_course_int_id());
68
        $exercise->read($trackingExercise->getExeExoId());
69
70
        $json = [
71
            'sec_token' => Security::get_token('exercisefocused'),
72
            'type' => (int) $exercise->selectType(),
73
            'remainingAbandonments' => $remainingAbandonments,
74
        ];
75
76
        return JsonResponse::create($json);
77
    }
78
}
79