ical_export.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file exclusively export calendar items to iCal or similar formats
  5. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  6. */
  7. // name of the language file that needs to be included
  8. $language_file = 'agenda';
  9. // we are not inside a course, so we reset the course id
  10. $cidReset = true;
  11. // setting the global file that gets the general configuration, the databases, the languages, ...
  12. require_once '../inc/global.inc.php';
  13. $this_section = SECTION_MYAGENDA;
  14. api_block_anonymous_users();
  15. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  16. require_once api_get_path(LIBRARY_PATH).'icalcreator/iCalcreator.class.php';
  17. require_once api_get_path(SYS_CODE_PATH).'calendar/agenda.lib.php';
  18. // setting the name of the tool
  19. $nameTools = get_lang('MyAgenda');
  20. // the variables for the days and the months
  21. // Defining the shorts for the days
  22. $DaysShort = api_get_week_days_short();
  23. // Defining the days of the week to allow translation of the days
  24. $DaysLong = api_get_week_days_long();
  25. // Defining the months of the year to allow translation of the months
  26. $MonthsLong = api_get_months_long();
  27. if (empty($_GET['id'])) {
  28. api_not_allowed();
  29. }
  30. $id = explode('_', $_GET['id']);
  31. $type = $id[0];
  32. $id = $id[1];
  33. $agenda = new Agenda();
  34. $agenda->type = $type; //course,admin or personal
  35. if (isset($_GET['course_id'])) {
  36. $course_info = api_get_course_info_by_id($_GET['course_id']);
  37. if (!empty($course_info)) {
  38. $agenda->set_course($course_info);
  39. }
  40. }
  41. $event = $agenda->get_event($id);
  42. if (!empty($event)) {
  43. define('ICAL_LANG',api_get_language_isocode());
  44. $ical = new vcalendar();
  45. $ical->setConfig('unique_id',api_get_path(WEB_PATH));
  46. $ical->setProperty( 'method', 'PUBLISH' );
  47. $ical->setConfig('url',api_get_path(WEB_PATH));
  48. $vevent = new vevent();
  49. switch($_GET['class']) {
  50. case 'public':
  51. $vevent->setClass('PUBLIC');
  52. break;
  53. case 'private':
  54. $vevent->setClass('PRIVATE');
  55. break;
  56. case 'confidential':
  57. $vevent->setClass('CONFIDENTIAL');
  58. break;
  59. default:
  60. $vevent->setClass('PRIVATE');
  61. break;
  62. }
  63. $event['start_date'] = api_get_local_time($event['start_date']);
  64. $event['end_date'] = api_get_local_time($event['end_date']);
  65. switch($type) {
  66. case 'personal':
  67. case 'platform':
  68. $vevent->setProperty( 'summary', api_convert_encoding($event['title'],'UTF-8', $charset));
  69. if(empty($event['start_date'])){header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));}
  70. list($y,$m,$d,$h,$M,$s) = preg_split('/[\s:-]/',$event['start_date']);
  71. $vevent->setProperty('dtstart',array('year'=>$y,'month'=>$m,'day'=>$d,'hour'=>$h,'min'=>$M,'sec'=>$s));
  72. if(empty($event['end_date'])) {
  73. $y2=$y;$m2=$m;$d2=$d;$h2=$h;$M2=$M+15;$s2=$s;
  74. if($M2>60){$M2=$M2-60;$h2+=1;}
  75. } else {
  76. list($y2,$m2,$d2,$h2,$M2,$s2) = preg_split('/[\s:-]/',$event['end_date']);
  77. }
  78. $vevent->setProperty('dtend',array('year'=>$y2,'month'=>$m2,'day'=>$d2,'hour'=>$h2,'min'=>$M2,'sec'=>$s2));
  79. //$vevent->setProperty( 'LOCATION', get_lang('Unknown') ); // property name - case independent
  80. $vevent->setProperty( 'description', api_convert_encoding($event['description'],'UTF-8',$charset));
  81. //$vevent->setProperty( 'comment', 'This is a comment' );
  82. //$user = api_get_user_info($event['user']);
  83. //$vevent->setProperty('organizer',$user['mail']);
  84. //$vevent->setProperty('attendee',$user['mail']);
  85. //$vevent->setProperty( 'rrule', array( 'FREQ' => 'WEEKLY', 'count' => 4));// occurs also four next weeks
  86. $ical->setConfig('filename',$y.$m.$d.$h.$M.$s.'-'.rand(1,1000).'.ics');
  87. $ical->setComponent ($vevent); // add event to calendar
  88. $ical->returnCalendar();
  89. break;
  90. case 'course':
  91. $vevent->setProperty( 'summary', api_convert_encoding($event['title'],'UTF-8',$charset));
  92. if(empty($event['start_date'])){header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));}
  93. list($y,$m,$d,$h,$M,$s) = preg_split('/[\s:-]/',$event['start_date']);
  94. $vevent->setProperty('dtstart',array('year'=>$y,'month'=>$m,'day'=>$d,'hour'=>$h,'min'=>$M,'sec'=>$s));
  95. if (empty($event['end_date'])) {
  96. $y2=$y;$m2=$m;$d2=$d;$h2=$h;$M2=$M+15;$s2=$s;
  97. if($M2>60){$M2=$M2-60;$h2+=1;}
  98. } else {
  99. list($y2,$m2,$d2,$h2,$M2,$s2) = preg_split('/[\s:-]/',$event['end_date']);
  100. }
  101. $vevent->setProperty('dtend',array('year'=>$y2,'month'=>$m2,'day'=>$d2,'hour'=>$h2,'min'=>$M2,'sec'=>$s2));
  102. $vevent->setProperty( 'description', api_convert_encoding($event['description'],'UTF-8',$charset));
  103. //$vevent->setProperty( 'comment', 'This is a comment' );
  104. //$user = api_get_user_info($event['user']);
  105. //$vevent->setProperty('organizer',$user['mail']);
  106. //$vevent->setProperty('attendee',$user['mail']);
  107. //$course = api_get_course_info();
  108. $vevent->setProperty('location', $course_info['name']); // property name - case independent
  109. /*if($ai['repeat']) {
  110. $trans = array('daily'=>'DAILY','weekly'=>'WEEKLY','monthlyByDate'=>'MONTHLY','yearly'=>'YEARLY');
  111. $freq = $trans[$ai['repeat_type']];
  112. list($e_y,$e_m,$e_d) = split('/',date('Y/m/d',$ai['repeat_end']));
  113. $vevent->setProperty('rrule',array('FREQ'=>$freq,'UNTIL'=>array('year'=>$e_y,'month'=>$e_m,'day'=>$e_d),'INTERVAL'=>'1'));
  114. }*/
  115. //$vevent->setProperty( 'rrule', array( 'FREQ' => 'WEEKLY', 'count' => 4));// occurs also four next weeks
  116. $ical->setConfig('filename',$y.$m.$d.$h.$M.$s.'-'.rand(1,1000).'.ics');
  117. $ical->setComponent ($vevent); // add event to calendar
  118. $ical->returnCalendar();
  119. break;
  120. default:
  121. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  122. die();
  123. }
  124. } else {
  125. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  126. die();
  127. }