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