ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilDownloadFilesBackgroundTask.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24
30{
32 protected ilLanguage $lng;
33 protected ?ilObjUser $user;
34 protected TaskFactory $task_factory;
35
36 private int $user_id;
37 private array $events = [];
38 private string $bucket_title;
39 private bool $has_files = false;
40 private \ilGlobalTemplateInterface $main_tpl;
41
47 public function __construct($a_usr_id)
48 {
49 global $DIC;
50 $this->main_tpl = $DIC->ui()->mainTemplate();
51
52 $this->logger = $DIC->logger()->cal();
53 $this->user_id = $a_usr_id;
54 $this->task_factory = $DIC->backgroundTasks()->taskFactory();
55 $this->lng = $DIC->language();
56
58 if (!$user instanceof ilObjUser) {
59 throw new DomainException('Invalid or deleted user id given: ' . $this->user_id);
60 }
61 $this->user = $user;
62 }
63
64 public function setEvents(array $a_events): void
65 {
66 $this->events = $a_events;
67 }
68
69 public function getEvents(): array
70 {
71 return $this->events;
72 }
73
74 public function setBucketTitle(string $a_title): void
75 {
76 $this->bucket_title = $a_title;
77 }
78
83 public function getBucketTitle(): string
84 {
85 //TODO: fix ilUtil zip stuff
86 // Error If name starts "-"
87 // error massage from ilUtil->execQuoted = ["","zip error: Invalid command arguments (short option 'a' not supported)"]
88 if (substr($this->bucket_title, 0, 1) === "-") {
89 $this->bucket_title = ltrim($this->bucket_title, "-");
90 }
92 }
93
94 public function run(): bool
95 {
97 $normalized_name = ilFileUtils::getASCIIFilename($this->getBucketTitle());
98 $definition->setTempDir($normalized_name);
99
100 $this->collectFiles($definition);
101
102 if (!$this->has_files) {
103 $this->main_tpl->setOnScreenMessage('info', $this->lng->txt("cal_down_no_files"), true);
104 return false;
105 }
106
107 $bucket = new BasicBucket();
108 $bucket->setUserId($this->user_id);
109
110 // move files from source dir to target directory
111 $copy_job = $this->task_factory->createTask(ilCalendarCopyFilesToTempDirectoryJob::class, [$definition]);
112 $zip_job = $this->task_factory->createTask(ilCalendarZipJob::class, [$copy_job]);
113
114 $download_name = new StringValue();
115
116 $this->logger->debug("Normalized name = " . $normalized_name);
117 $download_name->setValue($normalized_name . '.zip');
118
119 $download_interaction = $this->task_factory->createTask(
120 ilCalendarDownloadZipInteraction::class,
121 [$zip_job, $download_name]
122 );
123
124 // last task to bucket
125 $bucket->setTask($download_interaction);
126
127 $bucket->setTitle($this->getBucketTitle());
128
129 $task_manager = $GLOBALS['DIC']->backgroundTasks()->taskManager();
130 $task_manager->run($bucket);
131 return true;
132 }
133
135 {
136 //filter here the objects, don't repeat the object Id
137 $object_ids = [];
138 foreach ($this->getEvents() as $event) {
139 $start = new ilDateTime($event['dstart'], IL_CAL_UNIX);
140 $cat = ilCalendarCategory::getInstanceByCategoryId($event['category_id']);
141 $obj_id = $cat->getObjId();
142
143 $this->logger->debug('Handling event: ' . $event['event']->getPresentationTitle());
144 //22295 If the object type is exc then we need all the assignments.Otherwise we will get only one.
145 if (
146 $cat->getType() != \ilCalendarCategory::TYPE_OBJ ||
147 $cat->getObjType() == 'exc' ||
148 !in_array($obj_id, $object_ids)
149 ) {
150 $this->logger->debug('New obj_id..');
151 $object_ids[] = $obj_id;
152
153 $folder_date = $start->get(IL_CAL_FKT_DATE, 'Y-m-d', $this->user->getTimeZone());
154
155 if ($event['fullday']) {
156 $folder_app = ilFileUtils::getASCIIFilename($event['event']->getPresentationTitle(false)); //title formalized
157 } else {
158 $start_time = $start->get(IL_CAL_FKT_DATE, 'H.i', $this->user->getTimeZone());
159
160 $end = new ilDateTime($event['dend'], IL_CAL_UNIX);
161 $end_time = $end->get(IL_CAL_FKT_DATE, 'H.i', $this->user->getTimeZone());
162
163 if ($start_time != $end_time) {
164 $start_time .= (' - ' . $end_time);
165 }
166 $folder_app = $start_time . ' ' .
167 ilFileUtils::getASCIIFilename($event['event']->getPresentationTitle(false)); //title formalized
168 }
169
170 $this->logger->debug("collecting files...event title = " . $folder_app);
171 $file_handler = ilAppointmentFileHandlerFactory::getInstance($event);
172 $this->logger->debug('Current file handler: ' . get_class($file_handler));
173
174 if ($files = $file_handler->getFiles()) {
175 $this->has_files = true;
176 }
177
178 $this->logger->dump($files);
179 foreach ($files as $idx => $file_property) {
180 $this->logger->debug('Filename:' . $file_property->getFileName());
181 $ressource = '';
182 $target = $folder_date . '/' . $folder_app . '/' . $file_property->getFileName();
183 if (!is_null($file_property->getFileRId())) {
184 $this->logger->debug('Ressource Id: ' . $file_property->getFileRId());
185 $ressource = $file_property->getFileRId();
186 $def->addCopyDefinitionRId($file_property->getFileRId(), $target);
187 } else {
188 $this->logger->debug('Absolute Path: ' . $file_property->getAbsolutePath());
189 $ressource = $file_property->getAbsolutePath();
190 $def->addCopyDefinition($file_property->getAbsolutePath(), $target);
191 }
192 $this->logger->debug(
193 'Added new copy definition: ' .
194 $folder_date . '/' . $folder_app . '/' . $file_property->getFileName() . ' => ' .
195 $ressource
196 );
197 }
198 } else {
199 $this->logger->info('Ignoring obj_id: ' . $obj_id . ' already processed.');
200 }
201 }
202 }
203}
const IL_CAL_UNIX
const IL_CAL_FKT_DATE
static getInstanceByCategoryId(int $a_cat_id)
addCopyDefinition(string $a_path, string $a_target)
Add copy definition.
@classDescription Date and time handling
__construct($a_usr_id)
ilDownloadFilesBackgroundTask constructor.
collectFiles(ilCalendarRessourceStorageCopyDefinition $def)
static getASCIIFilename(string $a_filename)
language handling
Component logger with individual log levels by component id.
User class.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54