new_ticket.php 10 KB

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