new_ticket.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 $s
  97. *
  98. * @return string
  99. */
  100. function js_str($s)
  101. {
  102. return '"'.addcslashes($s, "\0..\37\"\\").'"';
  103. }
  104. /**
  105. * @param $array
  106. * @param $name
  107. * @param $key
  108. *
  109. * @return string
  110. */
  111. function js_array($array, $name, $key)
  112. {
  113. $return = "new Array(); ";
  114. foreach ($array as $value) {
  115. $return .= $name."['".$value['category_id']."'] ='".$value[$key]."'; ";
  116. }
  117. return $return;
  118. }
  119. function save_ticket()
  120. {
  121. $content = $_POST['content'];
  122. if (!empty($_POST['phone'])) {
  123. $content .= '<p style="color:red">&nbsp;'.get_lang('Phone').': '.Security::remove_XSS($_POST['phone']).'</p>';
  124. }
  125. $course_id = isset($_POST['course_id']) ? (int) $_POST['course_id'] : '';
  126. $sessionId = isset($_POST['session_id']) ? (int) $_POST['session_id'] : '';
  127. $category_id = isset($_POST['category_id']) ? (int) $_POST['category_id'] : '';
  128. $project_id = (int) $_POST['project_id'];
  129. $subject = $_POST['subject'];
  130. $other_area = (int) $_POST['other_area'];
  131. $personal_email = $_POST['personal_email'];
  132. $source = (int) $_POST['source_id'];
  133. $user_id = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
  134. $priority = isset($_POST['priority_id']) ? (int) $_POST['priority_id'] : '';
  135. $status = isset($_POST['status_id']) ? (int) $_POST['status_id'] : '';
  136. $file_attachments = $_FILES;
  137. if (TicketManager::add(
  138. $category_id,
  139. $course_id,
  140. $sessionId,
  141. $project_id,
  142. $other_area,
  143. $subject,
  144. $content,
  145. $personal_email,
  146. $file_attachments,
  147. $source,
  148. $priority,
  149. $status,
  150. $user_id
  151. )) {
  152. header('Location:'.api_get_path(WEB_CODE_PATH).'ticket/tickets.php');
  153. exit;
  154. } else {
  155. Display::addFlash(Display::return_message(get_lang('ThereWasAnErrorRegisteringTheTicket')));
  156. }
  157. }
  158. $interbreadcrumb[] = [
  159. 'url' => api_get_path(WEB_CODE_PATH).'ticket/tickets.php',
  160. 'name' => get_lang('MyTickets'),
  161. ];
  162. $userId = api_get_user_id();
  163. // Category List
  164. $categoryList = [];
  165. foreach ($types as $type) {
  166. $categoryList[$type['category_id']] = $type['name'].': '.$type['description'];
  167. }
  168. // Status List
  169. $statusAttributes = [
  170. 'style' => 'display: none;',
  171. 'id' => 'status_id',
  172. 'for' => 'status_id',
  173. ];
  174. $statusList = TicketManager::getStatusList();
  175. // Source List
  176. $sourceList = [];
  177. $sourceAttributes = [
  178. 'style' => 'display: none;',
  179. 'id' => 'source_id',
  180. 'for' => 'source_id',
  181. ];
  182. $sourceList[TicketManager::SOURCE_PLATFORM] = get_lang('SrcPlatform');
  183. if (api_is_platform_admin()) {
  184. $sourceAttributes = [
  185. 'id' => 'source_id',
  186. 'for' => 'source_id',
  187. ];
  188. $sourceList[TicketManager::SOURCE_EMAIL] = get_lang('SrcEmail');
  189. $sourceList[TicketManager::SOURCE_PHONE] = get_lang('SrcPhone');
  190. $sourceList[TicketManager::SOURCE_PRESENTIAL] = get_lang('SrcPresential');
  191. }
  192. // Priority List
  193. $priorityList = TicketManager::getPriorityList();
  194. $projectId = isset($_GET['project_id']) ? (int) $_GET['project_id'] : 0;
  195. $form = new FormValidator(
  196. 'send_ticket',
  197. 'POST',
  198. api_get_self().'?project_id='.$projectId,
  199. '',
  200. [
  201. 'enctype' => 'multipart/form-data',
  202. ]
  203. );
  204. $form->addElement(
  205. 'hidden',
  206. 'user_id_request',
  207. '',
  208. [
  209. 'id' => 'user_id_request',
  210. ]
  211. );
  212. $form->addElement(
  213. 'hidden',
  214. 'project_id',
  215. $projectId
  216. );
  217. $form->addElement(
  218. 'hidden',
  219. 'other_area',
  220. '',
  221. [
  222. 'id' => 'other_area',
  223. ]
  224. );
  225. $form->addElement(
  226. 'hidden',
  227. 'email',
  228. '',
  229. [
  230. 'id' => 'email',
  231. ]
  232. );
  233. $form->addSelect(
  234. 'category_id',
  235. get_lang('Category'),
  236. $categoryList,
  237. [
  238. 'id' => 'category_id',
  239. 'for' => 'category_id',
  240. 'style' => 'width: 562px;',
  241. ]
  242. );
  243. $form->addElement(
  244. 'text',
  245. 'subject',
  246. get_lang('Subject'),
  247. [
  248. 'id' => 'subject',
  249. ]
  250. );
  251. $form->addHtmlEditor(
  252. 'content',
  253. get_lang('Message'),
  254. false,
  255. false,
  256. [
  257. 'ToolbarSet' => 'Profile',
  258. 'Height' => '250',
  259. ]
  260. );
  261. if (api_is_platform_admin()) {
  262. $form->addSelectAjax(
  263. 'user_id',
  264. get_lang('Assign'),
  265. null,
  266. ['url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_like']
  267. );
  268. }
  269. $form->addElement(
  270. 'text',
  271. 'personal_email',
  272. get_lang('PersonalEmail'),
  273. [
  274. 'id' => 'personal_email',
  275. ]
  276. );
  277. $form->addLabel(
  278. '',
  279. Display::div(
  280. '',
  281. [
  282. 'id' => 'user_request',
  283. ]
  284. )
  285. );
  286. $form->addElement(
  287. 'select',
  288. 'status_id',
  289. get_lang('Status'),
  290. $statusList,
  291. $statusAttributes
  292. );
  293. $form->addElement(
  294. 'select',
  295. 'priority_id',
  296. get_lang('Priority'),
  297. $priorityList,
  298. [
  299. 'id' => 'priority_id',
  300. 'for' => 'priority_id',
  301. ]
  302. );
  303. $form->addElement(
  304. 'select',
  305. 'source_id',
  306. get_lang('Source'),
  307. $sourceList,
  308. $sourceAttributes
  309. );
  310. $form->addElement(
  311. 'text',
  312. 'phone',
  313. get_lang('Phone').' ('.get_lang('Optional').')',
  314. [
  315. 'id' => 'phone',
  316. ]
  317. );
  318. $sessionList = SessionManager::get_sessions_by_user($userId);
  319. if (api_is_platform_admin() || !empty($sessionList)) {
  320. $sessionListToSelect = [get_lang('Select')];
  321. // Course List
  322. foreach ($sessionList as $sessionInfo) {
  323. $sessionListToSelect[$sessionInfo['session_id']] = $sessionInfo['session_name'];
  324. }
  325. $form->addSelect('session_id', get_lang('Session'), $sessionListToSelect, ['id' => 'session_id']);
  326. } else {
  327. $form->addHidden('session_id', 0);
  328. }
  329. $form->addSelect('course_id', get_lang('Course'), [], ['id' => 'course_id']);
  330. $courseInfo = api_get_course_info();
  331. $params = [];
  332. if (!empty($courseInfo)) {
  333. $params = [
  334. 'course_id' => $courseInfo['real_id'],
  335. ];
  336. $sessionInfo = api_get_session_info(api_get_session_id());
  337. if (!empty($sessionInfo)) {
  338. $params['session_id'] = $sessionInfo['id'];
  339. }
  340. }
  341. $form->setDefaults($params);
  342. $form->addElement('file', 'attach_1', get_lang('FilesAttachment'));
  343. $form->addLabel('', '<span id="filepaths"><div id="filepath_1"></div></span>');
  344. $form->addLabel(
  345. '',
  346. '<span id="link-more-attach">
  347. <span class="btn btn-success" onclick="return add_image_form()">'.get_lang('AddOneMoreFile').'</span>
  348. </span>
  349. ('.sprintf(get_lang('MaximunFileSizeX'), format_file_size(api_get_setting('message_max_upload_filesize'))).')
  350. '
  351. );
  352. $form->addElement('html', '<br/>');
  353. $form->addElement(
  354. 'button',
  355. 'compose',
  356. get_lang('SendMessage'),
  357. null,
  358. null,
  359. null,
  360. 'btn btn-primary',
  361. [
  362. 'id' => 'btnsubmit',
  363. ]
  364. );
  365. $form->addRule('content', get_lang('ThisFieldIsRequired'), 'required');
  366. $form->addRule('category_id', get_lang('ThisFieldIsRequired'), 'required');
  367. $form->addRule('subject', get_lang('ThisFieldIsRequired'), 'required');
  368. if ($form->validate()) {
  369. save_ticket();
  370. }
  371. Display::display_header(get_lang('ComposeMessage'));
  372. echo '<div class="actions">';
  373. echo Display::url(
  374. Display::return_icon('back.png', get_lang('Tickets'), [], ICON_SIZE_MEDIUM),
  375. api_get_path(WEB_CODE_PATH).'ticket/tickets.php'
  376. );
  377. echo '</div>';
  378. $form->display();
  379. Display::display_footer();