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