answer.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class allows to instantiate an object of type Answer
  5. * 5 arrays are created to receive the attributes of each answer belonging to a specified question
  6. * @package chamilo.exercise
  7. * @author Olivier Brouckaert
  8. * @version $Id: answer.class.php 21172 2009-06-01 20:58:05Z darkvela $
  9. */
  10. if(!class_exists('Answer')):
  11. class Answer
  12. {
  13. var $questionId;
  14. // these are arrays
  15. var $answer;
  16. var $correct;
  17. var $comment;
  18. var $weighting;
  19. var $position;
  20. var $hotspot_coordinates;
  21. var $hotspot_type;
  22. var $destination;
  23. // these arrays are used to save temporarily new answers
  24. // then they are moved into the arrays above or deleted in the event of cancellation
  25. var $new_answer;
  26. var $new_correct;
  27. var $new_comment;
  28. var $new_weighting;
  29. var $new_position;
  30. var $new_hotspot_coordinates;
  31. var $new_hotspot_type;
  32. var $nbrAnswers;
  33. var $new_nbrAnswers;
  34. var $new_destination; // id of the next question if feedback option is set to Directfeedback
  35. /**
  36. * constructor of the class
  37. *
  38. * @author Olivier Brouckaert
  39. * @param integer Question ID that answers belong to
  40. */
  41. function Answer($questionId)
  42. {
  43. //$this->questionType=$questionType;
  44. $this->questionId = (int)$questionId;
  45. $this->answer = array();
  46. $this->correct = array();
  47. $this->comment = array();
  48. $this->weighting = array();
  49. $this->position = array();
  50. $this->hotspot_coordinates = array();
  51. $this->hotspot_type = array();
  52. $this->destination = array();
  53. // clears $new_* arrays
  54. $this->cancel();
  55. // fills arrays
  56. $objExercise = new Exercise();
  57. $objExercise->read($_REQUEST['exerciseId']);
  58. if($objExercise->random_answers=='1') {
  59. $this->readOrderedBy('rand()', '');// randomize answers
  60. } else {
  61. $this->read(); // natural order
  62. }
  63. }
  64. /**
  65. * Clears $new_* arrays
  66. *
  67. * @author - Olivier Brouckaert
  68. */
  69. function cancel()
  70. {
  71. $this->new_answer = array();
  72. $this->new_correct = array();
  73. $this->new_comment = array();
  74. $this->new_weighting = array();
  75. $this->new_position = array();
  76. $this->new_hotspot_coordinates = array();
  77. $this->new_hotspot_type = array();
  78. $this->new_nbrAnswers = 0;
  79. $this->new_destination = array();
  80. }
  81. /**
  82. * Reads answer informations from the data base
  83. *
  84. * @author - Olivier Brouckaert
  85. */
  86. function read()
  87. {
  88. global $_course;
  89. $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
  90. $questionId=$this->questionId;
  91. //$answerType=$this->selectType();
  92. $sql="SELECT id,answer,correct,comment,ponderation, position, hotspot_coordinates, hotspot_type, destination, id_auto FROM
  93. $TBL_ANSWER WHERE question_id ='".Database::escape_string($questionId)."' ORDER BY position";
  94. $result=Database::query($sql);
  95. $i=1;
  96. // while a record is found
  97. while($object=Database::fetch_object($result)) {
  98. $this->id[$i] = $object->id;
  99. $this->answer[$i] = $object->answer;
  100. $this->correct[$i] = $object->correct;
  101. $this->comment[$i] = $object->comment;
  102. $this->weighting[$i] = $object->ponderation;
  103. $this->position[$i] = $object->position;
  104. $this->hotspot_coordinates[$i] = $object->hotspot_coordinates;
  105. $this->hotspot_type[$i] = $object->hotspot_type;
  106. $this->destination[$i] = $object->destination;
  107. $this->autoId[$i] = $object->id_auto;
  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 = Database::escape_string($field);
  122. if(empty($field)) {
  123. $field = 'position';
  124. }
  125. if($order != 'ASC' and $order!='DESC')
  126. {
  127. $order = 'ASC';
  128. }
  129. $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
  130. $questionId=$this->questionId;
  131. //$answerType=$this->selectType();
  132. $sql="SELECT answer,correct,comment,ponderation,position, hotspot_coordinates, hotspot_type, destination, id_auto " .
  133. "FROM $TBL_ANSWER WHERE question_id='".Database::escape_string($questionId)."' " .
  134. "ORDER BY $field $order";
  135. $result=Database::query($sql);
  136. $i=1;
  137. // while a record is found
  138. while($object=Database::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. $this->destination[$i] = $object->destination;
  146. $this->autoId[$i] = $object->id_auto;
  147. $i++;
  148. }
  149. $this->nbrAnswers=$i-1;
  150. }
  151. /**
  152. * returns the autoincrement id identificator
  153. *
  154. * @author - Juan Carlos Ra�a
  155. * @return - integer - answer num
  156. */
  157. function selectAutoId($id)
  158. {
  159. return $this->autoId[$id];
  160. }
  161. /**
  162. * returns the number of answers in this question
  163. *
  164. * @author - Olivier Brouckaert
  165. * @return - integer - number of answers
  166. */
  167. function selectNbrAnswers()
  168. {
  169. return $this->nbrAnswers;
  170. }
  171. /**
  172. * returns the question ID which the answers belong to
  173. *
  174. * @author - Olivier Brouckaert
  175. * @return - integer - the question ID
  176. */
  177. function selectQuestionId()
  178. {
  179. return $this->questionId;
  180. }
  181. /**
  182. * returns the question ID of the destination question
  183. *
  184. * @author - Julio Montoya
  185. * @return - integer - the question ID
  186. */
  187. function selectDestination($id)
  188. {
  189. return $this->destination[$id];
  190. }
  191. /**
  192. * returns the answer title
  193. *
  194. * @author - Olivier Brouckaert
  195. * @param - integer $id - answer ID
  196. * @return - string - answer title
  197. */
  198. function selectAnswer($id)
  199. {
  200. return $this->answer[$id];
  201. }
  202. /**
  203. * return array answer by id else return a bool
  204. */
  205. function selectAnswerByAutoId($auto_id) {
  206. $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
  207. $auto_id = intval($auto_id);
  208. $sql="SELECT id, answer FROM $TBL_ANSWER WHERE id_auto='$auto_id'";
  209. $rs = Database::query($sql);
  210. if (Database::num_rows($rs)>0) {
  211. $row = Database::fetch_array($rs);
  212. return $row;
  213. }
  214. return false;
  215. }
  216. /**
  217. * returns the answer title from an answer's position
  218. *
  219. * @author - Yannick Warnier
  220. * @param - integer $id - answer ID
  221. * @return - bool - answer title
  222. */
  223. function selectAnswerIdByPosition($pos)
  224. {
  225. foreach ($this->position as $k => $v) {
  226. if ($v != $pos) { continue; }
  227. return $k;
  228. }
  229. return false;
  230. }
  231. /**
  232. * Returns a list of answers
  233. * @author Yannick Warnier <ywarnier@beeznest.org>
  234. * @return array List of answers where each answer is an array of (id, answer, comment, grade) and grade=weighting
  235. */
  236. function getAnswersList()
  237. {
  238. $list = array();
  239. for($i = 1; $i<=$this->nbrAnswers;$i++){
  240. if(!empty($this->answer[$i])){
  241. $list[] = array(
  242. 'id'=>$i,
  243. 'answer'=>$this->answer[$i],
  244. 'comment'=>$this->comment[$i],
  245. 'grade' => $this->weighting[$i],
  246. 'hotspot_coord' => $this->hotspot_coordinates[$i],
  247. 'hotspot_type' => $this->hotspot_type[$i],
  248. 'correct' => $this->correct[$i],
  249. 'destination' => $this->destination[$i]
  250. );
  251. }
  252. }
  253. return $list;
  254. }
  255. /**
  256. * Returns a list of grades
  257. * @author Yannick Warnier <ywarnier@beeznest.org>
  258. * @return array List of grades where grade=weighting (?)
  259. */
  260. function getGradesList()
  261. {
  262. $list = array();
  263. for($i = 0; $i<$this->nbrAnswers;$i++){
  264. if(!empty($this->answer[$i])){
  265. $list[$i] = $this->weighting[$i];
  266. }
  267. }
  268. return $list;
  269. }
  270. /**
  271. * Returns the question type
  272. * @author Yannick Warnier <ywarnier@beeznest.org>
  273. * @return integer The type of the question this answer is bound to
  274. */
  275. function getQuestionType()
  276. {
  277. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  278. $sql = "SELECT type FROM $TBL_QUESTIONS WHERE id = '".Database::escape_string($this->questionId)."'";
  279. $res = Database::query($sql);
  280. if(Database::num_rows($res)<=0){
  281. return null;
  282. }
  283. $row = Database::fetch_array($res);
  284. return $row['type'];
  285. }
  286. /**
  287. * tells if answer is correct or not
  288. *
  289. * @author - Olivier Brouckaert
  290. * @param - integer $id - answer ID
  291. * @return - integer - 0 if bad answer, not 0 if good answer
  292. */
  293. function isCorrect($id)
  294. {
  295. return $this->correct[$id];
  296. }
  297. /**
  298. * returns answer comment
  299. *
  300. * @author - Olivier Brouckaert
  301. * @param - integer $id - answer ID
  302. * @return - string - answer comment
  303. */
  304. function selectComment($id)
  305. {
  306. return $this->comment[$id];
  307. }
  308. /**
  309. * returns answer weighting
  310. *
  311. * @author - Olivier Brouckaert
  312. * @param - integer $id - answer ID
  313. * @return - integer - answer weighting
  314. */
  315. function selectWeighting($id)
  316. {
  317. return $this->weighting[$id];
  318. }
  319. /**
  320. * returns answer position
  321. *
  322. * @author - Olivier Brouckaert
  323. * @param - integer $id - answer ID
  324. * @return - integer - answer position
  325. */
  326. function selectPosition($id)
  327. {
  328. return $this->position[$id];
  329. }
  330. /**
  331. * returns answer hotspot coordinates
  332. *
  333. * @author Olivier Brouckaert
  334. * @param integer Answer ID
  335. * @return integer Answer position
  336. */
  337. function selectHotspotCoordinates($id)
  338. {
  339. return $this->hotspot_coordinates[$id];
  340. }
  341. /**
  342. * returns answer hotspot type
  343. *
  344. * @author Toon Keppens
  345. * @param integer Answer ID
  346. * @return integer Answer position
  347. */
  348. function selectHotspotType($id)
  349. {
  350. return $this->hotspot_type[$id];
  351. }
  352. /**
  353. * creates a new answer
  354. *
  355. * @author Olivier Brouckaert
  356. * @param string answer title
  357. * @param integer 0 if bad answer, not 0 if good answer
  358. * @param string answer comment
  359. * @param integer answer weighting
  360. * @param integer answer position
  361. * @param coordinates Coordinates for hotspot exercises (optional)
  362. * @param integer Type for hotspot exercises (optional)
  363. */
  364. function createAnswer($answer,$correct,$comment,$weighting,$position,$new_hotspot_coordinates = NULL, $new_hotspot_type = NULL,$destination='')
  365. {
  366. $this->new_nbrAnswers++;
  367. $id=$this->new_nbrAnswers;
  368. $this->new_answer[$id]=$answer;
  369. $this->new_correct[$id]=$correct;
  370. $this->new_comment[$id]=$comment;
  371. $this->new_weighting[$id]=$weighting;
  372. $this->new_position[$id]=$position;
  373. $this->new_hotspot_coordinates[$id]=$new_hotspot_coordinates;
  374. $this->new_hotspot_type[$id]=$new_hotspot_type;
  375. $this->new_destination[$id]=$destination;
  376. }
  377. /**
  378. * updates an answer
  379. *
  380. * @author Toon Keppens
  381. * @param string Answer title
  382. * @param string Answer comment
  383. * @param integer Answer weighting
  384. * @param integer Answer position
  385. */
  386. function updateAnswers($answer,$comment,$weighting,$position,$destination)
  387. {
  388. $TBL_REPONSES = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  389. $questionId=$this->questionId;
  390. $sql = "UPDATE $TBL_REPONSES SET " .
  391. "answer = '".Database::escape_string($answer)."', " .
  392. "comment = '".Database::escape_string($comment)."', " .
  393. "ponderation = '".Database::escape_string($weighting)."', " .
  394. "position = '".Database::escape_string($position)."', " .
  395. "destination = '".Database::escape_string($destination)."' " .
  396. "WHERE id = '".Database::escape_string($position)."' " .
  397. "AND question_i` = '".Database::escape_string($questionId)."'";
  398. Database::query($sql);
  399. }
  400. /**
  401. * Records answers into the data base
  402. *
  403. * @author - Olivier Brouckaert
  404. */
  405. function save()
  406. {
  407. $TBL_REPONSES = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  408. $questionId=$this->questionId;
  409. // removes old answers before inserting of new ones
  410. $sql="DELETE FROM $TBL_REPONSES WHERE question_id='".Database::escape_string($questionId)."'";
  411. Database::query($sql);
  412. // inserts new answers into data base
  413. $sql="INSERT INTO $TBL_REPONSES" .
  414. "(id,question_id,answer,correct,comment," .
  415. "ponderation,position,hotspot_coordinates,hotspot_type,destination) VALUES";
  416. for($i=1;$i <= $this->new_nbrAnswers;$i++) {
  417. $answer = Database::escape_string($this->new_answer[$i]);
  418. $correct = Database::escape_string($this->new_correct[$i]);
  419. $comment = Database::escape_string($this->new_comment[$i]);
  420. $weighting = Database::escape_string($this->new_weighting[$i]);
  421. $position = Database::escape_string($this->new_position[$i]);
  422. $hotspot_coordinates = Database::escape_string($this->new_hotspot_coordinates[$i]);
  423. $hotspot_type = Database::escape_string($this->new_hotspot_type[$i]);
  424. $destination = Database::escape_string($this->new_destination[$i]);
  425. $sql.="('$i','$questionId','$answer','$correct','$comment',
  426. '$weighting','$position','$hotspot_coordinates','$hotspot_type','$destination'),";
  427. }
  428. $sql = api_substr($sql,0,-1);
  429. Database::query($sql);
  430. // moves $new_* arrays
  431. $this->answer=$this->new_answer;
  432. $this->correct=$this->new_correct;
  433. $this->comment=$this->new_comment;
  434. $this->weighting=$this->new_weighting;
  435. $this->position=$this->new_position;
  436. $this->hotspot_coordinates=$this->new_hotspot_coordinates;
  437. $this->hotspot_type=$this->new_hotspot_type;
  438. $this->nbrAnswers=$this->new_nbrAnswers;
  439. $this->destination=$this->new_destination;
  440. // clears $new_* arrays
  441. $this->cancel();
  442. }
  443. /**
  444. * Duplicates answers by copying them into another question
  445. *
  446. * @author - Olivier Brouckaert
  447. * @param - integer $newQuestionId - ID of the new question
  448. */
  449. function duplicate($newQuestionId)
  450. {
  451. $TBL_REPONSES = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  452. // if at least one answer
  453. if($this->nbrAnswers) {
  454. // inserts new answers into data base
  455. $sql="INSERT INTO $TBL_REPONSES" .
  456. "(id,question_id,answer,correct,comment," .
  457. "ponderation,position,hotspot_coordinates,hotspot_type,destination) VALUES";
  458. for($i=1;$i <= $this->nbrAnswers;$i++) {
  459. $answer = Database::escape_string($this->answer[$i]);
  460. $correct = Database::escape_string($this->correct[$i]);
  461. $comment = Database::escape_string($this->comment[$i]);
  462. $weighting = Database::escape_string($this->weighting[$i]);
  463. $position = Database::escape_string($this->position[$i]);
  464. $hotspot_coordinates = Database::escape_string($this->hotspot_coordinates[$i]);
  465. $hotspot_type = Database::escape_string($this->hotspot_type[$i]);
  466. $destination = Database::escape_string($this->destination[$i]);
  467. $sql.="('$i','$newQuestionId','$answer','$correct','$comment'," .
  468. "'$weighting','$position','$hotspot_coordinates','$hotspot_type','$destination'),";
  469. }
  470. $sql=api_substr($sql,0,-1);
  471. Database::query($sql);
  472. }
  473. }
  474. }
  475. endif;
  476. ?>