|
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
|
|
|
$fileNamesToUpdate = []; |
|
38
|
|
|
|
|
39
|
|
|
if ($imgIddoc) { |
|
|
|
|
|
|
40
|
|
|
$newFilename = uniqid().'_iddoc.jpg'; |
|
41
|
|
|
$fileNamesToUpdate[] = $newFilename; |
|
42
|
|
|
|
|
43
|
|
|
$imgIddoc->move($userDirName, $newFilename); |
|
44
|
|
|
|
|
45
|
|
|
$log = new Log(); |
|
46
|
|
|
$log |
|
47
|
|
|
->setExercise($exercise) |
|
48
|
|
|
->setLevel(-1) |
|
49
|
|
|
->setImageFilename($newFilename) |
|
50
|
|
|
; |
|
51
|
|
|
|
|
52
|
|
|
$this->em->persist($log); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($imgLearner) { |
|
|
|
|
|
|
56
|
|
|
$newFilename = uniqid().'_learner.jpg'; |
|
57
|
|
|
$fileNamesToUpdate[] = $newFilename; |
|
58
|
|
|
|
|
59
|
|
|
$imgLearner->move($userDirName, $newFilename); |
|
60
|
|
|
|
|
61
|
|
|
$log = new Log(); |
|
62
|
|
|
$log |
|
63
|
|
|
->setExercise($exercise) |
|
64
|
|
|
->setLevel(-1) |
|
65
|
|
|
->setImageFilename($newFilename) |
|
66
|
|
|
; |
|
67
|
|
|
|
|
68
|
|
|
$this->em->persist($log); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->em->flush(); |
|
72
|
|
|
|
|
73
|
|
|
ChamiloSession::write($this->plugin->get_name().'_orphan_snapshots', $fileNamesToUpdate); |
|
74
|
|
|
|
|
75
|
|
|
return HttpResponse::create(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function createDirectory(): string |
|
79
|
|
|
{ |
|
80
|
|
|
$user = api_get_user_entity(api_get_user_id()); |
|
81
|
|
|
|
|
82
|
|
|
$pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring'; |
|
83
|
|
|
$userDirName = $pluginDirName.'/'.$user->getId(); |
|
84
|
|
|
|
|
85
|
|
|
$fs = new Filesystem(); |
|
86
|
|
|
$fs->mkdir( |
|
87
|
|
|
[$pluginDirName, $userDirName], |
|
88
|
|
|
api_get_permissions_for_new_directories() |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
return $userDirName; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: