TicketManager.php 83 KB

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