agenda.php 11 KB

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