From c89d59afc7864252c244a2278c9cacfc9eaa537c Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Mon, 8 Aug 2016 17:11:22 -0500 Subject: [PATCH] Use entities when inserting a new c_quiz_answer --- main/exercise/answer.class.php | 132 ++++++++++-------- main/exercise/question.class.php | 76 +++++----- main/exercise/unique_answer.class.php | 37 +++-- .../Component/CourseCopy/CourseRestorer.php | 41 +++--- .../CourseBundle/Entity/CQuizAnswer.php | 19 ++- 5 files changed, 176 insertions(+), 129 deletions(-) diff --git a/main/exercise/answer.class.php b/main/exercise/answer.class.php index adb2d45e8d..5087f4ef56 100755 --- a/main/exercise/answer.class.php +++ b/main/exercise/answer.class.php @@ -579,20 +579,22 @@ class Answer $hotspot_coordinates, $hotspot_type ) { - $answerTable = Database :: get_course_table(TABLE_QUIZ_ANSWER); - - $params = [ - 'answer' => $answer, - 'comment' => $comment, - 'correct' => intval($correct), - 'ponderation' => $weighting, - 'position' => $position, - 'destination' => $destination, - 'hotspot_coordinates' => $hotspot_coordinates, - 'hotspot_type' => $hotspot_type - ]; - - Database::update($answerTable, $params, ['iid = ?' => intval($iid)]); + $em = Database::getManager(); + + /** @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); + + $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); - - if ($id) { - $sql = "UPDATE $TBL_REPONSES SET id = iid, id_auto = iid WHERE iid = $id"; - Database::query($sql); - } + $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]); + + $em = Database::getManager(); + + $em->persist($quizAnswer); + $em->flush(); + + $quizAnswer + ->setId($quizAnswer->getIid()) + ->setIdAuto($quizAnswer->getIid()); + + $em->merge($quizAnswer); + $em->flush(); } } } diff --git a/main/exercise/question.class.php b/main/exercise/question.class.php index 486c7dd7eb..9d4e684dcb 100755 --- a/main/exercise/question.class.php +++ b/main/exercise/question.class.php @@ -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(); } } diff --git a/main/exercise/unique_answer.class.php b/main/exercise/unique_answer.class.php index 74970bbfd7..5146425138 100755 --- a/main/exercise/unique_answer.class.php +++ b/main/exercise/unique_answer.class.php @@ -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) { diff --git a/src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php b/src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php index cf380c450e..d714220f8a 100644 --- a/src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php +++ b/src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php @@ -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 { diff --git a/src/Chamilo/CourseBundle/Entity/CQuizAnswer.php b/src/Chamilo/CourseBundle/Entity/CQuizAnswer.php index 714de4f35f..06ddcbd5a2 100644 --- a/src/Chamilo/CourseBundle/Entity/CQuizAnswer.php +++ b/src/Chamilo/CourseBundle/Entity/CQuizAnswer.php @@ -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; + } }