1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For license terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
use Chamilo\CourseBundle\Entity\CQuiz; |
6
|
|
|
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
|
|
|
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response as HttpResponse; |
|
|
|
|
12
|
|
|
|
13
|
|
|
class StartController |
14
|
|
|
{ |
15
|
|
|
private $plugin; |
16
|
|
|
private $request; |
17
|
|
|
private $em; |
18
|
|
|
|
19
|
|
|
public function __construct(ExerciseMonitoringPlugin $plugin, HttpRequest $request, EntityManager $em) |
20
|
|
|
{ |
21
|
|
|
$this->plugin = $plugin; |
22
|
|
|
$this->request = $request; |
23
|
|
|
$this->em = $em; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function __invoke(): HttpResponse |
27
|
|
|
{ |
28
|
|
|
$userDirName = $this->createDirectory(); |
29
|
|
|
|
30
|
|
|
/** @var UploadedFile $imgIddoc */ |
31
|
|
|
$imgIddoc = $this->request->files->get('iddoc'); |
32
|
|
|
/** @var UploadedFile $imgLearner */ |
33
|
|
|
$imgLearner = $this->request->files->get('learner'); |
34
|
|
|
|
35
|
|
|
$exercise = $this->em->find(CQuiz::class, $this->request->request->getInt('exercise_id')); |
36
|
|
|
|
37
|
|
|
if ($imgIddoc) { |
|
|
|
|
38
|
|
|
$newFilename = uniqid().'_iddoc.jpg'; |
39
|
|
|
|
40
|
|
|
$imgIddoc->move($userDirName, $newFilename); |
41
|
|
|
|
42
|
|
|
$log = new Log(); |
43
|
|
|
$log |
44
|
|
|
->setExercise($exercise) |
45
|
|
|
->setLevel(-1) |
46
|
|
|
->setImageFilename($newFilename) |
47
|
|
|
; |
48
|
|
|
|
49
|
|
|
$this->em->persist($log); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($imgLearner) { |
|
|
|
|
53
|
|
|
$newFilename = uniqid().'_learner.jpg'; |
54
|
|
|
|
55
|
|
|
$imgLearner->move($userDirName, $newFilename); |
56
|
|
|
|
57
|
|
|
$log = new Log(); |
58
|
|
|
$log |
59
|
|
|
->setExercise($exercise) |
60
|
|
|
->setLevel(-1) |
61
|
|
|
->setImageFilename($newFilename) |
62
|
|
|
; |
63
|
|
|
|
64
|
|
|
$this->em->persist($log); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->em->flush(); |
68
|
|
|
|
69
|
|
|
return HttpResponse::create(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function createDirectory(): string |
73
|
|
|
{ |
74
|
|
|
$user = api_get_user_entity(api_get_user_id()); |
75
|
|
|
|
76
|
|
|
$pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring'; |
77
|
|
|
$userDirName = $pluginDirName.'/'.$user->getId(); |
78
|
|
|
|
79
|
|
|
$fs = new Filesystem(); |
80
|
|
|
$fs->mkdir( |
81
|
|
|
[$pluginDirName, $userDirName], |
82
|
|
|
api_get_permissions_for_new_directories() |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return $userDirName; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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: