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

ExerciseSubmitController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nop 3
nc 1
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\TrackEExercises;
6
use Chamilo\CourseBundle\Entity\CQuiz;
7
use Chamilo\CourseBundle\Entity\CQuizQuestion;
8
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log;
9
use Doctrine\ORM\EntityManager;
10
use Symfony\Component\Filesystem\Filesystem;
11
use Symfony\Component\HttpFoundation\File\UploadedFile;
12
use Symfony\Component\HttpFoundation\Request as HttpRequest;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HttpRequest. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Symfony\Component\HttpFoundation\Response as HttpResponse;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HttpResponse. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
15
class ExerciseSubmitController
16
{
17
    private $plugin;
18
    private $request;
19
    private $em;
20
21
    public function __construct(ExerciseMonitoringPlugin $plugin, HttpRequest $request, EntityManager $em)
22
    {
23
        $this->plugin = $plugin;
24
        $this->request = $request;
25
        $this->em = $em;
26
    }
27
28
    /**
29
     * @throws \Doctrine\ORM\OptimisticLockException
30
     * @throws \Doctrine\ORM\ORMException
31
     * @throws \Doctrine\ORM\TransactionRequiredException
32
     */
33
    public function __invoke(): HttpResponse
34
    {
35
        $userDirName = $this->createDirectory();
36
37
        $existingExeId = (int) ChamiloSession::read('exe_id');
38
        $imgIddoc = $this->request->files->get('snapshot');
39
40
        $exercise = $this->em->find(
41
            CQuiz::class,
42
            $this->request->request->getInt('exercise_id')
43
        );
44
        $question = $this->em->find(
45
            CQuizQuestion::class,
46
            $this->request->request->getInt('question_id')
47
        );
48
        $trackingExercise = $this->em->find(TrackEExercises::class, $existingExeId);
49
50
        if ($imgIddoc) {
51
            $newFilename = uniqid().'_exercise.jpg';
52
53
            $imgIddoc->move($userDirName, $newFilename);
54
55
            $log = new Log();
56
            $log
57
                ->setExercise($exercise)
58
                ->setExe($trackingExercise)
59
                ->setLevel($question->getIid())
60
                ->setImageFilename($newFilename)
61
            ;
62
63
            $this->em->persist($log);
64
        }
65
66
        $this->em->flush();
67
68
        return HttpResponse::create();
69
    }
70
71
    private function createDirectory(): string
72
    {
73
        $user = api_get_user_entity(api_get_user_id());
74
75
        $pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring';
76
        $userDirName = $pluginDirName.'/'.$user->getId();
77
78
        $fs = new Filesystem();
79
        $fs->mkdir(
80
            [$pluginDirName, $userDirName],
81
            api_get_permissions_for_new_directories()
82
        );
83
84
        return $userDirName;
85
    }
86
}
87