ical_export.php 5.9 KB

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