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