answer.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 15832 2008-07-21 08:02:02Z 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
  96. $TBL_ANSWER WHERE question_id ='".Database::escape_string($questionId)."' ORDER BY position";
  97. $result=api_sql_query($sql,__FILE__,__LINE__);
  98. $i=1;
  99. // while a record is found
  100. while($object=mysql_fetch_object($result))
  101. {
  102. $this->answer[$i]=$object->answer;
  103. $this->correct[$i]=$object->correct;
  104. $this->comment[$i]=$object->comment;
  105. $this->weighting[$i]=$object->ponderation;
  106. $this->position[$i]=$object->position;
  107. $this->hotspot_coordinates[$i]=$object->hotspot_coordinates;
  108. $this->hotspot_type[$i]=$object->hotspot_type;
  109. $i++;
  110. }
  111. $this->nbrAnswers=$i-1;
  112. }
  113. /**
  114. * reads answer informations from the data base ordered by parameter
  115. * @param string Field we want to order by
  116. * @param string DESC or ASC
  117. * @author Frederic Vauthier
  118. */
  119. function readOrderedBy($field,$order=ASC)
  120. {
  121. global $_course;
  122. $field = Database::escape_string($field);
  123. if(empty($field))
  124. {
  125. $field = 'position';
  126. }
  127. if($order != 'ASC' and $order!='DESC')
  128. {
  129. $order = 'ASC';
  130. }
  131. $TBL_ANSWER = Database::get_course_table('quiz_answer');
  132. $questionId=$this->questionId;
  133. //$answerType=$this->selectType();
  134. $sql="SELECT answer,correct,comment,ponderation,position, hotspot_coordinates, hotspot_type " .
  135. "FROM $TBL_ANSWER WHERE question_id='".Database::escape_string($questionId)."' " .
  136. "ORDER BY $field $order";
  137. $result=api_sql_query($sql,__FILE__,__LINE__);
  138. $i=1;
  139. // while a record is found
  140. while($object=mysql_fetch_object($result))
  141. {
  142. $this->answer[$i]=$object->answer;
  143. $this->correct[$i]=$object->correct;
  144. $this->comment[$i]=$object->comment;
  145. $this->weighting[$i]=$object->ponderation;
  146. $this->position[$i]=$object->position;
  147. $i++;
  148. }
  149. $this->nbrAnswers=$i-1;
  150. }
  151. /**
  152. * returns the number of answers in this question
  153. *
  154. * @author - Olivier Brouckaert
  155. * @return - integer - number of answers
  156. */
  157. function selectNbrAnswers()
  158. {
  159. return $this->nbrAnswers;
  160. }
  161. /**
  162. * returns the question ID which the answers belong to
  163. *
  164. * @author - Olivier Brouckaert
  165. * @return - integer - the question ID
  166. */
  167. function selectQuestionId()
  168. {
  169. return $this->questionId;
  170. }
  171. /**
  172. * returns the answer title
  173. *
  174. * @author - Olivier Brouckaert
  175. * @param - integer $id - answer ID
  176. * @return - string - answer title
  177. */
  178. function selectAnswer($id)
  179. {
  180. return $this->answer[$id];
  181. }
  182. /**
  183. * Returns a list of answers
  184. * @author Yannick Warnier <ywarnier@beeznest.org>
  185. * @return array List of answers where each answer is an array of (id, answer, comment, grade) and grade=weighting
  186. */
  187. function getAnswersList()
  188. {
  189. $list = array();
  190. for($i = 1; $i<=$this->nbrAnswers;$i++){
  191. if(!empty($this->answer[$i])){
  192. $list[] = array(
  193. 'id'=>$i,
  194. 'answer'=>addslashes($this->answer[$i]),
  195. 'comment'=>$this->comment[$i],
  196. 'grade' => $this->weighting[$i],
  197. 'hotspot_coord' => $this->hotspot_coordinates[$i],
  198. 'hotspot_type' => $this->hotspot_type[$i],
  199. 'correct' => $this->correct[$i]
  200. );
  201. }
  202. }
  203. return $list;
  204. }
  205. /**
  206. * Returns a list of grades
  207. * @author Yannick Warnier <ywarnier@beeznest.org>
  208. * @return array List of grades where grade=weighting (?)
  209. */
  210. function getGradesList()
  211. {
  212. $list = array();
  213. for($i = 0; $i<$this->nbrAnswers;$i++){
  214. if(!empty($this->answer[$i])){
  215. $list[$i] = $this->weighting[$i];
  216. }
  217. }
  218. return $list;
  219. }
  220. /**
  221. * Returns the question type
  222. * @author Yannick Warnier <ywarnier@beeznest.org>
  223. * @return integer The type of the question this answer is bound to
  224. */
  225. function getQuestionType()
  226. {
  227. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  228. $sql = "SELECT * FROM $TBL_QUESTIONS WHERE id = '".Database::escape_string($this->questionId)."'";
  229. $res = api_sql_query($sql,__FILE__,__LINE__);
  230. if(Database::num_rows($res)<=0){
  231. return null;
  232. }
  233. $row = Database::fetch_array($res);
  234. return $row['type'];
  235. }
  236. /**
  237. * tells if answer is correct or not
  238. *
  239. * @author - Olivier Brouckaert
  240. * @param - integer $id - answer ID
  241. * @return - integer - 0 if bad answer, not 0 if good answer
  242. */
  243. function isCorrect($id)
  244. {
  245. return $this->correct[$id];
  246. }
  247. /**
  248. * returns answer comment
  249. *
  250. * @author - Olivier Brouckaert
  251. * @param - integer $id - answer ID
  252. * @return - string - answer comment
  253. */
  254. function selectComment($id)
  255. {
  256. return $this->comment[$id];
  257. }
  258. /**
  259. * returns answer weighting
  260. *
  261. * @author - Olivier Brouckaert
  262. * @param - integer $id - answer ID
  263. * @return - integer - answer weighting
  264. */
  265. function selectWeighting($id)
  266. {
  267. return $this->weighting[$id];
  268. }
  269. /**
  270. * returns answer position
  271. *
  272. * @author - Olivier Brouckaert
  273. * @param - integer $id - answer ID
  274. * @return - integer - answer position
  275. */
  276. function selectPosition($id)
  277. {
  278. return $this->position[$id];
  279. }
  280. /**
  281. * returns answer hotspot coordinates
  282. *
  283. * @author Olivier Brouckaert
  284. * @param integer Answer ID
  285. * @return integer Answer position
  286. */
  287. function selectHotspotCoordinates($id)
  288. {
  289. return $this->hotspot_coordinates[$id];
  290. }
  291. /**
  292. * returns answer hotspot type
  293. *
  294. * @author Toon Keppens
  295. * @param integer Answer ID
  296. * @return integer Answer position
  297. */
  298. function selectHotspotType($id)
  299. {
  300. return $this->hotspot_type[$id];
  301. }
  302. /**
  303. * creates a new answer
  304. *
  305. * @author Olivier Brouckaert
  306. * @param string answer title
  307. * @param integer 0 if bad answer, not 0 if good answer
  308. * @param string answer comment
  309. * @param integer answer weighting
  310. * @param integer answer position
  311. * @param coordinates Coordinates for hotspot exercises (optional)
  312. * @param integer Type for hotspot exercises (optional)
  313. */
  314. function createAnswer($answer,$correct,$comment,$weighting,$position,$new_hotspot_coordinates = NULL, $new_hotspot_type = NULL)
  315. {
  316. $this->new_nbrAnswers++;
  317. $id=$this->new_nbrAnswers;
  318. $this->new_answer[$id]=$answer;
  319. $this->new_correct[$id]=$correct;
  320. $this->new_comment[$id]=$comment;
  321. $this->new_weighting[$id]=$weighting;
  322. $this->new_position[$id]=$position;
  323. $this->new_hotspot_coordinates[$id]=$new_hotspot_coordinates;
  324. $this->new_hotspot_type[$id]=$new_hotspot_type;
  325. }
  326. /**
  327. * updates an answer
  328. *
  329. * @author Toon Keppens
  330. * @param string Answer title
  331. * @param string Answer comment
  332. * @param integer Answer weighting
  333. * @param integer Answer position
  334. */
  335. function updateAnswers($answer,$comment,$weighting,$position)
  336. {
  337. global $TBL_REPONSES;
  338. $questionId=$this->questionId;
  339. $sql = "UPDATE $TBL_REPONSES SET " .
  340. "answer = '".Database::escape_string($answer)."', " .
  341. "comment = '".Database::escape_string($comment)."', " .
  342. "ponderation = '".Database::escape_string($weighting)."', " .
  343. "position = '".Database::escape_string($position)."' " .
  344. "WHERE id = '".Database::escape_string($position)."' " .
  345. "AND question_i` = '".Database::escape_string($questionId)."'";
  346. api_sql_query($sql,__FILE__,__LINE__);
  347. }
  348. /**
  349. * records answers into the data base
  350. *
  351. * @author - Olivier Brouckaert
  352. */
  353. function save()
  354. {
  355. global $TBL_REPONSES;
  356. $questionId=$this->questionId;
  357. // removes old answers before inserting of new ones
  358. $sql="DELETE FROM $TBL_REPONSES WHERE question_id='".Database::escape_string($questionId)."'";
  359. api_sql_query($sql,__FILE__,__LINE__);
  360. // inserts new answers into data base
  361. $sql="INSERT INTO $TBL_REPONSES" .
  362. "(id,question_id,answer,correct,comment," .
  363. "ponderation,position,hotspot_coordinates,hotspot_type) VALUES";
  364. for($i=1;$i <= $this->new_nbrAnswers;$i++)
  365. {
  366. $answer = Database::escape_string($this->new_answer[$i]);
  367. $correct = Database::escape_string($this->new_correct[$i]);
  368. $comment = Database::escape_string($this->new_comment[$i]);
  369. $weighting = Database::escape_string($this->new_weighting[$i]);
  370. $position = Database::escape_string($this->new_position[$i]);
  371. $hotspot_coordinates = Database::escape_string($this->new_hotspot_coordinates[$i]);
  372. $hotspot_type = Database::escape_string($this->new_hotspot_type[$i]);
  373. $sql.="('$i','$questionId','$answer','$correct','$comment',
  374. '$weighting','$position','$hotspot_coordinates','$hotspot_type'),";
  375. }
  376. $sql = substr($sql,0,-1);
  377. api_sql_query($sql,__FILE__,__LINE__);
  378. // moves $new_* arrays
  379. $this->answer=$this->new_answer;
  380. $this->correct=$this->new_correct;
  381. $this->comment=$this->new_comment;
  382. $this->weighting=$this->new_weighting;
  383. $this->position=$this->new_position;
  384. $this->hotspot_coordinates=$this->new_hotspot_coordinates;
  385. $this->hotspot_type=$this->new_hotspot_type;
  386. $this->nbrAnswers=$this->new_nbrAnswers;
  387. // clears $new_* arrays
  388. $this->cancel();
  389. }
  390. /**
  391. * duplicates answers by copying them into another question
  392. *
  393. * @author - Olivier Brouckaert
  394. * @param - integer $newQuestionId - ID of the new question
  395. */
  396. function duplicate($newQuestionId)
  397. {
  398. global $TBL_REPONSES;
  399. // if at least one answer
  400. if($this->nbrAnswers)
  401. {
  402. // inserts new answers into data base
  403. $sql="INSERT INTO $TBL_REPONSES" .
  404. "(id,question_id,answer,correct,comment," .
  405. "ponderation,position,hotspot_coordinates,hotspot_type) VALUES";
  406. for($i=1;$i <= $this->nbrAnswers;$i++)
  407. {
  408. $answer = Database::escape_string($this->answer[$i]);
  409. $correct = Database::escape_string($this->correct[$i]);
  410. $comment = Database::escape_string($this->comment[$i]);
  411. $weighting = Database::escape_string($this->weighting[$i]);
  412. $position = Database::escape_string($this->position[$i]);
  413. $hotspot_coordinates = Database::escape_string($this->hotspot_coordinates[$i]);
  414. $hotspot_type = Database::escape_string($this->hotspot_type[$i]);
  415. $sql.="('$i','$newQuestionId','$answer','$correct','$comment'," .
  416. "'$weighting','$position','$hotspot_coordinates','$hotspot_type'),";
  417. }
  418. $sql=substr($sql,0,-1);
  419. api_sql_query($sql,__FILE__,__LINE__);
  420. }
  421. }
  422. }
  423. endif;
  424. ?>