answer.class.php 12 KB

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