answer.class.php 17 KB

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