answer.class.php 18 KB

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