categories.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This script is the Tickets plugin main entry point
  6. * @package chamilo.plugin.ticket
  7. */
  8. // needed in order to load the plugin lang variables
  9. $course_plugin = 'ticket';
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. api_protect_admin_script(true);
  12. $toolName = get_lang('Categories');
  13. $libPath = api_get_path(LIBRARY_PATH);
  14. $webLibPath = api_get_path(WEB_LIBRARY_PATH);
  15. $user_id = api_get_user_id();
  16. $isAdmin = api_is_platform_admin();
  17. $this_section = 'tickets';
  18. unset($_SESSION['this_section']);
  19. $table = new SortableTable(
  20. 'TicketCategories',
  21. array('TicketManager', 'getCategoriesCount'),
  22. array('TicketManager', 'getCategories'),
  23. 1
  24. );
  25. if ($table->per_page == 0) {
  26. $table->per_page = 20;
  27. }
  28. $formToString = '';
  29. $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
  30. $projectId = isset($_GET['project_id']) ? (int) $_GET['project_id'] : 0;
  31. $project = TicketManager::getProject($projectId);
  32. if (empty($project)) {
  33. api_not_allowed(true);
  34. }
  35. Session::write('project_id', $projectId);
  36. $action = isset($_GET['action']) ? $_GET['action'] : '';
  37. $interbreadcrumb[] = array(
  38. 'url' => api_get_path(WEB_CODE_PATH).'ticket/tickets.php?project_id='.$projectId,
  39. 'name' => get_lang('MyTickets')
  40. );
  41. $interbreadcrumb[] = array(
  42. 'url' => api_get_path(WEB_CODE_PATH).'ticket/settings.php',
  43. 'name' => get_lang('Settings')
  44. );
  45. $interbreadcrumb[] = array(
  46. 'url' => api_get_path(WEB_CODE_PATH).'ticket/projects.php',
  47. 'name' => get_lang('Projects')
  48. );
  49. $interbreadcrumb[] = array(
  50. 'url' => api_get_path(WEB_CODE_PATH).'ticket/projects.php',
  51. 'name' => $project->getName()
  52. );
  53. switch ($action) {
  54. case 'delete':
  55. $tickets = TicketManager::getTicketsFromCriteria(['category' => $id]);
  56. if (empty($tickets)) {
  57. TicketManager::deleteCategory($id);
  58. Display::addFlash(Display::return_message(get_lang('Deleted')));
  59. } else {
  60. Display::addFlash(Display::return_message(get_lang('ThisItemIsRelatedToOtherTickets')));
  61. }
  62. header("Location: ".api_get_self().'?project_id='.$projectId);
  63. exit;
  64. break;
  65. case 'add':
  66. $toolName = get_lang('Add');
  67. $interbreadcrumb[] = array(
  68. 'url' => api_get_path(WEB_CODE_PATH).'ticket/categories.php',
  69. 'name' => get_lang('Categories')
  70. );
  71. $url = api_get_self().'?action=add&project_id='.$projectId;
  72. $form = TicketManager::getCategoryForm($url, $projectId);
  73. $formToString = $form->returnForm();
  74. if ($form->validate()) {
  75. $values = $form->getSubmitValues();
  76. $params = [
  77. 'name' => $values['name'],
  78. 'description' => $values['description'],
  79. 'total_tickets' => 0,
  80. 'sys_insert_user_id' => api_get_user_id(),
  81. 'sys_insert_datetime' => api_get_utc_datetime(),
  82. 'course_required' => '',
  83. 'project_id' => $projectId
  84. ];
  85. TicketManager::addCategory($params);
  86. Display::addFlash(Display::return_message(get_lang('Added')));
  87. header("Location: ".api_get_self().'?project_id='.$projectId);
  88. exit;
  89. }
  90. break;
  91. case 'edit':
  92. if (api_get_setting('ticket_allow_category_edition') !== 'true') {
  93. api_not_allowed();
  94. }
  95. $toolName = get_lang('Edit');
  96. $interbreadcrumb[] = array(
  97. 'url' => api_get_path(WEB_CODE_PATH).'ticket/categories.php?project_id='.$projectId,
  98. 'name' => get_lang('Categories')
  99. );
  100. $url = api_get_self().'?action=edit&project_id='.$projectId.'&id='.$id;
  101. $form = TicketManager::getCategoryForm($url, $projectId);
  102. $cat = TicketManager::getCategory($_GET['id']);
  103. $form->setDefaults($cat);
  104. $formToString = $form->returnForm();
  105. if ($form->validate()) {
  106. $values = $form->getSubmitValues();
  107. $params = [
  108. 'name' => $values['name'],
  109. 'description' => $values['description'],
  110. 'sys_lastedit_datetime' => api_get_utc_datetime(),
  111. 'sys_lastedit_user_id' => api_get_user_id()
  112. ];
  113. $cat = TicketManager::updateCategory($_GET['id'], $params);
  114. Display::addFlash(Display::return_message(get_lang('Updated')));
  115. header("Location: ".api_get_self().'?project_id='.$projectId);
  116. exit;
  117. }
  118. break;
  119. default:
  120. break;
  121. }
  122. /**
  123. * Build the modify-column of the table
  124. * @param int The user id
  125. * @param string URL params to add to table links
  126. * @param array Row of elements to alter
  127. * @return string Some HTML-code with modify-buttons
  128. */
  129. function modify_filter($id, $params, $row)
  130. {
  131. $projectId = Session::read('project_id');
  132. $result = '';
  133. if (api_get_setting('ticket_allow_category_edition') === 'true') {
  134. $result .= Display::url(
  135. Display::return_icon('edit.png', get_lang('Edit')),
  136. "categories.php?action=edit&id={$row['id']}&project_id=".$projectId
  137. );
  138. }
  139. $result .= Display::url(
  140. Display::return_icon('user.png', get_lang('AssignUser')),
  141. "categories_add_user.php?id={$row['id']}&project_id=".$projectId
  142. );
  143. if (api_get_setting('ticket_allow_category_edition') === 'true') {
  144. $result .= Display::url(
  145. Display::return_icon('delete.png', get_lang('Delete')),
  146. "categories.php?action=delete&id={$row['id']}&project_id=".$projectId
  147. );
  148. }
  149. return $result;
  150. }
  151. $table->set_header(0, '', false);
  152. $table->set_header(1, get_lang('Title'), false);
  153. $table->set_header(2, get_lang('Description'), true, array("style" => "width:200px"));
  154. $table->set_header(3, get_lang('TotalTickets'), false);
  155. $table->set_header(4, get_lang('Actions'), true);
  156. $table->set_column_filter(4, 'modify_filter');
  157. Display::display_header($toolName);
  158. $items = [
  159. [
  160. 'url' => 'categories.php?action=add&project_id='.$projectId,
  161. 'content' => Display::return_icon('new_folder.png', null, null, ICON_SIZE_MEDIUM)
  162. ]
  163. ];
  164. echo Display::actions($items);
  165. echo $formToString;
  166. echo $table->return_table();
  167. Display::display_footer();