question_pool.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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 (in_array(
  426. $objExercise->getFeedbackType(),
  427. [EXERCISE_FEEDBACK_TYPE_DIRECT, EXERCISE_FEEDBACK_TYPE_POPUP]
  428. )) {
  429. if (!in_array($key, [HOT_SPOT_DELINEATION, UNIQUE_ANSWER])) {
  430. continue;
  431. }
  432. $new_question_list[$key] = get_lang($item[1]);
  433. } else {
  434. if ($key == HOT_SPOT_DELINEATION) {
  435. continue;
  436. }
  437. $new_question_list[$key] = get_lang($item[1]);
  438. }
  439. }
  440. }
  441. // Answer type list
  442. $select_answer_html = Display::select(
  443. 'answerType',
  444. $new_question_list,
  445. $answerType,
  446. ['onchange' => 'submit_form(this);'],
  447. false
  448. );
  449. echo Display::form_row(get_lang('AnswerType'), $select_answer_html);
  450. echo Display::form_row(get_lang('Id'), Display::input('text', 'question_id', $questionId));
  451. echo Display::form_row(
  452. get_lang('Description'),
  453. Display::input('text', 'description', Security::remove_XSS($description))
  454. );
  455. $button = '<button class="btn btn-primary save" type="submit" name="name" value="'.get_lang('Filter').'">'.
  456. get_lang('Filter').'</button>';
  457. echo Display::form_row('', $button);
  458. echo "<input type='hidden' id='course_id_changed' name='course_id_changed' value='0' />";
  459. echo "<input type='hidden' id='exercise_id_changed' name='exercise_id_changed' value='0' />";
  460. ?>
  461. </form>
  462. <div class="clear"></div>
  463. <?php
  464. function getQuestions(
  465. $getCount,
  466. $start,
  467. $length,
  468. $exerciseId,
  469. $courseCategoryId,
  470. $selected_course,
  471. $session_id,
  472. $exerciseLevel,
  473. $answerType,
  474. $questionId,
  475. $description
  476. ) {
  477. $start = (int) $start;
  478. $length = (int) $length;
  479. $exerciseId = (int) $exerciseId;
  480. $courseCategoryId = (int) $courseCategoryId;
  481. $selected_course = (int) $selected_course;
  482. $session_id = (int) $session_id;
  483. $exerciseLevel = (int) $exerciseLevel;
  484. $answerType = (int) $answerType;
  485. $questionId = (int) $questionId;
  486. $description = Database::escape_string($description);
  487. $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  488. $TBL_EXERCISES = Database::get_course_table(TABLE_QUIZ_TEST);
  489. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  490. $TBL_COURSE_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
  491. // if we have selected an exercise in the list-box 'Filter'
  492. if ($exerciseId > 0) {
  493. $where = '';
  494. $from = '';
  495. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  496. $from = ", $TBL_COURSE_REL_CATEGORY crc ";
  497. $where .= " AND
  498. crc.c_id = $selected_course AND
  499. crc.question_id = qu.id AND
  500. crc.category_id = $courseCategoryId";
  501. }
  502. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  503. $where .= ' AND level='.$exerciseLevel;
  504. }
  505. if (isset($answerType) && $answerType > 0) {
  506. $where .= ' AND type='.$answerType;
  507. }
  508. if (!empty($questionId)) {
  509. $where .= ' AND qu.iid='.$questionId;
  510. }
  511. if (!empty($description)) {
  512. $where .= " AND qu.description LIKE '%$description%'";
  513. }
  514. $select = 'DISTINCT
  515. id,
  516. question,
  517. type,
  518. level,
  519. qt.exercice_id exerciseId';
  520. if ($getCount) {
  521. $select = 'count(qu.iid) as count';
  522. }
  523. $sql = "SELECT $select
  524. FROM
  525. $TBL_EXERCISE_QUESTION qt,
  526. $TBL_QUESTIONS qu
  527. $from
  528. WHERE
  529. qt.question_id = qu.id AND
  530. qt.exercice_id = $exerciseId AND
  531. qt.c_id = $selected_course AND
  532. qu.c_id = $selected_course
  533. $where
  534. ORDER BY question_order";
  535. } elseif ($exerciseId == -1) {
  536. // If we have selected the option 'Orphan questions' in the list-box 'Filter'
  537. $level_where = '';
  538. $from = '';
  539. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  540. $from = " INNER JOIN $TBL_COURSE_REL_CATEGORY crc
  541. ON crc.question_id = q.id AND crc.c_id = q.c_id ";
  542. $level_where .= " AND
  543. crc.c_id = $selected_course AND
  544. crc.category_id = $courseCategoryId";
  545. }
  546. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  547. $level_where = ' AND level='.$exerciseLevel;
  548. }
  549. $answer_where = '';
  550. if (isset($answerType) && $answerType > 0 - 1) {
  551. $answer_where = ' AND type='.$answerType;
  552. }
  553. if (!empty($questionId)) {
  554. $answer_where .= ' AND q.iid='.$questionId;
  555. }
  556. if (!empty($description)) {
  557. $answer_where .= " AND q.description LIKE '%$description%'";
  558. }
  559. $select = ' q.*, r.exercice_id exerciseId ';
  560. if ($getCount) {
  561. $select = 'count(q.iid) as count';
  562. }
  563. // @todo fix this query with the new id field
  564. $sql = " (
  565. SELECT $select
  566. FROM $TBL_QUESTIONS q
  567. INNER JOIN $TBL_EXERCISE_QUESTION r
  568. ON (q.c_id = r.c_id AND q.id = r.question_id)
  569. INNER JOIN $TBL_EXERCISES ex
  570. ON (ex.id = r.exercice_id AND ex.c_id = r.c_id)
  571. $from
  572. WHERE
  573. ex.c_id = '$selected_course' AND
  574. ex.active = '-1'
  575. $level_where
  576. $answer_where
  577. )
  578. UNION
  579. (
  580. SELECT $select
  581. FROM $TBL_QUESTIONS q
  582. LEFT OUTER JOIN $TBL_EXERCISE_QUESTION r
  583. ON (q.c_id = r.c_id AND q.id = r.question_id)
  584. $from
  585. WHERE
  586. q.c_id = '$selected_course' AND
  587. r.question_id is null
  588. $level_where
  589. $answer_where
  590. )
  591. UNION
  592. (
  593. SELECT $select
  594. FROM $TBL_QUESTIONS q
  595. INNER JOIN $TBL_EXERCISE_QUESTION r
  596. ON (q.c_id = r.c_id AND q.id = r.question_id)
  597. $from
  598. WHERE
  599. r.c_id = '$selected_course' AND
  600. (r.exercice_id = '-1' OR r.exercice_id = '0')
  601. $level_where
  602. $answer_where
  603. )
  604. ";
  605. if ($getCount) {
  606. $sql = "SELECT SUM(count) count FROM ($sql) as total";
  607. }
  608. } else {
  609. // All tests for selected course
  610. // If we have not selected any option in the list-box 'Filter'
  611. $filter = '';
  612. $from = '';
  613. if (isset($courseCategoryId) && $courseCategoryId > 0) {
  614. $from = ", $TBL_COURSE_REL_CATEGORY crc ";
  615. $filter .= " AND
  616. crc.c_id = $selected_course AND
  617. crc.question_id = qu.id AND
  618. crc.category_id = $courseCategoryId";
  619. }
  620. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  621. $filter .= ' AND level='.$exerciseLevel.' ';
  622. }
  623. if (isset($answerType) && $answerType > 0) {
  624. $filter .= ' AND qu.type='.$answerType.' ';
  625. }
  626. if (!empty($questionId)) {
  627. $filter .= ' AND qu.iid='.$questionId;
  628. }
  629. if (!empty($description)) {
  630. $filter .= " AND qu.description LIKE '%$description%'";
  631. }
  632. if ($session_id == -1 || empty($session_id)) {
  633. $session_id = 0;
  634. }
  635. $sessionCondition = api_get_session_condition($session_id, true, 'q.session_id');
  636. $select = 'qu.id, question, qu.type, level, q.session_id, qt.exercice_id exerciseId ';
  637. if ($getCount) {
  638. $select = 'count(qu.iid) as count';
  639. }
  640. // All tests for the course selected, not in session
  641. $sql = "SELECT DISTINCT
  642. $select
  643. FROM
  644. $TBL_QUESTIONS as qu,
  645. $TBL_EXERCISE_QUESTION as qt,
  646. $TBL_EXERCISES as q
  647. $from
  648. WHERE
  649. qu.c_id = $selected_course AND
  650. qt.c_id = $selected_course AND
  651. q.c_id = $selected_course AND
  652. qu.id = qt.question_id
  653. $sessionCondition AND
  654. q.id = qt.exercice_id
  655. $filter
  656. ORDER BY session_id ASC";
  657. }
  658. if ($getCount) {
  659. $result = Database::query($sql);
  660. $row = Database::fetch_array($result, 'ASSOC');
  661. return (int) $row['count'];
  662. }
  663. $sql .= " LIMIT $start, $length";
  664. $result = Database::query($sql);
  665. $mainQuestionList = [];
  666. while ($row = Database::fetch_array($result, 'ASSOC')) {
  667. $mainQuestionList[] = $row;
  668. }
  669. return $mainQuestionList;
  670. }
  671. $nbrQuestions = getQuestions(
  672. true,
  673. null,
  674. null,
  675. $exerciseId,
  676. $courseCategoryId,
  677. $selected_course,
  678. $session_id,
  679. $exerciseLevel,
  680. $answerType,
  681. $questionId,
  682. $description
  683. );
  684. $length = api_get_configuration_value('question_pagination_length');
  685. if (empty($length)) {
  686. $length = 20;
  687. }
  688. $start = ($page - 1) * $length;
  689. $paginator = new Paginator();
  690. $pagination = $paginator->paginate([]);
  691. $pagination->setTotalItemCount($nbrQuestions);
  692. $pagination->setItemNumberPerPage($length);
  693. $pagination->setCurrentPageNumber($page);
  694. $pagination->renderer = function ($data) use ($url) {
  695. $render = '';
  696. if ($data['pageCount'] > 1) {
  697. $render = '<ul class="pagination">';
  698. for ($i = 1; $i <= $data['pageCount']; $i++) {
  699. $pageContent = '<li><a href="'.$url.'&page='.$i.'">'.$i.'</a></li>';
  700. if ($data['current'] == $i) {
  701. $pageContent = '<li class="active"><a href="#" >'.$i.'</a></li>';
  702. }
  703. $render .= $pageContent;
  704. }
  705. $render .= '</ul>';
  706. }
  707. return $render;
  708. };
  709. $mainQuestionList = getQuestions(
  710. false,
  711. $start,
  712. $length,
  713. $exerciseId,
  714. $courseCategoryId,
  715. $selected_course,
  716. $session_id,
  717. $exerciseLevel,
  718. $answerType,
  719. $questionId,
  720. $description
  721. );
  722. // build the line of the array to display questions
  723. // Actions are different if you launch the question_pool page
  724. // They are different too if you have displayed questions from your course
  725. // Or from another course you are the admin(or session admin)
  726. // from a test or not
  727. /*
  728. +--------------------------------------------+--------------------------------------------+
  729. | NOT IN A TEST | IN A TEST |
  730. +----------------------+---------------------+---------------------+----------------------+
  731. |IN THE COURSE (*) "x | NOT IN THE COURSE o | IN THE COURSE + | NOT IN THE COURSE o |
  732. +----------------------+---------------------+---------------------+----------------------+
  733. |Edit the question | Do nothing | Add question to test|Clone question in test|
  734. |Delete the question | | | |
  735. |(true delete) | | | |
  736. +----------------------+---------------------+---------------------+----------------------+
  737. (*) this is the only way to delete or modify orphan questions
  738. */
  739. if ($fromExercise <= 0) {
  740. // NOT IN A TEST - NOT IN THE COURSE
  741. $actionLabel = get_lang('Reuse');
  742. $actionIcon1 = get_lang('MustBeInATest');
  743. $actionIcon2 = '';
  744. // We are not in this course, to messy if we link to the question in another course
  745. $questionTagA = 0;
  746. if ($selected_course == api_get_course_int_id()) {
  747. // NOT IN A TEST - IN THE COURSE
  748. $actionLabel = get_lang('Modify');
  749. $actionIcon1 = 'edit';
  750. $actionIcon2 = 'delete';
  751. // We are in the course, question title can be a link to the question edit page
  752. $questionTagA = 1;
  753. }
  754. } else {
  755. // IN A TEST - NOT IN THE COURSE
  756. $actionLabel = get_lang('ReUseACopyInCurrentTest');
  757. $actionIcon1 = 'clone';
  758. $actionIcon2 = '';
  759. $questionTagA = 0;
  760. if ($selected_course == api_get_course_int_id()) {
  761. // IN A TEST - IN THE COURSE
  762. $actionLabel = get_lang('Reuse');
  763. $actionIcon1 = 'add';
  764. $actionIcon2 = '';
  765. $questionTagA = 1;
  766. }
  767. }
  768. $data = [];
  769. if (is_array($mainQuestionList)) {
  770. foreach ($mainQuestionList as $question) {
  771. $row = [];
  772. // This function checks if the question can be read
  773. $question_type = get_question_type_for_question($selected_course, $question['id']);
  774. if (empty($question_type)) {
  775. continue;
  776. }
  777. $sessionId = isset($question['session_id']) ? $question['session_id'] : null;
  778. $exerciseName = isset($question['exercise_name']) ? '<br />('.$question['exercise_id'].') ' : null;
  779. if (!$objExercise->hasQuestion($question['id'])) {
  780. $row[] = Display::input(
  781. 'checkbox',
  782. 'questions[]',
  783. $question['id'],
  784. ['class' => 'question_checkbox']
  785. );
  786. } else {
  787. $row[] = '';
  788. }
  789. $row[] = getLinkForQuestion(
  790. $questionTagA,
  791. $fromExercise,
  792. $question['id'],
  793. $question['type'],
  794. $question['question'],
  795. $sessionId,
  796. $question['exerciseId']
  797. ).$exerciseName;
  798. $row[] = $question_type;
  799. $row[] = TestCategory::getCategoryNameForQuestion($question['id'], $selected_course);
  800. $row[] = $question['level'];
  801. $row[] = get_action_icon_for_question(
  802. $actionIcon1,
  803. $fromExercise,
  804. $question['id'],
  805. $question['type'],
  806. $question['question'],
  807. $selected_course,
  808. $courseCategoryId,
  809. $exerciseLevel,
  810. $answerType,
  811. $session_id,
  812. $question['exerciseId'],
  813. $objExercise
  814. ).'&nbsp;'.
  815. get_action_icon_for_question(
  816. $actionIcon2,
  817. $fromExercise,
  818. $question['id'],
  819. $question['type'],
  820. $question['question'],
  821. $selected_course,
  822. $courseCategoryId,
  823. $exerciseLevel,
  824. $answerType,
  825. $session_id,
  826. $question['exerciseId'],
  827. $objExercise
  828. );
  829. $data[] = $row;
  830. }
  831. }
  832. // Display table
  833. $header = [
  834. [
  835. '',
  836. false,
  837. ['style' => 'text-align:center'],
  838. ['style' => 'text-align:center'],
  839. '',
  840. ],
  841. [
  842. get_lang('QuestionUpperCaseFirstLetter'),
  843. false,
  844. ['style' => 'text-align:center'],
  845. '',
  846. ],
  847. [
  848. get_lang('Type'),
  849. false,
  850. ['style' => 'text-align:center'],
  851. ['style' => 'text-align:center'],
  852. '',
  853. ],
  854. [
  855. get_lang('QuestionCategory'),
  856. false,
  857. ['style' => 'text-align:center'],
  858. ['style' => 'text-align:center'],
  859. '',
  860. ],
  861. [
  862. get_lang('Difficulty'),
  863. false,
  864. ['style' => 'text-align:center'],
  865. ['style' => 'text-align:center'],
  866. '',
  867. ],
  868. [
  869. $actionLabel,
  870. false,
  871. ['style' => 'text-align:center'],
  872. ['style' => 'text-align:center'],
  873. '',
  874. ],
  875. ];
  876. echo $pagination;
  877. echo '<form id="question_pool_id" method="get" action="'.$url.'">';
  878. echo '<input type="hidden" name="fromExercise" value="'.$fromExercise.'">';
  879. echo '<input type="hidden" name="cidReq" value="'.$_course['code'].'">';
  880. echo '<input type="hidden" name="selected_course" value="'.$selected_course.'">';
  881. echo '<input type="hidden" name="course_id" value="'.$selected_course.'">';
  882. Display::display_sortable_table(
  883. $header,
  884. $data,
  885. '',
  886. ['per_page_default' => 999, 'per_page' => 999, 'page_nr' => 1]
  887. );
  888. echo '</form>';
  889. $tableId = 'question_pool_id';
  890. $html = '<div class="btn-toolbar">';
  891. $html .= '<div class="btn-group">';
  892. $html .= '<a class="btn btn-default" href="?'.$url.'selectall=1" onclick="javascript: setCheckbox(true, \''.$tableId.'\'); return false;">'.
  893. get_lang('SelectAll').'</a>';
  894. $html .= '<a class="btn btn-default" href="?'.$url.'" onclick="javascript: setCheckbox(false, \''.$tableId.'\'); return false;">'.get_lang('UnSelectAll').'</a> ';
  895. $html .= '</div>';
  896. $html .= '<div class="btn-group">
  897. <button class="btn btn-default" onclick="javascript:return false;">'.get_lang('Actions').'</button>
  898. <button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  899. <span class="caret"></span>
  900. </button>';
  901. $html .= '<ul class="dropdown-menu">';
  902. $actionLabel = get_lang('ReUseACopyInCurrentTest');
  903. $actions = ['clone' => get_lang('ReUseACopyInCurrentTest')];
  904. if ($selected_course == api_get_course_int_id()) {
  905. $actions = ['reuse' => get_lang('ReuseQuestion')];
  906. }
  907. foreach ($actions as $action => &$label) {
  908. $html .= '<li>
  909. <a data-action ="'.$action.'" href="#" onclick="javascript:action_click(this, \''.$tableId.'\');">'.
  910. $label.'
  911. </a>
  912. </li>';
  913. }
  914. $html .= '</ul>';
  915. $html .= '</div>'; //btn-group
  916. $html .= '</div>'; //toolbar
  917. echo $html;
  918. Display::display_footer();
  919. /**
  920. * Put the menu entry for level and type to default "Choice"
  921. * It is useful if you change the exercise, you need to reset the other menus.
  922. *
  923. * @author hubert.borderiou 13-10-2011
  924. */
  925. function reset_menu_lvl_type()
  926. {
  927. global $exerciseLevel, $answerType;
  928. $answerType = -1;
  929. $exerciseLevel = -1;
  930. }
  931. /**
  932. * Put the menu entry for exercise and level and type to default "Choice"
  933. * It is useful if you change the course, you need to reset the other menus.
  934. *
  935. * @author hubert.borderiou 13-10-2011
  936. */
  937. function reset_menu_exo_lvl_type()
  938. {
  939. global $exerciseId, $courseCategoryId;
  940. reset_menu_lvl_type();
  941. $exerciseId = 0;
  942. $courseCategoryId = 0;
  943. }
  944. /**
  945. * return the <a> link to admin question, if needed.
  946. *
  947. * @param int $in_addA
  948. * @param int $fromExercise
  949. * @param int $questionId
  950. * @param int $questionType
  951. * @param string $questionName
  952. * @param int $sessionId
  953. * @param int $exerciseId
  954. *
  955. * @return string
  956. *
  957. * @author hubert.borderiou
  958. */
  959. function getLinkForQuestion(
  960. $in_addA,
  961. $fromExercise,
  962. $questionId,
  963. $questionType,
  964. $questionName,
  965. $sessionId,
  966. $exerciseId
  967. ) {
  968. $result = $questionName;
  969. if ($in_addA) {
  970. $sessionIcon = '';
  971. if (!empty($sessionId) && $sessionId != -1) {
  972. $sessionIcon = ' '.Display::return_icon('star.png', get_lang('Session'));
  973. }
  974. $exerciseId = (int) $exerciseId;
  975. $questionId = (int) $questionId;
  976. $questionType = (int) $questionType;
  977. $fromExercise = (int) $fromExercise;
  978. $result = Display::url(
  979. $questionName.$sessionIcon,
  980. 'admin.php?'.api_get_cidreq().
  981. "&exerciseId=$exerciseId&editQuestion=$questionId&type=$questionType&fromExercise=$fromExercise"
  982. );
  983. }
  984. return $result;
  985. }
  986. /**
  987. Return the <a> html code for delete, add, clone, edit a question
  988. in_action = the code of the action triggered by the button
  989. from_exercise = the id of the current exercise from which we click on question pool
  990. in_questionid = the id of the current question
  991. in_questiontype = the code of the type of the current question
  992. in_questionname = the name of the question
  993. in_selected_course = the if of the course chosen in the FILTERING MENU
  994. in_courseCategoryId = the id of the category chosen in the FILTERING MENU
  995. in_exerciseLevel = the level of the exercise chosen in the FILTERING MENU
  996. in_answerType = the code of the type of the question chosen in the FILTERING MENU
  997. in_session_id = the id of the session_id chosen in the FILTERING MENU
  998. in_exercise_id = the id of the exercise chosen in the FILTERING MENU
  999. */
  1000. function get_action_icon_for_question(
  1001. $in_action,
  1002. $from_exercise,
  1003. $in_questionid,
  1004. $in_questiontype,
  1005. $in_questionname,
  1006. $in_selected_course,
  1007. $in_courseCategoryId,
  1008. $in_exerciseLevel,
  1009. $in_answerType,
  1010. $in_session_id,
  1011. $in_exercise_id,
  1012. Exercise $myObjEx
  1013. ) {
  1014. $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
  1015. $getParams = "&selected_course=$in_selected_course&courseCategoryId=$in_courseCategoryId&exerciseId=$in_exercise_id&exerciseLevel=$in_exerciseLevel&answerType=$in_answerType&session_id=$in_session_id";
  1016. $res = '';
  1017. switch ($in_action) {
  1018. case 'delete':
  1019. if ($limitTeacherAccess && !api_is_platform_admin()) {
  1020. break;
  1021. }
  1022. $res = "<a href='".api_get_self()."?".
  1023. api_get_cidreq().$getParams."&delete=$in_questionid' onclick='return confirm_your_choice()'>";
  1024. $res .= Display::return_icon('delete.png', get_lang('Delete'));
  1025. $res .= "</a>";
  1026. break;
  1027. case 'edit':
  1028. $res = getLinkForQuestion(
  1029. 1,
  1030. $from_exercise,
  1031. $in_questionid,
  1032. $in_questiontype,
  1033. Display::return_icon('edit.png', get_lang('Modify')),
  1034. $in_session_id,
  1035. $in_exercise_id
  1036. );
  1037. break;
  1038. case 'add':
  1039. $res = '-';
  1040. if (!$myObjEx->hasQuestion($in_questionid)) {
  1041. $res = "<a href='".api_get_self()."?".
  1042. api_get_cidreq().$getParams."&recup=$in_questionid&fromExercise=$from_exercise'>";
  1043. $res .= Display::return_icon('view_more_stats.gif', get_lang('InsertALinkToThisQuestionInTheExercise'));
  1044. $res .= '</a>';
  1045. }
  1046. break;
  1047. case 'clone':
  1048. $url = api_get_self().'?'.api_get_cidreq().$getParams.
  1049. "&question_copy=$in_questionid&course_id=$in_selected_course&fromExercise=$from_exercise";
  1050. $res = Display::url(
  1051. Display::return_icon('cd.png', get_lang('ReUseACopyInCurrentTest')),
  1052. $url
  1053. );
  1054. break;
  1055. default:
  1056. $res = $in_action;
  1057. break;
  1058. }
  1059. return $res;
  1060. }
  1061. /**
  1062. * Return the icon for the question type.
  1063. *
  1064. * @author hubert.borderiou 13-10-2011
  1065. */
  1066. function get_question_type_for_question($in_selectedcourse, $in_questionid)
  1067. {
  1068. $courseInfo = api_get_course_info_by_id($in_selectedcourse);
  1069. $myObjQuestion = Question::read($in_questionid, $courseInfo);
  1070. $questionType = null;
  1071. if (!empty($myObjQuestion)) {
  1072. list($typeImg, $typeExpl) = $myObjQuestion->get_type_icon_html();
  1073. $questionType = Display::tag('div', Display::return_icon($typeImg, $typeExpl, [], 32), []);
  1074. }
  1075. return $questionType;
  1076. }