TicketManager.php 82 KB

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