tests_category.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 '../inc/global.inc.php';
  22. $this_section = SECTION_COURSES;
  23. if (!api_is_allowed_to_edit()) {
  24. api_not_allowed(true);
  25. }
  26. $category = new TestCategory();
  27. $courseId = api_get_course_int_id();
  28. $sessionId = api_get_session_id();
  29. // breadcrumbs
  30. $interbreadcrumb[] = array(
  31. "url" => "exercise.php?".api_get_cidreq(),
  32. "name" => get_lang('Exercises'),
  33. );
  34. $action = isset($_GET['action']) ? $_GET['action'] : '';
  35. $content = '';
  36. switch ($action) {
  37. case 'addcategory':
  38. $content = add_category_form('addcategory');
  39. break;
  40. case 'editcategory':
  41. $content = edit_category_form('editcategory');
  42. break;
  43. case 'deletecategory':
  44. delete_category_form('deletecategory');
  45. break;
  46. case 'export_category':
  47. $archiveFile = 'export_exercise_categoroes_'.api_get_course_id().'_'.api_get_local_time();
  48. $categories = $category->getCategories($courseId, $sessionId);
  49. $export = [];
  50. $export[] = ['title', 'description'];
  51. if (!empty($categories)) {
  52. foreach ($categories as $category) {
  53. $export[] = [$category['title'], $category['description']];
  54. }
  55. }
  56. Export::arrayToCsv($export, $archiveFile);
  57. exit;
  58. break;
  59. case 'import_category':
  60. $form = importCategoryForm();
  61. if ($form->validate()) {
  62. $categories = Import::csv_reader($_FILES['file']['tmp_name']);
  63. if (!empty($categories)) {
  64. foreach ($categories as $item) {
  65. $cat = new TestCategory();
  66. $cat->name = $item['title'];
  67. $cat->description = $item['description'];
  68. $cat->addCategoryInBDD();
  69. }
  70. Display::addFlash(Display::return_message(get_lang('Imported')));
  71. }
  72. }
  73. $content = $form->returnForm();
  74. break;
  75. }
  76. Display::display_header(get_lang('Category'));
  77. displayActionBar();
  78. echo $content;
  79. echo $category->displayCategories($courseId, $sessionId);
  80. Display::display_footer();
  81. /**
  82. * @return FormValidator
  83. */
  84. function importCategoryForm()
  85. {
  86. $form = new FormValidator('import', 'post', api_get_self().'?action=import_category&'.api_get_cidreq());
  87. $form->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
  88. $form->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
  89. $form->addButtonImport(get_lang('Import'));
  90. return $form;
  91. }
  92. /**
  93. * Form to edit a category
  94. * @todo move to TestCategory.class.php
  95. * @param string $action
  96. */
  97. function edit_category_form($action)
  98. {
  99. $action = Security::remove_XSS($action);
  100. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  101. $category_id = intval($_GET['category_id']);
  102. $objcat = new TestCategory();
  103. $objcat = $objcat->getCategory($category_id);
  104. $form = new FormValidator(
  105. 'note',
  106. 'post',
  107. api_get_self().'?action='.$action.'&category_id='.$category_id.'&'.api_get_cidreq()
  108. );
  109. // Setting the form elements
  110. $form->addElement('header', get_lang('EditCategory'));
  111. $form->addElement('hidden', 'category_id');
  112. $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
  113. $form->addHtmlEditor(
  114. 'category_description',
  115. get_lang('CategoryDescription'),
  116. false,
  117. false,
  118. array('ToolbarSet' => 'TestQuestionDescription', 'Height' => '200')
  119. );
  120. $form->addButtonSave(get_lang('ModifyCategory'), 'SubmitNote');
  121. // setting the defaults
  122. $defaults = array();
  123. $defaults["category_id"] = $objcat->id;
  124. $defaults["category_name"] = $objcat->name;
  125. $defaults["category_description"] = $objcat->description;
  126. $form->setDefaults($defaults);
  127. // setting the rules
  128. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  129. // The validation or display
  130. if ($form->validate()) {
  131. $check = Security::check_token('post');
  132. if ($check) {
  133. $values = $form->exportValues();
  134. $category = new TestCategory();
  135. $category = $category->getCategory($values['category_id']);
  136. if ($category) {
  137. $category->name = $values['category_name'];
  138. $category->description = $values['category_description'];
  139. $category->modifyCategory();
  140. Display::addFlash(Display::return_message(get_lang('Updated')));
  141. } else {
  142. Display::addFlash(Display::return_message(get_lang('ModifyCategoryError'), 'error'));
  143. }
  144. }
  145. Security::clear_token();
  146. } else {
  147. $token = Security::get_token();
  148. $form->addElement('hidden', 'sec_token');
  149. $form->setConstants(array('sec_token' => $token));
  150. return $form->returnForm();
  151. }
  152. } else {
  153. Display::addFlash(
  154. Display::return_message(get_lang('CannotEditCategory'), 'error')
  155. );
  156. }
  157. }
  158. // process to delete a category
  159. function delete_category_form()
  160. {
  161. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  162. $category = new TestCategory();
  163. if ($category->removeCategory($_GET['category_id'])) {
  164. Display::addFlash(Display::return_message(get_lang('DeleteCategoryDone')));
  165. } else {
  166. Display::addFlash(Display::return_message(get_lang('CannotDeleteCategoryError'), 'error'));
  167. }
  168. } else {
  169. Display::addFlash(Display::return_message(get_lang('CannotDeleteCategoryError'), 'error'));
  170. }
  171. }
  172. /**
  173. * form to add a category
  174. * @todo move to TestCategory.class.php
  175. * @param string $action
  176. */
  177. function add_category_form($action)
  178. {
  179. $action = Security::remove_XSS($action);
  180. // initiate the object
  181. $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $action.'&'.api_get_cidreq());
  182. // Setting the form elements
  183. $form->addElement('header', get_lang('AddACategory'));
  184. $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
  185. $form->addHtmlEditor(
  186. 'category_description',
  187. get_lang('CategoryDescription'),
  188. false,
  189. false,
  190. array('ToolbarSet' => 'TestQuestionDescription', 'Height' => '200')
  191. );
  192. $form->addButtonCreate(get_lang('AddTestCategory'), 'SubmitNote');
  193. // setting the rules
  194. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  195. // The validation or display
  196. if ($form->validate()) {
  197. $check = Security::check_token('post');
  198. if ($check) {
  199. $values = $form->exportValues();
  200. $category = new TestCategory();
  201. $category->name = $values['category_name'];
  202. $category->description = $values['category_description'];
  203. if ($category->addCategoryInBDD()) {
  204. Display::addFlash(Display::return_message(get_lang('AddCategoryDone')));
  205. } else {
  206. Display::addFlash(Display::return_message(get_lang('AddCategoryNameAlreadyExists'), 'warning'));
  207. }
  208. }
  209. Security::clear_token();
  210. } else {
  211. $token = Security::get_token();
  212. $form->addElement('hidden', 'sec_token');
  213. $form->setConstants(array('sec_token' => $token));
  214. return $form->returnForm();
  215. }
  216. }
  217. // Display add category button
  218. function displayActionBar()
  219. {
  220. echo '<div class="actions">';
  221. echo '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?' . api_get_cidreq() . '">' .
  222. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>';
  223. echo '<a href="' . api_get_self() . '?action=addcategory&'.api_get_cidreq().'">' .
  224. Display::return_icon('question_category.gif', get_lang('AddACategory')) . '</a>';
  225. echo Display::url(
  226. Display::return_icon('export_csv.png', get_lang('ExportAsCSV'), [], ICON_SIZE_MEDIUM),
  227. api_get_self() . '?action=export_category&'.api_get_cidreq()
  228. );
  229. echo Display::url(
  230. Display::return_icon('import_csv.png', get_lang('ImportAsCSV'), [], ICON_SIZE_MEDIUM),
  231. api_get_self() . '?action=import_category&'.api_get_cidreq()
  232. );
  233. echo '</div>';
  234. echo "<br/>";
  235. echo "<fieldset><legend>" . get_lang('QuestionCategory') . "</legend></fieldset>";
  236. }