ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilECSCategoryMapping.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
24 {
25  private static ?array $cached_active_rules = null;
26 
32  public static function getActiveRules(): array
33  {
34  global $DIC;
35 
36  $ilDB = $DIC['ilDB'];
37  $rules = [];
38  $res = $ilDB->query('SELECT mapping_id FROM ecs_container_mapping');
39  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
40  $rules[] = new ilECSCategoryMappingRule((int) $row->mapping_id);
41  }
42  return $rules;
43  }
44 
48  public static function getMatchingCategory(int $a_server_id, array $a_matchable_content): ?int
49  {
50  global $DIC;
51 
52  $logger = $DIC->logger()->wsrv();
53 
54  if (is_null(self::$cached_active_rules)) {
55  self::$cached_active_rules = self::getActiveRules();
56  }
57  foreach (self::$cached_active_rules as $rule) {
58  if ($rule->matches($a_matchable_content)) {
59  $logger->info(__METHOD__ . ': Found assignment for field type: ' . $rule->getFieldName());
60  return $rule->getContainerId();
61  }
62  $logger->error(__METHOD__ . ': Category assignment failed for field: ' . $rule->getFieldName());
63  }
64  // Return default container
65  $logger->info(__METHOD__ . ': Using default container');
66 
67  return ilECSSetting::getInstanceByServerId($a_server_id)->getImportId();
68  }
69 
73  public static function handleUpdate(int $a_obj_id, int $a_server_id, array $a_matchable_content): bool
74  {
75  global $DIC;
76 
77  $tree = $DIC->repositoryTree();
78  $logger = $DIC->logger()->wsrv();
79 
80  $cat = self::getMatchingCategory($a_server_id, $a_matchable_content);
81 
82  $a_ref_id = current(ilObject::_getAllReferences($a_obj_id));
83  $references = ilObject::_getAllReferences(ilObject::_lookupObjId($a_ref_id));
84  $all_cats = self::lookupHandledCategories();
85 
86  $exists = false;
87  foreach (array_keys($references) as $ref_id) {
88  if ($tree->getParentId($ref_id) === $cat) {
89  $exists = true;
90  }
91  }
92  $logger->info(__METHOD__ . ': Creating/Deleting references...');
93 
94  if (!$exists) {
95  $logger->info(__METHOD__ . ': Add new reference. STEP 1');
96 
97  if ($obj_data = ilObjectFactory::getInstanceByRefId($a_ref_id, false)) {
98  $obj_data->createReference();
99  $obj_data->putInTree($cat);
100  $obj_data->setPermissions($cat);
101  $logger->info(__METHOD__ . ': Add new reference.');
102  }
103  }
104  // Now delete old references
105  foreach (array_keys($references) as $ref_id) {
106  $parent = $tree->getParentId($ref_id);
107  if ($parent === $cat) {
108  continue;
109  }
110  if (!in_array($parent, $all_cats, true)) {
111  continue;
112  }
113  if ($to_delete = ilObjectFactory::getInstanceByRefId($ref_id)) {
114  $to_delete->delete();
115  $logger->write(__METHOD__ . ': Deleted deprecated reference.');
116  }
117  }
118  return true;
119  }
120 
124  public static function lookupHandledCategories(): array
125  {
126  global $DIC;
127 
128  $ilDB = $DIC->database();
129 
130  $ref_ids = [];
131  $res = $ilDB->query("SELECT container_id FROM ecs_container_mapping ");
132  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
133  $ref_ids[] = $row->container_id;
134  }
135  return $ref_ids;
136  }
137 
141  public static function getPossibleFields(): array
142  {
143  global $DIC;
144 
145  $lng = $DIC['lng'];
146 
147  $options = array(
148  "community" => $lng->txt("ecs_field_community"),
149  "part_id" => $lng->txt("ecs_field_part_id"),
150  "type" => $lng->txt("type")
151  );
152 
153  // will be handled by server soon?
154 
155  // only courses for now
156  $course_fields = ilECSUtils::_getOptionalECourseFields();
157  foreach ($course_fields as $field) {
158  $options[$field] = $lng->txt("obj_rcrs") . " - " . $lng->txt("ecs_field_" . $field);
159  }
160 
161  return $options;
162  }
163 }
$res
Definition: ltiservices.php:69
$lng
static _getAllReferences(int $id)
get all reference ids for object ID
Defines a rule for the assignment of ECS remote courses to categories.
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
static getActiveRules()
get active rules
$ref_id
Definition: ltiauth.php:67
static getInstanceByServerId(int $a_server_id)
Get singleton instance per server.
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
static getMatchingCategory(int $a_server_id, array $a_matchable_content)
get matching category
static handleUpdate(int $a_obj_id, int $a_server_id, array $a_matchable_content)
Handle update of ecs content and create references.
static _getOptionalECourseFields()
get optional econtent fields These fields might be mapped against AdvancedMetaData field definitions ...