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