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