ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilVerificationObject.php
Go to the documentation of this file.
1<?php
2
24abstract class ilVerificationObject extends ilObject2
25{
26 protected array $map = array();
27 protected array $properties = array();
28
29 public const TYPE_STRING = 1;
30 public const TYPE_BOOL = 2;
31 public const TYPE_INT = 3;
32 public const TYPE_DATE = 4;
33 public const TYPE_RAW = 5;
34 public const TYPE_ARRAY = 6;
35
36 public function __construct(
37 int $a_id = 0,
38 bool $a_reference = true
39 ) {
40 global $DIC;
41
42 $this->db = $DIC->database();
43 $this->map = $this->getPropertyMap();
44 parent::__construct($a_id, $a_reference);
45 }
46
50 abstract protected function getPropertyMap(): array;
51
55 public function hasProperty(string $a_name): bool
56 {
57 return array_key_exists($a_name, $this->map);
58 }
59
63 public function getPropertyType(string $a_name): ?int
64 {
65 if ($this->hasProperty($a_name)) {
66 return (int) $this->map[$a_name];
67 }
68 return null;
69 }
70
75 public function getProperty(string $a_name)
76 {
77 if ($this->hasProperty($a_name)) {
78 return $this->properties[$a_name];
79 }
80 return null;
81 }
82
87 public function setProperty(string $a_name, $a_value): void
88 {
89 if ($this->hasProperty($a_name)) {
90 $this->properties[$a_name] = $a_value;
91 }
92 }
93
101 protected function importProperty(
102 string $a_type,
103 $a_data = null,
104 ?string $a_raw_data = null
105 ): void {
106 $data_type = $this->getPropertyType($a_type);
107 if ($data_type) {
108 $value = null;
109
110 switch ($data_type) {
112 $value = (string) $a_data;
113 break;
114
115 case self::TYPE_BOOL:
116 $value = (bool) $a_data;
117 break;
118
119 case self::TYPE_INT:
120 $value = (int) $a_data;
121 break;
122
123 case self::TYPE_DATE:
124 $value = new ilDate($a_data, IL_CAL_DATE);
125 break;
126
127 case self::TYPE_ARRAY:
128 if ($a_data) {
129 $value = unserialize($a_data, ['allowed_classes' => false]);
130 }
131 break;
132
133 case self::TYPE_RAW:
134 $value = $a_raw_data;
135 break;
136 }
137
138 $this->setProperty($a_type, $value);
139 }
140 }
141
147 protected function exportProperty(string $a_name): ?array
148 {
149 $data_type = $this->getPropertyType($a_name);
150 if ($data_type) {
151 $value = $this->getProperty($a_name);
152 $raw_data = null;
153
154 switch ($data_type) {
155 case self::TYPE_DATE:
156 if ($value) {
157 $value = $value->get(IL_CAL_DATE);
158 }
159 break;
160
161 case self::TYPE_ARRAY:
162 if ($value) {
163 $value = serialize($value);
164 }
165 break;
166
167 case self::TYPE_RAW:
168 $raw_data = $value;
169 $value = null;
170 break;
171 }
172
173 return array("parameters" => $value,
174 "raw_data" => $raw_data);
175 }
176 return null;
177 }
178
179 protected function doRead(): void
180 {
181 $ilDB = $this->db;
182
183 if ($this->id) {
184 $set = $ilDB->query("SELECT * FROM il_verification" .
185 " WHERE id = " . $ilDB->quote($this->id, "integer"));
186 if ($ilDB->numRows($set)) {
187 while ($row = $ilDB->fetchAssoc($set)) {
188 $this->importProperty(
189 $row["type"],
190 $row["parameters"],
191 $row["raw_data"]
192 );
193 }
194 }
195 }
196 }
197
198 protected function doCreate(bool $clone_mode = false): void
199 {
200 $this->saveProperties();
201 }
202
203 protected function doUpdate(): void
204 {
205 $this->saveProperties();
206 }
207
211 protected function saveProperties(): bool
212 {
213 $ilDB = $this->db;
214
215 if ($this->id) {
216 // remove all existing properties
217 $ilDB->manipulate("DELETE FROM il_verification" .
218 " WHERE id = " . $ilDB->quote($this->id, "integer"));
219
220 foreach ($this->getPropertyMap() as $name => $type) {
221 $property = $this->exportProperty($name);
222
223 $fields = array("id" => array("integer", $this->id),
224 "type" => array("text", $name),
225 "parameters" => array("text", $property["parameters"]),
226 "raw_data" => array("text", $property["raw_data"]));
227
228 $ilDB->insert("il_verification", $fields);
229 }
230
231 $this->handleQuotaUpdate();
232
233 return true;
234 }
235 return false;
236 }
237
238 protected function doDelete(): void
239 {
240 $ilDB = $this->db;
241
242 if ($this->id) {
243 // remove all files
244 $storage = new ilVerificationStorageFile($this->id);
245 $storage->delete();
246
247 $this->handleQuotaUpdate();
248
249 $ilDB->manipulate("DELETE FROM il_verification" .
250 " WHERE id = " . $ilDB->quote($this->id, "integer"));
251 }
252 }
253
254 public static function initStorage(int $a_id, ?string $a_subdir = null): string
255 {
256 $storage = new ilVerificationStorageFile($a_id);
257 $storage->create();
258
259 $path = $storage->getAbsolutePath() . "/";
260
261 if ($a_subdir) {
262 $path .= $a_subdir . "/";
263
264 if (!is_dir($path)) {
265 mkdir($path);
266 }
267 }
268
269 return $path;
270 }
271
272 public function getFilePath(): string
273 {
274 $file = $this->getProperty("file");
275 if ($file) {
276 $path = self::initStorage($this->getId(), "certificate");
277 return $path . $file;
278 }
279 return "";
280 }
281
282 public function getOfflineFilename(): string
283 {
284 return ilFileUtils::getASCIIFilename($this->getTitle()) . ".pdf";
285 }
286
287 protected function handleQuotaUpdate(): void
288 {
289 }
290}
const IL_CAL_DATE
Class for single dates.
static getASCIIFilename(string $a_filename)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
hasProperty(string $a_name)
Check if given property is valid.
getProperty(string $a_name)
Get property value.
static initStorage(int $a_id, ?string $a_subdir=null)
exportProperty(string $a_name)
Export property to database.
saveProperties()
Save current properties to database.
__construct(int $a_id=0, bool $a_reference=true)
Constructor.
getPropertyMap()
Return property map (name => type)
importProperty(string $a_type, $a_data=null, ?string $a_raw_data=null)
Import property from database.
getPropertyType(string $a_name)
Get property data type.
setProperty(string $a_name, $a_value)
Set property value.
doCreate(bool $clone_mode=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26