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