Fix matching drag question edition see #2072

pull/2487/head
jmontoyaa 7 years ago
parent 2cd1e948b2
commit f5e0998ca7
  1. 64
      main/exercise/answer.class.php

@ -50,7 +50,7 @@ class Answer
* @param int $course_id
* @param Exercise $exercise
*/
public function __construct($questionId, $course_id = null, $exercise = null)
public function __construct($questionId, $course_id = 0, $exercise = null)
{
$this->questionId = intval($questionId);
$this->answer = array();
@ -582,8 +582,10 @@ class Answer
* @param string $weighting
* @param string $position
* @param string $destination
* @param string $hotspot_coordinates
* @param string $hotspot_type
* @param string $hotSpotCoordinates
* @param string $hotSpotType
*
* @return CQuizAnswer
*/
public function updateAnswers(
$iid,
@ -593,13 +595,14 @@ class Answer
$weighting,
$position,
$destination,
$hotspot_coordinates,
$hotspot_type
$hotSpotCoordinates,
$hotSpotType
) {
$em = Database::getManager();
/** @var CQuizAnswer $quizAnswer */
$quizAnswer = $em->find('ChamiloCourseBundle:CQuizAnswer', $iid);
if ($quizAnswer) {
$quizAnswer
->setAnswer($answer)
->setComment($comment)
@ -607,11 +610,16 @@ class Answer
->setPonderation($weighting)
->setPosition($position)
->setDestination($destination)
->setHotspotCoordinates($hotspot_coordinates)
->setHotspotType($hotspot_type);
->setHotspotCoordinates($hotSpotCoordinates)
->setHotspotType($hotSpotType);
$em->merge($quizAnswer);
$em->flush();
return $quizAnswer;
}
return false;
}
/**
@ -623,9 +631,9 @@ class Answer
{
$answerTable = Database::get_course_table(TABLE_QUIZ_ANSWER);
$em = Database::getManager();
$questionId = intval($this->questionId);
$questionId = (int) $this->questionId;
$c_id = $this->course['real_id'];
$courseId = $this->course['real_id'];
$correctList = [];
$answerList = [];
@ -638,14 +646,16 @@ class Answer
$hotspot_coordinates = isset($this->new_hotspot_coordinates[$i]) ? $this->new_hotspot_coordinates[$i] : '';
$hotspot_type = isset($this->new_hotspot_type[$i]) ? $this->new_hotspot_type[$i] : '';
$destination = isset($this->new_destination[$i]) ? $this->new_destination[$i] : '';
$autoId = $this->selectAutoId($i);
$iid = isset($this->iid[$i]) ? $this->iid[$i] : 0;
$questionType = $this->getQuestionType();
if (!isset($this->position[$i])) {
$quizAnswer = new CQuizAnswer();
$quizAnswer
->setIdAuto($autoId)
->setCId($c_id)
->setCId($courseId)
->setQuestionId($questionId)
->setAnswer($answer)
->setCorrect($correct)
@ -661,6 +671,7 @@ class Answer
$iid = $quizAnswer->getIid();
if ($iid) {
$quizAnswer
->setId($iid)
@ -669,12 +680,7 @@ class Answer
$em->merge($quizAnswer);
$em->flush();
$questionType = $this->getQuestionType();
if (in_array(
$questionType,
[MATCHING, MATCHING_DRAGGABLE]
)) {
if (in_array($questionType, [MATCHING, MATCHING_DRAGGABLE])) {
$answer = new Answer($this->questionId);
$answer->read();
$correctAnswerId = $answer->selectAnswerIdByPosition($correct);
@ -696,7 +702,6 @@ class Answer
// https://support.chamilo.org/issues/6558
// function updateAnswers already escape_string, error if we do it twice.
// Feed function updateAnswers with none escaped strings
$this->updateAnswers(
$iid,
$this->new_answer[$i],
@ -719,12 +724,30 @@ class Answer
$questionType = self::getQuestionType();
if ($questionType == DRAGGABLE) {
switch ($questionType) {
case MATCHING_DRAGGABLE:
foreach ($this->new_correct as $value => $status) {
if (!empty($status)) {
if (isset($answerList[$status])) {
$correct = $answerList[$status];
} else {
$correct = $status;
}
$myAutoId = $answerList[$value];
$sql = "UPDATE $answerTable
SET correct = '$correct'
WHERE
id_auto = $myAutoId
";
Database::query($sql);
}
}
break;
case DRAGGABLE:
foreach ($this->new_correct as $value => $status) {
if (!empty($status)) {
$correct = $answerList[$status];
$myAutoId = $answerList[$value];
$sql = "UPDATE $answerTable
SET correct = '$correct'
WHERE
@ -733,6 +756,7 @@ class Answer
Database::query($sql);
}
}
break;
}
if (count($this->position) > $this->new_nbrAnswers) {
@ -940,7 +964,7 @@ class Answer
if (in_array($newQuestion->type, [DRAGGABLE, MATCHING, MATCHING_DRAGGABLE])) {
$onlyAnswersFlip = array_flip($onlyAnswers);
foreach ($correctAnswers as $answer_id => $correct_answer) {
$params = array();
$params = [];
if (isset($allAnswers[$correct_answer]) &&
isset($onlyAnswersFlip[$allAnswers[$correct_answer]])
) {

Loading…
Cancel
Save