new_ticket.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. function changeType() {
  46. var selected = document.getElementById("category_id").selectedIndex;
  47. var id = $("#category_id").val();
  48. $("#other_area").val(other_area[id]);
  49. $("#email").val(email[id]);
  50. if (parseInt(course_required[id]) == 0){
  51. $("#divCourse").css("display", "none");
  52. if( id != "CUR"){
  53. $("#divEmail").css("display", "block");
  54. $("#personal_email").attr("required","required");
  55. }
  56. $("#course_id").disabled = true;
  57. $("#course_id").value = 0;
  58. } else {
  59. $("#divCourse").css("display", "block");
  60. $("#course_id").prop("disabled", false);
  61. $("#course_id").val(0);
  62. }
  63. }
  64. function handleClick2(myRadio) {
  65. var user_id = myRadio.value;
  66. document.getElementById("user_id_request").value = user_id;
  67. alert(document.getElementById("user_id_request").value);
  68. }
  69. function validate() {
  70. var re = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
  71. fckEditor1val = CKEDITOR.instances["content"].getData();
  72. document.getElementById("content").value= fckEditor1val;
  73. var selected = document.getElementById("category_id").selectedIndex;
  74. var id = document.getElementById("category_id").options[selected].value;
  75. if(parseInt(course_required[id]) == 1 && document.getElementById("course_id").value == 0) {
  76. alert("' . addslashes(get_lang("ValidCourse")) . '");
  77. return false;
  78. } else if(id != "CUR" && parseInt(course_required[id]) != 1 && !re.test(document.getElementById("personal_email").value)) {
  79. if (document.getElementById("personal_email").value != "") {
  80. alert("' . addslashes(get_lang("PleaseEnterValidEmail")) . '");
  81. return false;
  82. }
  83. } else if(fckEditor1val == "") {
  84. alert("' . addslashes(get_lang("Filled")) . '");
  85. return false;
  86. }
  87. }
  88. var counter_image = 1;
  89. function remove_image_form(element_id) {
  90. $("#" + element_id).remove();
  91. counter_image = counter_image - 1;
  92. $("#link-more-attach").css("display", "block");
  93. }
  94. function add_image_form() {
  95. // Multiple filepaths for image form
  96. var filepaths = $("#filepaths");
  97. var new_elem, input_file, link_remove, img_remove, new_filepath_id;
  98. if ($("#filepath_"+counter_image)) {
  99. counter_image = counter_image + 1;
  100. } else {
  101. counter_image = counter_image;
  102. }
  103. new_elem = "filepath_"+counter_image;
  104. $("<div/>", {
  105. id: new_elem,
  106. class: "controls"
  107. }).appendTo(filepaths);
  108. input_file = $("<input/>", {
  109. type: "file",
  110. name: "attach_" + counter_image,
  111. size: 20
  112. });
  113. link_remove = $("<a/>", {
  114. onclick: "remove_image_form(\'" + new_elem + "\')",
  115. style: "cursor: pointer"
  116. });
  117. img_remove = $("<img/>", {
  118. src: "' . Display::returnIconPath('delete.png') . '"
  119. });
  120. new_filepath_id = $("#filepath_" + counter_image);
  121. new_filepath_id.append(input_file, link_remove.append(img_remove));
  122. if (counter_image === 6) {
  123. var link_attach = $("#link-more-attach");
  124. if (link_attach) {
  125. $(link_attach).css("display", "none");
  126. }
  127. }
  128. }
  129. </script>
  130. <style>
  131. div.row div.label2 {
  132. float:left;
  133. width:10%;
  134. }
  135. div.row div.formw2 {
  136. width:90%;
  137. float:left
  138. }
  139. div.divTicket {
  140. padding-top: 100px;
  141. }
  142. </style>';
  143. $projectId = isset($_GET['project_id']) ? (int) $_GET['project_id'] : '';
  144. $types = TicketManager::get_all_tickets_categories($projectId, 'category.name ASC');
  145. $htmlHeadXtra[] = '<script language="javascript">
  146. var projects = ' . js_array($types, 'projects', 'project_id') . '
  147. var course_required = ' . js_array($types, 'course_required', 'course_required') . '
  148. var other_area = ' . js_array($types, 'other_area', 'other_area') . '
  149. var email = ' . js_array($types, 'email', 'email') .
  150. '</script>';
  151. /**
  152. * @param $s
  153. * @return string
  154. */
  155. function js_str($s)
  156. {
  157. return '"' . addcslashes($s, "\0..\37\"\\") . '"';
  158. }
  159. /**
  160. * @param $array
  161. * @param $name
  162. * @param $key
  163. * @return string
  164. */
  165. function js_array($array, $name, $key)
  166. {
  167. $return = "new Array(); ";
  168. foreach ($array as $value) {
  169. $return .= $name . "['" . $value['category_id'] . "'] ='" . $value[$key] . "'; ";
  170. }
  171. return $return;
  172. }
  173. /**
  174. *
  175. */
  176. function show_form_send_ticket()
  177. {
  178. global $types;
  179. $userId = api_get_user_id();
  180. // Category List
  181. $categoryList = array();
  182. foreach ($types as $type) {
  183. $categoryList[$type['category_id']] = $type['name'].': '.$type['description'];
  184. }
  185. // Status List
  186. $statusAttributes = array(
  187. 'style' => 'display: none;',
  188. 'id' => 'status_id',
  189. 'for' => 'status_id'
  190. );
  191. $statusList = TicketManager::getStatusList();
  192. // Source List
  193. $sourceList = array();
  194. $sourceAttributes = array(
  195. 'style' => 'display: none;',
  196. 'id' => 'source_id',
  197. 'for' => 'source_id'
  198. );
  199. $sourceList[TicketManager::SOURCE_PLATFORM] = get_lang('SrcPlatform');
  200. if (api_is_platform_admin()) {
  201. $sourceAttributes = array(
  202. 'id' => 'source_id',
  203. 'for' => 'source_id'
  204. );
  205. $sourceList[TicketManager::SOURCE_EMAIL] = get_lang('SrcEmail');
  206. $sourceList[TicketManager::SOURCE_PHONE] = get_lang('SrcPhone');
  207. $sourceList[TicketManager::SOURCE_PRESENTIAL] = get_lang('SrcPresential');
  208. }
  209. // Priority List
  210. $priorityList = TicketManager::getPriorityList();
  211. $projectId = isset($_GET['project_id']) ? (int) $_GET['project_id'] : 0;
  212. $form = new FormValidator(
  213. 'send_ticket',
  214. 'POST',
  215. api_get_self().'?project_id='.$projectId,
  216. '',
  217. array(
  218. 'enctype' => 'multipart/form-data',
  219. 'onsubmit' => 'return validate()'
  220. )
  221. );
  222. $form->addElement(
  223. 'hidden',
  224. 'user_id_request',
  225. '',
  226. array(
  227. 'id' => 'user_id_request'
  228. )
  229. );
  230. $form->addElement(
  231. 'hidden',
  232. 'project_id',
  233. $projectId
  234. );
  235. $form->addElement(
  236. 'hidden',
  237. 'other_area',
  238. '',
  239. array(
  240. 'id' => 'other_area'
  241. )
  242. );
  243. $form->addElement(
  244. 'hidden',
  245. 'email',
  246. '',
  247. array(
  248. 'id' => 'email'
  249. )
  250. );
  251. $form->addElement(
  252. 'select',
  253. 'category_id',
  254. get_lang('Category'),
  255. $categoryList,
  256. array(
  257. 'onchange' => 'changeType()',
  258. 'id' => 'category_id',
  259. 'for' => 'category_id',
  260. 'style' => 'width: 562px;'
  261. )
  262. );
  263. $form->addElement(
  264. 'text',
  265. 'subject',
  266. get_lang('Subject'),
  267. array(
  268. 'id' => 'subject'
  269. )
  270. );
  271. $form->addHtmlEditor(
  272. 'content',
  273. get_lang('Message'),
  274. false,
  275. false,
  276. array(
  277. 'ToolbarSet' => 'Profile',
  278. 'Height' => '250'
  279. )
  280. );
  281. if (api_is_platform_admin()) {
  282. $form->addElement(
  283. 'SelectAjax',
  284. 'user_id',
  285. get_lang('Assign'),
  286. null,
  287. ['url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_like']
  288. );
  289. }
  290. $form->addElement(
  291. 'text',
  292. 'personal_email',
  293. get_lang('PersonalEmail'),
  294. array(
  295. 'id' => 'personal_email'
  296. )
  297. );
  298. $form->addLabel(
  299. '',
  300. Display::div(
  301. '',
  302. array(
  303. 'id' => 'user_request'
  304. )
  305. )
  306. );
  307. $form->addElement(
  308. 'select',
  309. 'status_id',
  310. get_lang('Status'),
  311. $statusList,
  312. $statusAttributes
  313. );
  314. $form->addElement(
  315. 'select',
  316. 'priority_id',
  317. get_lang('Priority'),
  318. $priorityList,
  319. array(
  320. 'id' => 'priority_id',
  321. 'for' => 'priority_id'
  322. )
  323. );
  324. $form->addElement(
  325. 'select',
  326. 'source_id',
  327. get_lang('Source'),
  328. $sourceList,
  329. $sourceAttributes
  330. );
  331. $form->addElement(
  332. 'text',
  333. 'phone',
  334. get_lang('Phone') . ' (' . get_lang('Optional') . ')',
  335. array(
  336. 'id' => 'phone'
  337. )
  338. );
  339. $sessionList = SessionManager::get_sessions_by_user($userId);
  340. $sessionListToSelect = array(get_lang('Select'));
  341. //Course List
  342. foreach ($sessionList as $sessionInfo) {
  343. $sessionListToSelect[$sessionInfo['session_id']] = $sessionInfo['session_name'];
  344. }
  345. $form->addSelect('session_id', get_lang('Session'), $sessionListToSelect, ['id' => 'session_id']);
  346. $form->addSelect('course_id', get_lang('Course'), [], ['id' => 'course_id']);
  347. $courseInfo = api_get_course_info();
  348. $params = [];
  349. if (!empty($courseInfo)) {
  350. $params = [
  351. 'course_id' => $courseInfo['real_id']
  352. ];
  353. $sessionInfo = api_get_session_info(api_get_session_id());
  354. if (!empty($sessionInfo)) {
  355. $params['session_id'] = $sessionInfo['id'];
  356. }
  357. }
  358. $form->setDefaults($params);
  359. $form->addElement('file', 'attach_1', get_lang('FilesAttachment'));
  360. $form->addLabel('', '<span id="filepaths"><div id="filepath_1"></div></span>');
  361. $form->addLabel('',
  362. '<span id="link-more-attach">
  363. <span class="btn btn-success" onclick="return add_image_form()">' . get_lang('AddOneMoreFile') . '</span>
  364. </span>
  365. ('.sprintf(get_lang('MaximunFileSizeX'), format_file_size(api_get_setting('message_max_upload_filesize'))).')
  366. ');
  367. $form->addElement('html', '<br/>');
  368. $form->addElement(
  369. 'button',
  370. 'compose',
  371. get_lang('SendMessage'),
  372. null,
  373. null,
  374. null,
  375. 'btn btn-primary',
  376. array(
  377. 'id' => 'btnsubmit'
  378. )
  379. );
  380. $form->addRule('content', get_lang('ThisFieldIsRequired'), 'required');
  381. $form->addRule('category_id', get_lang('ThisFieldIsRequired'), 'required');
  382. $form->addRule('subject', get_lang('ThisFieldIsRequired'), 'required');
  383. $form->display();
  384. }
  385. /**
  386. *
  387. */
  388. function save_ticket()
  389. {
  390. $category_id = $_POST['category_id'];
  391. $content = $_POST['content'];
  392. if ($_POST['phone'] != '') {
  393. $content .= '<p style="color:red">&nbsp;' . get_lang('Phone') . ': ' . Security::remove_XSS($_POST['phone']). '</p>';
  394. }
  395. $course_id = isset($_POST['course_id']) ? $_POST['course_id'] : '';
  396. $sessionId = isset($_POST['session_id']) ? $_POST['session_id'] : '';
  397. $project_id = $_POST['project_id'];
  398. $subject = $_POST['subject'];
  399. $other_area = (int) $_POST['other_area'];
  400. $email = $_POST['email'];
  401. $personal_email = $_POST['personal_email'];
  402. $source = $_POST['source_id'];
  403. $user_id = isset($_POST['user_id']) ? $_POST['user_id'] : 0;
  404. $priority = isset($_POST['priority_id']) ? $_POST['priority_id'] : '';
  405. $status = isset($_POST['status_id']) ? $_POST['status_id'] : '';
  406. $file_attachments = $_FILES;
  407. if (TicketManager::add(
  408. $category_id,
  409. $course_id,
  410. $sessionId,
  411. $project_id,
  412. $other_area,
  413. $email,
  414. $subject,
  415. $content,
  416. $personal_email,
  417. $file_attachments,
  418. $source,
  419. $priority,
  420. $status,
  421. $user_id
  422. )) {
  423. header('Location:' . api_get_path(WEB_CODE_PATH).'ticket/tickets.php');
  424. exit;
  425. } else {
  426. Display::display_header(get_lang('ComposeMessage'));
  427. Display::display_error_message(get_lang('ErrorRegisterMessage'));
  428. }
  429. }
  430. /**
  431. * Get the total number of users on the platform
  432. * @return int The number of users
  433. * @see SortableTable#get_total_number_of_items()
  434. */
  435. function get_number_of_users()
  436. {
  437. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  438. $sql = "SELECT COUNT(u.user_id) AS total_number_of_items FROM $user_table u";
  439. if ((api_is_platform_admin() || api_is_session_admin()) && api_get_multiple_access_url()) {
  440. $access_url_rel_user_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  441. $sql.= " INNER JOIN $access_url_rel_user_table url_rel_user ON (u.user_id=url_rel_user.user_id)";
  442. }
  443. if (isset($_GET['keyword'])) {
  444. $keyword = Database::escape_string(trim($_GET['keyword']));
  445. $sql .= " WHERE (u.firstname LIKE '%$keyword%' OR
  446. u.lastname LIKE '%$keyword%' OR
  447. concat(u.firstname,' ',u.lastname) LIKE '%$keyword%' OR
  448. concat(u.lastname,' ',u.firstname) LIKE '%$keyword%' OR
  449. u.username LIKE '%$keyword%' OR
  450. u.email LIKE '%$keyword%' OR
  451. u.official_code LIKE '%$keyword%') ";
  452. }
  453. $res = Database::query($sql);
  454. $obj = Database::fetch_object($res);
  455. return $obj->total_number_of_items;
  456. }
  457. /**
  458. * Get the users to display on the current page (fill the sortable-table)
  459. * @param int offset of first user to recover
  460. * @param int Number of users to get
  461. * @param int Column to sort on
  462. * @param string Order (ASC,DESC)
  463. * @return array A list of users with their data
  464. * @see SortableTable#get_table_data($from)
  465. */
  466. function get_user_data($from, $number_of_items, $column, $direction)
  467. {
  468. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  469. if (api_is_western_name_order()) {
  470. $col34 = "u.firstname AS col3,
  471. u.lastname AS col4,";
  472. } else {
  473. $col34 = "u.lastname AS col3,
  474. u.firstname AS col4,";
  475. }
  476. $sql = "SELECT
  477. u.user_id AS col0,
  478. u.official_code AS col2,
  479. $col34
  480. u.username AS col5,
  481. u.email AS col6,
  482. u.status AS col7,
  483. u.active AS col8,
  484. u.user_id AS col9 ,
  485. u.expiration_date AS exp
  486. FROM $user_table u ";
  487. if (isset($_GET['keyword'])) {
  488. $keyword = Database::escape_string(trim($_GET['keyword']));
  489. $sql .= " WHERE (u.firstname LIKE '%$keyword%' OR
  490. u.lastname LIKE '%$keyword%' OR
  491. concat(u.firstname,' ',u.lastname) LIKE '%$keyword%' OR
  492. concat(u.lastname,' ',u.firstname) LIKE '%$keyword%' OR
  493. u.username LIKE '%$keyword%' OR
  494. u.official_code LIKE '%$keyword%' OR
  495. u.email LIKE '%$keyword%' )";
  496. }
  497. if (!in_array($direction, array('ASC', 'DESC'))) {
  498. $direction = 'ASC';
  499. }
  500. $column = intval($column);
  501. $from = intval($from);
  502. $number_of_items = intval($number_of_items);
  503. $sql .= " ORDER BY col$column $direction ";
  504. $sql .= " LIMIT $from, $number_of_items";
  505. $res = Database::query($sql);
  506. $users = array();
  507. while ($user = Database::fetch_row($res)) {
  508. $user_id = $user[0];
  509. $userPicture = UserManager::getUserPicture($user_id);
  510. $photo = '<img src="' . $userPicture. '" alt="' . api_get_person_name($user[2], $user[3]) . '" title="' . api_get_person_name($user[2], $user[3]) . '" />';
  511. $users[] = array(
  512. $photo,
  513. $user_id,
  514. $user[2],
  515. $user[3],
  516. $user[4],
  517. $user[5]
  518. );
  519. }
  520. return $users;
  521. }
  522. $interbreadcrumb[] = array(
  523. 'url' => api_get_path(WEB_CODE_PATH).'ticket/tickets.php',
  524. 'name' => get_lang('MyTickets')
  525. );
  526. if (!isset($_POST['compose'])) {
  527. Display::display_header(get_lang('ComposeMessage'));
  528. show_form_send_ticket();
  529. } else {
  530. save_ticket();
  531. }
  532. Display::display_footer();