tests_category.php 7.3 KB

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