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 <smeyer.ilias@gmx.de>
27* @version $Id$
28*
29* @ingroup ServicesCalendar
30*/
31
32class ilCalendarCategoryAssignments
33{
34    protected $db;
35
36    protected $cal_entry_id = 0;
37    protected $assignments = array();
38
39    /**
40     * Constructor
41     *
42     * @access public
43     * @param int calendar entry id
44     */
45    public function __construct($a_cal_entry_id)
46    {
47        global $DIC;
48
49        $ilDB = $DIC['ilDB'];
50
51        $this->db = $ilDB;
52        $this->cal_entry_id = $a_cal_entry_id;
53
54        $this->read();
55    }
56
57    /**
58     * lookup categories
59     *
60     * @access public
61     * @param int cal_id
62     * @return array of categories
63     * @static
64     */
65    public static function _lookupCategories($a_cal_id)
66    {
67        global $DIC;
68
69        $ilDB = $DIC['ilDB'];
70
71        $query = "SELECT cat_id FROM cal_cat_assignments " .
72            "WHERE cal_id = " . $ilDB->quote($a_cal_id, 'integer') . " ";
73        $res = $ilDB->query($query);
74        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
75            $cat_ids[] = $row->cat_id;
76        }
77        return $cat_ids ? $cat_ids : array();
78    }
79
80    /**
81     * Lookup category id
82     *
83     * @access public
84     * @param
85     * @return
86     * @static
87     */
88    public static function _lookupCategory($a_cal_id)
89    {
90        if (count($cats = self::_lookupCategories($a_cal_id))) {
91            return $cats[0];
92        }
93        return 0;
94    }
95
96    /**
97     * lookup calendars for appointment ids
98     *
99     * @access public
100     * @param	array	$a_cal_ids
101     * @static
102     */
103    public static function _getAppointmentCalendars($a_cal_ids)
104    {
105        global $DIC;
106
107        $ilDB = $DIC['ilDB'];
108
109        $query = "SELECT * FROM cal_cat_assignments " .
110            "WHERE " . $ilDB->in('cal_id', $a_cal_ids, false, 'integer');
111        $res = $ilDB->query($query);
112        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
113            $map[$row->cal_id] = $row->cat_id;
114        }
115        return $map ? $map : array();
116    }
117
118    /**
119     * Get assigned apointments
120     *
121     * @access public
122     * @param	array	$a_cat_id
123     * @static
124     */
125    public static function _getAssignedAppointments($a_cat_id)
126    {
127        global $DIC;
128
129        $ilDB = $DIC['ilDB'];
130
131        $query = "SELECT * FROM cal_cat_assignments " .
132            "WHERE " . $ilDB->in('cat_id', $a_cat_id, false, 'integer');
133
134        $res = $ilDB->query($query);
135        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
136            $cal_ids[] = $row->cal_id;
137        }
138        return $cal_ids ? $cal_ids : array();
139    }
140
141    /**
142     * Get number of assigned appoitments
143     * @param type $a_cat_id
144     */
145    public static function lookupNumberOfAssignedAppointments($a_cat_ids)
146    {
147        global $DIC;
148
149        $ilDB = $DIC['ilDB'];
150
151        $query = 'SELECT COUNT(*) num FROM cal_cat_assignments ' .
152                'WHERE ' . $ilDB->in('cat_id', $a_cat_ids, false, 'integer');
153        $res = $ilDB->query($query);
154        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
155            return $row->num;
156        }
157        return 0;
158    }
159
160    /**
161     * get automatic generated appointments of category
162     *
163     * @access public
164     * @param int obj_id
165     * @return
166     * @static
167     */
168    public static function _getAutoGeneratedAppointmentsByObjId($a_obj_id)
169    {
170        global $DIC;
171
172        $ilDB = $DIC['ilDB'];
173
174        $query = "SELECT ce.cal_id FROM cal_categories cc " .
175            "JOIN cal_cat_assignments cca ON cc.cat_id = cca.cat_id " .
176            "JOIN cal_entries ce ON cca.cal_id = ce.cal_id " .
177            "WHERE auto_generated = 1 " .
178            "AND obj_id = " . $ilDB->quote($a_obj_id, 'integer') . " ";
179        $res = $ilDB->query($query);
180        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
181            $apps[] = $row->cal_id;
182        }
183        return $apps ? $apps : array();
184    }
185
186    /**
187     * Delete appointment assignment
188     *
189     * @access public
190     * @param int appointment id
191     * @static
192     */
193    public static function _deleteByAppointmentId($a_app_id)
194    {
195        global $DIC;
196
197        $ilDB = $DIC['ilDB'];
198
199        $query = "DELETE FROM cal_cat_assignments " .
200            "WHERE cal_id = " . $ilDB->quote($a_app_id, 'integer') . " ";
201        $res = $ilDB->manipulate($query);
202
203        return true;
204    }
205
206    /**
207     * Delete assignments by category id
208     *
209     * @access public
210     * @param int category_id
211     * @return
212     * @static
213     */
214    public static function _deleteByCategoryId($a_cat_id)
215    {
216        global $DIC;
217
218        $ilDB = $DIC['ilDB'];
219
220        $query = "DELETE FROM cal_cat_assignments " .
221            "WHERE cat_id = " . $ilDB->quote($a_cat_id, 'integer') . " ";
222        $res = $ilDB->manipulate($query);
223        return true;
224    }
225
226    /**
227     * get first assignment
228     *
229     * @access public
230     * @return
231     */
232    public function getFirstAssignment()
233    {
234        return isset($this->assignments[0]) ? $this->assignments[0] : false;
235    }
236
237    /**
238     * get assignments
239     *
240     * @access public
241     * @return
242     */
243    public function getAssignments()
244    {
245        return $this->assignments ? $this->assignments : array();
246    }
247
248    /**
249     * add assignment
250     *
251     * @access public
252     * @param int calendar category id
253     * @return
254     */
255    public function addAssignment($a_cal_cat_id)
256    {
257        global $DIC;
258
259        $ilDB = $DIC['ilDB'];
260
261        $query = "INSERT INTO cal_cat_assignments (cal_id,cat_id) " .
262            "VALUES ( " .
263            $this->db->quote($this->cal_entry_id, 'integer') . ", " .
264            $this->db->quote($a_cal_cat_id, 'integer') . " " .
265            ")";
266        $res = $ilDB->manipulate($query);
267        $this->assignments[] = (int) $a_cal_cat_id;
268
269        return true;
270    }
271
272    /**
273     * delete assignment
274     *
275     * @access public
276     * @param int calendar category id
277     * @return
278     */
279    public function deleteAssignment($a_cat_id)
280    {
281        global $DIC;
282
283        $ilDB = $DIC['ilDB'];
284
285        $query = "DELETE FROM cal_cat_assignments " .
286            "WHERE cal_id = " . $this->db->quote($this->cal_entry_id, 'integer') . ", " .
287            "AND cat_id = " . $this->db->quote($a_cat_id, 'integer') . " ";
288        $res = $ilDB->manipulate($query);
289
290        if (($key = array_search($a_cat_id, $this->assignments)) !== false) {
291            unset($this->assignments[$key]);
292        }
293        return true;
294    }
295
296    /**
297     * delete assignments
298     *
299     * @access public
300     */
301    public function deleteAssignments()
302    {
303        global $DIC;
304
305        $ilDB = $DIC['ilDB'];
306
307        $query = "DELETE FROM cal_cat_assignments " .
308            "WHERE cal_id = " . $this->db->quote($this->cal_entry_id, 'integer') . " ";
309        $res = $ilDB->manipulate($query);
310        return true;
311    }
312
313
314    /**
315     * read assignments
316     *
317     * @access private
318     * @return
319     */
320    private function read()
321    {
322        global $DIC;
323
324        $ilDB = $DIC['ilDB'];
325
326        $query = "SELECT * FROM cal_cat_assignments " .
327            "WHERE cal_id = " . $this->db->quote($this->cal_entry_id, 'integer') . " ";
328
329        $res = $this->db->query($query);
330        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
331            $this->assignments[] = $row->cat_id;
332        }
333    }
334}
335