tests_category.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // name of the language file that needs to be included
  21. $language_file = 'exercice';
  22. $nameTools = "";
  23. require_once '../inc/global.inc.php';
  24. require_once 'exercise.lib.php';
  25. require_once 'question.class.php';
  26. require_once 'testcategory.class.php';
  27. $this_section = SECTION_COURSES;
  28. if (!api_is_allowed_to_edit()) {
  29. api_not_allowed(true);
  30. }
  31. $category = new Testcategory();
  32. $courseId = api_get_course_int_id();
  33. $sessionId = api_get_session_id();
  34. // breadcrumbs
  35. $interbreadcrumb[] = array("url" => "exercice.php", "name" => get_lang('Exercices'));
  36. Display::display_header(get_lang('Category'));
  37. // Action handling: add, edit and remove
  38. if (isset($_GET['action']) && $_GET['action'] == 'addcategory') {
  39. add_category_form($_GET['action']);
  40. display_add_category();
  41. } else if (isset($_GET['action']) && $_GET['action'] == 'editcategory') {
  42. edit_category_form($_GET['action']);
  43. } else if (isset($_GET['action']) && $_GET['action'] == 'deletecategory') {
  44. delete_category_form($_GET['action']);
  45. display_add_category();
  46. } else {
  47. display_add_category();
  48. }
  49. echo $category->displayCategories($courseId, $sessionId);
  50. Display::display_footer();
  51. // FUNCTIONS
  52. // form to edit a category
  53. /**
  54. * @todo move to testcategory.class.php
  55. * @param string $in_action
  56. */
  57. function edit_category_form($in_action) {
  58. $in_action = Security::remove_XSS($in_action);
  59. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  60. $category_id = Security::remove_XSS($_GET['category_id']);
  61. $objcat = new Testcategory($category_id);
  62. // initiate the object
  63. $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $in_action . '&category_id=' . $category_id);
  64. // settting 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->add_html_editor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200'));
  69. $form->addElement('style_submit_button', 'SubmitNote', get_lang('ModifyCategory'), 'class="add"');
  70. // setting the defaults
  71. $defaults = array();
  72. $defaults["category_id"] = $objcat->id;
  73. $defaults["category_name"] = $objcat->name;
  74. $defaults["category_description"] = $objcat->description;
  75. $form->setDefaults($defaults);
  76. // setting the rules
  77. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  78. // The validation or display
  79. if ($form->validate()) {
  80. $check = Security::check_token('post');
  81. if ($check) {
  82. $values = $form->exportValues();
  83. $v_id = Security::remove_XSS($values['category_id']);
  84. $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
  85. $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
  86. $objcat = new Testcategory($v_id, $v_name, $v_description);
  87. if ($objcat->modifyCategory()) {
  88. Display::display_confirmation_message(get_lang('MofidfyCategoryDone'));
  89. } else {
  90. Display::display_confirmation_message(get_lang('ModifyCategoryError'));
  91. }
  92. }
  93. Security::clear_token();
  94. } else {
  95. display_goback();
  96. $token = Security::get_token();
  97. $form->addElement('hidden', 'sec_token');
  98. $form->setConstants(array('sec_token' => $token));
  99. $form->display();
  100. }
  101. } else {
  102. Display::display_error_message(get_lang('CannotEditCategory'));
  103. }
  104. }
  105. // process to delete a category
  106. function delete_category_form($in_action) {
  107. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  108. $category_id = Security::remove_XSS($_GET['category_id']);
  109. $catobject = new Testcategory($category_id);
  110. if ($catobject->removeCategory()) {
  111. Display::display_confirmation_message(get_lang('DeleteCategoryDone'));
  112. } else {
  113. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  114. }
  115. } else {
  116. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  117. }
  118. }
  119. /**
  120. * form to add a category
  121. * @todo move to testcategory.class.php
  122. * @param string $in_action
  123. */
  124. function add_category_form($in_action) {
  125. $in_action = Security::remove_XSS($in_action);
  126. // initiate the object
  127. $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $in_action);
  128. // Setting the form elements
  129. $form->addElement('header', get_lang('AddACategory'));
  130. $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
  131. $form->add_html_editor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200'));
  132. $form->addElement('style_submit_button', 'SubmitNote', get_lang('AddTestCategory'), 'class="add"');
  133. // setting the rules
  134. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  135. // The validation or display
  136. if ($form->validate()) {
  137. $check = Security::check_token('post');
  138. if ($check) {
  139. $values = $form->exportValues();
  140. $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
  141. $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
  142. $objcat = new Testcategory(0, $v_name, $v_description);
  143. if ($objcat->addCategoryInBDD()) {
  144. Display::display_confirmation_message(get_lang('AddCategoryDone'));
  145. } else {
  146. Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists'));
  147. }
  148. }
  149. Security::clear_token();
  150. } else {
  151. display_goback();
  152. $token = Security::get_token();
  153. $form->addElement('hidden', 'sec_token');
  154. $form->setConstants(array('sec_token' => $token));
  155. $form->display();
  156. }
  157. }
  158. // Display add category button
  159. function display_add_category() {
  160. echo '<div class="actions">';
  161. echo '<a href="exercice.php?' . api_get_cidreq() . '">' . Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>';
  162. echo '<a href="' . api_get_self() . '?action=addcategory">' . Display::return_icon('question_category.gif', get_lang('AddACategory')) . '</a>';
  163. echo '</div>';
  164. echo "<br/>";
  165. echo "<fieldset><legend>" . get_lang('QuestionCategory') . "</legend></fieldset>";
  166. }
  167. // display goback to category list page link
  168. function display_goback() {
  169. echo '<div class="actions">';
  170. echo '<a href="' . api_get_self() . '">' . Display::return_icon('back.png', get_lang('BackToCategoryList'), array(), 32) . '</a>';
  171. echo '</div>';
  172. }