ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilOrgUnitExporter.php
Go to the documentation of this file.
1<?php
2
25{
26 private ilTree $tree;
27
28 public function __construct()
29 {
30 global $DIC;
32 $this->tree = $DIC['tree'];
33 }
34
35 public function simpleExport(int $orgu_ref_id): ilXmlWriter
36 {
37 $nodes = $this->getStructure($orgu_ref_id);
38 $writer = new ilXmlWriter();
39 $writer->xmlHeader();
40 $writer->xmlStartTag("OrgUnits");
41 foreach ($nodes as $node_ref_id) {
42 $orgu = new ilObjOrgUnit($node_ref_id);
43 if ($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
44 continue;
45 }
46 $attributes = $this->getAttributesForOrgu($orgu);
47 $writer->xmlStartTag("OrgUnit", $attributes);
48 $writer->xmlElement("external_id", null, $this->getExternalId($node_ref_id));
49 $writer->xmlElement("title", null, $orgu->getTitle());
50 $writer->xmlElement("description", null, $orgu->getDescription());
51 $writer->xmlEndTag("OrgUnit");
52 }
53 $writer->xmlEndTag("OrgUnits");
54
55 return $writer;
56 }
57
58 protected function getExternalId(int $orgu_ref_id): string
59 {
60 $import_id = ilObjOrgunit::_lookupImportId(ilObjOrgUnit::_lookupObjectId($orgu_ref_id));
61
62 return $import_id ?: $this->buildExternalId($orgu_ref_id);
63 }
64
65 protected function buildExternalId(int $orgu_ref_id): string
66 {
67 return "orgu_" . CLIENT_ID . "_" . $orgu_ref_id;
68 }
69
70 public function simpleExportExcel(int $orgu_ref_id): void
71 {
72 // New File and Sheet
73 $file_name = "org_unit_export_" . $orgu_ref_id;
74 $worksheet = new ilExcel();
75 $worksheet->addSheet('org_units');
76 $row = 1;
77
78 // Headers
79 $worksheet->setCell($row, 0, "ou_id");
80 $worksheet->setCell($row, 1, "ou_id_type");
81 $worksheet->setCell($row, 2, "ou_parent_id");
82 $worksheet->setCell($row, 3, "ou_parent_id_type");
83 $worksheet->setCell($row, 4, "reference_id");
84 $worksheet->setCell($row, 5, "title");
85 $worksheet->setCell($row, 6, "description");
86 $worksheet->setCell($row, 7, "action");
87
88 // Rows
89 $nodes = $this->getStructure($orgu_ref_id);
90
91 foreach ($nodes as $node) {
92 $orgu = new ilObjOrgUnit($node);
93 if ($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
94 continue;
95 }
96 $row++;
97 $attrs = $this->getAttributesForOrgu($orgu);
98 $worksheet->setCell($row, 0, $attrs["ou_id"]);
99 $worksheet->setCell($row, 1, $attrs["ou_id_type"]);
100 $worksheet->setCell($row, 2, $attrs["ou_parent_id"]);
101 $worksheet->setCell($row, 3, $attrs["ou_parent_id_type"]);
102 $worksheet->setCell($row, 4, $orgu->getRefId());
103 $worksheet->setCell($row, 5, $orgu->getTitle());
104 $worksheet->setCell($row, 6, $orgu->getDescription());
105 $worksheet->setCell($row, 7, "create");
106 }
107 $worksheet->sendToClient($file_name);
108 }
109
110 public function sendAndCreateSimpleExportFile(): array
111 {
112 $orgu_id = ilObjOrgUnit::getRootOrgId();
113 $orgu_ref_id = ilObjOrgUnit::getRootOrgRefId();
114
115 ilExport::_createExportDirectory($orgu_id, "xml", "orgu");
116 $export_dir = ilExport::_getExportDirectory($orgu_id, "xml", "orgu");
117 $ts = time();
118
119 // Workaround for test assessment
120 $sub_dir = $ts . '__' . IL_INST_ID . '__' . "orgu" . '_' . $orgu_id . "";
121 $new_file = $sub_dir . '.zip';
122
123 $export_run_dir = $export_dir . "/" . $sub_dir;
124 ilFileUtils::makeDirParents($export_run_dir);
125
126 $writer = $this->simpleExport($orgu_ref_id);
127 $writer->xmlDumpFile($export_run_dir . "/manifest.xml", false);
128
129 // zip the file
130 ilFileUtils::zip($export_run_dir, $export_dir . "/" . $new_file);
131 ilFileUtils::delDir($export_run_dir);
132
133 // Store info about export
134 $exp = new ilExportFileInfo($orgu_id);
135 $exp->setVersion(ILIAS_VERSION_NUMERIC);
136 $exp->setCreationDate(new ilDateTime($ts, IL_CAL_UNIX));
137 $exp->setExportType('xml');
138 $exp->setFilename($new_file);
139 $exp->create();
140
142 $export_dir . "/" . $new_file,
143 $new_file
144 );
145
146 return array(
147 "success" => true,
148 "file" => $new_file,
149 "directory" => $export_dir,
150 );
151 }
152
153 private function getStructure(int $root_node_ref): array
154 {
155 $open = array($root_node_ref);
156 $closed = array();
157 while (count($open)) {
158 $current = array_shift($open);
159 $closed[] = $current;
160 foreach ($this->tree->getChildsByType($current, "orgu") as $new) {
161 if (in_array($new["child"], $closed, true) === false && in_array($new["child"], $open, true) === false) {
162 $open[] = $new["child"];
163 }
164 }
165 }
166
167 return $closed;
168 }
169
170 private function getAttributesForOrgu(ilObjOrgUnit $orgu): array
171 {
172 $parent_ref = $this->tree->getParentId($orgu->getRefId());
173 if ($parent_ref != ilObjOrgUnit::getRootOrgRefId()) {
174 $ou_parent_id = $this->getExternalId($parent_ref);
175 } else {
176 $ou_parent_id = "__ILIAS";
177 }
178 // Only the ref id is guaranteed to be unique.
179 $ref_id = $orgu->getRefId();
180 $attr = array("ou_id" => $this->getExternalId($ref_id),
181 "ou_id_type" => "external_id",
182 "ou_parent_id" => $ou_parent_id,
183 "ou_parent_id_type" => "external_id",
184 "action" => "create"
185 );
186
187 return $attr;
188 }
189}
const IL_CAL_UNIX
Class for category export.
@classDescription Date and time handling
@classDescription Stores information of creation date and versions of export files
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="")
@depricated 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)
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)
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
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
const ILIAS_VERSION_NUMERIC
$ref_id
Definition: ltiauth.php:66
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26