1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5* @classDescription Stores information of creation date and versions of export files
6*
7* @author Stefan Meyer <meyer@leifos.com>
8*
9* @version $Id$
10*
11* @ingroup ServicesExport
12*/
13class ilExportFileInfo
14{
15    const CURRENT_VERSION = "4.1.0";
16
17
18    private $obj_id = 0;
19    private $version = self::CURRENT_VERSION;
20    private $export_type = '';
21    private $file_name = '';
22    private $create_date = null;
23
24    /**
25     * ilExportFileInfo constructor.
26     * @param int $a_obj_id
27     * @param string $a_export_type
28     * @param string $a_filename
29     */
30    public function __construct($a_obj_id, $a_export_type = '', $a_filename = '')
31    {
32        $this->obj_id = $a_obj_id;
33        $this->export_type = $a_export_type;
34        $this->file_name = $a_filename;
35        if ($this->getObjId() and $this->getExportType() and $this->getFilename()) {
36            $this->read();
37        }
38    }
39
40    /**
41     * Lookup last export
42     * @param object $a_obj_id
43     * @param string type xml | html | scorm2004...
44     * @param string version
45     * @return object ilExportFileInfo
46     */
47    public static function lookupLastExport($a_obj_id, $a_type, $a_version = '')
48    {
49        global $DIC;
50
51        $ilDB = $DIC['ilDB'];
52
53        $query = "SELECT * FROM export_file_info " .
54            "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer') . ' ' .
55            "AND export_type = " . $ilDB->quote($a_type, 'text') . ' ' .
56            "ORDER BY create_date DESC";
57        $res = $ilDB->query($query);
58        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
59            if (!$a_version or $row->version == $a_version) {
60                return new ilExportFileInfo($row->obj_id, $row->export_type, $row->filename);
61            }
62        }
63        return null;
64    }
65
66
67    /**
68     * Delete all export entries by obj_id
69     * @param object $a_obj_id
70     * @return
71     */
72    public static function deleteByObjId($a_obj_id)
73    {
74        global $DIC;
75
76        $ilDB = $DIC['ilDB'];
77
78        $ilDB->manipulate("DELETE FROM export_file_info WHERE obj_id = " . $ilDB->quote($a_obj_id));
79        return true;
80    }
81
82
83
84    /**
85     * set export type
86     * @param string $a_type xml | html ...
87     * @return
88     */
89    public function setExportType($a_type)
90    {
91        $this->export_type = $a_type;
92    }
93
94    /**
95     * get export type
96     * @return string export type
97     */
98    public function getExportType()
99    {
100        return $this->export_type;
101    }
102
103    /**
104     * set filename
105     * @param string $a_name
106     * @return
107     */
108    public function setFilename($a_name)
109    {
110        $this->file_name = $a_name;
111    }
112
113    /**
114     * get filename
115     * @return
116     */
117    public function getFilename()
118    {
119        return $this->file_name;
120    }
121
122    public function getBasename($a_ext = '.zip')
123    {
124        return basename($this->getFilename(), $a_ext);
125    }
126
127    /**
128     * Set obj id
129     * @param object $a_id
130     * @return
131     */
132    public function setObjId($a_id)
133    {
134        $this->obj_id = $a_id;
135    }
136
137    /**
138     * Get obj id
139     * @return
140     */
141    public function getObjId()
142    {
143        return $this->obj_id;
144    }
145
146    /**
147     * set version
148     * @return
149     */
150    public function setVersion($a_version)
151    {
152        $this->version = $a_version;
153    }
154
155    /**
156     * get version
157     * @return
158     */
159    public function getVersion()
160    {
161        return $this->version;
162    }
163
164    /**
165     * get creation date
166     * @return ilDateTime $date
167     */
168    public function getCreationDate()
169    {
170        return $this->create_date instanceof ilDateTime ? $this->create_date : new ilDateTime(time(), IL_CAL_UNIX);
171    }
172
173    /**
174     * set creation date
175     * @param ilDateTime $dt [optional]
176     * @return
177     */
178    public function setCreationDate(ilDateTime $dt = null)
179    {
180        $this->create_date = $dt;
181    }
182
183    /**
184     * Create new export entry
185     */
186    public function create()
187    {
188        global $DIC;
189
190        $db = $DIC->database();
191
192        $exists_query = 'select * from export_file_info ' .
193            'where obj_id = ' . $db->quote($this->obj_id, 'integer') . ' ' .
194            'and export_type = ' . $db->quote($this->getExportType(), 'text') . ' ' .
195            'and filename = ' . $db->quote($this->getFilename(), 'text');
196        $exists_res = $db->query($exists_query);
197
198        if (!$exists_res->numRows()) {
199            $query = "INSERT INTO export_file_info (obj_id, export_type, filename, version, create_date) " .
200                "VALUES ( " .
201                $db->quote($this->getObjId(), 'integer') . ', ' .
202                $db->quote($this->getExportType(), 'text') . ', ' .
203                $db->quote($this->getFilename(), 'text') . ', ' .
204                $db->quote($this->getVersion(), 'text') . ', ' .
205                $db->quote($this->getCreationDate()->get(IL_CAL_DATETIME, '', ilTimeZone::UTC), 'timestamp') . ' ' .
206                ")";
207            $db->manipulate($query);
208        }
209    }
210
211    /**
212     * Delete one export entry
213     * @return
214     */
215    public function delete()
216    {
217        global $DIC;
218
219        $ilDB = $DIC['ilDB'];
220
221        $ilDB->manipulate(
222            'DELETE FROM export_file_info ' .
223            'WHERE obj_id = ' . $ilDB->quote($this->getObjId(), 'integer') . ' ' .
224            'AND filename = ' . $ilDB->quote($this->getFilename(), 'text')
225        );
226        return true;
227    }
228
229    /**
230     * Read
231     * @return
232     */
233    protected function read()
234    {
235        global $DIC;
236
237        $ilDB = $DIC['ilDB'];
238
239        $query = "SELECT * FROM export_file_info " .
240            "WHERE obj_id = " . $ilDB->quote($this->getObjId(), 'integer') . ' ' .
241            "AND export_type = " . $ilDB->quote($this->getExportType(), 'text') . ' ' .
242            "AND filename = " . $ilDB->quote($this->getFilename(), 'text');
243
244        $res = $ilDB->query($query);
245        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
246            $this->setVersion($row->version);
247            $this->setCreationDate(new ilDateTime($row->create_date, IL_CAL_DATETIME, ilTimeZone::UTC));
248        }
249        return true;
250    }
251}
252