question_pool.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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();
  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. }
  115. }
  116. if (isset($_SESSION['gradebook'])){
  117. $gradebook= $_SESSION['gradebook'];
  118. }
  119. if (!empty($gradebook) && $gradebook=='view') {
  120. $interbreadcrumb[]= array (
  121. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  122. 'name' => get_lang('Gradebook')
  123. );
  124. }
  125. $nameTools=get_lang('QuestionPool');
  126. $interbreadcrumb[]=array("url" => "exercice.php","name" => get_lang('Exercices'));
  127. // if admin of course
  128. if($is_allowedToEdit)
  129. {
  130. Display::display_header($nameTools,"Exercise");
  131. ?>
  132. <h3><?php echo $nameTools; ?></h3>
  133. <div class="actions">
  134. <?php
  135. if(!empty($fromExercise)) {
  136. echo '<a href="admin.php?',api_get_cidreq(),'&exerciseId=',$fromExercise,'">'.Display::return_icon('quiz.gif', get_lang('GoBackToEx')),get_lang('GoBackToEx'),'</a>';
  137. } else {
  138. echo '<a href="admin.php?',api_get_cidreq(),'&newQuestion=yes">'.Display::return_icon('new_test.gif'),get_lang('NewQu'),'</a>';
  139. }
  140. if (isset($type)) {
  141. $url = api_get_self().'?type=1';
  142. } else {
  143. $url = api_get_self();
  144. }
  145. ?>
  146. <form method="get" action="<?php echo $url; ?>" style="display:inline;">
  147. <?php
  148. if (isset($type)) {
  149. echo '<input type="hidden" name="type" value="1">';
  150. }
  151. ?>
  152. <input type="hidden" name="fromExercise" value="<?php echo $fromExercise; ?>">
  153. <?php echo get_lang('Filter'); ?> :
  154. <select name="exerciseId">
  155. <option value="0">-- <?php echo get_lang('AllExercises'); ?> --</option>
  156. <option value="-1" <?php if($exerciseId == -1) echo 'selected="selected"'; ?>>-- <?php echo get_lang('OrphanQuestions'); ?> --</option>
  157. <?php
  158. $sql="SELECT id,title FROM $TBL_EXERCICES WHERE id<>'".Database::escape_string($fromExercise)."' AND active<>'-1' ORDER BY id";
  159. $result=api_sql_query($sql,__FILE__,__LINE__);
  160. // shows a list-box allowing to filter questions
  161. while($row=Database::fetch_array($result)) {
  162. ?>
  163. <option value="<?php echo $row['id']; ?>" <?php if($exerciseId == $row['id']) echo 'selected="selected"'; ?>><?php echo $row['title']; ?></option>
  164. <?php
  165. }
  166. ?>
  167. </select>
  168. &nbsp;
  169. <?php
  170. echo get_lang('Difficulty');
  171. echo ' : <select name="exerciseLevel">';
  172. //echo '<option value="-1">-- '.get_lang('AllExercises').' --</option>';
  173. //level difficulty only from 1 to 5
  174. if (!isset($exerciseLevel)) $exerciseLevel = -1;
  175. for ($level = -1; $level <=5; $level++) {
  176. $selected ='';
  177. if ($level!=0) {
  178. if ($exerciseLevel == $level)
  179. $selected = ' selected="selected" ';
  180. if ($level==-1) {
  181. echo '<option value="'.$level.'" '.$selected.'>-- '.get_lang('AllExercises').' --</option>';
  182. } else {
  183. echo '<option value="'.$level.'" '.$selected.'>'.$level.'</option>';
  184. }
  185. }
  186. }
  187. echo '</select> ';
  188. ?>
  189. <button class="save" type="submit" name="name" value="<?php echo get_lang('Ok') ?>"><?php echo get_lang('Ok') ?></button>
  190. </form>
  191. </div>
  192. <table class="data_table">
  193. <?php
  194. $from=$page*$limitQuestPage;
  195. // if we have selected an exercise in the list-box 'Filter'
  196. if ($exerciseId > 0) {
  197. //$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);
  198. $where = '';
  199. if (isset($type) && $type==1) {
  200. $where = ' type = 1 AND ';
  201. }
  202. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  203. $where .= ' level='.$exerciseLevel.' AND ';
  204. }
  205. $sql="SELECT id,question,type,level
  206. FROM $TBL_EXERCICE_QUESTION,$TBL_QUESTIONS
  207. WHERE $where question_id=id AND exercice_id='".Database::escape_string($exerciseId)."'
  208. ORDER BY question_order";
  209. } elseif($exerciseId == -1) {
  210. // if we have selected the option 'Orphan questions' in the list-box 'Filter'
  211. // 1. Old logic: When a test is deleted, the correspondent records in 'quiz' and 'quiz_rel_question' tables are deleted.
  212. //$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);
  213. // 2. New logic: When a test is deleted, the field 'active' takes value -1 (it is in the correspondent record in 'quiz' table).
  214. //$sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id FROM '.$TBL_QUESTIONS.
  215. // ' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.
  216. // ' as exercices ON exercice_id=exercices.id WHERE exercices.active = -1 LIMIT $from, '.($limitQuestPage + 1);
  217. // 3. This is more safe to changes, it is a mix between old and new logic.
  218. /*$sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id FROM '.$TBL_QUESTIONS.
  219. ' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.
  220. ' as exercices ON exercice_id=exercices.id WHERE quizz_questions.exercice_id IS NULL OR exercices.active = -1 LIMIT '.$from.', '.($limitQuestPage + 1);
  221. */
  222. /* 4. Query changed because of the Level feature implemented
  223. $sql='SELECT id, question, type, exercice_id,level FROM '.$TBL_QUESTIONS.' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions
  224. ON questions.id=quizz_questions.question_id AND exercice_id IS NULL '.
  225. (!is_null($exerciseLevel) && $exerciseLevel >= 0 ? 'WHERE level=\''.$exerciseLevel.'\' ' : '');
  226. */
  227. // 5. this is the combination of the 3 and 4 query because of the level feature implementation
  228. // we filter the type of question, because in the DirectFeedback we can only add questions with type=1 = UNIQUE_ANSWER
  229. $type_where= '';
  230. if (isset($type) && $type==1) {
  231. $type_where = ' AND questions.type = 1 ';
  232. }
  233. $level_where = '';
  234. if (isset($exerciseLevel) && $exerciseLevel!= -1 ) {
  235. $level_where = ' level='.$exerciseLevel.' AND ';
  236. }
  237. $sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id , level
  238. FROM '.$TBL_QUESTIONS.' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions
  239. ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.' as exercices
  240. ON exercice_id=exercices.id
  241. WHERE '.$level_where.' (quizz_questions.exercice_id IS NULL OR exercices.active = -1 ) '.$type_where.'
  242. LIMIT '.$from.', '.($limitQuestPage + 1);
  243. } else {
  244. // if we have not selected any option in the list-box 'Filter'
  245. //$sql="SELECT id,question,type FROM $TBL_QUESTIONS LIMIT $from, ".($limitQuestPage + 1);
  246. $where = '';
  247. if (isset($type)&& $type==1){
  248. $where = ' WHERE type = 1 ';
  249. }
  250. if (isset($exerciseLevel) && $exerciseLevel != -1) {
  251. if (strlen($where)>0)
  252. $where .= ' AND level='.$exerciseLevel.' ';
  253. else
  254. $where = ' WHERE level='.$exerciseLevel.' ';
  255. }
  256. $sql="SELECT id,question,type,level FROM $TBL_QUESTIONS $where ";
  257. // forces the value to 0
  258. //echo $sql;
  259. $exerciseId=0;
  260. }
  261. $result=api_sql_query($sql,__FILE__,__LINE__);
  262. $nbrQuestions=Database::num_rows($result);
  263. echo '<tr>',
  264. '<td colspan="',($fromExercise?3:3),'">',
  265. '<table border="0" cellpadding="0" cellspacing="0" width="100%">',
  266. '<tr>',
  267. '<td>';
  268. echo '</td>',
  269. '<td align="right">';
  270. if(!empty($page)) {
  271. echo '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&fromExercise=',$fromExercise,'&page=',($page-1),'">&lt;&lt; ',get_lang('PreviousPage'),'</a> |';
  272. } elseif($nbrQuestions > $limitQuestPage) {
  273. echo '&lt;&lt; ',get_lang('PreviousPage'),' |';
  274. }
  275. if($nbrQuestions > $limitQuestPage) {
  276. echo '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&fromExercise=',$fromExercise,'&page=',($page+1),'">',get_lang('NextPage'),' &gt;&gt;</a>';
  277. } elseif($page) {
  278. echo ' ',get_lang('NextPage'),' &gt;&gt;';
  279. }
  280. echo '</td>
  281. </tr>
  282. </table>
  283. </td>
  284. </tr>
  285. <tr bgcolor="#e6e6e6">';
  286. if(!empty($fromExercise)) {
  287. echo '<th>',get_lang('Question'),'</th>',
  288. '<th>',get_lang('Level'),'</th>',
  289. '<th>',get_lang('Reuse'),'</th>';
  290. } else {
  291. echo '<td width="60%" align="center">',get_lang('Question'),'</td>',
  292. '<td width="20%" align="center">',get_lang('Modify'),'</td>',
  293. '<td width="20%" align="center">',get_lang('Delete'),'</td>';
  294. }
  295. echo '</tr>';
  296. $i=1;
  297. echo '<pre>';
  298. echo '</pre>';
  299. while ($row = Database::fetch_array($result)) {
  300. // if we come from the exercise administration to get a question,
  301. // don't show the questions already used by that exercise
  302. /*if (!$fromExercise) {echo '1'; }
  303. if (!isset($objExercise)){echo '2';}
  304. if (!($objExercise instanceOf Exercise)){echo '3';}
  305. if (!$objExercise->isInList($row['id'])) {echo '4';}
  306. */
  307. // original recipe -
  308. //if (!$fromExercise || !isset($objExercise) || !($objExercise instanceOf Exercise) || (!$objExercise->isInList($row['id'])))
  309. if (!$fromExercise || !isset($objExercise) || !($objExercise instanceOf Exercise) || (is_array($objExercise->questionList)) ) {
  310. echo '<tr ',($i%2==0?'class="row_odd"':'class="row_even"'),'>';
  311. echo ' <td><a href="admin.php?',api_get_cidreq(),'&editQuestion=',$row['id'],'&fromExercise=',$fromExercise,'">',$row['question'],'</a></td>';
  312. echo ' <td align="center" >';
  313. if (empty($fromExercise)) {
  314. echo '<a href="admin.php?'.api_get_cidreq().'&amp;editQuestion=',$row['id'],'"><img src="../img/edit.gif" border="0" alt="',get_lang('Modify'),'"></a>',
  315. '</td>',
  316. '<td align="center">',
  317. '<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>';
  318. //'<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>';
  319. } else {
  320. //echo $row['level'],'</td>',
  321. // '<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>';
  322. echo $row['level'],'</td>',
  323. '<td align="center" ><a href="',api_get_self(),'?',api_get_cidreq(),'&recup=',$row['id'],'&fromExercise=',$fromExercise,'">' .
  324. '<img src="../img/view_more_stats.gif" border="0" alt="',get_lang('Reuse'),'"></a>';
  325. }
  326. echo '</td>';
  327. echo '</tr>';
  328. // skips the last question, that is only used to know if we have or not to create a link "Next page"
  329. if($i == $limitQuestPage) {
  330. break;
  331. }
  332. $i++;
  333. }
  334. }
  335. if (!$nbrQuestions) {
  336. echo '<tr>',
  337. '<td colspan="',($fromExercise?3:3),'">',get_lang('NoQuestion'),'</td>',
  338. '</tr>';
  339. }
  340. echo '</table>';
  341. Display::display_footer();
  342. } else {
  343. // if not admin of course
  344. api_not_allowed(true);
  345. }
  346. ?>