1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2001 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
24
25/**
26* Meta Data class (element classification)
27*
28* @package ilias-core
29* @version $Id$
30*/
31include_once 'class.ilMDBase.php';
32
33class ilMDClassification extends ilMDBase
34{
35    // METHODS OF CLIENT OBJECTS (TaxonPath, Keyword)
36    public function &getTaxonPathIds()
37    {
38        include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDTaxonPath.php';
39
40        return ilMDTaxonPath::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_classification');
41    }
42    public function &getTaxonPath($a_taxon_path_id)
43    {
44        include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDTaxonPath.php';
45
46        if (!$a_taxon_path_id) {
47            return false;
48        }
49        $tax = new ilMDTaxonPath();
50        $tax->setMetaId($a_taxon_path_id);
51
52        return $tax;
53    }
54    public function &addTaxonPath()
55    {
56        include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDTaxonPath.php';
57
58        $tax = new ilMDTaxonPath($this->getRBACId(), $this->getObjId(), $this->getObjType());
59        $tax->setParentId($this->getMetaId());
60        $tax->setParentType('meta_classification');
61
62        return $tax;
63    }
64
65    public function &getKeywordIds()
66    {
67        include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDKeyword.php';
68
69        return ilMDKeyword::_getIds($this->getRBACId(), $this->getObjId(), $this->getMetaId(), 'meta_classification');
70    }
71    public function &getKeyword($a_keyword_id)
72    {
73        include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDKeyword.php';
74
75        if (!$a_keyword_id) {
76            return false;
77        }
78        $key = new ilMDKeyword();
79        $key->setMetaId($a_keyword_id);
80
81        return $key;
82    }
83    public function &addKeyword()
84    {
85        include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDKeyword.php';
86
87        $key = new ilMDKeyword($this->getRBACId(), $this->getObjId(), $this->getObjType());
88        $key->setParentId($this->getMetaId());
89        $key->setParentType('meta_classification');
90
91        return $key;
92    }
93
94    // SET/GET
95    public function setPurpose($a_purpose)
96    {
97        switch ($a_purpose) {
98            case 'Discipline':
99            case 'Idea':
100            case 'Prerequisite':
101            case 'EducationalObjective':
102            case 'AccessibilityRestrictions':
103            case 'EducationalLevel':
104            case 'SkillLevel':
105            case 'SecurityLevel':
106            case 'Competency':
107                $this->purpose = $a_purpose;
108                return true;
109
110            default:
111                return false;
112        }
113    }
114    public function getPurpose()
115    {
116        return $this->purpose;
117    }
118    public function setDescription($a_description)
119    {
120        $this->description = $a_description;
121    }
122    public function getDescription()
123    {
124        return $this->description;
125    }
126    public function setDescriptionLanguage(&$lng_obj)
127    {
128        if (is_object($lng_obj)) {
129            $this->description_language = &$lng_obj;
130        }
131    }
132    public function &getDescriptionLanguage()
133    {
134        return is_object($this->description_language) ? $this->description_language : false;
135    }
136    public function getDescriptionLanguageCode()
137    {
138        return is_object($this->description_language) ? $this->description_language->getLanguageCode() : false;
139    }
140
141
142    public function save()
143    {
144        if ($this->db->autoExecute(
145            'il_meta_classification',
146            $this->__getFields(),
147            ilDBConstants::AUTOQUERY_INSERT
148        )) {
149            $this->setMetaId($this->db->getLastInsertId());
150
151            return $this->getMetaId();
152        }
153        return false;
154    }
155
156    public function update()
157    {
158        global $ilDB;
159
160        if ($this->getMetaId()) {
161            if ($this->db->autoExecute(
162                'il_meta_classification',
163                $this->__getFields(),
164                ilDBConstants::AUTOQUERY_UPDATE,
165                "meta_classification_id = " . $ilDB->quote($this->getMetaId())
166            )) {
167                return true;
168            }
169        }
170        return false;
171    }
172
173    public function delete()
174    {
175        global $ilDB;
176
177        if ($this->getMetaId()) {
178            $query = "DELETE FROM il_meta_classification " .
179                "WHERE meta_classification_id = " . $ilDB->quote($this->getMetaId());
180
181            $this->db->query($query);
182
183            foreach ($this->getTaxonPathIds() as $id) {
184                $tax = &$this->getTaxonPath($id);
185                $tax->delete();
186            }
187            foreach ($this->getKeywordIds() as $id) {
188                $key = &$this->getKeyword($id);
189                $key->delete();
190            }
191
192            return true;
193        }
194        return false;
195    }
196
197
198    public function __getFields()
199    {
200        return array('rbac_id' => $this->getRBACId(),
201                     'obj_id' => $this->getObjId(),
202                     'obj_type' => ilUtil::prepareDBString($this->getObjType()),
203                     'purpose' => ilUtil::prepareDBString($this->getPurpose()),
204                     'description' => ilUtil::prepareDBString($this->getDescription()),
205                     'description_language' => ilUtil::prepareDBString($this->getDescriptionLanguageCode()));
206    }
207
208    public function read()
209    {
210        global $ilDB;
211
212        include_once 'Services/Migration/DBUpdate_426/classes/class.ilMDLanguageItem.php';
213
214        if ($this->getMetaId()) {
215            $query = "SELECT * FROM il_meta_classification " .
216                "WHERE meta_classification_id = " . $ilDB->quote($this->getMetaId());
217
218            $res = $this->db->query($query);
219            while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
220                $this->setRBACId($row->rbac_id);
221                $this->setObjId($row->obj_id);
222                $this->setObjType($row->obj_type);
223                $this->setPurpose(ilUtil::stripSlashes($row->purpose));
224                $this->setDescription(ilUtil::stripSlashes($row->description));
225                $this->description_language = new ilMDLanguageItem($row->description_language);
226            }
227        }
228        return true;
229    }
230
231    /*
232     * XML Export of all meta data
233     * @param object (xml writer) see class.ilMD2XML.php
234     *
235     */
236    public function toXML(&$writer)
237    {
238        $writer->xmlStartTag('Classification', array('Purpose' => $this->getPurpose()));
239
240        // Taxon Path
241        foreach ($this->getTaxonPathIds() as $id) {
242            $tax = &$this->getTaxonPath($id);
243            $tax->toXML($writer);
244        }
245        // Description
246        $writer->xmlElement('Description', array('Language' => $this->getDescriptionLanguageCode()), $this->getDescription());
247
248        // Keyword
249        foreach ($this->getKeywordIds() as $id) {
250            $key = &$this->getKeyword($id);
251            $key->toXML($writer);
252        }
253        $writer->xmlEndTag('Classification');
254    }
255
256
257
258    // STATIC
259    public function _getIds($a_rbac_id, $a_obj_id)
260    {
261        global $ilDB;
262
263        $query = "SELECT meta_classification_id FROM il_meta_classification " .
264            "WHERE rbac_id = " . $ilDB->quote($a_rbac_id) . " " .
265            "AND obj_id = " . $ilDB->quote($a_obj_id);
266
267
268        $res = $ilDB->query($query);
269        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
270            $ids[] = $row->meta_classification_id;
271        }
272        return $ids ? $ids : array();
273    }
274}
275