tests_category.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. hubert.borderiou
  5. Manage tests category page
  6. */
  7. $htmlHeadXtra[] = '
  8. <script>
  9. function confirmDelete(in_txt, in_id) {
  10. var oldbgcolor = document.getElementById(in_id).style.backgroundColor;
  11. document.getElementById(in_id).style.backgroundColor="#AAFFB0";
  12. if (confirm(in_txt)) {
  13. return true;
  14. } else {
  15. document.getElementById(in_id).style.backgroundColor = oldbgcolor;
  16. return false;
  17. }
  18. }
  19. </script>';
  20. $nameTools = '';
  21. require_once __DIR__.'/../inc/global.inc.php';
  22. $this_section = SECTION_COURSES;
  23. api_protect_course_script(true);
  24. if (!api_is_allowed_to_edit()) {
  25. api_not_allowed(true);
  26. }
  27. $category = new TestCategory();
  28. $courseId = api_get_course_int_id();
  29. $sessionId = api_get_session_id();
  30. // breadcrumbs
  31. $interbreadcrumb[] = [
  32. "url" => "exercise.php?".api_get_cidreq(),
  33. "name" => get_lang('Tests'),
  34. ];
  35. $action = isset($_GET['action']) ? $_GET['action'] : '';
  36. $content = '';
  37. switch ($action) {
  38. case 'addcategory':
  39. $content = add_category_form('addcategory');
  40. break;
  41. case 'editcategory':
  42. $content = edit_category_form('editcategory');
  43. break;
  44. case 'deletecategory':
  45. delete_category_form();
  46. break;
  47. case 'export_category':
  48. $archiveFile = 'export_exercise_categories_'.api_get_course_id().'_'.api_get_local_time();
  49. $categories = $category->getCategories($courseId, $sessionId);
  50. $export = [];
  51. $export[] = ['title', 'description'];
  52. if (!empty($categories)) {
  53. foreach ($categories as $category) {
  54. $export[] = [$category['title'], $category['description']];
  55. }
  56. }
  57. Export::arrayToCsv($export, $archiveFile);
  58. exit;
  59. break;
  60. case 'import_category':
  61. $form = importCategoryForm();
  62. if ($form->validate()) {
  63. $categories = Import::csv_reader($_FILES['file']['tmp_name']);
  64. if (!empty($categories)) {
  65. foreach ($categories as $item) {
  66. $cat = new TestCategory();
  67. $cat->name = $item['title'];
  68. $cat->description = $item['description'];
  69. $cat->save();
  70. }
  71. Display::addFlash(Display::return_message(get_lang('Imported')));
  72. }
  73. }
  74. $content = $form->returnForm();
  75. break;
  76. }
  77. Display::display_header(get_lang('Category'));
  78. displayActionBar();
  79. echo $content;
  80. echo $category->displayCategories($courseId, $sessionId);
  81. Display::display_footer();
  82. /**
  83. * @return FormValidator
  84. */
  85. function importCategoryForm()
  86. {
  87. $form = new FormValidator('import', 'post', api_get_self().'?action=import_category&'.api_get_cidreq());
  88. $form->addElement('file', 'file', get_lang('CSV file import location'));
  89. $form->addRule('file', get_lang('Required field'), 'required');
  90. $form->addButtonImport(get_lang('Import'));
  91. return $form;
  92. }
  93. /**
  94. * Form to edit a category.
  95. *
  96. * @todo move to TestCategory.class.php
  97. *
  98. * @param string $action
  99. */
  100. function edit_category_form($action)
  101. {
  102. $action = Security::remove_XSS($action);
  103. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  104. $category_id = intval($_GET['category_id']);
  105. $objcat = new TestCategory();
  106. $objcat = $objcat->getCategory($category_id);
  107. $form = new FormValidator(
  108. 'note',
  109. 'post',
  110. api_get_self().'?action='.$action.'&category_id='.$category_id.'&'.api_get_cidreq()
  111. );
  112. // Setting the form elements
  113. $form->addElement('header', get_lang('Edit this category'));
  114. $form->addElement('hidden', 'category_id');
  115. $form->addElement('text', 'category_name', get_lang('Category name'), ['size' => '95']);
  116. $form->addHtmlEditor(
  117. 'category_description',
  118. get_lang('Category description'),
  119. false,
  120. false,
  121. ['ToolbarSet' => 'TestQuestionDescription', 'Height' => '200']
  122. );
  123. $form->addButtonSave(get_lang('Edit category'), 'SubmitNote');
  124. // setting the defaults
  125. $defaults = [];
  126. $defaults['category_id'] = $objcat->id;
  127. $defaults['category_name'] = $objcat->name;
  128. $defaults['category_description'] = $objcat->description;
  129. $form->setDefaults($defaults);
  130. // setting the rules
  131. $form->addRule('category_name', get_lang('Required field'), 'required');
  132. // The validation or display
  133. if ($form->validate()) {
  134. $check = Security::check_token('post');
  135. if ($check) {
  136. $values = $form->exportValues();
  137. $category = new TestCategory();
  138. $category = $category->getCategory($values['category_id']);
  139. if ($category) {
  140. $category->name = $values['category_name'];
  141. $category->description = $values['category_description'];
  142. $category->modifyCategory();
  143. Display::addFlash(Display::return_message(get_lang('Update successful')));
  144. } else {
  145. Display::addFlash(Display::return_message(get_lang('Edit categoryError'), 'error'));
  146. }
  147. }
  148. Security::clear_token();
  149. } else {
  150. $token = Security::get_token();
  151. $form->addElement('hidden', 'sec_token');
  152. $form->setConstants(['sec_token' => $token]);
  153. return $form->returnForm();
  154. }
  155. } else {
  156. Display::addFlash(
  157. Display::return_message(get_lang('CannotEdit this category'), 'error')
  158. );
  159. }
  160. }
  161. // process to delete a category
  162. function delete_category_form()
  163. {
  164. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  165. $category = new TestCategory();
  166. if ($category->removeCategory($_GET['category_id'])) {
  167. Display::addFlash(Display::return_message(get_lang('Category deleted')));
  168. } else {
  169. Display::addFlash(Display::return_message(get_lang('Error: could not delete category'), 'error'));
  170. }
  171. } else {
  172. Display::addFlash(Display::return_message(get_lang('Error: could not delete category'), 'error'));
  173. }
  174. }
  175. /**
  176. * form to add a category.
  177. *
  178. * @todo move to TestCategory.class.php
  179. *
  180. * @param string $action
  181. */
  182. function add_category_form($action)
  183. {
  184. $action = Security::remove_XSS($action);
  185. // initiate the object
  186. $form = new FormValidator('note', 'post', api_get_self().'?action='.$action.'&'.api_get_cidreq());
  187. // Setting the form elements
  188. $form->addElement('header', get_lang('Add category'));
  189. $form->addElement('text', 'category_name', get_lang('Category name'), ['size' => '95']);
  190. $form->addHtmlEditor(
  191. 'category_description',
  192. get_lang('Category description'),
  193. false,
  194. false,
  195. ['ToolbarSet' => 'TestQuestionDescription', 'Height' => '200']
  196. );
  197. $form->addButtonCreate(get_lang('Add test category'), 'SubmitNote');
  198. // setting the rules
  199. $form->addRule('category_name', get_lang('Required field'), 'required');
  200. // The validation or display
  201. if ($form->validate()) {
  202. $check = Security::check_token('post');
  203. if ($check) {
  204. $values = $form->exportValues();
  205. $category = new TestCategory();
  206. $category->name = $values['category_name'];
  207. $category->description = $values['category_description'];
  208. if ($category->save()) {
  209. Display::addFlash(Display::return_message(get_lang('Category added')));
  210. } else {
  211. Display::addFlash(Display::return_message(get_lang('AddCategory nameAlreadyExists'), 'warning'));
  212. }
  213. }
  214. Security::clear_token();
  215. } else {
  216. $token = Security::get_token();
  217. $form->addElement('hidden', 'sec_token');
  218. $form->setConstants(['sec_token' => $token]);
  219. return $form->returnForm();
  220. }
  221. }
  222. // Display add category button
  223. function displayActionBar()
  224. {
  225. echo '<div class="actions">';
  226. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">'.
  227. Display::return_icon('back.png', get_lang('Go back to the questions list'), '', ICON_SIZE_MEDIUM).'</a>';
  228. echo '<a href="'.api_get_self().'?action=addcategory&'.api_get_cidreq().'">'.
  229. Display::return_icon('new_folder.png', get_lang('Add category'), null, ICON_SIZE_MEDIUM).'</a>';
  230. echo Display::url(
  231. Display::return_icon('export_csv.png', get_lang('CSV export'), [], ICON_SIZE_MEDIUM),
  232. api_get_self().'?action=export_category&'.api_get_cidreq()
  233. );
  234. echo Display::url(
  235. Display::return_icon('import_csv.png', get_lang('Import from a CSV'), [], ICON_SIZE_MEDIUM),
  236. api_get_self().'?action=import_category&'.api_get_cidreq()
  237. );
  238. echo '</div>';
  239. echo "<br/>";
  240. echo "<fieldset><legend>".get_lang('Questions category')."</legend></fieldset>";
  241. }