1<?php
2
3/**
4 * Class ilDataCollectionExporter
5 *
6 * @author Stefan Wanzenried <sw@studer-raimann.ch>
7 * @author Fabian Schmid <fs@studer-raimann.ch>
8 */
9class ilDataCollectionExporter extends ilXmlExporter
10{
11
12    /**
13     * @var ilDataCollectionDataSet
14     */
15    protected $ds;
16    /**
17     * @var ilDB
18     */
19    protected $db;
20
21
22    public function init()
23    {
24        global $DIC;
25        $ilDB = $DIC['ilDB'];
26        $this->ds = new ilDataCollectionDataSet();
27        $this->ds->setDSPrefix('ds');
28        $this->db = $ilDB;
29    }
30
31
32    /**
33     * @param string $a_entity
34     *
35     * @return array
36     */
37    public function getValidSchemaVersions($a_entity)
38    {
39        return array(
40            '4.5.0' => array(
41                'namespace' => 'http://www.ilias.de/Modules/DataCollection/dcl/4_5',
42                'xsd_file" => "ilias_dcl_4_5.xsd',
43                'uses_dataset' => true,
44                'min' => '4.5.0',
45                'max' => '',
46            ),
47        );
48    }
49
50
51    public function getXmlRepresentation($a_entity, $a_schema_version, $a_id)
52    {
53        ilUtil::makeDirParents($this->getAbsoluteExportDirectory());
54        $this->ds->setExportDirectories($this->dir_relative, $this->dir_absolute);
55
56        return $this->ds->getXmlRepresentation($a_entity, $a_schema_version, $a_id, '', true, true);
57    }
58
59
60    /**
61     * MOB/File fieldtypes objects are head dependencies
62     * They must be exported and imported first, so the new DC has the new IDs of those objects available
63     *
64     * @param $a_entity
65     * @param $a_target_release
66     * @param $a_ids
67     *
68     * @return array
69     */
70    public function getXmlExportHeadDependencies($a_entity, $a_target_release, $a_ids)
71    {
72        $dependencies = array(
73            ilDclDatatype::INPUTFORMAT_FILE => array(
74                'component' => 'Modules/File',
75                'entity' => 'file',
76                'ids' => array(),
77            ),
78            ilDclDatatype::INPUTFORMAT_MOB => array(
79                'component' => 'Services/MediaObjects',
80                'entity' => 'mob',
81                'ids' => array(),
82            ),
83        );
84
85        // Direct SQL query is faster than looping over objects
86        foreach ($a_ids as $dcl_obj_id) {
87            $sql = "SELECT stloc2.value AS ext_id, f." . $this->db->quoteIdentifier('datatype_id') . " FROM il_dcl_stloc2_value AS stloc2 "
88                . "INNER JOIN il_dcl_record_field AS rf ON (rf." . $this->db->quoteIdentifier('id') . " = stloc2." . $this->db->quoteIdentifier('record_field_id') . ") "
89                . "INNER JOIN il_dcl_field AS f ON (rf." . $this->db->quoteIdentifier('field_id') . " = f." . $this->db->quoteIdentifier('id') . ") " . "INNER JOIN il_dcl_table AS t ON (t."
90                . $this->db->quoteIdentifier('id') . " = f." . $this->db->quoteIdentifier('table_id') . ") "
91                . "WHERE t." . $this->db->quoteIdentifier('obj_id') . " = " . $this->db->quote($dcl_obj_id, 'integer') . " " . "AND f.datatype_id IN ("
92                . implode(',', array_keys($dependencies)) . ") AND stloc2." . $this->db->quoteIdentifier('value') . " IS NOT NULL";
93            $set = $this->db->query($sql);
94            while ($rec = $this->db->fetchObject($set)) {
95                $dependencies[$rec->datatype_id]['ids'][] = (int) $rec->ext_id;
96            }
97        }
98
99        // Return external dependencies/IDs if there are any
100        $return = array();
101        if (count($dependencies[ilDclDatatype::INPUTFORMAT_FILE]['ids'])) {
102            $return[] = $dependencies[ilDclDatatype::INPUTFORMAT_FILE];
103        }
104        if (count($dependencies[ilDclDatatype::INPUTFORMAT_MOB]['ids'])) {
105            $return[] = $dependencies[ilDclDatatype::INPUTFORMAT_MOB];
106        }
107
108        return $return;
109    }
110
111
112    /**
113     * @param $a_entity
114     * @param $a_target_release
115     * @param $a_ids
116     *
117     * @return array
118     */
119    public function getXmlExportTailDependencies($a_entity, $a_target_release, $a_ids)
120    {
121        $page_object_ids = array();
122        foreach ($a_ids as $dcl_obj_id) {
123            // If a DCL table has a detail view, we need to export the associated page objects!
124            $sql = "SELECT page_id FROM page_object "
125                . "WHERE parent_type = " . $this->db->quote('dclf', 'text') . " AND parent_id = " . $this->db->quote($dcl_obj_id, 'integer');
126            $set = $this->db->query($sql);
127            while ($rec = $this->db->fetchObject($set)) {
128                $page_object_ids[] = "dclf:" . $rec->page_id;
129            }
130        }
131        if (count($page_object_ids)) {
132            return array(
133                array(
134                    'component' => 'Services/COPage',
135                    'entity' => 'pg',
136                    'ids' => $page_object_ids,
137                ),
138            );
139        }
140
141        return array();
142    }
143}
144