question_pool.php 34 KB

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