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