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
24/**
25*
26* @author Stefan Meyer <meyer@leifos.com>
27* @version $Id$
28*
29*
30* @ilCtrl_Calls
31* @ingroup ServicesWebServicesECS
32*/
33
34class ilECSUtils
35{
36    const TYPE_ARRAY = 1;
37    const TYPE_INT = 2;
38    const TYPE_STRING = 3;
39    const TYPE_TIMEPLACE = 4;
40
41    /**
42     * Lookup participant name
43     * @param int	$a_owner	Mid of participant
44     * @param int	$a_server_id
45     * @return
46     */
47    public static function lookupParticipantName($a_owner, $a_server_id)
48    {
49        global $DIC;
50
51        $ilLog = $DIC['ilLog'];
52
53        try {
54            include_once './Services/WebServices/ECS/classes/class.ilECSCommunityReader.php';
55            $reader = ilECSCommunityReader::getInstanceByServerId($a_server_id);
56            if ($part = $reader->getParticipantByMID($a_owner)) {
57                return $part->getParticipantName();
58            }
59            return '';
60        } catch (ilECSConnectorException $e) {
61            $ilLog->write(__METHOD__ . ': Error reading participants.');
62            return '';
63        }
64    }
65
66    /**
67     * get optional econtent fields
68     * These fields might be mapped against AdvancedMetaData field definitions
69     *
70     * @access public
71     * @static
72     *
73     */
74    public static function _getOptionalEContentFields()
75    {
76        // :TODO: ?
77        $def = self::getEContentDefinition('/campusconnect/courselinks');
78        return array_keys($def);
79    }
80
81    /**
82     * get optional econtent fields
83     * These fields might be mapped against AdvancedMetaData field definitions
84     *
85     * @access public
86     * @static
87     *
88     */
89    public static function _getOptionalECourseFields()
90    {
91        // :TODO: ?
92        $def = self::getEContentDefinition('/campusconnect/courselinks');
93        return array_keys($def);
94    }
95
96    /**
97     * Get all possible remote object types
98     *
99     * @param bool $a_with_captions
100     * @return array
101     */
102    public static function getPossibleRemoteTypes($a_with_captions = false)
103    {
104        global $DIC;
105
106        $lng = $DIC['lng'];
107
108        $all = array("rcrs", "rcat", "rfil", "rglo", "rgrp", "rlm", "rwik");
109
110        if (!$a_with_captions) {
111            return $all;
112        }
113
114        $res = array();
115        foreach ($all as $id) {
116            $res[$id] = $lng->txt("obj_" . $id);
117        }
118        return $res;
119    }
120
121    /**
122     * Get all possible release object types
123     *
124     * @param bool $a_with_captions
125     * @return array
126     */
127    public static function getPossibleReleaseTypes($a_with_captions = false)
128    {
129        global $DIC;
130
131        $lng = $DIC['lng'];
132
133        $all = array("crs", "cat", "file", "glo", "grp", "lm", "wiki");
134
135        if (!$a_with_captions) {
136            return $all;
137        }
138
139        $res = array();
140        foreach ($all as $id) {
141            $res[$id] = $lng->txt("obj_" . $id);
142        }
143        return $res;
144    }
145
146    /**
147     * Get econtent / metadata definition
148     *
149     * @param string $a_resource_id
150     * @return array
151     */
152    public static function getEContentDefinition($a_resource_id)
153    {
154        switch ($a_resource_id) {
155            case '/campusconnect/courselinks':
156                return array(
157                    'study_courses' => self::TYPE_ARRAY,
158                    'lecturer' => self::TYPE_ARRAY,
159                    'courseType' => self::TYPE_STRING,
160                    'courseID' => self::TYPE_INT,
161                    'credits' => self::TYPE_INT,
162                    'semester_hours' => self::TYPE_INT,
163                    'term' => self::TYPE_STRING,
164                    'begin' => array(self::TYPE_TIMEPLACE, 'timePlace'),
165                    'end' => array(self::TYPE_TIMEPLACE, 'timePlace'),
166                    'room' => array(self::TYPE_TIMEPLACE, 'timePlace'),
167                    'cycle' => array(self::TYPE_TIMEPLACE, 'timePlace')
168                );
169
170            case '/campusconnect/categories':
171            case '/campusconnect/files':
172            case '/campusconnect/glossaries':
173            case '/campusconnect/groups':
174            case '/campusconnect/learningmodules':
175            case '/campusconnect/wikis':
176                // no metadata mapping yet
177                return array();
178        }
179    }
180
181    /**
182     * Convert ECS content to rule matchable values
183     *
184     * @param string $a_resource_id
185     * @param int $a_server_id
186     * @param object $a_ecs_content
187     * @param int $a_owner
188     * @return array
189     */
190    public static function getMatchableContent($a_resource_id, $a_server_id, $a_ecs_content, $a_owner)
191    {
192        include_once './Services/WebServices/ECS/classes/class.ilECSCategoryMappingRule.php';
193        include_once './Services/WebServices/ECS/classes/class.ilECSCommunitiesCache.php';
194
195        // see ilECSCategoryMapping::getPossibleFields();
196        $res = array();
197        $res["part_id"] = array($a_owner, ilECSCategoryMappingRule::ATTR_INT);
198        $res["community"] = array(ilECSCommunitiesCache::getInstance()->lookupTitle($a_server_id, $a_owner),
199            ilECSCategoryMappingRule::ATTR_STRING);
200
201        $definition = self::getEContentDefinition($a_resource_id);
202
203        $timePlace = null;
204        foreach ($definition as $id => $type) {
205            if (is_array($type)) {
206                $target = $type[1];
207                $type = $type[0];
208            } else {
209                $target = $id;
210            }
211            switch ($type) {
212                case ilECSUtils::TYPE_ARRAY:
213                    $value = array(implode(',', (array) $a_ecs_content->$target), ilECSCategoryMappingRule::ATTR_ARRAY);
214                    break;
215
216                case ilECSUtils::TYPE_INT:
217                    $value = array((int) $a_ecs_content->$target, ilECSCategoryMappingRule::ATTR_INT);
218                    break;
219
220                case ilECSUtils::TYPE_STRING:
221                    $value = array((string) $a_ecs_content->$target, ilECSCategoryMappingRule::ATTR_STRING);
222                    break;
223
224                case ilECSUtils::TYPE_TIMEPLACE:
225                    if (!is_object($timePlace)) {
226                        include_once('./Services/WebServices/ECS/classes/class.ilECSTimePlace.php');
227                        if (is_object($a_ecs_content->$target)) {
228                            $timePlace = new ilECSTimePlace();
229                            $timePlace->loadFromJSON($a_ecs_content->$target);
230                        } else {
231                            $timePlace = new ilECSTimePlace();
232                        }
233                    }
234                    switch ($id) {
235                        case 'begin':
236                        case 'end':
237                            $value = array($timePlace->{'getUT' . ucfirst($id)}(),
238                                ilECSCategoryMappingRule::ATTR_INT);
239                            break;
240
241                        case 'room':
242                        case 'cycle':
243                            $value = array($timePlace->{'get' . ucfirst($id)}(),
244                                ilECSCategoryMappingRule::ATTR_STRING);
245                            break;
246                    }
247                    break;
248            }
249
250            $res[$id] = $value;
251        }
252
253        return $res;
254    }
255
256    /**
257     * Get advanced metadata values for object id
258     *
259     * @param int $a_obj_id
260     * @return array
261     */
262    public static function getAdvancedMDValuesForObjId($a_obj_id)
263    {
264        $res = array();
265
266        include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
267
268        // getting all records
269        foreach (ilAdvancedMDValues::getInstancesForObjectId($a_obj_id) as $a_values) {
270            // this correctly binds group and definitions
271            $a_values->read();
272
273            // getting elements for record
274            $defs = $a_values->getDefinitions();
275            foreach ($a_values->getADTGroup()->getElements() as $element_id => $element) {
276                if (!$element->isNull()) {
277                    // :TODO: using this for a "flat" presentation
278                    $res[$element_id] = $defs[$element_id]->getValueForXML($element);
279                } else {
280                    // :TODO: is this needed?
281                    $res[$element_id] = null;
282                }
283            }
284        }
285
286        return $res;
287    }
288}
289