ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjFileBasedLM.php
Go to the documentation of this file.
1<?php
2
23
29{
30 private \ILIAS\ResourceStorage\Services $irss;
31 private \ILIAS\Filesystem\Util\Archive\Archives $archives;
32 protected ?string $start_file = null;
33 protected ?string $rid = null;
34 protected bool $online;
35
36 public function __construct(
37 int $a_id = 0,
38 bool $a_call_by_reference = true
39 ) {
40 global $DIC;
41
42
43 $this->db = $DIC->database();
44 $this->irss = $DIC->resourceStorage();
45 $this->archives = $DIC->archives();
46 // this also calls read() method! (if $a_id is set)
47 $this->type = "htlm";
48 parent::__construct($a_id, $a_call_by_reference);
49 }
50
51 public function update(bool $a_skip_meta = false): bool
52 {
53 if (!$a_skip_meta) {
54 $this->updateMetaData();
55 }
56 parent::update();
57
58 $this->db->update(
59 'file_based_lm',
60 [
61 'startfile' => ['text', $this->getStartFile()],
62 'rid' => ['text', $this->getRID() ?? '']
63 ],
64 ['id' => ['integer', $this->getId()]]
65 );
66
67 return true;
68 }
69
70 public function read(): void
71 {
72 parent::read();
73
74 $q = "SELECT * FROM file_based_lm WHERE id = " . $this->db->quote($this->getId(), "integer");
75 $lm_set = $this->db->query($q);
76 $lm_rec = $this->db->fetchAssoc($lm_set);
77 $this->setStartFile((string) ($lm_rec["startfile"] ?? ''));
78 $this->setRID((string) ($lm_rec["rid"] ?? ''));
79 }
80
81 public function create(bool $a_skip_meta = false): int
82 {
83 $id = parent::create();
84
85 // create empty container resource. empty zips are not allowed, we need at least one file which is hidden
86 $empty_zip = $this->archives->zip(
87 []
88 );
89
90 $rid = $this->irss->manageContainer()->containerFromStream(
91 $empty_zip->get(),
93 $this->getTitle()
94 );
95 $this->setRID($rid->serialize());
96
97 $this->db->insert(
98 'file_based_lm',
99 [
100 'id' => ['integer', $this->getId()],
101 'startfile' => ['text', $this->getStartFile()],
102 'rid' => ['text', $this->getRID()]
103 ]
104 );
105
106
107 if (!$a_skip_meta) {
108 $this->createMetaData();
109 }
110 return $id;
111 }
112
113 public function maybeDetermineStartFile(): bool
114 {
115 $valid_start_files = ["index.htm", "index.html", "start.htm", "start.html"];
117 $resource = $this->getResource();
118 if ($resource !== null) {
119 $zip = $this->irss->consume()->containerZIP($resource->getIdentification())->getZIP();
120 foreach ($zip->getFiles() as $file) {
121 if (in_array(basename($file), $valid_start_files, true)) {
122 $this->setStartFile($file);
123 $this->update();
124 return true;
125 }
126 }
127 }
128 return false;
129 }
130
131 public function getDataDirectory(string $mode = "filesystem"): string
132 {
133 return CLIENT_WEB_DIR . "/lm_data" . "/lm_" . $this->getId();
134 }
135
136 public function createDataDirectory(): void
137 {
138 //
139 }
140
141 public function getStartFile(): ?string
142 {
143 return $this->start_file;
144 }
145
146 public function setStartFile(
147 string $a_file,
148 bool $a_omit_file_check = false
149 ): void {
150 $this->start_file = $a_file;
151 }
152
153 public function getRID(): ?string
154 {
155 return $this->rid;
156 }
157
159 {
160 if ($this->getRID() === null) {
161 return null;
162 }
163 $rid = $this->irss->manage()->find($this->getRID());
164 if ($rid === null) {
165 return null;
166 }
167 return $this->irss->manage()->getResource($rid);
168 }
169
170 public function setRID(string $rid): void
171 {
172 $this->rid = $rid;
173 }
174
175
176 public function delete(): bool
177 {
178 // always call parent delete function first!!
179 if (!parent::delete()) {
180 return false;
181 }
182
183 // Delete meta data
184 $this->deleteMetaData();
185
186 // delete file_based_lm record
187 $this->db->manipulateF(
188 "DELETE FROM file_based_lm WHERE id = %s",
189 ["integer"],
190 [$this->getId()]
191 );
192
193 // delete data directory
194 ilFileUtils::delDir($this->getDataDirectory()); // for legacy reasons
195 // TODO remove RID
196
197 return true;
198 }
199
206 public function populateByDirectoy(
207 string $a_dir,
208 string $a_filename = ""
209 ): void {
210 preg_match("/.*htlm_([0-9]*)\.zip/", $a_filename, $match);
211 if (is_dir($a_dir . "/htlm_" . ($match[1] ?? ""))) {
212 $a_dir .= "/htlm_" . ($match[1] ?? "");
213 }
214 ilFileUtils::rCopy($a_dir, $this->getDataDirectory());
215 ilFileUtils::renameExecutables($this->getDataDirectory());
216 }
217
218 public function cloneObject(int $target_id, int $copy_id = 0, bool $omit_tree = false): ?ilObject
219 {
221 $new_obj = parent::cloneObject($target_id, $copy_id, $omit_tree);
222 $this->cloneMetaData($new_obj);
223
224 //copy online status if object is not the root copy object
225 $cp_options = ilCopyWizardOptions::_getInstance($copy_id);
226
227 if (!$cp_options->isRootNode($this->getRefId())) {
228 $new_obj->setOfflineStatus($this->getOfflineStatus());
229 } else {
230 $new_obj->setOfflineStatus(true);
231 }
232
233 // copy content
234 $new_obj->populateByDirectoy($this->getDataDirectory());
235
236 $new_obj->setStartFile((string) $this->getStartFile());
237 $new_obj->update();
238
239 return $new_obj;
240 }
241
242 public function isInfoEnabled(): bool
243 {
245 }
246}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static _getInstance(int $a_copy_id)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static renameExecutables(string $a_dir)
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
File Based Learning Module (HTML) object.
setStartFile(string $a_file, bool $a_omit_file_check=false)
ILIAS ResourceStorage Services $irss
__construct(int $a_id=0, bool $a_call_by_reference=true)
getDataDirectory(string $mode="filesystem")
create(bool $a_skip_meta=false)
update(bool $a_skip_meta=false)
ILIAS Filesystem Util Archive Archives $archives
populateByDirectoy(string $a_dir, string $a_filename="")
Populate by directory.
Class ilObject Basic functions for all objects.
const CLIENT_WEB_DIR
Definition: constants.php:47
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:23
$lm_set