categories.php 6.2 KB

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