new_ticket.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.ticket
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. if (!api_is_platform_admin() && api_get_setting('ticket_allow_student_add') !== 'true') {
  8. header('Location:'.api_get_path(WEB_CODE_PATH).'ticket/tickets.php');
  9. exit;
  10. }
  11. api_block_anonymous_users();
  12. $courseId = api_get_course_int_id();
  13. $htmlHeadXtra[] = '<script>
  14. function updateCourseList(sessionId) {
  15. $selectCourse = $("select#course_id");
  16. $selectCourse.empty();
  17. $.get("'.api_get_path(WEB_AJAX_PATH).'session.ajax.php", {
  18. a: "get_courses_inside_session",
  19. session_id : sessionId
  20. }, function (courseList) {
  21. $("<option>", {
  22. value: 0,
  23. text: "'.get_lang('Select').'"
  24. }).appendTo($selectCourse);
  25. if (courseList.length > 0) {
  26. $.each(courseList, function (index, course) {
  27. $("<option>", {
  28. value: course.id,
  29. text: course.name
  30. }).appendTo($selectCourse);
  31. });
  32. $("select#course_id option[value=\''.$courseId.'\']").attr("selected",true);
  33. $("select#course_id").selectpicker("refresh");
  34. }
  35. }, "json");
  36. }
  37. $(function() {
  38. $("select#session_id").on("change", function () {
  39. var sessionId = parseInt(this.value, 10);
  40. updateCourseList(sessionId);
  41. });
  42. var sessionId = $("select#session_id").val();
  43. updateCourseList(sessionId);
  44. });
  45. var counter_image = 1;
  46. function remove_image_form(element_id) {
  47. $("#" + element_id).remove();
  48. counter_image = counter_image - 1;
  49. $("#link-more-attach").css("display", "block");
  50. }
  51. function add_image_form() {
  52. // Multiple filepaths for image form
  53. var filepaths = $("#filepaths");
  54. var new_elem, input_file, link_remove, img_remove, new_filepath_id;
  55. if ($("#filepath_"+counter_image)) {
  56. counter_image = counter_image + 1;
  57. } else {
  58. counter_image = counter_image;
  59. }
  60. new_elem = "filepath_"+counter_image;
  61. $("<div/>", {
  62. id: new_elem,
  63. class: "controls"
  64. }).appendTo(filepaths);
  65. input_file = $("<input/>", {
  66. type: "file",
  67. name: "attach_" + counter_image,
  68. size: 20
  69. });
  70. link_remove = $("<a/>", {
  71. onclick: "remove_image_form(\'" + new_elem + "\')",
  72. style: "cursor: pointer"
  73. });
  74. img_remove = $("<img/>", {
  75. src: "'.Display::returnIconPath('delete.png').'"
  76. });
  77. new_filepath_id = $("#filepath_" + counter_image);
  78. new_filepath_id.append(input_file, link_remove.append(img_remove));
  79. if (counter_image === 6) {
  80. var link_attach = $("#link-more-attach");
  81. if (link_attach) {
  82. $(link_attach).css("display", "none");
  83. }
  84. }
  85. }
  86. </script>';
  87. $projectId = isset($_GET['project_id']) ? (int) $_GET['project_id'] : 0;
  88. $types = TicketManager::get_all_tickets_categories($projectId, 'category.name ASC');
  89. $htmlHeadXtra[] = '<script>
  90. var projects = '.js_array($types, 'projects', 'project_id').'
  91. var course_required = '.js_array($types, 'course_required', 'course_required').'
  92. var other_area = '.js_array($types, 'other_area', 'other_area').'
  93. var email = '.js_array($types, 'email', 'email').
  94. '</script>';
  95. /**
  96. * @param $array
  97. * @param $name
  98. * @param $key
  99. *
  100. * @return string
  101. */
  102. function js_array($array, $name, $key)
  103. {
  104. $return = "new Array(); ";
  105. foreach ($array as $value) {
  106. $return .= $name."['".$value['category_id']."'] ='".$value[$key]."'; ";
  107. }
  108. return $return;
  109. }
  110. function save_ticket()
  111. {
  112. $content = $_POST['content'];
  113. if (!empty($_POST['phone'])) {
  114. $content .= '<p style="color:red">&nbsp;'.get_lang('Phone').': '.Security::remove_XSS($_POST['phone']).'</p>';
  115. }
  116. $course_id = isset($_POST['course_id']) ? (int) $_POST['course_id'] : '';
  117. $sessionId = isset($_POST['session_id']) ? (int) $_POST['session_id'] : '';
  118. $category_id = isset($_POST['category_id']) ? (int) $_POST['category_id'] : '';
  119. $project_id = (int) $_POST['project_id'];
  120. $subject = $_POST['subject'];
  121. $other_area = (int) $_POST['other_area'];
  122. $personal_email = $_POST['personal_email'];
  123. $source = (int) $_POST['source_id'];
  124. $user_id = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
  125. $priority = isset($_POST['priority_id']) ? (int) $_POST['priority_id'] : '';
  126. $status = isset($_POST['status_id']) ? (int) $_POST['status_id'] : '';
  127. $file_attachments = $_FILES;
  128. if (TicketManager::add(
  129. $category_id,
  130. $course_id,
  131. $sessionId,
  132. $project_id,
  133. $other_area,
  134. $subject,
  135. $content,
  136. $personal_email,
  137. $file_attachments,
  138. $source,
  139. $priority,
  140. $status,
  141. $user_id
  142. )) {
  143. header('Location:'.api_get_path(WEB_CODE_PATH).'ticket/tickets.php');
  144. exit;
  145. } else {
  146. Display::addFlash(Display::return_message(get_lang('ThereWasAnErrorRegisteringTheTicket')));
  147. }
  148. }
  149. $interbreadcrumb[] = [
  150. 'url' => api_get_path(WEB_CODE_PATH).'ticket/tickets.php',
  151. 'name' => get_lang('MyTickets'),
  152. ];
  153. $userId = api_get_user_id();
  154. // Category List
  155. $categoryList = [];
  156. foreach ($types as $type) {
  157. $categoryList[$type['category_id']] = $type['name'].': '.$type['description'];
  158. }
  159. // Status List
  160. $statusAttributes = [
  161. 'style' => 'display: none;',
  162. 'id' => 'status_id',
  163. 'for' => 'status_id',
  164. ];
  165. $statusList = TicketManager::getStatusList();
  166. // Source List
  167. $sourceList = [];
  168. $sourceAttributes = [
  169. 'style' => 'display: none;',
  170. 'id' => 'source_id',
  171. 'for' => 'source_id',
  172. ];
  173. $sourceList[TicketManager::SOURCE_PLATFORM] = get_lang('SrcPlatform');
  174. if (api_is_platform_admin()) {
  175. $sourceAttributes = [
  176. 'id' => 'source_id',
  177. 'for' => 'source_id',
  178. ];
  179. $sourceList[TicketManager::SOURCE_EMAIL] = get_lang('SrcEmail');
  180. $sourceList[TicketManager::SOURCE_PHONE] = get_lang('SrcPhone');
  181. $sourceList[TicketManager::SOURCE_PRESENTIAL] = get_lang('SrcPresential');
  182. }
  183. // Priority List
  184. $priorityList = TicketManager::getPriorityList();
  185. $projectId = isset($_GET['project_id']) ? (int) $_GET['project_id'] : 0;
  186. $form = new FormValidator(
  187. 'send_ticket',
  188. 'POST',
  189. api_get_self().'?project_id='.$projectId,
  190. '',
  191. [
  192. 'enctype' => 'multipart/form-data',
  193. ]
  194. );
  195. $form->addElement(
  196. 'hidden',
  197. 'user_id_request',
  198. '',
  199. [
  200. 'id' => 'user_id_request',
  201. ]
  202. );
  203. $form->addElement(
  204. 'hidden',
  205. 'project_id',
  206. $projectId
  207. );
  208. $form->addElement(
  209. 'hidden',
  210. 'other_area',
  211. '',
  212. [
  213. 'id' => 'other_area',
  214. ]
  215. );
  216. $form->addElement(
  217. 'hidden',
  218. 'email',
  219. '',
  220. [
  221. 'id' => 'email',
  222. ]
  223. );
  224. $form->addSelect(
  225. 'category_id',
  226. get_lang('Category'),
  227. $categoryList,
  228. [
  229. 'id' => 'category_id',
  230. 'for' => 'category_id',
  231. 'style' => 'width: 562px;',
  232. ]
  233. );
  234. $form->addElement(
  235. 'text',
  236. 'subject',
  237. get_lang('Subject'),
  238. [
  239. 'id' => 'subject',
  240. ]
  241. );
  242. $form->addHtmlEditor(
  243. 'content',
  244. get_lang('Message'),
  245. false,
  246. false,
  247. [
  248. 'ToolbarSet' => 'Profile',
  249. 'Height' => '250',
  250. ]
  251. );
  252. if (api_is_platform_admin()) {
  253. $form->addSelectAjax(
  254. 'user_id',
  255. get_lang('Assign'),
  256. null,
  257. ['url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_like']
  258. );
  259. }
  260. $form->addElement(
  261. 'text',
  262. 'personal_email',
  263. get_lang('PersonalEmail'),
  264. [
  265. 'id' => 'personal_email',
  266. ]
  267. );
  268. $form->addLabel(
  269. '',
  270. Display::div(
  271. '',
  272. [
  273. 'id' => 'user_request',
  274. ]
  275. )
  276. );
  277. $form->addElement(
  278. 'select',
  279. 'status_id',
  280. get_lang('Status'),
  281. $statusList,
  282. $statusAttributes
  283. );
  284. $form->addElement(
  285. 'select',
  286. 'priority_id',
  287. get_lang('Priority'),
  288. $priorityList,
  289. [
  290. 'id' => 'priority_id',
  291. 'for' => 'priority_id',
  292. ]
  293. );
  294. $form->addElement(
  295. 'select',
  296. 'source_id',
  297. get_lang('Source'),
  298. $sourceList,
  299. $sourceAttributes
  300. );
  301. $form->addElement(
  302. 'text',
  303. 'phone',
  304. get_lang('Phone').' ('.get_lang('Optional').')',
  305. [
  306. 'id' => 'phone',
  307. ]
  308. );
  309. $sessionList = SessionManager::get_sessions_by_user($userId);
  310. if (api_is_platform_admin() || !empty($sessionList)) {
  311. $sessionListToSelect = [get_lang('Select')];
  312. // Course List
  313. foreach ($sessionList as $sessionInfo) {
  314. $sessionListToSelect[$sessionInfo['session_id']] = $sessionInfo['session_name'];
  315. }
  316. $form->addSelect('session_id', get_lang('Session'), $sessionListToSelect, ['id' => 'session_id']);
  317. } else {
  318. $form->addHidden('session_id', 0);
  319. }
  320. $form->addSelect('course_id', get_lang('Course'), [], ['id' => 'course_id']);
  321. $courseInfo = api_get_course_info();
  322. $params = [];
  323. if (!empty($courseInfo)) {
  324. $params = [
  325. 'course_id' => $courseInfo['real_id'],
  326. ];
  327. $sessionInfo = api_get_session_info(api_get_session_id());
  328. if (!empty($sessionInfo)) {
  329. $params['session_id'] = $sessionInfo['id'];
  330. }
  331. }
  332. $form->setDefaults($params);
  333. $form->addElement('file', 'attach_1', get_lang('FilesAttachment'));
  334. $form->addLabel('', '<span id="filepaths"><div id="filepath_1"></div></span>');
  335. $form->addLabel(
  336. '',
  337. '<span id="link-more-attach">
  338. <span class="btn btn-success" onclick="return add_image_form()">'.get_lang('AddOneMoreFile').'</span>
  339. </span>
  340. ('.sprintf(get_lang('MaximunFileSizeX'), format_file_size(api_get_setting('message_max_upload_filesize'))).')
  341. '
  342. );
  343. $form->addElement('html', '<br/>');
  344. $form->addElement(
  345. 'button',
  346. 'compose',
  347. get_lang('SendMessage'),
  348. null,
  349. null,
  350. null,
  351. 'btn btn-primary',
  352. [
  353. 'id' => 'btnsubmit',
  354. ]
  355. );
  356. $form->addRule('content', get_lang('ThisFieldIsRequired'), 'required');
  357. $form->addRule('category_id', get_lang('ThisFieldIsRequired'), 'required');
  358. $form->addRule('subject', get_lang('ThisFieldIsRequired'), 'required');
  359. if ($form->validate()) {
  360. save_ticket();
  361. }
  362. Display::display_header(get_lang('ComposeMessage'));
  363. echo '<div class="actions">';
  364. echo Display::url(
  365. Display::return_icon('back.png', get_lang('Tickets'), [], ICON_SIZE_MEDIUM),
  366. api_get_path(WEB_CODE_PATH).'ticket/tickets.php'
  367. );
  368. echo '</div>';
  369. $form->display();
  370. Display::display_footer();