ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilAssFileUploadUploadsExporter.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
12{
13 const ZIP_FILE_MIME_TYPE = 'application/zip';
14 const ZIP_FILE_EXTENSION = '.zip';
15
19 protected $db;
20
24 protected $lng;
25
29 private $testId;
30
34 private $testTitle;
35
39 private $question;
40
45
50
54 private $tempDirPath;
55
60
65 {
66 $this->db = $db;
67 $this->lng = $lng;
68 }
69
73 public function getTestId()
74 {
75 return $this->testId;
76 }
77
81 public function setTestId($testId)
82 {
83 $this->testId = $testId;
84 }
85
89 public function getTestTitle()
90 {
91 return $this->testTitle;
92 }
93
97 public function setTestTitle($testTitle)
98 {
99 $this->testTitle = $testTitle;
100 }
101
105 public function getQuestion()
106 {
107 return $this->question;
108 }
109
113 public function setQuestion($question)
114 {
115 $this->question = $question;
116 }
117
121 public function build()
122 {
123 $this->initFilenames();
124
125 $solutionData = $this->getFileUploadSolutionData();
126
127 $participantData = $this->getParticipantData($solutionData);
128
129 $this->collectUploadedFiles($solutionData, $participantData);
130
132
134 }
135
136 private function initFilenames()
137 {
138 $this->tempDirPath = ilUtil::ilTempnam();
139
140 $this->tempZipFilePath = ilUtil::ilTempnam($this->tempDirPath) . self::ZIP_FILE_EXTENSION;
141
142 $this->mainFolderName = ilUtil::getASCIIFilename(
143 str_replace(' ', '', $this->getTestTitle() . '_' . $this->question->getTitle())
144 );
145 }
146
147 private function getFileUploadSolutionData()
148 {
149 $query = "
150 SELECT tst_solutions.solution_id, tst_solutions.pass, tst_solutions.active_fi, tst_solutions.question_fi,
151 tst_solutions.value1, tst_solutions.value2, tst_solutions.tstamp
152 FROM tst_solutions, tst_active, qpl_questions
153 WHERE tst_solutions.active_fi = tst_active.active_id
154 AND tst_solutions.question_fi = qpl_questions.question_id
155 AND tst_solutions.question_fi = %s
156 AND tst_active.test_fi = %s
157 ORDER BY tst_solutions.active_fi, tst_solutions.tstamp
158 ";
159
160 $res = $this->db->queryF(
161 $query,
162 array("integer", "integer"),
163 array($this->question->getId(), $this->getTestId())
164 );
165
166 $solutionData = array();
167
168 while ($row = $this->db->fetchAssoc($res)) {
169 if (!isset($solutionData[$row['active_fi']])) {
170 $solutionData[ $row['active_fi'] ] = array();
171 }
172
173 if (!isset($solutionData[ $row['active_fi'] ][ $row['pass'] ])) {
174 $solutionData[ $row['active_fi'] ][ $row['pass'] ] = array();
175 }
176
177 $solutionData[ $row['active_fi'] ][ $row['pass'] ][] = $row;
178 }
179
180 return $solutionData;
181 }
182
183 private function getParticipantData($solutionData)
184 {
185 $activeIds = array();
186
187 foreach ($solutionData as $activeId => $passes) {
188 $activeIds[] = $activeId;
189 }
190
191 require_once 'Modules/Test/classes/class.ilTestParticipantData.php';
192 $participantData = new ilTestParticipantData($this->db, $this->lng);
193 $participantData->setActiveIds($activeIds);
194 $participantData->load($this->getTestId());
195
196 return $participantData;
197 }
198
199 private function collectUploadedFiles($solutionData, ilTestParticipantData $participantData)
200 {
201 foreach ($solutionData as $activeId => $passes) {
202 foreach ($passes as $pass => $files) {
203 foreach ($files as $file) {
204 $uploadedFileDir = $this->question->getFileUploadPath(
205 $this->getTestId(),
206 $activeId,
207 $this->question->getId()
208 );
209
210 // #20317
211 if (!is_file($uploadedFileDir . $file['value1'])) {
212 continue;
213 }
214
215 $destinationDir = $this->tempDirPath . '/' . $this->mainFolderName . '/';
216 $destinationDir .= $participantData->getFileSystemCompliantFullnameByActiveId($activeId) . '/';
217 $destinationDir .= $this->getPassSubDirName($file['pass']) . '/';
218
219 ilUtil::makeDirParents($destinationDir);
220
221 copy($uploadedFileDir . $file['value1'], $destinationDir . $file['value2']);
222 }
223 }
224 }
225 }
226
227 private function getPassSubDirName($pass)
228 {
229 return $this->lng->txt('pass') . '_' . ($pass + 1);
230 }
231
233 {
234 ilUtil::zip($this->tempDirPath . '/' . $this->mainFolderName, $this->tempZipFilePath);
235
236 $pathinfo = pathinfo($this->tempZipFilePath);
237 $this->finalZipFilePath = dirname($pathinfo['dirname']) . '/' . $pathinfo['basename'];
238
239 try {
240 require_once 'Services/Utilities/classes/class.ilFileUtils.php';
241 ilFileUtils::rename($this->tempZipFilePath, $this->finalZipFilePath);
242 } catch (\ilFileUtilsException $e) {
243 \ilLoggerFactory::getRootLogger()->error($e->getMessage());
244 }
245 }
246
247 private function removeFileUploadCollection()
248 {
249 ilUtil::delDir($this->tempDirPath);
250 }
251
252 public function getFinalZipFilePath()
253 {
255 }
256
257 public function getDispoZipFileName()
258 {
260 $this->mainFolderName . self::ZIP_FILE_EXTENSION
261 );
262 }
263
264 public function getZipFileMimeType()
265 {
267 }
268}
$files
Definition: add-vimline.php:18
An exception for terminatinating execution or to throw for unit testing.
__construct(ilDBInterface $db, ilLanguage $lng)
collectUploadedFiles($solutionData, ilTestParticipantData $participantData)
Class to report exception.
static rename($a_source, $a_target)
Rename a file.
language handling
static getRootLogger()
The unique root logger has a fixed error level.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static makeDirParents($a_dir)
Create a new directory and all parent directories.
Interface ilDBInterface.
$query
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
foreach($_POST as $key=> $value) $res