ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
trait.ilObjFileInfoProvider.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
28{
32 public function getFileInfoForUsers(): array
33 {
34 // page count
35 $page_count = $this->getFileObj()->getPageCount();
36 $page_count = ($page_count !== 0) ? $page_count : null;
37 // preview
38 $preview_renderer = new ilObjFilePreviewRendererGUI($this->getFileObj()->getId());
39 $preview = null;
40 if ($preview_renderer->has() && !$this->getCtrl()->isAsynch()) {
41 $preview = $preview_renderer->getRenderedTriggerComponents(true);
42 }
43
44 $data_size = new DataSize(
45 $this->getFileObj()->getFileSize(),
46 DataSize::KB
47 );
48
49 return [
50 $this->getLanguage()->txt("filename") => $this->getFileObj()->getFileName(),
51 $this->getLanguage()->txt("type") => $this->getFileObj()->getFileExtension(),
52 $this->getLanguage()->txt("size") => (string) $data_size,
53 $this->getLanguage()->txt("page_count") => $page_count,
54 $this->getLanguage()->txt("preview") => $preview
55 ];
56 }
57
64 public function getFileInfoForAll(): array
65 {
66 // version number
67 $version_nr = $this->getFileObj()->getVersion();
68 // version date
69 $version = $this->getFileObj()->getVersions([$version_nr]);
70 $version = end($version);
71 $version_date = null;
72 if ($version instanceof ilObjFileVersion) {
73 $version_date = (new ilDateTime($version->getDate(), IL_CAL_DATETIME))->get(IL_CAL_DATETIME);
74 }
75 // version uploader
76 $version_uploader = $version["user_id"] ?? -1; // unknown uploader
77 // download link
78 $download_link_tpl = null;
79 if ($this->getAccessHandler()->checkAccessOfUser(
80 $this->getUser()->getId(),
81 "read",
82 "sendfile",
83 $this->getFileObj()->getRefId()
84 )) {
85 $download_link_tpl = new ilTemplate("tpl.download_link.html", true, true, "components/ILIAS/File");
86 $download_link_tpl->setVariable("LINK", ilObjFileAccess::_getPermanentDownloadLink($this->getNodeID()));
87 }
88
89 return [
90 $this->getLanguage()->txt("version") => $version_nr,
91 $this->getLanguage()->txt("version_uploaded") => $version_date,
92 $this->getLanguage()->txt("file_uploaded_by") => ilUserUtil::getNamePresentation($version_uploader),
93 $this->getLanguage()->txt("download_link") => $download_link_tpl->get(),
94 $this->getLanguage()->txt("download_link") => $download_link_tpl->get(),
95 ];
96 }
97
101 public function getFileInfoForAuthorsAndAdmins(): array
102 {
103 $amount_of_downloads = null;
104
105 if ($this->getGeneralSettings()->isShowAmountOfDownloads()) {
106 $create_date = ilDatePresentation::formatDate(
107 new ilDateTime(
108 $this->getFileObj()->getCreateDate(),
110 'utc' // most likely the server timezone
111 )
112 );
113
114 $amount_of_downloads = $this->safeSprintf(
115 $this->getLanguage()->txt("amount_of_downloads_since"),
116 $this->getFileObj()->getAmountOfDownloads(),
117 $create_date,
118 );
119 }
120
121 return [
122 $this->getLanguage()->txt("mime_type") => $this->getFileObj()->getFileType(),
123 $this->getLanguage()->txt("resource_id") => $this->getFileObj()->getResourceId(),
124 $this->getLanguage()->txt("storage_id") => $this->getFileObj()->getStorageID(),
125 $this->getLanguage()->txt("amount_of_downloads") => $amount_of_downloads
126 ];
127 }
128
135 public function getAllFileInfoForCurrentUser(): array
136 {
137 $file_info = [];
138 $access_handler = $this->getAccessHandler();
139
140 if ($access_handler instanceof ilWorkspaceAccessHandler) {
141 return []; // ilWorkspaceAccessHandler has a different checkAccessOfUser method
142 }
143
144 if ($access_handler->checkAccessOfUser(
145 $this->getUser()->getId(),
146 "read",
147 "",
148 $this->getFileObj()->getRefId()
149 )) {
150 $file_info['for_users'] = $this->getFileInfoForUsers();
151 $file_info['for_all'] = $this->getFileInfoForAll();
152 }
153 if ($access_handler->checkAccessOfUser(
154 $this->getUser()->getId(),
155 "write",
156 "",
157 $this->getFileObj()->getRefId()
158 )) {
159 $file_info['for_authors_and_admins'] = $this->getFileInfoForAuthorsAndAdmins();
160 }
161
162 return $file_info;
163 }
164
165 abstract protected function getAccessHandler(); // must be compatible with ilObject2GUI::getAccessHandler()
166
167 abstract protected function getCtrl(): ilCtrl;
168
169 abstract protected function getFileObj(): ?ilObjFile;
170
171 abstract protected function getFileStakeholder(): ilObjFileStakeholder;
172
173 abstract protected function getGeneralSettings(): General;
174
175 abstract protected function getLanguage(): ilLanguage;
176
177 abstract protected function getNodeID(): int;
178
179 abstract protected function getUser(): ilObjUser;
180
184 private function safeSprintf(string $format, mixed ...$args): string
185 {
186 try {
187 return sprintf($format, ...$args);
188 } catch (\ValueError $e) {
189 $fixed = preg_replace('/%(?!([0-9]+\$)?[bcdeEfFgGosuxX%])/', '%%', $format);
190 try {
191 return sprintf($fixed, ...$args);
192 } catch (\ValueError $e2) {
193 return $format;
194 }
195 }
196 }
197
198}
$version
Definition: plugin.php:24
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:31
const IL_CAL_DATETIME
Class ilCtrl provides processing control methods.
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
@classDescription Date and time handling
language handling
static _getPermanentDownloadLink(int $ref_id)
Gets the permanent download link for the file.
Class ilObjFileStakeholder.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilObjFile.
User class.
special template class to simplify handling of ITX/PEAR
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path=null)
Default behaviour is:
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getCtrl()
safeSprintf(string $format, mixed ... $args)
Robust sprintf: tolerates broken translation placeholders.
getFileStakeholder()
getNodeID()
getFileObj()
getGeneralSettings()
getUser()
trait ilObjFileInfoProvider
getLanguage()