ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 */
11 {
12  public function simpleExport($orgu_ref_id)
13  {
14  $nodes = $this->getStructure($orgu_ref_id);
15  $writer = new ilXmlWriter();
16  $writer->xmlHeader();
17  $writer->xmlStartTag("OrgUnits");
18  foreach ($nodes as $orgu_ref_id) {
19  $orgu = new ilObjOrgUnit($orgu_ref_id);
20  if ($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
21  continue;
22  }
23  $attributes = $this->getAttributesForOrgu($orgu);
24  $writer->xmlStartTag("OrgUnit", $attributes);
25  $writer->xmlElement("external_id", null, $this->getExternalId($orgu_ref_id));
26  $writer->xmlElement("title", null, $orgu->getTitle());
27  $writer->xmlElement("description", null, $orgu->getDescription());
28  $writer->xmlEndTag("OrgUnit");
29  }
30  $writer->xmlEndTag("OrgUnits");
31  return $writer;
32  }
33 
34 
40  protected function getExternalId($orgu_ref_id)
41  {
42  $import_id = ilObjOrgunit::_lookupImportId(ilObjOrgUnit::_lookupObjectId($orgu_ref_id));
43  return $import_id ?: $this->buildExternalId($orgu_ref_id);
44  }
45 
50  protected function buildExternalId($orgu_ref_id)
51  {
52  return "orgu_" . CLIENT_ID . "_" . $orgu_ref_id;
53  }
54 
55 
59  public function simpleExportExcel($orgu_ref_id)
60  {
61  // New File and Sheet
62  $file_name = "org_unit_export_" . $orgu_ref_id;
63  $worksheet = new ilExcel();
64  $worksheet->addSheet('org_units');
65  $row = 1;
66 
67  // Headers
68  $worksheet->setCell($row, 0, "ou_id");
69  $worksheet->setCell($row, 1, "ou_id_type");
70  $worksheet->setCell($row, 2, "ou_parent_id");
71  $worksheet->setCell($row, 3, "ou_parent_id_type");
72  $worksheet->setCell($row, 4, "reference_id");
73  $worksheet->setCell($row, 5, "title");
74  $worksheet->setCell($row, 6, "description");
75  $worksheet->setCell($row, 7, "action");
76 
77  // Rows
78  $nodes = $this->getStructure($orgu_ref_id);
79 
80  foreach ($nodes as $node) {
81  $orgu = new ilObjOrgUnit($node);
82  if ($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
83  continue;
84  }
85  $row++;
86  $attrs = $this->getAttributesForOrgu($orgu);
87  $worksheet->setCell($row, 0, $attrs["ou_id"]);
88  $worksheet->setCell($row, 1, $attrs["ou_id_type"]);
89  $worksheet->setCell($row, 2, $attrs["ou_parent_id"]);
90  $worksheet->setCell($row, 3, $attrs["ou_parent_id_type"]);
91  $worksheet->setCell($row, 4, $orgu->getRefId());
92  $worksheet->setCell($row, 5, $orgu->getTitle());
93  $worksheet->setCell($row, 6, $orgu->getDescription());
94  $worksheet->setCell($row, 7, "create");
95  }
96  $worksheet->sendToClient($file_name);
97  }
98 
100  {
101  $orgu_id = ilObjOrgUnit::getRootOrgId();
102  $orgu_ref_id = ilObjOrgUnit::getRootOrgRefId();
103 
104  ilExport::_createExportDirectory($orgu_id, "xml", "orgu");
105  $export_dir = ilExport::_getExportDirectory($orgu_id, "xml", "orgu");
106  $ts = time();
107 
108  // Workaround for test assessment
109  $sub_dir = $ts . '__' . IL_INST_ID . '__' . "orgu" . '_' . $orgu_id . "";
110  $new_file = $sub_dir . '.zip';
111 
112  $export_run_dir = $export_dir . "/" . $sub_dir;
113  ilUtil::makeDirParents($export_run_dir);
114 
115  $writer = $this->simpleExport($orgu_ref_id);
116  $writer->xmlDumpFile($export_run_dir . "/manifest.xml", false);
117 
118  // zip the file
119  ilUtil::zip($export_run_dir, $export_dir . "/" . $new_file);
120  ilUtil::delDir($export_run_dir);
121 
122  // Store info about export
123  $exp = new ilExportFileInfo($orgu_id);
124  $exp->setVersion(ILIAS_VERSION_NUMERIC);
125  $exp->setCreationDate(new ilDateTime($ts, IL_CAL_UNIX));
126  $exp->setExportType('xml');
127  $exp->setFilename($new_file);
128  $exp->create();
129 
131  $export_dir . "/" . $new_file,
132  $new_file
133  );
134 
135  return array(
136  "success" => true,
137  "file" => $new_file,
138  "directory" => $export_dir
139  );
140  }
141 
142  private function getStructure($root_node_ref)
143  {
144  global $DIC;
145  $tree = $DIC['tree'];
146  $open = array($root_node_ref);
147  $closed = array();
148  while (count($open)) {
149  $current = array_shift($open);
150  $closed[] = $current;
151  foreach ($tree->getChildsByType($current, "orgu") as $new) {
152  if (!in_array($new["child"], $closed) && !in_array($new["child"], $open)) {
153  $open[] = $new["child"];
154  }
155  }
156  }
157  return $closed;
158  }
159 
164  private function getAttributesForOrgu($orgu)
165  {
166  global $DIC;
167  $tree = $DIC['tree'];
168  $parent_ref = $tree->getParentId($orgu->getRefId());
169  if ($parent_ref != ilObjOrgUnit::getRootOrgRefId()) {
170  $ou_parent_id = $this->getExternalId($parent_ref);
171  } else {
172  $ou_parent_id = "__ILIAS";
173  }
174  // Only the ref id is guaranteed to be unique.
175  $ref_id = $orgu->getRefId();
176  $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");
177  return $attr;
178  }
179 }
static _createExportDirectory($a_obj_id, $a_export_type="xml", $a_obj_type="")
static makeDirParents($a_dir)
Create a new directory and all parent directories.
Class ilOrgUnitExporter.
const ILIAS_VERSION_NUMERIC
global $DIC
Definition: saml.php:7
Class for category export.
XML writer class.
const IL_CAL_UNIX
static _lookupObjectId($a_ref_id)
lookup object id
Class ilObjOrgUnit.
Stores information of creation date and versions of export files
Date and time handling
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
static getRootOrgRefId()
$row
static _getExportDirectory($a_obj_id, $a_type="xml", $a_obj_type="", $a_entity="")
Get export directory for an repository object.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static deliverFile( $a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.