ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 require_once("./Modules/Category/classes/class.ilCategoryExporter.php");
4 require_once("./Services/Xml/classes/class.ilXmlWriter.php");
5 require_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 
49  public function simpleExportExcel($orgu_ref_id) {
50  // New File and Sheet
51  $file_name = "org_unit_export_" . $orgu_ref_id;
52  require_once('./Services/Excel/classes/class.ilExcel.php');
53  $worksheet = new ilExcel();
54  $worksheet->addSheet('org_units');
55  $row = 1;
56 
57  // Headers
58  $worksheet->setCell($row, 0, "ou_id");
59  $worksheet->setCell($row, 1, "ou_id_type");
60  $worksheet->setCell($row, 2, "ou_parent_id");
61  $worksheet->setCell($row, 3, "ou_parent_id_type");
62  $worksheet->setCell($row, 4, "reference_id");
63  $worksheet->setCell($row, 5, "external_id");
64  $worksheet->setCell($row, 6, "title");
65  $worksheet->setCell($row, 7, "description");
66  $worksheet->setCell($row, 8, "action");
67 
68  // Rows
69  $nodes = $this->getStructure($orgu_ref_id);
70 
71  foreach ($nodes as $node) {
72  $orgu = new ilObjOrgUnit($node);
73  if ($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId()) {
74  continue;
75  }
76  $row ++;
77  $attrs = $this->getAttributesForOrgu($orgu);
78  $worksheet->setCell($row, 0, $attrs["ou_id"]);
79  $worksheet->setCell($row, 1, $attrs["ou_id_type"]);
80  $worksheet->setCell($row, 2, $attrs["ou_parent_id"]);
81  $worksheet->setCell($row, 3, $attrs["ou_parent_id_type"]);
82  $worksheet->setCell($row, 4, $orgu->getRefId());
83  $worksheet->setCell($row, 5, $orgu->getImportId());
84  $worksheet->setCell($row, 6, $orgu->getTitle());
85  $worksheet->setCell($row, 7, $orgu->getDescription());
86  $worksheet->setCell($row, 8, "create");
87  }
88  $worksheet->sendToClient($file_name);
89  }
90 
91  public function sendAndCreateSimpleExportFile(){
92  $orgu_id = ilObjOrgUnit::getRootOrgId();
93  $orgu_ref_id = ilObjOrgUnit::getRootOrgRefId();
94 
95  ilExport::_createExportDirectory($orgu_id, "xml", "orgu");
96  $export_dir = ilExport::_getExportDirectory($orgu_id, "xml", "orgu");
97  $ts = time();
98 
99  // Workaround for test assessment
100  $sub_dir = $ts.'__'.IL_INST_ID.'__'."orgu".'_'.$orgu_id."";
101  $new_file = $sub_dir.'.zip';
102 
103  $export_run_dir = $export_dir."/".$sub_dir;
104  ilUtil::makeDirParents($export_run_dir);
105 
106  $writer = $this->simpleExport($orgu_ref_id);
107  $writer->xmlDumpFile($export_run_dir."/manifest.xml", false);
108 
109  // zip the file
110  ilUtil::zip($export_run_dir , $export_dir."/".$new_file);
111  ilUtil::delDir($export_run_dir );
112 
113  // Store info about export
114  include_once './Services/Export/classes/class.ilExportFileInfo.php';
115  $exp = new ilExportFileInfo($orgu_id);
116  $exp->setVersion(ILIAS_VERSION_NUMERIC);
117  $exp->setCreationDate(new ilDateTime($ts,IL_CAL_UNIX));
118  $exp->setExportType('xml');
119  $exp->setFilename($new_file);
120  $exp->create();
121 
122  ilUtil::deliverFile($export_dir."/".$new_file,
123  $new_file);
124 
125  return array(
126  "success" => true,
127  "file" => $new_file,
128  "directory" => $export_dir
129  );
130  }
131 
132  private function getStructure($root_node_ref){
133  global $DIC;
134  $tree = $DIC['tree'];
135  $open = array($root_node_ref);
136  $closed = array();
137  while(count($open)){
138  $current = array_shift($open);
139  $closed[] = $current;
140  foreach($tree->getChildsByType($current, "orgu") as $new){
141  if(!in_array($new["child"], $closed) && !in_array($new["child"], $open))
142  $open[] = $new["child"];
143  }
144  }
145  return $closed;
146  }
147 
152  private function getAttributesForOrgu($orgu){
153  global $DIC;
154  $tree = $DIC['tree'];
155  $parent_ref = $tree->getParentId($orgu->getRefId());
156  if($parent_ref != ilObjOrgUnit::getRootOrgRefId()){
157  $ou_parent_id = $this->buildExternalId($parent_ref);
158  } else {
159  $ou_parent_id = "__ILIAS";
160  }
161  // Only the ref id is guaranteed to be unique.
162  $ref_id = $orgu->getRefId();
163  $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");
164  return $attr;
165  }
166 }
167 ?>
static _createExportDirectory($a_obj_id, $a_export_type="xml", $a_obj_type="")
static makeDirParents($a_dir)
Create a new directory and all parent directories.
$worksheet
Class ilOrgUnitExporter.
const ILIAS_VERSION_NUMERIC
Class for category export.
XML writer class.
const IL_CAL_UNIX
Class ilObjOrgUnit.
Stores information of creation date and versions of export files
Date and time handling
static deliverFile($a_file, $a_filename, $a_mime='', $isInline=false, $removeAfterDelivery=false, $a_exit_after=true)
deliver file for download via browser.
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
Create styles array
The data for the language used.
static getRootOrgRefId()
$ref_id
Definition: sahs_server.php:39
static _getExportDirectory($a_obj_id, $a_type="xml", $a_obj_type="", $a_entity="")
Get export directory for an repository object.
global $DIC
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively