ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
29  protected $task_factory = null;
30 
34  private $events = [];
35 
40  protected $bucket_title;
41 
46  protected $has_files = false;
47 
52  public function __construct($a_usr_id)
53  {
54  global $DIC;
55  $this->logger = $DIC->logger()->cal();
56  $this->user_id = $a_usr_id;
57  $this->task_factory = $DIC->backgroundTasks()->taskFactory();
58  $this->lng = $DIC->language();
59  }
60 
65  public function setEvents(array $a_events)
66  {
67  $this->events = $a_events;
68  }
69 
74  public function getEvents()
75  {
76  return $this->events;
77  }
78 
83  public function setBucketTitle($a_title)
84  {
85  $this->bucket_title = $a_title;
86  }
87 
92  public function getBucketTitle()
93  {
94  //TODO: fix ilUtil zip stuff
95  // Error If name starts "-"
96  // error massage from ilUtil->execQuoted = ["","zip error: Invalid command arguments (short option 'a' not supported)"]
97  if (substr($this->bucket_title, 0, 1) === "-") {
98  $this->bucket_title = ltrim($this->bucket_title, "-");
99  }
100 
101  return $this->bucket_title;
102  }
103 
108  public function run()
109  {
110  $definition = new ilCalendarCopyDefinition();
111  $normalized_name = ilUtil::getASCIIFilename($this->getBucketTitle());
112  $definition->setTempDir($normalized_name);
113 
114  $this->collectFiles($definition);
115 
116  if (!$this->has_files) {
117  ilUtil::sendInfo($this->lng->txt("cal_down_no_files"), true);
118  return false;
119  }
120 
121  $bucket = new BasicBucket();
122  $bucket->setUserId($this->user_id);
123 
124  // move files from source dir to target directory
125  $copy_job = $this->task_factory->createTask(ilCalendarCopyFilesToTempDirectoryJob::class, [$definition]);
126  $zip_job = $this->task_factory->createTask(ilCalendarZipJob::class, [$copy_job]);
127 
128  $download_name = new StringValue();
129 
130  $this->logger->debug("Normalized name = " . $normalized_name);
131  $download_name->setValue($normalized_name . '.zip');
132 
133  $download_interaction = $this->task_factory->createTask(ilCalendarDownloadZipInteraction::class, [$zip_job, $download_name]);
134 
135  // last task to bucket
136  $bucket->setTask($download_interaction);
137 
138  $bucket->setTitle($this->getBucketTitle());
139 
140  $task_manager = $GLOBALS['DIC']->backgroundTasks()->taskManager();
141  $task_manager->run($bucket);
142  return true;
143  }
144 
149  {
150  //filter here the objects, don't repeat the object Id
151  $object_ids = array();
152  foreach ($this->getEvents() as $event) {
153  $cat = ilCalendarCategory::getInstanceByCategoryId($event['category_id']);
154  $obj_id = $cat->getObjId();
155 
156  //22295 If the object type is exc then we need all the assignments.Otherwise we will get only one.
157  if (!in_array($obj_id, $object_ids) || $cat->getObjType() == "exc") {
158  $object_ids[] = $obj_id;
159  $folder_date = $event['event']->getStart()->get(IL_CAL_FKT_DATE, 'Y-m-d');
160 
161  if ($event['fullday']) {
162  $folder_app = ilUtil::getASCIIFilename($event['event']->getPresentationTitle(false)); //title formalized
163  } else {
164  $time = $event['event']->getStart()->get(IL_CAL_FKT_DATE, 'H.i');
165  $end_time = $event['event']->getEnd()->get(IL_CAL_FKT_DATE, 'H.i');
166  if ($time != $end_time) {
167  $time .= " - " . $end_time;
168  }
169  $folder_app = $time . " " . ilUtil::getASCIIFilename($event['event']->getPresentationTitle(false)); //title formalized
170  }
171 
172  $this->logger->debug("collecting files...event title = " . $folder_app);
173 
174  $file_handler = ilAppointmentFileHandlerFactory::getInstance($event);
175 
176  if ($files = $file_handler->getFiles()) {
177  $this->has_files = true;
178  }
179  //if file_system_path is set, it is the real path of the file (courses use ids as names file->getId())
180  //otherwise $file_with_absolut_path is the path. ($file->getName())
181  foreach ($files as $file_system_path => $file_with_absolut_path) {
182  #22198 check if the key is a string defined by ILIAS or a number set by PHP as a sequential key
183  //[/Sites/data/client/ilCourse/2/crs_xx/info/1] => /Sites/data/client/ilCourse/2/crs_xxx/info/image.png
184  //[0] => /Sites/data/client/ilFile/3/file_3xx/001/image.png
185  if (is_string($file_system_path)) {
186  $file_with_absolut_path = $file_system_path;
187  $file_id = (int) basename($file_system_path);
188  $basename = $this->getEventFileNameFromId($event['event'], $file_id);
189  } else {
190  $basename = ilUtil::getASCIIFilename(basename($file_with_absolut_path));
191  }
192  $def->addCopyDefinition(
193  $file_with_absolut_path,
194  $folder_date . '/' . $folder_app . '/' . $basename
195  );
196 
197  $this->logger->debug('Added new copy definition: ' . $folder_date . '/' . $folder_app . '/' . $basename . ' -> ' . $file_with_absolut_path);
198  }
199  }
200  }
201  }
202 
208  private function getEventFileNameFromId(ilCalendarEntry $a_event, $a_file_id)
209  {
210  $filename = "";
213  $cat_type = $cat->getType();
214  $obj_id = $cat->getObjId();
215  $obj_type = ilObject::_lookupType($obj_id);
216 
217  if ($cat_type == ilCalendarCategory::TYPE_OBJ && $obj_type == "crs") {
218  $course_file = new ilCourseFile((int) $a_file_id);
219  $filename = $course_file->getFileName();
220  }
222  }
223 }
$files
Definition: add-vimline.php:18
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.
global $DIC
Definition: saml.php:7
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
getEntryId()
get entry id
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
$time
Definition: cron.php:21
addCopyDefinition($a_source, $a_target)
Add copy definition.
collectFiles(ilCalendarCopyDefinition $def)
Collect files.
const IL_CAL_FKT_DATE
static getInstanceByCategoryId($a_cat_id)
Get instance by category id.
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
$def
Definition: croninfo.php:21