agenda.php 11 KB

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