agenda.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. }
  77. $agenda = new Agenda($event_type);
  78. $allowToEdit = $agenda->getIsAllowedToEdit();
  79. $actions = $agenda->displayActions('calendar');
  80. if (!$allowToEdit && $event_type === 'course') {
  81. api_not_allowed(true);
  82. }
  83. if ($event_type === 'course') {
  84. $agendaUrl = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?'.api_get_cidreq().'&type=course';
  85. } else {
  86. $agendaUrl = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?&type='.$event_type;
  87. }
  88. $course_info = api_get_course_info();
  89. $content = null;
  90. if ($allowToEdit) {
  91. switch ($action) {
  92. case 'add':
  93. $actionName = get_lang('Add');
  94. $form = $agenda->getForm(array('action' => 'add'));
  95. if ($form->validate()) {
  96. $values = $form->getSubmitValues();
  97. $sendEmail = isset($values['add_announcement']) ? true : false;
  98. $allDay = isset($values['all_day']) ? 'true' : 'false';
  99. $sendAttachment = isset($_FILES) && !empty($_FILES) ? true : false;
  100. $attachmentList = $sendAttachment ? $_FILES : null;
  101. $attachmentCommentList = isset($values['legend']) ? $values['legend'] : null;
  102. $comment = isset($values['comment']) ? $values['comment'] : null;
  103. $startDate = $values['date_range_start'];
  104. $endDate = $values['date_range_end'];
  105. $eventId = $agenda->addEvent(
  106. $startDate,
  107. $endDate,
  108. $allDay,
  109. $values['title'],
  110. $values['content'],
  111. $values['users_to_send'],
  112. $sendEmail,
  113. null,
  114. $attachmentList,
  115. $attachmentCommentList,
  116. $comment
  117. );
  118. if (!empty($values['repeat']) && !empty($eventId)) {
  119. // End date is always set as 23:59:59
  120. $endDate = substr($values['repeat_end_day'], 0, 10).' 23:59:59';
  121. $agenda->addRepeatedItem(
  122. $eventId,
  123. $values['repeat_type'],
  124. $endDate,
  125. $values['users_to_send']
  126. );
  127. }
  128. $message = Display::return_message(get_lang('AddSuccess'), 'confirmation');
  129. if ($sendEmail) {
  130. $message .= Display::return_message(
  131. get_lang('AdditionalMailWasSentToSelectedUsers'),
  132. 'confirmation'
  133. );
  134. }
  135. Display::addFlash($message);
  136. header("Location: $agendaUrl");
  137. exit;
  138. } else {
  139. $content = $form->return_form();
  140. }
  141. break;
  142. case 'edit':
  143. $actionName = get_lang('Edit');
  144. $event = $agenda->get_event($eventId);
  145. if (empty($event)) {
  146. api_not_allowed(true);
  147. }
  148. $event['action'] = 'edit';
  149. $event['id'] = $eventId;
  150. $form = $agenda->getForm($event);
  151. if ($form->validate()) {
  152. $values = $form->getSubmitValues();
  153. $allDay = isset($values['all_day']) ? 'true' : 'false';
  154. $sendEmail = isset($values['add_announcement']) ? true : false;
  155. $startDate = $values['date_range_start'];
  156. $endDate = $values['date_range_end'];
  157. $sendAttachment = isset($_FILES) && !empty($_FILES) ? true : false;
  158. $attachmentList = $sendAttachment ? $_FILES : null;
  159. $attachmentCommentList = isset($values['legend']) ? $values['legend'] : null;
  160. $comment = isset($values['comment']) ? $values['comment'] : null;
  161. // This is a sub event. Delete the current and create another BT#7803
  162. if (!empty($event['parent_event_id'])) {
  163. $agenda->deleteEvent($eventId);
  164. $eventId = $agenda->addEvent(
  165. $startDate,
  166. $endDate,
  167. $allDay,
  168. $values['title'],
  169. $values['content'],
  170. $values['users_to_send'],
  171. false,
  172. null,
  173. $attachmentList,
  174. $attachmentCommentList,
  175. $comment
  176. );
  177. $message = Display::return_message(get_lang('Updated'), 'confirmation');
  178. Display::addFlash($message);
  179. header("Location: $agendaUrl");
  180. exit;
  181. }
  182. $usersToSend = isset($values['users_to_send']) ? $values['users_to_send'] : '';
  183. // Editing normal event.
  184. $agenda->editEvent(
  185. $eventId,
  186. $startDate,
  187. $endDate,
  188. $allDay,
  189. $values['title'],
  190. $values['content'],
  191. $usersToSend,
  192. $attachmentList,
  193. $attachmentCommentList,
  194. $comment,
  195. '',
  196. $sendEmail
  197. );
  198. if (!empty($values['repeat']) && !empty($eventId)) {
  199. // End date is always set as 23:59:59
  200. $endDate = substr($values['repeat_end_day'], 0, 10).' 23:59:59';
  201. $agenda->addRepeatedItem(
  202. $eventId,
  203. $values['repeat_type'],
  204. $endDate,
  205. $values['users_to_send']
  206. );
  207. }
  208. $deleteAttachmentList = isset($values['delete_attachment']) ? $values['delete_attachment'] : array();
  209. if (!empty($deleteAttachmentList)) {
  210. foreach ($deleteAttachmentList as $deleteAttachmentId => $value) {
  211. $agenda->deleteAttachmentFile(
  212. $deleteAttachmentId,
  213. $agenda->course
  214. );
  215. }
  216. }
  217. $message = Display::return_message(get_lang('Updated'), 'confirmation');
  218. Display::addFlash($message);
  219. header("Location: $agendaUrl");
  220. exit;
  221. } else {
  222. $content = $form->returnForm();
  223. }
  224. break;
  225. case 'importical':
  226. $actionName = get_lang('Import');
  227. $form = $agenda->getImportCalendarForm();
  228. if ($form->validate()) {
  229. $ical_name = $_FILES['ical_import']['name'];
  230. $ical_type = $_FILES['ical_import']['type'];
  231. $ext = substr($ical_name, (strrpos($ical_name, ".") + 1));
  232. if (in_array($ext, ['ics', 'ical', 'icalendar', 'ifb'])) {
  233. $content = $agenda->importEventFile($course_info, $_FILES['ical_import']);
  234. $message = Display::return_message(get_lang('AddSuccess'));
  235. } else {
  236. $message = Display::return_message(get_lang('IsNotiCalFormatFile'), 'error');
  237. }
  238. Display::addFlash($message);
  239. $url = api_get_self().'?action=importical&type='.$agenda->type;
  240. header("Location: $url");
  241. exit;
  242. }
  243. $content = $form->returnForm();
  244. break;
  245. case "delete":
  246. if (!(api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $eventId) )) {
  247. // a coach can only delete an element belonging to his session
  248. $content = $agenda->deleteEvent($eventId);
  249. }
  250. break;
  251. }
  252. }
  253. if (!empty($group_id)) {
  254. $group_properties = GroupManager :: get_group_properties($group_id);
  255. $interbreadcrumb[] = array(
  256. "url" => api_get_path(WEB_CODE_PATH)."group/group.php?".api_get_cidreq(),
  257. "name" => get_lang('Groups')
  258. );
  259. $interbreadcrumb[] = array(
  260. "url" => api_get_path(WEB_CODE_PATH)."group/group_space.php?".api_get_cidreq(),
  261. "name" => get_lang('GroupSpace').' '.$group_properties['name']
  262. );
  263. }
  264. if (!empty($actionName)) {
  265. $interbreadcrumb[] = array(
  266. "url" => $url,
  267. "name" => get_lang('Agenda')
  268. );
  269. }
  270. // Tool introduction
  271. $introduction = Display::return_introduction_section(TOOL_CALENDAR_EVENT);
  272. $tpl = new Template($actionName);
  273. $tpl->assign('content', $content);
  274. $tpl->assign('actions', $actions);
  275. // Loading main Chamilo 1 col template
  276. $tpl->display_one_col_template();