answer.class.php 13 KB

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