answer.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CQuizAnswer;
  4. /**
  5. * Class Answer
  6. * Allows to instantiate an object of type Answer
  7. * 5 arrays are created to receive the attributes of each answer belonging to a specified question
  8. * @package chamilo.exercise
  9. *
  10. * @author Olivier Brouckaert
  11. */
  12. class Answer
  13. {
  14. public $questionId;
  15. // these are arrays
  16. public $answer;
  17. public $correct;
  18. public $comment;
  19. public $weighting;
  20. public $position;
  21. public $hotspot_coordinates;
  22. public $hotspot_type;
  23. public $destination;
  24. // these arrays are used to save temporarily new answers
  25. // then they are moved into the arrays above or deleted in the event of cancellation
  26. public $new_answer;
  27. public $new_correct;
  28. public $new_comment;
  29. public $new_weighting;
  30. public $new_position;
  31. public $new_hotspot_coordinates;
  32. public $new_hotspot_type;
  33. public $autoId;
  34. public $nbrAnswers;
  35. public $new_nbrAnswers;
  36. public $new_destination; // id of the next question if feedback option is set to Directfeedback
  37. public $course; //Course information
  38. public $iid;
  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] = $object->hotspot_coordinates;
  240. $this->hotspot_type[$i] = $object->hotspot_type;
  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. $answerTable = Database :: get_course_table(TABLE_QUIZ_ANSWER);
  525. $params = [
  526. 'answer' => $answer,
  527. 'comment' => $comment,
  528. 'correct' => intval($correct),
  529. 'ponderation' => $weighting,
  530. 'position' => $position,
  531. 'destination' => $destination,
  532. 'hotspot_coordinates' => $hotspot_coordinates,
  533. 'hotspot_type' => $hotspot_type
  534. ];
  535. Database::update($answerTable, $params, ['iid = ?' => intval($iid)]);
  536. }
  537. /**
  538. * Records answers into the data base
  539. *
  540. * @author Olivier Brouckaert
  541. */
  542. public function save()
  543. {
  544. $answerTable = Database::get_course_table(TABLE_QUIZ_ANSWER);
  545. $questionId = intval($this->questionId);
  546. $c_id = $this->course['real_id'];
  547. $correctList = [];
  548. $answerList = [];
  549. for ($i=1; $i <= $this->new_nbrAnswers; $i++) {
  550. $answer = $this->new_answer[$i];
  551. $correct = isset($this->new_correct[$i]) ? $this->new_correct[$i] : '';
  552. $comment = isset($this->new_comment[$i]) ? $this->new_comment[$i] : '';
  553. $weighting = isset($this->new_weighting[$i]) ? $this->new_weighting[$i] : '';
  554. $position = isset($this->new_position[$i]) ? $this->new_position[$i] : '';
  555. $hotspot_coordinates = isset($this->new_hotspot_coordinates[$i]) ? $this->new_hotspot_coordinates[$i] : '';
  556. $hotspot_type = isset($this->new_hotspot_type[$i]) ? $this->new_hotspot_type[$i] : '';
  557. $destination = isset($this->new_destination[$i]) ? $this->new_destination[$i] : '';
  558. $autoId = $this->selectAutoId($i);
  559. $iid = isset($this->iid[$i]) ? $this->iid[$i] : 0;
  560. if (!isset($this->position[$i])) {
  561. $params = [
  562. 'id_auto' => $autoId,
  563. 'c_id' => $c_id,
  564. 'question_id' => $questionId,
  565. 'answer' => $answer,
  566. 'correct' => intval($correct),
  567. 'comment' => $comment,
  568. 'ponderation' => $weighting,
  569. 'position' => $position,
  570. 'hotspot_coordinates' => $hotspot_coordinates,
  571. 'hotspot_type' => $hotspot_type,
  572. 'destination' => $destination
  573. ];
  574. $iid = Database::insert($answerTable, $params);
  575. if ($iid) {
  576. $sql = "UPDATE $answerTable SET id = iid, id_auto = iid WHERE iid = $iid";
  577. Database::query($sql);
  578. $questionType = $this->getQuestionType();
  579. if (in_array(
  580. $questionType,
  581. [MATCHING, MATCHING_DRAGGABLE]
  582. )) {
  583. $answer = new Answer($this->questionId);
  584. $answer->read();
  585. $correctAnswerId = $answer->selectAnswerIdByPosition($correct);
  586. $correctAnswerAutoId = $answer->selectAutoId($correctAnswerId);
  587. Database::update(
  588. $answerTable,
  589. ['correct' => $correctAnswerAutoId ? $correctAnswerAutoId : 0],
  590. ['iid = ?' => $iid]
  591. );
  592. }
  593. }
  594. } else {
  595. // https://support.chamilo.org/issues/6558
  596. // function updateAnswers already escape_string, error if we do it twice.
  597. // Feed function updateAnswers with none escaped strings
  598. $this->updateAnswers(
  599. $iid,
  600. $this->new_answer[$i],
  601. $this->new_comment[$i],
  602. $this->new_correct[$i],
  603. $this->new_weighting[$i],
  604. $this->new_position[$i],
  605. $this->new_destination[$i],
  606. $this->new_hotspot_coordinates[$i],
  607. $this->new_hotspot_type[$i]
  608. );
  609. }
  610. $answerList[$i] = $iid;
  611. if ($correct) {
  612. $correctList[$iid] = true;
  613. }
  614. }
  615. $questionType = self::getQuestionType();
  616. if ($questionType == DRAGGABLE) {
  617. foreach ($this->new_correct as $value => $status) {
  618. if (!empty($status)) {
  619. $correct = $answerList[$status];
  620. $myAutoId = $answerList[$value];
  621. $sql = "UPDATE $answerTable
  622. SET correct = '$correct'
  623. WHERE
  624. id_auto = $myAutoId
  625. ";
  626. Database::query($sql);
  627. }
  628. }
  629. }
  630. if (count($this->position) > $this->new_nbrAnswers) {
  631. $i = $this->new_nbrAnswers + 1;
  632. while ($this->position[$i]) {
  633. $position = $this->position[$i];
  634. $sql = "DELETE FROM $answerTable
  635. WHERE
  636. c_id = {$this->course_id} AND
  637. question_id = '".$questionId."' AND
  638. position ='$position'";
  639. Database::query($sql);
  640. $i++;
  641. }
  642. }
  643. // moves $new_* arrays
  644. $this->answer = $this->new_answer;
  645. $this->correct = $this->new_correct;
  646. $this->comment = $this->new_comment;
  647. $this->weighting = $this->new_weighting;
  648. $this->position = $this->new_position;
  649. $this->hotspot_coordinates = $this->new_hotspot_coordinates;
  650. $this->hotspot_type = $this->new_hotspot_type;
  651. $this->nbrAnswers = $this->new_nbrAnswers;
  652. $this->destination = $this->new_destination;
  653. $this->cancel();
  654. }
  655. /**
  656. * Duplicates answers by copying them into another question
  657. *
  658. * @author Olivier Brouckaert
  659. * @param Question $newQuestion
  660. * @param array $course_info destination course info (result of the function api_get_course_info() )
  661. * @param string $newQuestionId
  662. */
  663. public function duplicate($newQuestion, $course_info = null)
  664. {
  665. $newQuestionId = $newQuestion->id;
  666. if (empty($course_info)) {
  667. $course_info = $this->course;
  668. }
  669. $fixed_list = array();
  670. $tableAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
  671. if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
  672. self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE
  673. ) {
  674. // Selecting origin options
  675. $origin_options = Question::readQuestionOption(
  676. $this->selectQuestionId(),
  677. $this->course['real_id']
  678. );
  679. if (!empty($origin_options)) {
  680. foreach ($origin_options as $item) {
  681. $new_option_list[] = $item['id'];
  682. }
  683. }
  684. $destination_options = Question::readQuestionOption(
  685. $newQuestionId,
  686. $course_info['real_id']
  687. );
  688. $i = 0;
  689. if (!empty($destination_options)) {
  690. foreach($destination_options as $item) {
  691. $fixed_list[$new_option_list[$i]] = $item['id'];
  692. $i++;
  693. }
  694. }
  695. }
  696. // if at least one answer
  697. if ($this->nbrAnswers) {
  698. // inserts new answers into data base
  699. $courseId = $course_info['real_id'];
  700. $correctAnswers = [];
  701. $onlyAnswers = [];
  702. $allAnswers = [];
  703. $em = Database::getManager();
  704. if (in_array($newQuestion->type, [MATCHING, MATCHING_DRAGGABLE])) {
  705. $temp = array();
  706. for ($i = 1; $i <= $this->nbrAnswers; $i++) {
  707. $answer = [
  708. 'id' => $this->id[$i],
  709. 'answer' => $this->answer[$i],
  710. 'correct' => $this->correct[$i],
  711. 'comment' => $this->comment[$i],
  712. 'weighting' => $this->weighting[$i],
  713. 'ponderation' => $this->weighting[$i],
  714. 'position' => $this->position[$i],
  715. 'hotspot_coordinates' => $this->hotspot_coordinates[$i],
  716. 'hotspot_type' => $this->hotspot_type[$i],
  717. 'destination' => $this->destination[$i],
  718. ];
  719. $temp[$answer['position']] = $answer;
  720. $allAnswers[$this->id[$i]] = $this->answer[$i];
  721. }
  722. foreach ($temp as $index => $answer) {
  723. if ($this->course['id'] != $course_info['id']) {
  724. // check resources inside html from ckeditor tool and copy correct urls into recipient course
  725. $answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  726. $answer['answer'],
  727. $this->course['id'],
  728. $course_info['id']
  729. );
  730. $answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  731. $answer['comment'],
  732. $this->course['id'],
  733. $course_info['id']
  734. );
  735. }
  736. $quizAnswer = new CQuizAnswer();
  737. $quizAnswer
  738. ->setCId($courseId)
  739. ->setQuestionId($newQuestionId)
  740. ->setAnswer($answer['answer'])
  741. ->setCorrect($answer['correct'])
  742. ->setComment($answer['comment'])
  743. ->setPonderation($answer['ponderation'])
  744. ->setPosition($answer['position'])
  745. ->setHotspotCoordinates($answer['hotspot_coordinates'])
  746. ->setHotspotType($answer['hotspot_type'])
  747. ->setIdAuto(0);
  748. $em->persist($quizAnswer);
  749. $em->flush();
  750. $answerId = $quizAnswer->getIid();
  751. if ($answerId) {
  752. $quizAnswer
  753. ->setId($answerId)
  754. ->setIdAuto($answerId);
  755. $em->merge($quizAnswer);
  756. $em->flush();
  757. $correctAnswers[$answerId] = $answer['correct'];
  758. $onlyAnswers[$answerId] = $answer['answer'];
  759. }
  760. }
  761. } else {
  762. for ($i = 1; $i <= $this->nbrAnswers; $i++) {
  763. if ($this->course['id'] != $course_info['id']) {
  764. $this->answer[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  765. $this->answer[$i],
  766. $this->course['id'],
  767. $course_info['id']
  768. );
  769. $this->comment[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  770. $this->comment[$i],
  771. $this->course['id'],
  772. $course_info['id']
  773. );
  774. }
  775. $correct = $this->correct[$i];
  776. if ($newQuestion->type == MULTIPLE_ANSWER_TRUE_FALSE ||
  777. $newQuestion->type == MULTIPLE_ANSWER_TRUE_FALSE
  778. ) {
  779. $correct = $fixed_list[intval($correct)];
  780. }
  781. $quizAnswer = new CQuizAnswer();
  782. $quizAnswer
  783. ->setCId($courseId)
  784. ->setQuestionId($newQuestionId)
  785. ->setAnswer($this->answer[$i])
  786. ->setCorrect($correct)
  787. ->setComment($this->comment[$i])
  788. ->setPonderation($this->weighting[$i])
  789. ->setPosition($this->position[$i])
  790. ->setHotspotCoordinates($this->hotspot_coordinates[$i])
  791. ->setHotspotType($this->hotspot_type[$i])
  792. ->setDestination($this->destination[$i]);
  793. $em->persist($quizAnswer);
  794. $em->flush();
  795. $answerId = $quizAnswer->getIid();
  796. $quizAnswer
  797. ->setId($answerId)
  798. ->setIdAuto($answerId);
  799. $em->merge($quizAnswer);
  800. $em->flush();
  801. $correctAnswers[$answerId] = $correct;
  802. $onlyAnswers[$answerId] = $this->answer[$i];
  803. $allAnswers[$this->id[$i]] = $this->answer[$i];
  804. }
  805. }
  806. // Fix correct answers
  807. if (in_array($newQuestion->type, [DRAGGABLE, MATCHING, MATCHING_DRAGGABLE])) {
  808. $onlyAnswersFlip = array_flip($onlyAnswers);
  809. foreach ($correctAnswers as $answer_id => $correct_answer) {
  810. $params = array();
  811. if (isset($allAnswers[$correct_answer]) &&
  812. isset($onlyAnswersFlip[$allAnswers[$correct_answer]])
  813. ) {
  814. $params['correct'] = $onlyAnswersFlip[$allAnswers[$correct_answer]];
  815. Database::update(
  816. $tableAnswer,
  817. $params,
  818. array(
  819. 'id = ? AND c_id = ? AND question_id = ? ' => array(
  820. $answer_id,
  821. $courseId,
  822. $newQuestionId,
  823. ),
  824. )
  825. );
  826. }
  827. }
  828. }
  829. }
  830. }
  831. /**
  832. * Get the necessary JavaScript for some answers
  833. * @return string
  834. */
  835. public function getJs()
  836. {
  837. //if ($this->questionId == 2)
  838. return "<script>
  839. jsPlumb.ready(function() {
  840. if ($('#drag{$this->questionId}_question').length > 0) {
  841. MatchingDraggable.init('{$this->questionId}');
  842. }
  843. });
  844. </script>";
  845. }
  846. }