answer.class.php 18 KB

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