question_pool.php 11 KB

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