ical_export.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php // $id: $
  2. /**
  3. * This file exclusively export calendar items to iCal or similar formats
  4. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  5. * See copyright information in the Dokeos root directory, dokeos_license.txt
  6. */
  7. /**
  8. * Initialisation
  9. */
  10. // name of the language file that needs to be included
  11. $language_file = 'agenda';
  12. // we are not inside a course, so we reset the course id
  13. $cidReset = true;
  14. // setting the global file that gets the general configuration, the databases, the languages, ...
  15. require_once '../inc/global.inc.php';
  16. $this_section = SECTION_MYAGENDA;
  17. api_block_anonymous_users();
  18. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  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. // setting the database variables
  23. $TABLECOURS = Database :: get_main_table(TABLE_MAIN_COURSE);
  24. $TABLECOURSUSER = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  25. $TABLEAGENDA = Database :: get_course_table(TABLE_AGENDA);
  26. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  27. $tbl_personal_agenda = Database :: get_user_personal_table(TABLE_PERSONAL_AGENDA);
  28. // the variables for the days and the months
  29. // Defining the shorts for the days
  30. $DaysShort = api_get_week_days_short();
  31. // Defining the days of the week to allow translation of the days
  32. $DaysLong = api_get_week_days_long();
  33. // Defining the months of the year to allow translation of the months
  34. $MonthsLong = api_get_months_long();
  35. if(!empty($_GET['id']) && $_GET['id']==strval(intval($_GET['id'])))
  36. {
  37. define('ICAL_LANG',api_get_language_isocode());
  38. if(!empty($_GET['type']))
  39. {
  40. $ical = new vcalendar();
  41. $ical->setConfig('unique_id',api_get_path(WEB_PATH));
  42. $ical->setProperty( 'method', 'PUBLISH' );
  43. $ical->setConfig('url',api_get_path(WEB_PATH));
  44. $vevent = new vevent();
  45. switch($_GET['class'])
  46. {
  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. switch($_GET['type'])
  61. {
  62. case 'personal':
  63. require_once (api_get_path(SYS_CODE_PATH).'calendar/myagenda.inc.php');
  64. $ai = get_personal_agenda_item($_GET['id']);
  65. $vevent->setProperty( 'summary', api_convert_encoding($ai['title'],'UTF-8',$charset));
  66. if(empty($ai['date'])){header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));}
  67. list($y,$m,$d,$h,$M,$s) = preg_split('/[\s:-]/',$ai['date']);
  68. $vevent->setProperty('dtstart',array('year'=>$y,'month'=>$m,'day'=>$d,'hour'=>$h,'min'=>$M,'sec'=>$s));
  69. if(empty($ai['enddate']))
  70. {
  71. $y2=$y;$m2=$m;$d2=$d;$h2=$h;$M2=$M+15;$s2=$s;
  72. if($M2>60){$M2=$M2-60;$h2+=1;}
  73. }
  74. else
  75. {
  76. list($y2,$m2,$d2,$h2,$M2,$s2) = preg_split('/[\s:-]/',$ai['enddate']);
  77. }
  78. $vevent->setProperty('dtend',array('year'=>$y2,'month'=>$m2,'day'=>$d2,'hour'=>$h2,'min'=>$M2,'sec'=>$s2));
  79. //$vevent->setProperty( 'LOCATION', get_lang('Unknown') ); // property name - case independent
  80. $vevent->setProperty( 'description', api_convert_encoding($ai['text'],'UTF-8',$charset));
  81. //$vevent->setProperty( 'comment', 'This is a comment' );
  82. $user = api_get_user_info($ai['user']);
  83. $vevent->setProperty('organizer',$user['mail']);
  84. $vevent->setProperty('attendee',$user['mail']);
  85. //$vevent->setProperty( 'rrule', array( 'FREQ' => 'WEEKLY', 'count' => 4));// occurs also four next weeks
  86. $ical->setConfig('filename',$y.$m.$d.$h.$M.$s.'-'.rand(1,1000).'.ics');
  87. $ical->setComponent ($vevent); // add event to calendar
  88. $ical->returnCalendar();
  89. break;
  90. case 'course':
  91. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  92. $TABLE_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  93. require_once (api_get_path(SYS_CODE_PATH).'calendar/agenda.inc.php');
  94. $ai = get_agenda_item($_GET['id']);
  95. $vevent->setProperty( 'summary', api_convert_encoding($ai['title'],'UTF-8',$charset));
  96. if(empty($ai['start_date'])){header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));}
  97. list($y,$m,$d,$h,$M,$s) = preg_split('/[\s:-]/',$ai['start_date']);
  98. $vevent->setProperty('dtstart',array('year'=>$y,'month'=>$m,'day'=>$d,'hour'=>$h,'min'=>$M,'sec'=>$s));
  99. if(empty($ai['end_date']))
  100. {
  101. $y2=$y;$m2=$m;$d2=$d;$h2=$h;$M2=$M+15;$s2=$s;
  102. if($M2>60){$M2=$M2-60;$h2+=1;}
  103. }
  104. else
  105. {
  106. list($y2,$m2,$d2,$h2,$M2,$s2) = preg_split('/[\s:-]/',$ai['end_date']);
  107. }
  108. $vevent->setProperty('dtend',array('year'=>$y2,'month'=>$m2,'day'=>$d2,'hour'=>$h2,'min'=>$M2,'sec'=>$s2));
  109. $vevent->setProperty( 'description', api_convert_encoding($ai['content'],'UTF-8',$charset));
  110. //$vevent->setProperty( 'comment', 'This is a comment' );
  111. $user = api_get_user_info($ai['user']);
  112. $vevent->setProperty('organizer',$user['mail']);
  113. //$vevent->setProperty('attendee',$user['mail']);
  114. $course = api_get_course_info();
  115. $vevent->setProperty('location', $course['name']); // property name - case independent
  116. if($ai['repeat'])
  117. {
  118. $trans = array('daily'=>'DAILY','weekly'=>'WEEKLY','monthlyByDate'=>'MONTHLY','yearly'=>'YEARLY');
  119. $freq = $trans[$ai['repeat_type']];
  120. list($e_y,$e_m,$e_d) = split('/',date('Y/m/d',$ai['repeat_end']));
  121. $vevent->setProperty('rrule',array('FREQ'=>$freq,'UNTIL'=>array('year'=>$e_y,'month'=>$e_m,'day'=>$e_d),'INTERVAL'=>'1'));
  122. }
  123. //$vevent->setProperty( 'rrule', array( 'FREQ' => 'WEEKLY', 'count' => 4));// occurs also four next weeks
  124. $ical->setConfig('filename',$y.$m.$d.$h.$M.$s.'-'.rand(1,1000).'.ics');
  125. $ical->setComponent ($vevent); // add event to calendar
  126. $ical->returnCalendar();
  127. break;
  128. default:
  129. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  130. die();
  131. }
  132. }
  133. }
  134. else
  135. {
  136. header('location:'.Security::remove_XSS($_SERVER['HTTP_REFERER']));
  137. die();
  138. }
  139. ?>