ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilFileDataCourse.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=0);
25{
26 private string $course_path;
27 private int $course_id;
28
30
34 public function __construct(int $a_course_id)
35 {
36 global $DIC;
37
38 $this->error = $DIC['ilErr'];
39
40 define('COURSE_PATH', 'course');
41
43 $this->course_path = parent::getPath() . "/" . COURSE_PATH;
44 $this->course_id = $a_course_id;
45
46 if (!$this->__checkPath()) {
47 $this->__initDirectory();
48 }
49 $this->__checkImportPath();
50 }
51
52 public function getArchiveFile($a_rel_name): string
53 {
54 if (file_exists($this->course_path . '/' . $a_rel_name . '.zip')) {
55 return $this->course_path . '/' . $a_rel_name . '.zip';
56 }
57 if (file_exists($this->course_path . '/' . $a_rel_name . '.pdf')) {
58 return $this->course_path . '/' . $a_rel_name . '.pdf';
59 }
60 return '';
61 }
62
63 public function getMemberExportFiles(): array
64 {
65 $files = array();
66 $dp = opendir($this->course_path);
67
68 while ($file = readdir($dp)) {
69 if (is_dir($file)) {
70 continue;
71 }
72
73 if (preg_match(
74 "/^([0-9]{10})_[a-zA-Z]*_export_([a-z]+)_([0-9]+)\.[a-z]+$/",
75 $file,
76 $matches
77 ) && $matches[3] == $this->course_id) {
78 $timest = $matches[1];
79 $file_info['name'] = $matches[0];
80 $file_info['timest'] = $matches[1];
81 $file_info['type'] = $matches[2];
82 $file_info['id'] = $matches[3];
83 $file_info['size'] = filesize($this->course_path . '/' . $file);
84
85 $files[$timest] = $file_info;
86 }
87 }
88 closedir($dp);
89 return $files;
90 }
91
92 public function deleteMemberExportFile(string $a_name): void
93 {
94 $file_name = $this->course_path . '/' . $a_name;
95 if (file_exists($file_name)) {
96 unlink($file_name);
97 }
98 }
99
100 public function getMemberExportFile(string $a_name): string
101 {
102 $file_name = $this->course_path . '/' . $a_name;
103 if (file_exists($file_name)) {
104 return file_get_contents($file_name);
105 }
106 return '';
107 }
108
109 public function deleteArchive(string $a_rel_name): void
110 {
111 $this->deleteZipFile($this->course_path . '/' . $a_rel_name . '.zip');
112 $this->deleteDirectory($this->course_path . '/' . $a_rel_name);
113 $this->deleteDirectory(CLIENT_WEB_DIR . '/courses/' . $a_rel_name);
114 $this->deletePdf($this->course_path . '/' . $a_rel_name . '.pdf');
115 }
116
117 public function deleteZipFile(string $a_abs_name): bool
118 {
119 if (file_exists($a_abs_name)) {
120 unlink($a_abs_name);
121 return true;
122 }
123 return false;
124 }
125
126 public function deleteDirectory(string $a_abs_name): bool
127 {
128 if (file_exists($a_abs_name)) {
129 ilFileUtils::delDir($a_abs_name);
130
131 return true;
132 }
133 return false;
134 }
135
136 public function deletePdf(string $a_abs_name): bool
137 {
138 if (file_exists($a_abs_name)) {
139 unlink($a_abs_name);
140
141 return true;
142 }
143 return false;
144 }
145
146 public function copy(string $a_from, string $a_to): bool
147 {
148 if (file_exists($a_from)) {
149 copy($a_from, $this->getCoursePath() . '/' . $a_to);
150
151 return true;
152 }
153 return false;
154 }
155
156 public function rCopy(string $a_from, string $a_to): bool
157 {
158 ilFileUtils::rCopy($a_from, $this->getCoursePath() . '/' . $a_to);
159 return true;
160 }
161
162 public function addDirectory(string $a_rel_name): bool
163 {
164 ilFileUtils::makeDir($this->getCoursePath() . '/' . $a_rel_name);
165 return true;
166 }
167
168 public function writeToFile(string $a_data, string $a_rel_name): bool
169 {
170 if (!$fp = fopen($this->getCoursePath() . '/' . $a_rel_name, 'w+')) {
171 die("Cannot open file: " . $this->getCoursePath() . '/' . $a_rel_name);
172 }
173 fwrite($fp, $a_data);
174 return true;
175 }
176
177 public function zipFile(string $a_rel_name, string $a_zip_name): int
178 {
179 ilFileUtils::zip($this->getCoursePath() . '/' . $a_rel_name, $this->getCoursePath() . '/' . $a_zip_name);
180
181 // RETURN filesize
182 return (int) filesize($this->getCoursePath() . '/' . $a_zip_name);
183 }
184
185 public function getCoursePath(): string
186 {
187 return $this->course_path;
188 }
189
190 public function createOnlineVersion(string $a_rel_name): bool
191 {
192 ilFileUtils::makeDir(CLIENT_WEB_DIR . '/courses/' . $a_rel_name);
193 ilFileUtils::rCopy($this->getCoursePath() . '/' . $a_rel_name, CLIENT_WEB_DIR . '/courses/' . $a_rel_name);
194 return true;
195 }
196
197 public function getOnlineLink(string $a_rel_name): string
198 {
199 return ilFileUtils::getWebspaceDir('filesystem') . '/courses/' . $a_rel_name . '/index.html';
200 }
201
202 public function __checkPath(): bool
203 {
204 if (!file_exists($this->getCoursePath())) {
205 return false;
206 }
207 if (!file_exists(CLIENT_WEB_DIR . '/courses')) {
209 }
210
211 $this->__checkReadWrite();
212
213 return true;
214 }
215
216 public function __checkImportPath(): void
217 {
218 if (!file_exists($this->getCoursePath() . '/import')) {
219 ilFileUtils::makeDir($this->getCoursePath() . '/import');
220 }
221
222 if (!is_writable($this->getCoursePath() . '/import') || !is_readable($this->getCoursePath() . '/import')) {
223 $this->error->raiseError("Course import path is not readable/writable by webserver", $this->error->FATAL);
224 }
225 }
226
227 public function __checkReadWrite(): bool
228 {
229 if (is_writable($this->course_path) && is_readable($this->course_path)) {
230 return true;
231 } else {
232 $this->error->raiseError("Course directory is not readable/writable by webserver", $this->error->FATAL);
233 }
234 return false;
235 }
236
237 public function __initDirectory(): bool
238 {
239 if (is_writable($this->getPath())) {
240 ilFileUtils::makeDir($this->getPath() . '/' . COURSE_PATH);
241 $this->course_path = $this->getPath() . '/' . COURSE_PATH;
242 return true;
243 }
244 return false;
245 }
246}
error(string $a_errmsg)
Error Handling & global info handling.
This class handles all operations of archive files for the course object.
deleteZipFile(string $a_abs_name)
deleteMemberExportFile(string $a_name)
deleteDirectory(string $a_abs_name)
zipFile(string $a_rel_name, string $a_zip_name)
createOnlineVersion(string $a_rel_name)
copy(string $a_from, string $a_to)
rCopy(string $a_from, string $a_to)
getOnlineLink(string $a_rel_name)
writeToFile(string $a_data, string $a_rel_name)
addDirectory(string $a_rel_name)
deletePdf(string $a_abs_name)
deleteArchive(string $a_rel_name)
getMemberExportFile(string $a_name)
__construct(int $a_course_id)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getWebspaceDir(string $mode="filesystem")
get webspace directory
static zip(string $a_dir, string $a_file, bool $compress_content=false)
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
const CLIENT_WEB_DIR
Definition: constants.php:47
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26