Fix #1675 duplicate function for MATCHING and MATCHING_DRAGGABLE

remotes/angel/1.11.x
jmontoyaa 8 years ago
parent 783566e18a
commit e3a2809867
  1. 2
      main/exercise/admin.php
  2. 196
      main/exercise/answer.class.php
  3. 2
      main/exercise/exercise.class.php
  4. 2
      main/exercise/hotspot_admin.inc.php
  5. 26
      main/exercise/question.class.php
  6. 4
      main/exercise/question_pool.php
  7. 23
      src/Chamilo/CourseBundle/Component/CourseCopy/CourseRestorer.php

@ -260,7 +260,7 @@ if (!empty($clone_question) && !empty($objExercise->id)) {
// This should be moved to the duplicate function
$new_answer_obj = new Answer($clone_question);
$new_answer_obj->read();
$new_answer_obj->duplicate($new_id);
$new_answer_obj->duplicate($new_question_obj);
//Reloading tne $objExercise obj
$objExercise->read($objExercise->id);

@ -752,17 +752,19 @@ class Answer
* Duplicates answers by copying them into another question
*
* @author Olivier Brouckaert
* @param int question id
* @param array destination course info (result of the function api_get_course_info() )
* @param string $newQuestionId
* @param Question $newQuestion
* @param array $course_info destination course info (result of the function api_get_course_info() )
*/
public function duplicate($newQuestionId, $course_info = null)
public function duplicate($newQuestion, $course_info = null)
{
$newQuestionId = $newQuestion->id;
if (empty($course_info)) {
$course_info = $this->course;
}
$fixed_list = array();
$tableAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE
@ -779,7 +781,10 @@ class Answer
}
}
$destination_options = Question::readQuestionOption($newQuestionId, $course_info['real_id']);
$destination_options = Question::readQuestionOption(
$newQuestionId,
$course_info['real_id']
);
$i = 0;
if (!empty($destination_options)) {
foreach($destination_options as $item) {
@ -792,55 +797,154 @@ class Answer
// if at least one answer
if ($this->nbrAnswers) {
// inserts new answers into data base
$c_id = $course_info['real_id'];
for ($i=1;$i <= $this->nbrAnswers;$i++) {
if ($this->course['id'] != $course_info['id']) {
$this->answer[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$this->answer[$i],
$this->course['id'],
$course_info['id']
);
$this->comment[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$this->comment[$i],
$this->course['id'],
$course_info['id']
);
$courseId = $course_info['real_id'];
$correctAnswers = [];
$onlyAnswers = [];
$allAnswers = [];
$em = Database::getManager();
if (in_array($newQuestion->type, [MATCHING, MATCHING_DRAGGABLE])) {
$temp = array();
for ($i = 1; $i <= $this->nbrAnswers; $i++) {
$answer = [
'id' => $this->id[$i],
'answer' => $this->answer[$i],
'correct' => $this->correct[$i],
'comment' => $this->comment[$i],
'weighting' => $this->weighting[$i],
'ponderation' => $this->weighting[$i],
'position' => $this->position[$i],
'hotspot_coordinates' => $this->hotspot_coordinates[$i],
'hotspot_type' => $this->hotspot_type[$i],
'destination' => $this->destination[$i],
];
$temp[$answer['position']] = $answer;
$allAnswers[$this->id[$i]] = $this->answer[$i];
}
$correct = $this->correct[$i];
foreach ($temp as $index => $answer) {
if ($this->course['id'] != $course_info['id']) {
// check resources inside html from ckeditor tool and copy correct urls into recipient course
$answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$answer['answer'],
$this->course['id'],
$course_info['id']
);
$answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$answer['comment'],
$this->course['id'],
$course_info['id']
);
}
if (self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE ||
self::getQuestionType() == MULTIPLE_ANSWER_TRUE_FALSE
) {
$correct = $fixed_list[intval($correct)];
}
$quizAnswer = new CQuizAnswer();
$quizAnswer
->setCId($courseId)
->setQuestionId($newQuestionId)
->setAnswer($answer['answer'])
->setCorrect($answer['correct'])
->setComment($answer['comment'])
->setPonderation($answer['ponderation'])
->setPosition($answer['position'])
->setHotspotCoordinates($answer['hotspot_coordinates'])
->setHotspotType($answer['hotspot_type'])
->setIdAuto(0);
$em->persist($quizAnswer);
$em->flush();
$quizAnswer = new 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]);
$answerId = $quizAnswer->getIid();
$em = Database::getManager();
if ($answerId) {
$quizAnswer
->setId($answerId)
->setIdAuto($answerId);
$em->persist($quizAnswer);
$em->flush();
$em->merge($quizAnswer);
$em->flush();
$quizAnswer
->setId($quizAnswer->getIid())
->setIdAuto($quizAnswer->getIid());
$correctAnswers[$answerId] = $answer['correct'];
$onlyAnswers[$answerId] = $answer['answer'];
}
}
} else {
for ($i = 1; $i <= $this->nbrAnswers; $i++) {
if ($this->course['id'] != $course_info['id']) {
$this->answer[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$this->answer[$i],
$this->course['id'],
$course_info['id']
);
$this->comment[$i] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$this->comment[$i],
$this->course['id'],
$course_info['id']
);
}
$correct = $this->correct[$i];
if ($newQuestion->type == MULTIPLE_ANSWER_TRUE_FALSE ||
$newQuestion->type == MULTIPLE_ANSWER_TRUE_FALSE
) {
$correct = $fixed_list[intval($correct)];
}
$quizAnswer = new CQuizAnswer();
$quizAnswer
->setCId($courseId)
->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->persist($quizAnswer);
$em->flush();
$answerId = $quizAnswer->getIid();
$quizAnswer
->setId($answerId)
->setIdAuto($answerId);
$em->merge($quizAnswer);
$em->flush();
$correctAnswers[$answerId] = $correct;
$onlyAnswers[$answerId] = $this->answer[$i];
$allAnswers[$this->id[$i]] = $this->answer[$i];
}
}
// Fix correct answers
if (in_array($newQuestion->type, [DRAGGABLE, MATCHING, MATCHING_DRAGGABLE])) {
$onlyAnswersFlip = array_flip($onlyAnswers);
foreach ($correctAnswers as $answer_id => $correct_answer) {
$params = array();
if (isset($allAnswers[$correct_answer]) &&
isset($onlyAnswersFlip[$allAnswers[$correct_answer]])
) {
$params['correct'] = $onlyAnswersFlip[$allAnswers[$correct_answer]];
Database::update(
$tableAnswer,
$params,
array(
'id = ? AND c_id = ? AND question_id = ? ' => array(
$answer_id,
$courseId,
$newQuestionId,
),
)
);
}
}
}
$em->merge($quizAnswer);
$em->flush();
}
}
}

