question_pool.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php // $Id: question_pool.php 18159 2009-02-02 17:30:00Z herodoto $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 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 18159 2009-02-02 17:30:00Z herodoto $
  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. // maximum number of questions on a same page
  59. $limitQuestPage=50;
  60. // document path
  61. $documentPath=api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  62. // picture path
  63. $picturePath=$documentPath.'/images';
  64. if(!($objExcercise instanceOf Exercise) && !empty($fromExercise))
  65. {
  66. $objExercise = new Exercise();
  67. $objExercise->read($fromExercise);
  68. }
  69. if(!($objExcercise instanceOf Exercise) && !empty($exerciseId))
  70. {
  71. $objExercise = new Exercise();
  72. $objExercise->read($exerciseId);
  73. }
  74. if($is_allowedToEdit)
  75. {
  76. // deletes a question from the data base and all exercises
  77. if($delete)
  78. {
  79. // construction of the Question object
  80. // if the question exists
  81. if($objQuestionTmp = Question::read($delete))
  82. {
  83. // deletes the question from all exercises
  84. $objQuestionTmp->delete();
  85. }
  86. // destruction of the Question object
  87. unset($objQuestionTmp);
  88. }
  89. // gets an existing question and copies it into a new exercise
  90. elseif($recup && $fromExercise)
  91. {
  92. // if the question exists
  93. if($objQuestionTmp = Question :: read($recup))
  94. {
  95. // adds the exercise ID represented by $fromExercise into the list of exercises for the current question
  96. $objQuestionTmp->addToList($fromExercise);
  97. }
  98. // destruction of the Question object
  99. unset($objQuestionTmp);
  100. if(!$objExcercise instanceOf Exercise)
  101. {
  102. $objExercise = new Exercise();
  103. $objExercise->read($fromExercise);
  104. }
  105. // adds the question ID represented by $recup into the list of questions for the current exercise
  106. $objExercise->addToList($recup);
  107. api_session_register('objExercise');
  108. header("Location: admin.php?exerciseId=$fromExercise");
  109. exit();
  110. }
  111. }
  112. $nameTools=get_lang('QuestionPool');
  113. $interbreadcrumb[]=array("url" => "exercice.php","name" => get_lang('Exercices'));
  114. // if admin of course
  115. if($is_allowedToEdit)
  116. {
  117. Display::display_header($nameTools,"Exercise");
  118. ?>
  119. <h3><?php echo $nameTools; ?></h3>
  120. <div class="actions">
  121. <?php
  122. if(!empty($fromExercise))
  123. {
  124. echo '<a href="admin.php?',api_get_cidreq(),'&exerciseId=',$fromExercise,'">'.Display::return_icon('quiz.gif', get_lang('GoBackToEx')),get_lang('GoBackToEx'),'</a>';
  125. }
  126. else
  127. {
  128. echo '<a href="admin.php?',api_get_cidreq(),'&newQuestion=yes">'.Display::return_icon('new_test.gif'),get_lang('NewQu'),'</a>';
  129. }
  130. ?>
  131. <form method="get" action="<?php echo api_get_self(); ?>" style="display:inline;">
  132. <input type="hidden" name="fromExercise" value="<?php echo $fromExercise; ?>">
  133. <?php echo get_lang('Filter'); ?> :
  134. <select name="exerciseId">
  135. <option value="0">-- <?php echo get_lang('AllExercises'); ?> --</option>
  136. <option value="-1" <?php if($exerciseId == -1) echo 'selected="selected"'; ?>>-- <?php echo get_lang('OrphanQuestions'); ?> --</option>
  137. <?php
  138. $sql="SELECT id,title FROM $TBL_EXERCICES WHERE id<>'".Database::escape_string($fromExercise)."' AND active<>'-1' ORDER BY id";
  139. $result=api_sql_query($sql,__FILE__,__LINE__);
  140. // shows a list-box allowing to filter questions
  141. while($row=Database::fetch_array($result))
  142. {
  143. ?>
  144. <option value="<?php echo $row['id']; ?>" <?php if($exerciseId == $row['id']) echo 'selected="selected"'; ?>><?php echo $row['title']; ?></option>
  145. <?php
  146. }
  147. ?>
  148. </select>
  149. <?php echo get_lang('Difficulty'); ?> : <select name="exerciseLevel">
  150. <option value="-1">-- <?php echo get_lang('AllExercises'); ?> --</option>
  151. <?php
  152. $sql = "SELECT MAX(level) FROM $TBL_QUESTIONS";
  153. $result = api_sql_query($sql,__FILE__,__LINE__);
  154. if ($row = Database::fetch_array($result)) {
  155. $maxLevel = $row[0];
  156. } else {
  157. $maxLevel = -1;
  158. }
  159. for ($level = 0; $level <= $maxLevel; $level++) {
  160. ?>
  161. <option value="<?php echo $level; ?>" <?php if($exerciseLevel == $level) echo 'selected="selected"'; ?>><?php echo $level; ?></option>
  162. <?php
  163. }
  164. ?>
  165. </select>
  166. <input type="submit" value="<?php echo get_lang('Ok'); ?>">
  167. </form>
  168. </div>
  169. <table class="data_table">
  170. <?php
  171. $from=$page*$limitQuestPage;
  172. // if we have selected an exercise in the list-box 'Filter'
  173. if($exerciseId > 0)
  174. {
  175. //$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);
  176. $sql="SELECT id,question,type,level FROM $TBL_EXERCICE_QUESTION,$TBL_QUESTIONS WHERE question_id=id AND exercice_id='".Database::escape_string($exerciseId)."' ".
  177. (!is_null($exerciseLevel) && $exerciseLevel >= 0 ? 'AND level=\''.$exerciseLevel.'\' ' : '')."ORDER BY question_order";
  178. }
  179. // if we have selected the option 'Orphan questions' in the list-box 'Filter'
  180. elseif($exerciseId == -1)
  181. {
  182. // Old logic: When a test is deleted, the correspondent records in 'quiz' and 'quiz_rel_question' tables are deleted.
  183. //$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);
  184. // New logic: When a test is deleted, the field 'active' takes value -1 (it is in the correspondent record in 'quiz' table).
  185. //$sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id FROM '.$TBL_QUESTIONS.
  186. // ' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.
  187. // ' as exercices ON exercice_id=exercices.id WHERE exercices.active = -1 LIMIT $from, '.($limitQuestPage + 1);
  188. // This is more safe to changes, it is a mix between old and new logic.
  189. /*$sql='SELECT questions.id, questions.question, questions.type, quizz_questions.exercice_id FROM '.$TBL_QUESTIONS.
  190. ' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id LEFT JOIN '.$TBL_EXERCICES.
  191. ' as exercices ON exercice_id=exercices.id WHERE quizz_questions.exercice_id IS NULL OR exercices.active = -1 LIMIT '.$from.', '.($limitQuestPage + 1);
  192. */
  193. $sql='SELECT id, question, type, exercice_id,level FROM '.$TBL_QUESTIONS.' as questions LEFT JOIN '.$TBL_EXERCICE_QUESTION.' as quizz_questions ON questions.id=quizz_questions.question_id AND exercice_id IS NULL '.
  194. (!is_null($exerciseLevel) && $exerciseLevel >= 0 ? 'WHERE level=\''.$exerciseLevel.'\' ' : '');
  195. }
  196. // if we have not selected any option in the list-box 'Filter'
  197. else
  198. {
  199. //$sql="SELECT id,question,type FROM $TBL_QUESTIONS LIMIT $from, ".($limitQuestPage + 1);
  200. $sql="SELECT id,question,type,level FROM $TBL_QUESTIONS ".
  201. (!is_null($exerciseLevel) && $exerciseLevel >= 0 ? 'WHERE level=\''.$exerciseLevel.'\' ' : '');
  202. // forces the value to 0
  203. $exerciseId=0;
  204. }
  205. $result=api_sql_query($sql,__FILE__,__LINE__);
  206. $nbrQuestions=Database::num_rows($result);
  207. echo '<tr>',
  208. '<td colspan="',($fromExercise?3:3),'">',
  209. '<table border="0" cellpadding="0" cellspacing="0" width="100%">',
  210. '<tr>',
  211. '<td>';
  212. echo '</td>',
  213. '<td align="right">';
  214. if(!empty($page))
  215. {
  216. echo '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&fromExercise=',$fromExercise,'&page=',($page-1),'">&lt;&lt; ',get_lang('PreviousPage'),'</a> |';
  217. }
  218. elseif($nbrQuestions > $limitQuestPage)
  219. {
  220. echo '&lt;&lt; ',get_lang('PreviousPage'),' |';
  221. }
  222. if($nbrQuestions > $limitQuestPage)
  223. {
  224. echo '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&fromExercise=',$fromExercise,'&page=',($page+1),'">',get_lang('NextPage'),' &gt;&gt;</a>';
  225. }
  226. elseif($page)
  227. {
  228. echo ' ',get_lang('NextPage'),' &gt;&gt;';
  229. }
  230. echo '</td>
  231. </tr>
  232. </table>
  233. </td>
  234. </tr>
  235. <tr bgcolor="#e6e6e6">';
  236. if(!empty($fromExercise))
  237. {
  238. echo '<th>',get_lang('Question'),'</th>',
  239. '<th>',get_lang('Level'),'</th>',
  240. '<th>',get_lang('Reuse'),'</th>';
  241. }
  242. else
  243. {
  244. echo '<td width="60%" align="center">',get_lang('Question'),'</td>',
  245. '<td width="20%" align="center">',get_lang('Modify'),'</td>',
  246. '<td width="20%" align="center">',get_lang('Delete'),'</td>';
  247. }
  248. echo '</tr>';
  249. $i=1;
  250. //print_r($objExercise->questionList);
  251. while ($row = Database::fetch_array($result))
  252. {
  253. // if we come from the exercise administration to get a question,
  254. // don't show the questions already used by that exercise
  255. /*if (!$fromExercise) {echo '1'; }
  256. if (!isset($objExercise)){echo '2';}
  257. if (!($objExercise instanceOf Exercise)){echo '3';}
  258. if (!$objExercise->isInList($row['id'])) {echo '4';}
  259. */
  260. // original recipe -
  261. //if (!$fromExercise || !isset($objExercise) || !($objExercise instanceOf Exercise) || (!$objExercise->isInList($row['id'])))
  262. if (!$fromExercise || !isset($objExercise) || !($objExercise instanceOf Exercise) || (is_array($objExercise->questionList)) )
  263. {
  264. echo '<tr ',($i%2==0?'class="row_odd"':'class="row_even"'),'>';
  265. echo ' <td><a href="admin.php?',api_get_cidreq(),'&editQuestion=',$row['id'],'&fromExercise=',$fromExercise,'">',$row['question'],'</a></td>';
  266. echo ' <td>';
  267. if (empty($fromExercise))
  268. {
  269. echo '<a href="admin.php?',api_get_cidreq(),'?editQuestion=',$row['id'],'"><img src="../img/edit.gif" border="0" alt="',get_lang('Modify'),'"></a>',
  270. '</td>',
  271. '<td align="center">',
  272. '<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&delete=',$row['id'],'" onclick="javascript:if(!confirm(\'',addslashes(htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)),'\')) return false;"><img src="../img/delete.gif" border="0" alt="',get_lang('Delete'),'"></a>';
  273. //'<a href="',api_get_self(),'?',api_get_cidreq(),'&exerciseId=',$exerciseId,'&delete=',$row['id'],'" onclick="javascript:if(!confirm(\'',addslashes(htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset)),'\')) return false;"><img src="../img/delete.gif" border="0" alt="',get_lang('Delete'),'"></a>';
  274. }
  275. else
  276. {
  277. //echo $row['level'],'</td>',
  278. // '<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>';
  279. echo $row['level'],'</td>',
  280. '<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>';
  281. }
  282. echo ' </td>';
  283. echo '</tr>';
  284. // skips the last question, that is only used to know if we have or not to create a link "Next page"
  285. if($i == $limitQuestPage)
  286. {
  287. break;
  288. }
  289. $i++;
  290. }
  291. }
  292. if(!$nbrQuestions)
  293. {
  294. echo '<tr>',
  295. '<td colspan="',($fromExercise?3:3),'">',get_lang('NoQuestion'),'</td>',
  296. '</tr>';
  297. }
  298. echo '</table>';
  299. Display::display_footer();
  300. }
  301. // if not admin of course
  302. else
  303. {
  304. api_not_allowed(true);
  305. }
  306. ?>