new_ticket.php 10 KB

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