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; |
|
|
|
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response as HttpResponse; |
|
|
|
|
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
|
|
|
|
39
|
|
|
$levelId = $this->request->request->getInt('level_id'); |
40
|
|
|
$exercise = $this->em->find( |
41
|
|
|
CQuiz::class, |
42
|
|
|
$this->request->request->getInt('exercise_id') |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$trackingExercise = $this->em->find(TrackEExercises::class, $existingExeId); |
46
|
|
|
|
47
|
|
|
$newFilename = ''; |
48
|
|
|
$level = 0; |
49
|
|
|
|
50
|
|
|
/** @var UploadedFile $imgSubmit */ |
51
|
|
|
if ($imgSubmit = $this->request->files->get('snapshot')) { |
52
|
|
|
$newFilename = uniqid().'_submit.jpg'; |
53
|
|
|
|
54
|
|
|
$imgSubmit->move($userDirName, $newFilename); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (ONE_PER_PAGE === $exercise->getType()) { |
58
|
|
|
$question = $this->em->find(CQuizQuestion::class, $levelId); |
59
|
|
|
$level = $question->getIid(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$log = new Log(); |
63
|
|
|
$log |
64
|
|
|
->setExercise($exercise) |
65
|
|
|
->setExe($trackingExercise) |
66
|
|
|
->setLevel($level) |
67
|
|
|
->setImageFilename($newFilename) |
68
|
|
|
; |
69
|
|
|
|
70
|
|
|
$this->em->persist($log); |
71
|
|
|
|
72
|
|
|
$this->em->flush(); |
73
|
|
|
|
74
|
|
|
return HttpResponse::create(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function createDirectory(): string |
78
|
|
|
{ |
79
|
|
|
$user = api_get_user_entity(api_get_user_id()); |
80
|
|
|
|
81
|
|
|
$pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring'; |
82
|
|
|
$userDirName = $pluginDirName.'/'.$user->getId(); |
83
|
|
|
|
84
|
|
|
$fs = new Filesystem(); |
85
|
|
|
$fs->mkdir( |
86
|
|
|
[$pluginDirName, $userDirName], |
87
|
|
|
api_get_permissions_for_new_directories() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
return $userDirName; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: