ical_export.php 6.6 KB

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