answer.class.php 18 KB

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