new_ticket.php 10 KB

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