answer.class.php 14 KB

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