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. *
  6. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  7. */
  8. // we are not inside a course, so we reset the course id
  9. $cidReset = true;
  10. // setting the global file that gets the general configuration, the databases, the languages, ...
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. $this_section = SECTION_MYAGENDA;
  13. api_block_anonymous_users();
  14. // setting the name of the tool
  15. $nameTools = get_lang('MyAgenda');
  16. // the variables for the days and the months
  17. // Defining the shorts for the days
  18. $DaysShort = api_get_week_days_short();
  19. // Defining the days of the week to allow translation of the days
  20. $DaysLong = api_get_week_days_long();
  21. // Defining the months of the year to allow translation of the months
  22. $MonthsLong = api_get_months_long();
  23. if (empty($_GET['id'])) {
  24. api_not_allowed();
  25. }
  26. $id = explode('_', $_GET['id']);
  27. $type = $id[0];
  28. $id = $id[1];
  29. $agenda = new Agenda($type);
  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. ['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. ['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. ['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. ['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. }