question_pool.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. }