agenda.php 12 KB

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