agenda.php 11 KB

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