ical_export.php 6.2 KB

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