tests_category.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. hubert.borderiou
  4. Manage tests category page
  5. */
  6. $htmlHeadXtra[] = '
  7. <script type="text/javascript">
  8. function confirmDelete(in_txt, in_id) {
  9. var oldbgcolor = document.getElementById(in_id).style.backgroundColor;
  10. document.getElementById(in_id).style.backgroundColor="#AAFFB0";
  11. if (confirm(in_txt)) {
  12. return true;
  13. }
  14. else {
  15. document.getElementById(in_id).style.backgroundColor = oldbgcolor;
  16. return false;
  17. }
  18. }
  19. </script>
  20. ';
  21. // name of the language file that needs to be included
  22. $language_file='exercice';
  23. $nameTools= "";
  24. include('question.class.php');
  25. include('testcategory.class.php');
  26. include('../inc/global.inc.php');
  27. include('exercise.lib.php');
  28. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.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. {
  39. add_category_form(Security::remove_XSS($_GET['action']));
  40. }
  41. else if (isset($_GET['action']) && $_GET['action'] == 'editcategory') {
  42. edit_category_form(Security::remove_XSS($_GET['action']));
  43. }
  44. else if (isset($_GET['action']) && $_GET['action'] == 'deletecategory') {
  45. delete_category_form(Security::remove_XSS($_GET['action']));
  46. }
  47. else {
  48. display_add_category();
  49. display_categories();
  50. }
  51. Display::display_footer();
  52. // **********************************************************************************
  53. // ****** FUNCTIONS ********************
  54. // **********************************************************************************
  55. // ------------------------------------------------------------------------
  56. // form to edit a category
  57. // ------------------------------------------------------------------------
  58. function edit_category_form($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. // --------------------
  63. // initiate the object
  64. // --------------------
  65. $form = new FormValidator('note','post', api_get_self().'?action='.$in_action.'&category_id='.$category_id);
  66. // --------------------
  67. // settting the form elements
  68. // --------------------
  69. $form->addElement('header', '', get_lang('EditCategory'));
  70. $form->addElement('hidden', 'category_id');
  71. $form->addElement('text', 'category_name', get_lang('CategoryName'),array('size'=>'95'));
  72. $form->addElement('html_editor', 'category_description', get_lang('CategoryDescription'), null, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200'));
  73. $form->addElement('style_submit_button', 'SubmitNote', get_lang('ModifyCategory'), 'class="add"');
  74. // --------------------
  75. // setting the defaults
  76. // --------------------
  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. // --------------------
  83. // setting the rules
  84. // --------------------
  85. $form->addRule('category_name', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  86. // --------------------
  87. // The validation or display
  88. // --------------------
  89. if ($form->validate())
  90. {
  91. $check = Security::check_token('post');
  92. if ($check) {
  93. $values = $form->exportValues();
  94. $v_id = Security::remove_XSS($values['category_id']);
  95. $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
  96. $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
  97. $objcat = new Testcategory($v_id, $v_name, $v_description);
  98. if ($objcat->modifyCategory()) {
  99. Display::display_confirmation_message(get_lang('MofidfyCategoryDone'));
  100. }
  101. else {
  102. Display::display_confirmation_message(get_lang('ModifyCategoryError'));
  103. }
  104. }
  105. Security::clear_token();
  106. display_add_category();
  107. display_categories();
  108. }
  109. else
  110. {
  111. display_goback();
  112. $token = Security::get_token();
  113. $form->addElement('hidden','sec_token');
  114. $form->setConstants(array('sec_token' => $token));
  115. $form->display();
  116. display_categories();
  117. }
  118. }
  119. else {
  120. Display::display_error_message(get_lang('CannotEditCategory'));
  121. }
  122. }
  123. // ------------------------------------------------------------------------
  124. // process to delete a category
  125. // ------------------------------------------------------------------------
  126. function delete_category_form($in_action) {
  127. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  128. $category_id = Security::remove_XSS($_GET['category_id']);
  129. $catobject = new Testcategory($category_id);
  130. if ($catobject->getCategoryQuestionsNumber() == 0) {
  131. if ($catobject->removeCategory()) {
  132. Display::display_confirmation_message(get_lang('DeleteCategoryDone'));
  133. }
  134. else {
  135. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  136. }
  137. }
  138. else {
  139. Display::display_error_message(get_lang('CannotDeleteCategory'));
  140. }
  141. }
  142. else {
  143. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  144. }
  145. display_add_category();
  146. display_categories();
  147. }
  148. // ------------------------------------------------------------------------
  149. // form to add a category
  150. // ------------------------------------------------------------------------
  151. function add_category_form($in_action) {
  152. // initiate the object
  153. $form = new FormValidator('note','post', api_get_self().'?action='.$in_action);
  154. // settting the form elements
  155. $form->addElement('header', '', get_lang('AddACategory'));
  156. $form->addElement('text', 'category_name', get_lang('CategoryName'),array('size'=>'95'));
  157. $form->addElement('html_editor', 'category_description', get_lang('CategoryDescription'), null, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200'));
  158. $form->addElement('style_submit_button', 'SubmitNote', get_lang('AddTestCategory'), 'class="add"');
  159. // setting the rules
  160. $form->addRule('category_name', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  161. // The validation or display
  162. if ($form->validate())
  163. {
  164. $check = Security::check_token('post');
  165. if ($check) {
  166. $values = $form->exportValues();
  167. $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER);
  168. $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER);
  169. $objcat = new Testcategory(0, $v_name, $v_description);
  170. if ($objcat->addCategoryInBDD()) {
  171. Display::display_confirmation_message(get_lang('AddCategoryDone'));
  172. }
  173. else {
  174. Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists'));
  175. }
  176. }
  177. Security::clear_token();
  178. display_add_category();
  179. display_categories();
  180. }
  181. else
  182. {
  183. display_goback();
  184. $token = Security::get_token();
  185. $form->addElement('hidden','sec_token');
  186. $form->setConstants(array('sec_token' => $token));
  187. $form->display();
  188. display_categories();
  189. }
  190. }
  191. // ------------------------------------------------------------------------
  192. // Display add category button
  193. // ------------------------------------------------------------------------
  194. function display_add_category() {
  195. echo '<div class="actions">';
  196. echo '<a href="exercice.php?'.api_get_cidreq().'">'.Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'',32).'</a>';
  197. echo '<a href="'.api_get_self().'?action=addcategory">'.Display::return_icon('question_category.gif').'</a>';
  198. echo '</div>';
  199. echo "<br/>";
  200. }
  201. // ------------------------------------------------------------------------
  202. // Display category list
  203. // ------------------------------------------------------------------------
  204. function display_categories() {
  205. $t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  206. $sql = "SELECT * FROM $t_cattable ORDER BY title";
  207. $res = Database::query($sql, __FILE__, __LINE__);
  208. while ($row = Database::fetch_array($res)) {
  209. // le titre avec le nombre de questions qui sont dans cette catégorie
  210. $tmpobj = new Testcategory($row['id']);
  211. $nb_question = $tmpobj->getCategoryQuestionsNumber();
  212. echo '<div class="sectiontitle" id="id_cat'.$row['id'].'">';
  213. echo "<span style='float:right'>".$nb_question.get_lang('NbCategory')."</span>";
  214. echo $row['title'];
  215. echo '</div>';
  216. echo '<div class="sectioncomment">';
  217. echo $row['description'];
  218. echo '</div>';
  219. echo '<div>';
  220. echo '<a href="'.api_get_self().'?action=editcategory&amp;category_id='.$row['id'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  221. if ($nb_question > 0) {
  222. echo '<a href="javascript:void(0)" onclick="alert(\''.protectJSDialogQuote(get_lang('CannotDeleteCategory')).'\')">';
  223. echo Display::return_icon('delete_na.gif', get_lang('CannotDeleteCategory'));
  224. echo '</a>';
  225. }
  226. else {
  227. $rowname = protectJSDialogQuote($row['title']);
  228. echo '<a href="'.api_get_self().'?action=deletecategory&amp;category_id='.$row['id'].'" ';
  229. echo 'onclick="return confirmDelete(\''.protectJSDialogQuote(get_lang('DeleteCategoryAreYouSure').'['.$rowname).'] ?\', \'id_cat'.$row['id'].'\');">';
  230. echo Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  231. }
  232. echo '</div>';
  233. }
  234. }
  235. // ------------------------------------------------------------------------
  236. // display goback to category list page link
  237. // ------------------------------------------------------------------------
  238. function display_goback() {
  239. echo '<div class="actions">';
  240. echo '<a href="'.api_get_self().'">'.Display::return_icon('back.png', '', 32).' '.get_lang('BackToCategoryList').'</a>';
  241. echo '</div>';
  242. }
  243. // ------------------------------------------------------------------------
  244. // To allowed " in javascript dialog box without bad surprises
  245. // replace " with two '
  246. // ------------------------------------------------------------------------
  247. function protectJSDialogQuote($in_txt) {
  248. $res = $in_txt;
  249. $res = str_replace("'", "\'", $res);
  250. $res = str_replace('"', "\'\'", $res); // super astuce pour afficher les " dans les boite de dialogue
  251. return $res;
  252. }
  253. ?>