question_pool.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. use Knp\Component\Pager\Paginator;
  5. /**
  6. * Question Pool
  7. * This script allows administrators to manage questions and add them into their exercises.
  8. * One question can be in several exercises.
  9. *
  10. * @package chamilo.exercise
  11. *
  12. * @author Olivier Brouckaert
  13. * @author Julio Montoya adding support to query all questions from all session, courses, exercises
  14. * @author Modify by hubert borderiou 2011-10-21 Question's category
  15. */
  16. require_once __DIR__.'/../inc/global.inc.php';
  17. api_protect_course_script(true);
  18. $this_section = SECTION_COURSES;
  19. $is_allowedToEdit = api_is_allowed_to_edit(null, true);
  20. $delete = isset($_GET['delete']) ? (int) $_GET['delete'] : null;
  21. $recup = isset($_GET['recup']) ? (int) $_GET['recup'] : null;
  22. $fromExercise = isset($_REQUEST['fromExercise']) ? (int) $_REQUEST['fromExercise'] : null;
  23. $exerciseId = isset($_REQUEST['exerciseId']) ? (int) $_REQUEST['exerciseId'] : null;
  24. $courseCategoryId = isset($_REQUEST['courseCategoryId']) ? (int) $_REQUEST['courseCategoryId'] : null;
  25. $exerciseLevel = isset($_REQUEST['exerciseLevel']) ? (int) $_REQUEST['exerciseLevel'] : -1;
  26. $answerType = isset($_REQUEST['answerType']) ? (int) $_REQUEST['answerType'] : null;
  27. $question_copy = isset($_REQUEST['question_copy']) ? (int) $_REQUEST['question_copy'] : 0;
  28. $session_id = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : null;
  29. $selected_course = isset($_GET['selected_course']) ? (int) $_GET['selected_course'] : null;
  30. // save the id of the previous course selected by user to reset menu if we detect that user change course hub 13-10-2011
  31. $course_id_changed = isset($_GET['course_id_changed']) ? (int) $_GET['course_id_changed'] : null;
  32. // save the id of the previous exercise selected by user to reset menu if we detect that user change course hub 13-10-2011
  33. $exercise_id_changed = isset($_GET['exercise_id_changed']) ? (int) $_GET['exercise_id_changed'] : null;
  34. $questionId = isset($_GET['question_id']) && !empty($_GET['question_id']) ? (int) $_GET['question_id'] : '';
  35. $description = isset($_GET['description']) ? Database::escape_string($_GET['description']) : '';
  36. $page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
  37. // by default when we go to the page for the first time, we select the current course
  38. if (!isset($_GET['selected_course']) && !isset($_GET['exerciseId'])) {
  39. $selected_course = api_get_course_int_id();
  40. }
  41. $_course = api_get_course_info();
  42. $objExercise = new Exercise();
  43. if (!empty($fromExercise)) {
  44. $objExercise->read($fromExercise, false);
  45. }
  46. $nameTools = get_lang('QuestionPool');
  47. $interbreadcrumb[] = ['url' => 'exercise.php?'.api_get_cidreq(), 'name' => get_lang('Exercises')];
  48. if (!empty($objExercise->id)) {
  49. $interbreadcrumb[] = [
  50. 'url' => 'admin.php?exerciseId='.$objExercise->id.'&'.api_get_cidreq(),
  51. 'name' => $objExercise->selectTitle(true),
  52. ];
  53. }
  54. // message to be displayed if actions successful
  55. $displayMessage = '';
  56. if ($is_allowedToEdit) {
  57. // Duplicating a Question
  58. if (!isset($_POST['recup']) && $question_copy != 0 && isset($fromExercise)) {
  59. $origin_course_id = (int) $_GET['course_id'];
  60. $origin_course_info = api_get_course_info_by_id($origin_course_id);
  61. $current_course = api_get_course_info();
  62. $old_question_id = $question_copy;
  63. // Reading the source question
  64. $old_question_obj = Question::read($old_question_id, $origin_course_info);
  65. $courseId = $current_course['real_id'];
  66. if ($old_question_obj) {
  67. $old_question_obj->updateTitle($old_question_obj->selectTitle().' - '.get_lang('Copy'));
  68. //Duplicating the source question, in the current course
  69. $new_id = $old_question_obj->duplicate($current_course);
  70. //Reading new question
  71. $new_question_obj = Question::read($new_id);
  72. $new_question_obj->addToList($fromExercise);
  73. //Reading Answers obj of the current course
  74. $new_answer_obj = new Answer($old_question_id, $origin_course_id);
  75. $new_answer_obj->read();
  76. //Duplicating the Answers in the current course
  77. $new_answer_obj->duplicate($new_question_obj, $current_course);
  78. // destruction of the Question object
  79. unset($new_question_obj);
  80. unset($old_question_obj);
  81. $objExercise = new Exercise($courseId);
  82. $objExercise->read($fromExercise);
  83. Session::write('objExercise', $objExercise);
  84. }
  85. $displayMessage = get_lang('ItemAdded');
  86. }
  87. // Deletes a question from the database and all exercises
  88. if ($delete) {
  89. $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
  90. if ($limitTeacherAccess && !api_is_platform_admin()) {
  91. api_not_allowed(true);
  92. }
  93. // Construction of the Question object
  94. $objQuestionTmp = Question::read($delete);
  95. // if the question exists
  96. if ($objQuestionTmp) {
  97. // deletes the question from all exercises
  98. $objQuestionTmp->delete();
  99. }
  100. // destruction of the Question object
  101. unset($objQuestionTmp);
  102. } elseif ($recup && $fromExercise) {
  103. // gets an existing question and copies it into a new exercise
  104. $objQuestionTmp = Question::read($recup);
  105. // if the question exists
  106. if ($objQuestionTmp) {
  107. /* Adds the exercise ID represented by $fromExercise into the list
  108. of exercises for the current question */
  109. $objQuestionTmp->addToList($fromExercise);
  110. }
  111. // destruction of the Question object
  112. unset($objQuestionTmp);
  113. if (!$objExercise instanceof Exercise) {
  114. $objExercise = new Exercise();
  115. $objExercise->read($fromExercise);
  116. }
  117. // Adds the question ID represented by $recup into the list of questions for the current exercise
  118. $objExercise->addToList($recup);
  119. Session::write('objExercise', $objExercise);
  120. Display::addFlash(Display::return_message(get_lang('ItemAdded'), 'success'));
  121. } elseif (isset($_POST['recup']) && is_array($_POST['recup']) && $fromExercise) {
  122. $list_recup = $_POST['recup'];
  123. foreach ($list_recup as $course_id => $question_data) {
  124. $origin_course_id = (int) $course_id;
  125. $origin_course_info = api_get_course_info_by_id($origin_course_id);
  126. $current_course = api_get_course_info();
  127. foreach ($question_data as $old_question_id) {
  128. // Reading the source question
  129. $old_question_obj = Question::read($old_question_id, $origin_course_info);
  130. if ($old_question_obj) {
  131. $old_question_obj->updateTitle(
  132. $old_question_obj->selectTitle().' - '.get_lang('Copy')
  133. );
  134. // Duplicating the source question, in the current course
  135. $new_id = $old_question_obj->duplicate($current_course);
  136. // Reading new question
  137. $new_question_obj = Question::read($new_id);
  138. $new_question_obj->addToList($fromExercise);
  139. //Reading Answers obj of the current course
  140. $new_answer_obj = new Answer($old_question_id, $origin_course_id);
  141. $new_answer_obj->read();
  142. //Duplicating the Answers in the current course
  143. $new_answer_obj->duplicate($new_question_obj, $current_course);
  144. // destruction of the Question object
  145. unset($new_question_obj);
  146. unset($old_question_obj);
  147. if (!$objExercise instanceof Exercise) {
  148. $objExercise = new Exercise();
  149. $objExercise->read($fromExercise);
  150. }
  151. }
  152. }
  153. }
  154. Session::write('objExercise', $objExercise);
  155. }
  156. }
  157. if (api_is_in_gradebook()) {
  158. $interbreadcrumb[] = [
  159. 'url' => Category::getUrl(),
  160. 'name' => get_lang('ToolGradebook'),
  161. ];
  162. }
  163. // if admin of course
  164. if (!$is_allowedToEdit) {
  165. api_not_allowed(true);
  166. }
  167. $confirmYourChoice = addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset));
  168. $htmlHeadXtra[] = "
  169. <script>
  170. function submit_form(obj) {
  171. document.question_pool.submit();
  172. }
  173. function mark_course_id_changed() {
  174. $('#course_id_changed').val('1');
  175. }
  176. function mark_exercise_id_changed() {
  177. $('#exercise_id_changed').val('1');
  178. }
  179. function confirm_your_choice() {
  180. return confirm('$confirmYourChoice');
  181. }
  182. </script>";
  183. $url = api_get_self().'?'.api_get_cidreq().'&'.http_build_query(
  184. [
  185. 'fromExercise' => $fromExercise,
  186. 'session_id' => $session_id,
  187. 'selected_course' => $selected_course,
  188. 'courseCategoryId' => $courseCategoryId,
  189. 'exerciseId' => $exerciseId,
  190. 'exerciseLevel' => $exerciseLevel,
  191. 'answerType' => $answerType,
  192. 'question_id' => $questionId,
  193. 'description' => Security::remove_XSS($description),
  194. 'course_id_changed' => $course_id_changed,
  195. 'exercise_id_changed' => $exercise_id_changed,
  196. ]
  197. );
  198. if (isset($_REQUEST['action'])) {
  199. switch ($_REQUEST['action']) {
  200. case 'reuse':
  201. if (!empty($_REQUEST['questions']) && !empty($fromExercise)) {
  202. $questions = $_REQUEST['questions'];
  203. $objExercise = new Exercise();
  204. $objExercise->read($fromExercise, false);
  205. if (count($questions) > 0) {
  206. foreach ($questions as $questionId) {
  207. // gets an existing question and copies it into a new exercise
  208. $objQuestionTmp = Question::read($questionId);
  209. // if the question exists
  210. if ($objQuestionTmp) {
  211. if ($objExercise->hasQuestion($questionId) === false) {
  212. $objExercise->addToList($questionId);
  213. $objQuestionTmp->addToList($fromExercise);
  214. }
  215. }
  216. }
  217. }
  218. Display::addFlash(Display::return_message(get_lang('Added')));
  219. header('Location: '.$url);
  220. exit;
  221. }
  222. break;
  223. case 'clone':
  224. if (!empty($_REQUEST['questions']) && !empty($fromExercise)) {
  225. $questions = $_REQUEST['questions'];
  226. $objExercise = new Exercise();
  227. $objExercise->read($fromExercise, false);
  228. $origin_course_id = (int) $_GET['course_id'];
  229. $origin_course_info = api_get_course_info_by_id($origin_course_id);
  230. $current_course = api_get_course_info();
  231. if (count($questions) > 0) {
  232. foreach ($questions as $questionId) {
  233. // gets an existing question and copies it into a new exercise
  234. // Reading the source question
  235. $old_question_obj = Question::read($questionId, $origin_course_info);
  236. $courseId = $current_course['real_id'];
  237. if ($old_question_obj) {
  238. $old_question_obj->updateTitle($old_question_obj->selectTitle().' - '.get_lang('Copy'));
  239. // Duplicating the source question, in the current course
  240. $new_id = $old_question_obj->duplicate($current_course);
  241. // Reading new question
  242. $new_question_obj = Question::read($new_id);
  243. $new_question_obj->addToList($fromExercise);
  244. //Reading Answers obj of the current course
  245. $new_answer_obj = new Answer($old_question_id, $origin_course_id);
  246. $new_answer_obj->read();
  247. //Duplicating the Answers in the current course
  248. $new_answer_obj->duplicate($new_question_obj, $current_course);
  249. // destruction of the Question object
  250. unset($new_question_obj);
  251. unset($old_question_obj);
  252. }
  253. }
  254. }
  255. Display::addFlash(Display::return_message(get_lang('Added')));
  256. header('Location: '.$url);
  257. exit;
  258. }
  259. break;
  260. }
  261. }
  262. Display::display_header($nameTools, 'Exercise');
  263. // Menu
  264. echo '<div class="actions">';
  265. if (isset($fromExercise) && $fromExercise > 0) {
  266. echo '<a href="admin.php?'.api_get_cidreq().'&exerciseId='.$fromExercise.'">'.
  267. Display::return_icon('back.png', get_lang('GoBackToQuestionList'), '', ICON_SIZE_MEDIUM).'</a>';
  268. $titleAdd = get_lang('AddQuestionToTest');
  269. } else {
  270. echo '<a href="exercise.php?'.api_get_cidreq().'">'.
  271. Display::return_icon('back.png', get_lang('BackToExercisesList'), '', ICON_SIZE_MEDIUM).'</a>';
  272. echo "<a href='admin.php?exerciseId=0'>".
  273. Display::return_icon('add_question.gif', get_lang('NewQu'), '', ICON_SIZE_MEDIUM).'</a>';
  274. $titleAdd = get_lang('ManageAllQuestions');
  275. }
  276. echo '</div>';
  277. if ($displayMessage != '') {
  278. echo Display::return_message($displayMessage, 'confirm');
  279. $displayMessage = '';
  280. }
  281. // Form
  282. echo '<form class="form-horizontal" name="question_pool" method="GET" action="'.$url.'">';
  283. // Title
  284. echo '<legend>'.$nameTools.' - '.$titleAdd.'</legend>';
  285. echo '<input type="hidden" name="fromExercise" value="'.$fromExercise.'">';
  286. // Session list, if sessions are used.
  287. $sessionList = SessionManager::get_sessions_by_user(api_get_user_id(), api_is_platform_admin());
  288. $session_select_list = [];
  289. foreach ($sessionList as $item) {
  290. $session_select_list[$item['session_id']] = $item['session_name'];
  291. }
  292. $sessionListToString = Display::select(
  293. 'session_id',
  294. $session_select_list,
  295. $session_id,
  296. ['onchange' => 'submit_form(this)']
  297. );
  298. echo Display::form_row(get_lang('Session'), $sessionListToString);
  299. // Course list, get course list of session, or for course where user is admin
  300. if (!empty($session_id) && $session_id != '-1' && !empty($sessionList)) {
  301. $sessionInfo = [];
  302. foreach ($sessionList as $session) {
  303. if ($session['session_id'] == $session_id) {
  304. $sessionInfo = $session;
  305. }
  306. }
  307. $course_list = $sessionInfo['courses'];
  308. } else {
  309. if (api_is_platform_admin()) {
  310. $course_list = CourseManager::get_courses_list(0, 0, 'title');
  311. } else {
  312. $course_list = CourseManager::get_course_list_of_user_as_course_admin(api_get_user_id());
  313. }
  314. // Admin fix, add the current course in the question pool.
  315. if (api_is_platform_admin()) {
  316. $courseInfo = api_get_course_info();
  317. if (!empty($course_list)) {
  318. if (!in_array($courseInfo['real_id'], $course_list)) {
  319. $course_list = array_merge($course_list, [$courseInfo]);
  320. }
  321. } else {
  322. $course_list = [$courseInfo];
  323. }
  324. }
  325. }
  326. $course_select_list = [];
  327. foreach ($course_list as $item) {
  328. $courseItemId = $item['real_id'];
  329. $courseInfo = api_get_course_info_by_id($courseItemId);
  330. $course_select_list[$courseItemId] = '';
  331. if ($courseItemId == api_get_course_int_id()) {
  332. $course_select_list[$courseItemId] = ">&nbsp;&nbsp;&nbsp;&nbsp;";
  333. }
  334. $course_select_list[$courseItemId] .= $courseInfo['title'];
  335. }
  336. $courseListToString = Display::select(
  337. 'selected_course',
  338. $course_select_list,
  339. $selected_course,
  340. ['onchange' => 'mark_course_id_changed(); submit_form(this);']
  341. );
  342. echo Display::form_row(get_lang('Course'), $courseListToString);
  343. if (empty($selected_course) || $selected_course == '-1') {
  344. $course_info = api_get_course_info();
  345. // no course selected, reset menu test / difficult� / type de reponse
  346. reset_menu_exo_lvl_type();
  347. } else {
  348. $course_info = api_get_course_info_by_id($selected_course);
  349. }
  350. // If course has changed, reset the menu default
  351. if ($course_id_changed) {
  352. reset_menu_exo_lvl_type();
  353. }
  354. $course_id = $course_info['real_id'];
  355. // Get category list for the course $selected_course
  356. $categoryList = TestCategory::getCategoriesIdAndName($selected_course);
  357. $selectCourseCategory = Display::select(
  358. 'courseCategoryId',
  359. $categoryList,
  360. $courseCategoryId,
  361. ['onchange' => 'submit_form(this);'],
  362. false
  363. );
  364. echo Display::form_row(get_lang('QuestionCategory'), $selectCourseCategory);
  365. // Get exercise list for this course
  366. $exercise_list = ExerciseLib::get_all_exercises_for_course_id(
  367. $course_info,
  368. $session_id,
  369. $selected_course,
  370. false
  371. );
  372. if ($exercise_id_changed == 1) {
  373. reset_menu_lvl_type();
  374. }
  375. // Exercise List
  376. $my_exercise_list = [];
  377. $my_exercise_list['0'] = get_lang('AllExercises');
  378. $my_exercise_list['-1'] = get_lang('OrphanQuestions');
  379. $titleSavedAsHtml = api_get_configuration_value('save_titles_as_html');
  380. if (is_array($exercise_list)) {
  381. foreach ($exercise_list as $row) {
  382. $my_exercise_list[$row['id']] = '';
  383. if ($row['id'] == $fromExercise && $selected_course == api_get_course_int_id()) {
  384. $my_exercise_list[$row['id']] = ">&nbsp;&nbsp;&nbsp;&nbsp;";
  385. }
  386. $exerciseTitle = $row['title'];
  387. if ($titleSavedAsHtml) {
  388. $exerciseTitle = strip_tags(api_html_entity_decode(trim($exerciseTitle)));
  389. }
  390. $my_exercise_list[$row['id']] .= $exerciseTitle;
  391. }
  392. }
  393. $exerciseListToString = Display::select(
  394. 'exerciseId',
  395. $my_exercise_list,
  396. $exerciseId,
  397. ['onchange' => 'mark_exercise_id_changed(); submit_form(this);'],
  398. false
  399. );
  400. echo Display::form_row(get_lang('Exercise'), $exerciseListToString);
  401. // Difficulty list (only from 0 to 5)
  402. $levels = [
  403. -1 => get_lang('All'),
  404. 0 => 0,
  405. 1 => 1,
  406. 2 => 2,
  407. 3 => 3,
  408. 4 => 4,
  409. 5 => 5,
  410. ];
  411. $select_difficulty_html = Display::select(
  412. 'exerciseLevel',
  413. $levels,
  414. $exerciseLevel,
  415. ['onchange' => 'submit_form(this);'],
  416. false
  417. );
  418. echo Display::form_row(get_lang('Difficulty'), $select_difficulty_html);
  419. // Answer type
  420. $question_list = Question::get_question_type_list();
  421. $new_question_list = [];
  422. $new_question_list['-1'] = get_lang('All');
  423. if (!empty($_course)) {
  424. foreach ($question_list as $key => $item) {
  425. if ($objExercise->feedback_type == EXERCISE_FEEDBACK_TYPE_DIRECT) {
  426. if (!in_array($key, [HOT_SPOT_DELINEATION, UNIQUE_ANSWER])) {
  427. continue;
  428. }
  429. $new_question_list[$key] = get_lang($item[1]);
  430. } else {
  431. if ($key == HOT_SPOT_DELINEATION) {
  432. continue;
  433. }
  434. $new_question_list[$key] = get_lang($item[1]);
  435. }
  436. }
  437. }
  438. // Answer type list
  439. $select_answer_html = Display::select(
  440. 'answerType',
  441. $new_question_list,
  442. $answerType,
  443. ['onchange' => 'submit_form(this);'],
  444. false
  445. );
  446. echo Display::form_row(get_lang('AnswerType'), $select_answer_html);
  447. echo Display::form_row(get_lang('Id'), Display::input('text', 'question_id', $questionId));
  448. echo Display::form_row(
  449. get_lang('Description'),
  450. Display::input('text', 'description', Security::remove_XSS($description))
  451. );
  452. $button = '<button class="btn btn-primary save" type="submit" name="name" value="'.get_lang('Filter').'">'.
  453. get_lang('Filter').'</button>';
  454. echo Display::form_row('', $button);
  455. echo "<input type='hidden' id='course_id_changed' name='course_id_changed' value='0' />";
  456. echo "<input type='hidden' id='exercise_id_changed' name='exercise_id_changed' value='0' />";
  457. ?>
  458. </form>
  459. <div class="clear"></div>
  460. <?php
  461. function getQuestions(
  462. $getCount,
  463. $start,
  464. $length,
  465. $exerciseId,
  466. $courseCategoryId,
  467. $selected_course,
  468. $session_id,
  469. $exerciseLevel,
  470. $answerType,
  471. $questionId,
  472. $description
  473. ) {
  474. $start = (int) $start;
  475. $length = (int) $length;
  476. $exerciseId = (int) $exerciseId;
  477. $courseCategoryId = (int) $courseCategoryId;
  478. $selected_course = (int) $selected_course;
  479. $session_id = (int) $session_id;
  480. $exerciseLevel = (int) $exerciseLevel;
  481. $answerType = (int) $answerType;
  482. $questionId = (int) $questionId;
  483. $description = Database::escape_string($description);
  484. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  485. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  486. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  487. $TBL_COURSE_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  488. // if we have selected an exercise in the list-box 'Filter'
  489. if ($exerciseId > 0) {
  490. $where = '';
  491. $from = '';
  492. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  493. $from = ", $TBL_COURSE_REL_CATEGORY crc ";
  494. $where .= " AND
  495. crc.c_id = $selected_course AND
  496. crc.question_id = qu.id AND
  497. crc.category_id = $courseCategoryId";
  498. }
  499. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  500. $where .= ' AND level='.$exerciseLevel;
  501. }
  502. if (isset($answerType) && $answerType > 0) {
  503. $where .= ' AND type='.$answerType;
  504. }
  505. if (!empty($questionId)) {
  506. $where .= ' AND qu.iid='.$questionId;
  507. }
  508. if (!empty($description)) {
  509. $where .= " AND qu.description LIKE '%$description%'";
  510. }
  511. $select = 'DISTINCT
  512. id,
  513. question,
  514. type,
  515. level,
  516. qt.exercice_id exerciseId';
  517. if ($getCount) {
  518. $select = 'count(qu.iid) as count';
  519. }
  520. $sql = "SELECT $select
  521. FROM
  522. $TBL_EXERCISE_QUESTION qt,
  523. $TBL_QUESTIONS qu
  524. $from
  525. WHERE
  526. qt.question_id = qu.id AND
  527. qt.exercice_id = $exerciseId AND
  528. qt.c_id = $selected_course AND
  529. qu.c_id = $selected_course
  530. $where
  531. ORDER BY question_order";
  532. } elseif ($exerciseId == -1) {
  533. // If we have selected the option 'Orphan questions' in the list-box 'Filter'
  534. $level_where = '';
  535. $from = '';
  536. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  537. $from = " INNER JOIN $TBL_COURSE_REL_CATEGORY crc
  538. ON crc.question_id = q.id AND crc.c_id = q.c_id ";
  539. $level_where .= " AND
  540. crc.c_id = $selected_course AND
  541. crc.category_id = $courseCategoryId";
  542. }
  543. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  544. $level_where = ' AND level='.$exerciseLevel;
  545. }
  546. $answer_where = '';
  547. if (isset($answerType) && $answerType > 0 - 1) {
  548. $answer_where = ' AND type='.$answerType;
  549. }
  550. if (!empty($questionId)) {
  551. $answer_where .= ' AND q.iid='.$questionId;
  552. }
  553. if (!empty($description)) {
  554. $answer_where .= " AND q.description LIKE '%$description%'";
  555. }
  556. $select = ' q.*, r.exercice_id exerciseId ';
  557. if ($getCount) {
  558. $select = 'count(q.iid) as count';
  559. }
  560. // @todo fix this query with the new id field
  561. $sql = " (
  562. SELECT $select
  563. FROM $TBL_QUESTIONS q
  564. INNER JOIN $TBL_EXERCISE_QUESTION r
  565. ON (q.c_id = r.c_id AND q.id = r.question_id)
  566. INNER JOIN $TBL_EXERCISES ex
  567. ON (ex.id = r.exercice_id AND ex.c_id = r.c_id)
  568. $from
  569. WHERE
  570. ex.c_id = '$selected_course' AND
  571. ex.active = '-1'
  572. $level_where
  573. $answer_where
  574. )
  575. UNION
  576. (
  577. SELECT $select
  578. FROM $TBL_QUESTIONS q
  579. LEFT OUTER JOIN $TBL_EXERCISE_QUESTION r
  580. ON (q.c_id = r.c_id AND q.id = r.question_id)
  581. $from
  582. WHERE
  583. q.c_id = '$selected_course' AND
  584. r.question_id is null
  585. $level_where
  586. $answer_where
  587. )
  588. UNION
  589. (
  590. SELECT $select
  591. FROM $TBL_QUESTIONS q
  592. INNER JOIN $TBL_EXERCISE_QUESTION r
  593. ON (q.c_id = r.c_id AND q.id = r.question_id)
  594. $from
  595. WHERE
  596. r.c_id = '$selected_course' AND
  597. (r.exercice_id = '-1' OR r.exercice_id = '0')
  598. $level_where
  599. $answer_where
  600. )
  601. ";
  602. if ($getCount) {
  603. $sql = "SELECT SUM(count) count FROM ($sql) as total";
  604. }
  605. } else {
  606. // All tests for selected course
  607. // If we have not selected any option in the list-box 'Filter'
  608. $filter = '';
  609. $from = '';
  610. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  611. $from = ", $TBL_COURSE_REL_CATEGORY crc ";
  612. $filter .= " AND
  613. crc.c_id = $selected_course AND
  614. crc.question_id = qu.id AND
  615. crc.category_id = $courseCategoryId";
  616. }
  617. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  618. $filter .= ' AND level='.$exerciseLevel.' ';
  619. }
  620. if (isset($answerType) && $answerType > 0) {
  621. $filter .= ' AND qu.type='.$answerType.' ';
  622. }
  623. if (!empty($questionId)) {
  624. $filter .= ' AND qu.iid='.$questionId;
  625. }
  626. if (!empty($description)) {
  627. $filter .= " AND qu.description LIKE '%$description%'";
  628. }
  629. if ($session_id == -1 || empty($session_id)) {
  630. $session_id = 0;
  631. }
  632. $sessionCondition = api_get_session_condition($session_id, true, 'q.session_id');
  633. $select = 'qu.id, question, qu.type, level, q.session_id, qt.exercice_id exerciseId ';
  634. if ($getCount) {
  635. $select = 'count(qu.iid) as count';
  636. }
  637. // All tests for the course selected, not in session
  638. $sql = "SELECT DISTINCT
  639. $select
  640. FROM
  641. $TBL_QUESTIONS as qu,
  642. $TBL_EXERCISE_QUESTION as qt,
  643. $TBL_EXERCISES as q
  644. $from
  645. WHERE
  646. qu.c_id = $selected_course AND
  647. qt.c_id = $selected_course AND
  648. q.c_id = $selected_course AND
  649. qu.id = qt.question_id
  650. $sessionCondition AND
  651. q.id = qt.exercice_id
  652. $filter
  653. ORDER BY session_id ASC";
  654. }
  655. if ($getCount) {
  656. $result = Database::query($sql);
  657. $row = Database::fetch_array($result, 'ASSOC');
  658. return (int) $row['count'];
  659. }
  660. $sql .= " LIMIT $start, $length";
  661. $result = Database::query($sql);
  662. $mainQuestionList = [];
  663. while ($row = Database::fetch_array($result, 'ASSOC')) {
  664. $mainQuestionList[] = $row;
  665. }
  666. return $mainQuestionList;
  667. }
  668. $nbrQuestions = getQuestions(
  669. true,
  670. null,
  671. null,
  672. $exerciseId,
  673. $courseCategoryId,
  674. $selected_course,
  675. $session_id,
  676. $exerciseLevel,
  677. $answerType,
  678. $questionId,
  679. $description
  680. );
  681. $length = api_get_configuration_value('question_pagination_length');
  682. if (empty($length)) {
  683. $length = 20;
  684. }
  685. $start = ($page - 1) * $length;
  686. $paginator = new Paginator();
  687. $pagination = $paginator->paginate([]);
  688. $pagination->setTotalItemCount($nbrQuestions);
  689. $pagination->setItemNumberPerPage($length);
  690. $pagination->setCurrentPageNumber($page);
  691. $pagination->renderer = function ($data) use ($url) {
  692. $render = '';
  693. if ($data['pageCount'] > 1) {
  694. $render = '<ul class="pagination">';
  695. for ($i = 1; $i <= $data['pageCount']; $i++) {
  696. $pageContent = '<li><a href="'.$url.'&page='.$i.'">'.$i.'</a></li>';
  697. if ($data['current'] == $i) {
  698. $pageContent = '<li class="active"><a href="#" >'.$i.'</a></li>';
  699. }
  700. $render .= $pageContent;
  701. }
  702. $render .= '</ul>';
  703. }
  704. return $render;
  705. };
  706. $mainQuestionList = getQuestions(
  707. false,
  708. $start,
  709. $length,
  710. $exerciseId,
  711. $courseCategoryId,
  712. $selected_course,
  713. $session_id,
  714. $exerciseLevel,
  715. $answerType,
  716. $questionId,
  717. $description
  718. );
  719. // build the line of the array to display questions
  720. // Actions are different if you launch the question_pool page
  721. // They are different too if you have displayed questions from your course
  722. // Or from another course you are the admin(or session admin)
  723. // from a test or not
  724. /*
  725. +--------------------------------------------+--------------------------------------------+
  726. | NOT IN A TEST | IN A TEST |
  727. +----------------------+---------------------+---------------------+----------------------+
  728. |IN THE COURSE (*) "x | NOT IN THE COURSE o | IN THE COURSE + | NOT IN THE COURSE o |
  729. +----------------------+---------------------+---------------------+----------------------+
  730. |Edit the question | Do nothing | Add question to test|Clone question in test|
  731. |Delete the question | | | |
  732. |(true delete) | | | |
  733. +----------------------+---------------------+---------------------+----------------------+
  734. (*) this is the only way to delete or modify orphan questions
  735. */
  736. if ($fromExercise <= 0) {
  737. // NOT IN A TEST - NOT IN THE COURSE
  738. $actionLabel = get_lang('Reuse');
  739. $actionIcon1 = get_lang('MustBeInATest');
  740. $actionIcon2 = '';
  741. // We are not in this course, to messy if we link to the question in another course
  742. $questionTagA = 0;
  743. if ($selected_course == api_get_course_int_id()) {
  744. // NOT IN A TEST - IN THE COURSE
  745. $actionLabel = get_lang('Modify');
  746. $actionIcon1 = 'edit';
  747. $actionIcon2 = 'delete';
  748. // We are in the course, question title can be a link to the question edit page
  749. $questionTagA = 1;
  750. }
  751. } else {
  752. // IN A TEST - NOT IN THE COURSE
  753. $actionLabel = get_lang('ReUseACopyInCurrentTest');
  754. $actionIcon1 = 'clone';
  755. $actionIcon2 = '';
  756. $questionTagA = 0;
  757. if ($selected_course == api_get_course_int_id()) {
  758. // IN A TEST - IN THE COURSE
  759. $actionLabel = get_lang('Reuse');
  760. $actionIcon1 = 'add';
  761. $actionIcon2 = '';
  762. $questionTagA = 1;
  763. }
  764. }
  765. $data = [];
  766. if (is_array($mainQuestionList)) {
  767. foreach ($mainQuestionList as $question) {
  768. $row = [];
  769. // This function checks if the question can be read
  770. $question_type = get_question_type_for_question($selected_course, $question['id']);
  771. if (empty($question_type)) {
  772. continue;
  773. }
  774. $sessionId = isset($question['session_id']) ? $question['session_id'] : null;
  775. $exerciseName = isset($question['exercise_name']) ? '<br />('.$question['exercise_id'].') ' : null;
  776. if (!$objExercise->hasQuestion($question['id'])) {
  777. $row[] = Display::input(
  778. 'checkbox',
  779. 'questions[]',
  780. $question['id'],
  781. ['class' => 'question_checkbox']
  782. );
  783. } else {
  784. $row[] = '';
  785. }
  786. $row[] = getLinkForQuestion(
  787. $questionTagA,
  788. $fromExercise,
  789. $question['id'],
  790. $question['type'],
  791. $question['question'],
  792. $sessionId,
  793. $question['exerciseId']
  794. ).$exerciseName;
  795. $row[] = $question_type;
  796. $row[] = TestCategory::getCategoryNameForQuestion($question['id'], $selected_course);
  797. $row[] = $question['level'];
  798. $row[] = get_action_icon_for_question(
  799. $actionIcon1,
  800. $fromExercise,
  801. $question['id'],
  802. $question['type'],
  803. $question['question'],
  804. $selected_course,
  805. $courseCategoryId,
  806. $exerciseLevel,
  807. $answerType,
  808. $session_id,
  809. $question['exerciseId'],
  810. $objExercise
  811. ).'&nbsp;'.
  812. get_action_icon_for_question(
  813. $actionIcon2,
  814. $fromExercise,
  815. $question['id'],
  816. $question['type'],
  817. $question['question'],
  818. $selected_course,
  819. $courseCategoryId,
  820. $exerciseLevel,
  821. $answerType,
  822. $session_id,
  823. $question['exerciseId'],
  824. $objExercise
  825. );
  826. $data[] = $row;
  827. }
  828. }
  829. // Display table
  830. $header = [
  831. [
  832. '',
  833. false,
  834. ['style' => 'text-align:center'],
  835. ['style' => 'text-align:center'],
  836. '',
  837. ],
  838. [
  839. get_lang('QuestionUpperCaseFirstLetter'),
  840. false,
  841. ['style' => 'text-align:center'],
  842. '',
  843. ],
  844. [
  845. get_lang('Type'),
  846. false,
  847. ['style' => 'text-align:center'],
  848. ['style' => 'text-align:center'],
  849. '',
  850. ],
  851. [
  852. get_lang('QuestionCategory'),
  853. false,
  854. ['style' => 'text-align:center'],
  855. ['style' => 'text-align:center'],
  856. '',
  857. ],
  858. [
  859. get_lang('Difficulty'),
  860. false,
  861. ['style' => 'text-align:center'],
  862. ['style' => 'text-align:center'],
  863. '',
  864. ],
  865. [
  866. $actionLabel,
  867. false,
  868. ['style' => 'text-align:center'],
  869. ['style' => 'text-align:center'],
  870. '',
  871. ],
  872. ];
  873. echo $pagination;
  874. echo '<form id="question_pool_id" method="get" action="'.$url.'">';
  875. echo '<input type="hidden" name="fromExercise" value="'.$fromExercise.'">';
  876. echo '<input type="hidden" name="cidReq" value="'.$_course['code'].'">';
  877. echo '<input type="hidden" name="selected_course" value="'.$selected_course.'">';
  878. echo '<input type="hidden" name="course_id" value="'.$selected_course.'">';
  879. Display::display_sortable_table(
  880. $header,
  881. $data,
  882. '',
  883. ['per_page_default' => 999, 'per_page' => 999, 'page_nr' => 1]
  884. );
  885. echo '</form>';
  886. $tableId = 'question_pool_id';
  887. $html = '<div class="btn-toolbar">';
  888. $html .= '<div class="btn-group">';
  889. $html .= '<a class="btn btn-default" href="?'.$url.'selectall=1" onclick="javascript: setCheckbox(true, \''.$tableId.'\'); return false;">'.
  890. get_lang('SelectAll').'</a>';
  891. $html .= '<a class="btn btn-default" href="?'.$url.'" onclick="javascript: setCheckbox(false, \''.$tableId.'\'); return false;">'.get_lang('UnSelectAll').'</a> ';
  892. $html .= '</div>';
  893. $html .= '<div class="btn-group">
  894. <button class="btn btn-default" onclick="javascript:return false;">'.get_lang('Actions').'</button>
  895. <button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  896. <span class="caret"></span>
  897. </button>';
  898. $html .= '<ul class="dropdown-menu">';
  899. $actionLabel = get_lang('ReUseACopyInCurrentTest');
  900. $actions = ['clone' => get_lang('ReUseACopyInCurrentTest')];
  901. if ($selected_course == api_get_course_int_id()) {
  902. $actions = ['reuse' => get_lang('ReuseQuestion')];
  903. }
  904. foreach ($actions as $action => &$label) {
  905. $html .= '<li>
  906. <a data-action ="'.$action.'" href="#" onclick="javascript:action_click(this, \''.$tableId.'\');">'.
  907. $label.'
  908. </a>
  909. </li>';
  910. }
  911. $html .= '</ul>';
  912. $html .= '</div>'; //btn-group
  913. $html .= '</div>'; //toolbar
  914. echo $html;
  915. Display::display_footer();
  916. /**
  917. * Put the menu entry for level and type to default "Choice"
  918. * It is useful if you change the exercise, you need to reset the other menus.
  919. *
  920. * @author hubert.borderiou 13-10-2011
  921. */
  922. function reset_menu_lvl_type()
  923. {
  924. global $exerciseLevel, $answerType;
  925. $answerType = -1;
  926. $exerciseLevel = -1;
  927. }
  928. /**
  929. * Put the menu entry for exercise and level and type to default "Choice"
  930. * It is useful if you change the course, you need to reset the other menus.
  931. *
  932. * @author hubert.borderiou 13-10-2011
  933. */
  934. function reset_menu_exo_lvl_type()
  935. {
  936. global $exerciseId, $courseCategoryId;
  937. reset_menu_lvl_type();
  938. $exerciseId = 0;
  939. $courseCategoryId = 0;
  940. }
  941. /**
  942. * return the <a> link to admin question, if needed.
  943. *
  944. * @param int $in_addA
  945. * @param int $fromExercise
  946. * @param int $questionId
  947. * @param int $questionType
  948. * @param string $questionName
  949. * @param int $sessionId
  950. * @param int $exerciseId
  951. *
  952. * @return string
  953. *
  954. * @author hubert.borderiou
  955. */
  956. function getLinkForQuestion(
  957. $in_addA,
  958. $fromExercise,
  959. $questionId,
  960. $questionType,
  961. $questionName,
  962. $sessionId,
  963. $exerciseId
  964. ) {
  965. $result = $questionName;
  966. if ($in_addA) {
  967. $sessionIcon = '';
  968. if (!empty($sessionId) && $sessionId != -1) {
  969. $sessionIcon = ' '.Display::return_icon('star.png', get_lang('Session'));
  970. }
  971. $exerciseId = (int) $exerciseId;
  972. $questionId = (int) $questionId;
  973. $questionType = (int) $questionType;
  974. $fromExercise = (int) $fromExercise;
  975. $result = Display::url(
  976. $questionName.$sessionIcon,
  977. 'admin.php?'.api_get_cidreq().
  978. "&exerciseId=$exerciseId&editQuestion=$questionId&type=$questionType&fromExercise=$fromExercise"
  979. );
  980. }
  981. return $result;
  982. }
  983. /**
  984. Return the <a> html code for delete, add, clone, edit a question
  985. in_action = the code of the action triggered by the button
  986. from_exercise = the id of the current exercise from which we click on question pool
  987. in_questionid = the id of the current question
  988. in_questiontype = the code of the type of the current question
  989. in_questionname = the name of the question
  990. in_selected_course = the if of the course chosen in the FILTERING MENU
  991. in_courseCategoryId = the id of the category chosen in the FILTERING MENU
  992. in_exerciseLevel = the level of the exercise chosen in the FILTERING MENU
  993. in_answerType = the code of the type of the question chosen in the FILTERING MENU
  994. in_session_id = the id of the session_id chosen in the FILTERING MENU
  995. in_exercise_id = the id of the exercise chosen in the FILTERING MENU
  996. */
  997. function get_action_icon_for_question(
  998. $in_action,
  999. $from_exercise,
  1000. $in_questionid,
  1001. $in_questiontype,
  1002. $in_questionname,
  1003. $in_selected_course,
  1004. $in_courseCategoryId,
  1005. $in_exerciseLevel,
  1006. $in_answerType,
  1007. $in_session_id,
  1008. $in_exercise_id,
  1009. Exercise $myObjEx
  1010. ) {
  1011. $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
  1012. $getParams = "&selected_course=$in_selected_course&courseCategoryId=$in_courseCategoryId&exerciseId=$in_exercise_id&exerciseLevel=$in_exerciseLevel&answerType=$in_answerType&session_id=$in_session_id";
  1013. $res = '';
  1014. switch ($in_action) {
  1015. case 'delete':
  1016. if ($limitTeacherAccess && !api_is_platform_admin()) {
  1017. break;
  1018. }
  1019. $res = "<a href='".api_get_self()."?".
  1020. api_get_cidreq().$getParams."&delete=$in_questionid' onclick='return confirm_your_choice()'>";
  1021. $res .= Display::return_icon('delete.png', get_lang('Delete'));
  1022. $res .= "</a>";
  1023. break;
  1024. case 'edit':
  1025. $res = getLinkForQuestion(
  1026. 1,
  1027. $from_exercise,
  1028. $in_questionid,
  1029. $in_questiontype,
  1030. Display::return_icon('edit.png', get_lang('Modify')),
  1031. $in_session_id,
  1032. $in_exercise_id
  1033. );
  1034. break;
  1035. case 'add':
  1036. $res = '-';
  1037. if (!$myObjEx->hasQuestion($in_questionid)) {
  1038. $res = "<a href='".api_get_self()."?".
  1039. api_get_cidreq().$getParams."&recup=$in_questionid&fromExercise=$from_exercise'>";
  1040. $res .= Display::return_icon('view_more_stats.gif', get_lang('InsertALinkToThisQuestionInTheExercise'));
  1041. $res .= '</a>';
  1042. }
  1043. break;
  1044. case 'clone':
  1045. $url = api_get_self().'?'.api_get_cidreq().$getParams.
  1046. "&question_copy=$in_questionid&course_id=$in_selected_course&fromExercise=$from_exercise";
  1047. $res = Display::url(
  1048. Display::return_icon('cd.png', get_lang('ReUseACopyInCurrentTest')),
  1049. $url
  1050. );
  1051. break;
  1052. default:
  1053. $res = $in_action;
  1054. break;
  1055. }
  1056. return $res;
  1057. }
  1058. /**
  1059. * Return the icon for the question type.
  1060. *
  1061. * @author hubert.borderiou 13-10-2011
  1062. */
  1063. function get_question_type_for_question($in_selectedcourse, $in_questionid)
  1064. {
  1065. $courseInfo = api_get_course_info_by_id($in_selectedcourse);
  1066. $myObjQuestion = Question::read($in_questionid, $courseInfo);
  1067. $questionType = null;
  1068. if (!empty($myObjQuestion)) {
  1069. list($typeImg, $typeExpl) = $myObjQuestion->get_type_icon_html();
  1070. $questionType = Display::tag('div', Display::return_icon($typeImg, $typeExpl, [], 32), []);
  1071. }
  1072. return $questionType;
  1073. }