ical_export.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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'])) {
  65. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  66. }
  67. list($y,$m,$d,$h,$M,$s) = preg_split('/[\s:-]/',$event['start_date']);
  68. $vevent->setProperty(
  69. 'dtstart',
  70. array('year' => $y, 'month' => $m, 'day' => $d, 'hour' => $h, 'min' => $M, 'sec' => $s)
  71. );
  72. if(empty($event['end_date'])) {
  73. $y2 = $y;
  74. $m2 = $m;
  75. $d2 = $d;
  76. $h2 = $h;
  77. $M2 = $M + 15;
  78. $s2 = $s;
  79. if ($M2 > 60) {
  80. $M2 = $M2 - 60;
  81. $h2 += 1;
  82. }
  83. } else {
  84. list($y2,$m2,$d2,$h2,$M2,$s2) = preg_split('/[\s:-]/',$event['end_date']);
  85. }
  86. $vevent->setProperty(
  87. 'dtend',
  88. array('year' => $y2, 'month' => $m2, 'day' => $d2, 'hour' => $h2, 'min' => $M2, 'sec' => $s2)
  89. );
  90. //$vevent->setProperty( 'LOCATION', get_lang('Unknown') ); // property name - case independent
  91. $vevent->setProperty( 'description', api_convert_encoding($event['description'],'UTF-8',$charset));
  92. //$vevent->setProperty( 'comment', 'This is a comment' );
  93. //$user = api_get_user_info($event['user']);
  94. //$vevent->setProperty('organizer',$user['mail']);
  95. //$vevent->setProperty('attendee',$user['mail']);
  96. //$vevent->setProperty( 'rrule', array( 'FREQ' => 'WEEKLY', 'count' => 4));// occurs also four next weeks
  97. $ical->setConfig('filename',$y.$m.$d.$h.$M.$s.'-'.rand(1,1000).'.ics');
  98. $ical->setComponent ($vevent); // add event to calendar
  99. $ical->returnCalendar();
  100. break;
  101. case 'course':
  102. $vevent->setProperty( 'summary', api_convert_encoding($event['title'],'UTF-8',$charset));
  103. if (empty($event['start_date'])) {
  104. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  105. }
  106. list($y,$m,$d,$h,$M,$s) = preg_split('/[\s:-]/',$event['start_date']);
  107. $vevent->setProperty(
  108. 'dtstart',
  109. array('year' => $y, 'month' => $m, 'day' => $d, 'hour' => $h, 'min' => $M, 'sec' => $s)
  110. );
  111. if (empty($event['end_date'])) {
  112. $y2 = $y;
  113. $m2 = $m;
  114. $d2 = $d;
  115. $h2 = $h;
  116. $M2 = $M + 15;
  117. $s2 = $s;
  118. if ($M2 > 60) {
  119. $M2 = $M2 - 60;
  120. $h2 += 1;
  121. }
  122. } else {
  123. list($y2,$m2,$d2,$h2,$M2,$s2) = preg_split('/[\s:-]/',$event['end_date']);
  124. }
  125. $vevent->setProperty(
  126. 'dtend',
  127. array('year' => $y2, 'month' => $m2, 'day' => $d2, 'hour' => $h2, 'min' => $M2, 'sec' => $s2)
  128. );
  129. $vevent->setProperty( 'description', api_convert_encoding($event['description'],'UTF-8',$charset));
  130. //$vevent->setProperty( 'comment', 'This is a comment' );
  131. //$user = api_get_user_info($event['user']);
  132. //$vevent->setProperty('organizer',$user['mail']);
  133. //$vevent->setProperty('attendee',$user['mail']);
  134. //$course = api_get_course_info();
  135. $vevent->setProperty('location', $course_info['name']); // property name - case independent
  136. /*if($ai['repeat']) {
  137. $trans = array('daily'=>'DAILY','weekly'=>'WEEKLY','monthlyByDate'=>'MONTHLY','yearly'=>'YEARLY');
  138. $freq = $trans[$ai['repeat_type']];
  139. list($e_y,$e_m,$e_d) = split('/',date('Y/m/d',$ai['repeat_end']));
  140. $vevent->setProperty('rrule',array('FREQ'=>$freq,'UNTIL'=>array('year'=>$e_y,'month'=>$e_m,'day'=>$e_d),'INTERVAL'=>'1'));
  141. }*/
  142. //$vevent->setProperty( 'rrule', array( 'FREQ' => 'WEEKLY', 'count' => 4));// occurs also four next weeks
  143. $ical->setConfig('filename',$y.$m.$d.$h.$M.$s.'-'.rand(1,1000).'.ics');
  144. $ical->setComponent ($vevent); // add event to calendar
  145. $ical->returnCalendar();
  146. break;
  147. default:
  148. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  149. die();
  150. }
  151. } else {
  152. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  153. exit;
  154. }