answer.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php // $Id: answer.class.php 9665 2006-10-24 10:43:48Z elixir_inter $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  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: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. if(!class_exists('Answer')):
  20. /*>>>>>>>>>>>>>>>>>>>> CLASS ANSWER <<<<<<<<<<<<<<<<<<<<*/
  21. /**
  22. * This class allows to instantiate an object of type Answer
  23. *
  24. * 5 arrays are created to receive the attributes of each answer
  25. * belonging to a specified question
  26. *
  27. * @author Olivier Brouckaert
  28. * @package dokeos.exercise
  29. */
  30. class Answer
  31. {
  32. var $questionId;
  33. // these are arrays
  34. var $answer;
  35. var $correct;
  36. var $comment;
  37. var $weighting;
  38. var $position;
  39. var $hotspot_coordinates;
  40. var $hotspot_type;
  41. // these arrays are used to save temporarily new answers
  42. // then they are moved into the arrays above or deleted in the event of cancellation
  43. var $new_answer;
  44. var $new_correct;
  45. var $new_comment;
  46. var $new_weighting;
  47. var $new_position;
  48. var $new_hotspot_coordinates;
  49. var $new_hotspot_type;
  50. var $nbrAnswers;
  51. var $new_nbrAnswers;
  52. /**
  53. * constructor of the class
  54. *
  55. * @author Olivier Brouckaert
  56. * @param integer Question ID that answers belong to
  57. */
  58. function Answer($questionId)
  59. {
  60. $this->questionType=$questionType;
  61. $this->questionId=$questionId;
  62. $this->answer=array();
  63. $this->correct=array();
  64. $this->comment=array();
  65. $this->weighting=array();
  66. $this->position=array();
  67. $this->hotspot_coordinates=array();
  68. $this->hotspot_type=array();
  69. // clears $new_* arrays
  70. $this->cancel();
  71. // fills arrays
  72. $this->read();
  73. }
  74. /**
  75. * clears $new_* arrays
  76. *
  77. * @author - Olivier Brouckaert
  78. */
  79. function cancel()
  80. {
  81. $this->new_answer=array();
  82. $this->new_correct=array();
  83. $this->new_comment=array();
  84. $this->new_weighting=array();
  85. $this->new_position=array();
  86. $this->new_hotspot_coordinates=array();
  87. $this->new_hotspot_type=array();
  88. $this->new_nbrAnswers=0;
  89. }
  90. /**
  91. * reads answer informations from the data base
  92. *
  93. * @author - Olivier Brouckaert
  94. */
  95. function read()
  96. {
  97. global $_course;
  98. $TBL_ANSWER = Database::get_course_table('quiz_answer');
  99. $questionId=$this->questionId;
  100. //$answerType=$this->selectType();
  101. $sql="SELECT answer,correct,comment,ponderation,position FROM $TBL_ANSWER WHERE question_id='$questionId' ORDER BY position";
  102. $result=api_sql_query($sql,__FILE__,__LINE__);
  103. $i=1;
  104. // while a record is found
  105. while($object=mysql_fetch_object($result))
  106. {
  107. $this->answer[$i]=$object->answer;
  108. $this->correct[$i]=$object->correct;
  109. $this->comment[$i]=$object->comment;
  110. $this->weighting[$i]=$object->ponderation;
  111. $this->position[$i]=$object->position;
  112. $this->hotspot_coordinates[$i]=$object->hotspot_coordinates;
  113. $this->hotspot_type[$i]=$object->hotspot_type;
  114. $i++;
  115. }
  116. $this->nbrAnswers=$i-1;
  117. }
  118. /**
  119. * reads answer informations from the data base ordered by parameter
  120. * @param string Field we want to order by
  121. * @param string DESC or ASC
  122. * @author Frederic Vauthier
  123. */
  124. function readOrderedBy($field,$order=ASC)
  125. {
  126. global $_course;
  127. $field = mysql_real_escape_string($field);
  128. if(empty($field)){
  129. $field = 'position';
  130. }
  131. if($order != 'ASC' and $order!='DESC'){
  132. $order = 'ASC';
  133. }
  134. $TBL_ANSWER = Database::get_course_table('quiz_answer');
  135. $questionId=$this->questionId;
  136. //$answerType=$this->selectType();
  137. $sql="SELECT answer,correct,comment,ponderation,position " .
  138. "FROM $TBL_ANSWER " .
  139. "WHERE question_id='$questionId' " .
  140. "ORDER BY $field $order";
  141. $result=api_sql_query($sql,__FILE__,__LINE__);
  142. $i=1;
  143. // while a record is found
  144. while($object=mysql_fetch_object($result))
  145. {
  146. $this->answer[$i]=$object->answer;
  147. $this->correct[$i]=$object->correct;
  148. $this->comment[$i]=$object->comment;
  149. $this->weighting[$i]=$object->ponderation;
  150. $this->position[$i]=$object->position;
  151. $i++;
  152. }
  153. $this->nbrAnswers=$i-1;
  154. }
  155. /**
  156. * returns the number of answers in this question
  157. *
  158. * @author - Olivier Brouckaert
  159. * @return - integer - number of answers
  160. */
  161. function selectNbrAnswers()
  162. {
  163. return $this->nbrAnswers;
  164. }
  165. /**
  166. * returns the question ID which the answers belong to
  167. *
  168. * @author - Olivier Brouckaert
  169. * @return - integer - the question ID
  170. */
  171. function selectQuestionId()
  172. {
  173. return $this->questionId;
  174. }
  175. /**
  176. * returns the answer title
  177. *
  178. * @author - Olivier Brouckaert
  179. * @param - integer $id - answer ID
  180. * @return - string - answer title
  181. */
  182. function selectAnswer($id)
  183. {
  184. return $this->answer[$id];
  185. }
  186. /**
  187. * Returns a list of answers
  188. * @author Yannick Warnier <ywarnier@beeznest.org>
  189. * @return array List of answers where each answer is an array of (id, answer, comment, grade) and grade=weighting
  190. */
  191. function getAnswersList()
  192. {
  193. $list = array();
  194. for($i = 1; $i<=$this->nbrAnswers;$i++){
  195. if(!empty($this->answer[$i])){
  196. $list[] = array(
  197. 'id'=>$i,
  198. 'answer'=>$this->answer[$i],
  199. 'comment'=>$this->comment[$i],
  200. 'grade' => $this->weighting[$i],
  201. 'hotspot_coord' => $this->hotspot_coordinates[$i],
  202. 'hotspot_type' => $this->hotspot_type[$i],
  203. 'correct' => $this->correct[$i]
  204. );
  205. }
  206. }
  207. return $list;
  208. }
  209. /**
  210. * Returns a list of grades
  211. * @author Yannick Warnier <ywarnier@beeznest.org>
  212. * @return array List of grades where grade=weighting (?)
  213. */
  214. function getGradesList()
  215. {
  216. $list = array();
  217. for($i = 0; $i<$this->nbrAnswers;$i++){
  218. if(!empty($this->answer[$i])){
  219. $list[$i] = $this->weighting[$i];
  220. }
  221. }
  222. return $list;
  223. }
  224. /**
  225. * Returns the question type
  226. * @author Yannick Warnier <ywarnier@beeznest.org>
  227. * @return integer The type of the question this answer is bound to
  228. */
  229. function getQuestionType()
  230. {
  231. $TBL_QUESTIONS = Database::get_course_table(QUIZ_QUESTION_TABLE);
  232. $sql = "SELECT * FROM $TBL_QUESTIONS WHERE id = '".$this->questionId."'";
  233. $res = api_sql_query($sql,__FILE__,__LINE__);
  234. if(Database::num_rows($res)<=0){
  235. return null;
  236. }
  237. $row = Database::fetch_array($res);
  238. return $row['type'];
  239. }
  240. /**
  241. * tells if answer is correct or not
  242. *
  243. * @author - Olivier Brouckaert
  244. * @param - integer $id - answer ID
  245. * @return - integer - 0 if bad answer, not 0 if good answer
  246. */
  247. function isCorrect($id)
  248. {
  249. return $this->correct[$id];
  250. }
  251. /**
  252. * returns answer comment
  253. *
  254. * @author - Olivier Brouckaert
  255. * @param - integer $id - answer ID
  256. * @return - string - answer comment
  257. */
  258. function selectComment($id)
  259. {
  260. return $this->comment[$id];
  261. }
  262. /**
  263. * returns answer weighting
  264. *
  265. * @author - Olivier Brouckaert
  266. * @param - integer $id - answer ID
  267. * @return - integer - answer weighting
  268. */
  269. function selectWeighting($id)
  270. {
  271. return $this->weighting[$id];
  272. }
  273. /**
  274. * returns answer position
  275. *
  276. * @author - Olivier Brouckaert
  277. * @param - integer $id - answer ID
  278. * @return - integer - answer position
  279. */
  280. function selectPosition($id)
  281. {
  282. return $this->position[$id];
  283. }
  284. /**
  285. * returns answer hotspot coordinates
  286. *
  287. * @author Olivier Brouckaert
  288. * @param integer Answer ID
  289. * @return integer Answer position
  290. */
  291. function selectHotspotCoordinates($id)
  292. {
  293. return $this->hotspot_coordinates[$id];
  294. }
  295. /**
  296. * returns answer hotspot type
  297. *
  298. * @author Toon Keppens
  299. * @param integer Answer ID
  300. * @return integer Answer position
  301. */
  302. function selectHotspotType($id)
  303. {
  304. return $this->hotspot_type[$id];
  305. }
  306. /**
  307. * creates a new answer
  308. *
  309. * @author Olivier Brouckaert
  310. * @param string answer title
  311. * @param integer 0 if bad answer, not 0 if good answer
  312. * @param string answer comment
  313. * @param integer answer weighting
  314. * @param integer answer position
  315. * @param coordinates Coordinates for hotspot exercises (optional)
  316. * @param integer Type for hotspot exercises (optional)
  317. */
  318. function createAnswer($answer,$correct,$comment,$weighting,$position,$new_hotspot_coordinates = NULL, $new_hotspot_type = NULL)
  319. {
  320. $this->new_nbrAnswers++;
  321. $id=$this->new_nbrAnswers;
  322. $this->new_answer[$id]=$answer;
  323. $this->new_correct[$id]=$correct;
  324. $this->new_comment[$id]=$comment;
  325. $this->new_weighting[$id]=$weighting;
  326. $this->new_position[$id]=$position;
  327. $this->new_hotspot_coordinates[$id]=$new_hotspot_coordinates;
  328. $this->new_hotspot_type[$id]=$new_hotspot_type;
  329. }
  330. /**
  331. * updates an answer
  332. *
  333. * @author Toon Keppens
  334. * @param string Answer title
  335. * @param string Answer comment
  336. * @param integer Answer weighting
  337. * @param integer Answer position
  338. */
  339. function updateAnswers($answer,$comment,$weighting,$position)
  340. {
  341. global $TBL_REPONSES;
  342. $questionId=$this->questionId;
  343. $sql = "UPDATE `$TBL_REPONSES` SET " .
  344. "`answer` = '$answer', " .
  345. "`comment` = '$comment', " .
  346. "`ponderation` = '$weighting', " .
  347. "`position` = '$position' " .
  348. "WHERE `id` =$position " .
  349. "AND `question_id` =$questionId";
  350. api_sql_query($sql,__FILE__,__LINE__);
  351. }
  352. /**
  353. * records answers into the data base
  354. *
  355. * @author - Olivier Brouckaert
  356. */
  357. function save()
  358. {
  359. global $TBL_REPONSES;
  360. $questionId=$this->questionId;
  361. // removes old answers before inserting of new ones
  362. $sql="DELETE FROM `$TBL_REPONSES` WHERE question_id='$questionId'";
  363. api_sql_query($sql,__FILE__,__LINE__);
  364. // inserts new answers into data base
  365. $sql="INSERT INTO `$TBL_REPONSES`" .
  366. "(id,question_id,answer,correct,comment," .
  367. "ponderation,position,hotspot_coordinates,hotspot_type) VALUES";
  368. for($i=1;$i <= $this->new_nbrAnswers;$i++)
  369. {
  370. $answer=addslashes($this->new_answer[$i]);
  371. $correct=$this->new_correct[$i];
  372. $comment=addslashes($this->new_comment[$i]);
  373. $weighting=$this->new_weighting[$i];
  374. $position=$this->new_position[$i];
  375. $hotspot_coordinates=$this->new_hotspot_coordinates[$i];
  376. $hotspot_type=$this->new_hotspot_type[$i];
  377. $sql.="('$i','$questionId','$answer','$correct','$comment',
  378. '$weighting','$position','$hotspot_coordinates','$hotspot_type'),";
  379. }
  380. $sql = substr($sql,0,-1);
  381. api_sql_query($sql,__FILE__,__LINE__);
  382. // moves $new_* arrays
  383. $this->answer=$this->new_answer;
  384. $this->correct=$this->new_correct;
  385. $this->comment=$this->new_comment;
  386. $this->weighting=$this->new_weighting;
  387. $this->position=$this->new_position;
  388. $this->hotspot_coordinates=$this->new_hotspot_coordinates;
  389. $this->hotspot_type=$this->new_hotspot_type;
  390. $this->nbrAnswers=$this->new_nbrAnswers;
  391. // clears $new_* arrays
  392. $this->cancel();
  393. }
  394. /**
  395. * duplicates answers by copying them into another question
  396. *
  397. * @author - Olivier Brouckaert
  398. * @param - integer $newQuestionId - ID of the new question
  399. */
  400. function duplicate($newQuestionId)
  401. {
  402. global $TBL_REPONSES;
  403. // if at least one answer
  404. if($this->nbrAnswers)
  405. {
  406. // inserts new answers into data base
  407. $sql="INSERT INTO `$TBL_REPONSES`" .
  408. "(id,question_id,answer,correct,comment," .
  409. "ponderation,position,hotspot_coordinates,hotspot_type) VALUES";
  410. for($i=1;$i <= $this->nbrAnswers;$i++)
  411. {
  412. $answer=addslashes($this->answer[$i]);
  413. $correct=$this->correct[$i];
  414. $comment=addslashes($this->comment[$i]);
  415. $weighting=$this->weighting[$i];
  416. $position=$this->position[$i];
  417. $hotspot_coordinates=$this->hotspot_coordinates[$i];
  418. $hotspot_type=$this->hotspot_type[$i];
  419. $sql.="('$i','$newQuestionId','$answer','$correct','$comment'," .
  420. "'$weighting','$position','$hotspot_coordinates','$hotspot_type'),";
  421. }
  422. $sql=substr($sql,0,-1);
  423. api_sql_query($sql,__FILE__,__LINE__);
  424. }
  425. }
  426. }
  427. endif;
  428. ?>