ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilOrgUnitSimpleImport.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  public function simpleImport(string $file_path)
28  {
29  $this->stats = array("created" => 0, "updated" => 0, "deleted" => 0);
30  $a = file_get_contents($file_path, "r");
31  $xml = new SimpleXMLElement($a);
32 
33  if (!count($xml->OrgUnit)) {
34  $this->addError("no_orgunit", $xml->external_id, null);
35  return;
36  }
37 
38  foreach ($xml->OrgUnit as $o) {
39  $this->simpleImportElement($o);
40  }
41  }
42 
44  {
45  global $DIC;
46  $ilUser = $DIC['ilUser'];
47  $title = (string) $o->title;
48  $description = (string) $o->description;
49  $external_id = (string) $o->external_id;
50  $create_mode = true;
51  $attributes = $o->attributes();
52  $action = (string) $attributes->action;
53  $ou_id = (string) $attributes->ou_id;
54  $ou_id_type = (string) $attributes->ou_id_type;
55  $ou_parent_id = (string) $attributes->ou_parent_id;
56  $ou_parent_id_type = (string) $attributes->ou_parent_id_type;
57 
58  if ($ou_id == ilObjOrgUnit::getRootOrgRefId()) {
59  $this->addWarning("cannot_change_root_node", $ou_id ? $ou_id : $external_id, $action);
60 
61  return;
62  }
63 
64  if ($ou_parent_id == "__ILIAS") {
65  $ou_parent_id = ilObjOrgUnit::getRootOrgRefId();
66  $ou_parent_id_type = "reference_id";
67  }
68 
69  //see mantis 0024601
70  if ($ou_id_type == 'external_id') {
71  if (strlen($external_id) == 0) {
72  $external_id = $ou_id;
73  }
74 
75  if ($this->hasMoreThanOneMatch($external_id)) {
76  $this->addError("ou_more_than_one_match_found", $external_id, $action);
77 
78  return;
79  }
80  }
81 
82  $ref_id = $this->buildRef($ou_id, $ou_id_type);
83  $parent_ref_id = $this->buildRef($ou_parent_id, $ou_parent_id_type);
84 
85  if ($action == "delete") {
86  if (!$parent_ref_id) {
87  $this->addError("ou_parent_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
88 
89  return;
90  }
91  if (!$ref_id) {
92  $this->addError("ou_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
93 
94  return;
95  }
96  $ru = new ilRepUtil($this);
97  try {
98  $ru->deleteObjects($parent_ref_id, array($ref_id)) !== false;
99  $this->stats["deleted"]++;
100  } catch (Exception $e) {
101  $this->addWarning("orgu_already_deleted", $ou_id ? $ou_id : $external_id, $action);
102  }
103 
104  return;
105  } elseif ($action == "update") {
106  if (!$parent_ref_id) {
107  $this->addError("ou_parent_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
108 
109  return;
110  }
111  if (!$ref_id) {
112  $this->addError("ou_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
113 
114  return;
115  }
116  $object = new ilObjOrgUnit($ref_id);
117  $object->setTitle($title);
118 
119  $object->updateTranslation($title, $description, $ilUser->getLanguage(), "");
120 
121  $object->setDescription($description);
122  $object->update();
123  $object->setImportId($external_id);
124  $this->moveObject($ref_id, $parent_ref_id, $ou_id, $external_id);
125 
126  $this->stats["updated"]++;
127  } elseif ($action == "create") {
128  if (!$parent_ref_id) {
129  $this->addError("ou_parent_id_not_valid", $ou_id ? $ou_id : $external_id, $action);
130 
131  return;
132  }
133  if ($external_id) {
134  $obj_id = ilObject::_lookupObjIdByImportId($external_id);
135  if (ilObject::_hasUntrashedReference($obj_id) && ilObject::_lookupType($obj_id) == 'orgu') {
136  $this->addError("ou_external_id_exists", $ou_id ? $ou_id : $external_id, $action);
137 
138  return;
139  }
140  }
141  $object = new ilObjOrgUnit();
142  $object->setTitle($title);
143  $object->setDescription($description);
144  $object->setImportId($external_id);
145  $object->create();
146  $object->createReference();
147  $object->putInTree($parent_ref_id);
148  $object->setPermissions($parent_ref_id);
149  $this->stats["created"]++;
150  } else {
151  $this->addError("no_valid_action_given", $ou_id, $action);
152  }
153  }
154 
159  protected function moveObject(int $ref_id, int $parent_ref_id, string $ou_id, string $external_id)
160  {
161  global $DIC;
162  $tree = $DIC['tree'];
163  if ($parent_ref_id != $tree->getParentId($ref_id)) {
164  try {
165  $path = $tree->getPathId($parent_ref_id);
166  if (in_array($ref_id, $path)) {
167  $this->addWarning("not_movable_to_subtree", $ou_id ? $ou_id : $external_id, "update");
168  } else {
169  $tree->moveTree($ref_id, $parent_ref_id);
170  }
171  } catch (Exception $e) {
172  global $DIC;
173  $ilLog = $DIC['ilLog'];
174  $this->addWarning("not_movable", $ou_id ? $ou_id : $external_id, "update");
175  $ilLog->write($e->getMessage() . "\\n" . $e->getTraceAsString());
176  error_log($e->getMessage() . "\\n" . $e->getTraceAsString());
177  }
178  }
179  }
180 }
static _lookupObjIdByImportId(string $import_id)
Get (latest) object id for an import id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))
static _hasUntrashedReference(int $obj_id)
checks whether an object has at least one reference that is not in trash
hasMoreThanOneMatch(string $external_id)
$path
Definition: ltiservices.php:30
$ref_id
Definition: ltiauth.php:66
buildRef($id, string $type)
global $DIC
Definition: shib_login.php:25
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getRootOrgRefId()
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
moveObject(int $ref_id, int $parent_ref_id, string $ou_id, string $external_id)
simpleImportElement(SimpleXMLElement $o)
static _lookupType(int $id, bool $reference=false)
addError(string $lang_var, string $import_id, ?string $action=null)
addWarning(string $lang_var, string $import_id, ?string $action=null)