ILIAS  release_8 Revision v8.24
class.ilOrgUnitExporter.php
Go to the documentation of this file.
1<?php
18/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
19
26{
27 private ilTree $tree;
28
29 public function __construct()
30 {
31 global $DIC;
33 $this->tree = $DIC['tree'];
34 }
35
36 public function simpleExport(int $orgu_ref_id): ilXmlWriter
37 {
38 $nodes = $this->getStructure($orgu_ref_id);
39 $writer = new ilXmlWriter();
40 $writer->xmlHeader();
41 $writer->xmlStartTag("OrgUnits");
42 foreach ($nodes as $node_ref_id) {
43 $orgu = new ilObjOrgUnit($node_ref_id);
44 if ($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
45 continue;
46 }
47 $attributes = $this->getAttributesForOrgu($orgu);
48 $writer->xmlStartTag("OrgUnit", $attributes);
49 $writer->xmlElement("external_id", null, $this->getExternalId($node_ref_id));
50 $writer->xmlElement("title", null, $orgu->getTitle());
51 $writer->xmlElement("description", null, $orgu->getDescription());
52 $writer->xmlEndTag("OrgUnit");
53 }
54 $writer->xmlEndTag("OrgUnits");
55
56 return $writer;
57 }
58
59 protected function getExternalId(int $orgu_ref_id): string
60 {
61 $import_id = ilObjOrgunit::_lookupImportId(ilObjOrgUnit::_lookupObjectId($orgu_ref_id));
62
63 return $import_id ?: $this->buildExternalId($orgu_ref_id);
64 }
65
66 protected function buildExternalId(int $orgu_ref_id): string
67 {
68 return "orgu_" . CLIENT_ID . "_" . $orgu_ref_id;
69 }
70
71 public function simpleExportExcel(int $orgu_ref_id): void
72 {
73 // New File and Sheet
74 $file_name = "org_unit_export_" . $orgu_ref_id;
75 $worksheet = new ilExcel();
76 $worksheet->addSheet('org_units');
77 $row = 1;
78
79 // Headers
80 $worksheet->setCell($row, 0, "ou_id");
81 $worksheet->setCell($row, 1, "ou_id_type");
82 $worksheet->setCell($row, 2, "ou_parent_id");
83 $worksheet->setCell($row, 3, "ou_parent_id_type");
84 $worksheet->setCell($row, 4, "reference_id");
85 $worksheet->setCell($row, 5, "title");
86 $worksheet->setCell($row, 6, "description");
87 $worksheet->setCell($row, 7, "action");
88
89 // Rows
90 $nodes = $this->getStructure($orgu_ref_id);
91
92 foreach ($nodes as $node) {
93 $orgu = new ilObjOrgUnit($node);
94 if ($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
95 continue;
96 }
97 $row++;
98 $attrs = $this->getAttributesForOrgu($orgu);
99 $worksheet->setCell($row, 0, $attrs["ou_id"]);
100 $worksheet->setCell($row, 1, $attrs["ou_id_type"]);
101 $worksheet->setCell($row, 2, $attrs["ou_parent_id"]);
102 $worksheet->setCell($row, 3, $attrs["ou_parent_id_type"]);
103 $worksheet->setCell($row, 4, $orgu->getRefId());
104 $worksheet->setCell($row, 5, $orgu->getTitle());
105 $worksheet->setCell($row, 6, $orgu->getDescription());
106 $worksheet->setCell($row, 7, "create");
107 }
108 $worksheet->sendToClient($file_name);
109 }
110
111 public function sendAndCreateSimpleExportFile(): array
112 {
113 $orgu_id = ilObjOrgUnit::getRootOrgId();
114 $orgu_ref_id = ilObjOrgUnit::getRootOrgRefId();
115
116 ilExport::_createExportDirectory($orgu_id, "xml", "orgu");
117 $export_dir = ilExport::_getExportDirectory($orgu_id, "xml", "orgu");
118 $ts = time();
119
120 // Workaround for test assessment
121 $sub_dir = $ts . '__' . IL_INST_ID . '__' . "orgu" . '_' . $orgu_id . "";
122 $new_file = $sub_dir . '.zip';
123
124 $export_run_dir = $export_dir . "/" . $sub_dir;
125 ilFileUtils::makeDirParents($export_run_dir);
126
127 $writer = $this->simpleExport($orgu_ref_id);
128 $writer->xmlDumpFile($export_run_dir . "/manifest.xml", false);
129
130 // zip the file
131 ilFileUtils::zip($export_run_dir, $export_dir . "/" . $new_file);
132 ilFileUtils::delDir($export_run_dir);
133
134 // Store info about export
135 $exp = new ilExportFileInfo($orgu_id);
136 $exp->setVersion(ILIAS_VERSION_NUMERIC);
137 $exp->setCreationDate(new ilDateTime($ts, IL_CAL_UNIX));
138 $exp->setExportType('xml');
139 $exp->setFilename($new_file);
140 $exp->create();
141
143 $export_dir . "/" . $new_file,
144 $new_file
145 );
146
147 return array(
148 "success" => true,
149 "file" => $new_file,
150 "directory" => $export_dir,
151 );
152 }
153
154 private function getStructure(int $root_node_ref): array
155 {
156 $open = array($root_node_ref);
157 $closed = array();
158 while (count($open)) {
159 $current = array_shift($open);
160 $closed[] = $current;
161 foreach ($this->tree->getChildsByType($current, "orgu") as $new) {
162 if (in_array($new["child"], $closed, true) === false && in_array($new["child"], $open, true) === false) {
163 $open[] = $new["child"];
164 }
165 }
166 }
167
168 return $closed;
169 }
170
171 private function getAttributesForOrgu(ilObjOrgUnit $orgu): array
172 {
173 $parent_ref = $this->tree->getParentId($orgu->getRefId());
174 if ($parent_ref != ilObjOrgUnit::getRootOrgRefId()) {
175 $ou_parent_id = $this->getExternalId($parent_ref);
176 } else {
177 $ou_parent_id = "__ILIAS";
178 }
179 // Only the ref id is guaranteed to be unique.
180 $ref_id = $orgu->getRefId();
181 $attr = array("ou_id" => $this->getExternalId($ref_id),
182 "ou_id_type" => "external_id",
183 "ou_parent_id" => $ou_parent_id,
184 "ou_parent_id_type" => "external_id",
185 "action" => "create"
186 );
187
188 return $attr;
189 }
190}
const IL_CAL_UNIX
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
@classDescription Date and time handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _createExportDirectory(int $a_obj_id, string $a_export_type="xml", string $a_obj_type="")
static _getExportDirectory(int $a_obj_id, string $a_type="xml", string $a_obj_type="", string $a_entity="")
Get export directory for an repository object.
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static zip(string $a_dir, string $a_file, bool $compress_content=false)
zips given directory/file into given zip.file
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getRootOrgRefId()
static _lookupObjectId(int $ref_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
simpleExportExcel(int $orgu_ref_id)
getExternalId(int $orgu_ref_id)
buildExternalId(int $orgu_ref_id)
getAttributesForOrgu(ilObjOrgUnit $orgu)
simpleExport(int $orgu_ref_id)
getStructure(int $root_node_ref)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLIENT_ID
Definition: constants.php:41
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
const ILIAS_VERSION_NUMERIC
$ref_id
Definition: ltiauth.php:67
$attributes
Definition: metadata.php:248
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc