ical_export.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 __DIR__.'/../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($type);
  29. if (isset($_GET['course_id'])) {
  30. $course_info = api_get_course_info_by_id($_GET['course_id']);
  31. if (!empty($course_info)) {
  32. $agenda->set_course($course_info);
  33. }
  34. }
  35. $event = $agenda->get_event($id);
  36. if (!empty($event)) {
  37. define('ICAL_LANG', api_get_language_isocode());
  38. $ical = new vcalendar();
  39. $ical->setConfig('unique_id', api_get_path(WEB_PATH));
  40. $ical->setProperty('method', 'PUBLISH');
  41. $ical->setConfig('url', api_get_path(WEB_PATH));
  42. $vevent = new vevent();
  43. switch ($_GET['class']) {
  44. case 'public':
  45. $vevent->setClass('PUBLIC');
  46. break;
  47. case 'private':
  48. $vevent->setClass('PRIVATE');
  49. break;
  50. case 'confidential':
  51. $vevent->setClass('CONFIDENTIAL');
  52. break;
  53. default:
  54. $vevent->setClass('PRIVATE');
  55. break;
  56. }
  57. $event['start_date'] = api_get_local_time($event['start_date']);
  58. $event['end_date'] = api_get_local_time($event['end_date']);
  59. switch ($type) {
  60. case 'personal':
  61. case 'platform':
  62. $vevent->setProperty('summary', api_convert_encoding($event['title'], 'UTF-8', $charset));
  63. if (empty($event['start_date'])) {
  64. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  65. }
  66. list($y, $m, $d, $h, $M, $s) = preg_split('/[\s:-]/', $event['start_date']);
  67. $vevent->setProperty(
  68. 'dtstart',
  69. array('year' => $y, 'month' => $m, 'day' => $d, 'hour' => $h, 'min' => $M, 'sec' => $s)
  70. );
  71. if (empty($event['end_date'])) {
  72. $y2 = $y;
  73. $m2 = $m;
  74. $d2 = $d;
  75. $h2 = $h;
  76. $M2 = $M + 15;
  77. $s2 = $s;
  78. if ($M2 > 60) {
  79. $M2 = $M2 - 60;
  80. $h2 += 1;
  81. }
  82. } else {
  83. list($y2, $m2, $d2, $h2, $M2, $s2) = preg_split('/[\s:-]/', $event['end_date']);
  84. }
  85. $vevent->setProperty(
  86. 'dtend',
  87. array('year' => $y2, 'month' => $m2, 'day' => $d2, 'hour' => $h2, 'min' => $M2, 'sec' => $s2)
  88. );
  89. //$vevent->setProperty( 'LOCATION', get_lang('Unknown') ); // property name - case independent
  90. $vevent->setProperty('description', api_convert_encoding($event['description'], 'UTF-8', $charset));
  91. //$vevent->setProperty( 'comment', 'This is a comment' );
  92. //$user = api_get_user_info($event['user']);
  93. //$vevent->setProperty('organizer',$user['mail']);
  94. //$vevent->setProperty('attendee',$user['mail']);
  95. //$vevent->setProperty( 'rrule', array( 'FREQ' => 'WEEKLY', 'count' => 4));// occurs also four next weeks
  96. $ical->setConfig('filename', $y.$m.$d.$h.$M.$s.'-'.rand(1, 1000).'.ics');
  97. $ical->setComponent($vevent); // add event to calendar
  98. $ical->returnCalendar();
  99. break;
  100. case 'course':
  101. $vevent->setProperty('summary', api_convert_encoding($event['title'], 'UTF-8', $charset));
  102. if (empty($event['start_date'])) {
  103. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  104. }
  105. list($y, $m, $d, $h, $M, $s) = preg_split('/[\s:-]/', $event['start_date']);
  106. $vevent->setProperty(
  107. 'dtstart',
  108. array('year' => $y, 'month' => $m, 'day' => $d, 'hour' => $h, 'min' => $M, 'sec' => $s)
  109. );
  110. if (empty($event['end_date'])) {
  111. $y2 = $y;
  112. $m2 = $m;
  113. $d2 = $d;
  114. $h2 = $h;
  115. $M2 = $M + 15;
  116. $s2 = $s;
  117. if ($M2 > 60) {
  118. $M2 = $M2 - 60;
  119. $h2 += 1;
  120. }
  121. } else {
  122. list($y2, $m2, $d2, $h2, $M2, $s2) = preg_split('/[\s:-]/', $event['end_date']);
  123. }
  124. $vevent->setProperty(
  125. 'dtend',
  126. array('year' => $y2, 'month' => $m2, 'day' => $d2, 'hour' => $h2, 'min' => $M2, 'sec' => $s2)
  127. );
  128. $vevent->setProperty('description', api_convert_encoding($event['description'], 'UTF-8', $charset));
  129. //$vevent->setProperty( 'comment', 'This is a comment' );
  130. //$user = api_get_user_info($event['user']);
  131. //$vevent->setProperty('organizer',$user['mail']);
  132. //$vevent->setProperty('attendee',$user['mail']);
  133. //$course = api_get_course_info();
  134. $vevent->setProperty('location', $course_info['name']); // property name - case independent
  135. /*if($ai['repeat']) {
  136. $trans = array('daily'=>'DAILY','weekly'=>'WEEKLY','monthlyByDate'=>'MONTHLY','yearly'=>'YEARLY');
  137. $freq = $trans[$ai['repeat_type']];
  138. list($e_y,$e_m,$e_d) = split('/',date('Y/m/d',$ai['repeat_end']));
  139. $vevent->setProperty('rrule',array('FREQ'=>$freq,'UNTIL'=>array('year'=>$e_y,'month'=>$e_m,'day'=>$e_d),'INTERVAL'=>'1'));
  140. }*/
  141. //$vevent->setProperty( 'rrule', array( 'FREQ' => 'WEEKLY', 'count' => 4));// occurs also four next weeks
  142. $ical->setConfig('filename', $y.$m.$d.$h.$M.$s.'-'.rand(1, 1000).'.ics');
  143. $ical->setComponent($vevent); // add event to calendar
  144. $ical->returnCalendar();
  145. break;
  146. default:
  147. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  148. die();
  149. }
  150. } else {
  151. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  152. exit;
  153. }