ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilBadge.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
15 protected $lng;
16
20 protected $db;
21
22 protected $id; // [int]
23 protected $parent_id; // [int]
24 protected $type_id; // [string]
25 protected $active; // [bool]
26 protected $title; // [string]
27 protected $desc; // [string]
28 protected $image; // [string]
29 protected $valid; // [string]
30 protected $config; // [array]
31 protected $criteria; // [string]
32
39 public function __construct($a_id = null)
40 {
41 global $DIC;
42
43 $this->lng = $DIC->language();
44 $this->db = $DIC->database();
45 if ($a_id) {
46 $this->read($a_id);
47 }
48 }
49
50 public static function getInstancesByParentId($a_parent_id, array $a_filter = null)
51 {
52 global $DIC;
53
54 $ilDB = $DIC->database();
55
56 $res = array();
57
58 $sql = "SELECT * FROM badge_badge" .
59 " WHERE parent_id = " . $ilDB->quote($a_parent_id);
60
61 if ($a_filter) {
62 if ($a_filter["title"]) {
63 $sql .= " AND " . $ilDB->like("title", "text", "%" . trim($a_filter["title"]) . "%");
64 }
65 if ($a_filter["type"]) {
66 $sql .= " AND type_id = " . $ilDB->quote($a_filter["type"], "integer");
67 }
68 }
69
70 $set = $ilDB->query($sql .
71 " ORDER BY title");
72 while ($row = $ilDB->fetchAssoc($set)) {
73 $obj = new self();
74 $obj->importDBRow($row);
75 $res[] = $obj;
76 }
77
78 return $res;
79 }
80
81 public static function getInstancesByType($a_type_id)
82 {
83 global $DIC;
84
85 $ilDB = $DIC->database();
86
87 $res = array();
88
89 $set = $ilDB->query("SELECT * FROM badge_badge" .
90 " WHERE type_id = " . $ilDB->quote($a_type_id) .
91 " ORDER BY title");
92 while ($row = $ilDB->fetchAssoc($set)) {
93 $obj = new self();
94 $obj->importDBRow($row);
95 $res[] = $obj;
96 }
97
98 return $res;
99 }
100
101 public function getTypeInstance()
102 {
103 if ($this->getTypeId()) {
104 $handler = ilBadgeHandler::getInstance();
105 return $handler->getTypeInstanceByUniqueId($this->getTypeId());
106 }
107 }
108
109 public function copy($a_new_parent_id)
110 {
112
113 $this->setTitle($this->getTitle() . " " . $lng->txt("copy_of_suffix"));
114 $this->setParentId($a_new_parent_id);
115 $this->setActive(false);
116
117 if ($this->getId()) {
118 $img = $this->getImagePath();
119
120 $this->setId(null);
121 $this->create();
122
123 if ($img) {
124 // see uploadImage()
125 copy($img, $this->getImagePath());
126 }
127 }
128 }
129
130 public static function getObjectInstances(array $a_filter = null)
131 {
132 global $DIC;
133
134 $ilDB = $DIC->database();
135
136 $res = $raw = array();
137
138 $where = "";
139
140 if ($a_filter["type"]) {
141 $where .= " AND bb.type_id = " . $ilDB->quote($a_filter["type"], "text");
142 }
143 if ($a_filter["title"]) {
144 $where .= " AND " . $ilDB->like("bb.title", "text", "%" . $a_filter["title"] . "%");
145 }
146 if ($a_filter["object"]) {
147 $where .= " AND " . $ilDB->like("od.title", "text", "%" . $a_filter["object"] . "%");
148 }
149
150 $set = $ilDB->query("SELECT bb.*, od.title parent_title, od.type parent_type" .
151 " FROM badge_badge bb" .
152 " JOIN object_data od ON (bb.parent_id = od.obj_id)" .
153 " WHERE od.type <> " . $ilDB->quote("bdga", "text") .
154 $where);
155 while ($row = $ilDB->fetchAssoc($set)) {
156 $raw[] = $row;
157 }
158
159 $set = $ilDB->query("SELECT bb.*, od.title parent_title, od.type parent_type" .
160 " FROM badge_badge bb" .
161 " JOIN object_data_del od ON (bb.parent_id = od.obj_id)" .
162 " WHERE od.type <> " . $ilDB->quote("bdga", "text") .
163 $where);
164 while ($row = $ilDB->fetchAssoc($set)) {
165 $row["deleted"] = true;
166 $raw[] = $row;
167 }
168
169 foreach ($raw as $row) {
170 // :TODO:
171
172 $res[] = $row;
173 }
174
175 return $res;
176 }
177
178
179 //
180 // setter/getter
181 //
182
183 protected function setId($a_id)
184 {
185 $this->id = (int) $a_id;
186 }
187
188 public function getId()
189 {
190 return $this->id;
191 }
192
193 public function setParentId($a_id)
194 {
195 $this->parent_id = (int) $a_id;
196 }
197
198 public function getParentId()
199 {
200 return $this->parent_id;
201 }
202
203 public function setTypeId($a_id)
204 {
205 $this->type_id = trim($a_id);
206 }
207
208 public function getTypeId()
209 {
210 return $this->type_id;
211 }
212
213 public function setActive($a_value)
214 {
215 $this->active = (bool) $a_value;
216 }
217
218 public function isActive()
219 {
220 return $this->active;
221 }
222
223 public function setTitle($a_value)
224 {
225 $this->title = trim($a_value);
226 }
227
228 public function getTitle()
229 {
230 return $this->title;
231 }
232
233 public function setDescription($a_value)
234 {
235 $this->desc = trim($a_value);
236 }
237
238 public function getDescription()
239 {
240 return $this->desc;
241 }
242
243 public function setCriteria($a_value)
244 {
245 $this->criteria = trim($a_value);
246 }
247
248 public function getCriteria()
249 {
250 return $this->criteria;
251 }
252
253 public function setValid($a_value)
254 {
255 $this->valid = trim($a_value);
256 }
257
258 public function getValid()
259 {
260 return $this->valid;
261 }
262
263 public function setConfiguration(array $a_value = null)
264 {
265 if (is_array($a_value) &&
266 !sizeof($a_value)) {
267 $a_value = null;
268 }
269 $this->config = $a_value;
270 }
271
272 public function getConfiguration()
273 {
274 return $this->config;
275 }
276
277 protected function setImage($a_value)
278 {
279 $this->image = trim($a_value);
280 }
281
282 public function getImage()
283 {
284 return $this->image;
285 }
286
291 public function uploadImage(array $a_upload_meta)
292 {
293 if ($this->getId() &&
294 $a_upload_meta["tmp_name"]) {
295 $this->setImage($a_upload_meta["name"]);
296 $path = $this->getImagePath();
297
298 if (ilUtil::moveUploadedFile($a_upload_meta["tmp_name"], $this->getImagePath(false), $path)) {
299 $this->update();
300 }
301 }
302 }
303
304 public function importImage($a_name, $a_file)
305 {
306 if (file_exists($a_file)) {
307 $this->setImage($a_name);
308 copy($a_file, $this->getImagePath()); // #18280
309
310 $this->update();
311 }
312 }
313
314 public function getImagePath($a_full_path = true)
315 {
316 if ($this->getId()) {
317 $suffix = strtolower(array_pop(explode(".", $this->getImage())));
318 if ($a_full_path) {
319 return $this->getFilePath($this->getId()) . "img" . $this->getId() . "." . $suffix;
320 } else {
321 return "img" . $this->getId() . "." . $suffix;
322 }
323 }
324 }
325
333 protected function getFilePath($a_id, $a_subdir = null)
334 {
335 $storage = new ilFSStorageBadge($a_id);
336 $storage->create();
337
338 $path = $storage->getAbsolutePath() . "/";
339
340 if ($a_subdir) {
341 $path .= $a_subdir . "/";
342
343 if (!is_dir($path)) {
344 mkdir($path);
345 }
346 }
347
348 return $path;
349 }
350
351
352 //
353 // crud
354 //
355
356 protected function read($a_id)
357 {
359
360 $set = $ilDB->query("SELECT * FROM badge_badge" .
361 " WHERE id = " . $ilDB->quote($a_id, "integer"));
362 if ($ilDB->numRows($set)) {
363 $row = $ilDB->fetchAssoc($set);
364 $this->importDBRow($row);
365 }
366 }
367
368 protected function importDBRow(array $a_row)
369 {
370 $this->setId($a_row["id"]);
371 $this->setParentId($a_row["parent_id"]);
372 $this->setTypeId($a_row["type_id"]);
373 $this->setActive($a_row["active"]);
374 $this->setTitle($a_row["title"]);
375 $this->setDescription($a_row["descr"]);
376 $this->setCriteria($a_row["crit"]);
377 $this->setImage($a_row["image"]);
378 $this->setValid($a_row["valid"]);
379 $this->setConfiguration($a_row["conf"]
380 ? unserialize($a_row["conf"])
381 : null);
382 }
383
384 public function create()
385 {
387
388 if ($this->getId()) {
389 return $this->update();
390 }
391
392 $id = $ilDB->nextId("badge_badge");
393 $this->setId($id);
394
395 $fields = $this->getPropertiesForStorage();
396
397 $fields["id"] = array("integer", $id);
398 $fields["parent_id"] = array("integer", $this->getParentId());
399 $fields["type_id"] = array("text", $this->getTypeId());
400
401 $ilDB->insert("badge_badge", $fields);
402 }
403
404 public function update()
405 {
407
408 if (!$this->getId()) {
409 return $this->create();
410 }
411
412 $fields = $this->getPropertiesForStorage();
413
414 $ilDB->update(
415 "badge_badge",
416 $fields,
417 array("id" => array("integer", $this->getId()))
418 );
419 }
420
421 public function delete()
422 {
424
425 if (!$this->getId()) {
426 return;
427 }
428
429 if (file_exists($this->getImagePath())) {
430 unlink($this->getImagePath());
431 }
432
433 $this->deleteStaticFiles();
434
436
437 $ilDB->manipulate("DELETE FROM badge_badge" .
438 " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
439 }
440
441 protected function getPropertiesForStorage()
442 {
443 return array(
444 "active" => array("integer", $this->isActive()),
445 "title" => array("text", $this->getTitle()),
446 "descr" => array("text", $this->getDescription()),
447 "crit" => array("text", $this->getCriteria()),
448 "image" => array("text", $this->getImage()),
449 "valid" => array("text", $this->getValid()),
450 "conf" => array("text", $this->getConfiguration()
451 ? serialize($this->getConfiguration())
452 : null)
453 );
454 }
455
456
457 //
458 // helper
459 //
460
461 public function getParentMeta()
462 {
463 $parent_type = ilObject::_lookupType($this->getParentId());
464 if ($parent_type) {
465 $parent_title = ilObject::_lookupTitle($this->getParentId());
466 $deleted = false;
467 } else {
468 // already deleted?
469 $parent = ilObjectDataDeletionLog::get($this->getParentId());
470 if ($parent["type"]) {
471 $parent_type = $parent["type"];
472 $parent_title = $parent["title"];
473 }
474 $deleted = true;
475 }
476
477 return array(
478 "id" => $this->getParentId(),
479 "type" => $parent_type,
480 "title" => $parent_title,
481 "deleted" => $deleted
482 );
483 }
484
485
486 //
487 // PUBLISHING
488 //
489
490 protected function prepareJson($a_base_url, $a_img_suffix)
491 {
492 $json = new stdClass();
493 $json->{"@context"} = "https://w3id.org/openbadges/v1";
494 $json->type = "BadgeClass";
495 $json->id = $a_base_url . "class.json";
496 $json->name = $this->getTitle();
497 $json->description = $this->getDescription();
498 $json->image = $a_base_url . "image." . $a_img_suffix;
499 $json->criteria = $a_base_url . "criteria.txt";
500 $json->issuer = ilBadgeHandler::getInstance()->getIssuerStaticUrl();
501
502 return $json;
503 }
504
505 public function getStaticUrl()
506 {
507 $path = ilBadgeHandler::getInstance()->getBadgePath($this);
508
509 $base_url = ILIAS_HTTP_PATH . substr($path, 1);
510
511 if (!file_exists($path . "class.json")) {
512 $img_suffix = array_pop(explode(".", $this->getImage()));
513
514 $json = json_encode($this->prepareJson($base_url, $img_suffix));
515 file_put_contents($path . "class.json", $json);
516
517 // :TODO: scale?
518 copy($this->getImagePath(), $path . "image." . $img_suffix);
519
520 file_put_contents($path . "criteria.txt", $this->getCriteria());
521 }
522
523 return $base_url . "class.json";
524 }
525
526 public function deleteStaticFiles()
527 {
528 // remove instance files
529 $path = ilBadgeHandler::getInstance()->getBadgePath($this);
530 if (is_dir($path)) {
531 ilUtil::delDir($path);
532 }
533 }
534
536 {
537 global $DIC;
538
539 $lng = $DIC->language();
540
541 return $a_type->getCaption() . " (" .
542 ($a_type instanceof ilBadgeAuto
543 ? $lng->txt("badge_subtype_auto")
544 : $lng->txt("badge_subtype_manual")) . ")";
545 }
546}
An exception for terminatinating execution or to throw for unit testing.
static deleteByBadgeId($a_badge_id)
static getInstance()
Constructor.
Class ilBadge.
setTitle($a_value)
setCriteria($a_value)
setValid($a_value)
setConfiguration(array $a_value=null)
deleteStaticFiles()
setTypeId($a_id)
copy($a_new_parent_id)
__construct($a_id=null)
Constructor.
uploadImage(array $a_upload_meta)
static getInstancesByParentId($a_parent_id, array $a_filter=null)
importImage($a_name, $a_file)
read($a_id)
static getInstancesByType($a_type_id)
getImagePath($a_full_path=true)
static getExtendedTypeCaption(ilBadgeType $a_type)
setImage($a_value)
getConfiguration()
setParentId($a_id)
prepareJson($a_base_url, $a_img_suffix)
setActive($a_value)
getPropertiesForStorage()
importDBRow(array $a_row)
getFilePath($a_id, $a_subdir=null)
Init file system storage.
static getObjectInstances(array $a_filter=null)
setDescription($a_value)
setId($a_id)
static _lookupTitle($a_id)
lookup object title
static _lookupType($a_id, $a_reference=false)
lookup object type
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
Manual Badge Auto.
Badge type interface.
$lng
foreach($_POST as $key=> $value) $res
global $ilDB
$a_type
Definition: workflow.php:92
$DIC
Definition: xapitoken.php:46