1<?php
2/**
3*
4* @author Stefan Meyer <smeyer.ilias@gmx.de>
5* @version $Id$
6*
7* @ingroup ServicesCalendar
8*/
9include_once('./Services/Calendar/classes/class.ilCalendarSettings.php');
10
11class ilCalendarUserSettings
12{
13    const CAL_SELECTION_MEMBERSHIP = 1;
14    const CAL_SELECTION_ITEMS = 2;
15
16    const CAL_EXPORT_TZ_TZ = 1;
17    const CAL_EXPORT_TZ_UTC = 2;
18
19    public static $instances = array();
20
21    protected $user;
22    protected $settings;
23
24    private $calendar_selection_type = 1;
25    private $timezone;
26    private $export_tz_type = self::CAL_EXPORT_TZ_TZ;
27    private $weekstart;
28    private $time_format;
29    private $date_format;
30
31    private $day_start;
32    private $day_end;
33
34    /**
35     * @var bool
36     */
37    private $show_weeks = true;
38
39    /**
40     * Constructor
41     *
42     * @access private
43     * @param
44     * @return
45     */
46    private function __construct($a_user_id)
47    {
48        global $DIC;
49
50        $ilUser = $DIC['ilUser'];
51
52        if ($ilUser->getId() == $a_user_id) {
53            $this->user = $ilUser;
54        } else {
55            $this->user = ilObjectFactory::getInstanceByObjId($a_user_id, false);
56        }
57        $this->settings = ilCalendarSettings::_getInstance();
58        $this->read();
59    }
60
61    /**
62     * get singleton instance
63     *
64     * @access public
65     * @param int user id
66     * @return object ilCalendarUserSettings
67     * @static
68     */
69    public static function _getInstanceByUserId($a_user_id)
70    {
71        if (isset(self::$instances[$a_user_id])) {
72            return self::$instances[$a_user_id];
73        }
74        return self::$instances[$a_user_id] = new ilCalendarUserSettings($a_user_id);
75    }
76
77    /**
78     * get instance for logged in user
79     *
80     * @return object ilCalendarUserSettings
81     * @static
82     */
83    public static function _getInstance()
84    {
85        global $DIC;
86
87        $ilUser = $DIC['ilUser'];
88
89        return self::_getInstanceByUserId($ilUser->getId());
90    }
91
92    /**
93     * get Time zone
94     *
95     * @access public
96     * @param
97     * @return
98     */
99    public function getTimeZone()
100    {
101        return $this->timezone;
102    }
103
104    /**
105     * set timezone
106     *
107     * @access public
108     * @param
109     * @return
110     */
111    public function setTimeZone($a_tz)
112    {
113        $this->timezone = $a_tz;
114    }
115
116    /**
117     * Get export timezone setting
118     * @return type
119     */
120    public function getExportTimeZoneType()
121    {
122        return $this->export_tz_type;
123    }
124
125    /**
126     * Set export timezone type
127     * @param type $a_type
128     */
129    public function setExportTimeZoneType($a_type)
130    {
131        $this->export_tz_type = $a_type;
132    }
133
134    /**
135     * Get export timezone
136     * @return type
137     */
138    public function getExportTimeZone()
139    {
140        switch ($this->getExportTimeZoneType()) {
141            case self::CAL_EXPORT_TZ_TZ:
142                return $this->getTimeZone();
143
144            case self::CAL_EXPORT_TZ_UTC:
145                include_once './Services/Calendar/classes/class.ilTimeZone.php';
146                return ilTimeZone::UTC;
147        }
148    }
149
150
151
152    /**
153     * set week start
154     *
155     * @access public
156     * @param
157     * @return
158     */
159    public function setWeekStart($a_weekstart)
160    {
161        $this->weekstart = $a_weekstart;
162    }
163
164    /**
165     * get weekstart
166     *
167     * @access public
168     * @return
169     */
170    public function getWeekStart()
171    {
172        return (int) $this->weekstart;
173    }
174
175    /**
176     * Set start of day
177     * @return
178     * @param int $a_start
179     */
180    public function setDayStart($a_start)
181    {
182        $this->day_start = $a_start;
183    }
184
185    /**
186     * get start of day
187     * @return
188     */
189    public function getDayStart()
190    {
191        return $this->day_start;
192    }
193
194    /**
195     * Set day end
196     * @return
197     * @param int $a_end
198     */
199    public function setDayEnd($a_end)
200    {
201        $this->day_end = $a_end;
202    }
203
204    /**
205     * Get end of day
206     * @return
207     */
208    public function getDayEnd()
209    {
210        return $this->day_end;
211    }
212
213    /**
214     * set date format
215     *
216     * @access public
217     * @param int date
218     * @return
219     */
220    public function setDateFormat($a_format)
221    {
222        $this->date_format = $a_format;
223    }
224
225    /**
226     * get date format
227     *
228     * @access public
229     * @return int date format
230     */
231    public function getDateFormat()
232    {
233        return $this->date_format;
234    }
235
236    /**
237     * set time format
238     *
239     * @access public
240     * @param int time
241     * @return
242     */
243    public function setTimeFormat($a_format)
244    {
245        $this->time_format = $a_format;
246    }
247
248    /**
249     * get time format
250     *
251     * @access public
252     * @return int time format
253     */
254    public function getTimeFormat()
255    {
256        return $this->time_format;
257    }
258
259    /**
260     * get calendar selection type
261     * ("MyMembership" or "Selected Items")
262     *
263     * @return
264     */
265    public function getCalendarSelectionType()
266    {
267        return $this->calendar_selection_type;
268    }
269
270    /**
271     * set calendar selection type
272     * @param int $type self::CAL_SELECTION_MEMBERSHIP | self::CAL_SELECTION_ITEM
273     * @return void
274     */
275    public function setCalendarSelectionType($a_type)
276    {
277        $this->calendar_selection_type = $a_type;
278    }
279
280    /**
281     * Set show weeks
282     *
283     * @param bool $a_val show weeks
284     */
285    public function setShowWeeks($a_val)
286    {
287        $this->show_weeks = $a_val;
288    }
289
290    /**
291     * Get show weeks
292     *
293     * @return bool show weeks
294     */
295    public function getShowWeeks()
296    {
297        return $this->show_weeks;
298    }
299
300    /**
301     * save
302     *
303     * @access public
304     */
305    public function save()
306    {
307        $this->user->writePref('user_tz', $this->getTimeZone());
308        $this->user->writePref('export_tz_type', $this->getExportTimeZoneType());
309        $this->user->writePref('weekstart', $this->getWeekStart());
310        $this->user->writePref('date_format', $this->getDateFormat());
311        $this->user->writePref('time_format', $this->getTimeFormat());
312        $this->user->writePref('calendar_selection_type', $this->getCalendarSelectionType());
313        $this->user->writePref('day_start', $this->getDayStart());
314        $this->user->writePref('day_end', $this->getDayEnd());
315        $this->user->writePref('show_weeks', $this->getShowWeeks());
316    }
317
318
319    /**
320     * read
321     *
322     * @access protected
323     */
324    protected function read()
325    {
326        $this->timezone = $this->user->getTimeZone();
327        $this->export_tz_type = (
328            ($this->user->getPref('export_tz_type') !== false) ?
329                $this->user->getPref('export_tz_type') :
330                $this->export_tz_type
331        );
332        $this->date_format = $this->user->getDateFormat();
333        $this->time_format = $this->user->getTimeFormat();
334        if (($weekstart = $this->user->getPref('weekstart')) === false) {
335            $weekstart = $this->settings->getDefaultWeekStart();
336        }
337        $this->calendar_selection_type = $this->user->getPref('calendar_selection_type') ?
338            $this->user->getPref('calendar_selection_type') :
339            self::CAL_SELECTION_MEMBERSHIP;
340
341        $this->weekstart = $weekstart;
342
343        $this->setDayStart(
344            $this->user->getPref('day_start') !== false ?
345            $this->user->getPref('day_start') :
346            $this->settings->getDefaultDayStart()
347        );
348        $this->setDayEnd(
349            $this->user->getPref('day_end') !== false ?
350            $this->user->getPref('day_end') :
351            $this->settings->getDefaultDayEnd()
352        );
353        $this->setShowWeeks(
354            $this->user->getPref('show_weeks') !== false ?
355            $this->user->getPref('show_weeks') :
356            $this->settings->getShowWeeks()
357        );
358    }
359}
360