tests_category.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. hubert.borderiou
  5. Manage tests category page
  6. */
  7. // name of the language file that needs to be included
  8. $language_file = 'exercice';
  9. $nameTools = "";
  10. //require_once '../inc/global.inc.php';
  11. require_once 'question.class.php';
  12. $this_section = SECTION_COURSES;
  13. if (!(api_is_allowed_to_edit() || api_is_question_manager())) {
  14. api_not_allowed(true);
  15. }
  16. $type = isset($_GET['type']) ? Security::remove_XSS($_GET['type']) : 'simple';
  17. if ($type == 'global' && !(api_is_platform_admin() || api_is_question_manager())) {
  18. api_not_allowed(true);
  19. }
  20. $url = api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?type='.$type;
  21. $htmlHeadXtra[] = '
  22. <script>
  23. function confirmDelete(in_txt, in_id) {
  24. var oldbgcolor = document.getElementById(in_id).style.backgroundColor;
  25. document.getElementById(in_id).style.backgroundColor="#AAFFB0";
  26. if (confirm(in_txt)) {
  27. return true;
  28. }
  29. else {
  30. document.getElementById(in_id).style.backgroundColor = oldbgcolor;
  31. return false;
  32. }
  33. }
  34. function check() {
  35. $("#parent_id option:selected").each(function() {
  36. var id = $(this).val();
  37. var name = $(this).text();
  38. if (id != "" ) {
  39. $.ajax({
  40. async: false,
  41. url: "'.$url.'&a=exercise_category_exists",
  42. data: "id="+id,
  43. success: function(return_value) {
  44. if (return_value == 0 ) {
  45. alert("'.get_lang('CategoryDoesNotExists').'");
  46. //Deleting select option tag
  47. $("#parent_id").find("option").remove();
  48. $(".holder li").each(function () {
  49. if ($(this).attr("rel") == id) {
  50. $(this).remove();
  51. }
  52. });
  53. }
  54. }
  55. });
  56. }
  57. });
  58. }
  59. $(function() {
  60. $("#parent_id").fcbkcomplete({
  61. json_url: "'.$url.'&a=search_category_parent",
  62. maxitems: 1,
  63. addontab: false,
  64. input_min_size: 1,
  65. cache: false,
  66. complete_text:"'.get_lang('StartToType').'",
  67. firstselected: false,
  68. onselect: check,
  69. filter_selected: true,
  70. newel: true
  71. });
  72. });
  73. </script>';
  74. // Breadcrumbs
  75. $interbreadcrumb[] = array("url" => "exercice.php", "name" => get_lang('Exercices'));
  76. Display::display_header(get_lang('Category'));
  77. // Action handling: add, edit and remove
  78. if (isset($_GET['action']) && $_GET['action'] == 'addcategory') {
  79. add_category_form($_GET['action'], $type);
  80. } else if (isset($_GET['action']) && $_GET['action'] == 'addcategoryglobal') {
  81. add_category_form($_GET['action'], $type);
  82. } else if (isset($_GET['action']) && $_GET['action'] == 'editcategory') {
  83. edit_category_form($_GET['action'], $type);
  84. } else if (isset($_GET['action']) && $_GET['action'] == 'deletecategory') {
  85. delete_category_form($_GET['action'], $type);
  86. } else {
  87. display_add_category($type);
  88. display_categories($type);
  89. }
  90. Display::display_footer();
  91. // FUNCTIONS
  92. // form to edit a category
  93. function edit_category_form($in_action, $type = 'simple') {
  94. $in_action = Security::remove_XSS($in_action);
  95. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  96. $category_id = Security::remove_XSS($_GET['category_id']);
  97. $objcat = new Testcategory($category_id);
  98. // initiate the object
  99. $form = new FormValidator('note', 'post', api_get_self().'?'.api_get_cidreq().'&action='.$in_action.'&category_id='.$category_id."&type=".$type);
  100. $objcat->getForm($form, 'edit');
  101. // The validation or display
  102. if ($form->validate()) {
  103. $check = Security::check_token('post');
  104. if ($check) {
  105. $values = $form->getSubmitValues();
  106. $v_id = $values['category_id'];
  107. $v_name = $values['category_name'];
  108. $v_description = $values['category_description'];
  109. $parent_id = isset($values['parent_id']) ? $values['parent_id'] : null;
  110. $visibility = isset($values['visibility']) ? $values['visibility'] : 1;
  111. $objcat = new Testcategory($v_id, $v_name, $v_description, $parent_id, $type, null, $visibility);
  112. if ($objcat->modifyCategory()) {
  113. Display::display_confirmation_message(get_lang('MofidfyCategoryDone'));
  114. } else {
  115. Display::display_confirmation_message(get_lang('ModifyCategoryError'));
  116. }
  117. }
  118. Security::clear_token();
  119. display_add_category($type);
  120. display_categories($type);
  121. } else {
  122. display_goback($type);
  123. $token = Security::get_token();
  124. $form->addElement('hidden', 'sec_token');
  125. $form->setConstants(array('sec_token' => $token));
  126. $form->display();
  127. display_categories($type);
  128. }
  129. } else {
  130. Display::display_error_message(get_lang('CannotEditCategory'));
  131. }
  132. }
  133. // process to delete a category
  134. function delete_category_form($in_action, $type = 'simple')
  135. {
  136. $in_action = Security::remove_XSS($in_action);
  137. if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) {
  138. $category_id = Security::remove_XSS($_GET['category_id']);
  139. $catobject = new Testcategory($category_id);
  140. if ($catobject->getCategoryQuestionsNumber() == 0) {
  141. if ($catobject->removeCategory()) {
  142. Display::display_confirmation_message(get_lang('DeleteCategoryDone'));
  143. } else {
  144. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  145. }
  146. } else {
  147. Display::display_error_message(get_lang('CannotDeleteCategory'));
  148. }
  149. } else {
  150. Display::display_error_message(get_lang('CannotDeleteCategoryError'));
  151. }
  152. display_add_category($type);
  153. display_categories($type);
  154. }
  155. // Form to add a category
  156. function add_category_form($in_action, $type = 'simple')
  157. {
  158. $in_action = Security::remove_XSS($in_action);
  159. // Initiate the object
  160. $form = new FormValidator('note', 'post', api_get_self().'?'.api_get_cidreq().'&action='.$in_action."&type=".$type);
  161. // Setting the form elements
  162. $form->addElement('header', get_lang('AddACategory'));
  163. $form->addElement('text', 'category_name', get_lang('CategoryName'), array('class' => 'span6'));
  164. $form->add_html_editor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200'));
  165. $form->addElement('select', 'parent_id', get_lang('Parent'), array(), array('id' => 'parent_id'));
  166. $form->addElement('style_submit_button', 'SubmitNote', get_lang('AddTestCategory'), 'class="add"');
  167. // Setting the rules
  168. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  169. // The validation or display
  170. if ($form->validate()) {
  171. $check = Security::check_token('post');
  172. if ($check) {
  173. $values = $form->getSubmitValues();
  174. $parent_id = isset($values['parent_id']) && isset($values['parent_id'][0]) ? $values['parent_id'][0] : null;
  175. $objcat = new Testcategory(0, $values['category_name'], $values['category_description'], $parent_id, $type, api_get_course_int_id());
  176. if ($objcat->addCategoryInBDD()) {
  177. Display::display_confirmation_message(get_lang('AddCategoryDone'));
  178. } else {
  179. Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists'));
  180. }
  181. }
  182. Security::clear_token();
  183. display_add_category($type);
  184. display_categories($type);
  185. } else {
  186. display_goback($type);
  187. $token = Security::get_token();
  188. $form->addElement('hidden', 'sec_token');
  189. $form->setConstants(array('sec_token' => $token));
  190. $form->display();
  191. }
  192. }
  193. // Display add category button
  194. function display_add_category($type) {
  195. echo '<div class="actions">';
  196. echo '<a href="exercice.php?' . api_get_cidreq() . '">' . Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM) . '</a>';
  197. $icon = "question_category.gif";
  198. if ($type == 'global') {
  199. $icon = "folder_global_category_new.png";
  200. }
  201. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=addcategory&type='.$type.'">'.Display::return_icon($icon, get_lang('AddACategory'), array(), ICON_SIZE_MEDIUM).'</a>';
  202. echo '</div>';
  203. echo "<br/>";
  204. if ($type == 'simple') {
  205. echo "<fieldset><legend>" . get_lang('QuestionCategory') . "</legend></fieldset>";
  206. } else {
  207. echo "<fieldset><legend>".get_lang('QuestionGlobalCategory')."</legend></fieldset>";
  208. }
  209. }
  210. // Display category list
  211. function display_categories($type = 'simple')
  212. {
  213. $options = array(
  214. 'decorate' => true,
  215. 'rootOpen' => '<ul>',
  216. 'rootClose' => '</ul>',
  217. 'childOpen' => '<li>',
  218. 'childClose' => '</li>',
  219. 'nodeDecorator' => function($row) use ($type) {
  220. $category_id = $row['iid'];
  221. $courseId = $row['cId'];
  222. $tmpobj = new Testcategory($category_id);
  223. $nb_question = $tmpobj->getCategoryQuestionsNumber();
  224. $nb_question_label = $nb_question == 1 ? $nb_question . ' ' . get_lang('Question') : $nb_question . ' ' . get_lang('Questions');
  225. $nb_question_label = Display::label($nb_question_label, 'info');
  226. $actions = null;
  227. if ($courseId == 0 && $type == 'simple') {
  228. $actions .= Display::return_icon('edit_na.png', get_lang('Edit'), array(), ICON_SIZE_SMALL);
  229. } else {
  230. $actions .= '<a href="'.api_get_self().'?action=editcategory&category_id='.$category_id.'&type='.$type.'">'.Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
  231. }
  232. if ($nb_question > 0 && $courseId == 0 && $type == 'simple') {
  233. $actions .= '<a href="javascript:void(0)" onclick="alert(\'' . protectJSDialogQuote(get_lang('CannotDeleteCategory')) . '\')">';
  234. $actions .= Display::return_icon('delete_na.png', get_lang('CannotDeleteCategory'), array(), ICON_SIZE_SMALL);
  235. $actions .='</a>';
  236. } else {
  237. $rowname = protectJSDialogQuote($row['title']);
  238. $actions .= ' <a href="'.api_get_self().'?action=deletecategory&amp;category_id='.$category_id.'&type='.$type.'"';
  239. $actions .= 'onclick="return confirmDelete(\''.protectJSDialogQuote(get_lang('DeleteCategoryAreYouSure').'['.$rowname).'] ?\', \'id_cat'.$category_id.'\');">';
  240. $actions .= Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>';
  241. }
  242. return $row['title'].' '.$nb_question_label.' '.$actions;
  243. }
  244. //'representationField' => 'slug',
  245. //'html' => true
  246. );
  247. // @todo put this in a function
  248. $repo = Database::getManager()->getRepository('ChamiloCoreBundle:CQuizCategory');
  249. $query = null;
  250. if ($type == 'global') {
  251. $query = Database::getManager()
  252. ->createQueryBuilder()
  253. ->select('node')
  254. ->from('ChamiloCoreBundle:CQuizCategory', 'node')
  255. ->where('node.cId = 0')
  256. ->orderBy('node.root, node.lft', 'ASC')
  257. ->getQuery();
  258. } else {
  259. $query = Database::getManager()
  260. ->createQueryBuilder()
  261. ->select('node')
  262. ->from('ChamiloCoreBundle:CQuizCategory', 'node')
  263. ->where('node.cId = :courseId')
  264. //->add('orderBy', 'node.title ASC')
  265. ->orderBy('node.root, node.lft', 'ASC')
  266. ->setParameter('courseId', api_get_course_int_id())
  267. ->getQuery();
  268. }
  269. $htmlTree = $repo->buildTree($query->getArrayResult(), $options);
  270. /*
  271. $htmlTree = $repo->childrenHierarchy(
  272. null, //starting from root nodes
  273. false, //load all children, not only direct
  274. $options
  275. );*/
  276. echo $htmlTree;
  277. return true;
  278. }
  279. // display goback to category list page link
  280. function display_goback($type) {
  281. $type = Security::remove_XSS($type);
  282. echo '<div class="actions">';
  283. echo '<a href="'.api_get_self().'?type='.$type.'">'.Display::return_icon('back.png', get_lang('BackToCategoryList'), array(), 32).'</a>';
  284. echo '</div>';
  285. }
  286. // To allowed " in javascript dialog box without bad surprises
  287. // replace " with two '
  288. function protectJSDialogQuote($in_txt) {
  289. $res = $in_txt;
  290. $res = str_replace("'", "\'", $res);
  291. $res = str_replace('"', "\'\'", $res); // super astuce pour afficher les " dans les boite de dialogue
  292. return $res;
  293. }