ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  protected $refId;
30 
34  private $testId;
35 
39  private $testTitle;
40 
44  private $question;
45 
50 
55 
59  private $tempDirPath;
60 
64  private $mainFolderName;
65 
70  {
71  $this->db = $db;
72  $this->lng = $lng;
73  }
74 
78  public function getRefId()
79  {
80  return $this->refId;
81  }
82 
86  public function setRefId($refId)
87  {
88  $this->refId = $refId;
89  }
90 
94  public function getTestId()
95  {
96  return $this->testId;
97  }
98 
102  public function setTestId($testId)
103  {
104  $this->testId = $testId;
105  }
106 
110  public function getTestTitle()
111  {
112  return $this->testTitle;
113  }
114 
118  public function setTestTitle($testTitle)
119  {
120  $this->testTitle = $testTitle;
121  }
122 
126  public function getQuestion()
127  {
128  return $this->question;
129  }
130 
134  public function setQuestion($question)
135  {
136  $this->question = $question;
137  }
138 
142  public function build()
143  {
144  $this->initFilenames();
145 
146  $solutionData = $this->getFileUploadSolutionData();
147 
148  $participantData = $this->getParticipantData($solutionData);
149 
150  $this->collectUploadedFiles($solutionData, $participantData);
151 
153 
155  }
156 
157  private function initFilenames()
158  {
159  $this->tempDirPath = ilUtil::ilTempnam();
160 
161  $this->tempZipFilePath = ilUtil::ilTempnam($this->tempDirPath) . self::ZIP_FILE_EXTENSION;
162 
163  $this->mainFolderName = ilUtil::getASCIIFilename(
164  str_replace(' ', '', $this->getTestTitle() . '_' . $this->question->getTitle())
165  );
166  }
167 
168  private function getFileUploadSolutionData()
169  {
170  $query = "
171  SELECT tst_solutions.solution_id, tst_solutions.pass, tst_solutions.active_fi, tst_solutions.question_fi,
172  tst_solutions.value1, tst_solutions.value2, tst_solutions.tstamp
173  FROM tst_solutions, tst_active, qpl_questions
174  WHERE tst_solutions.active_fi = tst_active.active_id
175  AND tst_solutions.question_fi = qpl_questions.question_id
176  AND tst_solutions.question_fi = %s
177  AND tst_active.test_fi = %s
178  ORDER BY tst_solutions.active_fi, tst_solutions.tstamp
179  ";
180 
181  $res = $this->db->queryF(
182  $query,
183  array("integer", "integer"),
184  array($this->question->getId(), $this->getTestId())
185  );
186 
187  $solutionData = array();
188 
189  while ($row = $this->db->fetchAssoc($res)) {
190  if (!isset($solutionData[$row['active_fi']])) {
191  $solutionData[ $row['active_fi'] ] = array();
192  }
193 
194  if (!isset($solutionData[ $row['active_fi'] ][ $row['pass'] ])) {
195  $solutionData[ $row['active_fi'] ][ $row['pass'] ] = array();
196  }
197 
198  $solutionData[ $row['active_fi'] ][ $row['pass'] ][] = $row;
199  }
200 
201  return $solutionData;
202  }
203 
204  private function getParticipantData($solutionData)
205  {
206  $activeIds = array();
207 
208  foreach ($solutionData as $activeId => $passes) {
209  $activeIds[] = $activeId;
210  }
211 
212  require_once 'Modules/Test/classes/class.ilTestParticipantData.php';
213  $participantData = new ilTestParticipantData($this->db, $this->lng);
214  $participantData->setActiveIdsFilter($activeIds);
215  $participantData->setParticipantAccessFilter(
217  );
218  $participantData->load($this->getTestId());
219 
220  return $participantData;
221  }
222 
223  private function collectUploadedFiles($solutionData, ilTestParticipantData $participantData)
224  {
225  foreach ($solutionData as $activeId => $passes) {
226  if (!in_array($activeId, $participantData->getActiveIds())) {
227  continue;
228  }
229 
230  foreach ($passes as $pass => $files) {
231  foreach ($files as $file) {
232  $uploadedFileDir = $this->question->getFileUploadPath(
233  $this->getTestId(),
234  $activeId,
235  $this->question->getId()
236  );
237 
238  // #20317
239  if (!is_file($uploadedFileDir . $file['value1'])) {
240  continue;
241  }
242 
243  $destinationDir = $this->tempDirPath . '/' . $this->mainFolderName . '/';
244  $destinationDir .= $participantData->getFileSystemCompliantFullnameByActiveId($activeId) . '/';
245  $destinationDir .= $this->getPassSubDirName($file['pass']) . '/';
246 
247  ilUtil::makeDirParents($destinationDir);
248 
249  copy($uploadedFileDir . $file['value1'], $destinationDir . $file['value2']);
250  }
251  }
252  }
253  }
254 
255  private function getPassSubDirName($pass)
256  {
257  return $this->lng->txt('pass') . '_' . ($pass + 1);
258  }
259 
261  {
262  ilUtil::zip($this->tempDirPath . '/' . $this->mainFolderName, $this->tempZipFilePath);
263 
264  $pathinfo = pathinfo($this->tempZipFilePath);
265  $this->finalZipFilePath = dirname($pathinfo['dirname']) . '/' . $pathinfo['basename'];
266 
267  try {
268  require_once 'Services/Utilities/classes/class.ilFileUtils.php';
269  ilFileUtils::rename($this->tempZipFilePath, $this->finalZipFilePath);
270  } catch (\ilFileUtilsException $e) {
271  \ilLoggerFactory::getRootLogger()->error($e->getMessage());
272  }
273  }
274 
275  private function removeFileUploadCollection()
276  {
277  ilUtil::delDir($this->tempDirPath);
278  }
279 
280  public function getFinalZipFilePath()
281  {
283  }
284 
285  public function getDispoZipFileName()
286  {
288  $this->mainFolderName . self::ZIP_FILE_EXTENSION
289  );
290  }
291 
292  public function getZipFileMimeType()
293  {
294  return self::ZIP_FILE_MIME_TYPE;
295  }
296 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
$files
Definition: metarefresh.php:49
collectUploadedFiles($solutionData, ilTestParticipantData $participantData)
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static rename($a_source, $a_target)
Rename a file.
foreach($_POST as $key=> $value) $res
__construct(ilDBInterface $db, ilLanguage $lng)
$query
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
$row
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
language handling
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static getRootLogger()
The unique root logger has a fixed error level.
Class to report exception.