ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
10 class ilBadge
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 clone(int $target_parent_obj_id) : void
102  {
103  $this->setParentId($target_parent_obj_id);
104  $this->setActive(false);
105 
106  if ($this->getId()) {
107  $img = $this->getImagePath();
108 
109  $this->setId(0);
110  $this->create();
111 
112  if ($img) {
113  // see uploadImage()
114  copy($img, $this->getImagePath());
115  }
116  }
117  }
118 
119  public function getTypeInstance()
120  {
121  if ($this->getTypeId()) {
122  $handler = ilBadgeHandler::getInstance();
123  return $handler->getTypeInstanceByUniqueId($this->getTypeId());
124  }
125  }
126 
127  public function copy($a_new_parent_id)
128  {
129  $lng = $this->lng;
130 
131  $this->setTitle($this->getTitle() . " " . $lng->txt("copy_of_suffix"));
132  $this->setParentId($a_new_parent_id);
133  $this->setActive(false);
134 
135  if ($this->getId()) {
136  $img = $this->getImagePath();
137 
138  $this->setId(null);
139  $this->create();
140 
141  if ($img) {
142  // see uploadImage()
143  copy($img, $this->getImagePath());
144  }
145  }
146  }
147 
148  public static function getObjectInstances(array $a_filter = null)
149  {
150  global $DIC;
151 
152  $ilDB = $DIC->database();
153 
154  $res = $raw = array();
155 
156  $where = "";
157 
158  if ($a_filter["type"]) {
159  $where .= " AND bb.type_id = " . $ilDB->quote($a_filter["type"], "text");
160  }
161  if ($a_filter["title"]) {
162  $where .= " AND " . $ilDB->like("bb.title", "text", "%" . $a_filter["title"] . "%");
163  }
164  if ($a_filter["object"]) {
165  $where .= " AND " . $ilDB->like("od.title", "text", "%" . $a_filter["object"] . "%");
166  }
167 
168  $set = $ilDB->query("SELECT bb.*, od.title parent_title, od.type parent_type" .
169  " FROM badge_badge bb" .
170  " JOIN object_data od ON (bb.parent_id = od.obj_id)" .
171  " WHERE od.type <> " . $ilDB->quote("bdga", "text") .
172  $where);
173  while ($row = $ilDB->fetchAssoc($set)) {
174  $raw[] = $row;
175  }
176 
177  $set = $ilDB->query("SELECT bb.*, od.title parent_title, od.type parent_type" .
178  " FROM badge_badge bb" .
179  " JOIN object_data_del od ON (bb.parent_id = od.obj_id)" .
180  " WHERE od.type <> " . $ilDB->quote("bdga", "text") .
181  $where);
182  while ($row = $ilDB->fetchAssoc($set)) {
183  $row["deleted"] = true;
184  $raw[] = $row;
185  }
186 
187  foreach ($raw as $row) {
188  // :TODO:
189 
190  $res[] = $row;
191  }
192 
193  return $res;
194  }
195 
196 
197  //
198  // setter/getter
199  //
200 
201  protected function setId($a_id)
202  {
203  $this->id = (int) $a_id;
204  }
205 
206  public function getId()
207  {
208  return $this->id;
209  }
210 
211  public function setParentId($a_id)
212  {
213  $this->parent_id = (int) $a_id;
214  }
215 
216  public function getParentId()
217  {
218  return $this->parent_id;
219  }
220 
221  public function setTypeId($a_id)
222  {
223  $this->type_id = trim($a_id);
224  }
225 
226  public function getTypeId()
227  {
228  return $this->type_id;
229  }
230 
231  public function setActive($a_value)
232  {
233  $this->active = (bool) $a_value;
234  }
235 
236  public function isActive()
237  {
238  return $this->active;
239  }
240 
241  public function setTitle($a_value)
242  {
243  $this->title = trim($a_value);
244  }
245 
246  public function getTitle()
247  {
248  return $this->title;
249  }
250 
251  public function setDescription($a_value)
252  {
253  $this->desc = trim($a_value);
254  }
255 
256  public function getDescription()
257  {
258  return $this->desc;
259  }
260 
261  public function setCriteria($a_value)
262  {
263  $this->criteria = trim($a_value);
264  }
265 
266  public function getCriteria()
267  {
268  return $this->criteria;
269  }
270 
271  public function setValid($a_value)
272  {
273  $this->valid = trim($a_value);
274  }
275 
276  public function getValid()
277  {
278  return $this->valid;
279  }
280 
281  public function setConfiguration(array $a_value = null)
282  {
283  if (is_array($a_value) &&
284  !sizeof($a_value)) {
285  $a_value = null;
286  }
287  $this->config = $a_value;
288  }
289 
290  public function getConfiguration()
291  {
292  return $this->config;
293  }
294 
295  protected function setImage($a_value)
296  {
297  $this->image = trim($a_value);
298  }
299 
300  public function getImage()
301  {
302  return $this->image;
303  }
304 
309  public function uploadImage(array $a_upload_meta)
310  {
311  if ($this->getId() &&
312  $a_upload_meta["tmp_name"]) {
313  $this->setImage($a_upload_meta["name"]);
314  $path = $this->getImagePath();
315 
316  if (ilUtil::moveUploadedFile($a_upload_meta["tmp_name"], $this->getImagePath(false), $path)) {
317  $this->update();
318  }
319  }
320  }
321 
322  public function importImage($a_name, $a_file)
323  {
324  if (file_exists($a_file)) {
325  $this->setImage($a_name);
326  copy($a_file, $this->getImagePath()); // #18280
327 
328  $this->update();
329  }
330  }
331 
332  public function getImagePath($a_full_path = true)
333  {
334  if ($this->getId()) {
335  $suffix = strtolower(array_pop(explode(".", $this->getImage())));
336  if ($a_full_path) {
337  return $this->getFilePath($this->getId()) . "img" . $this->getId() . "." . $suffix;
338  } else {
339  return "img" . $this->getId() . "." . $suffix;
340  }
341  }
342  }
343 
351  protected function getFilePath($a_id, $a_subdir = null)
352  {
353  $storage = new ilFSStorageBadge($a_id);
354  $storage->create();
355 
356  $path = $storage->getAbsolutePath() . "/";
357 
358  if ($a_subdir) {
359  $path .= $a_subdir . "/";
360 
361  if (!is_dir($path)) {
362  mkdir($path);
363  }
364  }
365 
366  return $path;
367  }
368 
369 
370  //
371  // crud
372  //
373 
374  protected function read($a_id)
375  {
376  $ilDB = $this->db;
377 
378  $set = $ilDB->query("SELECT * FROM badge_badge" .
379  " WHERE id = " . $ilDB->quote($a_id, "integer"));
380  if ($ilDB->numRows($set)) {
381  $row = $ilDB->fetchAssoc($set);
382  $this->importDBRow($row);
383  }
384  }
385 
386  protected function importDBRow(array $a_row)
387  {
388  $this->setId($a_row["id"]);
389  $this->setParentId($a_row["parent_id"]);
390  $this->setTypeId($a_row["type_id"]);
391  $this->setActive($a_row["active"]);
392  $this->setTitle($a_row["title"]);
393  $this->setDescription($a_row["descr"]);
394  $this->setCriteria($a_row["crit"]);
395  $this->setImage($a_row["image"]);
396  $this->setValid($a_row["valid"]);
397  $this->setConfiguration($a_row["conf"]
398  ? unserialize($a_row["conf"])
399  : null);
400  }
401 
402  public function create()
403  {
404  $ilDB = $this->db;
405 
406  if ($this->getId()) {
407  return $this->update();
408  }
409 
410  $id = $ilDB->nextId("badge_badge");
411  $this->setId($id);
412 
413  $fields = $this->getPropertiesForStorage();
414 
415  $fields["id"] = array("integer", $id);
416  $fields["parent_id"] = array("integer", $this->getParentId());
417  $fields["type_id"] = array("text", $this->getTypeId());
418 
419  $ilDB->insert("badge_badge", $fields);
420  }
421 
422  public function update()
423  {
424  $ilDB = $this->db;
425 
426  if (!$this->getId()) {
427  return $this->create();
428  }
429 
430  $fields = $this->getPropertiesForStorage();
431 
432  $ilDB->update(
433  "badge_badge",
434  $fields,
435  array("id" => array("integer", $this->getId()))
436  );
437  }
438 
439  public function delete()
440  {
441  $ilDB = $this->db;
442 
443  if (!$this->getId()) {
444  return;
445  }
446 
447  if (file_exists($this->getImagePath())) {
448  unlink($this->getImagePath());
449  }
450 
451  $this->deleteStaticFiles();
452 
454 
455  $ilDB->manipulate("DELETE FROM badge_badge" .
456  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
457  }
458 
459  protected function getPropertiesForStorage()
460  {
461  return array(
462  "active" => array("integer", $this->isActive()),
463  "title" => array("text", $this->getTitle()),
464  "descr" => array("text", $this->getDescription()),
465  "crit" => array("text", $this->getCriteria()),
466  "image" => array("text", $this->getImage()),
467  "valid" => array("text", $this->getValid()),
468  "conf" => array("text", $this->getConfiguration()
469  ? serialize($this->getConfiguration())
470  : null)
471  );
472  }
473 
474 
475  //
476  // helper
477  //
478 
479  public function getParentMeta()
480  {
481  $parent_type = ilObject::_lookupType($this->getParentId());
482  if ($parent_type) {
483  $parent_title = ilObject::_lookupTitle($this->getParentId());
484  $deleted = false;
485  } else {
486  // already deleted?
487  $parent = ilObjectDataDeletionLog::get($this->getParentId());
488  if ($parent["type"]) {
489  $parent_type = $parent["type"];
490  $parent_title = $parent["title"];
491  }
492  $deleted = true;
493  }
494 
495  return array(
496  "id" => $this->getParentId(),
497  "type" => $parent_type,
498  "title" => $parent_title,
499  "deleted" => $deleted
500  );
501  }
502 
503 
504  //
505  // PUBLISHING
506  //
507 
508  protected function prepareJson($a_base_url, $a_img_suffix)
509  {
510  $json = new stdClass();
511  $json->{"@context"} = "https://w3id.org/openbadges/v1";
512  $json->type = "BadgeClass";
513  $json->id = $a_base_url . "class.json";
514  $json->name = $this->getTitle();
515  $json->description = $this->getDescription();
516  $json->image = $a_base_url . "image." . $a_img_suffix;
517  $json->criteria = $a_base_url . "criteria.txt";
518  $json->issuer = ilBadgeHandler::getInstance()->getIssuerStaticUrl();
519 
520  return $json;
521  }
522 
523  public function getStaticUrl()
524  {
525  $path = ilBadgeHandler::getInstance()->getBadgePath($this);
526 
527  $base_url = ILIAS_HTTP_PATH . substr($path, 1);
528 
529  if (!file_exists($path . "class.json")) {
530  $img_suffix = array_pop(explode(".", $this->getImage()));
531 
532  $json = json_encode($this->prepareJson($base_url, $img_suffix));
533  file_put_contents($path . "class.json", $json);
534 
535  // :TODO: scale?
536  copy($this->getImagePath(), $path . "image." . $img_suffix);
537 
538  file_put_contents($path . "criteria.txt", $this->getCriteria());
539  }
540 
541  return $base_url . "class.json";
542  }
543 
544  public function deleteStaticFiles()
545  {
546  // remove instance files
547  $path = ilBadgeHandler::getInstance()->getBadgePath($this);
548  if (is_dir($path)) {
549  ilUtil::delDir($path);
550  }
551  }
552 
553  public static function getExtendedTypeCaption(ilBadgeType $a_type)
554  {
555  global $DIC;
556 
557  $lng = $DIC->language();
558 
559  return $a_type->getCaption() . " (" .
560  ($a_type instanceof ilBadgeAuto
561  ? $lng->txt("badge_subtype_auto")
562  : $lng->txt("badge_subtype_manual")) . ")";
563  }
564 }
getPropertiesForStorage()
uploadImage(array $a_upload_meta)
getImagePath($a_full_path=true)
getConfiguration()
getCaption()
Get caption.
static getObjectInstances(array $a_filter=null)
Badge type interface.
static _lookupTitle($a_id)
lookup object title
setCriteria($a_value)
deleteStaticFiles()
setImage($a_value)
getFilePath($a_id, $a_subdir=null)
Init file system storage.
clone(int $target_parent_obj_id)
setActive($a_value)
Class ilBadge.
Manual Badge Auto.
static getInstancesByParentId($a_parent_id, array $a_filter=null)
setParentId($a_id)
foreach($_POST as $key=> $value) $res
setValid($a_value)
static getExtendedTypeCaption(ilBadgeType $a_type)
setTypeId($a_id)
copy($a_new_parent_id)
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
global $DIC
Definition: goto.php:24
__construct($a_id=null)
Constructor.
static deleteByBadgeId($a_badge_id)
read($a_id)
setId($a_id)
static _lookupType($a_id, $a_reference=false)
lookup object type
$img
Definition: imgupload.php:57
importImage($a_name, $a_file)
setConfiguration(array $a_value=null)
global $ilDB
setDescription($a_value)
setTitle($a_value)
importDBRow(array $a_row)
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static getInstancesByType($a_type_id)
prepareJson($a_base_url, $a_img_suffix)
static getInstance()
Constructor.