agenda.php 12 KB

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