tests_category.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. Display::display_header(get_lang('Category'));
  35. // Action handling: add, edit and remove
  36. if (isset($_GET['action']) && $_GET['action'] == 'addcategory') {
  37. add_category_form($_GET['action']);
  38. display_add_category();
  39. } else if (isset($_GET['action']) && $_GET['action'] == 'editcategory') {
  40. edit_category_form($_GET['action']);
  41. } else if (isset($_GET['action']) && $_GET['action'] == 'deletecategory') {
  42. delete_category_form($_GET['action']);
  43. display_add_category();
  44. } else {
  45. display_add_category();
  46. }
  47. echo $category->displayCategories($courseId, $sessionId);
  48. Display::display_footer();
  49. /**
  50. * Form to edit a category
  51. * @todo move to TestCategory.class.php
  52. * @param string $action
  53. */
  54. function edit_category_form($action) {
  55. $action = Security::remove_XSS($action);
  56. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  57. $category_id = Security::remove_XSS($_GET['category_id']);
  58. $objcat = new TestCategory($category_id);
  59. $form = new FormValidator(
  60. 'note',
  61. 'post',
  62. api_get_self().'?action='.$action.'&category_id='.$category_id
  63. );
  64. // Setting the form elements
  65. $form->addElement('header', get_lang('EditCategory'));
  66. $form->addElement('hidden', 'category_id');
  67. $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
  68. $form->addHtmlEditor(
  69. 'category_description',
  70. get_lang('CategoryDescription'),
  71. false,
  72. false,
  73. array('ToolbarSet' => 'test_category', 'Height' => '200')
  74. );
  75. $form->addButtonSave(get_lang('ModifyCategory'), 'SubmitNote');
  76. // setting the defaults
  77. $defaults = array();
  78. $defaults["category_id"] = $objcat->id;
  79. $defaults["category_name"] = $objcat->name;
  80. $defaults["category_description"] = $objcat->description;
  81. $form->setDefaults($defaults);
  82. // setting the rules
  83. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  84. // The validation or display
  85. if ($form->validate()) {
  86. $check = Security::check_token('post');
  87. if ($check) {
  88. $values = $form->exportValues();
  89. $v_id = Security::remove_XSS($values['category_id']);
  90. $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
  91. $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
  92. $objcat = new TestCategory($v_id, $v_name, $v_description);
  93. if ($objcat->modifyCategory()) {
  94. Display::display_confirmation_message(get_lang('MofidfyCategoryDone'));
  95. } else {
  96. Display::display_confirmation_message(get_lang('ModifyCategoryError'));
  97. }
  98. }
  99. Security::clear_token();
  100. } else {
  101. display_goback();
  102. $token = Security::get_token();
  103. $form->addElement('hidden', 'sec_token');
  104. $form->setConstants(array('sec_token' => $token));
  105. $form->display();
  106. }
  107. } else {
  108. Display::display_error_message(get_lang('CannotEditCategory'));
  109. }
  110. }
  111. // process to delete a category
  112. function delete_category_form($action) {
  113. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  114. $category_id = Security::remove_XSS($_GET['category_id']);
  115. $catobject = new TestCategory($category_id);
  116. if ($catobject->removeCategory()) {
  117. Display::display_confirmation_message(get_lang('DeleteCategoryDone'));
  118. } else {
  119. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  120. }
  121. } else {
  122. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  123. }
  124. }
  125. /**
  126. * form to add a category
  127. * @todo move to TestCategory.class.php
  128. * @param string $action
  129. */
  130. function add_category_form($action) {
  131. $action = Security::remove_XSS($action);
  132. // initiate the object
  133. $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $action);
  134. // Setting the form elements
  135. $form->addElement('header', get_lang('AddACategory'));
  136. $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
  137. $form->addHtmlEditor(
  138. 'category_description',
  139. get_lang('CategoryDescription'),
  140. false,
  141. false,
  142. array('ToolbarSet' => 'test_category', 'Height' => '200')
  143. );
  144. $form->addButtonCreate(get_lang('AddTestCategory'), 'SubmitNote');
  145. // setting the rules
  146. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  147. // The validation or display
  148. if ($form->validate()) {
  149. $check = Security::check_token('post');
  150. if ($check) {
  151. $values = $form->exportValues();
  152. $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
  153. $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
  154. $objcat = new TestCategory(0, $v_name, $v_description);
  155. if ($objcat->addCategoryInBDD()) {
  156. Display::display_confirmation_message(get_lang('AddCategoryDone'));
  157. } else {
  158. Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists'));
  159. }
  160. }
  161. Security::clear_token();
  162. } else {
  163. display_goback();
  164. $token = Security::get_token();
  165. $form->addElement('hidden', 'sec_token');
  166. $form->setConstants(array('sec_token' => $token));
  167. $form->display();
  168. }
  169. }
  170. // Display add category button
  171. function display_add_category() {
  172. echo '<div class="actions">';
  173. echo '<a href="exercise.php?' . api_get_cidreq() . '">' .
  174. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>';
  175. echo '<a href="' . api_get_self() . '?action=addcategory">' .
  176. Display::return_icon('question_category.gif', get_lang('AddACategory')) . '</a>';
  177. echo '</div>';
  178. echo "<br/>";
  179. echo "<fieldset><legend>" . get_lang('QuestionCategory') . "</legend></fieldset>";
  180. }
  181. // display goback to category list page link
  182. function display_goback() {
  183. echo '<div class="actions">';
  184. echo '<a href="' . api_get_self() . '">' .
  185. Display::return_icon('back.png', get_lang('BackToCategoryList'), array(), 32) . '</a>';
  186. echo '</div>';
  187. }