answer.class.php 18 KB

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