question_pool.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. // name of the language file that needs to be included
  13. use ChamiloSession as Session;
  14. require_once '../inc/global.inc.php';
  15. $this_section = SECTION_COURSES;
  16. $is_allowedToEdit = api_is_allowed_to_edit(null, true);
  17. $delete = isset($_GET['delete']) ? intval($_GET['delete']) : null;
  18. $recup = isset($_GET['recup']) ? intval($_GET['recup']) : null;
  19. $fromExercise = isset($_REQUEST['fromExercise']) ? intval($_REQUEST['fromExercise']) : null;
  20. $exerciseId = isset($_REQUEST['exerciseId']) ? intval($_REQUEST['exerciseId']) : null;
  21. $courseCategoryId = isset($_REQUEST['courseCategoryId']) ? intval($_REQUEST['courseCategoryId']) : null;
  22. $exerciseLevel = isset($_REQUEST['exerciseLevel']) ? intval($_REQUEST['exerciseLevel']) : -1;
  23. $answerType = isset($_REQUEST['answerType']) ? intval($_REQUEST['answerType']) : null;
  24. $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 0;
  25. $question_copy = isset($_REQUEST['question_copy']) ? intval($_REQUEST['question_copy']) : 0;
  26. $session_id = isset($_REQUEST['session_id']) ? intval($_REQUEST['session_id']) : null;
  27. $selected_course = isset($_GET['selected_course']) ? intval($_GET['selected_course']) : null;
  28. // save the id of the previous course selected by user to reset menu if we detect that user change course hub 13-10-2011
  29. $course_id_changed = isset($_GET['course_id_changed']) ? intval($_GET['course_id_changed']) : null;
  30. // save the id of the previous exercise selected by user to reset menu if we detect that user change course hub 13-10-2011
  31. $exercise_id_changed = isset($_GET['exercise_id_changed']) ? intval($_GET['exercise_id_changed']) : null;
  32. // by default when we go to the page for the first time, we select the current course
  33. if (!isset($_GET['selected_course']) && !isset($_GET['exerciseId'])) {
  34. $selected_course = api_get_course_int_id();
  35. }
  36. $_course = api_get_course_info();
  37. if (empty($objExercise) && !empty($fromExercise)) {
  38. $objExercise = new Exercise();
  39. $objExercise->read($fromExercise);
  40. }
  41. $nameTools = get_lang('QuestionPool');
  42. $interbreadcrumb[] = array("url" => "exercise.php","name" => get_lang('Exercises'));
  43. if (!empty($objExercise)) {
  44. $interbreadcrumb[] = array("url" => "admin.php?exerciseId=".$objExercise->id, "name" => $objExercise->name);
  45. }
  46. // message to be displayed if actions successful
  47. $displayMessage = "";
  48. if ($is_allowedToEdit) {
  49. // Duplicating a Question
  50. if (!isset($_POST['recup']) && $question_copy != 0 && isset($fromExercise)) {
  51. $origin_course_id = intval($_GET['course_id']);
  52. $origin_course_info = api_get_course_info_by_id($origin_course_id);
  53. $current_course = api_get_course_info();
  54. $old_question_id = $question_copy;
  55. // Reading the source question
  56. $old_question_obj = Question::read($old_question_id, $origin_course_id);
  57. $courseId = $current_course['real_id'];
  58. if ($old_question_obj) {
  59. $old_question_obj->updateTitle(
  60. $old_question_obj->selectTitle() . ' - ' . get_lang('Copy')
  61. );
  62. //Duplicating the source question, in the current course
  63. $new_id = $old_question_obj->duplicate($current_course);
  64. //Reading new question
  65. $new_question_obj = Question::read($new_id);
  66. $new_question_obj->addToList($fromExercise);
  67. //Reading Answers obj of the current course
  68. $new_answer_obj = new Answer($old_question_id, $origin_course_id);
  69. $new_answer_obj->read();
  70. //Duplicating the Answers in the current course
  71. $new_answer_obj->duplicate($new_id, $current_course);
  72. // destruction of the Question object
  73. unset($new_question_obj);
  74. unset($old_question_obj);
  75. $objExercise = new Exercise($courseId);
  76. $objExercise->read($fromExercise);
  77. Session::write('objExercise', $objExercise);
  78. }
  79. $displayMessage = get_lang('ItemAdded');
  80. }
  81. // Deletes a question from the database and all exercises
  82. if ($delete) {
  83. // Construction of the Question object
  84. $objQuestionTmp = Question::read($delete);
  85. // if the question exists
  86. if ($objQuestionTmp) {
  87. // deletes the question from all exercises
  88. $objQuestionTmp->delete();
  89. }
  90. // destruction of the Question object
  91. unset($objQuestionTmp);
  92. } elseif($recup && $fromExercise) {
  93. // gets an existing question and copies it into a new exercise
  94. $objQuestionTmp = Question :: read($recup);
  95. // if the question exists
  96. if ($objQuestionTmp) {
  97. /* Adds the exercise ID represented by $fromExercise into the list
  98. of exercises for the current question */
  99. $objQuestionTmp->addToList($fromExercise);
  100. }
  101. // destruction of the Question object
  102. unset($objQuestionTmp);
  103. if (!$objExercise instanceOf Exercise) {
  104. $objExercise = new Exercise();
  105. $objExercise->read($fromExercise);
  106. }
  107. // Adds the question ID represented by $recup into the list of questions for the current exercise
  108. $objExercise->addToList($recup);
  109. Session::write('objExercise', $objExercise);
  110. } else if (isset($_POST['recup']) && is_array($_POST['recup']) && $fromExercise) {
  111. $list_recup = $_POST['recup'];
  112. foreach ($list_recup as $course_id => $question_data) {
  113. $origin_course_id = intval($course_id);
  114. $origin_course_info = api_get_course_info_by_id($origin_course_id);
  115. $current_course = api_get_course_info();
  116. foreach ($question_data as $old_question_id) {
  117. //Reading the source question
  118. $old_question_obj = Question::read($old_question_id, $origin_course_id);
  119. if ($old_question_obj) {
  120. $old_question_obj->updateTitle(
  121. $old_question_obj->selectTitle() . ' - ' . get_lang('Copy')
  122. );
  123. //Duplicating the source question, in the current course
  124. $new_id = $old_question_obj->duplicate($current_course);
  125. //Reading new question
  126. $new_question_obj = Question::read($new_id);
  127. $new_question_obj->addToList($fromExercise);
  128. //Reading Answers obj of the current course
  129. $new_answer_obj = new Answer($old_question_id, $origin_course_id);
  130. $new_answer_obj->read();
  131. //Duplicating the Answers in the current course
  132. $new_answer_obj->duplicate($new_id, $current_course);
  133. // destruction of the Question object
  134. unset($new_question_obj);
  135. unset($old_question_obj);
  136. if (!$objExercise instanceOf Exercise) {
  137. $objExercise = new Exercise();
  138. $objExercise->read($fromExercise);
  139. }
  140. }
  141. }
  142. }
  143. Session::write('objExercise',$objExercise);
  144. }
  145. }
  146. if (isset($_SESSION['gradebook'])){
  147. $gradebook= $_SESSION['gradebook'];
  148. }
  149. if (!empty($gradebook) && $gradebook=='view') {
  150. $interbreadcrumb[]= array ('url' => '../gradebook/'.Security::remove_XSS($_SESSION['gradebook_dest']),'name' => get_lang('ToolGradebook'));
  151. }
  152. // if admin of course
  153. if (!$is_allowedToEdit) {
  154. api_not_allowed(true);
  155. }
  156. $confirmYourChoice = addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset));
  157. $htmlHeadXtra[] = "
  158. <script>
  159. function submit_form(obj) {
  160. document.question_pool.submit();
  161. }
  162. function mark_course_id_changed() {
  163. $('#course_id_changed').val('1');
  164. }
  165. function mark_exercise_id_changed() {
  166. $('#exercise_id_changed').val('1');
  167. }
  168. function confirm_your_choice() {
  169. return confirm('$confirmYourChoice');
  170. }
  171. </script>";
  172. Display::display_header($nameTools, 'Exercise');
  173. // Menu
  174. echo '<div class="actions">';
  175. if (isset($type)) {
  176. $url = api_get_self().'?type=1';
  177. } else {
  178. $url = api_get_self();
  179. }
  180. if (isset($fromExercise) && $fromExercise > 0) {
  181. echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$fromExercise.'">'.
  182. Display::return_icon('back.png', get_lang('GoBackToQuestionList'),'',ICON_SIZE_MEDIUM).'</a>';
  183. $titleAdd = get_lang('AddQuestionToTest');
  184. } else {
  185. echo '<a href="exercise.php?'.api_get_cidReq().'">'.
  186. Display::return_icon('back.png', get_lang('BackToExercisesList'),'',ICON_SIZE_MEDIUM).'</a>';
  187. echo "<a href='admin.php?exerciseId=0'>".Display::return_icon('add_question.gif', get_lang('NewQu'), '', ICON_SIZE_MEDIUM)."</a>";
  188. $titleAdd = get_lang('ManageAllQuestions');
  189. }
  190. echo '</div>';
  191. if ($displayMessage != "") {
  192. Display::display_confirmation_message($displayMessage);
  193. $displayMessage = "";
  194. }
  195. // Form
  196. echo '<form class="form-horizontal" name="question_pool" method="GET" action="'.$url.'">';
  197. // Title
  198. echo '<legend>'.$nameTools.' - '.$titleAdd.'</legend>';
  199. if (isset($type)) {
  200. echo '<input type="hidden" name="type" value="1">';
  201. }
  202. echo '<input type="hidden" name="fromExercise" value="'.$fromExercise.'">';
  203. // Session list, if sessions are used.
  204. $sessionList = SessionManager::get_sessions_by_user(api_get_user_id(), api_is_platform_admin());
  205. $tabAttrParam = array('class'=>'chzn-select', 'onchange'=>'submit_form(this)');
  206. $labelFormRow = get_lang('Session');
  207. $session_select_list = array();
  208. foreach ($sessionList as $item) {
  209. $session_select_list[$item['session_id']] = $item['session_name'];
  210. }
  211. $select_session_html = Display::select('session_id', $session_select_list, $session_id, $tabAttrParam);
  212. echo Display::form_row($labelFormRow, $select_session_html);
  213. // Course list, get course list of session, or for course where user is admin
  214. if (!empty($session_id) && $session_id != '-1' && !empty($sessionList)) {
  215. $sessionInfo = array();
  216. foreach ($sessionList as $session) {
  217. if ($session['session_id'] == $session_id) {
  218. $sessionInfo = $session;
  219. }
  220. }
  221. $course_list = $sessionInfo['courses'];
  222. } else {
  223. $course_list = CourseManager::get_course_list_of_user_as_course_admin(
  224. api_get_user_id()
  225. );
  226. // Admin fix, add the current course in the question pool.
  227. if (api_is_platform_admin()) {
  228. $courseInfo = api_get_course_info();
  229. if (!empty($course_list)) {
  230. if (!in_array($courseInfo['real_id'], $course_list)) {
  231. $course_list = array_merge($course_list, array($courseInfo));
  232. }
  233. } else {
  234. $course_list = array($courseInfo);
  235. }
  236. }
  237. }
  238. $course_select_list = array();
  239. foreach ($course_list as $item) {
  240. $courseItemId = $item['real_id'];
  241. $courseInfo = api_get_course_info_by_id($courseItemId);
  242. $course_select_list[$courseItemId] = "";
  243. if ($courseItemId == api_get_course_int_id()) {
  244. $course_select_list[$courseItemId] = ">&nbsp;&nbsp;&nbsp;&nbsp;";
  245. }
  246. $course_select_list[$courseItemId] .= $courseInfo['title'];
  247. }
  248. $select_course_html = Display::select(
  249. 'selected_course',
  250. $course_select_list,
  251. $selected_course,
  252. array('class'=>'chzn-select','onchange'=>'mark_course_id_changed(); submit_form(this);')
  253. );
  254. echo Display::form_row(get_lang('Course'), $select_course_html);
  255. if (empty($selected_course) || $selected_course == '-1') {
  256. $course_info = api_get_course_info();
  257. // no course selected, reset menu test / difficult� / type de reponse
  258. reset_menu_exo_lvl_type();
  259. } else {
  260. $course_info = api_get_course_info_by_id($selected_course);
  261. }
  262. // If course has changed, reset the menu default
  263. if ($course_id_changed) {
  264. reset_menu_exo_lvl_type();
  265. }
  266. $course_id = $course_info['real_id'];
  267. // Redefining table calls
  268. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  269. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  270. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  271. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  272. $TBL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
  273. $TBL_COURSE_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  274. // Get course categories for the selected course
  275. // get category list for the course $selected_course
  276. $categoryList = TestCategory::getCategoriesIdAndName($selected_course);
  277. $selectCourseCategory = Display::select(
  278. 'courseCategoryId',
  279. $categoryList,
  280. $courseCategoryId,
  281. array('class'=>'chzn-select','onchange'=>'submit_form(this);'),
  282. false
  283. );
  284. echo Display::form_row(get_lang("QuestionCategory"), $selectCourseCategory);
  285. // Get exercise list for this course
  286. $exercise_list = ExerciseLib::get_all_exercises_for_course_id(
  287. $course_info,
  288. $session_id,
  289. $selected_course,
  290. false
  291. );
  292. //Exercise List
  293. $my_exercise_list = array();
  294. $my_exercise_list['0'] = get_lang('AllExercises');
  295. $my_exercise_list['-1'] = get_lang('OrphanQuestions');
  296. if (is_array($exercise_list)) {
  297. foreach($exercise_list as $row) {
  298. $my_exercise_list[$row['id']] = "";
  299. if ($row['id'] == $fromExercise && $selected_course == api_get_course_int_id()) {
  300. $my_exercise_list[$row['id']] = ">&nbsp;&nbsp;&nbsp;&nbsp;";
  301. }
  302. $my_exercise_list[$row['id']] .= $row['title'];
  303. }
  304. }
  305. if ($exercise_id_changed == 1) {
  306. reset_menu_lvl_type();
  307. }
  308. $select_exercise_html = Display::select(
  309. 'exerciseId',
  310. $my_exercise_list,
  311. $exerciseId,
  312. array('class'=>'chzn-select','onchange'=>'mark_exercise_id_changed(); submit_form(this);'),
  313. false
  314. );
  315. echo Display::form_row(get_lang('Exercise'), $select_exercise_html);
  316. // Difficulty list (only from 0 to 5)
  317. $levels = array(
  318. -1 => get_lang('All'),
  319. 0 => 0,
  320. 1 => 1,
  321. 2 => 2,
  322. 3 => 3,
  323. 4 => 4,
  324. 5 => 5
  325. );
  326. $select_difficulty_html = Display::select(
  327. 'exerciseLevel',
  328. $levels,
  329. $exerciseLevel,
  330. array('class'=>'chzn-select', 'onchange'=>'submit_form(this);'),
  331. false
  332. );
  333. echo Display::form_row(get_lang('Difficulty'), $select_difficulty_html);
  334. // Answer type
  335. $question_list = Question::get_question_type_list();
  336. $new_question_list = array();
  337. $new_question_list['-1'] = get_lang('All');
  338. if (!empty($_course)) {
  339. $objExercise = new Exercise();
  340. $objExercise->read($fromExercise);
  341. foreach ($question_list as $key => $item) {
  342. if ($objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  343. if (!in_array($key, array(HOT_SPOT_DELINEATION, UNIQUE_ANSWER))) {
  344. continue;
  345. }
  346. $new_question_list[$key] = get_lang($item[1]);
  347. } else {
  348. if ($key == HOT_SPOT_DELINEATION) {
  349. continue;
  350. }
  351. $new_question_list[$key] = get_lang($item[1]);
  352. }
  353. }
  354. }
  355. // Answer type list
  356. $select_answer_html = Display::select(
  357. 'answerType',
  358. $new_question_list,
  359. $answerType,
  360. array('class'=>'chzn-select','onchange'=>'submit_form(this);'),
  361. false
  362. );
  363. echo Display::form_row(get_lang('AnswerType'), $select_answer_html);
  364. $button = '<button class="save" type="submit" name="name" value="'.get_lang('Filter').'">'.get_lang('Filter').'</button>';
  365. echo Display::form_row('', $button);
  366. echo "<input type='hidden' id='course_id_changed' name='course_id_changed' value='0' />";
  367. echo "<input type='hidden' id='exercise_id_changed' name='exercise_id_changed' value='0' />";
  368. ?>
  369. </form>
  370. <div class="clear"></div>
  371. <form method="post" action="<?php echo $url.'?'.api_get_cidreq().'&fromExercise='.$fromExercise; ?>" >
  372. <?php
  373. echo '<input type="hidden" name="course_id" value="'.$selected_course.'">';
  374. $mainQuestionList = array();
  375. // if we have selected an exercise in the list-box 'Filter'
  376. if ($exerciseId > 0) {
  377. $where = '';
  378. $from = '';
  379. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  380. $from = ", $TBL_COURSE_REL_CATEGORY crc ";
  381. $where .= " AND crc.c_id=$selected_course AND crc.question_id=qu.id AND crc.category_id=$courseCategoryId";
  382. }
  383. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  384. $where .= ' AND level='.$exerciseLevel;
  385. }
  386. if (isset($answerType) && $answerType > 0) {
  387. $where .= ' AND type='.$answerType;
  388. }
  389. $sql = "SELECT DISTINCT
  390. id,
  391. question,
  392. type,
  393. level
  394. FROM
  395. $TBL_EXERCISE_QUESTION qt,
  396. $TBL_QUESTIONS qu
  397. $from
  398. WHERE
  399. qt.question_id = qu.id
  400. AND qt.exercice_id=$exerciseId
  401. AND qt.c_id=$selected_course
  402. AND qu.c_id=$selected_course
  403. $where
  404. ORDER BY question_order";
  405. $result = Database::query($sql);
  406. while($row = Database::fetch_array($result, 'ASSOC')) {
  407. $mainQuestionList[] = $row;
  408. }
  409. } elseif ($exerciseId == -1) {
  410. // If we have selected the option 'Orphan questions' in the list-box 'Filter'
  411. $level_where = '';
  412. $from = '';
  413. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  414. $from = " INNER JOIN $TBL_COURSE_REL_CATEGORY crc ON crc.question_id=q.id AND crc.c_id= q.c_id ";
  415. $level_where .= " AND
  416. crc.c_id = $selected_course AND
  417. crc.category_id = $courseCategoryId";
  418. }
  419. if (isset($exerciseLevel) && $exerciseLevel!= -1 ) {
  420. $level_where = ' AND level='.$exerciseLevel;
  421. }
  422. $answer_where = '';
  423. if (isset($answerType) && $answerType >0 -1 ) {
  424. $answer_where = ' AND type='.$answerType;
  425. }
  426. // @todo fix this query with the new id field
  427. $sql = " (
  428. SELECT q.* FROM $TBL_QUESTIONS q
  429. INNER JOIN $TBL_EXERCISE_QUESTION r
  430. ON (q.c_id = r.c_id AND q.id = r.question_id)
  431. INNER JOIN $TBL_EXERCISES ex
  432. ON (ex.id = r.exercice_id AND ex.c_id = r.c_id )
  433. $from
  434. WHERE
  435. ex.c_id = '$selected_course' AND
  436. ex.active = '-1'
  437. $level_where $answer_where
  438. )
  439. UNION
  440. (
  441. SELECT q.* FROM $TBL_QUESTIONS q
  442. LEFT OUTER JOIN $TBL_EXERCISE_QUESTION r
  443. ON (q.c_id = r.c_id AND q.id = r.question_id)
  444. $from
  445. WHERE
  446. q.c_id = '$selected_course' AND
  447. r.question_id is null
  448. $level_where $answer_where
  449. )
  450. UNION
  451. (
  452. SELECT q.* FROM $TBL_QUESTIONS q
  453. INNER JOIN $TBL_EXERCISE_QUESTION r
  454. ON (q.c_id = r.c_id AND q.id = r.question_id)
  455. $from
  456. WHERE
  457. r.c_id = '$selected_course' AND
  458. (r.exercice_id = '-1' OR r.exercice_id = '0')
  459. $level_where $answer_where
  460. )";
  461. $result = Database::query($sql);
  462. while($row = Database::fetch_array($result, 'ASSOC')) {
  463. $mainQuestionList[] = $row;
  464. }
  465. } else {
  466. // All tests for selected course
  467. // If we have not selected any option in the list-box 'Filter'
  468. $filter = '';
  469. $from = '';
  470. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  471. $from = ", $TBL_COURSE_REL_CATEGORY crc ";
  472. $filter .= " AND
  473. crc.c_id = $selected_course AND
  474. crc.question_id = qu.id AND
  475. crc.category_id = $courseCategoryId";
  476. }
  477. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  478. $filter .= ' AND level='.$exerciseLevel.' ';
  479. }
  480. if (isset($answerType) && $answerType > 0) {
  481. $filter .= ' AND qu.type='.$answerType.' ';
  482. }
  483. if (!empty($session_id) && $session_id != '-1') {
  484. $mainQuestionList = array();
  485. if (!empty($course_list)) {
  486. foreach ($course_list as $course_item) {
  487. $courseItemId = $course_item['real_id'];
  488. if (!empty($selected_course) && $selected_course != '-1') {
  489. if ($selected_course != $courseItemId) {
  490. continue;
  491. }
  492. }
  493. $exerciseList = ExerciseLib::get_all_exercises($course_item, $session_id);
  494. if (!empty($exerciseList)) {
  495. foreach ($exerciseList as $exercise) {
  496. $my_exercise = new Exercise($courseItemId);
  497. $my_exercise->read($exercise['id']);
  498. if (!empty($my_exercise)) {
  499. if (!empty($my_exercise->questionList)) {
  500. foreach ($my_exercise->questionList as $question_id) {
  501. $question_obj = Question::read(
  502. $question_id,
  503. $courseItemId
  504. );
  505. if ($exerciseLevel != '-1') {
  506. if ($exerciseLevel != $question_obj->level) {
  507. continue;
  508. }
  509. }
  510. if ($answerType > 0) {
  511. if ($answerType != $question_obj->type) {
  512. continue;
  513. }
  514. }
  515. $categoryIdFromQuestion = TestCategory::getCategoryForQuestion(
  516. $question_obj->id,
  517. $selected_course
  518. );
  519. if ($courseCategoryId > 0 &&
  520. $categoryIdFromQuestion != $courseCategoryId
  521. ) {
  522. continue;
  523. }
  524. if (!empty($objExercise) &&
  525. $objExercise->feedback_type != EXERCISE_FEEDBACK_TYPE_DIRECT
  526. ) {
  527. if ($question_obj->type == HOT_SPOT_DELINEATION) {
  528. continue;
  529. }
  530. }
  531. $question_row = array(
  532. 'id' => $question_obj->id,
  533. 'question' => $question_obj->question,
  534. 'type' => $question_obj->type,
  535. 'level' => $question_obj->level,
  536. 'exercise_id' => $exercise['id'],
  537. 'exercise_name' => $exercise['title'],
  538. 'course_id' => $courseItemId,
  539. );
  540. $mainQuestionList[] = $question_row;
  541. }
  542. }
  543. }
  544. }
  545. }
  546. }
  547. }
  548. } else {
  549. if ($session_id == -1 or empty($session_id)) {
  550. $session_id = 0;
  551. }
  552. // All tests for the course selected, not in session
  553. $sql = "SELECT DISTINCT qu.id, question, qu.type, level, q.session_id
  554. FROM
  555. $TBL_QUESTIONS as qu,
  556. $TBL_EXERCISE_QUESTION as qt,
  557. $TBL_EXERCISES as q
  558. $from
  559. WHERE
  560. qu.c_id = $selected_course AND
  561. qt.c_id = $selected_course AND
  562. q.c_id = $selected_course AND
  563. qu.id = qt.question_id AND
  564. q.session_id = $session_id AND
  565. q.id = qt.exercice_id $filter
  566. ORDER BY session_id ASC";
  567. $result = Database::query($sql);
  568. while ($row = Database::fetch_array($result, 'ASSOC')) {
  569. $mainQuestionList[] = $row;
  570. }
  571. }
  572. // forces the value to 0
  573. $exerciseId = 0;
  574. }
  575. $nbrQuestions = count($mainQuestionList);
  576. // build the line of the array to display questions
  577. // Actions are different if you launch the question_pool page
  578. // They are different too if you have displayed questions from your course
  579. // Or from another course you are the admin(or session admin)
  580. // from a test or not
  581. /*
  582. +--------------------------------------------+--------------------------------------------+
  583. | NOT IN A TEST | IN A TEST |
  584. +----------------------+---------------------+---------------------+----------------------+
  585. |IN THE COURSE (*) "x | NOT IN THE COURSE o | IN THE COURSE + | NOT IN THE COURSE o |
  586. +----------------------+---------------------+---------------------+----------------------+
  587. |Edit the question | Do nothing | Add question to test|Clone question in test|
  588. |Delete the question | | | |
  589. |(true delete) | | | |
  590. +----------------------+---------------------+---------------------+----------------------+
  591. (*) this is the only way to delete or modify orphan questions
  592. */
  593. //
  594. if ($fromExercise <= 0) {
  595. // NOT IN A TEST - IN THE COURSE
  596. if ($selected_course == api_get_course_int_id()) {
  597. $actionLabel = get_lang('Modify');
  598. $actionIcon1 = "edit";
  599. $actionIcon2 = "delete";
  600. // We are in the course, question title can be a link to the question edit page
  601. $questionTagA = 1;
  602. } else { // NOT IN A TEST - NOT IN THE COURSE
  603. $actionLabel = get_lang('Reuse');
  604. $actionIcon1 = get_lang('MustBeInATest');
  605. $actionIcon2 = "";
  606. // We are not in this course, to messy if we link to the question in another course
  607. $questionTagA = 0;
  608. }
  609. } else {
  610. // IN A TEST - IN THE COURSE
  611. if ($selected_course == api_get_course_int_id()) {
  612. $actionLabel = get_lang('Reuse');
  613. $actionIcon1 = "add";
  614. $actionIcon2 = "";
  615. $questionTagA = 1;
  616. } else {
  617. // IN A TEST - NOT IN THE COURSE
  618. $actionLabel = get_lang('Reuse');
  619. $actionIcon1 = "clone";
  620. $actionIcon2 = "";
  621. $questionTagA = 0;
  622. }
  623. }
  624. // Display table
  625. $header = array(
  626. array(get_lang('QuestionUpperCaseFirstLetter'), false, array("style"=>"text-align:center"), ''),
  627. array(get_lang('Type'), false, array("style"=>"text-align:center"), array("style"=>"text-align:center"), ''),
  628. array(get_lang('QuestionCategory'), false, array("style"=>"text-align:center"), array("style"=>"text-align:center"), ''),
  629. array(get_lang('Difficulty'), false, array("style"=>"text-align:center"), array("style"=>"text-align:center"), ''),
  630. array($actionLabel, false, array("style"=>"text-align:center"), array("style"=>"text-align:center"), '')
  631. );
  632. $data = array();
  633. /*$hideDoubles = false;
  634. if (empty($exerciseId) && !empty($session_id) && $session_id != '-1') {
  635. $hideDoubles = true;
  636. }
  637. $questionAdded = array();*/
  638. if (is_array($mainQuestionList)) {
  639. foreach ($mainQuestionList as $question) {
  640. /*if ($hideDoubles) {
  641. if (in_array($question['question'], $questionAdded)) {
  642. continue;
  643. }
  644. }
  645. $questionAdded[$question['question']] = $question;*/
  646. $row = array();
  647. // This function checks if the question can be read
  648. $question_type = get_question_type_for_question(
  649. $selected_course,
  650. $question['id']
  651. );
  652. if (empty($question_type)) {
  653. continue;
  654. }
  655. $sessionId = isset($question['session_id']) ? $question['session_id'] : null;
  656. $exerciseName = isset($question['exercise_name']) ? '<br />('.$question['exercise_id'].') ' : null;
  657. $row[] = get_a_tag_for_question(
  658. $questionTagA,
  659. $fromExercise,
  660. $question['id'],
  661. $question['type'],
  662. $question['question'],
  663. $sessionId
  664. ).$exerciseName;
  665. $row[] = $question_type;
  666. $row[] = get_question_categorie_for_question($selected_course, $question['id']);
  667. $row[] = $question['level'];
  668. $row[] = get_action_icon_for_question(
  669. $actionIcon1,
  670. $fromExercise,
  671. $question['id'],
  672. $question['type'],
  673. $question['question'],
  674. $selected_course,
  675. $courseCategoryId,
  676. $exerciseLevel,
  677. $answerType,
  678. $session_id,
  679. $exerciseId
  680. ).
  681. "&nbsp;".
  682. get_action_icon_for_question(
  683. $actionIcon2,
  684. $fromExercise,
  685. $question['id'],
  686. $question['type'],
  687. $question['question'],
  688. $selected_course,
  689. $courseCategoryId,
  690. $exerciseLevel,
  691. $answerType,
  692. $session_id,
  693. $exerciseId
  694. );
  695. $data[] = $row;
  696. }
  697. }
  698. Display :: display_sortable_table(
  699. $header,
  700. $data,
  701. '',
  702. array('per_page_default' => 999, 'per_page' => 999, 'page_nr' => 1)
  703. );
  704. if (!$nbrQuestions) {
  705. echo get_lang('NoQuestion');
  706. }
  707. Display::display_footer();
  708. /**
  709. * Put the menu entry for level and type to default "Choice"
  710. * It is useful if you change the exercise, you need to reset the other menus
  711. * @author hubert.borderiou 13-10-2011
  712. */
  713. function reset_menu_lvl_type()
  714. {
  715. global $exerciseLevel, $answerType;
  716. $answerType = -1;
  717. $exerciseLevel = -1;
  718. }
  719. /**
  720. * Put the menu entry for exercise and level and type to default "Choice"
  721. * It is useful if you change the course, you need to reset the other menus
  722. * @author hubert.borderiou 13-10-2011
  723. */
  724. function reset_menu_exo_lvl_type()
  725. {
  726. global $exerciseId, $courseCategoryId;
  727. reset_menu_lvl_type();
  728. $exerciseId = 0;
  729. $courseCategoryId = 0;
  730. }
  731. /**
  732. * return the <a> link to admin question, if needed
  733. * @param int $in_addA
  734. * @param int $in_fromex
  735. * @param int $in_questionid
  736. * @param int $in_questiontype
  737. * @param string $in_questionname
  738. * @param int $sessionId
  739. * @return string
  740. * @author hubert.borderiou
  741. */
  742. function get_a_tag_for_question(
  743. $in_addA,
  744. $in_fromex,
  745. $in_questionid,
  746. $in_questiontype,
  747. $in_questionname,
  748. $sessionId
  749. ) {
  750. $res = $in_questionname;
  751. $sessionIcon = null;
  752. if ($in_addA) {
  753. if (!empty($sessionId) && $sessionId != -1) {
  754. $sessionIcon = ' '.Display::return_icon('star.png', get_lang('Session'));
  755. }
  756. $res = "<a href='admin.php?".api_get_cidreq()."&editQuestion=$in_questionid&type=$in_questiontype&fromExercise=$in_fromex'>".
  757. $res.$sessionIcon.
  758. "</a>";
  759. }
  760. return $res;
  761. }
  762. /**
  763. Return the <a> html code for delete, add, clone, edit a question
  764. in_action = the code of the action triggered by the button
  765. from_exercise = the id of the current exercise from which we click on question pool
  766. in_questionid = the id of the current question
  767. in_questiontype = the code of the type of the current question
  768. in_questionname = the name of the question
  769. in_selected_course = the if of the course chosen in the FILTERING MENU
  770. in_courseCategoryId = the id of the category chosen in the FILTERING MENU
  771. in_exerciseLevel = the level of the exercise chosen in the FILTERING MENU
  772. in_answerType = the code of the type of the question chosen in the FILTERING MENU
  773. in_session_id = the id of the session_id chosen in the FILTERING MENU
  774. in_exercise_id = the id of the exercise chosen in the FILTERING MENU
  775. */
  776. function get_action_icon_for_question(
  777. $in_action,
  778. $from_exercise,
  779. $in_questionid,
  780. $in_questiontype,
  781. $in_questionname,
  782. $in_selected_course,
  783. $in_courseCategoryId,
  784. $in_exerciseLevel,
  785. $in_answerType,
  786. $in_session_id,
  787. $in_exercise_id
  788. ) {
  789. $res = "";
  790. $getParams = "&selected_course=$in_selected_course&courseCategoryId=$in_courseCategoryId&exerciseId=$in_exercise_id&exerciseLevel=$in_exerciseLevel&answerType=$in_answerType&session_id=$in_session_id";
  791. switch ($in_action) {
  792. case "delete" :
  793. $res = "<a href='".api_get_self()."?".api_get_cidreq().$getParams."&delete=$in_questionid' onclick='return confirm_your_choice()'>";
  794. $res .= Display::return_icon("delete.png", get_lang('Delete'));
  795. $res .= "</a>";
  796. break;
  797. case "edit" :
  798. $res = get_a_tag_for_question(
  799. 1,
  800. $from_exercise,
  801. $in_questionid,
  802. $in_questiontype,
  803. Display::return_icon("edit.png", get_lang('Modify')),
  804. $in_session_id
  805. );
  806. break;
  807. case "add":
  808. // add if question is not already in test
  809. $myObjEx = new Exercise();
  810. $myObjEx->read($from_exercise);
  811. if (!$myObjEx->isInList($in_questionid)) {
  812. $res = "<a href='".api_get_self()."?".api_get_cidreq().$getParams."&recup=$in_questionid&fromExercise=$from_exercise'>";
  813. $res .= Display::return_icon("view_more_stats.gif", get_lang('InsertALinkToThisQuestionInTheExercise'));
  814. $res .= "</a>";
  815. } else {
  816. $res = "-";
  817. }
  818. unset($myObjEx);
  819. break;
  820. case "clone":
  821. $url = api_get_self()."?".api_get_cidreq().$getParams."&question_copy=$in_questionid&course_id=$in_selected_course&fromExercise=$from_exercise";
  822. $res = Display::url(
  823. Display::return_icon('cd.gif', get_lang('ReUseACopyInCurrentTest')),
  824. $url
  825. );
  826. break;
  827. default :
  828. $res = $in_action;
  829. break;
  830. }
  831. return $res;
  832. }
  833. /**
  834. * Return the icon for the question type
  835. * @author hubert.borderiou 13-10-2011
  836. */
  837. function get_question_type_for_question($in_selectedcourse, $in_questionid)
  838. {
  839. $myObjQuestion = Question::read($in_questionid, $in_selectedcourse);
  840. $questionType = null;
  841. if (!empty($myObjQuestion)) {
  842. list($typeImg, $typeExpl) = $myObjQuestion->get_type_icon_html();
  843. $questionType = Display::tag('div', Display::return_icon($typeImg, $typeExpl, array(), 32), array());
  844. unset($myObjQuestion);
  845. }
  846. return $questionType;
  847. }
  848. /**
  849. * Return the name of the category for the question in a course
  850. * @author hubert.borderiou 13-10-2011
  851. */
  852. function get_question_categorie_for_question($in_courseid, $in_questionid)
  853. {
  854. $cat = TestCategory::getCategoryNameForQuestion($in_questionid, $in_courseid);
  855. return $cat;
  856. }