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