ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
59  private $mainFolderName;
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  // #20317
214  if(!is_file($uploadedFileDir . $file['value1']))
215  {
216  continue;
217  }
218 
219  $destinationDir = $this->tempDirPath.'/'.$this->mainFolderName.'/';
220  $destinationDir .= $participantData->getFileSystemCompliantFullnameByActiveId($activeId).'/';
221  $destinationDir .= $this->getPassSubDirName($file['pass']).'/';
222 
223  ilUtil::makeDirParents($destinationDir);
224 
225  copy($uploadedFileDir.$file['value1'], $destinationDir.$file['value2']);
226  }
227  }
228  }
229  }
230 
231  private function getPassSubDirName($pass)
232  {
233  return $this->lng->txt('pass').'_'.($pass + 1);
234  }
235 
237  {
238  ilUtil::zip($this->tempDirPath.'/'.$this->mainFolderName, $this->tempZipFilePath);
239 
240  $pathinfo = pathinfo($this->tempZipFilePath);
241  $this->finalZipFilePath = dirname($pathinfo['dirname']).'/'.$pathinfo['basename'];
242 
243  try
244  {
245  require_once 'Services/Utilities/classes/class.ilFileUtils.php';
246  ilFileUtils::rename($this->tempZipFilePath, $this->finalZipFilePath);
247  }
248  catch (\ilFileUtilsException $e)
249  {
250  \ilLoggerFactory::getRootLogger()->error($e->getMessage());
251  }
252  }
253 
254  private function removeFileUploadCollection()
255  {
256  ilUtil::delDir($this->tempDirPath);
257  }
258 
259  public function getFinalZipFilePath()
260  {
262  }
263 
264  public function getDispoZipFileName()
265  {
267  $this->mainFolderName.self::ZIP_FILE_EXTENSION
268  );
269  }
270 
271  public function getZipFileMimeType()
272  {
273  return self::ZIP_FILE_MIME_TYPE;
274  }
275 }
$files
Definition: add-vimline.php:18
static makeDirParents($a_dir)
Create a new directory and all parent directories.
collectUploadedFiles($solutionData, ilTestParticipantData $participantData)
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static rename($a_source, $a_target)
Rename a file.
Interface ilDBInterface.
__construct(ilDBInterface $db, ilLanguage $lng)
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
Create styles array
The data for the language used.
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
language handling
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
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.