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