question_pool.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. include('../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(!empty($_GET['page'])){
  56. $page = intval($_GET['page']);
  57. }
  58. //only that type of question
  59. if(!empty($_GET['type'])){
  60. $type = intval($_GET['type']);
  61. }
  62. // maximum number of questions on a same page
  63. $limitQuestPage=50;
  64. // document path
  65. $documentPath=api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  66. // picture path
  67. $picturePath=$documentPath.'/images';
  68. if(!($objExcercise instanceOf Exercise) && !empty($fromExercise))
  69. {
  70. $objExercise = new Exercise();
  71. $objExercise->read($fromExercise);
  72. }
  73. if(!($objExcercise instanceOf Exercise) && !empty($exerciseId))
  74. {
  75. $objExercise = new Exercise();
  76. $objExercise->read($exerciseId);
  77. }
  78. if($is_allowedToEdit)
  79. {
  80. // deletes a question from the data base and all exercises
  81. if($delete)
  82. {
  83. // construction of the Question object
  84. // if the question exists
  85. if($objQuestionTmp = Question::read($delete))
  86. {
  87. // deletes the question from all exercises
  88. $objQuestionTmp->delete();
  89. }
  90. // destruction of the Question object
  91. unset($objQuestionTmp);
  92. }
  93. // gets an existing question and copies it into a new exercise
  94. elseif($recup && $fromExercise)
  95. {
  96. // if the question exists
  97. if($objQuestionTmp = Question :: read($recup))
  98. {
  99. // adds the exercise ID represented by $fromExercise into the list of exercises for the current question
  100. $objQuestionTmp->addToList($fromExercise);
  101. }
  102. // destruction of the Question object
  103. unset($objQuestionTmp);
  104. if(!$objExcercise instanceOf Exercise)
  105. {
  106. $objExercise = new Exercise();
  107. $objExercise->read($fromExercise);
  108. }
  109. // adds the question ID represented by $recup into the list of questions for the current exercise
  110. $objExercise->addToList($recup);
  111. api_session_register('objExercise');
  112. header("Location: admin.php?exerciseId=$fromExercise");
  113. exit();
  114. } else if( isset($_POST['recup']) && is_array($_POST['recup']) && $fromExercise) {
  115. $list_recup = $_POST['recup'];
  116. foreach ($list_recup as $recup) {
  117. $recup = intval($recup);
  118. // if the question exists
  119. if($objQuestionTmp = Question :: read($recup)) {
  120. // adds the exercise ID represented by $fromExercise into the list of exercises for the current question
  121. $objQuestionTmp->addToList($fromExercise);
  122. }
  123. // destruction of the Question object
  124. unset($objQuestionTmp);
  125. if(!$objExcercise instanceOf Exercise) {
  126. $objExercise = new Exercise();
  127. $objExercise->read($fromExercise);
  128. }
  129. // adds the question ID represented by $recup into the list of questions for the current exercise
  130. $objExercise->addToList($recup);
  131. }
  132. api_session_register('objExercise');
  133. header("Location: admin.php?exerciseId=$fromExercise");
  134. exit();
  135. }
  136. }
  137. if (isset($_SESSION['gradebook'])){
  138. $gradebook= $_SESSION['gradebook'];
  139. }
  140. if (!empty($gradebook) && $gradebook=='view') {
  141. $interbreadcrumb[]= array (
  142. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  143. 'name' => get_lang('Gradebook')
  144. );
  145. }
  146. $nameTools=get_lang('QuestionPool');
  147. $interbreadcrumb[]=array("url" => "exercice.php","name" => get_lang('Exercices'));
  148. // if admin of course
  149. if($is_allowedToEdit)
  150. {
  151. Display::display_header($nameTools,"Exercise");
  152. ?>
  153. <h3><?php echo $nameTools; ?></h3>
  154. <div class="actions">
  155. <?php
  156. if(!empty($fromExercise)) {
  157. echo '<a href="admin.php?',api_get_cidreq(),'&exerciseId=',$fromExercise,'">'.Display::return_icon('quiz.gif', get_lang('GoBackToEx')),get_lang('GoBackToEx'),'</a>';
  158. } else {
  159. echo '<a href="admin.php?',api_get_cidreq(),'&newQuestion=yes">'.Display::return_icon('new_test.gif'),get_lang('NewQu'),'</a>';
  160. }
  161. if (isset($type)) {
  162. $url = api_get_self().'?type=1';
  163. } else {
  164. $url = api_get_self();
  165. }
  166. ?>
  167. <form method="get" action="<?php echo $url; ?>" style="display:inline;">
  168. <?php
  169. if (isset($type)) {
  170. echo '<input type="hidden" name="type" value="1">';
  171. }
  172. ?>
  173. <input type="hidden" name="fromExercise" value="<?php echo $fromExercise; ?>">
  174. <?php echo get_lang('Filter'); ?> :
  175. <select name="exerciseId">
  176. <option value="0">-- <?php echo get_lang('AllExercises'); ?> --</option>
  177. <option value="-1" <?php if($exerciseId == -1) echo 'selected="selected"'; ?>>-- <?php echo get_lang('OrphanQuestions'); ?> --</option>
  178. <?php
  179. $sql="SELECT id,title FROM $TBL_EXERCICES WHERE id<>'".Database::escape_string($fromExercise)."' AND active<>'-1' ORDER BY id";
  180. $result=Database::query($sql,__FILE__,__LINE__);
  181. // shows a list-box allowing to filter questions
  182. while($row=Database::fetch_array($result)) {
  183. ?>
  184. <option value="<?php echo $row['id']; ?>" <?php if($exerciseId == $row['id']) echo 'selected="selected"'; ?>><?php echo $row['title']; ?></option>
  185. <?php
  186. }
  187. ?>
  188. </select>
  189. &nbsp;
  190. <?php
  191. echo get_lang('Difficulty');
  192. echo ' : <select name="exerciseLevel">';
  193. //echo '<option value="-1">-- '.get_lang('AllExercises').' --</option>';
  194. //level difficulty only from 1 to 5
  195. if (!isset($exerciseLevel)) $exerciseLevel = -1;
  196. for ($level = -1; $level <=5; $level++) {
  197. $selected ='';
  198. if ($level!=0) {
  199. if ($exerciseLevel == $level)
  200. $selected = ' selected="selected" ';
  201. if ($level==-1) {
  202. echo '<option value="'.$level.'" '.$selected.'>-- '.get_lang('AllExercises').' --</option>';
  203. } else {
  204. echo '<option value="'.$level.'" '.$selected.'>'.$level.'</option>';
  205. }
  206. }
  207. }
  208. echo '</select> ';
  209. ?>
  210. <button class="save" type="submit" name="name" value="<?php echo get_lang('Ok') ?>"><?php echo get_lang('Ok') ?></button>
  211. </form>
  212. </div>
  213. <form method="post" action="<?php echo $url.'?'.api_get_cidreq().'&fromExercise='.$fromExercise; ?>" >
  214. <table class="data_table">
  215. <?php
  216. $from=$page*$limitQuestPage;
  217. // if we have selected an exercise in the list-box 'Filter'
  218. if ($exerciseId > 0) {
  219. //$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);
  220. $where = '';
  221. if (isset($type) && $type==1) {
  222. $where = ' type = 1 AND ';
  223. }
  224. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  225. $where .= ' level='.$exerciseLevel.' AND ';
  226. }
  227. $sql="SELECT id,question,type,level
  228. FROM $TBL_EXERCICE_QUESTION,$TBL_QUESTIONS
  229. WHERE $where question_id=id AND exercice_id='".Database::escape_string($exerciseId)."'
  230. ORDER BY question_order";
  231. } elseif($exerciseId == -1) {
  232. // if we have selected the option 'Orphan questions' in the list-box 'Filter'
  233. // 1. Old logic: When a test is deleted, the correspondent records in 'quiz' and 'quiz_rel_question' tables are deleted.
  234. //$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);
  235. // 2. New logic: When a test is deleted, the field 'active' takes value -1 (it is in the correspondent record in 'quiz' table).
  236. //$sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id FROM '.$TBL_QUESTIONS.
  237. // ' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.
  238. // ' as exercices ON exercice_id=exercices.id WHERE exercices.active = -1 LIMIT $from, '.($limitQuestPage + 1);
  239. // 3. This is more safe to changes, it is a mix between old and new logic.
  240. /*$sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id FROM '.$TBL_QUESTIONS.
  241. ' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.
  242. ' as exercices ON exercice_id=exercices.id WHERE quizz_questions.exercice_id IS NULL OR exercices.active = -1 LIMIT '.$from.', '.($limitQuestPage + 1);
  243. */
  244. /* 4. Query changed because of the Level feature implemented
  245. $sql='SELECT id, question, type, exercice_id,level FROM '.$TBL_QUESTIONS.' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions
  246. ON questions.id=quizz_questions.question_id AND exercice_id IS NULL '.
  247. (!is_null($exerciseLevel) && $exerciseLevel >= 0 ? 'WHERE level=\''.$exerciseLevel.'\' ' : '');
  248. */
  249. // 5. this is the combination of the 3 and 4 query because of the level feature implementation
  250. // we filter the type of question, because in the DirectFeedback we can only add questions with type=1 = UNIQUE_ANSWER
  251. $type_where= '';
  252. if (isset($type) && $type==1) {
  253. $type_where = ' AND questions.type = 1 ';
  254. }
  255. $level_where = '';
  256. if (isset($exerciseLevel) && $exerciseLevel!= -1 ) {
  257. $level_where = ' level='.$exerciseLevel.' AND ';
  258. }
  259. $sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id , level
  260. FROM '.$TBL_QUESTIONS.' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions
  261. ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.' as exercices
  262. ON exercice_id=exercices.id
  263. WHERE '.$level_where.' (quizz_questions.exercice_id IS NULL OR exercices.active = -1 ) '.$type_where.'
  264. LIMIT '.$from.', '.($limitQuestPage + 1);
  265. } else {
  266. // if we have not selected any option in the list-box 'Filter'
  267. //$sql="SELECT id,question,type FROM $TBL_QUESTIONS LIMIT $from, ".($limitQuestPage + 1);
  268. $where = '';
  269. if (isset($type)&& $type==1){
  270. $where = ' WHERE type = 1 ';
  271. }
  272. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  273. if (strlen($where)>0)
  274. $where .= ' AND level='.$exerciseLevel.' ';
  275. else
  276. $where = ' WHERE level='.$exerciseLevel.' ';
  277. }
  278. $sql="SELECT id,question,type,level FROM $TBL_QUESTIONS $where ";
  279. // forces the value to 0
  280. //echo $sql;
  281. $exerciseId=0;
  282. }
  283. $result=Database::query($sql,__FILE__,__LINE__);
  284. $nbrQuestions=Database::num_rows($result);
  285. echo '<tr>',
  286. '<td colspan="',($fromExercise?4:4),'">',
  287. '<table border="0" cellpadding="0" cellspacing="0" width="100%">',
  288. '<tr>',
  289. '<td>';
  290. echo '</td>',
  291. '<td align="right">';
  292. if(!empty($page)) {
  293. echo '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&fromExercise=',$fromExercise,'&page=',($page-1),'">&lt;&lt; ',get_lang('PreviousPage'),'</a> |';
  294. } elseif($nbrQuestions > $limitQuestPage) {
  295. echo '&lt;&lt; ',get_lang('PreviousPage'),' |';
  296. }
  297. if($nbrQuestions > $limitQuestPage) {
  298. echo '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&fromExercise=',$fromExercise,'&page=',($page+1),'">',get_lang('NextPage'),' &gt;&gt;</a>';
  299. } elseif($page) {
  300. echo ' ',get_lang('NextPage'),' &gt;&gt;';
  301. }
  302. echo '</td>
  303. </tr>
  304. </table>
  305. </td>
  306. </tr>
  307. <tr bgcolor="#e6e6e6">';
  308. if(!empty($fromExercise)) {
  309. echo '<th width="4%"> </th>',
  310. '<th>',get_lang('Question'),'</th>',
  311. '<th>',get_lang('Level'),'</th>',
  312. '<th>',get_lang('Reuse'),'</th>';
  313. } else {
  314. echo '<td width="60%" align="center">',get_lang('Question'),'</td>',
  315. '<td width="20%" align="center">',get_lang('Modify'),'</td>',
  316. '<td width="16%" align="center">',get_lang('Delete'),'</td>';
  317. }
  318. echo '</tr>';
  319. $i=1;
  320. echo '<pre>';
  321. echo '</pre>';
  322. while ($row = Database::fetch_array($result)) {
  323. // if we come from the exercise administration to get a question,
  324. // don't show the questions already used by that exercise
  325. /*if (!$fromExercise) {echo '1'; }
  326. if (!isset($objExercise)){echo '2';}
  327. if (!($objExercise instanceOf Exercise)){echo '3';}
  328. if (!$objExercise->isInList($row['id'])) {echo '4';}
  329. */
  330. // original recipe -
  331. //if (!$fromExercise || !isset($objExercise) || !($objExercise instanceOf Exercise) || (!$objExercise->isInList($row['id'])))
  332. if (!$fromExercise || !isset($objExercise) || !($objExercise instanceOf Exercise) || (is_array($objExercise->questionList)) ) {
  333. echo '<tr ',($i%2==0?'class="row_odd"':'class="row_even"'),'>';
  334. echo '<td align="center"> <input type="checkbox" value="'.$row['id'].'" name="recup[]"/></td>';
  335. echo ' <td><a href="admin.php?',api_get_cidreq(),'&editQuestion=',$row['id'],'&fromExercise=',$fromExercise,'">',$row['question'],'</a></td>';
  336. echo ' <td align="center" >';
  337. if (empty($fromExercise)) {
  338. echo '<a href="admin.php?'.api_get_cidreq().'&amp;editQuestion=',$row['id'],'"><img src="../img/edit.gif" border="0" alt="',get_lang('Modify'),'"></a>',
  339. '</td>',
  340. '<td align="center">',
  341. '<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>';
  342. //'<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>';
  343. } else {
  344. //echo $row['level'],'</td>',
  345. // '<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>';
  346. echo $row['level'],'</td>',
  347. '<td align="center" ><a href="',api_get_self(),'?',api_get_cidreq(),'&recup=',$row['id'],'&fromExercise=',$fromExercise,'">' .
  348. '<img src="../img/view_more_stats.gif" border="0" alt="',get_lang('Reuse'),'"></a>';
  349. }
  350. echo '</td>';
  351. echo '</tr>';
  352. // skips the last question, that is only used to know if we have or not to create a link "Next page"
  353. if($i == $limitQuestPage) {
  354. break;
  355. }
  356. $i++;
  357. }
  358. }
  359. if (!$nbrQuestions) {
  360. echo '<tr>',
  361. '<td colspan="',($fromExercise?4:4),'">',get_lang('NoQuestion'),'</td>',
  362. '</tr>';
  363. }
  364. echo '</table>';
  365. echo '<div style="width:100%; border-top:1px dotted #4171B5;">
  366. <button class="save" type="submit">'.get_lang('Reuse').'</button>
  367. </div></form>';
  368. Display::display_footer();
  369. } else {
  370. // if not admin of course
  371. api_not_allowed(true);
  372. }
  373. ?>