ILIAS  release_8 Revision v8.24
class.ilAdvancedMDRecordExportFiles.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/*
5 +-----------------------------------------------------------------------------+
6 | ILIAS open source |
7 +-----------------------------------------------------------------------------+
8 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
9 | |
10 | This program is free software; you can redistribute it and/or |
11 | modify it under the terms of the GNU General Public License |
12 | as published by the Free Software Foundation; either version 2 |
13 | of the License, or (at your option) any later version. |
14 | |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU General Public License |
21 | along with this program; if not, write to the Free Software |
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 +-----------------------------------------------------------------------------+
24*/
25
32{
33 protected string $export_dir = '';
34
35 public function __construct(int $a_obj_id = null)
36 {
37 $this->export_dir = ilFileUtils::getDataDir() . '/ilAdvancedMetaData/export';
38 if ($a_obj_id) {
39 $this->export_dir .= "_" . $a_obj_id;
40 }
41 $this->init();
42 }
43
49 public function readFilesInfo(): array
50 {
51 $file_info = array();
52 foreach ($this->getFiles() as $name => $data) {
53 if ($data['type'] != 'file') {
54 continue;
55 }
56 $file_parts = explode('.', $name);
57 if (!is_numeric($file_parts[0]) or (strcmp('xml', $file_parts[1]) != 0)) {
58 continue;
59 }
60 $file_info = [];
61 $file_info[$file_parts[0]]['size'] = $data['size'];
62 $file_info[$file_parts[0]]['date'] = $file_parts[0];
63
64 if ($xml = simplexml_load_file($this->export_dir . '/' . $name)) {
65 $records = array();
66 foreach ($xml->xpath('Record/Title') as $title) {
67 $records[] = (string) $title;
68 }
69 $file_info[$file_parts[0]]['name'] = $records;
70 }
71 }
72 return $file_info;
73 }
74
79 public function getFiles(): array
80 {
81 if (!is_dir($this->export_dir)) {
82 return array();
83 }
84 $files = [];
85 foreach (ilFileUtils::getDir($this->export_dir) as $file_name => $file_data) {
86 $files[$file_name] = $file_data;
87 }
88 return $files;
89 }
90
94 public function create(string $a_xml): void
95 {
96 global $DIC;
97
98 $ilLog = $DIC['ilLog'];
99
100 if (!$fp = fopen($this->export_dir . '/' . time() . '.xml', 'w+')) {
101 $ilLog->write(__METHOD__ . ': Cannot open file ' . $this->export_dir . '/' . time() . '.xml');
102 throw new ilException('Cannot write export file.');
103 }
104
105 fwrite($fp, $a_xml);
106 fclose($fp);
107 }
108
109 public function deleteByFileId(int $a_timest): bool
110 {
111 global $DIC;
112
113 $ilLog = $DIC['ilLog'];
114
115 if (!unlink($this->export_dir . '/' . $a_timest . '.xml')) {
116 $ilLog->write(__METHOD__ . ': Cannot delete file ' . $this->export_dir . '/' . $a_timest . '.xml');
117 return false;
118 }
119 return true;
120 }
121
122 public function getAbsolutePathByFileId(int $a_file_basename): string
123 {
124 global $DIC;
125
126 $ilLog = $DIC['ilLog'];
127
128 $a_file_basename = (string) $a_file_basename;
129 if (!file_exists($this->export_dir . '/' . $a_file_basename . '.xml')) {
130 $ilLog->write(__METHOD__ . ': Cannot find file ' . $this->export_dir . '/' . $a_file_basename . '.xml');
131 return '';
132 }
133 return $this->export_dir . '/' . $a_file_basename . '.xml';
134 }
135
140 private function init(): void
141 {
142 if (!is_dir($this->export_dir)) {
143 ilFileUtils::makeDirParents($this->export_dir);
144 }
145 }
146}
readFilesInfo()
Read files info @access public.
init()
init export directory @access private
create(string $a_xml)
Create new export file from xml string.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
static getDataDir()
get data directory (outside webspace)
global $DIC
Definition: feed.php:28
if($format !==null) $name
Definition: metadata.php:247
$xml
Definition: metadata.php:351