ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
class.ilConditionsImporter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Conditions\Export\Factory as ConditionExportFactory;
23
25{
26 protected const string ENTITY = 'cond';
27 protected ConditionExportFactory $factory;
28
29 public function init(): void
30 {
31 global $DIC;
32 $this->factory = new ConditionExportFactory($DIC->database());
34 }
35
36 public function importXmlRepresentation(
37 string $a_entity,
38 string $a_id,
39 string $a_xml,
40 ilImportMapping $a_mapping
41 ): void {
42 if ($a_entity !== self::ENTITY) {
43 return;
44 }
45 $root_id = $this->determineRootIdOfImportTree($a_mapping);
46 if ($root_id !== (int) $a_id) {
47 return;
48 }
49 $reader = $this->factory->xmlReader();
50 $infos = $reader->readString($a_xml);
51 if ($infos->count() === 0) {
52 return;
53 }
54 $updated_infos = $this->updateInfosWithMapping($infos, $a_mapping);
55 $repository = $this->factory->repository();
56 $repository->writeByInfos($updated_infos);
57 }
58
59 protected function determineRootIdOfImportTree(
60 ilImportMapping $mapping
61 ): int {
62 // $root_id relies on the fact that the smallest obj_id belongs to the first imported
63 // object, wich is the root of the imported object tree
64 return (int) min(array_keys($mapping->getAllMappings()['components/ILIAS/Container']['objs']));
65 }
66
67 protected function updateInfosWithMapping(
68 InfoCollection $infos,
69 ilImportMapping $mapping
71 $updated_infos = $this->factory->infoCollection();
72 foreach ($infos as $info) {
73 $new_ref_id = $this->getNewRefId(
74 $info->getReferenceId(),
75 $mapping
76 );
77 if (is_null($new_ref_id)) {
78 continue;
79 }
80 $new_obj_id = ilObject::_lookupObjId($new_ref_id);
81 $conditions = $this->updateConditionsWithMapping(
82 $info->getConditionSet()->getConditions(),
83 $mapping
84 );
85 $obligatory_count = $this->countObligatory($conditions);
86 $conditions_count = count($conditions);
87 $num_obligatory = ($conditions_count - $obligatory_count) < 2
88 ? 0
89 : min($info->getConditionSet()->getNumObligatory(), $conditions_count - 1);
90 $condition_set = (new ilConditionSet($conditions))
91 ->withHiddenStatus($info->getConditionSet()->getHiddenStatus())
92 ->withNumObligatory($num_obligatory);
93 if ($obligatory_count === $conditions_count) {
94 $condition_set = $condition_set->withAllObligatory();
95 }
96 $updated_infos = $updated_infos->withInfo(
97 $info
98 ->withConditionSet($condition_set)
99 ->withObjectId($new_obj_id)
100 ->withReferenceId($new_ref_id)
101 );
102 }
103 return $updated_infos;
104 }
105
110 protected function updateConditionsWithMapping(
111 array $conditions,
112 ilImportMapping $mapping
113 ): array {
114 $updated_conditions = [];
115 foreach ($conditions as $condition) {
116 $new_trigger_ref_id = $this->getNewRefId(
117 $condition->getTrigger()->getRefId(),
118 $mapping
119 );
120 if (is_null($new_trigger_ref_id)) {
121 continue;
122 }
123 $new_trigger_obj_id = ilObject::_lookupObjId($new_trigger_ref_id);
124 $trigger = new ilConditionTrigger(
125 $new_trigger_ref_id,
126 $new_trigger_obj_id,
127 $condition->getTrigger()->getType()
128 );
129 $updated_conditions[] = (new ilCondition(
130 $trigger,
131 $condition->getOperator(),
132 $condition->getValue()
133 ))
134 ->withObligatory($condition->getObligatory())
135 ->withId(-1);
136 }
137 return $updated_conditions;
138 }
139
143 protected function countObligatory(
144 array $conditions
145 ): int {
146 $count = 0;
147 foreach ($conditions as $condition) {
148 if ($condition->getObligatory()) {
149 $count++;
150 }
151 }
152 return $count;
153 }
154
155 protected function getNewRefId(
156 int $old_ref_id,
157 ilImportMapping $mapping
158 ): int|null {
159 $new_ref_id = $mapping->getMapping('components/ILIAS/Container', 'refs', (string) $old_ref_id);
160 return is_null($new_ref_id) ? null : (int) $new_ref_id;
161 }
162}
factory()
Condition set Note: This object currently focuses on repository objects as targets.
Represents a condition trigger object.
Condition class.
updateInfosWithMapping(InfoCollection $infos, ilImportMapping $mapping)
getNewRefId(int $old_ref_id, ilImportMapping $mapping)
updateConditionsWithMapping(array $conditions, ilImportMapping $mapping)
determineRootIdOfImportTree(ilImportMapping $mapping)
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping)
ConditionExportFactory $factory
static _lookupObjId(int $ref_id)
Xml importer class.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$info
Definition: entry_point.php:21
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26