ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
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->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  $attrs = $this->getAttrForOrgu($orgu);
26  $writer->xmlStartTag("OrgUnit", $attrs);
27  $writer->xmlElement("reference_id", null, $orgu->getRefId());
28  $writer->xmlElement("external_id", null, $orgu->getImportId());
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 
37  public function simpleExportExcel($orgu_ref_id){
38  $nodes = $this->getStructure($orgu_ref_id);
39  include_once "./Services/Excel/classes/class.ilExcelUtils.php";
40  include_once "./Services/Excel/classes/class.ilExcelWriterAdapter.php";
41  $adapter = new ilExcelWriterAdapter("org_unit_export_".$orgu_ref_id.".xls", true);
42  $workbook = $adapter->getWorkbook();
44  $worksheet = $workbook->addWorksheet();
45 
46  ob_start();
47  $worksheet->write(0, 0, "ou_id");
48  $worksheet->write(0, 1, "ou_id_type");
49  $worksheet->write(0, 2, "ou_parent_id");
50  $worksheet->write(0, 3, "ou_parent_id_type");
51  $worksheet->write(0, 4, "reference_id");
52  $worksheet->write(0, 5, "external_id");
53  $worksheet->write(0, 6, "title");
54  $worksheet->write(0, 7, "description");
55  $worksheet->write(0, 8, "action");
56 
57  $row = 0;
58  foreach($nodes as $node)
59  {
60  $orgu = new ilObjOrgUnit($node);
61  if($orgu->getRefId() == ilObjOrgUnit::getRootOrgRefId())
62  continue;
63  $row++;
64  $attrs = $this->getAttrForOrgu($orgu);
65  $worksheet->write($row, 0, $attrs["ou_id"]);
66  $worksheet->write($row, 1, $attrs["ou_id_type"]);
67  $worksheet->write($row, 2, $attrs["ou_parent_id"]);
68  $worksheet->write($row, 3, $attrs["ou_parent_id_type"]);
69  $worksheet->write($row, 4, $orgu->getRefId());
70  $worksheet->write($row, 5, $orgu->getImportId());
71  $worksheet->write($row, 6, $orgu->getTitle());
72  $worksheet->write($row, 7, $orgu->getDescription());
73  $worksheet->write($row, 8, "create");
74  }
75  ob_end_clean();
76  $workbook->close();
77  }
78 
79  public function sendAndCreateSimpleExportFile(){
80  $orgu_id = ilObjOrgUnit::getRootOrgId();
81  $orgu_ref_id = ilObjOrgUnit::getRootOrgRefId();
82 
83  ilExport::_createExportDirectory($orgu_id, "xml", "orgu");
84  $export_dir = ilExport::_getExportDirectory($orgu_id, "xml", "orgu");
85  $ts = time();
86 
87  // Workaround for test assessment
88  $sub_dir = $ts.'__'.IL_INST_ID.'__'."orgu".'_'.$orgu_id."";
89  $new_file = $sub_dir.'.zip';
90 
91  $export_run_dir = $export_dir."/".$sub_dir;
92  ilUtil::makeDirParents($export_run_dir);
93 
94  $writer = $this->simpleExport($orgu_ref_id);
95  $writer->xmlDumpFile($export_run_dir."/manifest.xml", false);
96 
97  // zip the file
98  ilUtil::zip($export_run_dir , $export_dir."/".$new_file);
99  ilUtil::delDir($export_run_dir );
100 
101  // Store info about export
102  include_once './Services/Export/classes/class.ilExportFileInfo.php';
103  $exp = new ilExportFileInfo($orgu_id);
104  $exp->setVersion(ILIAS_VERSION_NUMERIC);
105  $exp->setCreationDate(new ilDateTime($ts,IL_CAL_UNIX));
106  $exp->setExportType('xml');
107  $exp->setFilename($new_file);
108  $exp->create();
109 
110  ilUtil::deliverFile($export_dir."/".$new_file,
111  $new_file);
112 
113  return array(
114  "success" => true,
115  "file" => $new_file,
116  "directory" => $export_dir
117  );
118  }
119 
120 // public function lookupExportDirectory()
121 
122  private function getStructure($root_node_ref){
123  global $tree;
124  $open = array($root_node_ref);
125  $closed = array();
126  while(count($open)){
127  $current = array_shift($open);
128  $closed[] = $current;
129  foreach($tree->getChildsByType($current, "orgu") as $new){
130  if(!in_array($new["child"], $closed) && !in_array($new["child"], $open))
131  $open[] = $new["child"];
132  }
133  }
134  return $closed;
135  }
136 
141  private function getAttrForOrgu($orgu){
142  global $tree;
143  $parent_ref = $tree->getParentId($orgu->getRefId());
144  if($parent_ref != ilObjOrgUnit::getRootOrgRefId()){
145  $parent = new ilObjOrgUnit($parent_ref);
146  $ou_parent_id = $parent->getRefId();
147  } else {
148  $ou_parent_id = "__ILIAS";
149  }
150  // Only the ref id is guaranteed to be unique.
151  $ou_id = $orgu->getRefId();
152  $attr = array("ou_id" => $ou_id, "ou_id_type" => "reference_id", "ou_parent_id" => $ou_parent_id, "ou_parent_id_type" => "reference_id", "action" => "create");
153  return $attr;
154  }
155 }
156 ?>
static makeDirParents($a_dir)
Create a new directory and all parent directories.
Class ilOrgUnitExporter.
const ILIAS_VERSION_NUMERIC
Class for category export.
_createExportDirectory($a_obj_id, $a_export_type="xml", $a_obj_type="")
Create export directory.
XML writer class.
const IL_CAL_UNIX
Class ilExcelWriterAdapter.
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
static getRootOrgRefId()
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