Use entities when inserting a new c_quiz_answer

pull/2487/head
Angel Fernando Quiroz Campos 9 years ago
parent 7d51b42830
commit c89d59afc7
  1. 126
      main/exercise/answer.class.php
  2. 76
      main/exercise/question.class.php
  3. 37
      main/exercise/unique_answer.class.php
  4. 41
      src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php
  5. 19
      src/Chamilo/CourseBundle/Entity/CQuizAnswer.php

@ -579,20 +579,22 @@ class Answer
$hotspot_coordinates,
$hotspot_type
) {
$answerTable = Database :: get_course_table(TABLE_QUIZ_ANSWER);
$em = Database::getManager();
$params = [
'answer' => $answer,
'comment' => $comment,
'correct' => intval($correct),
'ponderation' => $weighting,
'position' => $position,
'destination' => $destination,
'hotspot_coordinates' => $hotspot_coordinates,
'hotspot_type' => $hotspot_type
];
/** @var \Chamilo\CourseBundle\Entity\CQuizAnswer $quizAnswer */
$quizAnswer = $em->find('ChamiloCourseBundle:CQuizAnswer', $iid);
$quizAnswer
->setAnswer($answer)
->setComment($comment)
->setCorrect($correct)
->setPonderation($weighting)
->setPosition($position)
->setDestination($destination)
->setHotspotCoordinates($hotspot_coordinates)
->setHotspotType($hotspot_type);
Database::update($answerTable, $params, ['iid = ?' => intval($iid)]);
$em->merge($quizAnswer);
$em->flush();
}
/**
@ -603,6 +605,7 @@ class Answer
public function save()
{
$answerTable = Database::get_course_table(TABLE_QUIZ_ANSWER);
$em = Database::getManager();
$questionId = intval($this->questionId);
$c_id = $this->course['real_id'];
@ -622,23 +625,32 @@ class Answer
$iid = isset($this->iid[$i]) ? $this->iid[$i] : 0;
if (!isset($this->position[$i])) {
$params = [
'id_auto' => $autoId,
'c_id' => $c_id,
'question_id' => $questionId,
'answer' => $answer,
'correct' => intval($correct),
'comment' => $comment,
'ponderation' => $weighting,
'position' => $position,
'hotspot_coordinates' => $hotspot_coordinates,
'hotspot_type' => $hotspot_type,
'destination' => $destination
];
$iid = Database::insert($answerTable, $params);
$quizAnswer = new \Chamilo\CourseBundle\Entity\CQuizAnswer();
$quizAnswer
->setIdAuto($autoId)
->setCId($c_id)
->setQuestionId($questionId)
->setAnswer($answer)
->setCorrect($correct)
->setComment($comment)
->setPonderation($weighting)
->setPosition($position)
->setHotspotCoordinates($hotspot_coordinates)
->setHotspotType($hotspot_type)
->setDestination($destination);
$em->persist($quizAnswer);
$em->flush();
$iid = $quizAnswer->getIid();
if ($iid) {
$sql = "UPDATE $answerTable SET id = iid, id_auto = iid WHERE iid = $iid";
Database::query($sql);
$quizAnswer
->setId($iid)
->setIdAuto($iid);
$em->merge($quizAnswer);
$em->flush();
$questionType = $this->getQuestionType();
@ -659,11 +671,10 @@ class Answer
$correctAnswerAutoId = $answer->selectAutoId($correct);
Database::update(
$answerTable,
['correct' => $correctAnswerAutoId ? $correctAnswerAutoId : 0],
['iid = ?' => $iid]
);
$quizAnswer->setCorrect($correctAnswerAutoId ? $correctAnswerAutoId : 0);
$em->merge($quizAnswer);
$em->flush();
}
}
} else {
@ -751,7 +762,6 @@ class Answer
$course_info = $this->course;
}
$TBL_REPONSES = Database :: get_course_table(TABLE_QUIZ_ANSWER);
$fixed_list = array();
if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
@ -798,7 +808,6 @@ class Answer
);
}
$answer = $this->answer[$i];
$correct = $this->correct[$i];
if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
@ -807,31 +816,30 @@ class Answer
$correct = $fixed_list[intval($correct)];
}
$comment = $this->comment[$i];
$weighting = $this->weighting[$i];
$position = $this->position[$i];
$hotspot_coordinates = $this->hotspot_coordinates[$i];
$hotspot_type = $this->hotspot_type[$i];
$destination = $this->destination[$i];
$params = [
'c_id' => $c_id,
'question_id' => $newQuestionId,
'answer' => $answer,
'correct' => $correct,
'comment' => $comment,
'ponderation' => $weighting,
'position' => $position,
'hotspot_coordinates' => $hotspot_coordinates,
'hotspot_type' => $hotspot_type,
'destination' => $destination
];
$id = Database::insert($TBL_REPONSES, $params);
$quizAnswer = new \Chamilo\CourseBundle\Entity\CQuizAnswer();
$quizAnswer
->setCId($c_id)
->setQuestionId($newQuestionId)
->setAnswer($this->answer[$i])
->setCorrect($correct)
->setComment($this->comment[$i])
->setPonderation($this->weighting[$i])
->setPosition($this->position[$i])
->setHotspotCoordinates($this->hotspot_coordinates[$i])
->setHotspotType($this->hotspot_type[$i])
->setDestination($this->destination[$i]);
if ($id) {
$sql = "UPDATE $TBL_REPONSES SET id = iid, id_auto = iid WHERE iid = $id";
Database::query($sql);
}
$em = Database::getManager();
$em->persist($quizAnswer);
$em->flush();
$quizAnswer
->setId($quizAnswer->getIid())
->setIdAuto($quizAnswer->getIid());
$em->merge($quizAnswer);
$em->flush();
}
}
}

@ -795,6 +795,7 @@ abstract class Question
{
$TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$em = Database::getManager();
$id = $this->id;
$question = $this->question;
@ -892,47 +893,54 @@ abstract class Question
// If hotspot, create first answer
if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) {
$TBL_ANSWERS = Database::get_course_table(
TABLE_QUIZ_ANSWER
);
$params = [
'c_id' => $c_id,
'question_id' => $this->id,
'answer' => '',
'correct' => '',
'comment' => '',
'ponderation' => 10,
'position' => 1,
'hotspot_coordinates' => '0;0|0|0',
'hotspot_type' => 'square',
];
$id = Database::insert($TBL_ANSWERS, $params);
$quizAnswer = new \Chamilo\CourseBundle\Entity\CQuizAnswer();
$quizAnswer
->setCId($c_id)
->setQuestionId($this->id)
->setAnswer('')
->setPonderation(10)
->setPosition(1)
->setHotspotCoordinates('0;0|0|0')
->setHotspotType('square');
$em->persist($quizAnswer);
$em->flush();
$id = $quizAnswer->getIid();
if ($id) {
$sql = "UPDATE $TBL_ANSWERS SET id = iid, id_auto = iid WHERE iid = $id";
Database::query($sql);
$quizAnswer
->setId($id)
->setIdAuto($id);
$em->merge($quizAnswer);
$em->flush();
}
}
if ($type == HOT_SPOT_DELINEATION) {
$TBL_ANSWERS = Database::get_course_table(
TABLE_QUIZ_ANSWER
);
$params = [
'c_id' => $c_id,
'question_id' => $this->id,
'answer' => '',
'correct' => '',
'comment' => '',
'ponderation' => 10,
'position' => 1,
'hotspot_coordinates' => '0;0|0|0',
'hotspot_type' => 'delineation',
];
$id = Database::insert($TBL_ANSWERS, $params);
$quizAnswer = new \Chamilo\CourseBundle\Entity\CQuizAnswer();
$quizAnswer
->setCId($c_id)
->setQuestionId($this->id)
->setAnswer('')
->setPonderation(10)
->setPosition(1)
->setHotspotCoordinates('0;0|0|0')
->setHotspotType('delineation');
$em->persist($quizAnswer);
$em->flush();
$id = $quizAnswer->getIid();
if ($id) {
$sql = "UPDATE $TBL_ANSWERS SET id = iid, id_auto = iid WHERE iid = $id";
Database::query($sql);
$quizAnswer
->setId($id)
->setIdAuto($id);
$em->merge($quizAnswer);
$em->flush();
}
}

@ -429,6 +429,7 @@ class UniqueAnswer extends Question
$score = 0.0,
$correct = 0
) {
$em = Database::getManager();
$tbl_quiz_answer = Database::get_course_table(TABLE_QUIZ_ANSWER);
$tbl_quiz_question = Database::get_course_table(TABLE_QUIZ_QUESTION);
$course_id = api_get_course_int_id();
@ -448,23 +449,29 @@ class UniqueAnswer extends Question
$position = $row_max->max_position + 1;
// Insert a new answer
$params = [
'c_id' => $course_id,
'id' => $id,
'question_id' => $question_id,
'answer' => $title,
'correct' => $correct,
'comment' => $comment,
'ponderation' => $score,
'position' => $position,
'destination' => '0@@0@@0@@0',
];
$id = Database::insert($tbl_quiz_answer, $params);
$quizAnswer = new \Chamilo\CourseBundle\Entity\CQuizAnswer();
$quizAnswer
->setCId($course_id)
->setId($id)
->setQuestionId($question_id)
->setAnswer($title)
->setCorrect($correct)
->setComment($comment)
->setPonderation($score)
->setPosition($position)
->setDestination('0@@0@@0@@0');
$em->persist($quizAnswer);
$em->flush();
$id = $quizAnswer->getIid();
if ($id) {
$sql = "UPDATE $tbl_quiz_answer SET id = iid WHERE iid = $id";
Database::query($sql);
$quizAnswer
->setId($id);
$em->merge($quizAnswer);
$em->flush();
}
if ($correct) {

@ -3,6 +3,7 @@
namespace Chamilo\CourseBundle\Component\CourseCopy;
use Chamilo\CourseBundle\Entity\CQuizAnswer;
use DocumentManager;
use Database;
use CourseManager;
@ -1791,6 +1792,7 @@ class CourseRestorer
*/
public function restore_quiz_question($id)
{
$em = Database::getManager();
$resources = $this->course->resources;
$question = isset($resources[RESOURCE_QUIZQUESTION][$id]) ? $resources[RESOURCE_QUIZQUESTION][$id] : null;
@ -1866,24 +1868,31 @@ class CourseRestorer
foreach ($temp as $index => $answer) {
//id = '".$index."',
$params = [
'c_id' => $this->destination_course_id,
'question_id' => $new_id,
'answer' => self::DBUTF8($answer['answer']),
'correct' => $answer['correct'],
'comment' => self::DBUTF8($answer['comment']),
'ponderation' => $answer['ponderation'],
'position' => $answer['position'],
'hotspot_coordinates' => $answer['hotspot_coordinates'],
'hotspot_type' => $answer['hotspot_type'],
'id_auto' => 0,
'destination' => ''
];
$answerId = Database::insert($table_ans, $params);
$quizAnswer = new CQuizAnswer();
$quizAnswer
->setCId($this->destination_course_id)
->setQuestionId($new_id)
->setAnswer(self::DBUTF8($answer['answer']))
->setCorrect($answer['correct'])
->setComment(self::DBUTF8($answer['comment']))
->setPonderation($answer['ponderation'])
->setPosition($answer['position'])
->setHotspotCoordinates($answer['hotspot_coordinates'])
->setHotspotType($answer['hotspot_type'])
->setIdAuto(0);
$em->persist($quizAnswer);
$em->flush();
$answerId = $quizAnswer->getIid();
if ($answerId) {
$sql = "UPDATE $table_ans SET id = iid, id_auto = iid WHERE iid = $answerId";
Database::query($sql);
$quizAnswer
->setId($answerId)
->setIdAuto($answerId);
$em->merge($quizAnswer);
$em->flush();
}
}
} else {

@ -121,8 +121,14 @@ class CQuizAnswer
public function __construct()
{
$this->id = null;
$this->correct = null;
$this->comment = null;
$this->ponderation = 0;
$this->hotspotCoordinates = null;
$this->hotspotType = null;
$this->destination = null;
$this->answerCode = null;
}
/**
@ -248,7 +254,7 @@ class CQuizAnswer
*/
public function setPonderation($ponderation)
{
$this->ponderation = $ponderation;
$this->ponderation = empty($ponderation) ? 0 : $ponderation;
return $this;
}
@ -340,7 +346,7 @@ class CQuizAnswer
*/
public function setDestination($destination)
{
$this->destination = $destination;
$this->destination = empty($destination) ? null : $destination;
return $this;
}
@ -423,4 +429,13 @@ class CQuizAnswer
{
return $this->cId;
}
/**
* Get iid
* @return int
*/
public function getIid()
{
return $this->iid;
}
}

Loading…
Cancel
Save