ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilBadgeHandler.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13 {
14  protected $settings; // [ilSetting]
15 
16  protected static $instance; // [ilBadgeHandler]
17 
23  protected function __construct()
24  {
25  $this->settings = new ilSetting("bdga");
26  }
27 
33  public static function getInstance()
34  {
35  if(!self::$instance)
36  {
37  self::$instance = new self();
38  }
39  return self::$instance;
40  }
41 
42 
43  //
44  // setter/getter
45  //
46 
47  public function isActive()
48  {
49  return $this->settings->get("active", false);
50  }
51 
52  public function setActive($a_value)
53  {
54  $this->settings->set("active", (bool)$a_value);
55  }
56 
57  public function isObiActive()
58  {
59  // see bug #20124
60  return false;
61 
62  return $this->settings->get("obi_active", false);
63  }
64 
65  public function setObiActive($a_value)
66  {
67  $this->settings->set("obi_active", (bool)$a_value);
68  }
69 
70  public function getObiOrganistation()
71  {
72  return $this->settings->get("obi_organisation", null);
73  }
74 
75  public function setObiOrganisation($a_value)
76  {
77  $this->settings->set("obi_organisation", trim($a_value));
78  }
79 
80  public function getObiContact()
81  {
82  return $this->settings->get("obi_contact", null);
83  }
84 
85  public function setObiContact($a_value)
86  {
87  $this->settings->set("obi_contact", trim($a_value));
88  }
89 
90  public function getObiSalt()
91  {
92  return $this->settings->get("obi_salt", null);
93  }
94 
95  public function setObiSalt($a_value)
96  {
97  $this->settings->set("obi_salt", trim($a_value));
98  }
99 
100  public function getComponents()
101  {
102  $components = $this->settings->get("components", null);
103  if($components)
104  {
105  return unserialize($components);
106  }
107  return array();
108  }
109 
110  public function setComponents(array $a_components = null)
111  {
112  if(is_array($a_components) &&
113  !sizeof($a_components))
114  {
115  $a_components = null;
116  }
117  $this->settings->set("components", $a_components !== null
118  ? serialize(array_unique($a_components))
119  : null);
120  }
121 
122 
123  //
124  // component handling
125  //
126 
127  protected function getComponent($a_id)
128  {
129  global $ilDB;
130 
131  // see ilCtrl
132  $set = $ilDB->query("SELECT * FROM il_component".
133  " WHERE id = ".$ilDB->quote($a_id, "text"));
134  $rec = $ilDB->fetchAssoc($set);
135  if($rec["type"])
136  {
137  return $rec;
138  }
139  }
140 
147  public function getProviderInstance($a_component_id)
148  {
149  $comp = $this->getComponent($a_component_id);
150  if($comp)
151  {
152  $class = "il".$comp["name"]."BadgeProvider";
153  $file = $comp["type"]."/".$comp["name"]."/classes/class.".$class.".php";
154  if(file_exists($file))
155  {
156  include_once $file;
157  $obj = new $class;
158  if($obj instanceof ilBadgeProvider)
159  {
160  return $obj;
161  }
162  }
163  }
164  }
165 
166  public function getComponentCaption($a_component_id)
167  {
168  $comp = $this->getComponent($a_component_id);
169  if($comp)
170  {
171  return $comp["type"]."/".$comp["name"];
172  }
173  }
174 
175  //
176  // types
177  //
178 
179  public function getUniqueTypeId($a_component_id, ilBadgeType $a_badge)
180  {
181  return $a_component_id."/".$a_badge->getId();
182  }
183 
189  public function getTypeInstanceByUniqueId($a_id)
190  {
191  $parts = explode("/", $a_id);
192  $comp_id = $parts[0];
193  $type_id = $parts[1];
194  $provider = $this->getProviderInstance($comp_id);
195  if($provider)
196  {
197  foreach($provider->getBadgeTypes() as $type)
198  {
199  if($type->getId() == $type_id)
200  {
201  return $type;
202  }
203  }
204  }
205  }
206 
207  public function getInactiveTypes()
208  {
209  $types = $this->settings->get("inactive_types", null);
210  if($types)
211  {
212  return unserialize($types);
213  }
214  return array();
215  }
216 
217  public function setInactiveTypes(array $a_types = null)
218  {
219  if(is_array($a_types) &&
220  !sizeof($a_types))
221  {
222  $a_types = null;
223  }
224  $this->settings->set("inactive_types", $a_types !== null
225  ? serialize(array_unique($a_types))
226  : null);
227  }
228 
234  public function getAvailableTypes()
235  {
236  $res = array();
237 
238  $inactive = $this->getInactiveTypes();
239  foreach($this->getComponents() as $component_id)
240  {
241  $provider = $this->getProviderInstance($component_id);
242  if($provider)
243  {
244  foreach($provider->getBadgeTypes() as $type)
245  {
246  $id = $this->getUniqueTypeId($component_id, $type);
247  if(!in_array($id, $inactive))
248  {
249  $res[$id] = $type;
250  }
251  }
252  }
253  }
254 
255  return $res;
256  }
257 
264  public function getAvailableTypesForObjType($a_object_type)
265  {
266  $res = array();
267 
268  foreach($this->getAvailableTypes() as $id => $type)
269  {
270  if(in_array($a_object_type, $type->getValidObjectTypes()))
271  {
272  $res[$id] = $type;
273  }
274  }
275 
276  return $res;
277  }
278 
286  public function getAvailableManualBadges($a_parent_obj_id, $a_parent_obj_type = null)
287  {
288  $res = array();
289 
290  if(!$a_parent_obj_type)
291  {
292  $a_parent_obj_type = ilObject::_lookupType($a_parent_obj_id);
293  }
294 
295  include_once "./Services/Badge/classes/class.ilBadge.php";
296  $badges = ilBadge::getInstancesByParentId($a_parent_obj_id);
297  foreach(ilBadgeHandler::getInstance()->getAvailableTypesForObjType($a_parent_obj_type) as $type_id => $type)
298  {
299  if(!$type instanceof ilBadgeAuto)
300  {
301  foreach($badges as $badge)
302  {
303  if($badge->getTypeId() == $type_id &&
304  $badge->isActive())
305  {
306  $res[$badge->getId()] = $badge->getTitle();
307  }
308  }
309  }
310  }
311 
312  asort($res);
313  return $res;
314  }
315 
316 
317 
318  //
319  // service/module definition
320  //
321 
327  public static function updateFromXML($a_component_id)
328  {
329  $handler = self::getInstance();
330  $components = $handler->getComponents();
331  $components[] = $a_component_id;
332  $handler->setComponents($components);
333  }
334 
340  public static function clearFromXML($a_component_id)
341  {
342  $handler = self::getInstance();
343  $components = $handler->getComponents();
344  foreach($components as $idx => $component)
345  {
346  if($component == $a_component_id)
347  {
348  unset($components[$idx]);
349  }
350  }
351  $handler->setComponents($components);
352  }
353 
354 
355  //
356  // helper
357  //
358 
359  public function isObjectActive($a_obj_id, $a_obj_type = null)
360  {
361  if(!$this->isActive())
362  {
363  return false;
364  }
365 
366  if(!$a_obj_type)
367  {
368  $a_obj_type = ilObject::_lookupType($a_obj_id);
369  }
370 
371  if($a_obj_type != "bdga")
372  {
373  include_once 'Services/Container/classes/class.ilContainer.php';
374  include_once 'Services/Object/classes/class.ilObjectServiceSettingsGUI.php';
376  $a_obj_id,
378  false))
379  {
380  return false;
381  }
382  }
383 
384  return true;
385  }
386 
387  public function triggerEvaluation($a_type_id, $a_user_id, array $a_params = null)
388  {
389  if(!$this->isActive() ||
390  in_array($a_type_id, $this->getInactiveTypes()))
391  {
392  return;
393  }
394 
395  $type = $this->getTypeInstanceByUniqueId($a_type_id);
396  if(!$type ||
397  !$type instanceof ilBadgeAuto)
398  {
399  return;
400  }
401 
402  include_once "Services/Badge/classes/class.ilBadge.php";
403  include_once "Services/Badge/classes/class.ilBadgeAssignment.php";
404  $new_badges = array();
405  foreach(ilBadge::getInstancesByType($a_type_id) as $badge)
406  {
407  if($badge->isActive())
408  {
409  // already assigned?
410  if(!ilBadgeAssignment::exists($badge->getId(), $a_user_id))
411  {
412  if((bool)$type->evaluate($a_user_id, (array)$a_params, (array)$badge->getConfiguration()))
413  {
414  $ass = new ilBadgeAssignment($badge->getId(), $a_user_id);
415  $ass->store();
416 
417  $new_badges[$a_user_id][] = $badge->getId();
418  }
419  }
420  }
421  }
422 
423  $this->sendNotification($new_badges);
424  }
425 
426  public function getUserIds($a_parent_ref_id, $a_parent_obj_id = null, $a_parent_type = null)
427  {
428  global $tree;
429 
430  if(!$a_parent_obj_id)
431  {
432  $a_parent_obj_id = ilObject::_lookupObjectId($a_parent_ref_id);
433  }
434  if(!$a_parent_type)
435  {
436  $a_parent_type = ilObject::_lookupType($a_parent_obj_id);
437  }
438 
439  // try to get participants from (parent) course/group
440  switch($a_parent_type)
441  {
442  case "crs":
443  include_once "Modules/Course/classes/class.ilCourseParticipants.php";
444  $member_obj = ilCourseParticipants::_getInstanceByObjId($a_parent_obj_id);
445  return $member_obj->getMembers();
446 
447  case "grp":
448  include_once "Modules/Group/classes/class.ilGroupParticipants.php";
449  $member_obj = ilGroupParticipants::_getInstanceByObjId($a_parent_obj_id);
450  return $member_obj->getMembers();
451 
452  default:
453  // walk path to find course or group object and use members of that object
454  $path = $tree->getPathId($a_parent_ref_id);
455  array_pop($path);
456  foreach(array_reverse($path) as $path_ref_id)
457  {
458  $type = ilObject::_lookupType($path_ref_id, true);
459  if($type == "crs" || $type == "grp")
460  {
461  return $this->getParticipantsForObject($path_ref_id, null, $type);
462  }
463  }
464  break;
465  }
466  }
467 
468 
469  //
470  // PATH HANDLING (PUBLISHING)
471  //
472 
473  protected function getBasePath()
474  {
475  return ilUtil::getWebspaceDir()."/pub_badges/";
476  }
477 
478  public function getInstancePath(ilBadgeAssignment $a_ass)
479  {
480  $hash = md5($a_ass->getBadgeId()."_".$a_ass->getUserId());
481 
482  $path = $this->getBasePath()."instances/".
483  $a_ass->getBadgeId()."/".
484  floor($a_ass->getUserId()/1000)."/";
485 
486  ilUtil::makeDirParents($path);
487 
488  $path .= $hash.".json";
489 
490  return $path;
491  }
492 
493  public function countStaticBadgeInstances(ilBadge $a_badge)
494  {
495  $path = $this->getBasePath()."instances/".$a_badge->getId();
496  $cnt = 0;
497  if(is_dir($path))
498  {
500  }
501  return $cnt;
502  }
503 
504  protected function countStaticBadgeInstancesHelper(&$a_cnt, $a_path)
505  {
506  foreach(glob($a_path."/*") as $item)
507  {
508  if(is_dir($item))
509  {
510  $this->countStaticBadgeInstancesHelper($a_cnt, $item);
511  }
512  else if(substr($item, -5) == ".json")
513  {
514  $a_cnt++;
515  }
516  }
517  }
518 
519  public function getBadgePath(ilBadge $a_badge)
520  {
521  $hash = md5($a_badge->getId());
522 
523  $path = $this->getBasePath()."badges/".
524  floor($a_badge->getId()/100)."/".
525  $hash."/";
526 
527  ilUtil::makeDirParents($path);
528 
529  return $path;
530  }
531 
532  protected function prepareIssuerJson($a_url)
533  {
534  $json = new stdClass();
535  $json->{"@context"} = "https://w3id.org/openbadges/v1";
536  $json->type = "Issuer";
537  $json->id = $a_url;
538  $json->name = $this->getObiOrganistation();
539  $json->url = ILIAS_HTTP_PATH."/";
540  $json->email = $this->getObiContact();
541 
542  return $json;
543  }
544 
545  public function getIssuerStaticUrl()
546  {
547  $path = $this->getBasePath()."issuer/";
549  $path .= "issuer.json";
550 
551  $url = ILIAS_HTTP_PATH.substr($path, 1);
552 
553  if(!file_exists($path))
554  {
555  $json = json_encode($this->prepareIssuerJson($url));
556  file_put_contents($path, $json);
557  }
558 
559  return $url;
560  }
561 
562  public function rebuildIssuerStaticUrl()
563  {
564  $path = $this->getBasePath()."issuer/issuer.json";
565  if(file_exists($path))
566  {
567  unlink($path);
568  }
569  $this->getIssuerStaticUrl();
570  }
571 
572 
573  //
574  // notification
575  //
576 
577  public function sendNotification(array $a_user_map, $a_parent_ref_id = null)
578  {
579  global $lng;
580 
581  $badges = array();
582 
583  include_once "Services/Badge/classes/class.ilBadge.php";
584  include_once "Services/Badge/classes/class.ilBadgeAssignment.php";
585  include_once "Services/Notification/classes/class.ilSystemNotification.php";
586  include_once "Services/Link/classes/class.ilLink.php";
587 
588  foreach($a_user_map as $user_id => $badge_ids)
589  {
590  $user_badges = array();
591 
592  foreach($badge_ids as $badge_id)
593  {
594  // making extra sure
595  if(!ilBadgeAssignment::exists($badge_id, $user_id))
596  {
597  continue;
598  }
599 
600  if(!array_key_exists($badge_id, $badges))
601  {
602  $badges[$badge_id] = new ilBadge($badge_id);
603  }
604 
605  $badge = $badges[$badge_id];
606 
607  $user_badges[] = $badge->getTitle();
608  }
609 
610  if(sizeof($user_badges))
611  {
612  // compose and send mail
613 
614  $ntf = new ilSystemNotification(false);
615  $ntf->setLangModules(array("badge"));
616 
617  $ntf->setRefId($a_parent_ref_id);
618  $ntf->setGotoLangId("badge_notification_parent_goto");
619 
620  // user specific language
621  $lng = $ntf->getUserLanguage($user_id);
622 
623  $ntf->setIntroductionLangId("badge_notification_body");
624 
625  $ntf->addAdditionalInfo("badge_notification_badges", implode("\n", $user_badges), true);
626 
627  $url = ilLink::_getLink($user_id, "usr", array(), "_bdg");
628  $ntf->addAdditionalInfo("badge_notification_badges_goto", $url);
629 
630  $ntf->setReasonLangId("badge_notification_reason");
631 
632  // force email
633  $mail = new ilMail(ANONYMOUS_USER_ID);
634  $mail->enableSOAP(false);
635  $mail->sendMail(ilObjUser::_lookupEmail($user_id),
636  null,
637  null,
638  $lng->txt("badge_notification_subject"),
639  $ntf->composeAndGetMessage($user_id, null, "read", true),
640  null,
641  array("system"));
642 
643 
644  // osd
645 
646  $osd_params = array("badge_list" => "<br />".implode("<br />", $user_badges));
647 
648  require_once "Services/Notifications/classes/class.ilNotificationConfig.php";
649  $notification = new ilNotificationConfig("osd_main");
650  $notification->setTitleVar("badge_notification_subject", array(), "badge");
651  $notification->setShortDescriptionVar("badge_notification_osd", $osd_params, "badge");
652  $notification->setLongDescriptionVar("", $osd_params, "");
653  $notification->setAutoDisable(false);
654  $notification->setLink($url);
655  $notification->setIconPath("templates/default/images/icon_bdga.svg");
656  $notification->setValidForSeconds(ilNotificationConfig::TTL_SHORT);
657  $notification->setVisibleForSeconds(ilNotificationConfig::DEFAULT_TTS);
658  $notification->notifyByUsers(array($user_id));
659  }
660  }
661  }
662 }
Badge Provider interface.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
getAvailableTypesForObjType($a_object_type)
Get valid badges types for object type.
ILIAS Setting Class.
$path
Definition: aliased.php:25
isObjectActive($a_obj_id, $a_obj_type=null)
static exists($a_badge_id, $a_user_id)
getAvailableTypes()
Get badges types.
Badge type interface.
getTypeInstanceByUniqueId($a_id)
Get type instance by unique id (component, type)
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
static updateFromXML($a_component_id)
Import component definition.
getProviderInstance($a_component_id)
Get provider instance.
sendNotification(array $a_user_map, $a_parent_ref_id=null)
$url
Definition: shib_logout.php:72
getInstancePath(ilBadgeAssignment $a_ass)
static _lookupObjectId($a_ref_id)
lookup object id
Describes a notification and provides methods for publishing this notification.
Manual Badge Auto.
static clearFromXML($a_component_id)
Remove component definition.
static getInstancesByParentId($a_parent_id, array $a_filter=null)
triggerEvaluation($a_type_id, $a_user_id, array $a_params=null)
getId()
Get typ id (unique for component)
setComponents(array $a_components=null)
This class handles base functions for mail handling.
__construct()
Constructor.
setInactiveTypes(array $a_types=null)
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
static _lookupContainerSetting($a_id, $a_keyword, $a_default_value=NULL)
Lookup a container setting.
getUniqueTypeId($a_component_id, ilBadgeType $a_badge)
settings()
Definition: settings.php:2
getComponentCaption($a_component_id)
static _lookupEmail($a_user_id)
Lookup email.
getBadgePath(ilBadge $a_badge)
global $lng
Definition: privfeed.php:17
global $ilDB
getAvailableManualBadges($a_parent_obj_id, $a_parent_obj_type=null)
Get available manual badges for object id.
countStaticBadgeInstancesHelper(&$a_cnt, $a_path)
setObiOrganisation($a_value)
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
countStaticBadgeInstances(ilBadge $a_badge)
getUserIds($a_parent_ref_id, $a_parent_obj_id=null, $a_parent_type=null)
static getWebspaceDir($mode="filesystem")
get webspace directory
Wrapper classes for system notifications.
static getInstancesByType($a_type_id)
static getInstance()
Constructor.