ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilAdvancedMDRecordExportFiles.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
33 {
34  protected $export_dir = '';
35 
42  public function __construct($a_obj_id = null)
43  {
44  $this->export_dir = ilUtil::getDataDir() . '/ilAdvancedMetaData/export';
45  if ($a_obj_id) {
46  $this->export_dir .= "_" . $a_obj_id;
47  }
48  $this->init();
49  }
50 
57  public function readFilesInfo()
58  {
59  $file_info = array();
60  foreach ($this->getFiles() as $name => $data) {
61  if ($data['type'] != 'file') {
62  continue;
63  }
64  $file_parts = explode('.', $name);
65  if (!is_numeric($file_parts[0]) or (strcmp('xml', $file_parts[1]) != 0)) {
66  continue;
67  }
68  $file_info[$file_parts[0]]['size'] = $data['size'];
69  $file_info[$file_parts[0]]['date'] = $file_parts[0];
70 
71  if ($xml = simplexml_load_file($this->export_dir . '/' . $name)) {
72  $records = array();
73  foreach ($xml->xpath('Record/Title') as $title) {
74  $records[] = (string) $title;
75  }
76  $file_info[$file_parts[0]]['name'] = $records;
77  }
78  }
79  return $file_info ? $file_info : array();
80  }
81 
88  public function getFiles()
89  {
90  if (!@is_dir($this->export_dir)) {
91  return array();
92  }
93  foreach (ilUtil::getDir($this->export_dir) as $file_name => $file_data) {
94  $files[$file_name] = $file_data;
95  }
96  return $files ? $files : array();
97  }
98 
106  public function create($a_xml)
107  {
108  global $DIC;
109 
110  $ilLog = $DIC['ilLog'];
111 
112  if (!$fp = @fopen($this->export_dir . '/' . time() . '.xml', 'w+')) {
113  $ilLog->write(__METHOD__ . ': Cannot open file ' . $this->export_dir . '/' . time() . '.xml');
114 
115  require_once './Services/Exceptions/classes/class.ilException.php';
116  throw new ilException('Cannot write export file.');
117  }
118 
119  @fwrite($fp, $a_xml);
120  @fclose($fp);
121  }
122 
129  public function deleteByFileId($a_timest)
130  {
131  global $DIC;
132 
133  $ilLog = $DIC['ilLog'];
134 
135  if (!unlink($this->export_dir . '/' . $a_timest . '.xml')) {
136  $ilLog->write(__METHOD__ . ': Cannot delete file ' . $this->export_dir . '/' . $a_timest . '.xml');
137  return false;
138  }
139  return true;
140  }
141 
149  public function getAbsolutePathByFileId($a_file_basename)
150  {
151  global $DIC;
152 
153  $ilLog = $DIC['ilLog'];
154 
155  if (!@file_exists($this->export_dir . '/' . $a_file_basename . '.xml')) {
156  $ilLog->write(__METHOD__ . ': Cannot find file ' . $this->export_dir . '/' . $a_file_basename . '.xml');
157  return false;
158  }
159  return $this->export_dir . '/' . $a_file_basename . '.xml';
160  }
161 
168  private function init()
169  {
170  if (!@is_dir($this->export_dir)) {
171  ilUtil::makeDirParents($this->export_dir);
172  }
173  }
174 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
$files
Definition: metarefresh.php:49
global $DIC
Definition: saml.php:7
create($a_xml)
Create new export file from xml string.
static getDir($a_dir, $a_rec=false, $a_sub_dir="")
get directory
$records
Definition: simple_test.php:22
getAbsolutePathByFileId($a_file_basename)
Get absolut path by file id.
static getDataDir()
get data directory (outside webspace)
$data
Definition: bench.php:6