category.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. if (api_get_configuration_value('allow_exercise_categories') === false) {
  5. api_not_allowed();
  6. }
  7. api_protect_course_script();
  8. if (!api_is_allowed_to_edit()) {
  9. api_not_allowed(true);
  10. }
  11. $interbreadcrumb[] = [
  12. 'url' => api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq(),
  13. 'name' => get_lang('Tests'),
  14. ];
  15. $courseId = api_get_course_int_id();
  16. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_exercise_categories&c_id='.$courseId.'&'.api_get_cidreq();
  17. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  18. $obj = new ExerciseCategoryManager();
  19. $check = Security::check_token('request');
  20. $token = Security::get_token();
  21. //Add the JS needed to use the jqgrid
  22. $htmlHeadXtra[] = api_get_jqgrid_js();
  23. //The order is important you need to check the the $column variable in the model.ajax.php file
  24. $columns = [
  25. get_lang('Name'),
  26. get_lang('Detail'),
  27. ];
  28. // Column config
  29. $column_model = [
  30. [
  31. 'name' => 'name',
  32. 'index' => 'name',
  33. 'width' => '140',
  34. 'align' => 'left',
  35. ],
  36. [
  37. 'name' => 'actions',
  38. 'index' => 'actions',
  39. 'width' => '40',
  40. 'align' => 'left',
  41. 'formatter' => 'action_formatter',
  42. 'sortable' => 'false',
  43. ],
  44. ];
  45. // Autowidth
  46. $extra_params['autowidth'] = 'true';
  47. // height auto
  48. $extra_params['height'] = 'auto';
  49. $action_links = $obj->getJqgridActionLinks($token);
  50. $htmlHeadXtra[] = '<script>
  51. $(function() {
  52. // grid definition see the $obj->display() function
  53. '.Display::grid_js(
  54. 'categories',
  55. $url,
  56. $columns,
  57. $column_model,
  58. $extra_params,
  59. [],
  60. $action_links,
  61. true
  62. ).'
  63. });
  64. </script>';
  65. $url = api_get_self().'?'.api_get_cidreq();
  66. switch ($action) {
  67. case 'add':
  68. $interbreadcrumb[] = ['url' => $url, 'name' => get_lang('Categories')];
  69. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Add')];
  70. $form = $obj->return_form($url.'&action=add', 'add');
  71. // The validation or display
  72. if ($form->validate()) {
  73. $values = $form->exportValues();
  74. unset($values['id']);
  75. $res = $obj->save($values);
  76. if ($res) {
  77. Display::addFlash(Display::return_message(get_lang('Item added'), 'confirmation'));
  78. }
  79. header('Location: '.$url);
  80. exit;
  81. } else {
  82. $content = '<div class="actions">';
  83. $content .= '<a href="'.$url.'">'.
  84. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
  85. $content .= '</div>';
  86. $form->addElement('hidden', 'sec_token');
  87. $form->setConstants(['sec_token' => $token]);
  88. $content .= $form->returnForm();
  89. }
  90. break;
  91. case 'edit':
  92. $interbreadcrumb[] = ['url' => $url, 'name' => get_lang('Categories')];
  93. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Edit')];
  94. $form = $obj->return_form($url.'&action=edit&id='.intval($_GET['id']), 'edit');
  95. // The validation or display
  96. if ($form->validate()) {
  97. $values = $form->exportValues();
  98. $res = $obj->update($values);
  99. if ($res) {
  100. Display::addFlash(Display::return_message(get_lang('Item updated'), 'confirmation'));
  101. }
  102. header('Location: '.$url);
  103. exit;
  104. } else {
  105. $content = '<div class="actions">';
  106. $content .= '<a href="'.$url.'">'.
  107. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
  108. $content .= '</div>';
  109. $form->addElement('hidden', 'sec_token');
  110. $form->setConstants(['sec_token' => $token]);
  111. $content .= $form->returnForm();
  112. }
  113. break;
  114. case 'delete':
  115. $res = $obj->delete($_GET['id']);
  116. if ($res) {
  117. Display::addFlash(Display::return_message(get_lang('Item deleted'), 'confirmation'));
  118. }
  119. header('Location: '.$url);
  120. exit;
  121. break;
  122. default:
  123. $content = $obj->display();
  124. break;
  125. }
  126. Display::display_header();
  127. echo $content;