question_pool.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php // $Id: question_pool.php 20451 2009-05-10 12:02:22Z ivantcholakov $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2009 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. /**
  21. * Question Pool
  22. * This script allows administrators to manage questions and add them into their exercises.
  23. * One question can be in several exercises
  24. * @package dokeos.exercise
  25. * @author Olivier Brouckaert
  26. * @version $Id: question_pool.php 20451 2009-05-10 12:02:22Z ivantcholakov $
  27. */
  28. // name of the language file that needs to be included
  29. $language_file='exercice';
  30. include('exercise.class.php');
  31. include('question.class.php');
  32. include('answer.class.php');
  33. require_once '../inc/global.inc.php';
  34. $this_section=SECTION_COURSES;
  35. $is_allowedToEdit=api_is_allowed_to_edit(null,true);
  36. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  37. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  38. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  39. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  40. if ( empty ( $delete ) ) {
  41. $delete = intval($_GET['delete']);
  42. }
  43. if ( empty ( $recup ) ) {
  44. $recup = intval($_GET['recup']);
  45. }
  46. if ( empty ( $fromExercise ) ) {
  47. $fromExercise = intval($_GET['fromExercise']);
  48. }
  49. if(isset($_GET['exerciseId'])){
  50. $exerciseId = intval($_GET['exerciseId']);
  51. }
  52. if(isset($_GET['exerciseLevel'])){
  53. $exerciseLevel = intval($_GET['exerciseLevel']);
  54. }
  55. if(isset($_GET['answerType'])){
  56. $answerType = intval($_GET['answerType']);
  57. }
  58. if(!empty($_GET['page'])){
  59. $page = intval($_GET['page']);
  60. }
  61. //only that type of question
  62. if(!empty($_GET['type'])){
  63. $type = intval($_GET['type']);
  64. }
  65. // maximum number of questions on a same page
  66. $limitQuestPage=50;
  67. // document path
  68. $documentPath=api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  69. // picture path
  70. $picturePath=$documentPath.'/images';
  71. if(!($objExcercise instanceOf Exercise) && !empty($fromExercise))
  72. {
  73. $objExercise = new Exercise();
  74. $objExercise->read($fromExercise);
  75. }
  76. if(!($objExcercise instanceOf Exercise) && !empty($exerciseId))
  77. {
  78. $objExercise = new Exercise();
  79. $objExercise->read($exerciseId);
  80. }
  81. if($is_allowedToEdit)
  82. {
  83. // deletes a question from the data base and all exercises
  84. if($delete)
  85. {
  86. // construction of the Question object
  87. // if the question exists
  88. if($objQuestionTmp = Question::read($delete))
  89. {
  90. // deletes the question from all exercises
  91. $objQuestionTmp->delete();
  92. }
  93. // destruction of the Question object
  94. unset($objQuestionTmp);
  95. }
  96. // gets an existing question and copies it into a new exercise
  97. elseif($recup && $fromExercise)
  98. {
  99. // if the question exists
  100. if($objQuestionTmp = Question :: read($recup))
  101. {
  102. // adds the exercise ID represented by $fromExercise into the list of exercises for the current question
  103. $objQuestionTmp->addToList($fromExercise);
  104. }
  105. // destruction of the Question object
  106. unset($objQuestionTmp);
  107. if(!$objExcercise instanceOf Exercise)
  108. {
  109. $objExercise = new Exercise();
  110. $objExercise->read($fromExercise);
  111. }
  112. // adds the question ID represented by $recup into the list of questions for the current exercise
  113. $objExercise->addToList($recup);
  114. api_session_register('objExercise');
  115. header("Location: admin.php?".api_get_cidreq()."&exerciseId=$fromExercise");
  116. exit();
  117. } else if( isset($_POST['recup']) && is_array($_POST['recup']) && $fromExercise) {
  118. $list_recup = $_POST['recup'];
  119. foreach ($list_recup as $recup) {
  120. $recup = intval($recup);
  121. // if the question exists
  122. if($objQuestionTmp = Question :: read($recup)) {
  123. // adds the exercise ID represented by $fromExercise into the list of exercises for the current question
  124. $objQuestionTmp->addToList($fromExercise);
  125. }
  126. // destruction of the Question object
  127. unset($objQuestionTmp);
  128. if(!$objExcercise instanceOf Exercise) {
  129. $objExercise = new Exercise();
  130. $objExercise->read($fromExercise);
  131. }
  132. // adds the question ID represented by $recup into the list of questions for the current exercise
  133. $objExercise->addToList($recup);
  134. }
  135. api_session_register('objExercise');
  136. header("Location: admin.php?".api_get_cidreq()."&exerciseId=$fromExercise");
  137. exit();
  138. }
  139. }
  140. if (isset($_SESSION['gradebook'])){
  141. $gradebook= $_SESSION['gradebook'];
  142. }
  143. if (!empty($gradebook) && $gradebook=='view') {
  144. $interbreadcrumb[]= array (
  145. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  146. 'name' => get_lang('Gradebook')
  147. );
  148. }
  149. $nameTools=get_lang('QuestionPool');
  150. $interbreadcrumb[]=array("url" => "exercice.php","name" => get_lang('Exercices'));
  151. // if admin of course
  152. if($is_allowedToEdit)
  153. {
  154. Display::display_header($nameTools,"Exercise");
  155. ?>
  156. <h3><?php echo $nameTools; ?></h3>
  157. <div class="actions">
  158. <?php
  159. if (isset($type)) {
  160. $url = api_get_self().'?type=1';
  161. } else {
  162. $url = api_get_self();
  163. }
  164. ?>
  165. <form method="get" action="<?php echo $url; ?>" style="display:inline;">
  166. <?php
  167. if (isset($type)) {
  168. echo '<input type="hidden" name="type" value="1">';
  169. }
  170. ?>
  171. <input type="hidden" name="fromExercise" value="<?php echo $fromExercise; ?>">
  172. <?php echo get_lang('Filter'); ?> :
  173. <select name="exerciseId">
  174. <option value="0"><?php echo get_lang('AllExercises'); ?></option>
  175. <option value="-1" <?php if($exerciseId == -1) echo 'selected="selected"'; ?>><?php echo get_lang('OrphanQuestions'); ?></option>
  176. <?php
  177. $sql="SELECT id,title FROM $TBL_EXERCICES WHERE id<>'".Database::escape_string($fromExercise)."' AND active<>'-1' ORDER BY id";
  178. $result=Database::query($sql,__FILE__,__LINE__);
  179. // shows a list-box allowing to filter questions
  180. while($row=Database::fetch_array($result)) {
  181. ?>
  182. <option value="<?php echo $row['id']; ?>" <?php if($exerciseId == $row['id']) echo 'selected="selected"'; ?>><?php echo $row['title']; ?></option>
  183. <?php
  184. }
  185. ?>
  186. </select>
  187. &nbsp;
  188. <?php
  189. echo get_lang('Difficulty');
  190. echo ' : <select name="exerciseLevel">';
  191. //echo '<option value="-1">-- '.get_lang('AllExercises').' --</option>';
  192. //level difficulty only from 1 to 5
  193. if (!isset($exerciseLevel)) $exerciseLevel = -1;
  194. for ($level = -1; $level <=5; $level++) {
  195. $selected ='';
  196. if ($level!=0) {
  197. if ($exerciseLevel == $level)
  198. $selected = ' selected="selected" ';
  199. if ($level==-1) {
  200. echo '<option value="'.$level.'" '.$selected.'>'.get_lang('Any').'</option>';
  201. } else {
  202. echo '<option value="'.$level.'" '.$selected.'>'.$level.'</option>';
  203. }
  204. }
  205. }
  206. echo '</select> ';
  207. //
  208. echo get_lang('AnswerType');
  209. echo ' : <select name="answerType">';
  210. //answer type
  211. if (!isset($answerType)) $answerType = -1;
  212. {
  213. for ($answer_type = -1; $answer_type <=6; $answer_type++) {
  214. $selected ='';
  215. if ($answer_type!=0) {
  216. if ($answerType == $answer_type)
  217. $selected = ' selected="selected" ';
  218. if ($answer_type==-1) {echo '<option value="-1" '.$selected.'>'.get_lang('Any').'</option>'; } // check 0 or -1
  219. elseif ($answer_type==1) {echo '<option value="'.$answer_type.'" '.$selected.'>'.get_lang('UniqueAnswer').'</option>'; }
  220. elseif ($answer_type==2) {echo '<option value="'.$answer_type.'" '.$selected.'>'.get_lang('MultipleAnswer').'</option>'; }
  221. elseif ($answer_type==3) {echo '<option value="'.$answer_type.'" '.$selected.'>'.get_lang('langFillBlanks').'</option>'; }
  222. elseif ($answer_type==4) {echo '<option value="'.$answer_type.'" '.$selected.'>'.get_lang('langMatching').'</option>'; }
  223. elseif ($answer_type==5) {echo '<option value="'.$answer_type.'" '.$selected.'>'.get_lang('FreeAnswer').'</option>'; }
  224. elseif ($answer_type==6) {echo '<option value="'.$answer_type.'" '.$selected.'>'.get_lang('HotSpot').'</option>'; }
  225. }
  226. }
  227. }
  228. echo '</select> ';
  229. ?>
  230. <button class="save" type="submit" name="name" value="<?php echo get_lang('Ok') ?>"><?php echo get_lang('Ok') ?></button>
  231. <?php
  232. echo '<a href="admin.php?',api_get_cidreq(),'&exerciseId=',$fromExercise,'">'.Display::return_icon('message_reply_forum.png', get_lang('GoBackToQuestionList')),get_lang('GoBackToQuestionList'),'</a>';
  233. /*if(!empty($fromExercise)) {
  234. echo '<a href="admin.php?',api_get_cidreq(),'&exerciseId=',$fromExercise,'">'.Display::return_icon('message_reply_forum.png', get_lang('GoBackToQuestionList')),get_lang('GoBackToQuestionList'),'</a>';
  235. } else {
  236. echo '<a href="admin.php?'.api_get_cidreq().'&newQuestion=yes">'.Display::return_icon('new_test.gif'),get_lang('NewQu').'</a>';
  237. }*/
  238. ?>
  239. </form>
  240. </div>
  241. <form method="post" action="<?php echo $url.'?'.api_get_cidreq().'&fromExercise='.$fromExercise; ?>" >
  242. <table class="data_table">
  243. <?php
  244. $from=$page*$limitQuestPage;
  245. // if we have selected an exercise in the list-box 'Filter'
  246. if ($exerciseId > 0) {
  247. //$sql="SELECT id,question,type FROM $TBL_EXERCICE_QUESTION,$TBL_QUESTIONS WHERE question_id=id AND exercice_id='".Database::escape_string($exerciseId)."' ORDER BY question_order LIMIT $from, ".($limitQuestPage + 1);
  248. $where = '';
  249. if (isset($type) && $type==1) {
  250. $where = ' type = 1 AND ';
  251. }
  252. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  253. $where .= ' level='.$exerciseLevel.' AND ';
  254. }
  255. if (isset($answerType) && $answerType != -1) {
  256. $where .= ' type='.$answerType.' AND ';
  257. }
  258. $sql="SELECT id,question,type,level
  259. FROM $TBL_EXERCICE_QUESTION,$TBL_QUESTIONS
  260. WHERE $where question_id=id AND exercice_id='".Database::escape_string($exerciseId)."'
  261. ORDER BY question_order";
  262. } elseif($exerciseId == -1) {
  263. // if we have selected the option 'Orphan questions' in the list-box 'Filter'
  264. // 1. Old logic: When a test is deleted, the correspondent records in 'quiz' and 'quiz_rel_question' tables are deleted.
  265. //$sql='SELECT id, question, type, exercice_id FROM '.$TBL_QUESTIONS.' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id WHERE exercice_id IS NULL LIMIT $from, '.($limitQuestPage + 1);
  266. // 2. New logic: When a test is deleted, the field 'active' takes value -1 (it is in the correspondent record in 'quiz' table).
  267. //$sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id FROM '.$TBL_QUESTIONS.
  268. // ' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.
  269. // ' as exercices ON exercice_id=exercices.id WHERE exercices.active = -1 LIMIT $from, '.($limitQuestPage + 1);
  270. // 3. This is more safe to changes, it is a mix between old and new logic.
  271. /*$sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id FROM '.$TBL_QUESTIONS.
  272. ' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.
  273. ' as exercices ON exercice_id=exercices.id WHERE quizz_questions.exercice_id IS NULL OR exercices.active = -1 LIMIT '.$from.', '.($limitQuestPage + 1);
  274. */
  275. /* 4. Query changed because of the Level feature implemented
  276. $sql='SELECT id, question, type, exercice_id,level FROM '.$TBL_QUESTIONS.' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions
  277. ON questions.id=quizz_questions.question_id AND exercice_id IS NULL '.
  278. (!is_null($exerciseLevel) && $exerciseLevel >= 0 ? 'WHERE level=\''.$exerciseLevel.'\' ' : '');
  279. */
  280. // 5. this is the combination of the 3 and 4 query because of the level feature implementation
  281. // we filter the type of question, because in the DirectFeedback we can only add questions with type=1 = UNIQUE_ANSWER
  282. $type_where= '';
  283. if (isset($type) && $type==1) {
  284. $type_where = ' AND questions.type = 1 ';
  285. }
  286. $level_where = '';
  287. if (isset($exerciseLevel) && $exerciseLevel!= -1 ) {
  288. $level_where = ' level='.$exerciseLevel.' AND ';
  289. }
  290. $answer_where = '';
  291. if (isset($answerType) && $answerType!= -1 ) {
  292. $answer_where = ' questions.type='.$answerType.' AND ';
  293. }
  294. $sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id , level
  295. FROM '.$TBL_QUESTIONS.' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions
  296. ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.' as exercices
  297. ON exercice_id=exercices.id
  298. WHERE '.$answer_where.' '.$level_where.' (quizz_questions.exercice_id IS NULL OR exercices.active = -1 ) '.$type_where.'
  299. LIMIT '.$from.', '.($limitQuestPage + 1);
  300. } else {
  301. // if we have not selected any option in the list-box 'Filter'
  302. //$sql="SELECT id,question,type FROM $TBL_QUESTIONS LIMIT $from, ".($limitQuestPage + 1);
  303. $where = '';
  304. if (isset($type)&& $type==1){
  305. $where = ' WHERE type = 1 ';
  306. }
  307. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  308. if (strlen($where)>0)
  309. $where .= ' AND level='.$exerciseLevel.' ';
  310. else
  311. $where = ' WHERE level='.$exerciseLevel.' ';
  312. }
  313. if (isset($answerType) && $answerType != -1) {
  314. if (strlen($where)>0)
  315. $where .= ' AND type='.$answerType.' ';
  316. else
  317. $where = ' WHERE type='.$answerType.' ';
  318. }
  319. $sql="SELECT id,question,type,level FROM $TBL_QUESTIONS $where ";
  320. // forces the value to 0
  321. //echo $sql;
  322. $exerciseId=0;
  323. }
  324. $result=Database::query($sql,__FILE__,__LINE__);
  325. $nbrQuestions=Database::num_rows($result);
  326. echo '<tr>',
  327. '<td colspan="',($fromExercise?4:4),'">',
  328. '<table border="0" cellpadding="0" cellspacing="0" width="100%">',
  329. '<tr>',
  330. '<td>';
  331. echo '</td>',
  332. '<td align="right">';
  333. if(!empty($page)) {
  334. echo '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&fromExercise=',$fromExercise,'&page=',($page-1),'">&lt;&lt; ',get_lang('PreviousPage'),'</a> |';
  335. } elseif($nbrQuestions > $limitQuestPage) {
  336. echo '&lt;&lt; ',get_lang('PreviousPage'),' |';
  337. }
  338. if($nbrQuestions > $limitQuestPage) {
  339. echo '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&fromExercise=',$fromExercise,'&page=',($page+1),'">',get_lang('NextPage'),' &gt;&gt;</a>';
  340. } elseif($page) {
  341. echo ' ',get_lang('NextPage'),' &gt;&gt;';
  342. }
  343. echo '</td>
  344. </tr>
  345. </table>
  346. </td>
  347. </tr>
  348. <tr bgcolor="#e6e6e6">';
  349. if(!empty($fromExercise)) {
  350. echo '<th width="4%"> </th>',
  351. '<th>',get_lang('Question'),'</th>',
  352. '<th>',get_lang('Level'),'</th>',
  353. '<th>',get_lang('Reuse'),'</th>';
  354. } else {
  355. echo '<td width="60%" align="center">',get_lang('Question'),'</td>',
  356. '<td width="20%" align="center">',get_lang('Modify'),'</td>',
  357. '<td width="16%" align="center">',get_lang('Delete'),'</td>';
  358. }
  359. echo '</tr>';
  360. $i=1;
  361. echo '<pre>';
  362. echo '</pre>';
  363. while ($row = Database::fetch_array($result)) {
  364. // if we come from the exercise administration to get a question,
  365. // don't show the questions already used by that exercise
  366. /*if (!$fromExercise) {echo '1'; }
  367. if (!isset($objExercise)){echo '2';}
  368. if (!($objExercise instanceOf Exercise)){echo '3';}
  369. if (!$objExercise->isInList($row['id'])) {echo '4';}
  370. */
  371. // original recipe -
  372. //if (!$fromExercise || !isset($objExercise) || !($objExercise instanceOf Exercise) || (!$objExercise->isInList($row['id'])))
  373. if (!$fromExercise || !isset($objExercise) || !($objExercise instanceOf Exercise) || (is_array($objExercise->questionList)) ) {
  374. echo '<tr ',($i%2==0?'class="row_odd"':'class="row_even"'),'>';
  375. echo '<td align="center"> <input type="checkbox" value="'.$row['id'].'" name="recup[]"/></td>';
  376. echo ' <td><a href="admin.php?',api_get_cidreq(),'&editQuestion=',$row['id'],'&fromExercise=',$fromExercise,'">',$row['question'],'</a></td>';
  377. echo ' <td align="center" >';
  378. if (empty($fromExercise)) {
  379. echo '<a href="admin.php?'.api_get_cidreq().'&amp;editQuestion=',$row['id'],'"><img src="../img/edit.gif" border="0" alt="',get_lang('Modify'),'"></a>',
  380. '</td>',
  381. '<td align="center">',
  382. '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&delete=',$row['id'],'" onclick="javascript:if(!confirm(\'',addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)),'\')) return false;"><img src="../img/delete.gif" border="0" alt="',get_lang('Delete'),'"></a>';
  383. //'<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&delete=',$row['id'],'" onclick="javascript:if(!confirm(\'',addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)),'\')) return false;"><img src="../img/delete.gif" border="0" alt="',get_lang('Delete'),'"></a>';
  384. } else {
  385. //echo $row['level'],'</td>',
  386. // '<td><a href="',api_get_self(),'?',api_get_cidreq(),'&recup=',$row['id'],'&fromExercise=',$fromExercise,'"><img src="../img/view_more_stats.gif" border="0" alt="',get_lang('Reuse'),'"></a>';
  387. echo $row['level'],'</td>',
  388. '<td align="center" ><a href="',api_get_self(),'?',api_get_cidreq(),'&recup=',$row['id'],'&fromExercise=',$fromExercise,'">' .
  389. '<img src="../img/view_more_stats.gif" border="0" alt="',get_lang('Reuse'),'"></a>';
  390. }
  391. echo '</td>';
  392. echo '</tr>';
  393. // skips the last question, that is only used to know if we have or not to create a link "Next page"
  394. if($i == $limitQuestPage) {
  395. break;
  396. }
  397. $i++;
  398. }
  399. }
  400. if (!$nbrQuestions) {
  401. echo '<tr>',
  402. '<td colspan="',($fromExercise?4:4),'">',get_lang('NoQuestion'),'</td>',
  403. '</tr>';
  404. }
  405. echo '</table>';
  406. echo '<div style="width:100%; border-top:1px dotted #4171B5;">
  407. <button class="save" type="submit">'.get_lang('Reuse').'</button>
  408. </div></form>';
  409. Display::display_footer();
  410. } else {
  411. // if not admin of course
  412. api_not_allowed(true);
  413. }
  414. ?>