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
24include_once './Services/WebServices/ECS/classes/class.ilECSParticipantSetting.php';
25include_once './Services/WebServices/ECS/classes/class.ilECSDataMappingSetting.php';
26
27/**
28*
29* @author Stefan Meyer <meyer@leifos.com>
30* @version $Id$
31*
32*
33* @ingroup ServicesWebServicesECS
34*/
35class ilECSDataMappingSettings
36{
37    private static $instances = null;
38
39    private $settings = null;
40    private $mappings = array();
41
42    /**
43     * Singleton Constructor
44     *
45     * @access private
46     *
47     */
48    private function __construct($a_server_id)
49    {
50        $this->settings = ilECSSetting::getInstanceByServerId($a_server_id);
51        $this->read();
52    }
53
54    /**
55     * Get Singleton instance
56     *
57     * @access public
58     * @static
59     * @deprecated
60     */
61    public static function _getInstance()
62    {
63        $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Using deprecate call');
64        $GLOBALS['DIC']['ilLog']->logStack();
65
66        return self::getInstanceByServerId(1);
67    }
68
69    /**
70     * Get singleton instance
71     * @param int $a_server_id
72     * @return ilECSDataMappingSettings
73     */
74    public static function getInstanceByServerId($a_server_id)
75    {
76        if (isset(self::$instances[$a_server_id])) {
77            return self::$instances[$a_server_id];
78        }
79        return self::$instances[$a_server_id] = new ilECSDataMappingSettings($a_server_id);
80    }
81
82    /**
83     * Delete server
84     * @global ilDB $ilDB
85     * @param int $a_server_id
86     */
87    public static function delete($a_server_id)
88    {
89        global $DIC;
90
91        $ilDB = $DIC['ilDB'];
92
93        $query = 'DELETE from ecs_data_mapping ' .
94            'WHERE sid = ' . $ilDB->quote($a_server_id, 'integer');
95        $ilDB->manipulate($query);
96    }
97
98    /**
99     * Get actice ecs setting
100     * @return ilECSSetting
101     */
102    public function getServer()
103    {
104        return $this->settings;
105    }
106
107
108    /**
109     * get mappings
110     *
111     * @access public
112     *
113     */
114    public function getMappings($a_mapping_type = 0)
115    {
116        if (!$a_mapping_type) {
117            $a_mapping_type = ilECSDataMappingSetting::MAPPING_IMPORT_RCRS;
118        }
119        return $this->mappings[$a_mapping_type];
120    }
121
122
123    /**
124     * get mapping by key
125     *
126     * @access public
127     * @param int mapping type import, export, crs, rcrs
128     * @param string ECS data field name. E.g. 'lecturer'
129     * @return int AdvancedMetaData field id or 0 (no mapping)
130     *
131     */
132    public function getMappingByECSName($a_mapping_type, $a_key)
133    {
134        if (!$a_mapping_type) {
135            $a_mapping_type = ilECSDataMappingSetting::MAPPING_IMPORT_RCRS;
136        }
137
138        return array_key_exists($a_key, (array) $this->mappings[$a_mapping_type]) ?
139            $this->mappings[$a_mapping_type][$a_key] :
140            0;
141    }
142
143
144
145    /**
146     * Read settings
147     *
148     * @access private
149     *
150     */
151    private function read()
152    {
153        global $DIC;
154
155        $ilDB = $DIC['ilDB'];
156
157        $this->mappings = array();
158
159        $query = 'SELECT * FROM ecs_data_mapping ' .
160            'WHERE sid = ' . $ilDB->quote($this->getServer()->getServerId(), 'integer') . ' ';
161        $res = $ilDB->query($query);
162        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
163            $this->mappings[$row->mapping_type][$row->ecs_field] = $row->advmd_id;
164        }
165    }
166}
167