agenda.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.calendar
  5. */
  6. /**
  7. * INIT SECTION
  8. */
  9. use \ChamiloSession as Session;
  10. // use anonymous mode when accessing this course tool
  11. $use_anonymous = true;
  12. $current_course_tool = TOOL_CALENDAR_EVENT;
  13. $course_info = api_get_course_info();
  14. if (!empty($course_info)) {
  15. api_protect_course_script(true);
  16. }
  17. $action = isset($_GET['action']) ? $_GET['action'] : null;
  18. $origin = isset($_GET['origin']) ? $_GET['origin'] : null;
  19. $this_section = SECTION_COURSES;
  20. $url = null;
  21. if (empty($action)) {
  22. if (!empty($course_info)) {
  23. $url = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?type=course'.'&'.api_get_cidreq();
  24. } else {
  25. $url = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?';
  26. }
  27. header("Location: $url");
  28. exit;
  29. }
  30. /* Resource linker */
  31. $_SESSION['source_type'] = 'Agenda';
  32. require_once '../resourcelinker/resourcelinker.inc.php';
  33. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  34. $group_id = api_get_group_id();
  35. $eventId = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  36. $type = $event_type = isset($_GET['type']) ? $_GET['type'] : null;
  37. $htmlHeadXtra[] = "<script>
  38. function plus_repeated_event() {
  39. if (document.getElementById('options2').style.display == 'none') {
  40. document.getElementById('options2').style.display = 'block';
  41. } else {
  42. document.getElementById('options2').style.display = 'none';
  43. }
  44. }
  45. $(function() {
  46. var checked = $('input[name=repeat]').attr('checked');
  47. if (checked) {
  48. $('#options2').show();
  49. }
  50. });
  51. </script>
  52. ";
  53. // setting the name of the tool
  54. $nameTools = get_lang('Agenda');
  55. Event::event_access_tool(TOOL_CALENDAR_EVENT);
  56. // permission stuff - also used by loading from global in agenda.inc.php
  57. $is_allowed_to_edit = api_is_allowed_to_edit(false, true) OR (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous());
  58. $agenda = new Agenda();
  59. $agenda->type = $type;
  60. $actions = $agenda->displayActions('calendar');
  61. if ($type == 'fromjs') {
  62. $id_list = explode('_', $eventId);
  63. $eventId = $id_list[1];
  64. $event_type = $id_list[0];
  65. }
  66. if (!api_is_allowed_to_edit(null, true) && $event_type == 'course') {
  67. api_not_allowed(true);
  68. }
  69. if ($event_type == 'course') {
  70. $agendaUrl = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?'.api_get_cidreq().'&type=course';
  71. } else {
  72. $agendaUrl = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?&type='.$event_type;
  73. }
  74. $course_info = api_get_course_info();
  75. $agenda->type = $event_type;
  76. $message = null;
  77. $content = null;
  78. if (api_is_allowed_to_edit(false, true) OR
  79. (api_get_course_setting('allow_user_edit_agenda') &&
  80. !api_is_anonymous() &&
  81. api_is_allowed_to_session_edit(false, true)) OR
  82. GroupManager::user_has_access(api_get_user_id(), $group_id, GroupManager::GROUP_TOOL_CALENDAR) &&
  83. GroupManager::is_tutor_of_group(api_get_user_id(), $group_id)
  84. ) {
  85. switch ($action) {
  86. case 'add':
  87. $actionName = get_lang('Add');
  88. $form = $agenda->getForm(array('action' => 'add'));
  89. if ($form->validate()) {
  90. $values = $form->getSubmitValues();
  91. $sendEmail = isset($values['add_announcement']) ? true : false;
  92. $allDay = isset($values['all_day']) ? 'true' : 'false';
  93. $sendAttachment = isset($_FILES['user_upload']) ? true : false;
  94. $attachment = $sendAttachment ? $_FILES['user_upload'] : null;
  95. $attachmentComment = isset($values['file_comment']) ? $values['file_comment'] : null;
  96. $startDate = $values['date_range_start'];
  97. $endDate = $values['date_range_end'];
  98. $eventId = $agenda->add_event(
  99. $startDate,
  100. $endDate,
  101. $allDay,
  102. $values['title'],
  103. $values['content'],
  104. $values['users_to_send'],
  105. $sendEmail,
  106. null,
  107. $attachment,
  108. $attachmentComment
  109. );
  110. if (!empty($values['repeat']) && !empty($eventId)) {
  111. // End date is always set as 23:59:59
  112. $endDate = substr($values['repeat_end_day'], 0, 10).' 23:59:59';
  113. $agenda->addRepeatedItem(
  114. $eventId,
  115. $values['repeat_type'],
  116. $endDate,
  117. $values['users_to_send']
  118. );
  119. }
  120. $message = Display::return_message(get_lang('AddSuccess'), 'confirmation');
  121. if ($sendEmail) {
  122. $message .= Display::return_message(get_lang('AdditionalMailWasSentToSelectedUsers'), 'confirmation');
  123. }
  124. Session::write('message', $message);
  125. header("Location: $agendaUrl");
  126. exit;
  127. } else {
  128. $content = $form->return_form();
  129. }
  130. break;
  131. case 'edit':
  132. $actionName = get_lang('Edit');
  133. $event = $agenda->get_event($eventId);
  134. if (empty($event)) {
  135. api_not_allowed(true);
  136. }
  137. $event['action'] = 'edit';
  138. $event['id'] = $eventId;
  139. $form = $agenda->getForm($event);
  140. if ($form->validate()) {
  141. $values = $form->getSubmitValues();
  142. $allDay = isset($values['all_day']) ? 'true' : 'false';
  143. $startDate = $values['date_range_start'];
  144. $endDate = $values['date_range_end'];
  145. $sendAttachment = isset($_FILES['user_upload']) ? true : false;
  146. $attachment = $sendAttachment ? $_FILES['user_upload'] : null;
  147. $attachmentComment = isset($values['file_comment']) ? $values['file_comment'] : null;
  148. // This is a sub event. Delete the current and create another BT#7803
  149. if (!empty($event['parent_event_id'])) {
  150. $agenda->delete_event($eventId);
  151. $eventId = $agenda->add_event(
  152. $startDate,
  153. $endDate,
  154. $allDay,
  155. $values['title'],
  156. $values['content'],
  157. $values['users_to_send'],
  158. false,
  159. null,
  160. $attachment,
  161. $attachmentComment
  162. );
  163. $message = Display::return_message(get_lang('Updated'), 'confirmation');
  164. Session::write('message', $message);
  165. header("Location: $agendaUrl");
  166. exit;
  167. }
  168. // Editing normal event.
  169. $agenda->edit_event(
  170. $eventId,
  171. $startDate,
  172. $endDate,
  173. $allDay,
  174. $values['title'],
  175. $values['content'],
  176. $values['users_to_send'],
  177. $attachment,
  178. $attachmentComment
  179. );
  180. if (!empty($values['repeat']) && !empty($eventId)) {
  181. // End date is always set as 23:59:59
  182. $endDate = substr($values['repeat_end_day'], 0, 10).' 23:59:59';
  183. $agenda->addRepeatedItem(
  184. $eventId,
  185. $values['repeat_type'],
  186. $endDate,
  187. $values['users_to_send']
  188. );
  189. }
  190. $deleteAttachment = isset($values['delete_attachment']) ? true : false;
  191. if ($deleteAttachment && isset($event['attachment']) && !empty($event['attachment'])) {
  192. $agenda->deleteAttachmentFile(
  193. $event['attachment']['id'],
  194. $agenda->course
  195. );
  196. }
  197. $message = Display::return_message(get_lang('Updated'), 'confirmation');
  198. Session::write('message', $message);
  199. header("Location: $agendaUrl");
  200. exit;
  201. } else {
  202. $content = $form->return_form();
  203. }
  204. break;
  205. case 'importical':
  206. $form = $agenda->getImportCalendarForm();
  207. $content = $form->return_form();
  208. if ($form->validate()) {
  209. $ical_name = $_FILES['ical_import']['name'];
  210. $ical_type = $_FILES['ical_import']['type'];
  211. $ext = substr($ical_name, (strrpos($ical_name, ".") + 1));
  212. if ($ext === 'ics' || $ext === 'ical' || $ext === 'icalendar' || $ext === 'ifb') {
  213. $result = $agenda->importEventFile($course_info, $_FILES['ical_import']);
  214. $is_ical = true;
  215. } else {
  216. $is_ical = false;
  217. }
  218. if (!$is_ical) {
  219. $message = Display::return_message(get_lang('IsNotiCalFormatFile'), 'error');
  220. $form = $agenda->getImportCalendarForm();
  221. $content = $form->return_form();
  222. break;
  223. } else {
  224. $message = Display::return_message(get_lang('AddSuccess'), 'error');
  225. $content = $result;
  226. }
  227. Session::write('message', $message);
  228. }
  229. break;
  230. case "delete":
  231. if (!(api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $eventId) )) {
  232. // a coach can only delete an element belonging to his session
  233. $content = $agenda->delete_event($eventId);
  234. }
  235. break;
  236. }
  237. }
  238. if (!empty($group_id)) {
  239. $group_properties = GroupManager :: get_group_properties($group_id);
  240. $interbreadcrumb[] = array(
  241. "url" => api_get_path(WEB_CODE_PATH)."group/group.php",
  242. "name" => get_lang('Groups')
  243. );
  244. $interbreadcrumb[] = array(
  245. "url" => api_get_path(WEB_CODE_PATH)."group/group_space.php?gidReq=".$group_id,
  246. "name" => get_lang('GroupSpace').' '.$group_properties['name']
  247. );
  248. }
  249. if (!empty($actionName)) {
  250. $interbreadcrumb[] = array(
  251. "url" => $url,
  252. "name" => get_lang('Agenda')
  253. );
  254. }
  255. // Tool introduction
  256. $introduction = Display::return_introduction_section(TOOL_CALENDAR_EVENT);
  257. $message = Session::read('message');
  258. Session::erase('message');
  259. $tpl = new Template($actionName);
  260. $tpl->assign('content', $content);
  261. $tpl->assign('actions', $actions);
  262. // Loading main Chamilo 1 col template
  263. $tpl->display_one_col_template();