ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilDownloadFilesBackgroundTask.php
Go to the documentation of this file.
1 <?php
2 
4 use \ILIAS\BackgroundTasks\Implementation\Values\ScalarValues\StringValue;
5 
6 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
7 
15 {
19  private $logger = null;
20 
24  protected $user_id;
25 
26 
30  protected $user = null;
31 
35  protected $task_factory = null;
36 
40  private $events = [];
41 
46  protected $bucket_title;
47 
52  protected $has_files = false;
53 
58  public function __construct($a_usr_id)
59  {
60  global $DIC;
61  $this->logger = $DIC->logger()->cal();
62  $this->user_id = $a_usr_id;
63  $this->task_factory = $DIC->backgroundTasks()->taskFactory();
64  $this->lng = $DIC->language();
65 
66  $this->user = \ilObjectFactory::getInstanceByObjId($a_usr_id, false);
67  }
68 
73  public function setEvents(array $a_events)
74  {
75  $this->events = $a_events;
76  }
77 
82  public function getEvents()
83  {
84  return $this->events;
85  }
86 
91  public function setBucketTitle($a_title)
92  {
93  $this->bucket_title = $a_title;
94  }
95 
100  public function getBucketTitle()
101  {
102  //TODO: fix ilUtil zip stuff
103  // Error If name starts "-"
104  // error massage from ilUtil->execQuoted = ["","zip error: Invalid command arguments (short option 'a' not supported)"]
105  if (substr($this->bucket_title, 0, 1) === "-") {
106  $this->bucket_title = ltrim($this->bucket_title, "-");
107  }
108 
109  return $this->bucket_title;
110  }
111 
116  public function run()
117  {
118  $definition = new ilCalendarCopyDefinition();
119  $normalized_name = ilUtil::getASCIIFilename($this->getBucketTitle());
120  $definition->setTempDir($normalized_name);
121 
122  $this->collectFiles($definition);
123 
124  if (!$this->has_files) {
125  ilUtil::sendInfo($this->lng->txt("cal_down_no_files"), true);
126  return false;
127  }
128 
129  $bucket = new BasicBucket();
130  $bucket->setUserId($this->user_id);
131 
132  // move files from source dir to target directory
133  $copy_job = $this->task_factory->createTask(ilCalendarCopyFilesToTempDirectoryJob::class, [$definition]);
134  $zip_job = $this->task_factory->createTask(ilCalendarZipJob::class, [$copy_job]);
135 
136  $download_name = new StringValue();
137 
138  $this->logger->debug("Normalized name = " . $normalized_name);
139  $download_name->setValue($normalized_name . '.zip');
140 
141  $download_interaction = $this->task_factory->createTask(ilCalendarDownloadZipInteraction::class, [$zip_job, $download_name]);
142 
143  // last task to bucket
144  $bucket->setTask($download_interaction);
145 
146  $bucket->setTitle($this->getBucketTitle());
147 
148  $task_manager = $GLOBALS['DIC']->backgroundTasks()->taskManager();
149  $task_manager->run($bucket);
150  return true;
151  }
152 
157  {
158  //filter here the objects, don't repeat the object Id
159  $object_ids = array();
160  foreach ($this->getEvents() as $event) {
161  $start = new ilDateTime($event['dstart'], IL_CAL_UNIX);
162  $cat = ilCalendarCategory::getInstanceByCategoryId($event['category_id']);
163  $obj_id = $cat->getObjId();
164 
165  //22295 If the object type is exc then we need all the assignments.Otherwise we will get only one.
166  if (!in_array($obj_id, $object_ids) || $cat->getObjType() == "exc") {
167  $object_ids[] = $obj_id;
168 
169  $folder_date = $start->get(IL_CAL_FKT_DATE, 'Y-m-d', $this->user->getTimeZone());
170 
171 
172  if ($event['fullday']) {
173  $folder_app = ilUtil::getASCIIFilename($event['event']->getPresentationTitle(false)); //title formalized
174  } else {
175  $start_time = $start->get(IL_CAL_FKT_DATE, 'H.i', $this->user->getTimeZone());
176 
177  $end = new ilDateTime($event['dend'], IL_CAL_UNIX);
178  $end_time = $end->get(IL_CAL_FKT_DATE, 'H.i', $this->user->getTimeZone());
179 
180  if ($start_time != $end_time) {
181  $start_time .= (' - ' . $end_time);
182  }
183  $folder_app = $start_time . ' ' .
184  ilUtil::getASCIIFilename($event['event']->getPresentationTitle(false)); //title formalized
185  }
186 
187  $this->logger->debug("collecting files...event title = " . $folder_app);
188 
189  $file_handler = ilAppointmentFileHandlerFactory::getInstance($event);
190 
191  if ($files = $file_handler->getFiles()) {
192  $this->has_files = true;
193  }
194  //if file_system_path is set, it is the real path of the file (courses use ids as names file->getId())
195  //otherwise $file_with_absolut_path is the path. ($file->getName())
196  foreach ($files as $file_system_path => $file_with_absolut_path) {
197  #22198 check if the key is a string defined by ILIAS or a number set by PHP as a sequential key
198  //[/Sites/data/client/ilCourse/2/crs_xx/info/1] => /Sites/data/client/ilCourse/2/crs_xxx/info/image.png
199  //[0] => /Sites/data/client/ilFile/3/file_3xx/001/image.png
200  if (is_string($file_system_path)) {
201  $file_with_absolut_path = $file_system_path;
202  $file_id = (int) basename($file_system_path);
203  $basename = $this->getEventFileNameFromId($event['event'], $file_id);
204  } else {
205  $basename = ilUtil::getASCIIFilename(basename($file_with_absolut_path));
206  }
207  $def->addCopyDefinition(
208  $file_with_absolut_path,
209  $folder_date . '/' . $folder_app . '/' . $basename
210  );
211 
212  $this->logger->debug('Added new copy definition: ' . $folder_date . '/' . $folder_app . '/' . $basename . ' -> ' . $file_with_absolut_path);
213  }
214  }
215  }
216  }
217 
223  private function getEventFileNameFromId(ilCalendarEntry $a_event, $a_file_id)
224  {
225  $filename = "";
228  $cat_type = $cat->getType();
229  $obj_id = $cat->getObjId();
230  $obj_type = ilObject::_lookupType($obj_id);
231 
232  if ($cat_type == ilCalendarCategory::TYPE_OBJ && $obj_type == "crs") {
233  $course_file = new ilCourseFile((int) $a_file_id);
234  $filename = $course_file->getFileName();
235  }
237  }
238 }
static _lookupCategory($a_cal_id)
Lookup category id.
Model for a calendar entry.
getEventFileNameFromId(ilCalendarEntry $a_event, $a_file_id)
Only courses store the files using the id for naming.
$files
Definition: metarefresh.php:49
global $DIC
Definition: saml.php:7
getEntryId()
get entry id
const IL_CAL_UNIX
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
user()
Definition: user.php:4
$start
Definition: bench.php:8
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
addCopyDefinition($a_source, $a_target)
Add copy definition.
collectFiles(ilCalendarCopyDefinition $def)
Collect files.
const IL_CAL_FKT_DATE
Date and time handling
static getInstanceByCategoryId($a_cat_id)
Get instance by category id.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static _lookupType($a_id, $a_reference=false)
lookup object type
$filename
Definition: buildRTE.php:89
$def
Definition: croninfo.php:21
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.