answer.class.php 26 KB

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