question_pool.php 42 KB

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