ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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 $amount_of_downloads = $this->safeSprintf(
107 $this->getLanguage()->txt("amount_of_downloads_since"),
108 $this->getFileObj()->getAmountOfDownloads(),
109 $this->getFileObj()->getCreateDate(),
110 );
111
112 }
113
114 return [
115 $this->getLanguage()->txt("mime_type") => $this->getFileObj()->getFileType(),
116 $this->getLanguage()->txt("resource_id") => $this->getFileObj()->getResourceId(),
117 $this->getLanguage()->txt("storage_id") => $this->getFileObj()->getStorageID(),
118 $this->getLanguage()->txt("amount_of_downloads") => $amount_of_downloads
119 ];
120 }
121
128 public function getAllFileInfoForCurrentUser(): array
129 {
130 $file_info = [];
131 $access_handler = $this->getAccessHandler();
132
133 if ($access_handler instanceof ilWorkspaceAccessHandler) {
134 return []; // ilWorkspaceAccessHandler has a different checkAccessOfUser method
135 }
136
137 if ($access_handler->checkAccessOfUser(
138 $this->getUser()->getId(),
139 "read",
140 "",
141 $this->getFileObj()->getRefId()
142 )) {
143 $file_info['for_users'] = $this->getFileInfoForUsers();
144 $file_info['for_all'] = $this->getFileInfoForAll();
145 }
146 if ($access_handler->checkAccessOfUser(
147 $this->getUser()->getId(),
148 "write",
149 "",
150 $this->getFileObj()->getRefId()
151 )) {
152 $file_info['for_authors_and_admins'] = $this->getFileInfoForAuthorsAndAdmins();
153 }
154
155 return $file_info;
156 }
157
158 abstract protected function getAccessHandler(); // must be compatible with ilObject2GUI::getAccessHandler()
159
160 abstract protected function getCtrl(): ilCtrl;
161
162 abstract protected function getFileObj(): ?ilObjFile;
163
164 abstract protected function getFileStakeholder(): ilObjFileStakeholder;
165
166 abstract protected function getGeneralSettings(): General;
167
168 abstract protected function getLanguage(): ilLanguage;
169
170 abstract protected function getNodeID(): int;
171
172 abstract protected function getUser(): ilObjUser;
173
177 private function safeSprintf(string $format, mixed ...$args): string
178 {
179 try {
180 return sprintf($format, ...$args);
181 } catch (\ValueError $e) {
182 $fixed = preg_replace('/%(?!([0-9]+\$)?[bcdeEfFgGosuxX%])/', '%%', $format);
183 try {
184 return sprintf($fixed, ...$args);
185 } catch (\ValueError $e2) {
186 return $format;
187 }
188 }
189 }
190
191}
$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.
@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()