answer.class.php 25 KB

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