answer.class.php 26 KB

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