tests_category.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. }
  15. else {
  16. document.getElementById(in_id).style.backgroundColor = oldbgcolor;
  17. return false;
  18. }
  19. }
  20. </script>
  21. ';
  22. // name of the language file that needs to be included
  23. $language_file = 'exercice';
  24. $nameTools = "";
  25. require_once '../inc/global.inc.php';
  26. require_once 'exercise.lib.php';
  27. require_once 'question.class.php';
  28. require_once 'testcategory.class.php';
  29. $this_section = SECTION_COURSES;
  30. if (!api_is_allowed_to_edit()) {
  31. api_not_allowed(true);
  32. }
  33. // breadcrumbs
  34. $interbreadcrumb[] = array("url" => "exercice.php", "name" => get_lang('Exercices'));
  35. Display::display_header(get_lang('Category'));
  36. // Action handling: add, edit and remove
  37. if (isset($_GET['action']) && $_GET['action'] == 'addcategory') {
  38. add_category_form($_GET['action']);
  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. } else {
  44. display_add_category();
  45. display_categories();
  46. }
  47. Display::display_footer();
  48. // FUNCTIONS
  49. // form to edit a category
  50. function edit_category_form($in_action) {
  51. $in_action = Security::remove_XSS($in_action);
  52. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  53. $category_id = Security::remove_XSS($_GET['category_id']);
  54. $objcat = new Testcategory($category_id);
  55. // initiate the object
  56. $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $in_action . '&category_id=' . $category_id);
  57. // settting the form elements
  58. $form->addElement('header', get_lang('EditCategory'));
  59. $form->addElement('hidden', 'category_id');
  60. $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
  61. $form->add_html_editor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200'));
  62. $form->addElement('style_submit_button', 'SubmitNote', get_lang('ModifyCategory'), 'class="add"');
  63. // setting the defaults
  64. $defaults = array();
  65. $defaults["category_id"] = $objcat->id;
  66. $defaults["category_name"] = $objcat->name;
  67. $defaults["category_description"] = $objcat->description;
  68. $form->setDefaults($defaults);
  69. // setting the rules
  70. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  71. // The validation or display
  72. if ($form->validate()) {
  73. $check = Security::check_token('post');
  74. if ($check) {
  75. $values = $form->exportValues();
  76. $v_id = Security::remove_XSS($values['category_id']);
  77. $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
  78. $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
  79. $objcat = new Testcategory($v_id, $v_name, $v_description);
  80. if ($objcat->modifyCategory()) {
  81. Display::display_confirmation_message(get_lang('MofidfyCategoryDone'));
  82. } else {
  83. Display::display_confirmation_message(get_lang('ModifyCategoryError'));
  84. }
  85. }
  86. Security::clear_token();
  87. display_add_category();
  88. display_categories();
  89. } else {
  90. display_goback();
  91. $token = Security::get_token();
  92. $form->addElement('hidden', 'sec_token');
  93. $form->setConstants(array('sec_token' => $token));
  94. $form->display();
  95. display_categories();
  96. }
  97. } else {
  98. Display::display_error_message(get_lang('CannotEditCategory'));
  99. }
  100. }
  101. // process to delete a category
  102. function delete_category_form($in_action) {
  103. $in_action = Security::remove_XSS($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->getCategoryQuestionsNumber() == 0) {
  108. if ($catobject->removeCategory()) {
  109. Display::display_confirmation_message(get_lang('DeleteCategoryDone'));
  110. } else {
  111. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  112. }
  113. } else {
  114. Display::display_error_message(get_lang('CannotDeleteCategory'));
  115. }
  116. } else {
  117. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  118. }
  119. display_add_category();
  120. display_categories();
  121. }
  122. // form to add a category
  123. function add_category_form($in_action) {
  124. $in_action = Security::remove_XSS($in_action);
  125. // initiate the object
  126. $form = new FormValidator('note', 'post', api_get_self() . '?action=' . $in_action);
  127. // settting the form elements
  128. $form->addElement('header', get_lang('AddACategory'));
  129. $form->addElement('text', 'category_name', get_lang('CategoryName'), array('size' => '95'));
  130. $form->add_html_editor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200'));
  131. $form->addElement('style_submit_button', 'SubmitNote', get_lang('AddTestCategory'), 'class="add"');
  132. // setting the rules
  133. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  134. // The validation or display
  135. if ($form->validate()) {
  136. $check = Security::check_token('post');
  137. if ($check) {
  138. $values = $form->exportValues();
  139. $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
  140. $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
  141. $objcat = new Testcategory(0, $v_name, $v_description);
  142. if ($objcat->addCategoryInBDD()) {
  143. Display::display_confirmation_message(get_lang('AddCategoryDone'));
  144. } else {
  145. Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists'));
  146. }
  147. }
  148. Security::clear_token();
  149. display_add_category();
  150. display_categories();
  151. } else {
  152. display_goback();
  153. $token = Security::get_token();
  154. $form->addElement('hidden', 'sec_token');
  155. $form->setConstants(array('sec_token' => $token));
  156. $form->display();
  157. display_categories();
  158. }
  159. }
  160. // Display add category button
  161. function display_add_category() {
  162. echo '<div class="actions">';
  163. echo '<a href="exercice.php?' . api_get_cidreq() . '">' . Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>';
  164. echo '<a href="' . api_get_self() . '?action=addcategory">' . Display::return_icon('question_category.gif', get_lang('AddACategory')) . '</a>';
  165. echo '</div>';
  166. echo "<br/>";
  167. echo "<fieldset><legend>" . get_lang('QuestionCategory') . "</legend></fieldset>";
  168. }
  169. // Display category list
  170. function display_categories() {
  171. $course_id = api_get_course_int_id();
  172. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  173. $sql = "SELECT * FROM $t_cattable WHERE c_id = $course_id ORDER BY title";
  174. $res = Database::query($sql);
  175. while ($row = Database::fetch_array($res)) {
  176. // le titre avec le nombre de questions qui sont dans cette cat�gorie
  177. $tmpobj = new Testcategory($row['id']);
  178. $nb_question = $tmpobj->getCategoryQuestionsNumber();
  179. echo '<div class="sectiontitle" id="id_cat' . $row['id'] . '">';
  180. $nb_question_label = $nb_question == 1 ? $nb_question . ' ' . get_lang('Question') : $nb_question . ' ' . get_lang('Questions');
  181. echo "<span style='float:right'>" . $nb_question_label . "</span>";
  182. echo $row['title'];
  183. echo '</div>';
  184. echo '<div class="sectioncomment">';
  185. echo $row['description'];
  186. echo '</div>';
  187. echo '<div>';
  188. echo '<a href="' . api_get_self() . '?action=editcategory&amp;category_id=' . $row['id'] . '">' . Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL) . '</a>';
  189. if ($nb_question > 0) {
  190. echo '<a href="javascript:void(0)" onclick="alert(\'' . protectJSDialogQuote(get_lang('CannotDeleteCategory')) . '\')">';
  191. echo Display::return_icon('delete_na.png', get_lang('CannotDeleteCategory'), array(), ICON_SIZE_SMALL);
  192. echo '</a>';
  193. } else {
  194. $rowname = protectJSDialogQuote($row['title']);
  195. echo ' <a href="' . api_get_self() . '?action=deletecategory&amp;category_id=' . $row['id'] . '" ';
  196. echo 'onclick="return confirmDelete(\'' . protectJSDialogQuote(get_lang('DeleteCategoryAreYouSure') . '[' . $rowname) . '] ?\', \'id_cat' . $row['id'] . '\');">';
  197. echo Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>';
  198. }
  199. echo '</div>';
  200. }
  201. }
  202. // display goback to category list page link
  203. function display_goback() {
  204. echo '<div class="actions">';
  205. echo '<a href="' . api_get_self() . '">' . Display::return_icon('back.png', get_lang('BackToCategoryList'), array(), 32) . '</a>';
  206. echo '</div>';
  207. }
  208. // To allowed " in javascript dialog box without bad surprises
  209. // replace " with two '
  210. function protectJSDialogQuote($in_txt) {
  211. $res = $in_txt;
  212. $res = str_replace("'", "\'", $res);
  213. $res = str_replace('"', "\'\'", $res); // super astuce pour afficher les " dans les boite de dialogue
  214. return $res;
  215. }