question_pool.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Question Pool
  5. * This script allows administrators to manage questions and add them into their exercises.
  6. * One question can be in several exercises
  7. * @package chamilo.exercise
  8. * @author Olivier Brouckaert
  9. * @author Julio Montoya adding support to query all questions from all session, courses, exercises
  10. * @author Modify by hubert borderiou 2011-10-21 Question's category
  11. */
  12. /**
  13. * Code
  14. */
  15. // name of the language file that needs to be included
  16. use \ChamiloSession as Session;
  17. $language_file = 'exercice';
  18. require_once 'exercise.class.php';
  19. require_once 'question.class.php';
  20. require_once 'answer.class.php';
  21. require_once '../inc/global.inc.php';
  22. require_once 'exercise.lib.php';
  23. $this_section = SECTION_COURSES;
  24. $is_allowedToEdit=api_is_allowed_to_edit(null,true);
  25. if (empty($delete)) {
  26. $delete = intval($_GET['delete']);
  27. }
  28. if ( empty ( $recup ) ) {
  29. $recup = intval($_GET['recup']);
  30. }
  31. if ( empty ( $fromExercise ) ) {
  32. $fromExercise = intval($_REQUEST['fromExercise']);
  33. }
  34. if(isset($_GET['exerciseId'])){
  35. $exerciseId = intval($_GET['exerciseId']);
  36. }
  37. if (isset($_GET['courseCategoryId'])) {
  38. $courseCategoryId = intval($_GET['courseCategoryId']);
  39. }
  40. $exerciseLevel = -1;
  41. if(isset($_REQUEST['exerciseLevel'])){
  42. $exerciseLevel = intval($_REQUEST['exerciseLevel']);
  43. }
  44. if(isset($_GET['answerType'])){
  45. $answerType = intval($_REQUEST['answerType']);
  46. }
  47. $page = 0;
  48. if(!empty($_GET['page'])){
  49. $page = intval($_GET['page']);
  50. }
  51. $copy_question = 0;
  52. if(!empty($_GET['copy_question'])){
  53. $copy_question = intval($_GET['copy_question']);
  54. }
  55. $session_id = intval($_GET['session_id']);
  56. $selected_course = intval($_GET['selected_course']);
  57. $course_id_changed = intval($_GET['course_id_changed']); // save the id of the previous course selected by user to reset menu if we detect that user change course hub 13-10-2011
  58. $exercice_id_changed = intval($_GET['exercice_id_changed']); // save the id of the previous exercice selected by user to reset menu if we detect that user change course hub 13-10-2011
  59. // by default when we go to the page for the first time, we select the current course
  60. if (!isset($_GET['selected_course']) && !isset($_GET['exerciseId'])) {
  61. $selected_course = api_get_course_int_id();
  62. }
  63. // document path
  64. $documentPath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  65. // picture path
  66. $picturePath = $documentPath.'/images';
  67. if(!($objExcercise instanceOf Exercise) && !empty($fromExercise)) {
  68. $objExercise = new Exercise();
  69. $objExercise->read($fromExercise);
  70. }
  71. $nameTools = get_lang('QuestionPool');
  72. $interbreadcrumb[] = array("url" => "exercice.php","name" => get_lang('Exercices'));
  73. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$objExercise->id, "name" => $objExercise->name);
  74. $displayMessage = ""; // messag to be displayed if actions succesfull
  75. if ($is_allowedToEdit) {
  76. //Duplicating a Question
  77. if (!isset($_POST['recup']) && $copy_question != 0 && isset($fromExercise)) {
  78. $origin_course_id = intval($_GET['course_id']);
  79. $origin_course_info = api_get_course_info_by_id($origin_course_id);
  80. $current_course = api_get_course_info();
  81. $old_question_id = $copy_question;
  82. //Reading the source question
  83. $old_question_obj = Question::read($old_question_id, $origin_course_id);
  84. if ($old_question_obj) {
  85. $old_question_obj->updateTitle($old_question_obj->selectTitle().' - '.get_lang('Copy'));
  86. //Duplicating the source question, in the current course
  87. $new_id = $old_question_obj->duplicate($current_course);
  88. //Reading new question
  89. $new_question_obj = Question::read($new_id);
  90. $new_question_obj->addToList($fromExercise);
  91. //Reading Answers obj of the current course
  92. $new_answer_obj = new Answer($old_question_id, $origin_course_id);
  93. $new_answer_obj->read();
  94. //Duplicating the Answers in the current course
  95. $new_answer_obj->duplicate($new_id, $current_course);
  96. // destruction of the Question object
  97. unset($new_question_obj);
  98. unset($old_question_obj);
  99. if (!$objExcercise instanceOf Exercise) {
  100. $objExercise = new Exercise();
  101. $objExercise->read($fromExercise);
  102. }
  103. Session::write('objExercise', $objExercise);
  104. }
  105. $displayMessage = get_lang('ItemAdded');
  106. }
  107. // deletes a question from the database and all exercises
  108. if ($delete) {
  109. // construction of the Question object
  110. // if the question exists
  111. if ($objQuestionTmp = Question::read($delete)) {
  112. // deletes the question from all exercises
  113. $objQuestionTmp->delete();
  114. }
  115. // destruction of the Question object
  116. unset($objQuestionTmp);
  117. } elseif($recup && $fromExercise) {
  118. // gets an existing question and copies it into a new exercise
  119. $objQuestionTmp = Question :: read($recup);
  120. // if the question exists
  121. if($objQuestionTmp = Question :: read($recup)) {
  122. // adds the exercise ID represented by $fromExercise into the list of exercises for the current question
  123. $objQuestionTmp->addToList($fromExercise);
  124. }
  125. // destruction of the Question object
  126. unset($objQuestionTmp);
  127. if (!$objExcercise instanceOf Exercise) {
  128. $objExercise = new Exercise();
  129. $objExercise->read($fromExercise);
  130. }
  131. // adds the question ID represented by $recup into the list of questions for the current exercise
  132. $objExercise->addToList($recup);
  133. Session::write('objExercise',$objExercise);
  134. // header("Location: admin.php?".api_get_cidreq()."&exerciseId=$fromExercise");
  135. // exit();
  136. }
  137. else if( isset($_POST['recup']) && is_array($_POST['recup']) && $fromExercise) {
  138. $list_recup = $_POST['recup'];
  139. foreach ($list_recup as $course_id => $question_data) {
  140. $origin_course_id = intval($course_id);
  141. $origin_course_info = api_get_course_info_by_id($origin_course_id);
  142. $current_course = api_get_course_info();
  143. foreach ($question_data as $old_question_id) {
  144. /*
  145. $recup = intval($recup);
  146. // if the question exists
  147. if($objQuestionTmp = Question :: read($recup)) {
  148. // adds the exercise ID represented by $fromExercise into the list of exercises for the current question
  149. $objQuestionTmp->addToList($fromExercise);
  150. }
  151. // destruction of the Question object
  152. unset($objQuestionTmp);
  153. if(!$objExcercise instanceOf Exercise) {
  154. $objExercise = new Exercise();
  155. $objExercise->read($fromExercise);
  156. }
  157. // adds the question ID represented by $recup into the list of questions for the current exercise
  158. $objExercise->addToList($recup);
  159. */
  160. //Reading the source question
  161. $old_question_obj = Question::read($old_question_id, $origin_course_id);
  162. if ($old_question_obj) {
  163. $old_question_obj->updateTitle($old_question_obj->selectTitle().' - '.get_lang('Copy'));
  164. //Duplicating the source question, in the current course
  165. $new_id = $old_question_obj->duplicate($current_course);
  166. //Reading new question
  167. $new_question_obj = Question::read($new_id);
  168. $new_question_obj->addToList($fromExercise);
  169. //Reading Answers obj of the current course
  170. $new_answer_obj = new Answer($old_question_id, $origin_course_id);
  171. $new_answer_obj->read();
  172. //Duplicating the Answers in the current course
  173. $new_answer_obj->duplicate($new_id, $current_course);
  174. // destruction of the Question object
  175. unset($new_question_obj);
  176. unset($old_question_obj);
  177. if (!$objExcercise instanceOf Exercise) {
  178. $objExercise = new Exercise();
  179. $objExercise->read($fromExercise);
  180. }
  181. }
  182. }
  183. }
  184. Session::write('objExercise',$objExercise);
  185. // header("Location: admin.php?".api_get_cidreq()."&exerciseId=$fromExercise");
  186. // exit();
  187. }
  188. }
  189. if (isset($_SESSION['gradebook'])){
  190. $gradebook= $_SESSION['gradebook'];
  191. }
  192. if (!empty($gradebook) && $gradebook=='view') {
  193. $interbreadcrumb[]= array ('url' => '../gradebook/'.Security::remove_XSS($_SESSION['gradebook_dest']),'name' => get_lang('ToolGradebook'));
  194. }
  195. // if admin of course
  196. if (!$is_allowedToEdit) {
  197. api_not_allowed(true);
  198. }
  199. $confirmYourChoice = addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset));
  200. $htmlHeadXtra[] = "
  201. <script type='text/javascript'>
  202. function submit_form(obj) {
  203. document.question_pool.submit();
  204. }
  205. function mark_course_id_changed() {
  206. $('#course_id_changed').val('1');
  207. }
  208. function mark_exercice_id_changed() {
  209. $('#exercice_id_changed').val('1');
  210. }
  211. function confirm_your_choice() {
  212. return confirm('$confirmYourChoice');
  213. }
  214. </script>
  215. ";
  216. Display::display_header($nameTools,'Exercise');
  217. // Menu
  218. echo '<div class="actions">';
  219. if (isset($type)) {
  220. $url = api_get_self().'?type=1';
  221. } else {
  222. $url = api_get_self();
  223. }
  224. if (isset($fromExercise) && $fromExercise > 0) {
  225. echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$fromExercise.'">'.Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  226. $titleAdd = get_lang('AddQuestionToTest');
  227. }
  228. else {
  229. echo '<a href="exercice.php?'.api_get_cidReq().'">'.Display::return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
  230. echo "<a href='admin.php?exerciseId=0'>".Display::return_icon('add_question.gif', get_lang('NewQu'), '', 32)."</a>";
  231. $titleAdd = get_lang('ManageAllQuestions');
  232. }
  233. echo '</div>';
  234. if ($displayMessage != "") {
  235. Display::display_confirmation_message($displayMessage);
  236. $displayMessage = "";
  237. }
  238. //Form
  239. echo '<form class="form-horizontal" name="question_pool" method="GET" action="'.$url.'">';
  240. //Title
  241. echo '<legend>'.$nameTools.' - '.$titleAdd.'</legend>';
  242. if (isset($type)) {
  243. echo '<input type="hidden" name="type" value="1">';
  244. }
  245. echo '<input type="hidden" name="fromExercise" value="'.$fromExercise.'">';
  246. // Session list, if sessions are used.
  247. $session_list = SessionManager::get_sessions_by_general_coach(api_get_user_id());
  248. $tabAttrParam = array('class'=>'chzn-select', 'onchange'=>'submit_form(this)'); // when sessions are used
  249. $labelFormRow = get_lang('Session');
  250. $session_select_list = array();
  251. foreach($session_list as $item) {
  252. $session_select_list[$item['id']] = $item['name'];
  253. }
  254. $select_session_html = Display::select('session_id', $session_select_list, $session_id, $tabAttrParam);
  255. echo Display::form_row($labelFormRow, $select_session_html); // hub 13-10-2011
  256. // Course list, get course list of session, or for course where user is admin
  257. if (!empty($session_id) && $session_id != '-1') {
  258. $course_list = SessionManager::get_course_list_by_session_id($session_id);
  259. } else {
  260. $course_list = CourseManager::get_course_list_of_user_as_course_admin(api_get_user_id());
  261. }
  262. $course_select_list = array();
  263. foreach ($course_list as $item) {
  264. $course_select_list[$item['id']] = "";
  265. if ($item['id'] == api_get_course_int_id()) {
  266. $course_select_list[$item['id']] = ">&nbsp;&nbsp;&nbsp;&nbsp;";
  267. }
  268. $course_select_list[$item['id']] .= $item['title'];
  269. }
  270. $select_course_html = Display::select('selected_course', $course_select_list, $selected_course, array('class'=>'chzn-select','onchange'=>'mark_course_id_changed(); submit_form(this);'));
  271. echo Display::form_row(get_lang('Course'), $select_course_html);
  272. if (empty($selected_course) || $selected_course == '-1') {
  273. $course_info = api_get_course_info();
  274. reset_menu_exo_lvl_type(); // no course selected, reset menu test / difficult� / type de reponse // hub 13-10-2011
  275. }
  276. else {
  277. $course_info = CourseManager::get_course_information_by_id($selected_course);
  278. }
  279. // If course has changed, reset the menu default
  280. if ($course_id_changed) {
  281. reset_menu_exo_lvl_type();
  282. }
  283. $course_id = $course_info['real_id'];
  284. //Redefining table calls
  285. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  286. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  287. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  288. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  289. $TBL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); // hub 13-10-2011
  290. $TBL_COURSE_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); // hub 13-10-2011
  291. // Get course categories for the selected course
  292. // get category list for the course $selected_course
  293. $tabCatList = Testcategory::getCategoriesIdAndName($selected_course);
  294. $selectCourseCateogry = Display::select('courseCategoryId', $tabCatList, $courseCategoryId, array('class'=>'chzn-select','onchange'=>'submit_form(this);'), false);
  295. echo Display::form_row(get_lang("QuestionCategory"), $selectCourseCateogry);
  296. // Get exercice list for this course
  297. $exercise_list = get_all_exercises_for_course_id($course_info, $session_id, $selected_course);
  298. //Exercise List
  299. $my_exercise_list = array();
  300. $my_exercise_list['0'] = get_lang('AllExercises');
  301. $my_exercise_list['-1'] = get_lang('OrphanQuestions');
  302. if (is_array($exercise_list)) {
  303. foreach($exercise_list as $row) {
  304. $my_exercise_list[$row['id']] = "";
  305. if ($row['id'] == $fromExercise && $selected_course == api_get_course_int_id()) {
  306. $my_exercise_list[$row['id']] = ">&nbsp;&nbsp;&nbsp;&nbsp;"; // hub 13-10-2011
  307. }
  308. $my_exercise_list[$row['id']] .= $row['title'];
  309. }
  310. }
  311. if ($exercice_id_changed == 1) {
  312. reset_menu_lvl_type();
  313. }
  314. $select_exercise_html = Display::select('exerciseId', $my_exercise_list, $exerciseId, array('class'=>'chzn-select','onchange'=>'mark_exercice_id_changed(); submit_form(this);'), false);
  315. echo Display::form_row(get_lang('Exercise'), $select_exercise_html);
  316. // Difficulty list (only from 0 to 5)
  317. $select_difficulty_html = Display::select('exerciseLevel', array(-1 => get_lang('All'), 0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5), $exerciseLevel, array('class'=>'chzn-select', 'onchange'=>'submit_form(this);'), false);
  318. echo Display::form_row(get_lang('Difficulty'), $select_difficulty_html);
  319. // Answer type
  320. $question_list = Question::get_question_type_list();
  321. $new_question_list = array();
  322. $new_question_list['-1'] = get_lang('All');
  323. $objExercise = new Exercise();
  324. $objExercise->read($fromExercise);
  325. foreach ($question_list as $key=>$item) {
  326. if ($objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  327. if (!in_array($key, array(HOT_SPOT_DELINEATION, UNIQUE_ANSWER))) {
  328. continue;
  329. }
  330. $new_question_list[$key] = get_lang($item[1]);
  331. } else {
  332. if ($key == HOT_SPOT_DELINEATION) {
  333. continue;
  334. }
  335. $new_question_list[$key] = get_lang($item[1]);
  336. }
  337. }
  338. //Answer type list
  339. $select_answer_html = Display::select('answerType', $new_question_list, $answerType, array('class'=>'chzn-select','onchange'=>'submit_form(this);'), false);
  340. echo Display::form_row(get_lang('AnswerType'), $select_answer_html);
  341. $button = '<button class="save" type="submit" name="name" value="'.get_lang('Filter').'">'.get_lang('Filter').'</button>';
  342. echo Display::form_row('', $button);
  343. echo "<input type='hidden' id='course_id_changed' name='course_id_changed' value='0' />";
  344. echo "<input type='hidden' id='exercice_id_changed' name='exercice_id_changed' value='0' />";
  345. ?>
  346. </form>
  347. <div class="clear"></div>
  348. <form method="post" action="<?php echo $url.'?'.api_get_cidreq().'&fromExercise='.$fromExercise; ?>" >
  349. <?php
  350. echo '<input type="hidden" name="course_id" value="'.$selected_course.'">';
  351. // if we have selected an exercise in the list-box 'Filter'
  352. if ($exerciseId > 0) {
  353. $where = '';
  354. $from = '';
  355. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  356. $from = ", $TBL_COURSE_REL_CATEGORY crc ";
  357. $where .= " AND crc.c_id = $selected_course AND crc.question_id=qu.id AND crc.category_id=$courseCategoryId";
  358. }
  359. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  360. $where .= ' AND level='.$exerciseLevel;
  361. }
  362. if (isset($answerType) && $answerType > 0) {
  363. $where .= ' AND type='.$answerType;
  364. }
  365. $sql = "SELECT DISTINCT
  366. id,question,
  367. type,
  368. level
  369. FROM
  370. $TBL_EXERCICE_QUESTION qt,
  371. $TBL_QUESTIONS qu
  372. $from
  373. WHERE
  374. qt.question_id=qu.id
  375. AND qt.exercice_id = $exerciseId
  376. AND qt.c_id = $selected_course
  377. AND qu.c_id = $selected_course
  378. $where
  379. ORDER BY
  380. question_order";
  381. $result=Database::query($sql);
  382. while($row = Database::fetch_array($result, 'ASSOC')) {
  383. $main_question_list[] = $row;
  384. }
  385. } elseif ($exerciseId == -1) {
  386. //
  387. // if we have selected the option 'Orphan questions' in the list-box 'Filter'
  388. //
  389. $level_where = '';
  390. $from = '';
  391. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  392. $from = ", $TBL_COURSE_REL_CATEGORY crc ";
  393. $level_where .= " AND crc.c_id=$selected_course AND crc.question_id=qu.id AND crc.category_id = $courseCategoryId";
  394. }
  395. if (isset($exerciseLevel) && $exerciseLevel!= -1 ) {
  396. $level_where = ' AND level='.$exerciseLevel;
  397. }
  398. $answer_where = '';
  399. if (isset($answerType) && $answerType >0 -1 ) {
  400. $answer_where = ' AND type='.$answerType;
  401. }
  402. $sql = "SELECT DISTINCT * FROM $TBL_QUESTIONS qu $from
  403. WHERE qu.c_id=$selected_course AND qu.id NOT IN (
  404. SELECT question_id FROM $TBL_EXERCICE_QUESTION
  405. WHERE c_id=$selected_course
  406. ) $level_where $answer_where";
  407. $result = Database::query($sql);
  408. while($row = Database::fetch_array($result, 'ASSOC')) {
  409. $main_question_list[] = $row;
  410. }
  411. } else {
  412. //
  413. // All tests for selected course
  414. //
  415. // if we have not selected any option in the list-box 'Filter'
  416. $filter = '';
  417. $from = '';
  418. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  419. $from = ", $TBL_COURSE_REL_CATEGORY crc ";
  420. $filter .= " AND crc.c_id=$selected_course AND crc.question_id=qu.id AND crc.category_id=$courseCategoryId";
  421. }
  422. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  423. $filter .= ' AND level='.$exerciseLevel.' ';
  424. }
  425. if (isset($answerType) && $answerType > 0) {
  426. $filter .= ' AND qu.type='.$answerType.' ';
  427. }
  428. if (!empty($session_id) && $session_id != '-1') {
  429. $main_question_list = array();
  430. if (!empty($course_list))
  431. foreach ($course_list as $course_item) {
  432. if (!empty($selected_course) && $selected_course != '-1') {
  433. if ($selected_course != $course_item['id']) {
  434. continue;
  435. }
  436. }
  437. $exercise_list = get_all_exercises($course_item, $session_id);
  438. if (!empty($exercise_list)) {
  439. foreach ($exercise_list as $exercise) {
  440. $my_exercise = new Exercise($course_item['id']);
  441. $my_exercise->read($exercise['id']);
  442. if (!empty($my_exercise)) {
  443. if (!empty($my_exercise->questionList)) {
  444. foreach ($my_exercise->questionList as $question_id) {
  445. $question_obj = Question::read($question_id, $course_item['id']);
  446. if ($exerciseLevel != '-1')
  447. if ($exerciseLevel != $question_obj->level) {
  448. continue;
  449. }
  450. if ($answerType > 0)
  451. if ($answerType != $question_obj->type) {
  452. continue;
  453. }
  454. $category_list = Testcategory::getCategoryForQuestion($question_obj->id, $selected_course);
  455. if ($courseCategoryId > 0 && !empty($category_list)) {
  456. continue;
  457. }
  458. if ($objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_DIRECT) {
  459. if ($question_obj->type == HOT_SPOT_DELINEATION) {
  460. continue;
  461. }
  462. }
  463. $question_row = array( 'id' => $question_obj->id,
  464. 'question' => $question_obj->question,
  465. 'type' => $question_obj->type,
  466. 'level' => $question_obj->level,
  467. 'exercise_id' => $exercise['id'],
  468. 'course_id' => $course_item['id'],
  469. );
  470. $main_question_list[] = $question_row;
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. }
  478. else {
  479. //
  480. // All tests for the course selected, not in session
  481. //
  482. $sql = "SELECT DISTINCT qu.id, question, qu.type, level, q.session_id FROM $TBL_QUESTIONS as qu, $TBL_EXERCICE_QUESTION as qt, $TBL_EXERCICES as q $from WHERE qu.c_id=$selected_course AND qt.c_id=$selected_course AND q.c_id=$selected_course AND qu.id = qt.question_id AND q.id = qt.exercice_id $filter ORDER BY session_id ASC";
  483. $result = Database::query($sql);
  484. while($row = Database::fetch_array($result, 'ASSOC')) {
  485. $main_question_list[] = $row;
  486. }
  487. }
  488. // forces the value to 0
  489. $exerciseId=0;
  490. }
  491. $nbrQuestions = count($main_question_list);
  492. // build the line of the array to display questions
  493. // Actions are different if you launch the question_pool page
  494. // They are different too if you have displayed questions from your course
  495. // Or from another course you are the admin(or session admin)
  496. // from a test or not
  497. /*
  498. +--------------------------------------------+--------------------------------------------+
  499. | NOT IN A TEST | IN A TEST |
  500. +----------------------+---------------------+---------------------+----------------------+
  501. |IN THE COURSE (*) "x | NOT IN THE COURSE o | IN THE COURSE + | NOT IN THE COURSE o |
  502. +----------------------+---------------------+---------------------+----------------------+
  503. |Edit the question | Do nothing | Add question to test|Clone question in test|
  504. |Delete the question | | | |
  505. |(true delete) | | | |
  506. +----------------------+---------------------+---------------------+----------------------+
  507. (*) this is the only way to delete or modify orphan questions
  508. */
  509. //
  510. if ($fromExercise <= 0) { // NOT IN A TEST - IN THE COURSE
  511. if ($selected_course == api_get_course_int_id()) {
  512. $actionLabel = get_lang('Modify');
  513. $actionIcon1 = "edit";
  514. $actionIcon2 = "delete";
  515. $questionTagA = 1; // we are in the course, question title can be a link to the question edit page
  516. }
  517. else { // NOT IN A TEST - NOT IN THE COURSE
  518. $actionLabel = get_lang('Reuse');
  519. $actionIcon1 = get_lang('MustBeInATest');
  520. $actionIcon2 = "";
  521. $questionTagA = 0; // we are not in this course, to messy if we link to the question in another course
  522. }
  523. }
  524. else { // IN A TEST - IN THE COURSE
  525. if ($selected_course == api_get_course_int_id()) {
  526. $actionLabel = get_lang('Reuse');
  527. $actionIcon1 = "add";
  528. $actionIcon2 = "";
  529. $questionTagA = 1;
  530. }
  531. else { // IN A TEST - NOT IN THE COURSE
  532. $actionLabel = get_lang('Reuse');
  533. $actionIcon1 = "clone";
  534. $actionIcon2 = "";
  535. $questionTagA = 0;
  536. }
  537. }
  538. //
  539. // display table
  540. //
  541. $header = array();
  542. $header[] = array(get_lang('QuestionUpperCaseFirstLetter'), false, array("style"=>"text-align:center"), '');
  543. $header[] = array(get_lang('Type'), false, array("style"=>"text-align:center"), array("style"=>"text-align:center"), '');
  544. $header[] = array(get_lang('QuestionCategory'), false, array("style"=>"text-align:center"), array("style"=>"text-align:center"), '');
  545. $header[] = array(get_lang('Difficulty'), false, array("style"=>"text-align:center"), array("style"=>"text-align:center"), '');
  546. $header[] = array($actionLabel, false, array("style"=>"text-align:center"), array("style"=>"text-align:center"), '');
  547. $data = array();
  548. if (is_array($main_question_list)) {
  549. $all_category_list = Testcategory::getCategoryListName($selected_course);
  550. foreach ($main_question_list as $tabQuestion) {
  551. $row = array();
  552. //This function checks if the question can be read
  553. $question_type = get_question_type_for_question($selected_course, $tabQuestion['id']);
  554. if (empty($question_type)) {
  555. continue;
  556. }
  557. $row[] = get_a_tag_for_question($questionTagA, $fromExercise, $tabQuestion['id'], $tabQuestion['type'], $tabQuestion['question']);
  558. $row[] = $question_type;
  559. $row[] = get_question_categorie_for_question($selected_course, $tabQuestion['id']);
  560. $row[] = $tabQuestion['level'];
  561. $row[] = get_action_icon_for_question($actionIcon1,
  562. $fromExercise,
  563. $tabQuestion['id'],
  564. $tabQuestion['type'],
  565. $tabQuestion['question'],
  566. $selected_course,
  567. $courseCategoryId,
  568. $exerciseLevel,
  569. $answerType,
  570. $session_id, $exerciseId).
  571. "&nbsp;".
  572. get_action_icon_for_question($actionIcon2, $fromExercise, $tabQuestion['id'], $tabQuestion['type'],
  573. $tabQuestion['question'], $selected_course, $courseCategoryId, $exerciseLevel, $answerType,
  574. $session_id, $exerciseId);
  575. $data[] = $row;
  576. }
  577. }
  578. Display :: display_sortable_table($header, $data, '', array('per_page_default'=>999,'per_page'=>999,'page_nr'=>1));
  579. if (!$nbrQuestions) {
  580. echo get_lang('NoQuestion');
  581. }
  582. // The (+) system as now make this button useless
  583. // Hubert Borderiou 27-10-2011
  584. //if (api_get_session_id() == 0){
  585. // echo '<div style="width:100%; border-top:1px dotted #4171B5;"><button class="save" type="submit">'.get_lang('Reuse').'</button></div></form>';
  586. //}
  587. Display::display_footer();
  588. // Some functions here, just for question_pool to ease the code
  589. /*
  590. Put the menu entry for level and type to default "Choice"
  591. It is usefull if you change the exercice, you need to reset the other menus
  592. hubert.borderiou 13-10-2011
  593. */
  594. function reset_menu_lvl_type() {
  595. global $exerciseLevel, $answerType;
  596. $answerType = -1;
  597. $exerciseLevel = -1;
  598. }
  599. /*
  600. Put the menu entry for exercice and level and type to default "Choice"
  601. It is usefull if you change the course, you need to reset the other menus
  602. hubert.borderiou 13-10-2011
  603. */
  604. function reset_menu_exo_lvl_type() {
  605. global $exerciseId, $courseCategoryId;
  606. reset_menu_lvl_type();
  607. $exerciseId = 0;
  608. $courseCategoryId = 0;
  609. }
  610. //
  611. // return the <a> link to admin question, if needed
  612. // hubert.borderiou 13-10-2011
  613. function get_a_tag_for_question($in_addA, $in_fromex, $in_questionid, $in_questiontype, $in_questionname) {
  614. $res = $in_questionname;
  615. if ($in_addA) {
  616. $res = "<a href='admin.php?".api_get_cidreq()."&editQuestion=$in_questionid&type=$in_questiontype&fromExercise=$in_fromex'>".$res."</a>";
  617. }
  618. return $res;
  619. }
  620. /*
  621. $row[] = get_action_icon_for_question(
  622. $actionIcon1, --
  623. $fromExercise, --
  624. $tabQuestion['id'], --
  625. $tabQuestion['type'],
  626. $tabQuestion['question'],
  627. $selected_course,
  628. $courseCategoryId,
  629. $exerciseLevel,
  630. $answerType,
  631. $session_id,
  632. $exerciseId).
  633. "&nbsp;".
  634. get_action_icon_for_question($actionIcon2, $fromExercise, $tabQuestion['id'], $tabQuestion['type'],
  635. $tabQuestion['question'], $selected_course, $courseCategoryId, $exerciseLevel, $answerType,
  636. $session_id, $exerciseId);
  637. */
  638. //
  639. // return the <a> html code for delete, add, clone, edit a question
  640. // hubert.borderiou 13-10-2011
  641. // in_action = the code of the action triggered by the button
  642. // from_exercice = the id of the current exercice from which we click on question pool
  643. // in_questionid = the id of the current question
  644. // in_questiontype = the code of the type of the current question
  645. // in_questionname = the name of the question
  646. // in_selected_course = the if of the course chosen in the FILTERING MENU
  647. // in_courseCategoryId = the id of the category chosen in the FILTERING MENU
  648. // in_exerciseLevel = the level of the exercice chosen in the FILTERING MENU
  649. // in_answerType = the code of the type of the question chosen in the FILTERING MENU
  650. // in_session_id = the id of the session_id chosen in the FILTERING MENU
  651. // in_exercice_id = the id of the exercice chosen in the FILTERING MENU
  652. function get_action_icon_for_question($in_action, $from_exercice, $in_questionid, $in_questiontype, $in_questionname,
  653. $in_selected_course, $in_courseCategoryId, $in_exerciseLevel, $in_answerType, $in_session_id, $in_exercice_id
  654. ) {
  655. $res = "";
  656. $getParams = "&selected_course=$in_selected_course&courseCategoryId=$in_courseCategoryId&exerciseId=$in_exercice_id&exerciseLevel=$in_exerciseLevel&answerType=$in_answerType&session_id=$in_session_id";
  657. switch ($in_action) {
  658. case "delete" :
  659. $res = "<a href='".api_get_self()."?".api_get_cidreq()."&delete=$in_questionid$getParams' onclick='return confirm_your_choice()'>";
  660. $res .= Display::return_icon("delete.gif", get_lang('Delete'));
  661. $res .= "</a>";
  662. break;
  663. case "edit" :
  664. $res = get_a_tag_for_question(1, $from_exercice, $in_questionid, $in_questiontype, Display::return_icon("edit.gif", get_lang('Modify')));
  665. break;
  666. case "add":
  667. // add if question is not already in test
  668. $myObjEx = new Exercise();
  669. $myObjEx->read($from_exercice);
  670. if (!$myObjEx->isInList($in_questionid)) {
  671. $res = "<a href='".api_get_self()."?".api_get_cidreq()."&recup=$in_questionid&fromExercise=$from_exercice$getParams'>";
  672. $res .= Display::return_icon("view_more_stats.gif", get_lang('InsertALinkToThisQuestionInTheExercise'));
  673. $res .= "</a>";
  674. }
  675. else {
  676. $res = "-";
  677. }
  678. unset($myObjEx);
  679. break;
  680. case "clone":
  681. $res = "<a href='".api_get_self()."?".api_get_cidreq()."&amp;copy_question=$in_questionid&course_id=$in_selected_course&fromExercise=$from_exercice$getParams'>";
  682. $res .= Display::return_icon('cd.gif', get_lang('ReUseACopyInCurrentTest'));
  683. $res .= "</a>";
  684. break;
  685. default :
  686. $res = $in_action;
  687. break;
  688. }
  689. return $res;
  690. }
  691. // return the icon for the question type
  692. // hubert.borderiou 13-10-2011
  693. function get_question_type_for_question($in_selectedcourse, $in_questionid) {
  694. $myObjQuestion = Question::read($in_questionid, $in_selectedcourse);
  695. $questionType = null;
  696. if (!empty($myObjQuestion)) {
  697. list($typeImg, $typeExpl) = $myObjQuestion->get_type_icon_html();
  698. $questionType = Display::tag('div', Display::return_icon($typeImg, $typeExpl, array(), 32), array());
  699. unset($myObjQuestion);
  700. }
  701. return $questionType;
  702. }
  703. // return the name of the category for the question in a course
  704. // hubert.borderiou 13-10-2011
  705. function get_question_categorie_for_question($course_id, $question_id) {
  706. global $all_category_list;
  707. $objQuestionTmp = Question :: read($question_id, $course_id);
  708. return Testcategory::return_category_labels($objQuestionTmp->category_list, $all_category_list);
  709. }