answer.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Answer
  5. * Allows to instantiate an object of type Answer
  6. * 5 arrays are created to receive the attributes of each answer belonging to a specified question
  7. * @package chamilo.exercise
  8. *
  9. * @author Olivier Brouckaert
  10. */
  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 $autoId;
  33. public $nbrAnswers;
  34. public $new_nbrAnswers;
  35. public $new_destination; // id of the next question if feedback option is set to Directfeedback
  36. public $course; //Course information
  37. /**
  38. * constructor of the class
  39. *
  40. * @author Olivier Brouckaert
  41. * @param int $questionId that answers belong to
  42. * @param int $course_id
  43. */
  44. public function __construct($questionId, $course_id = null)
  45. {
  46. $this->questionId = intval($questionId);
  47. $this->answer = array();
  48. $this->correct = array();
  49. $this->comment = array();
  50. $this->weighting = array();
  51. $this->position = array();
  52. $this->hotspot_coordinates = array();
  53. $this->hotspot_type = array();
  54. $this->destination = array();
  55. // clears $new_* arrays
  56. $this->cancel();
  57. if (!empty($course_id)) {
  58. $courseInfo = api_get_course_info_by_id($course_id);
  59. } else {
  60. $courseInfo = api_get_course_info();
  61. }
  62. $this->course = $courseInfo;
  63. $this->course_id = $courseInfo['real_id'];
  64. // fills arrays
  65. $objExercise = new Exercise($this->course_id);
  66. $exerciseId = isset($_REQUEST['exerciseId']) ? $_REQUEST['exerciseId'] : null;
  67. $objExercise->read($exerciseId);
  68. if ($objExercise->random_answers == '1') {
  69. $this->readOrderedBy('rand()', '');// randomize answers
  70. } else {
  71. $this->read(); // natural order
  72. }
  73. }
  74. /**
  75. * Clears $new_* arrays
  76. *
  77. * @author Olivier Brouckaert
  78. */
  79. public function cancel()
  80. {
  81. $this->new_answer = array();
  82. $this->new_correct = array();
  83. $this->new_comment = array();
  84. $this->new_weighting = array();
  85. $this->new_position = array();
  86. $this->new_hotspot_coordinates = array();
  87. $this->new_hotspot_type = array();
  88. $this->new_nbrAnswers = 0;
  89. $this->new_destination = array();
  90. }
  91. /**
  92. * Reads answer information from the database
  93. *
  94. * @author Olivier Brouckaert
  95. */
  96. public function read()
  97. {
  98. $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
  99. $questionId = $this->questionId;
  100. $sql = "SELECT * FROM $TBL_ANSWER
  101. WHERE
  102. c_id = {$this->course_id} AND
  103. question_id ='".$questionId."'
  104. ORDER BY position";
  105. $result = Database::query($sql);
  106. $i=1;
  107. // while a record is found
  108. while ($object = Database::fetch_object($result)) {
  109. $this->id[$i] = $object->id;
  110. $this->answer[$i] = $object->answer;
  111. $this->correct[$i] = $object->correct;
  112. $this->comment[$i] = $object->comment;
  113. $this->weighting[$i] = $object->ponderation;
  114. $this->position[$i] = $object->position;
  115. $this->hotspot_coordinates[$i] = $object->hotspot_coordinates;
  116. $this->hotspot_type[$i] = $object->hotspot_type;
  117. $this->destination[$i] = $object->destination;
  118. $this->autoId[$i] = $object->id_auto;
  119. $i++;
  120. }
  121. $this->nbrAnswers = $i-1;
  122. }
  123. /**
  124. * returns all answer ids from this question Id
  125. *
  126. * @author Yoselyn Castillo
  127. * @return array - $id (answer ids)
  128. */
  129. public function selectAnswerId()
  130. {
  131. $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
  132. $questionId = $this->questionId;
  133. $sql="SELECT id FROM
  134. $TBL_ANSWER
  135. WHERE c_id = {$this->course_id} AND question_id ='".$questionId."'";
  136. $result = Database::query($sql);
  137. $id = array();
  138. // while a record is found
  139. if (Database::num_rows($result) > 0) {
  140. while ($object = Database::fetch_array($result)) {
  141. $id[] = $object['id'];
  142. }
  143. }
  144. return $id;
  145. }
  146. /**
  147. * Reads answer information from the data base ordered by parameter
  148. * @param string Field we want to order by
  149. * @param string DESC or ASC
  150. * @author Frederic Vauthier
  151. */
  152. public function readOrderedBy($field, $order='ASC')
  153. {
  154. $field = Database::escape_string($field);
  155. if (empty($field)) {
  156. $field = 'position';
  157. }
  158. if ($order != 'ASC' && $order!='DESC') {
  159. $order = 'ASC';
  160. }
  161. $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
  162. $TBL_QUIZ = Database::get_course_table(TABLE_QUIZ_QUESTION);
  163. $questionId=intval($this->questionId);
  164. $sql = "SELECT type FROM $TBL_QUIZ WHERE c_id = {$this->course_id} AND id = $questionId";
  165. $result_question = Database::query($sql);
  166. $question_type = Database::fetch_array($result_question);
  167. $sql = "SELECT answer,correct,comment,ponderation,position, hotspot_coordinates, hotspot_type, destination, id_auto
  168. FROM $TBL_ANSWER WHERE c_id = {$this->course_id} AND question_id='".$questionId."'
  169. ORDER BY $field $order";
  170. $result=Database::query($sql);
  171. $i = 1;
  172. // while a record is found
  173. $doubt_data = null;
  174. while ($object = Database::fetch_object($result)) {
  175. if ($question_type['type'] == UNIQUE_ANSWER_NO_OPTION && $object->position == 666) {
  176. $doubt_data = $object;
  177. continue;
  178. }
  179. $this->answer[$i] = $object->answer;
  180. $this->correct[$i] = $object->correct;
  181. $this->comment[$i] = $object->comment;
  182. $this->weighting[$i] = $object->ponderation;
  183. $this->position[$i] = $object->position;
  184. $this->destination[$i] = $object->destination;
  185. $this->autoId[$i] = $object->id_auto;
  186. $i++;
  187. }
  188. if ($question_type['type'] == UNIQUE_ANSWER_NO_OPTION && !empty($doubt_data)) {
  189. $this->answer[$i] = $doubt_data->answer;
  190. $this->correct[$i] = $doubt_data->correct;
  191. $this->comment[$i] = $doubt_data->comment;
  192. $this->weighting[$i] = $doubt_data->ponderation;
  193. $this->position[$i] = $doubt_data->position;
  194. $this->destination[$i] = $doubt_data->destination;
  195. $this->autoId[$i] = $doubt_data->id_auto;
  196. $i++;
  197. }
  198. $this->nbrAnswers = $i-1;
  199. }
  200. /**
  201. * returns the autoincrement id identificator
  202. *
  203. * @author Juan Carlos Ra�a
  204. * @return integer - answer num
  205. */
  206. public function selectAutoId($id)
  207. {
  208. return isset($this->autoId[$id]) ? $this->autoId[$id] : null;
  209. }
  210. /**
  211. * returns the number of answers in this question
  212. *
  213. * @author Olivier Brouckaert
  214. * @return integer - number of answers
  215. */
  216. public function selectNbrAnswers()
  217. {
  218. return $this->nbrAnswers;
  219. }
  220. /**
  221. * returns the question ID which the answers belong to
  222. *
  223. * @author Olivier Brouckaert
  224. * @return integer - the question ID
  225. */
  226. public function selectQuestionId()
  227. {
  228. return $this->questionId;
  229. }
  230. /**
  231. * returns the question ID of the destination question
  232. *
  233. * @author Julio Montoya
  234. * @return integer - the question ID
  235. */
  236. public function selectDestination($id)
  237. {
  238. return isset($this->destination[$id]) ? $this->destination[$id] : null;
  239. }
  240. /**
  241. * returns the answer title
  242. *
  243. * @author Olivier Brouckaert
  244. * @param - integer $id - answer ID
  245. * @return string - answer title
  246. */
  247. public function selectAnswer($id)
  248. {
  249. return isset($this->answer[$id]) ? $this->answer[$id] : null;
  250. }
  251. /**
  252. * return array answer by id else return a bool
  253. */
  254. public function selectAnswerByAutoId($auto_id)
  255. {
  256. $TBL_ANSWER = Database::get_course_table(TABLE_QUIZ_ANSWER);
  257. $auto_id = intval($auto_id);
  258. $sql = "SELECT id, answer, id_auto FROM $TBL_ANSWER
  259. WHERE c_id = {$this->course_id} AND id_auto='$auto_id'";
  260. $rs = Database::query($sql);
  261. if (Database::num_rows($rs) > 0) {
  262. $row = Database::fetch_array($rs);
  263. return $row;
  264. }
  265. return false;
  266. }
  267. /**
  268. * returns the answer title from an answer's position
  269. *
  270. * @author Yannick Warnier
  271. * @param - integer $id - answer ID
  272. * @return bool - answer title
  273. */
  274. public function selectAnswerIdByPosition($pos)
  275. {
  276. foreach ($this->position as $k => $v) {
  277. if ($v != $pos) {
  278. continue;
  279. }
  280. return $k;
  281. }
  282. return false;
  283. }
  284. /**
  285. * Returns a list of answers
  286. * @author Yannick Warnier <ywarnier@beeznest.org>
  287. * @return array List of answers where each answer is an array
  288. * of (id, answer, comment, grade) and grade=weighting
  289. */
  290. public function getAnswersList($decode = false)
  291. {
  292. $list = array();
  293. for ($i = 1; $i <= $this->nbrAnswers; $i++) {
  294. if (!empty($this->answer[$i])) {
  295. //Avoid problems when parsing elements with accents
  296. if ($decode) {
  297. $this->answer[$i] = api_html_entity_decode($this->answer[$i], ENT_QUOTES, api_get_system_encoding());
  298. $this->comment[$i] = api_html_entity_decode($this->comment[$i], ENT_QUOTES, api_get_system_encoding());
  299. }
  300. $list[] = array(
  301. 'id' => $i,
  302. 'answer' => $this->answer[$i],
  303. 'comment' => $this->comment[$i],
  304. 'grade' => $this->weighting[$i],
  305. 'hotspot_coord' => $this->hotspot_coordinates[$i],
  306. 'hotspot_type' => $this->hotspot_type[$i],
  307. 'correct' => $this->correct[$i],
  308. 'destination' => $this->destination[$i]
  309. );
  310. }
  311. }
  312. return $list;
  313. }
  314. /**
  315. * Returns a list of grades
  316. * @author Yannick Warnier <ywarnier@beeznest.org>
  317. * @return array List of grades where grade=weighting (?)
  318. */
  319. public function getGradesList()
  320. {
  321. $list = array();
  322. for ($i = 0; $i<$this->nbrAnswers;$i++){
  323. if(!empty($this->answer[$i])){
  324. $list[$i] = $this->weighting[$i];
  325. }
  326. }
  327. return $list;
  328. }
  329. /**
  330. * Returns the question type
  331. * @author Yannick Warnier <ywarnier@beeznest.org>
  332. * @return integer The type of the question this answer is bound to
  333. */
  334. public function getQuestionType()
  335. {
  336. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  337. $sql = "SELECT type FROM $TBL_QUESTIONS
  338. WHERE c_id = {$this->course_id} AND id = '".$this->questionId."'";
  339. $res = Database::query($sql);
  340. if (Database::num_rows($res)<=0){
  341. return null;
  342. }
  343. $row = Database::fetch_array($res);
  344. return $row['type'];
  345. }
  346. /**
  347. * tells if answer is correct or not
  348. *
  349. * @author Olivier Brouckaert
  350. * @param - integer $id - answer ID
  351. * @return integer - 0 if bad answer, not 0 if good answer
  352. */
  353. public function isCorrect($id)
  354. {
  355. return isset($this->correct[$id]) ? $this->correct[$id] : null;
  356. }
  357. /**
  358. * returns answer comment
  359. *
  360. * @author Olivier Brouckaert
  361. * @param - integer $id - answer ID
  362. * @return string - answer comment
  363. */
  364. public function selectComment($id)
  365. {
  366. return isset($this->comment[$id]) ? $this->comment[$id] : null;
  367. }
  368. /**
  369. * returns answer weighting
  370. *
  371. * @author Olivier Brouckaert
  372. * @param - integer $id - answer ID
  373. * @return integer - answer weighting
  374. */
  375. public function selectWeighting($id)
  376. {
  377. return isset($this->weighting[$id]) ? $this->weighting[$id] : null;
  378. }
  379. /**
  380. * returns answer position
  381. *
  382. * @author Olivier Brouckaert
  383. * @param - integer $id - answer ID
  384. * @return integer - answer position
  385. */
  386. function selectPosition($id)
  387. {
  388. return isset($this->position[$id]) ? $this->position[$id] : null;
  389. }
  390. /**
  391. * returns answer hotspot coordinates
  392. *
  393. * @author Olivier Brouckaert
  394. * @param integer Answer ID
  395. * @return integer Answer position
  396. */
  397. public function selectHotspotCoordinates($id)
  398. {
  399. return isset($this->hotspot_coordinates[$id]) ? $this->hotspot_coordinates[$id] : null;
  400. }
  401. /**
  402. * returns answer hotspot type
  403. *
  404. * @author Toon Keppens
  405. * @param integer Answer ID
  406. * @return integer Answer position
  407. */
  408. public function selectHotspotType($id)
  409. {
  410. return isset($this->hotspot_type[$id]) ? $this->hotspot_type[$id] : null;
  411. }
  412. /**
  413. * Creates a new answer
  414. *
  415. * @author Olivier Brouckaert
  416. * @param string $answer answer title
  417. * @param integer $correct 0 if bad answer, not 0 if good answer
  418. * @param string $comment answer comment
  419. * @param integer $weighting answer weighting
  420. * @param integer $position answer position
  421. * @param array $new_hotspot_coordinates Coordinates for hotspot exercises (optional)
  422. * @param integer $new_hotspot_type Type for hotspot exercises (optional)
  423. * @param string $destination
  424. */
  425. public function createAnswer(
  426. $answer,
  427. $correct,
  428. $comment,
  429. $weighting,
  430. $position,
  431. $new_hotspot_coordinates = null,
  432. $new_hotspot_type = null,
  433. $destination = ''
  434. ) {
  435. $this->new_nbrAnswers++;
  436. $id = $this->new_nbrAnswers;
  437. $this->new_answer[$id] = $answer;
  438. $this->new_correct[$id] = $correct;
  439. $this->new_comment[$id] = $comment;
  440. $this->new_weighting[$id] = $weighting;
  441. $this->new_position[$id] = $position;
  442. $this->new_hotspot_coordinates[$id] = $new_hotspot_coordinates;
  443. $this->new_hotspot_type[$id] = $new_hotspot_type;
  444. $this->new_destination[$id] = $destination;
  445. }
  446. /**
  447. * Updates an answer
  448. *
  449. * @author Toon Keppens
  450. *
  451. * @param string $answer
  452. * @param string $comment
  453. * @param string $correct
  454. * @param string $weighting
  455. * @param string $position
  456. * @param string $destination
  457. * @param string $hotspot_coordinates
  458. * @param string $hotspot_type
  459. */
  460. public function updateAnswers(
  461. $autoId,
  462. $answer,
  463. $comment,
  464. $correct,
  465. $weighting,
  466. $position,
  467. $destination,
  468. $hotspot_coordinates,
  469. $hotspot_type
  470. ) {
  471. $answerTable = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  472. $autoId = intval($autoId);
  473. $params = [
  474. 'answer' => $answer,
  475. 'comment' => $comment,
  476. 'correct' => $correct,
  477. 'ponderation' => $weighting,
  478. 'position' => $position,
  479. 'destination' => $destination,
  480. 'hotspot_coordinates' => $hotspot_coordinates,
  481. 'hotspot_type' => $hotspot_type,
  482. ];
  483. Database::update($answerTable, $params, ['id_auto= ?' => $autoId]);
  484. }
  485. /**
  486. * Records answers into the data base
  487. *
  488. * @author Olivier Brouckaert
  489. */
  490. public function save()
  491. {
  492. $answerTable = Database::get_course_table(TABLE_QUIZ_ANSWER);
  493. $questionId = intval($this->questionId);
  494. $c_id = $this->course['real_id'];
  495. $correctList = [];
  496. $answerList = [];
  497. for ($i=1; $i <= $this->new_nbrAnswers; $i++) {
  498. $answer = $this->new_answer[$i];
  499. $correct = $this->new_correct[$i];
  500. $comment = $this->new_comment[$i];
  501. $weighting = $this->new_weighting[$i];
  502. $position = $this->new_position[$i];
  503. $hotspot_coordinates = $this->new_hotspot_coordinates[$i];
  504. $hotspot_type = $this->new_hotspot_type[$i];
  505. $destination = $this->new_destination[$i];
  506. $autoId = $this->selectAutoId($i);
  507. if (!(isset($this->position[$i]))) {
  508. $params = [
  509. 'c_id' => $c_id,
  510. 'question_id' => $questionId,
  511. 'answer' => $answer,
  512. 'correct' => $correct,
  513. 'comment' => $comment,
  514. 'ponderation' => $weighting,
  515. 'position' => $position,
  516. 'hotspot_coordinates' => $hotspot_coordinates,
  517. 'hotspot_type' => $hotspot_type,
  518. 'destination' => $destination
  519. ];
  520. $autoId = Database::insert($answerTable, $params);
  521. if ($autoId) {
  522. $sql = "UPDATE $answerTable SET id = iid, id_auto = iid WHERE iid = $autoId";
  523. Database::query($sql);
  524. }
  525. } else {
  526. // https://support.chamilo.org/issues/6558
  527. // function updateAnswers already escape_string, error if we do it twice.
  528. // Feed function updateAnswers with none escaped strings
  529. $this->updateAnswers(
  530. $autoId,
  531. $this->new_answer[$i],
  532. $this->new_comment[$i],
  533. $this->new_correct[$i],
  534. $this->new_weighting[$i],
  535. $this->new_position[$i],
  536. $this->new_destination[$i],
  537. $this->new_hotspot_coordinates[$i],
  538. $this->new_hotspot_type[$i]
  539. );
  540. }
  541. $answerList[$i] = $autoId;
  542. if ($correct) {
  543. $correctList[$autoId] = array('id' => $i, 'correct' => $correct);
  544. }
  545. }
  546. if (!empty($correctList)) {
  547. foreach ($correctList as $autoId => $data) {
  548. $correct = $data['correct'];
  549. if (isset($answerList[$correct])) {
  550. $correct = $answerList[$correct];
  551. }
  552. $sql = "UPDATE $answerTable
  553. SET correct = $correct
  554. WHERE
  555. id_auto = $autoId
  556. ";
  557. Database::query($sql);
  558. }
  559. }
  560. if (count($this->position) > $this->new_nbrAnswers) {
  561. $i = $this->new_nbrAnswers + 1;
  562. while ($this->position[$i]) {
  563. $position = $this->position[$i];
  564. $sql = "DELETE FROM $answerTable
  565. WHERE
  566. c_id = {$this->course_id} AND
  567. question_id = '".$questionId."' AND
  568. position ='$position'";
  569. Database::query($sql);
  570. $i++;
  571. }
  572. }
  573. // moves $new_* arrays
  574. $this->answer = $this->new_answer;
  575. $this->correct = $this->new_correct;
  576. $this->comment = $this->new_comment;
  577. $this->weighting = $this->new_weighting;
  578. $this->position = $this->new_position;
  579. $this->hotspot_coordinates = $this->new_hotspot_coordinates;
  580. $this->hotspot_type = $this->new_hotspot_type;
  581. $this->nbrAnswers = $this->new_nbrAnswers;
  582. $this->destination = $this->new_destination;
  583. // clears $new_* arrays
  584. $this->cancel();
  585. }
  586. /**
  587. * Duplicates answers by copying them into another question
  588. *
  589. * @author Olivier Brouckaert
  590. * @param int question id
  591. * @param array destination course info (result of the function api_get_course_info() )
  592. */
  593. public function duplicate($newQuestionId, $course_info = null)
  594. {
  595. if (empty($course_info)) {
  596. $course_info = $this->course;
  597. }
  598. $TBL_REPONSES = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  599. $fixed_list = array();
  600. if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
  601. self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE
  602. ) {
  603. // Selecting origin options
  604. $origin_options = Question::readQuestionOption($this->selectQuestionId(), $this->course['real_id']);
  605. if (!empty($origin_options)) {
  606. foreach ($origin_options as $item) {
  607. $new_option_list[] = $item['id'];
  608. }
  609. }
  610. $destination_options = Question::readQuestionOption($newQuestionId, $course_info['real_id']);
  611. $i = 0;
  612. if (!empty($destination_options)) {
  613. foreach($destination_options as $item) {
  614. $fixed_list[$new_option_list[$i]] = $item['id'];
  615. $i++;
  616. }
  617. }
  618. }
  619. // if at least one answer
  620. if ($this->nbrAnswers) {
  621. // inserts new answers into data base
  622. $c_id = $course_info['real_id'];
  623. for ($i=1;$i <= $this->nbrAnswers;$i++) {
  624. if ($this->course['id'] != $course_info['id']) {
  625. $this->answer[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  626. $this->answer[$i],
  627. $this->course['id'],
  628. $course_info['id']
  629. );
  630. $this->comment[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  631. $this->comment[$i],
  632. $this->course['id'],
  633. $course_info['id']
  634. );
  635. }
  636. $answer = $this->answer[$i];
  637. $correct = $this->correct[$i];
  638. if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
  639. self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE
  640. ) {
  641. $correct = $fixed_list[intval($correct)];
  642. }
  643. $comment = $this->comment[$i];
  644. $weighting = $this->weighting[$i];
  645. $position = $this->position[$i];
  646. $hotspot_coordinates = $this->hotspot_coordinates[$i];
  647. $hotspot_type = $this->hotspot_type[$i];
  648. $destination = $this->destination[$i];
  649. $params = [
  650. 'c_id' => $c_id,
  651. 'question_id' =>$newQuestionId,
  652. 'answer' => $answer,
  653. 'correct' => $correct,
  654. 'comment' => $comment,
  655. 'ponderation' => $weighting,
  656. 'position' => $position,
  657. 'hotspot_coordinates' => $hotspot_coordinates,
  658. 'hotspot_type' => $hotspot_type,
  659. 'destination' => $destination
  660. ];
  661. $id = Database::insert($TBL_REPONSES, $params);
  662. if ($id) {
  663. $sql = "UPDATE $TBL_REPONSES SET id = id_auto WHERE id_auto = $id";
  664. Database::query($sql);
  665. }
  666. }
  667. }
  668. }
  669. /**
  670. * Get the necessary JavaScript for some answers
  671. * @return string
  672. */
  673. public function getJs() {
  674. //if ($this->questionId == 2)
  675. return "<script>
  676. jsPlumb.ready(function() {
  677. if ($('#drag{$this->questionId}_question').length > 0) {
  678. MatchingDraggable.init('{$this->questionId}');
  679. }
  680. });
  681. </script>";
  682. }
  683. }