Use entities when inserting a new c_quiz_answer

pull/2487/head
Angel Fernando Quiroz Campos 9 years ago
parent 7d51b42830
commit c89d59afc7
  1. 128
      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_coordinates,
$hotspot_type $hotspot_type
) { ) {
$answerTable = Database :: get_course_table(TABLE_QUIZ_ANSWER); $em = Database::getManager();
$params = [ /** @var \Chamilo\CourseBundle\Entity\CQuizAnswer $quizAnswer */
'answer' => $answer, $quizAnswer = $em->find('ChamiloCourseBundle:CQuizAnswer', $iid);
'comment' => $comment, $quizAnswer
'correct' => intval($correct), ->setAnswer($answer)
'ponderation' => $weighting, ->setComment($comment)
'position' => $position, ->setCorrect($correct)
'destination' => $destination, ->setPonderation($weighting)
'hotspot_coordinates' => $hotspot_coordinates, ->setPosition($position)
'hotspot_type' => $hotspot_type ->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() public function save()
{ {
$answerTable = Database::get_course_table(TABLE_QUIZ_ANSWER); $answerTable = Database::get_course_table(TABLE_QUIZ_ANSWER);
$em = Database::getManager();
$questionId = intval($this->questionId); $questionId = intval($this->questionId);
$c_id = $this->course['real_id']; $c_id = $this->course['real_id'];
@ -622,23 +625,32 @@ class Answer
$iid = isset($this->iid[$i]) ? $this->iid[$i] : 0; $iid = isset($this->iid[$i]) ? $this->iid[$i] : 0;
if (!isset($this->position[$i])) { if (!isset($this->position[$i])) {
$params = [ $quizAnswer = new \Chamilo\CourseBundle\Entity\CQuizAnswer();
'id_auto' => $autoId, $quizAnswer
'c_id' => $c_id, ->setIdAuto($autoId)
'question_id' => $questionId, ->setCId($c_id)
'answer' => $answer, ->setQuestionId($questionId)
'correct' => intval($correct), ->setAnswer($answer)
'comment' => $comment, ->setCorrect($correct)
'ponderation' => $weighting, ->setComment($comment)
'position' => $position, ->setPonderation($weighting)
'hotspot_coordinates' => $hotspot_coordinates, ->setPosition($position)
'hotspot_type' => $hotspot_type, ->setHotspotCoordinates($hotspot_coordinates)
'destination' => $destination ->setHotspotType($hotspot_type)
]; ->setDestination($destination);
$iid = Database::insert($answerTable, $params);
$em->persist($quizAnswer);
$em->flush();
$iid = $quizAnswer->getIid();
if ($iid) { if ($iid) {
$sql = "UPDATE $answerTable SET id = iid, id_auto = iid WHERE iid = $iid"; $quizAnswer
Database::query($sql); ->setId($iid)
->setIdAuto($iid);
$em->merge($quizAnswer);
$em->flush();
$questionType = $this->getQuestionType(); $questionType = $this->getQuestionType();
@ -659,11 +671,10 @@ class Answer
$correctAnswerAutoId = $answer->selectAutoId($correct); $correctAnswerAutoId = $answer->selectAutoId($correct);
Database::update( $quizAnswer->setCorrect($correctAnswerAutoId ? $correctAnswerAutoId : 0);
$answerTable,
['correct' => $correctAnswerAutoId ? $correctAnswerAutoId : 0], $em->merge($quizAnswer);
['iid = ?' => $iid] $em->flush();
);
} }
} }
} else { } else {
@ -751,7 +762,6 @@ class Answer
$course_info = $this->course; $course_info = $this->course;
} }
$TBL_REPONSES = Database :: get_course_table(TABLE_QUIZ_ANSWER);
$fixed_list = array(); $fixed_list = array();
if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE || if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
@ -798,7 +808,6 @@ class Answer
); );
} }
$answer = $this->answer[$i];
$correct = $this->correct[$i]; $correct = $this->correct[$i];
if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE || if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
@ -807,31 +816,30 @@ class Answer
$correct = $fixed_list[intval($correct)]; $correct = $fixed_list[intval($correct)];
} }
$comment = $this->comment[$i]; $quizAnswer = new \Chamilo\CourseBundle\Entity\CQuizAnswer();
$weighting = $this->weighting[$i]; $quizAnswer
$position = $this->position[$i]; ->setCId($c_id)
$hotspot_coordinates = $this->hotspot_coordinates[$i]; ->setQuestionId($newQuestionId)
$hotspot_type = $this->hotspot_type[$i]; ->setAnswer($this->answer[$i])
$destination = $this->destination[$i]; ->setCorrect($correct)
->setComment($this->comment[$i])
$params = [ ->setPonderation($this->weighting[$i])
'c_id' => $c_id, ->setPosition($this->position[$i])
'question_id' => $newQuestionId, ->setHotspotCoordinates($this->hotspot_coordinates[$i])
'answer' => $answer, ->setHotspotType($this->hotspot_type[$i])
'correct' => $correct, ->setDestination($this->destination[$i]);
'comment' => $comment,
'ponderation' => $weighting,
'position' => $position,
'hotspot_coordinates' => $hotspot_coordinates,
'hotspot_type' => $hotspot_type,
'destination' => $destination
];
$id = Database::insert($TBL_REPONSES, $params);
if ($id) { $em = Database::getManager();
$sql = "UPDATE $TBL_REPONSES SET id = iid, id_auto = iid WHERE iid = $id";
Database::query($sql); $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_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION); $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
$em = Database::getManager();
$id = $this->id; $id = $this->id;
$question = $this->question; $question = $this->question;
@ -892,47 +893,54 @@ abstract class Question
// If hotspot, create first answer // If hotspot, create first answer
if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) { if ($type == HOT_SPOT || $type == HOT_SPOT_ORDER) {
$TBL_ANSWERS = Database::get_course_table( $quizAnswer = new \Chamilo\CourseBundle\Entity\CQuizAnswer();
TABLE_QUIZ_ANSWER $quizAnswer
); ->setCId($c_id)
$params = [ ->setQuestionId($this->id)
'c_id' => $c_id, ->setAnswer('')
'question_id' => $this->id, ->setPonderation(10)
'answer' => '', ->setPosition(1)
'correct' => '', ->setHotspotCoordinates('0;0|0|0')
'comment' => '', ->setHotspotType('square');
'ponderation' => 10,
'position' => 1, $em->persist($quizAnswer);
'hotspot_coordinates' => '0;0|0|0', $em->flush();
'hotspot_type' => 'square',
]; $id = $quizAnswer->getIid();
$id = Database::insert($TBL_ANSWERS, $params);
if ($id) { if ($id) {
$sql = "UPDATE $TBL_ANSWERS SET id = iid, id_auto = iid WHERE iid = $id"; $quizAnswer
Database::query($sql); ->setId($id)
->setIdAuto($id);
$em->merge($quizAnswer);
$em->flush();
} }
} }
if ($type == HOT_SPOT_DELINEATION) { if ($type == HOT_SPOT_DELINEATION) {
$TBL_ANSWERS = Database::get_course_table( $quizAnswer = new \Chamilo\CourseBundle\Entity\CQuizAnswer();
TABLE_QUIZ_ANSWER $quizAnswer
); ->setCId($c_id)
$params = [ ->setQuestionId($this->id)
'c_id' => $c_id, ->setAnswer('')
'question_id' => $this->id, ->setPonderation(10)
'answer' => '', ->setPosition(1)
'correct' => '', ->setHotspotCoordinates('0;0|0|0')
'comment' => '', ->setHotspotType('delineation');
'ponderation' => 10,
'position' => 1, $em->persist($quizAnswer);
'hotspot_coordinates' => '0;0|0|0', $em->flush();
'hotspot_type' => 'delineation',
]; $id = $quizAnswer->getIid();
$id = Database::insert($TBL_ANSWERS, $params);
if ($id) { if ($id) {
$sql = "UPDATE $TBL_ANSWERS SET id = iid, id_auto = iid WHERE iid = $id"; $quizAnswer
Database::query($sql); ->setId($id)
->setIdAuto($id);
$em->merge($quizAnswer);
$em->flush();
} }
} }

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

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

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

Loading…
Cancel
Save