@ -2772,7 +2772,7 @@ class Exercise
// This should be moved to the duplicate function
$new_answer_obj = new Answer($old_question_id);
$new_answer_obj->read();
$new_answer_obj->duplicate($new_id);
$new_answer_obj->duplicate($new_question_obj);
}
}
}

@ -49,7 +49,7 @@ if ($modifyIn) {
$objQuestion->addToList($exerciseId);
// copies answers from $modifyAnswers to $questionId
$objAnswer->duplicate($questionId);
$objAnswer->duplicate($objQuestion);
// construction of the duplicated Answers
$objAnswer = new Answer($questionId);

@ -114,8 +114,7 @@ abstract class Question
$course_id = $course_info['real_id'];
if (empty($course_id) || $course_id == -1 ) {
if (empty($course_id) || $course_id == -1) {
return false;
}
@ -1316,7 +1315,7 @@ abstract class Question
$course_id = $course_info['real_id'];
//Read the source options
// Read the source options
$options = self::readQuestionOption($this->id, $this->course['real_id']);
// Inserting in the new course db / or the same course db
@ -1330,24 +1329,25 @@ abstract class Question
'level' => $level,
'extra' => $extra
];
$new_question_id = Database::insert($TBL_QUESTIONS, $params);
if ($new_question_id) {
$newQuestionId = Database::insert($TBL_QUESTIONS, $params);
$sql = "UPDATE $TBL_QUESTIONS SET id = iid
WHERE iid = $new_question_id";
if ($newQuestionId) {
$sql = "UPDATE $TBL_QUESTIONS
SET id = iid
WHERE iid = $newQuestionId";
Database::query($sql);
if (!empty($options)) {
//Saving the quiz_options
// Saving the quiz_options
foreach ($options as $item) {
$item['question_id'] = $new_question_id;
$item['question_id'] = $newQuestionId;
$item['c_id'] = $course_id;
unset($item['id']);
unset($item['iid']);
$id = Database::insert($TBL_QUESTION_OPTIONS, $item);
if ($id) {
$sql = "UPDATE $TBL_QUESTION_OPTIONS SET id = iid
$sql = "UPDATE $TBL_QUESTION_OPTIONS
SET id = iid
WHERE iid = $id";
Database::query($sql);
}
@ -1355,10 +1355,10 @@ abstract class Question
}
// Duplicates the picture of the hotspot
$this->exportPicture($new_question_id, $course_info);
$this->exportPicture($newQuestionId, $course_info);
}
return $new_question_id;
return $newQuestionId;
}
/**

@ -84,7 +84,7 @@ if ($is_allowedToEdit) {
$new_answer_obj = new Answer($old_question_id, $origin_course_id);
$new_answer_obj->read();
//Duplicating the Answers in the current course
$new_answer_obj->duplicate($new_id, $current_course);
$new_answer_obj->duplicate($new_question_obj, $current_course);
// destruction of the Question object
unset($new_question_obj);
unset($old_question_obj);
@ -154,7 +154,7 @@ if ($is_allowedToEdit) {
$new_answer_obj->read();
//Duplicating the Answers in the current course
$new_answer_obj->duplicate($new_id, $current_course);
$new_answer_obj->duplicate($new_question_obj, $current_course);
// destruction of the Question object
unset($new_question_obj);

@ -1861,7 +1861,7 @@ class CourseRestorer
$allAnswers = [];
$onlyAnswers = [];
if (in_array($question->quiz_type, [DRAGGABLE, MATCHING, MATCHING_DRAGGABLE])) {
if (in_array($question->quiz_type, [DRAGGABLE, MATCHING, MATCHING_DRAGGABLE])) {
$allAnswers = array_column($question->answers, 'answer', 'id');
}
@ -1872,6 +1872,23 @@ class CourseRestorer
}
foreach ($temp as $index => $answer) {
// check resources inside html from ckeditor tool and copy correct urls into recipient course
$answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$answer['answer'],
$this->course->code,
$this->course->destination_path,
$this->course->backup_path,
$this->course->info['path']
);
$answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
$answer['comment'],
$this->course->code,
$this->course->destination_path,
$this->course->backup_path,
$this->course->info['path']
);
$quizAnswer = new CQuizAnswer();
$quizAnswer
->setCId($this->destination_course_id)
@ -1903,7 +1920,6 @@ class CourseRestorer
}
}
} else {
foreach ($question->answers as $index => $answer) {
// check resources inside html from ckeditor tool and copy correct urls into recipient course
$answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
@ -2060,8 +2076,7 @@ class CourseRestorer
$this->destination_course_id,
$new_id,
),
),
false
)
);
}
}

Loading…
Cancel
Save