ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 */
3require_once("./Modules/Category/classes/class.ilCategoryExporter.php");
4require_once("./Services/Xml/classes/class.ilXmlWriter.php");
5require_once("./Services/Export/classes/class.ilExport.php");
14
15
16
17 public function simpleExport($orgu_ref_id){
18 $nodes = $this->getStructure($orgu_ref_id);
19 $writer = new ilXmlWriter();
20 $writer->xmlHeader();
21 $writer->xmlStartTag("OrgUnits");
22 foreach($nodes as $orgu_ref_id){
23 $orgu = new ilObjOrgUnit($orgu_ref_id);
24 if($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId())
25 continue;
26 $attributes = $this->getAttributesForOrgu($orgu);
27 $writer->xmlStartTag("OrgUnit", $attributes);
28 $writer->xmlElement("external_id", null, $this->buildExternalId($orgu_ref_id));
29 $writer->xmlElement("title", null, $orgu->getTitle());
30 $writer->xmlElement("description", null, $orgu->getDescription());
31 $writer->xmlEndTag("OrgUnit");
32 }
33 $writer->xmlEndTag("OrgUnits");
34 return $writer;
35 }
36
41 protected function buildExternalId($orgu_ref_id) {
42 return "orgu_".CLIENT_ID."_".$orgu_ref_id;
43 }
44
45 public function simpleExportExcel($orgu_ref_id){
46 $nodes = $this->getStructure($orgu_ref_id);
47 include_once "./Services/Excel/classes/class.ilExcelUtils.php";
48 include_once "./Services/Excel/classes/class.ilExcelWriterAdapter.php";
49 $adapter = new ilExcelWriterAdapter("org_unit_export_".$orgu_ref_id.".xls", true);
50 $workbook = $adapter->getWorkbook();
52 $worksheet = $workbook->addWorksheet();
53
54 ob_start();
55 $worksheet->write(0, 0, "ou_id");
56 $worksheet->write(0, 1, "ou_id_type");
57 $worksheet->write(0, 2, "ou_parent_id");
58 $worksheet->write(0, 3, "ou_parent_id_type");
59 $worksheet->write(0, 4, "reference_id");
60 $worksheet->write(0, 5, "external_id");
61 $worksheet->write(0, 6, "title");
62 $worksheet->write(0, 7, "description");
63 $worksheet->write(0, 8, "action");
64
65 $row = 0;
66 foreach($nodes as $node)
67 {
68 $orgu = new ilObjOrgUnit($node);
69 if($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId())
70 continue;
71 $row++;
72 $attrs = $this->getAttributesForOrgu($orgu);
73 $worksheet->write($row, 0, $attrs["ou_id"]);
74 $worksheet->write($row, 1, $attrs["ou_id_type"]);
75 $worksheet->write($row, 2, $attrs["ou_parent_id"]);
76 $worksheet->write($row, 3, $attrs["ou_parent_id_type"]);
77 $worksheet->write($row, 4, $orgu->getRefId());
78 $worksheet->write($row, 5, $orgu->getImportId());
79 $worksheet->write($row, 6, $orgu->getTitle());
80 $worksheet->write($row, 7, $orgu->getDescription());
81 $worksheet->write($row, 8, "create");
82 }
83 ob_end_clean();
84 $workbook->close();
85 }
86
88 $orgu_id = ilObjOrgUnit::getRootOrgId();
89 $orgu_ref_id = ilObjOrgUnit::getRootOrgRefId();
90
91 ilExport::_createExportDirectory($orgu_id, "xml", "orgu");
92 $export_dir = ilExport::_getExportDirectory($orgu_id, "xml", "orgu");
93 $ts = time();
94
95 // Workaround for test assessment
96 $sub_dir = $ts.'__'.IL_INST_ID.'__'."orgu".'_'.$orgu_id."";
97 $new_file = $sub_dir.'.zip';
98
99 $export_run_dir = $export_dir."/".$sub_dir;
100 ilUtil::makeDirParents($export_run_dir);
101
102 $writer = $this->simpleExport($orgu_ref_id);
103 $writer->xmlDumpFile($export_run_dir."/manifest.xml", false);
104
105 // zip the file
106 ilUtil::zip($export_run_dir , $export_dir."/".$new_file);
107 ilUtil::delDir($export_run_dir );
108
109 // Store info about export
110 include_once './Services/Export/classes/class.ilExportFileInfo.php';
111 $exp = new ilExportFileInfo($orgu_id);
112 $exp->setVersion(ILIAS_VERSION_NUMERIC);
113 $exp->setCreationDate(new ilDateTime($ts,IL_CAL_UNIX));
114 $exp->setExportType('xml');
115 $exp->setFilename($new_file);
116 $exp->create();
117
118 ilUtil::deliverFile($export_dir."/".$new_file,
119 $new_file);
120
121 return array(
122 "success" => true,
123 "file" => $new_file,
124 "directory" => $export_dir
125 );
126 }
127
128 private function getStructure($root_node_ref){
129 global $tree;
130 $open = array($root_node_ref);
131 $closed = array();
132 while(count($open)){
133 $current = array_shift($open);
134 $closed[] = $current;
135 foreach($tree->getChildsByType($current, "orgu") as $new){
136 if(!in_array($new["child"], $closed) && !in_array($new["child"], $open))
137 $open[] = $new["child"];
138 }
139 }
140 return $closed;
141 }
142
147 private function getAttributesForOrgu($orgu){
148 global $tree;
149 $parent_ref = $tree->getParentId($orgu->getRefId());
150 if($parent_ref != ilObjOrgUnit::getRootOrgRefId()){
151 $ou_parent_id = $this->buildExternalId($parent_ref);
152 } else {
153 $ou_parent_id = "__ILIAS";
154 }
155 // Only the ref id is guaranteed to be unique.
156 $ref_id = $orgu->getRefId();
157 $attr = array("ou_id" => $this->buildExternalId($ref_id), "ou_id_type" => "external_id", "ou_parent_id" => $ou_parent_id, "ou_parent_id_type" => "external_id", "action" => "create");
158 return $attr;
159 }
160}
161?>
const IL_CAL_UNIX
Class for category export.
@classDescription Date and time handling
Class ilExcelWriterAdapter.
@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.
_createExportDirectory($a_obj_id, $a_export_type="xml", $a_obj_type="")
Create export directory.
Class ilObjOrgUnit.
static getRootOrgRefId()
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)
static makeDirParents($a_dir)
Create a new directory and all parent directories.
static deliverFile($a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.
XML writer class.
const ILIAS_VERSION_NUMERIC
$ref_id
Definition: sahs_server.php:39