ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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, array("integer", "integer"), array($this->question->getId(), $this->getTestId())
162 );
163
164 $solutionData = array();
165
166 while( $row = $this->db->fetchAssoc($res) )
167 {
168 if( !isset($solutionData[$row['active_fi']]) )
169 {
170 $solutionData[ $row['active_fi'] ] = array();
171 }
172
173 if( !isset($solutionData[ $row['active_fi'] ][ $row['pass'] ]) )
174 {
175 $solutionData[ $row['active_fi'] ][ $row['pass'] ] = array();
176 }
177
178 $solutionData[ $row['active_fi'] ][ $row['pass'] ][] = $row;
179 }
180
181 return $solutionData;
182 }
183
184 private function getParticipantData($solutionData)
185 {
186 $activeIds = array();
187
188 foreach($solutionData as $activeId => $passes)
189 {
190 $activeIds[] = $activeId;
191 }
192
193 require_once 'Modules/Test/classes/class.ilTestParticipantData.php';
194 $participantData = new ilTestParticipantData($this->db, $this->lng);
195 $participantData->setActiveIds($activeIds);
196 $participantData->load($this->getTestId());
197
198 return $participantData;
199 }
200
201 private function collectUploadedFiles($solutionData, ilTestParticipantData $participantData)
202 {
203 foreach($solutionData as $activeId => $passes)
204 {
205 foreach($passes as $pass => $files)
206 {
207 foreach($files as $file)
208 {
209 $uploadedFileDir = $this->question->getFileUploadPath(
210 $this->getTestId(), $activeId, $this->question->getId()
211 );
212
213 $destinationDir = $this->tempDirPath.'/'.$this->mainFolderName.'/';
214 $destinationDir .= $participantData->getFileSystemCompliantFullnameByActiveId($activeId).'/';
215 $destinationDir .= $this->getPassSubDirName($file['pass']).'/';
216
217 ilUtil::makeDirParents($destinationDir);
218
219 copy($uploadedFileDir.$file['value1'], $destinationDir.$file['value2']);
220 }
221 }
222 }
223 }
224
225 private function getPassSubDirName($pass)
226 {
227 return $this->lng->txt('pass').'_'.($pass + 1);
228 }
229
231 {
232 ilUtil::zip($this->tempDirPath.'/'.$this->mainFolderName, $this->tempZipFilePath);
233
234 $pathinfo = pathinfo($this->tempZipFilePath);
235 $this->finalZipFilePath = dirname($pathinfo['dirname']).'/'.$pathinfo['basename'];
236
237 try
238 {
239 require_once 'Services/Utilities/classes/class.ilFileUtils.php';
240 ilFileUtils::rename($this->tempZipFilePath, $this->finalZipFilePath);
241 }
242 catch (\ilFileUtilsException $e)
243 {
244 \ilLoggerFactory::getRootLogger()->error($e->getMessage());
245 }
246 }
247
248 private function removeFileUploadCollection()
249 {
250 ilUtil::delDir($this->tempDirPath);
251 }
252
253 public function getFinalZipFilePath()
254 {
256 }
257
258 public function getDispoZipFileName()
259 {
261 $this->mainFolderName.self::ZIP_FILE_EXTENSION
262 );
263 }
264
265 public function getZipFileMimeType()
266 {
268 }
269}
print $file
collectUploadedFiles($solutionData, ilTestParticipantData $participantData)
Database Wrapper.
Definition: class.ilDB.php:29
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)
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.