agenda.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.calendar
  5. */
  6. use \ChamiloSession as Session;
  7. // name of the language file that needs to be included
  8. $language_file = array('agenda', 'group', 'messages', 'userInfo', 'admin');
  9. // use anonymous mode when accessing this course tool
  10. $use_anonymous = true;
  11. require_once '../inc/global.inc.php';
  12. $current_course_tool = TOOL_CALENDAR_EVENT;
  13. $course_info = api_get_course_info();
  14. if (!empty($course_info)) {
  15. api_protect_course_script(true);
  16. }
  17. $action = isset($_GET['action']) ? $_GET['action'] : null;
  18. $origin = isset($_GET['origin']) ? $_GET['origin'] : null;
  19. $this_section = SECTION_COURSES;
  20. $url = null;
  21. if (empty($action)) {
  22. if (!empty($course_info)) {
  23. $url = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?type=course'.'&'.api_get_cidreq();
  24. } else {
  25. $url = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?';
  26. }
  27. header("Location: $url");
  28. exit;
  29. }
  30. /* Resource linker */
  31. $_SESSION['source_type'] = 'Agenda';
  32. require_once '../resourcelinker/resourcelinker.inc.php';
  33. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  34. $group_id = api_get_group_id();
  35. $eventId = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
  36. $type = $event_type = isset($_GET['type']) ? $_GET['type'] : null;
  37. $htmlHeadXtra[] = "<script>
  38. function plus_repeated_event() {
  39. if (document.getElementById('options2').style.display == 'none') {
  40. document.getElementById('options2').style.display = 'block';
  41. } else {
  42. document.getElementById('options2').style.display = 'none';
  43. }
  44. }
  45. $(function() {
  46. var checked = $('input[name=repeat]').attr('checked');
  47. if (checked) {
  48. $('#options2').show();
  49. }
  50. });
  51. </script>
  52. ";
  53. $htmlHeadXtra[] = '<script type="text/javascript">
  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_access_tool(TOOL_CALENDAR_EVENT);
  80. // permission stuff - also used by loading from global in agenda.inc.php
  81. $is_allowed_to_edit = api_is_allowed_to_edit(false, true) OR (api_get_course_setting('allow_user_edit_agenda') && !api_is_anonymous());
  82. $agenda = new Agenda();
  83. $agenda->type = $type;
  84. $actions = $agenda->displayActions('calendar');
  85. if ($type == 'fromjs') {
  86. $id_list = explode('_', $eventId);
  87. $eventId = $id_list[1];
  88. $event_type = $id_list[0];
  89. }
  90. if (!api_is_allowed_to_edit(null, true) && $event_type == 'course') {
  91. api_not_allowed(true);
  92. }
  93. if ($event_type == 'course') {
  94. $agendaUrl = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?'.api_get_cidreq().'&type=course';
  95. } else {
  96. $agendaUrl = api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?&type='.$event_type;
  97. }
  98. $course_info = api_get_course_info();
  99. $agenda->type = $event_type;
  100. $message = null;
  101. $content = null;
  102. if (api_is_allowed_to_edit(false, true) OR
  103. (api_get_course_setting('allow_user_edit_agenda') &&
  104. !api_is_anonymous() &&
  105. api_is_allowed_to_session_edit(false, true)) OR
  106. GroupManager::user_has_access(api_get_user_id(), $group_id, GroupManager::GROUP_TOOL_CALENDAR) &&
  107. GroupManager::is_tutor_of_group(api_get_user_id(), $group_id)
  108. ) {
  109. switch ($action) {
  110. case 'add':
  111. $actionName = get_lang('Add');
  112. $form = $agenda->getForm(array('action' => 'add'));
  113. if ($form->validate()) {
  114. $values = $form->getSubmitValues();
  115. $sendEmail = isset($values['add_announcement']) ? true : false;
  116. $allDay = isset($values['all_day']) ? 'true' : 'false';
  117. $sendAttachment = isset($_FILES) && !empty($_FILES) ? true : false;
  118. $attachmentList = $sendAttachment ? $_FILES : null;
  119. $attachmentCommentList = isset($values['legend']) ? $values['legend'] : null;
  120. $comment = isset($values['comment']) ? $values['comment'] : null;
  121. $startDate = $values['date_range_start'];
  122. $endDate = $values['date_range_end'];
  123. $eventId = $agenda->add_event(
  124. $startDate,
  125. $endDate,
  126. $allDay,
  127. $values['title'],
  128. $values['content'],
  129. $values['users_to_send'],
  130. $sendEmail,
  131. null,
  132. $attachmentList,
  133. $attachmentCommentList,
  134. $comment
  135. );
  136. if (!empty($values['repeat']) && !empty($eventId)) {
  137. // End date is always set as 23:59:59
  138. $endDate = substr($values['repeat_end_day'], 0, 10).' 23:59:59';
  139. $agenda->addRepeatedItem(
  140. $eventId,
  141. $values['repeat_type'],
  142. $endDate,
  143. $values['users_to_send']
  144. );
  145. }
  146. $message = Display::return_message(get_lang('AddSuccess'), 'confirmation');
  147. if ($sendEmail) {
  148. $message .= Display::return_message(get_lang('AdditionalMailWasSentToSelectedUsers'), 'confirmation');
  149. }
  150. Session::write('message', $message);
  151. header("Location: $agendaUrl");
  152. exit;
  153. } else {
  154. $content = $form->return_form();
  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'] : null;
  175. $comment = isset($values['comment']) ? $values['comment'] : null;
  176. // This is a sub event. Delete the current and create another BT#7803
  177. if (!empty($event['parent_event_id'])) {
  178. $agenda->delete_event($eventId);
  179. $eventId = $agenda->add_event(
  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('Updated'), 'confirmation');
  193. Session::write('message', $message);
  194. header("Location: $agendaUrl");
  195. exit;
  196. }
  197. // Editing normal event.
  198. $agenda->edit_event(
  199. $eventId,
  200. $startDate,
  201. $endDate,
  202. $allDay,
  203. $values['title'],
  204. $values['content'],
  205. $values['users_to_send'],
  206. $attachmentList,
  207. $attachmentCommentList,
  208. $comment,
  209. '',
  210. $sendEmail
  211. );
  212. if (!empty($values['repeat']) && !empty($eventId)) {
  213. // End date is always set as 23:59:59
  214. $endDate = substr($values['repeat_end_day'], 0, 10).' 23:59:59';
  215. $agenda->addRepeatedItem(
  216. $eventId,
  217. $values['repeat_type'],
  218. $endDate,
  219. $values['users_to_send']
  220. );
  221. }
  222. $deleteAttachmentList = isset($values['delete_attachment']) ? $values['delete_attachment'] : array();
  223. if (!empty($deleteAttachmentList)) {
  224. foreach ($deleteAttachmentList as $deleteAttachmentId => $value) {
  225. $agenda->deleteAttachmentFile(
  226. $deleteAttachmentId,
  227. $agenda->course
  228. );
  229. }
  230. }
  231. $message = Display::return_message(get_lang('Updated'), 'confirmation');
  232. Session::write('message', $message);
  233. header("Location: $agendaUrl");
  234. exit;
  235. } else {
  236. $content = $form->return_form();
  237. }
  238. break;
  239. case 'importical':
  240. $form = $agenda->getImportCalendarForm();
  241. $content = $form->return_form();
  242. if ($form->validate()) {
  243. $ical_name = $_FILES['ical_import']['name'];
  244. $ical_type = $_FILES['ical_import']['type'];
  245. $ext = substr($ical_name, (strrpos($ical_name, ".") + 1));
  246. if ($ext === 'ics' || $ext === 'ical' || $ext === 'icalendar' || $ext === 'ifb') {
  247. $result = $agenda->importEventFile($course_info, $_FILES['ical_import']);
  248. $is_ical = true;
  249. } else {
  250. $is_ical = false;
  251. }
  252. if (!$is_ical) {
  253. $message = Display::return_message(get_lang('IsNotiCalFormatFile'), 'error');
  254. $form = $agenda->getImportCalendarForm();
  255. $content = $form->return_form();
  256. break;
  257. } else {
  258. $message = Display::return_message(get_lang('AddSuccess'), 'error');
  259. $content = $result;
  260. }
  261. Session::write('message', $message);
  262. }
  263. break;
  264. case "delete":
  265. if (!(api_is_course_coach() && !api_is_element_in_the_session(TOOL_AGENDA, $eventId) )) {
  266. // a coach can only delete an element belonging to his session
  267. $content = $agenda->delete_event($eventId);
  268. }
  269. break;
  270. }
  271. }
  272. if (!empty($group_id)) {
  273. $group_properties = GroupManager :: get_group_properties($group_id);
  274. $interbreadcrumb[] = array(
  275. "url" => api_get_path(WEB_CODE_PATH)."group/group.php",
  276. "name" => get_lang('Groups')
  277. );
  278. $interbreadcrumb[] = array(
  279. "url" => api_get_path(WEB_CODE_PATH)."group/group_space.php?gidReq=".$group_id,
  280. "name" => get_lang('GroupSpace').' '.$group_properties['name']
  281. );
  282. }
  283. if (!empty($actionName)) {
  284. $interbreadcrumb[] = array(
  285. "url" => $url,
  286. "name" => get_lang('Agenda')
  287. );
  288. }
  289. // Tool introduction
  290. $introduction = Display::return_introduction_section(TOOL_CALENDAR_EVENT);
  291. $message = Session::read('message');
  292. Session::erase('message');
  293. $tpl = new Template($actionName);
  294. $tpl->assign('content', $content);
  295. $tpl->assign('actions', $actions);
  296. // Loading main Chamilo 1 col template
  297. $tpl->display_one_col_template();