TicketManager.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\TicketBundle\Entity\MessageAttachment;
  4. use Chamilo\TicketBundle\Entity\Priority;
  5. use Chamilo\TicketBundle\Entity\Project;
  6. use Chamilo\TicketBundle\Entity\Status;
  7. use Chamilo\TicketBundle\Entity\Ticket;
  8. /**
  9. * Class TicketManager.
  10. *
  11. * @package chamilo.plugin.ticket
  12. */
  13. class TicketManager
  14. {
  15. const PRIORITY_NORMAL = 'NRM';
  16. const PRIORITY_HIGH = 'HGH';
  17. const PRIORITY_LOW = 'LOW';
  18. const SOURCE_EMAIL = 'MAI';
  19. const SOURCE_PHONE = 'TEL';
  20. const SOURCE_PLATFORM = 'PLA';
  21. const SOURCE_PRESENTIAL = 'PRE';
  22. const STATUS_NEW = 'NAT';
  23. const STATUS_PENDING = 'PND';
  24. const STATUS_UNCONFIRMED = 'XCF';
  25. const STATUS_CLOSE = 'CLS';
  26. const STATUS_FORWARDED = 'REE';
  27. /**
  28. * Constructor.
  29. */
  30. public function __construct()
  31. {
  32. }
  33. /**
  34. * Get categories of tickets.
  35. *
  36. * @param int $projectId
  37. * @param string $order
  38. *
  39. * @return array
  40. */
  41. public static function get_all_tickets_categories($projectId, $order = '')
  42. {
  43. $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
  44. $table_support_project = Database::get_main_table(TABLE_TICKET_PROJECT);
  45. $order = empty($order) ? 'category.total_tickets DESC' : $order;
  46. $projectId = (int) $projectId;
  47. $sql = "SELECT
  48. category.*,
  49. category.id category_id,
  50. project.other_area,
  51. project.email
  52. FROM
  53. $table_support_category category
  54. INNER JOIN $table_support_project project
  55. ON project.id = category.project_id
  56. WHERE project.id = $projectId
  57. ORDER BY $order";
  58. $result = Database::query($sql);
  59. $types = [];
  60. while ($row = Database::fetch_assoc($result)) {
  61. $types[] = $row;
  62. }
  63. return $types;
  64. }
  65. /**
  66. * @param $from
  67. * @param $numberItems
  68. * @param $column
  69. * @param $direction
  70. *
  71. * @return array
  72. */
  73. public static function getCategories($from, $numberItems, $column, $direction)
  74. {
  75. $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
  76. $sql = "SELECT id, name, description, total_tickets
  77. FROM $table";
  78. if (!in_array($direction, ['ASC', 'DESC'])) {
  79. $direction = 'ASC';
  80. }
  81. $column = intval($column);
  82. $from = intval($from);
  83. $numberItems = intval($numberItems);
  84. //$sql .= " ORDER BY col$column $direction ";
  85. $sql .= " LIMIT $from,$numberItems";
  86. $result = Database::query($sql);
  87. $types = [];
  88. while ($row = Database::fetch_array($result)) {
  89. $types[] = $row;
  90. }
  91. return $types;
  92. }
  93. /**
  94. * @param int $id
  95. *
  96. * @return array|mixed
  97. */
  98. public static function getCategory($id)
  99. {
  100. $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
  101. $id = intval($id);
  102. $sql = "SELECT id, name, description, total_tickets
  103. FROM $table WHERE id = $id";
  104. $result = Database::query($sql);
  105. $category = Database::fetch_array($result);
  106. return $category;
  107. }
  108. /**
  109. * @return int
  110. */
  111. public static function getCategoriesCount()
  112. {
  113. $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
  114. $sql = "SELECT count(id) count
  115. FROM $table ";
  116. $result = Database::query($sql);
  117. $category = Database::fetch_array($result);
  118. return $category['count'];
  119. }
  120. /**
  121. * @param int $id
  122. * @param array $params
  123. */
  124. public static function updateCategory($id, $params)
  125. {
  126. $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
  127. $id = intval($id);
  128. Database::update($table, $params, ['id = ?' => $id]);
  129. }
  130. /**
  131. * @param array $params
  132. */
  133. public static function addCategory($params)
  134. {
  135. $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
  136. Database::insert($table, $params);
  137. }
  138. /**
  139. * @param int $id
  140. *
  141. * @return bool
  142. */
  143. public static function deleteCategory($id)
  144. {
  145. $id = intval($id);
  146. if (empty($id)) {
  147. return false;
  148. }
  149. $table = Database::get_main_table(TABLE_TICKET_TICKET);
  150. $sql = "UPDATE $table SET category_id = NULL WHERE category_id = $id";
  151. Database::query($sql);
  152. $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
  153. $sql = "DELETE FROM $table WHERE id = $id";
  154. Database::query($sql);
  155. return true;
  156. }
  157. /**
  158. * @param int $categoryId
  159. * @param array $users
  160. *
  161. * @return bool
  162. */
  163. public static function addUsersToCategory($categoryId, $users)
  164. {
  165. if (empty($users) || empty($categoryId)) {
  166. return false;
  167. }
  168. $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
  169. foreach ($users as $userId) {
  170. if (self::userIsAssignedToCategory($userId, $categoryId) == false) {
  171. $params = [
  172. 'category_id' => $categoryId,
  173. 'user_id' => $userId,
  174. ];
  175. Database::insert($table, $params);
  176. }
  177. }
  178. return true;
  179. }
  180. /**
  181. * @param int $userId
  182. * @param int $categoryId
  183. *
  184. * @return bool
  185. */
  186. public static function userIsAssignedToCategory($userId, $categoryId)
  187. {
  188. $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
  189. $userId = intval($userId);
  190. $categoryId = intval($categoryId);
  191. $sql = "SELECT * FROM $table
  192. WHERE category_id = $categoryId AND user_id = $userId";
  193. $result = Database::query($sql);
  194. return Database::num_rows($result) > 0;
  195. }
  196. /**
  197. * @param int $categoryId
  198. *
  199. * @return array
  200. */
  201. public static function getUsersInCategory($categoryId)
  202. {
  203. $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
  204. $categoryId = intval($categoryId);
  205. $sql = "SELECT * FROM $table WHERE category_id = $categoryId";
  206. $result = Database::query($sql);
  207. return Database::store_result($result);
  208. }
  209. /**
  210. * @param int $categoryId
  211. */
  212. public static function deleteAllUserInCategory($categoryId)
  213. {
  214. $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
  215. $categoryId = intval($categoryId);
  216. $sql = "DELETE FROM $table WHERE category_id = $categoryId";
  217. Database::query($sql);
  218. }
  219. /**
  220. * Get all possible tickets statuses.
  221. *
  222. * @return array
  223. */
  224. public static function get_all_tickets_status()
  225. {
  226. $table = Database::get_main_table(TABLE_TICKET_STATUS);
  227. $sql = "SELECT * FROM ".$table;
  228. $result = Database::query($sql);
  229. $types = [];
  230. while ($row = Database::fetch_assoc($result)) {
  231. $types[] = $row;
  232. }
  233. return $types;
  234. }
  235. /**
  236. * Inserts a new ticket in the corresponding tables.
  237. *
  238. * @param int $category_id
  239. * @param int $course_id
  240. * @param int $sessionId
  241. * @param int $project_id
  242. * @param string $other_area
  243. * @param string $subject
  244. * @param string $content
  245. * @param string $personalEmail
  246. * @param array $fileAttachments
  247. * @param string $source
  248. * @param string $priority
  249. * @param string $status
  250. * @param int $assignedUserId
  251. *
  252. * @return bool
  253. */
  254. public static function add(
  255. $category_id,
  256. $course_id,
  257. $sessionId,
  258. $project_id,
  259. $other_area,
  260. $subject,
  261. $content,
  262. $personalEmail = '',
  263. $fileAttachments = [],
  264. $source = '',
  265. $priority = '',
  266. $status = '',
  267. $assignedUserId = 0
  268. ) {
  269. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  270. $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
  271. if (empty($category_id)) {
  272. return false;
  273. }
  274. $currentUserId = api_get_user_id();
  275. $currentUserInfo = api_get_user_info();
  276. $now = api_get_utc_datetime();
  277. $course_id = intval($course_id);
  278. $category_id = intval($category_id);
  279. $project_id = intval($project_id);
  280. $priority = empty($priority) ? self::PRIORITY_NORMAL : $priority;
  281. if ($status === '') {
  282. $status = self::STATUS_NEW;
  283. if ($other_area > 0) {
  284. $status = self::STATUS_FORWARDED;
  285. }
  286. }
  287. if (!empty($category_id)) {
  288. if (empty($assignedUserId)) {
  289. $usersInCategory = self::getUsersInCategory($category_id);
  290. if (!empty($usersInCategory) && count($usersInCategory) > 0) {
  291. $userCategoryInfo = $usersInCategory[0];
  292. if (isset($userCategoryInfo['user_id'])) {
  293. $assignedUserId = $userCategoryInfo['user_id'];
  294. }
  295. }
  296. }
  297. }
  298. $assignedUserInfo = [];
  299. if (!empty($assignedUserId)) {
  300. $assignedUserInfo = api_get_user_info($assignedUserId);
  301. if (empty($assignedUserInfo)) {
  302. return false;
  303. }
  304. }
  305. // insert_ticket
  306. $params = [
  307. 'project_id' => $project_id,
  308. 'category_id' => $category_id,
  309. 'priority_id' => $priority,
  310. 'personal_email' => $personalEmail,
  311. 'status_id' => $status,
  312. 'start_date' => $now,
  313. 'sys_insert_user_id' => $currentUserId,
  314. 'sys_insert_datetime' => $now,
  315. 'sys_lastedit_user_id' => $currentUserId,
  316. 'sys_lastedit_datetime' => $now,
  317. 'source' => $source,
  318. 'assigned_last_user' => $assignedUserId,
  319. 'subject' => $subject,
  320. 'message' => $content,
  321. ];
  322. if (!empty($course_id)) {
  323. $params['course_id'] = $course_id;
  324. }
  325. if (!empty($sessionId)) {
  326. $params['session_id'] = $sessionId;
  327. }
  328. $ticketId = Database::insert($table_support_tickets, $params);
  329. if ($ticketId) {
  330. $ticket_code = "A".str_pad($ticketId, 11, '0', STR_PAD_LEFT);
  331. $titleCreated = sprintf(
  332. get_lang('TicketXCreated'),
  333. $ticket_code
  334. );
  335. Display::addFlash(Display::return_message(
  336. $titleCreated,
  337. 'normal',
  338. false
  339. ));
  340. if ($assignedUserId != 0) {
  341. self::assignTicketToUser(
  342. $ticketId,
  343. $assignedUserId
  344. );
  345. Display::addFlash(Display::return_message(
  346. sprintf(
  347. get_lang('TicketXAssignedToUserX'),
  348. $ticket_code,
  349. $assignedUserInfo['complete_name']
  350. ),
  351. 'normal',
  352. false
  353. ));
  354. }
  355. if (!empty($fileAttachments)) {
  356. $attachmentCount = 0;
  357. foreach ($fileAttachments as $attach) {
  358. if (!empty($attach['tmp_name'])) {
  359. $attachmentCount++;
  360. }
  361. }
  362. if ($attachmentCount > 0) {
  363. self::insertMessage(
  364. $ticketId,
  365. '',
  366. '',
  367. $fileAttachments,
  368. $currentUserId
  369. );
  370. }
  371. }
  372. // Update code
  373. $sql = "UPDATE $table_support_tickets
  374. SET code = '$ticket_code'
  375. WHERE id = '$ticketId'";
  376. Database::query($sql);
  377. // Update total
  378. $sql = "UPDATE $table_support_category
  379. SET total_tickets = total_tickets + 1
  380. WHERE id = $category_id";
  381. Database::query($sql);
  382. $helpDeskMessage =
  383. '<table>
  384. <tr>
  385. <td width="100px"><b>'.get_lang('User').'</b></td>
  386. <td width="400px">'.$currentUserInfo['complete_name'].'</td>
  387. </tr>
  388. <tr>
  389. <td width="100px"><b>'.get_lang('Username').'</b></td>
  390. <td width="400px">'.$currentUserInfo['username'].'</td>
  391. </tr>
  392. <tr>
  393. <td width="100px"><b>'.get_lang('Email').'</b></td>
  394. <td width="400px">'.$currentUserInfo['email'].'</td>
  395. </tr>
  396. <tr>
  397. <td width="100px"><b>'.get_lang('Phone').'</b></td>
  398. <td width="400px">'.$currentUserInfo['phone'].'</td>
  399. </tr>
  400. <tr>
  401. <td width="100px"><b>'.get_lang('Date').'</b></td>
  402. <td width="400px">'.api_convert_and_format_date($now, DATE_TIME_FORMAT_LONG).'</td>
  403. </tr>
  404. <tr>
  405. <td width="100px"><b>'.get_lang('Title').'</b></td>
  406. <td width="400px">'.$subject.'</td>
  407. </tr>
  408. <tr>
  409. <td width="100px"><b>'.get_lang('Description').'</b></td>
  410. <td width="400px">'.$content.'</td>
  411. </tr>
  412. </table>';
  413. if ($assignedUserId != 0) {
  414. $href = api_get_path(WEB_CODE_PATH).'/ticket/ticket_details.php?ticket_id='.$ticketId;
  415. $helpDeskMessage .= sprintf(
  416. get_lang('TicketAssignedToXCheckZAtLinkY'),
  417. $assignedUserInfo['complete_name'],
  418. $href,
  419. $ticketId
  420. );
  421. }
  422. if (empty($category_id)) {
  423. if (api_get_setting('ticket_send_warning_to_all_admins') === 'true') {
  424. $warningSubject = sprintf(
  425. get_lang('TicketXCreatedWithNoCategory'),
  426. $ticket_code
  427. );
  428. Display::addFlash(Display::return_message($warningSubject));
  429. $admins = UserManager::get_all_administrators();
  430. foreach ($admins as $userId => $data) {
  431. if ($data['active']) {
  432. MessageManager::send_message_simple(
  433. $userId,
  434. $warningSubject,
  435. $helpDeskMessage
  436. );
  437. }
  438. }
  439. }
  440. } else {
  441. $categoryInfo = self::getCategory($category_id);
  442. $usersInCategory = self::getUsersInCategory($category_id);
  443. $message = '<h2>'.get_lang('TicketInformation').'</h2><br />'.$helpDeskMessage;
  444. if (api_get_setting('ticket_warn_admin_no_user_in_category') === 'true') {
  445. $usersInCategory = self::getUsersInCategory($category_id);
  446. if (empty($usersInCategory)) {
  447. $subject = sprintf(
  448. get_lang('WarningCategoryXDoesntHaveUsers'),
  449. $categoryInfo['name']
  450. );
  451. if (api_get_setting('ticket_send_warning_to_all_admins') === 'true') {
  452. Display::addFlash(Display::return_message(
  453. sprintf(
  454. get_lang('CategoryWithNoUserNotificationSentToAdmins'),
  455. $categoryInfo['name']
  456. ),
  457. null,
  458. false
  459. ));
  460. $admins = UserManager::get_all_administrators();
  461. foreach ($admins as $userId => $data) {
  462. if ($data['active']) {
  463. self::sendNotification(
  464. $ticketId,
  465. $subject,
  466. $message,
  467. $userId
  468. );
  469. }
  470. }
  471. } else {
  472. Display::addFlash(Display::return_message($subject));
  473. }
  474. }
  475. }
  476. // Send notification to all users
  477. if (!empty($usersInCategory)) {
  478. foreach ($usersInCategory as $data) {
  479. if ($data['user_id']) {
  480. self::sendNotification(
  481. $ticketId,
  482. $subject,
  483. $message,
  484. $data['user_id']
  485. );
  486. }
  487. }
  488. }
  489. }
  490. if (!empty($personalEmail)) {
  491. api_mail_html(
  492. get_lang('VirtualSupport'),
  493. $personalEmail,
  494. get_lang('IncidentResentToVirtualSupport'),
  495. $helpDeskMessage
  496. );
  497. }
  498. self::sendNotification(
  499. $ticketId,
  500. $titleCreated,
  501. $helpDeskMessage
  502. );
  503. return true;
  504. } else {
  505. return false;
  506. }
  507. }
  508. /**
  509. * Assign ticket to admin.
  510. *
  511. * @param int $ticketId
  512. * @param int $userId
  513. *
  514. * @return bool
  515. */
  516. public static function assignTicketToUser(
  517. $ticketId,
  518. $userId
  519. ) {
  520. $ticketId = intval($ticketId);
  521. $userId = intval($userId);
  522. if (empty($ticketId)) {
  523. return false;
  524. }
  525. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  526. $ticket = self::get_ticket_detail_by_id($ticketId);
  527. if ($ticket) {
  528. $sql = "UPDATE $table_support_tickets
  529. SET assigned_last_user = $userId
  530. WHERE id = $ticketId";
  531. Database::query($sql);
  532. $table = Database::get_main_table(TABLE_TICKET_ASSIGNED_LOG);
  533. $params = [
  534. 'ticket_id' => $ticketId,
  535. 'user_id' => $userId,
  536. 'sys_insert_user_id' => api_get_user_id(),
  537. 'assigned_date' => api_get_utc_datetime(),
  538. ];
  539. Database::insert($table, $params);
  540. return true;
  541. } else {
  542. return false;
  543. }
  544. }
  545. /**
  546. * Insert message between Users and Admins.
  547. *
  548. * @param int $ticketId
  549. * @param string $subject
  550. * @param string $content
  551. * @param array $fileAttachments
  552. * @param int $userId
  553. * @param string $status
  554. * @param bool $sendConfirmation
  555. *
  556. * @return bool
  557. */
  558. public static function insertMessage(
  559. $ticketId,
  560. $subject,
  561. $content,
  562. $fileAttachments,
  563. $userId,
  564. $status = 'NOL',
  565. $sendConfirmation = false
  566. ) {
  567. $ticketId = intval($ticketId);
  568. $userId = intval($userId);
  569. $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
  570. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  571. if ($sendConfirmation) {
  572. $form = '<form action="ticket_details.php?ticket_id='.$ticketId.'" id="confirmticket" method="POST" >
  573. <p>'.get_lang('TicketWasThisAnswerSatisfying').'</p>
  574. <button class="btn btn-primary responseyes" name="response" id="responseyes" value="1">'.get_lang('Yes').'</button>
  575. <button class="btn btn-danger responseno" name="response" id="responseno" value="0">'.get_lang('No').'</button>
  576. </form>';
  577. $content .= $form;
  578. }
  579. $now = api_get_utc_datetime();
  580. $params = [
  581. 'ticket_id' => $ticketId,
  582. 'subject' => $subject,
  583. 'message' => $content,
  584. 'ip_address' => $_SERVER['REMOTE_ADDR'],
  585. 'sys_insert_user_id' => $userId,
  586. 'sys_insert_datetime' => $now,
  587. 'sys_lastedit_user_id' => $userId,
  588. 'sys_lastedit_datetime' => $now,
  589. 'status' => $status,
  590. ];
  591. $messageId = Database::insert($table_support_messages, $params);
  592. if ($messageId) {
  593. // update_total_message
  594. $sql = "UPDATE $table_support_tickets
  595. SET
  596. sys_lastedit_user_id ='$userId',
  597. sys_lastedit_datetime ='$now',
  598. total_messages = (
  599. SELECT COUNT(*) as total_messages
  600. FROM $table_support_messages
  601. WHERE ticket_id ='$ticketId'
  602. )
  603. WHERE id = $ticketId ";
  604. Database::query($sql);
  605. if (is_array($fileAttachments)) {
  606. foreach ($fileAttachments as $file_attach) {
  607. if ($file_attach['error'] == 0) {
  608. self::saveMessageAttachmentFile(
  609. $file_attach,
  610. $ticketId,
  611. $messageId
  612. );
  613. } else {
  614. if ($file_attach['error'] != UPLOAD_ERR_NO_FILE) {
  615. return false;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. return true;
  622. }
  623. /**
  624. * Attachment files when a message is sent.
  625. *
  626. * @param $file_attach
  627. * @param $ticketId
  628. * @param $message_id
  629. *
  630. * @return bool
  631. */
  632. public static function saveMessageAttachmentFile(
  633. $file_attach,
  634. $ticketId,
  635. $message_id
  636. ) {
  637. $now = api_get_utc_datetime();
  638. $userId = api_get_user_id();
  639. $ticketId = intval($ticketId);
  640. $new_file_name = add_ext_on_mime(
  641. stripslashes($file_attach['name']),
  642. $file_attach['type']
  643. );
  644. $table_support_message_attachments = Database::get_main_table(TABLE_TICKET_MESSAGE_ATTACHMENTS);
  645. if (!filter_extension($new_file_name)) {
  646. echo Display::return_message(
  647. get_lang('UplUnableToSaveFileFilteredExtension'),
  648. 'error'
  649. );
  650. } else {
  651. $result = api_upload_file('ticket_attachment', $file_attach, $ticketId);
  652. if ($result) {
  653. $safe_file_name = Database::escape_string($new_file_name);
  654. $safe_new_file_name = Database::escape_string($result['path_to_save']);
  655. $sql = "INSERT INTO $table_support_message_attachments (
  656. filename,
  657. path,
  658. ticket_id,
  659. message_id,
  660. size,
  661. sys_insert_user_id,
  662. sys_insert_datetime,
  663. sys_lastedit_user_id,
  664. sys_lastedit_datetime
  665. ) VALUES (
  666. '$safe_file_name',
  667. '$safe_new_file_name',
  668. '$ticketId',
  669. '$message_id',
  670. '".$file_attach['size']."',
  671. '$userId',
  672. '$now',
  673. '$userId',
  674. '$now'
  675. )";
  676. Database::query($sql);
  677. return true;
  678. }
  679. }
  680. }
  681. /**
  682. * Get tickets by userId.
  683. *
  684. * @param int $from
  685. * @param int $number_of_items
  686. * @param $column
  687. * @param $direction
  688. * @param int $userId
  689. *
  690. * @return array
  691. */
  692. public static function get_tickets_by_user_id(
  693. $from,
  694. $number_of_items,
  695. $column,
  696. $direction,
  697. $userId = 0
  698. ) {
  699. $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
  700. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  701. $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
  702. $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
  703. $direction = !empty($direction) ? $direction : 'DESC';
  704. $userId = !empty($userId) ? $userId : api_get_user_id();
  705. $userInfo = api_get_user_info($userId);
  706. $isAdmin = UserManager::is_admin($userId);
  707. if (!isset($_GET['project_id'])) {
  708. return [];
  709. }
  710. switch ($column) {
  711. case 0:
  712. $column = 'ticket_id';
  713. break;
  714. case 1:
  715. $column = 'status_name';
  716. break;
  717. case 2:
  718. $column = 'start_date';
  719. break;
  720. case 3:
  721. $column = 'sys_lastedit_datetime';
  722. break;
  723. case 4:
  724. $column = 'category_name';
  725. break;
  726. case 5:
  727. $column = 'sys_insert_user_id';
  728. break;
  729. case 6:
  730. $column = 'assigned_last_user';
  731. break;
  732. case 7:
  733. $column = 'total_messages';
  734. break;
  735. case 8:
  736. $column = 'subject';
  737. break;
  738. default:
  739. $column = 'ticket_id';
  740. }
  741. $sql = "SELECT DISTINCT
  742. ticket.*,
  743. ticket.id ticket_id,
  744. status.name AS status_name,
  745. ticket.start_date,
  746. ticket.sys_lastedit_datetime,
  747. cat.name AS category_name,
  748. priority.name AS priority_name,
  749. ticket.total_messages AS total_messages,
  750. ticket.message AS message,
  751. ticket.subject AS subject,
  752. ticket.assigned_last_user
  753. FROM $table_support_tickets ticket
  754. INNER JOIN $table_support_category cat
  755. ON (cat.id = ticket.category_id)
  756. INNER JOIN $table_support_priority priority
  757. ON (ticket.priority_id = priority.id)
  758. INNER JOIN $table_support_status status
  759. ON (ticket.status_id = status.id)
  760. WHERE 1=1
  761. ";
  762. $projectId = (int) $_GET['project_id'];
  763. $userIsAllowInProject = self::userIsAllowInProject($userInfo, $projectId);
  764. // Check if a role was set to the project
  765. if ($userIsAllowInProject == false) {
  766. $sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
  767. }
  768. // Search simple
  769. if (isset($_GET['submit_simple']) && $_GET['keyword'] != '') {
  770. $keyword = Database::escape_string(trim($_GET['keyword']));
  771. $sql .= " AND (
  772. ticket.id LIKE '%$keyword%' OR
  773. ticket.code LIKE '%$keyword%' OR
  774. ticket.subject LIKE '%$keyword%' OR
  775. ticket.message LIKE '%$keyword%' OR
  776. ticket.keyword LIKE '%$keyword%' OR
  777. ticket.source LIKE '%$keyword%' OR
  778. cat.name LIKE '%$keyword%' OR
  779. status.name LIKE '%$keyword%' OR
  780. priority.name LIKE '%$keyword%' OR
  781. ticket.personal_email LIKE '%$keyword%'
  782. )";
  783. }
  784. $keywords = [
  785. 'project_id' => 'ticket.project_id',
  786. 'keyword_category' => 'ticket.category_id',
  787. 'keyword_assigned_to' => 'ticket.assigned_last_user',
  788. 'keyword_source' => 'ticket.source ',
  789. 'keyword_status' => 'ticket.status_id',
  790. 'keyword_priority' => 'ticket.priority_id',
  791. ];
  792. foreach ($keywords as $keyword => $label) {
  793. if (isset($_GET[$keyword])) {
  794. $data = Database::escape_string(trim($_GET[$keyword]));
  795. if (!empty($data)) {
  796. $sql .= " AND $label = '$data' ";
  797. }
  798. }
  799. }
  800. // Search advanced
  801. $keyword_start_date_start = isset($_GET['keyword_start_date_start']) ? Database::escape_string(trim($_GET['keyword_start_date_start'])) : '';
  802. $keyword_start_date_end = isset($_GET['keyword_start_date_end']) ? Database::escape_string(trim($_GET['keyword_start_date_end'])) : '';
  803. $keyword_course = isset($_GET['keyword_course']) ? Database::escape_string(trim($_GET['keyword_course'])) : '';
  804. $keyword_range = !empty($keyword_start_date_start) && !empty($keyword_start_date_end);
  805. if ($keyword_range == false && $keyword_start_date_start != '') {
  806. $sql .= " AND DATE_FORMAT(ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start' ";
  807. }
  808. if ($keyword_range && $keyword_start_date_start != '' && $keyword_start_date_end != '') {
  809. $sql .= " AND DATE_FORMAT(ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start'
  810. AND DATE_FORMAT(ticket.start_date,'%d/%m/%Y') <= '$keyword_start_date_end'";
  811. }
  812. if ($keyword_course != '') {
  813. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  814. $sql .= " AND ticket.course_id IN (
  815. SELECT id FROM $course_table
  816. WHERE (
  817. title LIKE '%$keyword_course%' OR
  818. code LIKE '%$keyword_course%' OR
  819. visual_code LIKE '%$keyword_course%'
  820. )
  821. )";
  822. }
  823. $sql .= " ORDER BY $column $direction";
  824. $sql .= " LIMIT $from, $number_of_items";
  825. $result = Database::query($sql);
  826. $tickets = [];
  827. $webPath = api_get_path(WEB_PATH);
  828. while ($row = Database::fetch_assoc($result)) {
  829. $userInfo = api_get_user_info($row['sys_insert_user_id']);
  830. $hrefUser = $webPath.'main/admin/user_information.php?user_id='.$userInfo['user_id'];
  831. $name = "<a href='$hrefUser'> {$userInfo['complete_name_with_username']} </a>";
  832. if ($row['assigned_last_user'] != 0) {
  833. $assignedUserInfo = api_get_user_info($row['assigned_last_user']);
  834. if (!empty($assignedUserInfo)) {
  835. $hrefResp = $webPath.'main/admin/user_information.php?user_id='.$assignedUserInfo['user_id'];
  836. $row['assigned_last_user'] = "<a href='$hrefResp'> {$assignedUserInfo['complete_name_with_username']} </a>";
  837. } else {
  838. $row['assigned_last_user'] = get_lang('UnknownUser');
  839. }
  840. } else {
  841. if ($row['status_id'] !== self::STATUS_FORWARDED) {
  842. $row['assigned_last_user'] = '<span style="color:#ff0000;">'.get_lang('ToBeAssigned').'</span>';
  843. } else {
  844. $row['assigned_last_user'] = '<span style="color:#00ff00;">'.get_lang('MessageResent').'</span>';
  845. }
  846. }
  847. switch ($row['source']) {
  848. case self::SOURCE_PRESENTIAL:
  849. $img_source = 'icons/32/user.png';
  850. break;
  851. case self::SOURCE_EMAIL:
  852. $img_source = 'icons/32/mail.png';
  853. break;
  854. case self::SOURCE_PHONE:
  855. $img_source = 'icons/32/event.png';
  856. break;
  857. default:
  858. $img_source = 'icons/32/ticket.png';
  859. break;
  860. }
  861. $row['start_date'] = Display::dateToStringAgoAndLongDate($row['start_date']);
  862. $row['sys_lastedit_datetime'] = Display::dateToStringAgoAndLongDate($row['sys_lastedit_datetime']);
  863. $icon = Display::return_icon(
  864. $img_source,
  865. get_lang('Info'),
  866. ['style' => 'margin-right: 10px; float: left;']
  867. );
  868. $icon .= '<a href="ticket_details.php?ticket_id='.$row['id'].'">'.$row['code'].'</a>';
  869. if ($isAdmin) {
  870. $ticket = [
  871. $icon.' '.$row['subject'],
  872. $row['status_name'],
  873. $row['start_date'],
  874. $row['sys_lastedit_datetime'],
  875. $row['category_name'],
  876. $name,
  877. $row['assigned_last_user'],
  878. $row['total_messages'],
  879. ];
  880. } else {
  881. $ticket = [
  882. $icon.' '.$row['subject'],
  883. $row['status_name'],
  884. $row['start_date'],
  885. $row['sys_lastedit_datetime'],
  886. $row['category_name'],
  887. ];
  888. }
  889. if ($isAdmin) {
  890. $ticket['0'] .= '&nbsp;&nbsp;<a href="javascript:void(0)" onclick="load_history_ticket(\'div_'.$row['ticket_id'].'\','.$row['ticket_id'].')">
  891. <img onclick="load_course_list(\'div_'.$row['ticket_id'].'\','.$row['ticket_id'].')" onmouseover="clear_course_list (\'div_'.$row['ticket_id'].'\')" src="'.Display::returnIconPath('history.gif').'" title="'.get_lang('Historial').'" alt="'.get_lang('Historial').'"/>
  892. <div class="blackboard_hide" id="div_'.$row['ticket_id'].'">&nbsp;&nbsp;</div>
  893. </a>&nbsp;&nbsp;';
  894. }
  895. $tickets[] = $ticket;
  896. }
  897. return $tickets;
  898. }
  899. /**
  900. * @param int $userId
  901. *
  902. * @return int
  903. */
  904. public static function get_total_tickets_by_user_id($userId = 0)
  905. {
  906. $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
  907. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  908. $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
  909. $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
  910. $userInfo = api_get_user_info();
  911. if (empty($userInfo)) {
  912. return 0;
  913. }
  914. $userId = $userInfo['id'];
  915. if (!isset($_GET['project_id'])) {
  916. return 0;
  917. }
  918. $sql = "SELECT COUNT(ticket.id) AS total
  919. FROM $table_support_tickets ticket
  920. INNER JOIN $table_support_category cat
  921. ON (cat.id = ticket.category_id)
  922. INNER JOIN $table_support_priority priority
  923. ON (ticket.priority_id = priority.id)
  924. INNER JOIN $table_support_status status
  925. ON (ticket.status_id = status.id)
  926. WHERE 1 = 1";
  927. $projectId = (int) $_GET['project_id'];
  928. $allowRoleList = self::getAllowedRolesFromProject($projectId);
  929. // Check if a role was set to the project
  930. if (!empty($allowRoleList) && is_array($allowRoleList)) {
  931. if (!in_array($userInfo['status'], $allowRoleList)) {
  932. $sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
  933. }
  934. } else {
  935. if (!api_is_platform_admin()) {
  936. $sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
  937. }
  938. }
  939. // Search simple
  940. if (isset($_GET['submit_simple'])) {
  941. if ($_GET['keyword'] != '') {
  942. $keyword = Database::escape_string(trim($_GET['keyword']));
  943. $sql .= " AND (
  944. ticket.code LIKE '%$keyword%' OR
  945. ticket.subject LIKE '%$keyword%' OR
  946. ticket.message LIKE '%$keyword%' OR
  947. ticket.keyword LIKE '%$keyword%' OR
  948. ticket.personal_email LIKE '%$keyword%' OR
  949. ticket.source LIKE '%$keyword%'
  950. )";
  951. }
  952. }
  953. $keywords = [
  954. 'project_id' => 'ticket.project_id',
  955. 'keyword_category' => 'ticket.category_id',
  956. 'keyword_assigned_to' => 'ticket.assigned_last_user',
  957. 'keyword_source' => 'ticket.source',
  958. 'keyword_status' => 'ticket.status_id',
  959. 'keyword_priority' => 'ticket.priority_id',
  960. ];
  961. foreach ($keywords as $keyword => $sqlLabel) {
  962. if (isset($_GET[$keyword])) {
  963. $data = Database::escape_string(trim($_GET[$keyword]));
  964. $sql .= " AND $sqlLabel = '$data' ";
  965. }
  966. }
  967. // Search advanced
  968. $keyword_start_date_start = isset($_GET['keyword_start_date_start']) ? Database::escape_string(trim($_GET['keyword_start_date_start'])) : '';
  969. $keyword_start_date_end = isset($_GET['keyword_start_date_end']) ? Database::escape_string(trim($_GET['keyword_start_date_end'])) : '';
  970. $keyword_range = isset($_GET['keyword_dates']) ? Database::escape_string(trim($_GET['keyword_dates'])) : '';
  971. $keyword_course = isset($_GET['keyword_course']) ? Database::escape_string(trim($_GET['keyword_course'])) : '';
  972. if ($keyword_range == false && $keyword_start_date_start != '') {
  973. $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') = '$keyword_start_date_start' ";
  974. }
  975. if ($keyword_range && $keyword_start_date_start != '' && $keyword_start_date_end != '') {
  976. $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start'
  977. AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') <= '$keyword_start_date_end'";
  978. }
  979. if ($keyword_course != '') {
  980. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  981. $sql .= " AND ticket.course_id IN (
  982. SELECT id
  983. FROM $course_table
  984. WHERE (
  985. title LIKE '%$keyword_course%' OR
  986. code LIKE '%$keyword_course%' OR
  987. visual_code LIKE '%$keyword_course%'
  988. )
  989. ) ";
  990. }
  991. $res = Database::query($sql);
  992. $obj = Database::fetch_object($res);
  993. return (int) $obj->total;
  994. }
  995. /**
  996. * @param int $id
  997. *
  998. * @return MessageAttachment
  999. */
  1000. public static function getTicketMessageAttachment($id)
  1001. {
  1002. $id = (int) $id;
  1003. $em = Database::getManager();
  1004. $item = $em->getRepository('ChamiloTicketBundle:MessageAttachment')->find($id);
  1005. if ($item) {
  1006. return $item;
  1007. }
  1008. return false;
  1009. }
  1010. /**
  1011. * @param int $id
  1012. *
  1013. * @return array
  1014. */
  1015. public static function getTicketMessageAttachmentsByTicketId($id)
  1016. {
  1017. $id = (int) $id;
  1018. $em = Database::getManager();
  1019. $items = $em->getRepository('ChamiloTicketBundle:MessageAttachment')->findBy(['ticket' => $id]);
  1020. if ($items) {
  1021. return $items;
  1022. }
  1023. return false;
  1024. }
  1025. /**
  1026. * @param int $ticketId
  1027. *
  1028. * @return array
  1029. */
  1030. public static function get_ticket_detail_by_id($ticketId)
  1031. {
  1032. $ticketId = intval($ticketId);
  1033. $table_support_category = Database::get_main_table(TABLE_TICKET_CATEGORY);
  1034. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  1035. $table_support_priority = Database::get_main_table(TABLE_TICKET_PRIORITY);
  1036. $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
  1037. $table_support_messages = Database::get_main_table(TABLE_TICKET_MESSAGE);
  1038. $table_support_message_attachments = Database::get_main_table(TABLE_TICKET_MESSAGE_ATTACHMENTS);
  1039. $table_main_user = Database::get_main_table(TABLE_MAIN_USER);
  1040. $sql = "SELECT
  1041. ticket.*,
  1042. cat.name,
  1043. status.name as status,
  1044. priority.name priority
  1045. FROM $table_support_tickets ticket
  1046. INNER JOIN $table_support_category cat
  1047. ON (cat.id = ticket.category_id)
  1048. INNER JOIN $table_support_priority priority
  1049. ON (priority.id = ticket.priority_id)
  1050. INNER JOIN $table_support_status status
  1051. ON (status.id = ticket.status_id)
  1052. WHERE
  1053. ticket.id = $ticketId ";
  1054. $result = Database::query($sql);
  1055. $ticket = [];
  1056. if (Database::num_rows($result) > 0) {
  1057. while ($row = Database::fetch_assoc($result)) {
  1058. $row['course'] = null;
  1059. $row['start_date_from_db'] = $row['start_date'];
  1060. $row['start_date'] = api_convert_and_format_date(
  1061. api_get_local_time($row['start_date']),
  1062. DATE_TIME_FORMAT_LONG,
  1063. api_get_timezone()
  1064. );
  1065. $row['end_date_from_db'] = $row['end_date'];
  1066. $row['end_date'] = api_convert_and_format_date(
  1067. api_get_local_time($row['end_date']),
  1068. DATE_TIME_FORMAT_LONG,
  1069. api_get_timezone()
  1070. );
  1071. $row['sys_lastedit_datetime_from_db'] = $row['sys_lastedit_datetime'];
  1072. $row['sys_lastedit_datetime'] = api_convert_and_format_date(
  1073. api_get_local_time($row['sys_lastedit_datetime']),
  1074. DATE_TIME_FORMAT_LONG,
  1075. api_get_timezone()
  1076. );
  1077. $row['course_url'] = null;
  1078. if ($row['course_id'] != 0) {
  1079. $course = api_get_course_info_by_id($row['course_id']);
  1080. $sessionId = 0;
  1081. if ($row['session_id']) {
  1082. $sessionId = $row['session_id'];
  1083. }
  1084. if ($course) {
  1085. $row['course_url'] = '<a href="'.$course['course_public_url'].'?id_session='.$sessionId.'">'.$course['name'].'</a>';
  1086. }
  1087. }
  1088. $userInfo = api_get_user_info($row['sys_insert_user_id']);
  1089. $row['user_url'] = '<a href="'.api_get_path(WEB_PATH).'main/admin/user_information.php?user_id='.$userInfo['user_id'].'">
  1090. '.$userInfo['complete_name'].'</a>';
  1091. $ticket['usuario'] = $userInfo;
  1092. $ticket['ticket'] = $row;
  1093. }
  1094. $sql = "SELECT *, message.id as message_id
  1095. FROM $table_support_messages message
  1096. INNER JOIN $table_main_user user
  1097. ON (message.sys_insert_user_id = user.user_id)
  1098. WHERE
  1099. message.ticket_id = '$ticketId' ";
  1100. $result = Database::query($sql);
  1101. $ticket['messages'] = [];
  1102. $attach_icon = Display::return_icon('attachment.gif', '');
  1103. $webPath = api_get_path(WEB_CODE_PATH);
  1104. while ($row = Database::fetch_assoc($result)) {
  1105. $message = $row;
  1106. $message['admin'] = UserManager::is_admin($message['user_id']);
  1107. $message['user_info'] = api_get_user_info($message['user_id']);
  1108. $sql = "SELECT *
  1109. FROM $table_support_message_attachments
  1110. WHERE
  1111. message_id = ".$row['message_id']." AND
  1112. ticket_id = $ticketId";
  1113. $result_attach = Database::query($sql);
  1114. while ($row2 = Database::fetch_assoc($result_attach)) {
  1115. $archiveURL = $webPath.'ticket/download.php?ticket_id='.$ticketId.'&id='.$row2['id'];
  1116. $row2['attachment_link'] = $attach_icon.'&nbsp;<a href="'.$archiveURL.'">'.$row2['filename'].'</a>&nbsp;('.$row2['size'].')';
  1117. $message['attachments'][] = $row2;
  1118. }
  1119. $ticket['messages'][] = $message;
  1120. }
  1121. }
  1122. return $ticket;
  1123. }
  1124. /**
  1125. * @param int $ticketId
  1126. * @param int $userId
  1127. *
  1128. * @return bool
  1129. */
  1130. public static function update_message_status($ticketId, $userId)
  1131. {
  1132. $ticketId = intval($ticketId);
  1133. $userId = intval($userId);
  1134. $table_support_messages = Database::get_main_table(
  1135. TABLE_TICKET_MESSAGE
  1136. );
  1137. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  1138. $now = api_get_utc_datetime();
  1139. $sql = "UPDATE $table_support_messages
  1140. SET
  1141. status = 'LEI',
  1142. sys_lastedit_user_id ='".api_get_user_id()."',
  1143. sys_lastedit_datetime ='".$now."'
  1144. WHERE ticket_id ='$ticketId' ";
  1145. if (api_is_platform_admin()) {
  1146. $sql .= " AND sys_insert_user_id = '$userId'";
  1147. } else {
  1148. $sql .= " AND sys_insert_user_id != '$userId'";
  1149. }
  1150. $result = Database::query($sql);
  1151. if (Database::affected_rows($result) > 0) {
  1152. Database::query(
  1153. "UPDATE $table_support_tickets SET
  1154. status_id = '".self::STATUS_PENDING."'
  1155. WHERE id ='$ticketId' AND status_id = '".self::STATUS_NEW."'"
  1156. );
  1157. return true;
  1158. } else {
  1159. return false;
  1160. }
  1161. }
  1162. /**
  1163. * Send notification to a user through the internal messaging system.
  1164. *
  1165. * @param int $ticketId
  1166. * @param string $title
  1167. * @param string $message
  1168. * @param int $onlyToUserId
  1169. *
  1170. * @return bool
  1171. */
  1172. public static function sendNotification($ticketId, $title, $message, $onlyToUserId = 0)
  1173. {
  1174. $ticketInfo = self::get_ticket_detail_by_id($ticketId);
  1175. if (empty($ticketInfo)) {
  1176. return false;
  1177. }
  1178. $assignedUserInfo = api_get_user_info($ticketInfo['ticket']['assigned_last_user']);
  1179. $requestUserInfo = $ticketInfo['usuario'];
  1180. $ticketCode = $ticketInfo['ticket']['code'];
  1181. $status = $ticketInfo['ticket']['status'];
  1182. $priority = $ticketInfo['ticket']['priority'];
  1183. // Subject
  1184. $titleEmail = "[$ticketCode] $title";
  1185. // Content
  1186. $href = api_get_path(WEB_CODE_PATH).'/ticket/ticket_details.php?ticket_id='.$ticketId;
  1187. $ticketUrl = Display::url($ticketCode, $href);
  1188. $messageEmail = get_lang('TicketNum').": $ticketUrl <br />";
  1189. $messageEmail .= get_lang('Status').": $status <br />";
  1190. $messageEmail .= get_lang('Priority').": $priority <br />";
  1191. $messageEmail .= '<hr /><br />';
  1192. $messageEmail .= $message;
  1193. $currentUserId = api_get_user_id();
  1194. $attachmentList = [];
  1195. $attachments = self::getTicketMessageAttachmentsByTicketId($ticketId);
  1196. if (!empty($attachments)) {
  1197. /** @var MessageAttachment $attachment */
  1198. foreach ($attachments as $attachment) {
  1199. $file = api_get_uploaded_file(
  1200. 'ticket_attachment',
  1201. $ticketId,
  1202. $attachment->getPath()
  1203. );
  1204. if (!empty($file)) {
  1205. $attachmentList[] = [
  1206. 'tmp_name' => api_get_uploaded_file(
  1207. 'ticket_attachment',
  1208. $ticketId,
  1209. $attachment->getPath()
  1210. ),
  1211. 'size' => $attachment->getSize(),
  1212. 'name' => $attachment->getFilename(),
  1213. 'error' => 0,
  1214. ];
  1215. }
  1216. }
  1217. }
  1218. if (!empty($onlyToUserId)) {
  1219. // Send only to specific user
  1220. if ($currentUserId != $onlyToUserId) {
  1221. MessageManager::send_message_simple(
  1222. $onlyToUserId,
  1223. $titleEmail,
  1224. $messageEmail,
  1225. 0,
  1226. false,
  1227. false,
  1228. [],
  1229. false,
  1230. $attachmentList
  1231. );
  1232. }
  1233. } else {
  1234. // Send to assigned user and to author
  1235. if ($requestUserInfo && $currentUserId != $requestUserInfo['id']) {
  1236. MessageManager::send_message_simple(
  1237. $requestUserInfo['id'],
  1238. $titleEmail,
  1239. $messageEmail,
  1240. 0,
  1241. false,
  1242. false,
  1243. [],
  1244. false,
  1245. $attachmentList
  1246. );
  1247. }
  1248. if ($assignedUserInfo &&
  1249. $requestUserInfo['id'] != $assignedUserInfo['id'] &&
  1250. $currentUserId != $assignedUserInfo['id']
  1251. ) {
  1252. MessageManager::send_message_simple(
  1253. $assignedUserInfo['id'],
  1254. $titleEmail,
  1255. $messageEmail,
  1256. 0,
  1257. false,
  1258. false,
  1259. [],
  1260. false,
  1261. $attachmentList
  1262. );
  1263. }
  1264. }
  1265. }
  1266. /**
  1267. * @param array $params
  1268. * @param int $ticketId
  1269. * @param int $userId
  1270. *
  1271. * @return bool
  1272. */
  1273. public static function updateTicket(
  1274. $params,
  1275. $ticketId,
  1276. $userId
  1277. ) {
  1278. $now = api_get_utc_datetime();
  1279. $table = Database::get_main_table(TABLE_TICKET_TICKET);
  1280. $newParams = [
  1281. 'priority_id' => isset($params['priority_id']) ? $params['priority_id'] : '',
  1282. 'status_id' => isset($params['status_id']) ? $params['status_id'] : '',
  1283. 'sys_lastedit_user_id' => $userId,
  1284. 'sys_lastedit_datetime' => $now,
  1285. ];
  1286. Database::update($table, $newParams, ['id = ? ' => $ticketId]);
  1287. return true;
  1288. }
  1289. /**
  1290. * @param int $status_id
  1291. * @param int $ticketId
  1292. * @param int $userId
  1293. *
  1294. * @return bool
  1295. */
  1296. public static function update_ticket_status(
  1297. $status_id,
  1298. $ticketId,
  1299. $userId
  1300. ) {
  1301. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  1302. $ticketId = intval($ticketId);
  1303. $status_id = intval($status_id);
  1304. $userId = intval($userId);
  1305. $now = api_get_utc_datetime();
  1306. $sql = "UPDATE $table_support_tickets
  1307. SET
  1308. status_id = '$status_id',
  1309. sys_lastedit_user_id ='$userId',
  1310. sys_lastedit_datetime ='".$now."'
  1311. WHERE id ='$ticketId'";
  1312. $result = Database::query($sql);
  1313. if (Database::affected_rows($result) > 0) {
  1314. self::sendNotification(
  1315. $ticketId,
  1316. get_lang('TicketUpdated'),
  1317. get_lang('TicketUpdated')
  1318. );
  1319. return true;
  1320. } else {
  1321. return false;
  1322. }
  1323. }
  1324. /**
  1325. * @return mixed
  1326. */
  1327. public static function getNumberOfMessages()
  1328. {
  1329. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  1330. $table_support_messages = Database::get_main_table(
  1331. TABLE_TICKET_MESSAGE
  1332. );
  1333. $table_main_user = Database::get_main_table(TABLE_MAIN_USER);
  1334. $table_main_admin = Database::get_main_table(TABLE_MAIN_ADMIN);
  1335. $user_info = api_get_user_info();
  1336. $userId = $user_info['user_id'];
  1337. $sql = "SELECT COUNT(DISTINCT ticket.id) AS unread
  1338. FROM $table_support_tickets ticket,
  1339. $table_support_messages message ,
  1340. $table_main_user user
  1341. WHERE
  1342. ticket.id = message.ticket_id AND
  1343. message.status = 'NOL' AND
  1344. user.user_id = message.sys_insert_user_id ";
  1345. if (!api_is_platform_admin()) {
  1346. $sql .= " AND ticket.request_user = '$userId'
  1347. AND user_id IN (SELECT user_id FROM $table_main_admin) ";
  1348. } else {
  1349. $sql .= " AND user_id NOT IN (SELECT user_id FROM $table_main_admin)
  1350. AND ticket.status_id != '".self::STATUS_FORWARDED."'";
  1351. }
  1352. $sql .= " AND ticket.project_id != '' ";
  1353. $res = Database::query($sql);
  1354. $obj = Database::fetch_object($res);
  1355. return $obj->unread;
  1356. }
  1357. /**
  1358. * @param int $ticketId
  1359. * @param int $userId
  1360. */
  1361. public static function send_alert($ticketId, $userId)
  1362. {
  1363. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  1364. $now = api_get_utc_datetime();
  1365. $ticketId = intval($ticketId);
  1366. $userId = intval($userId);
  1367. $sql = "UPDATE $table_support_tickets SET
  1368. priority_id = '".self::PRIORITY_HIGH."',
  1369. sys_lastedit_user_id ='$userId',
  1370. sys_lastedit_datetime ='$now'
  1371. WHERE id = '$ticketId'";
  1372. Database::query($sql);
  1373. }
  1374. /**
  1375. * @param int $ticketId
  1376. * @param int $userId
  1377. */
  1378. public static function close_ticket($ticketId, $userId)
  1379. {
  1380. $ticketId = intval($ticketId);
  1381. $userId = intval($userId);
  1382. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  1383. $now = api_get_utc_datetime();
  1384. $sql = "UPDATE $table_support_tickets SET
  1385. status_id = '".self::STATUS_CLOSE."',
  1386. sys_lastedit_user_id ='$userId',
  1387. sys_lastedit_datetime ='".$now."',
  1388. end_date ='$now'
  1389. WHERE id ='$ticketId'";
  1390. Database::query($sql);
  1391. self::sendNotification(
  1392. $ticketId,
  1393. get_lang('TicketClosed'),
  1394. get_lang('TicketClosed')
  1395. );
  1396. }
  1397. /**
  1398. * Close old tickets
  1399. */
  1400. public static function close_old_tickets()
  1401. {
  1402. $table = Database::get_main_table(TABLE_TICKET_TICKET);
  1403. $now = api_get_utc_datetime();
  1404. $userId = api_get_user_id();
  1405. $sql = "UPDATE $table
  1406. SET
  1407. status_id = '".self::STATUS_CLOSE."',
  1408. sys_lastedit_user_id ='$userId',
  1409. sys_lastedit_datetime ='$now',
  1410. end_date = '$now'
  1411. WHERE
  1412. DATEDIFF('$now', sys_lastedit_datetime) > 7 AND
  1413. status_id != '".self::STATUS_CLOSE."' AND
  1414. status_id != '".self::STATUS_NEW."' AND
  1415. status_id != '".self::STATUS_FORWARDED."'";
  1416. Database::query($sql);
  1417. }
  1418. /**
  1419. * @param int $ticketId
  1420. *
  1421. * @return array
  1422. */
  1423. public static function get_assign_log($ticketId)
  1424. {
  1425. $table = Database::get_main_table(TABLE_TICKET_ASSIGNED_LOG);
  1426. $ticketId = intval($ticketId);
  1427. $sql = "SELECT * FROM $table
  1428. WHERE ticket_id = $ticketId
  1429. ORDER BY assigned_date DESC";
  1430. $result = Database::query($sql);
  1431. $history = [];
  1432. $webpath = api_get_path(WEB_PATH);
  1433. while ($row = Database::fetch_assoc($result)) {
  1434. if ($row['user_id'] != 0) {
  1435. $assignuser = api_get_user_info($row['user_id']);
  1436. $row['assignuser'] = '<a href="'.$webpath.'main/admin/user_information.php?user_id='.$row['user_id'].'" target="_blank">'.
  1437. $assignuser['username'].'</a>';
  1438. } else {
  1439. $row['assignuser'] = get_lang('Unassign');
  1440. }
  1441. $row['assigned_date'] = Display::dateToStringAgoAndLongDate($row['assigned_date']);
  1442. $insertuser = api_get_user_info($row['sys_insert_user_id']);
  1443. $row['insertuser'] = '<a href="'.$webpath.'main/admin/user_information.php?user_id='.$row['sys_insert_user_id'].'" target="_blank">'.
  1444. $insertuser['username'].'</a>';
  1445. $history[] = $row;
  1446. }
  1447. return $history;
  1448. }
  1449. /**
  1450. * @param $from
  1451. * @param $number_of_items
  1452. * @param $column
  1453. * @param $direction
  1454. * @param null $userId
  1455. *
  1456. * @return array
  1457. */
  1458. public static function export_tickets_by_user_id(
  1459. $from,
  1460. $number_of_items,
  1461. $column,
  1462. $direction,
  1463. $userId = null
  1464. ) {
  1465. $from = intval($from);
  1466. $number_of_items = intval($number_of_items);
  1467. $table_support_category = Database::get_main_table(
  1468. TABLE_TICKET_CATEGORY
  1469. );
  1470. $table_support_tickets = Database::get_main_table(TABLE_TICKET_TICKET);
  1471. $table_support_priority = Database::get_main_table(
  1472. TABLE_TICKET_PRIORITY
  1473. );
  1474. $table_support_status = Database::get_main_table(TABLE_TICKET_STATUS);
  1475. $table_support_messages = Database::get_main_table(
  1476. TABLE_TICKET_MESSAGE
  1477. );
  1478. $table_main_user = Database::get_main_table(TABLE_MAIN_USER);
  1479. if (is_null($direction)) {
  1480. $direction = "DESC";
  1481. }
  1482. if (is_null($userId) || $userId == 0) {
  1483. $userId = api_get_user_id();
  1484. }
  1485. $sql = "SELECT
  1486. ticket.code,
  1487. ticket.sys_insert_datetime,
  1488. ticket.sys_lastedit_datetime,
  1489. cat.name as category,
  1490. CONCAT(user.lastname,' ', user.firstname) AS fullname,
  1491. status.name as status,
  1492. ticket.total_messages as messages,
  1493. ticket.assigned_last_user as responsable
  1494. FROM $table_support_tickets ticket,
  1495. $table_support_category cat ,
  1496. $table_support_priority priority,
  1497. $table_support_status status ,
  1498. $table_main_user user
  1499. WHERE
  1500. cat.id = ticket.category_id
  1501. AND ticket.priority_id = priority.id
  1502. AND ticket.status_id = status.id
  1503. AND user.user_id = ticket.request_user ";
  1504. // Search simple
  1505. if (isset($_GET['submit_simple'])) {
  1506. if ($_GET['keyword'] !== '') {
  1507. $keyword = Database::escape_string(trim($_GET['keyword']));
  1508. $sql .= " AND (ticket.code = '$keyword'
  1509. OR user.firstname LIKE '%$keyword%'
  1510. OR user.lastname LIKE '%$keyword%'
  1511. OR concat(user.firstname,' ',user.lastname) LIKE '%$keyword%'
  1512. OR concat(user.lastname,' ',user.firstname) LIKE '%$keyword%'
  1513. OR user.username LIKE '%$keyword%') ";
  1514. }
  1515. }
  1516. // Search advanced
  1517. if (isset($_GET['submit_advanced'])) {
  1518. $keyword_category = Database::escape_string(
  1519. trim($_GET['keyword_category'])
  1520. );
  1521. $keyword_request_user = Database::escape_string(
  1522. trim($_GET['keyword_request_user'])
  1523. );
  1524. $keywordAssignedTo = (int) $_GET['keyword_assigned_to'];
  1525. $keyword_start_date_start = Database::escape_string(
  1526. trim($_GET['keyword_start_date_start'])
  1527. );
  1528. $keyword_start_date_end = Database::escape_string(
  1529. trim($_GET['keyword_start_date_end'])
  1530. );
  1531. $keyword_status = Database::escape_string(
  1532. trim($_GET['keyword_status'])
  1533. );
  1534. $keyword_source = Database::escape_string(
  1535. trim($_GET['keyword_source'])
  1536. );
  1537. $keyword_priority = Database::escape_string(
  1538. trim($_GET['keyword_priority'])
  1539. );
  1540. $keyword_range = Database::escape_string(
  1541. trim($_GET['keyword_dates'])
  1542. );
  1543. $keyword_unread = Database::escape_string(
  1544. trim($_GET['keyword_unread'])
  1545. );
  1546. $keyword_course = Database::escape_string(
  1547. trim($_GET['keyword_course'])
  1548. );
  1549. if ($keyword_category != '') {
  1550. $sql .= " AND ticket.category_id = '$keyword_category' ";
  1551. }
  1552. if ($keyword_request_user != '') {
  1553. $sql .= " AND (ticket.request_user = '$keyword_request_user'
  1554. OR user.firstname LIKE '%$keyword_request_user%'
  1555. OR user.official_code LIKE '%$keyword_request_user%'
  1556. OR user.lastname LIKE '%$keyword_request_user%'
  1557. OR concat(user.firstname,' ',user.lastname) LIKE '%$keyword_request_user%'
  1558. OR concat(user.lastname,' ',user.firstname) LIKE '%$keyword_request_user%'
  1559. OR user.username LIKE '%$keyword_request_user%') ";
  1560. }
  1561. if (!empty($keywordAssignedTo)) {
  1562. $sql .= " AND ticket.assigned_last_user = $keywordAssignedTo ";
  1563. }
  1564. if ($keyword_status != '') {
  1565. $sql .= " AND ticket.status_id = '$keyword_status' ";
  1566. }
  1567. if ($keyword_range == '' && $keyword_start_date_start != '') {
  1568. $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') = '$keyword_start_date_start' ";
  1569. }
  1570. if ($keyword_range == '1' && $keyword_start_date_start != '' && $keyword_start_date_end != '') {
  1571. $sql .= " AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') >= '$keyword_start_date_start'
  1572. AND DATE_FORMAT( ticket.start_date,'%d/%m/%Y') <= '$keyword_start_date_end'";
  1573. }
  1574. if ($keyword_priority != '') {
  1575. $sql .= " AND ticket.priority_id = '$keyword_priority' ";
  1576. }
  1577. if ($keyword_source != '') {
  1578. $sql .= " AND ticket.source = '$keyword_source' ";
  1579. }
  1580. if ($keyword_priority != '') {
  1581. $sql .= " AND ticket.priority_id = '$keyword_priority' ";
  1582. }
  1583. if ($keyword_course != '') {
  1584. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  1585. $sql .= " AND ticket.course_id IN ( ";
  1586. $sql .= "SELECT id
  1587. FROM $course_table
  1588. WHERE (title LIKE '%$keyword_course%'
  1589. OR code LIKE '%$keyword_course%'
  1590. OR visual_code LIKE '%$keyword_course%' )) ";
  1591. }
  1592. if ($keyword_unread == 'yes') {
  1593. $sql .= " AND ticket.id IN (
  1594. SELECT ticket.id
  1595. FROM $table_support_tickets ticket,
  1596. $table_support_messages message,
  1597. $table_main_user user
  1598. WHERE ticket.id = message.ticket_id
  1599. AND message.status = 'NOL'
  1600. AND message.sys_insert_user_id = user.user_id
  1601. AND user.status != 1 AND ticket.status_id != '".self::STATUS_FORWARDED."'
  1602. GROUP BY ticket.id)";
  1603. } else {
  1604. if ($keyword_unread == 'no') {
  1605. $sql .= " AND ticket.id NOT IN (
  1606. SELECT ticket.id
  1607. FROM $table_support_tickets ticket,
  1608. $table_support_messages message,
  1609. $table_main_user user
  1610. WHERE ticket.id = message.ticket_id
  1611. AND message.status = 'NOL'
  1612. AND message.sys_insert_user_id = user.user_id
  1613. AND user.status != 1
  1614. AND ticket.status_id != '".self::STATUS_FORWARDED."'
  1615. GROUP BY ticket.id)";
  1616. }
  1617. }
  1618. }
  1619. $sql .= " LIMIT $from,$number_of_items";
  1620. $result = Database::query($sql);
  1621. $tickets[0] = [
  1622. utf8_decode('Ticket#'),
  1623. utf8_decode('Fecha'),
  1624. utf8_decode('Fecha Edicion'),
  1625. utf8_decode('Categoria'),
  1626. utf8_decode('Usuario'),
  1627. utf8_decode('Estado'),
  1628. utf8_decode('Mensajes'),
  1629. utf8_decode('Responsable'),
  1630. utf8_decode('Programa'),
  1631. ];
  1632. while ($row = Database::fetch_assoc($result)) {
  1633. if ($row['responsable'] != 0) {
  1634. $row['responsable'] = api_get_user_info($row['responsable']);
  1635. $row['responsable'] = $row['responsable']['firstname'].' '.$row['responsable']['lastname'];
  1636. }
  1637. $row['sys_insert_datetime'] = api_format_date(
  1638. $row['sys_insert_datetime'],
  1639. '%d/%m/%y - %I:%M:%S %p'
  1640. );
  1641. $row['sys_lastedit_datetime'] = api_format_date(
  1642. $row['sys_lastedit_datetime'],
  1643. '%d/%m/%y - %I:%M:%S %p'
  1644. );
  1645. $row['category'] = utf8_decode($row['category']);
  1646. $row['programa'] = utf8_decode($row['fullname']);
  1647. $row['fullname'] = utf8_decode($row['fullname']);
  1648. $row['responsable'] = utf8_decode($row['responsable']);
  1649. $tickets[] = $row;
  1650. }
  1651. return $tickets;
  1652. }
  1653. /**
  1654. * @param string $url
  1655. * @param int $projectId
  1656. *
  1657. * @return FormValidator
  1658. */
  1659. public static function getCategoryForm($url, $projectId)
  1660. {
  1661. $form = new FormValidator('category', 'post', $url);
  1662. $form->addText('name', get_lang('Name'));
  1663. $form->addHtmlEditor('description', get_lang('Description'));
  1664. $form->addHidden('project_id', $projectId);
  1665. $form->addButtonUpdate(get_lang('Save'));
  1666. return $form;
  1667. }
  1668. /**
  1669. * @return array
  1670. */
  1671. public static function getStatusList()
  1672. {
  1673. $items = Database::getManager()->getRepository('ChamiloTicketBundle:Status')->findAll();
  1674. $list = [];
  1675. /** @var Status $row */
  1676. foreach ($items as $row) {
  1677. $list[$row->getId()] = $row->getName();
  1678. }
  1679. return $list;
  1680. }
  1681. /**
  1682. * @return array
  1683. */
  1684. public static function getTicketsFromCriteria($criteria)
  1685. {
  1686. $items = Database::getManager()->getRepository('ChamiloTicketBundle:Ticket')->findBy($criteria);
  1687. $list = [];
  1688. /** @var Ticket $row */
  1689. foreach ($items as $row) {
  1690. $list[$row->getId()] = $row->getCode();
  1691. }
  1692. return $list;
  1693. }
  1694. /**
  1695. * @param string $code
  1696. *
  1697. * @return int
  1698. */
  1699. public static function getStatusIdFromCode($code)
  1700. {
  1701. $item = Database::getManager()
  1702. ->getRepository('ChamiloTicketBundle:Status')
  1703. ->findOneBy(['code' => $code])
  1704. ;
  1705. if ($item) {
  1706. return $item->getId();
  1707. }
  1708. return 0;
  1709. }
  1710. /**
  1711. * @return array
  1712. */
  1713. public static function getPriorityList()
  1714. {
  1715. $projects = Database::getManager()->getRepository('ChamiloTicketBundle:Priority')->findAll();
  1716. $list = [];
  1717. /** @var Priority $row */
  1718. foreach ($projects as $row) {
  1719. $list[$row->getId()] = $row->getName();
  1720. }
  1721. return $list;
  1722. }
  1723. /**
  1724. * @return array
  1725. */
  1726. public static function getProjects()
  1727. {
  1728. $projects = Database::getManager()->getRepository('ChamiloTicketBundle:Project')->findAll();
  1729. $list = [];
  1730. /** @var Project $row */
  1731. foreach ($projects as $row) {
  1732. $list[] = [
  1733. 'id' => $row->getId(),
  1734. '0' => $row->getId(),
  1735. '1' => $row->getName(),
  1736. '2' => $row->getDescription(),
  1737. '3' => $row->getId(),
  1738. ];
  1739. }
  1740. return $list;
  1741. }
  1742. /**
  1743. * @return array
  1744. */
  1745. public static function getProjectsSimple()
  1746. {
  1747. $projects = Database::getManager()->getRepository('ChamiloTicketBundle:Project')->findAll();
  1748. $list = [];
  1749. /** @var Project $row */
  1750. foreach ($projects as $row) {
  1751. $list[] = [
  1752. 'id' => $row->getId(),
  1753. '0' => $row->getId(),
  1754. '1' => Display::url(
  1755. $row->getName(),
  1756. api_get_path(WEB_CODE_PATH).'ticket/tickets.php?project_id='.$row->getId()
  1757. ),
  1758. '2' => $row->getDescription(),
  1759. ];
  1760. }
  1761. return $list;
  1762. }
  1763. /**
  1764. * @return int
  1765. */
  1766. public static function getProjectsCount()
  1767. {
  1768. $count = Database::getManager()->getRepository('ChamiloTicketBundle:Project')->createQueryBuilder('p')
  1769. ->select('COUNT(p.id)')
  1770. ->getQuery()
  1771. ->getSingleScalarResult();
  1772. return $count;
  1773. }
  1774. /**
  1775. * @param array $params
  1776. */
  1777. public static function addProject($params)
  1778. {
  1779. $project = new Project();
  1780. $project->setName($params['name']);
  1781. $project->setDescription($params['description']);
  1782. $project->setInsertUserId(api_get_user_id());
  1783. Database::getManager()->persist($project);
  1784. Database::getManager()->flush();
  1785. }
  1786. /**
  1787. * @param $id
  1788. *
  1789. * @return Project
  1790. */
  1791. public static function getProject($id)
  1792. {
  1793. return Database::getManager()->getRepository('ChamiloTicketBundle:Project')->find($id);
  1794. }
  1795. /**
  1796. * @param int $id
  1797. * @param array $params
  1798. */
  1799. public static function updateProject($id, $params)
  1800. {
  1801. $project = self::getProject($id);
  1802. $project->setName($params['name']);
  1803. $project->setDescription($params['description']);
  1804. $project->setLastEditDateTime(new DateTime($params['sys_lastedit_datetime']));
  1805. $project->setLastEditUserId($params['sys_lastedit_user_id']);
  1806. Database::getManager()->merge($project);
  1807. Database::getManager()->flush();
  1808. }
  1809. /**
  1810. * @param int $id
  1811. */
  1812. public static function deleteProject($id)
  1813. {
  1814. $project = self::getProject($id);
  1815. if ($project) {
  1816. Database::getManager()->remove($project);
  1817. Database::getManager()->flush();
  1818. }
  1819. }
  1820. /**
  1821. * @param string $url
  1822. *
  1823. * @return FormValidator
  1824. */
  1825. public static function getProjectForm($url)
  1826. {
  1827. $form = new FormValidator('project', 'post', $url);
  1828. $form->addText('name', get_lang('Name'));
  1829. $form->addHtmlEditor('description', get_lang('Description'));
  1830. $form->addButtonUpdate(get_lang('Save'));
  1831. return $form;
  1832. }
  1833. /**
  1834. * @return array
  1835. */
  1836. public static function getStatusAdminList()
  1837. {
  1838. $items = Database::getManager()->getRepository('ChamiloTicketBundle:Status')->findAll();
  1839. $list = [];
  1840. /** @var Status $row */
  1841. foreach ($items as $row) {
  1842. $list[] = [
  1843. 'id' => $row->getId(),
  1844. 'code' => $row->getCode(),
  1845. '0' => $row->getId(),
  1846. '1' => $row->getName(),
  1847. '2' => $row->getDescription(),
  1848. '3' => $row->getId(),
  1849. ];
  1850. }
  1851. return $list;
  1852. }
  1853. /**
  1854. * @return array
  1855. */
  1856. public static function getStatusSimple()
  1857. {
  1858. $projects = Database::getManager()->getRepository('ChamiloTicketBundle:Status')->findAll();
  1859. $list = [];
  1860. /** @var Project $row */
  1861. foreach ($projects as $row) {
  1862. $list[] = [
  1863. 'id' => $row->getId(),
  1864. '0' => $row->getId(),
  1865. '1' => Display::url($row->getName()),
  1866. '2' => $row->getDescription(),
  1867. ];
  1868. }
  1869. return $list;
  1870. }
  1871. /**
  1872. * @return int
  1873. */
  1874. public static function getStatusCount()
  1875. {
  1876. $count = Database::getManager()->getRepository('ChamiloTicketBundle:Status')->createQueryBuilder('p')
  1877. ->select('COUNT(p.id)')
  1878. ->getQuery()
  1879. ->getSingleScalarResult();
  1880. return $count;
  1881. }
  1882. /**
  1883. * @param array $params
  1884. */
  1885. public static function addStatus($params)
  1886. {
  1887. $item = new Status();
  1888. $item->setCode(URLify::filter($params['name']));
  1889. $item->setName($params['name']);
  1890. $item->setDescription($params['description']);
  1891. Database::getManager()->persist($item);
  1892. Database::getManager()->flush();
  1893. }
  1894. /**
  1895. * @param $id
  1896. *
  1897. * @return Project
  1898. */
  1899. public static function getStatus($id)
  1900. {
  1901. return Database::getManager()->getRepository('ChamiloTicketBundle:Status')->find($id);
  1902. }
  1903. /**
  1904. * @param int $id
  1905. * @param array $params
  1906. */
  1907. public static function updateStatus($id, $params)
  1908. {
  1909. $item = self::getStatus($id);
  1910. $item->setName($params['name']);
  1911. $item->setDescription($params['description']);
  1912. Database::getManager()->merge($item);
  1913. Database::getManager()->flush();
  1914. }
  1915. /**
  1916. * @param int $id
  1917. */
  1918. public static function deleteStatus($id)
  1919. {
  1920. $item = self::getStatus($id);
  1921. if ($item) {
  1922. Database::getManager()->remove($item);
  1923. Database::getManager()->flush();
  1924. }
  1925. }
  1926. /**
  1927. * @param string $url
  1928. *
  1929. * @return FormValidator
  1930. */
  1931. public static function getStatusForm($url)
  1932. {
  1933. $form = new FormValidator('status', 'post', $url);
  1934. $form->addText('name', get_lang('Name'));
  1935. $form->addHtmlEditor('description', get_lang('Description'));
  1936. $form->addButtonUpdate(get_lang('Save'));
  1937. return $form;
  1938. }
  1939. /**
  1940. * @return array
  1941. */
  1942. public static function getPriorityAdminList()
  1943. {
  1944. $items = Database::getManager()->getRepository('ChamiloTicketBundle:Priority')->findAll();
  1945. $list = [];
  1946. /** @var Status $row */
  1947. foreach ($items as $row) {
  1948. $list[] = [
  1949. 'id' => $row->getId(),
  1950. 'code' => $row->getCode(),
  1951. '0' => $row->getId(),
  1952. '1' => $row->getName(),
  1953. '2' => $row->getDescription(),
  1954. '3' => $row->getId(),
  1955. ];
  1956. }
  1957. return $list;
  1958. }
  1959. /**
  1960. * @return array
  1961. */
  1962. public static function getPrioritySimple()
  1963. {
  1964. $projects = Database::getManager()->getRepository('ChamiloTicketBundle:Priority')->findAll();
  1965. $list = [];
  1966. /** @var Priority $row */
  1967. foreach ($projects as $row) {
  1968. $list[] = [
  1969. 'id' => $row->getId(),
  1970. '0' => $row->getId(),
  1971. '1' => Display::url($row->getName()),
  1972. '2' => $row->getDescription(),
  1973. ];
  1974. }
  1975. return $list;
  1976. }
  1977. /**
  1978. * @return int
  1979. */
  1980. public static function getPriorityCount()
  1981. {
  1982. $count = Database::getManager()->getRepository('ChamiloTicketBundle:Priority')->createQueryBuilder('p')
  1983. ->select('COUNT(p.id)')
  1984. ->getQuery()
  1985. ->getSingleScalarResult();
  1986. return $count;
  1987. }
  1988. /**
  1989. * @param array $params
  1990. */
  1991. public static function addPriority($params)
  1992. {
  1993. $item = new Priority();
  1994. $item
  1995. ->setCode(URLify::filter($params['name']))
  1996. ->setName($params['name'])
  1997. ->setDescription($params['description'])
  1998. ->setColor('')
  1999. ->setInsertUserId(api_get_user_id())
  2000. ->setUrgency('')
  2001. ;
  2002. Database::getManager()->persist($item);
  2003. Database::getManager()->flush();
  2004. }
  2005. /**
  2006. * @param $id
  2007. *
  2008. * @return Priority
  2009. */
  2010. public static function getPriority($id)
  2011. {
  2012. return Database::getManager()->getRepository('ChamiloTicketBundle:Priority')->find($id);
  2013. }
  2014. /**
  2015. * @param int $id
  2016. * @param array $params
  2017. */
  2018. public static function updatePriority($id, $params)
  2019. {
  2020. $item = self::getPriority($id);
  2021. $item->setName($params['name']);
  2022. $item->setDescription($params['description']);
  2023. Database::getManager()->merge($item);
  2024. Database::getManager()->flush();
  2025. }
  2026. /**
  2027. * @param int $id
  2028. */
  2029. public static function deletePriority($id)
  2030. {
  2031. $item = self::getPriority($id);
  2032. if ($item) {
  2033. Database::getManager()->remove($item);
  2034. Database::getManager()->flush();
  2035. }
  2036. }
  2037. /**
  2038. * @param string $url
  2039. *
  2040. * @return FormValidator
  2041. */
  2042. public static function getPriorityForm($url)
  2043. {
  2044. $form = new FormValidator('priority', 'post', $url);
  2045. $form->addText('name', get_lang('Name'));
  2046. $form->addHtmlEditor('description', get_lang('Description'));
  2047. $form->addButtonUpdate(get_lang('Save'));
  2048. return $form;
  2049. }
  2050. /**
  2051. * Returns a list of menu elements for the tickets system's configuration.
  2052. *
  2053. * @param string $exclude The element to exclude from the list
  2054. *
  2055. * @return array
  2056. */
  2057. public static function getSettingsMenuItems($exclude = null)
  2058. {
  2059. $items = [];
  2060. $project = [
  2061. 'icon' => 'project.png',
  2062. 'url' => 'projects.php',
  2063. 'content' => get_lang('Projects'),
  2064. ];
  2065. $status = [
  2066. 'icon' => 'check-circle.png',
  2067. 'url' => 'status.php',
  2068. 'content' => get_lang('Status'),
  2069. ];
  2070. $priority = [
  2071. 'icon' => 'tickets_urgent.png',
  2072. 'url' => 'priorities.php',
  2073. 'content' => get_lang('Priority'),
  2074. ];
  2075. switch ($exclude) {
  2076. case 'project':
  2077. $items = [$status, $priority];
  2078. break;
  2079. case 'status':
  2080. $items = [$project, $priority];
  2081. break;
  2082. case 'priority':
  2083. $items = [$project, $status];
  2084. break;
  2085. default:
  2086. $items = [$project, $status, $priority];
  2087. break;
  2088. }
  2089. return $items;
  2090. }
  2091. /**
  2092. * Returns a list of strings representing the default statuses.
  2093. *
  2094. * @return array
  2095. */
  2096. public static function getDefaultStatusList()
  2097. {
  2098. return [
  2099. self::STATUS_NEW,
  2100. self::STATUS_PENDING,
  2101. self::STATUS_UNCONFIRMED,
  2102. self::STATUS_CLOSE,
  2103. self::STATUS_FORWARDED,
  2104. ];
  2105. }
  2106. /**
  2107. * @return array
  2108. */
  2109. public static function getDefaultPriorityList()
  2110. {
  2111. return [
  2112. self::PRIORITY_NORMAL,
  2113. self::PRIORITY_HIGH,
  2114. self::PRIORITY_LOW,
  2115. self::STATUS_CLOSE,
  2116. self::STATUS_FORWARDED,
  2117. ];
  2118. }
  2119. /**
  2120. * Deletes the user from all the ticket system.
  2121. *
  2122. * @param int $userId
  2123. */
  2124. public static function deleteUserFromTicketSystem($userId)
  2125. {
  2126. $userId = (int) $userId;
  2127. $schema = Database::getManager()->getConnection()->getSchemaManager();
  2128. if ($schema->tablesExist('ticket_assigned_log')) {
  2129. $sql = "UPDATE ticket_assigned_log SET user_id = NULL WHERE user_id = $userId";
  2130. Database::query($sql);
  2131. $sql = "UPDATE ticket_assigned_log SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
  2132. Database::query($sql);
  2133. }
  2134. if ($schema->tablesExist('ticket_ticket')) {
  2135. $sql = "UPDATE ticket_ticket SET assigned_last_user = NULL WHERE assigned_last_user = $userId";
  2136. Database::query($sql);
  2137. $sql = "UPDATE ticket_ticket SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
  2138. Database::query($sql);
  2139. $sql = "UPDATE ticket_ticket SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
  2140. Database::query($sql);
  2141. }
  2142. if ($schema->tablesExist('ticket_category')) {
  2143. $sql = "UPDATE ticket_category SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
  2144. Database::query($sql);
  2145. $sql = "UPDATE ticket_category SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
  2146. Database::query($sql);
  2147. }
  2148. if ($schema->tablesExist('ticket_category_rel_user')) {
  2149. $sql = "DELETE FROM ticket_category_rel_user WHERE user_id = $userId";
  2150. Database::query($sql);
  2151. }
  2152. if ($schema->tablesExist('ticket_message')) {
  2153. $sql = "UPDATE ticket_message SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
  2154. Database::query($sql);
  2155. $sql = "UPDATE ticket_message SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
  2156. Database::query($sql);
  2157. }
  2158. if ($schema->tablesExist('ticket_message_attachments')) {
  2159. $sql = "UPDATE ticket_message_attachments SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
  2160. Database::query($sql);
  2161. $sql = "UPDATE ticket_message_attachments SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
  2162. Database::query($sql);
  2163. }
  2164. if ($schema->tablesExist('ticket_priority')) {
  2165. $sql = "UPDATE ticket_priority SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
  2166. Database::query($sql);
  2167. $sql = "UPDATE ticket_priority SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
  2168. Database::query($sql);
  2169. }
  2170. if ($schema->tablesExist('ticket_project')) {
  2171. $sql = "UPDATE ticket_project SET sys_insert_user_id = NULL WHERE sys_insert_user_id = $userId";
  2172. Database::query($sql);
  2173. $sql = "UPDATE ticket_project SET sys_lastedit_user_id = NULL WHERE sys_lastedit_user_id = $userId";
  2174. Database::query($sql);
  2175. }
  2176. }
  2177. /**
  2178. * @param array $userInfo
  2179. * @param int $projectId
  2180. *
  2181. * @return bool
  2182. */
  2183. public static function userIsAllowInProject($userInfo, $projectId)
  2184. {
  2185. if (api_is_platform_admin()) {
  2186. return true;
  2187. }
  2188. $allowRoleList = self::getAllowedRolesFromProject($projectId);
  2189. // Check if a role was set to the project
  2190. if (!empty($allowRoleList) && is_array($allowRoleList)) {
  2191. if (in_array($userInfo['status'], $allowRoleList)) {
  2192. return true;
  2193. }
  2194. }
  2195. return false;
  2196. }
  2197. /**
  2198. * @param int $projectId
  2199. *
  2200. * @todo load from database instead of configuration.php setting
  2201. *
  2202. * @return array
  2203. */
  2204. public static function getAllowedRolesFromProject($projectId)
  2205. {
  2206. $options = api_get_configuration_value('ticket_project_user_roles');
  2207. if ($options) {
  2208. if (isset($options['permissions'][$projectId])) {
  2209. return $options['permissions'][$projectId];
  2210. }
  2211. }
  2212. return [];
  2213. }
  2214. }