ical_export.php 6.3 KB

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