tests_category.php 7.1 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. $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("url" => "exercise.php", "name" => get_lang('Exercises'));
  31. Display::display_header(get_lang('Category'));
  32. // Action handling: add, edit and remove
  33. if (isset($_GET['action']) && $_GET['action'] == 'addcategory') {
  34. add_category_form($_GET['action']);
  35. display_add_category();
  36. } else if (isset($_GET['action']) && $_GET['action'] == 'editcategory') {
  37. edit_category_form($_GET['action']);
  38. } else if (isset($_GET['action']) && $_GET['action'] == 'deletecategory') {
  39. delete_category_form($_GET['action']);
  40. display_add_category();
  41. } else {
  42. display_add_category();
  43. }
  44. echo $category->displayCategories($courseId, $sessionId);
  45. Display::display_footer();
  46. // FUNCTIONS
  47. // form to edit a category
  48. /**
  49. * @todo move to TestCategory.class.php
  50. * @param string $in_action
  51. */
  52. function edit_category_form($in_action) {
  53. $in_action = Security::remove_XSS($in_action);
  54. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  55. $category_id = Security::remove_XSS($_GET['category_id']);
  56. $objcat = new TestCategory($category_id);
  57. $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $in_action . '&category_id=' . $category_id);
  58. // Setting the form elements
  59. $form->addElement('header', get_lang('EditCategory'));
  60. $form->addElement('hidden', 'category_id');
  61. $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
  62. $form->addHtmlEditor(
  63. 'category_description',
  64. get_lang('CategoryDescription'),
  65. false,
  66. false,
  67. array('ToolbarSet' => 'test_category', 'Height' => '200')
  68. );
  69. $form->addButtonSave(get_lang('ModifyCategory'), 'SubmitNote');
  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->addHtmlEditor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Height' => '200'));
  132. $form->addButtonCreate(get_lang('AddTestCategory'), 'SubmitNote');
  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="exercise.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. }