answer.class.php 24 KB

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