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

LogController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 43
c 1
b 0
f 0
dl 0
loc 78
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 66 7
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\CourseBundle\Entity\CQuizQuestion;
9
use Chamilo\PluginBundle\ExerciseFocused\Entity\Log;
10
use ChamiloSession;
11
use Exception;
12
use Exercise;
13
use ExerciseFocusedPlugin;
14
use Security;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Response;
17
18
class LogController extends BaseController
19
{
20
    public const VALID_ACTIONS = [
21
        Log::TYPE_OUTFOCUSED,
22
        Log::TYPE_RETURN,
23
        Log::TYPE_OUTFOCUSED_LIMIT,
24
        Log::TYPE_TIME_LIMIT,
25
    ];
26
27
    /**
28
     * @throws Exception
29
     */
30
    public function __invoke(): Response
31
    {
32
        parent::__invoke();
33
34
        $tokenIsValid = Security::check_token('get', null, 'exercisefocused');
35
36
        if (!$tokenIsValid) {
37
            throw new Exception('token invalid');
38
        }
39
40
        $action = $this->request->query->get('action');
41
        $levelId = $this->request->query->getInt('level_id');
42
43
        $exeId = (int) ChamiloSession::read('exe_id');
44
45
        if (!in_array($action, self::VALID_ACTIONS)) {
46
            throw new Exception('action invalid');
47
        }
48
49
        $trackingExercise = $this->em->find(TrackEExercises::class, $exeId);
50
51
        if (!$trackingExercise) {
52
            throw new Exception('no exercise attempt');
53
        }
54
55
        $objExercise = new Exercise($trackingExercise->getCId());
56
        $objExercise->read($trackingExercise->getExeExoId());
57
58
        $level = 0;
59
60
        if (ONE_PER_PAGE == $objExercise->selectType()) {
61
            $question = $this->em->find(CQuizQuestion::class, $levelId);
62
63
            if (!$question) {
64
                throw new Exception('Invalid level');
65
            }
66
67
            $level = $question->getIid();
68
        }
69
70
        $log = new Log();
71
        $log
72
            ->setAction($action)
73
            ->setExe($trackingExercise)
74
            ->setLevel($level);
75
76
        $this->em->persist($log);
77
        $this->em->flush();
78
79
        $remainingOutfocused = -1;
80
81
        if ('true' === $this->plugin->get(ExerciseFocusedPlugin::SETTING_ENABLE_OUTFOCUSED_LIMIT)) {
82
            $countOutfocused = $this->logRepository->countByActionInExe($trackingExercise, Log::TYPE_OUTFOCUSED);
83
84
            $remainingOutfocused = (int) $this->plugin->get(ExerciseFocusedPlugin::SETTING_OUTFOCUSED_LIMIT) - $countOutfocused;
85
        }
86
87
        $exercise = new Exercise(api_get_course_int_id());
88
        $exercise->read($trackingExercise->getExeExoId());
89
90
        $json = [
91
            'sec_token' => Security::get_token('exercisefocused'),
92
            'remainingOutfocused' => $remainingOutfocused,
93
        ];
94
95
        return JsonResponse::create($json);
96
    }
97
}
98