ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCalendarUtil.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2007 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 include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
25 
34 {
35  private static $today = null;
36  private static $default_calendar = array();
37  static $init_done;
38 
45  public static function _isToday($date)
46  {
47  global $ilUser;
48 
49 
50  if(!is_object(self::$today))
51  {
52  self::$today = new ilDateTime(time(),IL_CAL_UNIX,$ilUser->getTimeZone());
53  }
54  return ilDateTime::_equals(self::$today,$date,IL_CAL_DAY,$ilUser->getTimeZone());
55  }
56 
66  public static function _numericMonthToString($a_month,$a_long = true)
67  {
68  global $lng;
69 
70  $month = $a_month < 10 ? '0'.$a_month : $a_month;
71 
72  return $a_long ? $lng->txt('month_'.$month.'_long') : $lng->txt('month_'.$month.'_short');
73  }
74 
84  public static function _numericDayToString($a_day,$a_long = true)
85  {
86  global $lng;
87 
88  $lng->loadLanguageModule('dateplaner');
89  static $days = array('Su','Mo','Tu','We','Th','Fr','Sa','Su');
90 
91  return $a_long ? $lng->txt($days[$a_day].'_long') : $lng->txt($days[$a_day].'_short');
92  }
93 
103  public static function _buildWeekDayList($a_day,$a_weekstart)
104  {
105  include_once('Services/Calendar/classes/class.ilDateList.php');
106  $day_list = new ilDateList(ilDateList::TYPE_DATE);
107 
108  $start = clone $a_day;
109  $start_info = $start->get(IL_CAL_FKT_GETDATE,'','UTC');
110  $day_diff = $a_weekstart - $start_info['isoday'];
111  if($day_diff == 7)
112  {
113  $day_diff = 0;
114  }
115  $start->increment(IL_CAL_DAY,$day_diff);
116  $day_list->add($start);
117  for($i = 1; $i < 7;$i++)
118  {
119  $start->increment(IL_CAL_DAY,1);
120  $day_list->add($start);
121  }
122  return $day_list;
123  }
124 
135  public static function _buildMonthDayList($a_month,$a_year,$weekstart)
136  {
137  include_once('Services/Calendar/classes/class.ilDateList.php');
138  $day_list = new ilDateList(ilDateList::TYPE_DATE);
139 
140  $prev_month = ($a_month == 1) ? 12 : $a_month - 1;
141  $prev_year = ($prev_month == 12) ? $a_year - 1 : $a_year;
142  $next_month = $a_month == 12 ? 1 : $a_month + 1;
143  $next_year = $a_month == 12 ? $a_year + 1 : $a_year;
144 
145  $days_in_month = self::_getMaxDayOfMonth($a_year,$a_month);
146  $days_in_prev_month = self::_getMaxDayOfMonth($a_year,$prev_month);
147 
148  $week_day['year'] = $a_year;
149  $week_day['mon'] = $a_month;
150  $week_day['mday'] = 1;
151  $week_day['hours'] = 0;
152  $week_day['minutes'] = 0;
153  $week_day = new ilDate($week_day,IL_CAL_FKT_GETDATE);
154 
155  $weekday = $week_day->get(IL_CAL_FKT_DATE,'w');
156  $first_day_offset = (($weekday - $weekstart) < 0) ? 6 : $weekday - $weekstart;
157 
158 
159  for($i = 0;$i < 42;$i++)
160  {
161 
162  if($i < $first_day_offset)
163  {
164  $day = $days_in_prev_month - $first_day_offset + $i + 1;
165 
166  $day_list->add(new ilDate(gmmktime(0,0,0,$prev_month,
167  $days_in_prev_month - $first_day_offset + $i + 1,
168  $prev_year),
169  IL_CAL_UNIX));
170  }
171  elseif($i < $days_in_month + $first_day_offset)
172  {
173  $day = $i - $first_day_offset + 1;
174 
175 
176  $day_list->add(new ilDate(gmmktime(0,0,0,$a_month,
177  $i - $first_day_offset + 1,
178  $a_year),
179  IL_CAL_UNIX));
180  }
181  else
182  {
183  $day = $i - $days_in_month - $first_day_offset + 1;
184  $day_list->add(new ilDate(gmmktime(0,0,0,$next_month,
185  $i - $days_in_month - $first_day_offset + 1,
186  $next_year),
187  IL_CAL_UNIX));
188  }
189  if($i == 34 and ($day < 15 or $day == $days_in_month))
190  {
191  break;
192  }
193  }
194  return $day_list;
195  }
196 
200  static function initJSCalendar()
201  {
202  global $tpl, $lng;
203 
204  if (self::$init_done == "done")
205  {
206  return;
207  }
208 
209  $lng->loadLanguageModule("jscalendar");
210  $tpl->addBlockFile("CALENDAR_LANG_JAVASCRIPT", "calendar_javascript", "tpl.calendar.html");
211  $tpl->setCurrentBlock("calendar_javascript");
212  $tpl->setVariable("FULL_SUNDAY", $lng->txt("l_su"));
213  $tpl->setVariable("FULL_MONDAY", $lng->txt("l_mo"));
214  $tpl->setVariable("FULL_TUESDAY", $lng->txt("l_tu"));
215  $tpl->setVariable("FULL_WEDNESDAY", $lng->txt("l_we"));
216  $tpl->setVariable("FULL_THURSDAY", $lng->txt("l_th"));
217  $tpl->setVariable("FULL_FRIDAY", $lng->txt("l_fr"));
218  $tpl->setVariable("FULL_SATURDAY", $lng->txt("l_sa"));
219  $tpl->setVariable("SHORT_SUNDAY", $lng->txt("s_su"));
220  $tpl->setVariable("SHORT_MONDAY", $lng->txt("s_mo"));
221  $tpl->setVariable("SHORT_TUESDAY", $lng->txt("s_tu"));
222  $tpl->setVariable("SHORT_WEDNESDAY", $lng->txt("s_we"));
223  $tpl->setVariable("SHORT_THURSDAY", $lng->txt("s_th"));
224  $tpl->setVariable("SHORT_FRIDAY", $lng->txt("s_fr"));
225  $tpl->setVariable("SHORT_SATURDAY", $lng->txt("s_sa"));
226  $tpl->setVariable("FULL_JANUARY", $lng->txt("l_01"));
227  $tpl->setVariable("FULL_FEBRUARY", $lng->txt("l_02"));
228  $tpl->setVariable("FULL_MARCH", $lng->txt("l_03"));
229  $tpl->setVariable("FULL_APRIL", $lng->txt("l_04"));
230  $tpl->setVariable("FULL_MAY", $lng->txt("l_05"));
231  $tpl->setVariable("FULL_JUNE", $lng->txt("l_06"));
232  $tpl->setVariable("FULL_JULY", $lng->txt("l_07"));
233  $tpl->setVariable("FULL_AUGUST", $lng->txt("l_08"));
234  $tpl->setVariable("FULL_SEPTEMBER", $lng->txt("l_09"));
235  $tpl->setVariable("FULL_OCTOBER", $lng->txt("l_10"));
236  $tpl->setVariable("FULL_NOVEMBER", $lng->txt("l_11"));
237  $tpl->setVariable("FULL_DECEMBER", $lng->txt("l_12"));
238  $tpl->setVariable("SHORT_JANUARY", $lng->txt("s_01"));
239  $tpl->setVariable("SHORT_FEBRUARY", $lng->txt("s_02"));
240  $tpl->setVariable("SHORT_MARCH", $lng->txt("s_03"));
241  $tpl->setVariable("SHORT_APRIL", $lng->txt("s_04"));
242  $tpl->setVariable("SHORT_MAY", $lng->txt("s_05"));
243  $tpl->setVariable("SHORT_JUNE", $lng->txt("s_06"));
244  $tpl->setVariable("SHORT_JULY", $lng->txt("s_07"));
245  $tpl->setVariable("SHORT_AUGUST", $lng->txt("s_08"));
246  $tpl->setVariable("SHORT_SEPTEMBER", $lng->txt("s_09"));
247  $tpl->setVariable("SHORT_OCTOBER", $lng->txt("s_10"));
248  $tpl->setVariable("SHORT_NOVEMBER", $lng->txt("s_11"));
249  $tpl->setVariable("SHORT_DECEMBER", $lng->txt("s_12"));
250  $tpl->setVariable("ABOUT_CALENDAR", $lng->txt("about_calendar"));
251  $tpl->setVariable("ABOUT_CALENDAR_LONG", $lng->txt("about_calendar_long"));
252  $tpl->setVariable("ABOUT_TIME_LONG", $lng->txt("about_time"));
253  $tpl->setVariable("PREV_YEAR", $lng->txt("prev_year"));
254  $tpl->setVariable("PREV_MONTH", $lng->txt("prev_month"));
255  $tpl->setVariable("GO_TODAY", $lng->txt("go_today"));
256  $tpl->setVariable("NEXT_MONTH", $lng->txt("next_month"));
257  $tpl->setVariable("NEXT_YEAR", $lng->txt("next_year"));
258  $tpl->setVariable("SEL_DATE", $lng->txt("select_date"));
259  $tpl->setVariable("DRAG_TO_MOVE", $lng->txt("drag_to_move"));
260  $tpl->setVariable("PART_TODAY", $lng->txt("part_today"));
261  $tpl->setVariable("DAY_FIRST", $lng->txt("day_first"));
262  $tpl->setVariable("CLOSE", $lng->txt("close"));
263  $tpl->setVariable("TODAY", $lng->txt("today"));
264  $tpl->setVariable("TIME_PART", $lng->txt("time_part"));
265  $tpl->setVariable("DEF_DATE_FORMAT", $lng->txt("def_date_format"));
266  $tpl->setVariable("TT_DATE_FORMAT", $lng->txt("tt_date_format"));
267  $tpl->setVariable("WK", $lng->txt("wk"));
268  $tpl->setVariable("TIME", $lng->txt("time"));
269  $tpl->parseCurrentBlock();
270  $tpl->setCurrentBlock("CalendarJS");
271  $tpl->setVariable("LOCATION_JAVASCRIPT_CALENDAR", "./Services/Calendar/js/calendar.js");
272  $tpl->setVariable("LOCATION_JAVASCRIPT_CALENDAR_SETUP", "./Services/Calendar/js/calendar-setup.js");
273  $tpl->setVariable("LOCATION_JAVASCRIPT_CALENDAR_STYLESHEET", "./Services/Calendar/css/calendar.css");
274  $tpl->parseCurrentBlock();
275 
276  self::$init_done = "done";
277  }
278 
287  public static function _getShortTimeZoneList()
288  {
289  return array(
290  'Pacific/Samoa' => 'GMT-11: Midway Islands, Samoa',
291  'US/Hawaii' => 'GMT-10:00: Hawaii, Polynesia',
292  'US/Alaska' => 'GMT-9:00: Alaska',
293  'America/Los_Angeles' => 'GMT-8:00: Tijuana, Los Angeles, Seattle, Vancouver',
294  'US/Arizona' => 'GMT-7:00: Arizona',
295  'America/Chihuahua' => 'GMT-7:00: Chihuahua, La Paz, Mazatlan',
296  'America/Denver' => 'GMT-7:00: Arizona, Denver, Salt Lake City, Calgary',
297  'America/Chicago' => 'GMT-6:00: Chicago, Dallas, Kansas City, Winnipeg',
298  'America/Monterrey' => 'GMT-6:00: Guadalajara, Mexico City, Monterrey',
299  'Canada/Saskatchewan' => 'GMT-6:00: Saskatchewan',
300  'US/Central' => 'GMT-6:00: Central America',
301  'America/Bogota' => 'GMT-5:00: Bogota, Lima, Quito',
302  'US/East-Indiana' => 'GMT-5:00: East-Indiana',
303  'America/New_York' => 'GMT-5:00: New York, Miami, Atlanta, Detroit, Toronto',
304  'Canada/Atlantic' => 'GMT-4:00: Atlantic (Canada)',
305  'America/La_Paz' => 'GMT-4:00: Carcas, La Paz',
306  'America/Santiago' => 'GMT-4:00: Santiago',
307  'Canada/Newfoundland' => 'GMT-3:00: Newfoundland',
308  'Brazil/East' => 'GMT-3:00: Sao Paulo',
309  'America/Argentina/Buenos_Aires' => 'GMT-3:00: Buenes Aires, Georgtown',
310  'Etc/GMT+3' => 'GMT-3:00: Greenland, Uruguay, Surinam',
311  'Atlantic/Cape_Verde' => 'GMT-2:00: Cape Verde, Greenland, South Georgia',
312  'Atlantic/Azores' => 'GMT-1:00: Azores',
313  'Africa/Casablanca' => 'GMT+0:00: Casablanca, Monrovia',
314  'Europe/London' => 'GMT+0:00: Dublin, Edinburgh, Lisbon, London',
315  'Europe/Berlin' => 'GMT+1:00: Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna',
316  'Europe/Belgrade' => 'GMT+1:00: Belgrade, Bratislava, Budapest, Ljubljana, Prague',
317  'Europe/Paris' => 'GMT+1:00: Brussels, Copenhagen, Paris, Madrid',
318  'Europe/Sarajevo' => 'GMT+1:00: Sarajevo, Skopje, Warsaw, Zagreb',
319  'Africa/Lagos' => 'GMT+1:00: West-Central Africa',
320  'Europe/Athens' => 'GMT+2:00: Athens, Beirut, Istanbul, Minsk',
321  'Europe/Bucharest' => 'GMT+2:00: Bucharest',
322  'Africa/Harare' => 'GMT+2:00: Harare, Pratoria',
323  'Europe/Helsinki' => 'GMT+2:00: Helsinki, Kiev, Riga, Sofia, Tallinn, Vilnius',
324  'Asia/Jerusalem' => 'GMT+2:00: Jerusalem',
325  'Africa/Cairo' => 'GMT+2:00: Cairo',
326  'Asia/Baghdad' => 'GMT+3:00: Baghdad',
327  'Asia/Kuwait' => 'GMT+3:00: Kuwait, Riyadh',
328  'Europe/Moscow' => 'GMT+3:00: Moscow, Saint Petersburg',
329  'Africa/Nairobi' => 'GMT+3:00: Nairobi,Teheran',
330  'Asia/Muscat' => 'GMT+4:00: Abu Dhabi, Muscat',
331  'Asia/Baku' => 'GMT+4:00: Baku, Tbilisi, Erivan',
332  'Asia/Kabul' => 'GMT+4:00: Kabul',
333  'Asia/Karachi' => 'GMT+5:00: Islamabad, Karachi, Taschkent',
334  'Asia/Yekaterinburg' => 'GMT+5:00: Yekaterinburg',
335  'Asia/Calcutta' => 'GMT+5:30: New Dehli',
336  'Asia/Katmandu' => 'GMT+5:45: Katmandu',
337  'Asia/Novosibirsk' => 'GMT+6:00: Almaty, Novosibirsk',
338  'Asia/Dhaka' => 'GMT+6:00: Astana, Dhaka',
339  'Asia/Rangoon' => 'GMT+6:00: Sri Jayawardenepura, Rangoon',
340  'Asia/Jakarta' => 'GMT+7:00: Bangkok, Hanoi, Jakarta',
341  'Asia/Krasnoyarsk' => 'GMT+7:00: Krasnoyarsk',
342  'Asia/Irkutsk' => 'GMT+8:00: Irkutsk, Ulan Bator',
343  'Asia/Singapore' => 'GMT+8:00: Kuala Lumpour, Singapore',
344  'Asia/Hong_Kong' => 'GMT+8:00: Beijing, Chongqing, Hong kong, Urumchi',
345  'Australia/Perth' => 'GMT+8:00: Perth',
346  'Asia/Taipei' => 'GMT+8:00: Taipei',
347  'Asia/Yakutsk' => 'GMT+9:00: Yakutsk',
348  'Asia/Tokyo' => 'GMT+9:00: Osaka, Sapporo, Tokyo',
349  'Asia/Seoul' => 'GMT+9:00: Seoul, Darwin, Adelaide',
350  'Australia/Brisbane' => 'GMT+10:00: Brisbane',
351  'Australia/Sydney' => 'GMT+10:00: Canberra, Melbourne, Sydney',
352  'Pacific/Guam' => 'GMT+10:00: Guam, Port Moresby',
353  'Australia/Hobart' => 'GMT+10:00: Hobart',
354  'Asia/Vladivostok' => 'GMT+10:00: Vladivostok',
355  'Asia/Magadan' => 'GMT+11:00: Salomon Islands, New Caledonia, Magadan',
356  'Pacific/Auckland' => 'GMT+12:00: Auckland, Wellington',
357  'Pacific/Fiji' => 'GMT+12:00: Fiji, Kamchatka, Marshall-Islands');
358  }
359 
360 
368  public static function _isLeapYear($a_year)
369  {
370  $is_leap = false;
371 
372  if($a_year % 4 == 0)
373  {
374  $is_leap = true;
375  if($a_year % 100 == 0)
376  {
377  $is_leap = false;
378  if($a_year % 400)
379  {
380  return true;
381  }
382  }
383  }
384  return $is_leap;
385  }
386 
396  public static function _getMaxDayOfMonth($a_year,$a_month)
397  {
398  if(@function_exists('cal_days_in_month'))
399  {
400  return cal_days_in_month(CAL_GREGORIAN,$a_month,$a_year);
401  }
402  $months = array(0,31,
403  self::_isLeapYear($a_year) ? 29 : 28,
404  31,30,31,30,31,31,30,31,30,31);
405  return $months[(int) $a_month];
406  }
407 
416  public static function calculateFontColor($a_html_color_code)
417  {
418  if(strpos($a_html_color_code,'#') !== 0 or strlen($a_html_color_code) != 7)
419  {
420  return '#000000';
421  }
422  $hex = str_replace('#','0x',$a_html_color_code);
423 
424  return hexdec($hex) > 8000000 ? '#000000' : '#FFFFFF';
425  }
426 
432  public static function getHourSelection($a_format)
433  {
434  switch($a_format)
435  {
437  for($i = 0; $i < 24; $i++)
438  {
439  $options[$i] = sprintf('%02d:00',$i);
440  }
441  break;
442 
444  for($i = 0; $i < 24; $i++)
445  {
446  $options[$i] = date('h a',mktime($i,0,0,1,1,2000));
447  }
448  break;
449  }
450  return $options ? $options : array();
451 
452  }
453 
462  public static function initDefaultCalendarByType($a_type_id, $a_usr_id, $a_title, $a_create = false)
463  {
464  global $ilDB,$lng;
465 
466  if(isset(self::$default_calendar[$a_type_id]))
467  {
468  return self::$default_calendar[$a_type_id];
469  }
470 
471  include_once './Services/Calendar/classes/class.ilCalendarCategory.php';
472 
473  $query = "SELECT cat_id FROM cal_categories ".
474  "WHERE obj_id = ".$ilDB->quote($a_usr_id,'integer')." ".
475  "AND type = ".$ilDB->quote($a_type_id,'integer');
476  $res = $ilDB->query($query);
477  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
478  {
479  return self::$default_calendar[$a_type_id] = new ilCalendarCategory($row->cat_id);
480  }
481 
482  if(!$a_create)
483  {
484  return null;
485  }
486 
487  // Create default calendar
488  self::$default_calendar[$a_type_id] = new ilCalendarCategory();
489  self::$default_calendar[$a_type_id]->setType($a_type_id);
490  self::$default_calendar[$a_type_id]->setColor(ilCalendarCategory::DEFAULT_COLOR);
491  self::$default_calendar[$a_type_id]->setTitle($a_title);
492  self::$default_calendar[$a_type_id]->setObjId($a_usr_id);
493  self::$default_calendar[$a_type_id]->add();
494 
495  return self::$default_calendar[$a_type_id];
496  }
497 }
498 ?>