question_pool.php 42 KB

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