answer.class.php 14 KB

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