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