ILIAS  release_8 Revision v8.24
class.ilAssFileUploadUploadsExporter.php
Go to the documentation of this file.
1<?php
2
26{
27 public const ZIP_FILE_MIME_TYPE = 'application/zip';
28 public const ZIP_FILE_EXTENSION = '.zip';
29
33 protected $db;
34
38 protected $lng;
39
43 protected $refId;
44
48 private $testId;
49
53 private $testTitle;
54
58 private $question;
59
64
69
73 private $tempDirPath;
74
79
84 {
85 $this->db = $db;
86 $this->lng = $lng;
87 }
88
92 public function getRefId(): int
93 {
94 return $this->refId;
95 }
96
100 public function setRefId($refId): void
101 {
102 $this->refId = $refId;
103 }
104
108 public function getTestId(): int
109 {
110 return $this->testId;
111 }
112
116 public function setTestId($testId): void
117 {
118 $this->testId = $testId;
119 }
120
124 public function getTestTitle(): string
125 {
126 return $this->testTitle;
127 }
128
132 public function setTestTitle($testTitle): void
133 {
134 $this->testTitle = $testTitle;
135 }
136
141 {
142 return $this->question;
143 }
144
148 public function setQuestion($question): void
149 {
150 $this->question = $question;
151 }
152
153 public function build(): void
154 {
155 $this->initFilenames();
156
157 $solutionData = $this->getFileUploadSolutionData();
158
159 $participantData = $this->getParticipantData($solutionData);
160
161 $this->collectUploadedFiles($solutionData, $participantData);
162
164
166 }
167
168 private function initFilenames(): void
169 {
170 $this->tempDirPath = ilFileUtils::ilTempnam();
171
172 $this->tempZipFilePath = ilFileUtils::ilTempnam($this->tempDirPath) . self::ZIP_FILE_EXTENSION;
173
174 $this->mainFolderName = ilFileUtils::getASCIIFilename(
175 str_replace(' ', '', $this->getTestTitle() . '_' . $this->question->getTitle())
176 );
177 }
178
179 private function getFileUploadSolutionData(): array
180 {
181 $query = "
182 SELECT tst_solutions.solution_id, tst_solutions.pass, tst_solutions.active_fi, tst_solutions.question_fi,
183 tst_solutions.value1, tst_solutions.value2, tst_solutions.tstamp
184 FROM tst_solutions, tst_active, qpl_questions
185 WHERE tst_solutions.active_fi = tst_active.active_id
186 AND tst_solutions.question_fi = qpl_questions.question_id
187 AND tst_solutions.question_fi = %s
188 AND tst_active.test_fi = %s
189 ORDER BY tst_solutions.active_fi, tst_solutions.tstamp
190 ";
191
192 $res = $this->db->queryF(
193 $query,
194 array("integer", "integer"),
195 array($this->question->getId(), $this->getTestId())
196 );
197
198 $solutionData = array();
199
200 while ($row = $this->db->fetchAssoc($res)) {
201 if (!isset($solutionData[$row['active_fi']])) {
202 $solutionData[ $row['active_fi'] ] = array();
203 }
204
205 if (!isset($solutionData[ $row['active_fi'] ][ $row['pass'] ])) {
206 $solutionData[ $row['active_fi'] ][ $row['pass'] ] = array();
207 }
208
209 $solutionData[ $row['active_fi'] ][ $row['pass'] ][] = $row;
210 }
211
212 return $solutionData;
213 }
214
215 private function getParticipantData($solutionData): ilTestParticipantData
216 {
217 $activeIds = array();
218
219 foreach ($solutionData as $activeId => $passes) {
220 $activeIds[] = $activeId;
221 }
222
223 require_once 'Modules/Test/classes/class.ilTestParticipantData.php';
224 $participantData = new ilTestParticipantData($this->db, $this->lng);
225 $participantData->setActiveIdsFilter($activeIds);
226 $participantData->setParticipantAccessFilter(
228 );
229 $participantData->load($this->getTestId());
230
231 return $participantData;
232 }
233
234 private function collectUploadedFiles($solutionData, ilTestParticipantData $participantData): void
235 {
236 foreach ($solutionData as $activeId => $passes) {
237 if (!in_array($activeId, $participantData->getActiveIds())) {
238 continue;
239 }
240
241 foreach ($passes as $pass => $files) {
242 foreach ($files as $file) {
243 $uploadedFileDir = $this->question->getFileUploadPath(
244 $this->getTestId(),
245 $activeId,
246 $this->question->getId()
247 );
248
249 // #20317
250 if (!is_file($uploadedFileDir . $file['value1'])) {
251 continue;
252 }
253
254 $destinationDir = $this->tempDirPath . '/' . $this->mainFolderName . '/';
255 $destinationDir .= $participantData->getFileSystemCompliantFullnameByActiveId($activeId) . '/';
256 $destinationDir .= $this->getPassSubDirName($file['pass']) . '/';
257
258 ilFileUtils::makeDirParents($destinationDir);
259
260 copy($uploadedFileDir . $file['value1'], $destinationDir . $file['value2']);
261 }
262 }
263 }
264 }
265
266 private function getPassSubDirName($pass): string
267 {
268 return $this->lng->txt('pass') . '_' . ($pass + 1);
269 }
270
271 private function createFileUploadCollectionZipFile(): void
272 {
273 ilFileUtils::zip($this->tempDirPath . '/' . $this->mainFolderName, $this->tempZipFilePath);
274
275 $pathinfo = pathinfo($this->tempZipFilePath);
276 $this->finalZipFilePath = dirname($pathinfo['dirname']) . '/' . $pathinfo['basename'];
277
278 try {
279 ilFileUtils::rename($this->tempZipFilePath, $this->finalZipFilePath);
280 } catch (\ilFileUtilsException $e) {
281 \ilLoggerFactory::getRootLogger()->error($e->getMessage());
282 }
283 }
284
285 private function removeFileUploadCollection(): void
286 {
287 ilFileUtils::delDir($this->tempDirPath);
288 }
289
290 public function getFinalZipFilePath(): string
291 {
293 }
294
295 public function getDispoZipFileName(): string
296 {
298 $this->mainFolderName . self::ZIP_FILE_EXTENSION
299 );
300 }
301
302 public function getZipFileMimeType(): string
303 {
305 }
306}
__construct(ilDBInterface $db, ilLanguage $lng)
collectUploadedFiles($solutionData, ilTestParticipantData $participantData)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static getASCIIFilename(string $a_filename)
static zip(string $a_dir, string $a_file, bool $compress_content=false)
zips given directory/file into given zip.file
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static rename(string $a_source, string $a_target)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
language handling
static getRootLogger()
The unique root logger has a fixed error level.
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$res
Definition: ltiservices.php:69
$query