Improve aiken import see BT#15232

pull/2874/head
Julio 7 years ago
parent a718393ecb
commit a004c95712
  1. 35
      main/exercise/answer.class.php
  2. 50
      main/exercise/export/aiken/aiken_import.inc.php

@ -43,6 +43,8 @@ class Answer
public $iid; public $iid;
public $questionJSId; public $questionJSId;
public $standalone; public $standalone;
/** @var Exercise|null */
private $exercise;
/** /**
* constructor of the class. * constructor of the class.
@ -52,8 +54,9 @@ class Answer
* @param int $questionId that answers belong to * @param int $questionId that answers belong to
* @param int $course_id * @param int $course_id
* @param Exercise $exercise * @param Exercise $exercise
* @param bool $readAnswer
*/ */
public function __construct($questionId, $course_id = 0, $exercise = null) public function __construct($questionId, $course_id = 0, $exercise = null, $readAnswer = true)
{ {
$this->questionId = (int) $questionId; $this->questionId = (int) $questionId;
$this->answer = []; $this->answer = [];
@ -84,11 +87,14 @@ class Answer
} else { } else {
$objExercise = $exercise; $objExercise = $exercise;
} }
$this->exercise = $objExercise;
if ($objExercise->random_answers == '1' && $this->getQuestionType() != CALCULATED_ANSWER) { if ($readAnswer) {
$this->readOrderedBy('rand()', ''); // randomize answers if ($objExercise->random_answers == '1' && $this->getQuestionType() != CALCULATED_ANSWER) {
} else { $this->readOrderedBy('rand()', ''); // randomize answers
$this->read(); // natural order } else {
$this->read(); // natural order
}
} }
} }
@ -670,7 +676,6 @@ class Answer
$questionId = (int) $this->questionId; $questionId = (int) $this->questionId;
$courseId = $this->course['real_id']; $courseId = $this->course['real_id'];
$correctList = [];
$answerList = []; $answerList = [];
for ($i = 1; $i <= $this->new_nbrAnswers; $i++) { for ($i = 1; $i <= $this->new_nbrAnswers; $i++) {
@ -710,16 +715,13 @@ class Answer
->setId($iid) ->setId($iid)
->setIdAuto($iid); ->setIdAuto($iid);
$em->merge($quizAnswer);
$em->flush();
$questionType = $this->getQuestionType(); $questionType = $this->getQuestionType();
if (in_array( if (in_array(
$questionType, $questionType,
[MATCHING, MATCHING_DRAGGABLE] [MATCHING, MATCHING_DRAGGABLE]
)) { )) {
$answer = new Answer($this->questionId); $answer = new Answer($this->questionId, $courseId, $this->exercise, false);
$answer->read(); $answer->read();
$correctAnswerId = $answer->selectAnswerIdByPosition($correct); $correctAnswerId = $answer->selectAnswerIdByPosition($correct);
@ -728,13 +730,12 @@ class Answer
if ($questionType == MATCHING && !$correctAnswerId) { if ($questionType == MATCHING && !$correctAnswerId) {
continue; continue;
} }
$correctAnswerAutoId = $answer->selectAutoId($correct); $correctAnswerAutoId = $answer->selectAutoId($correct);
$quizAnswer->setCorrect($correctAnswerAutoId ? $correctAnswerAutoId : 0); $quizAnswer->setCorrect($correctAnswerAutoId ? $correctAnswerAutoId : 0);
$em->merge($quizAnswer);
$em->flush();
} }
$em->merge($quizAnswer);
$em->flush();
} }
} else { } else {
// https://support.chamilo.org/issues/6558 // https://support.chamilo.org/issues/6558
@ -754,13 +755,9 @@ class Answer
} }
$answerList[$i] = $iid; $answerList[$i] = $iid;
if ($correct) {
$correctList[$iid] = true;
}
} }
$questionType = self::getQuestionType(); $questionType = $this->getQuestionType();
switch ($questionType) { switch ($questionType) {
case MATCHING_DRAGGABLE: case MATCHING_DRAGGABLE:

@ -10,6 +10,8 @@
* @package chamilo.exercise * @package chamilo.exercise
*/ */
use Chamilo\CourseBundle\Entity\CQuizAnswer;
/** /**
* This function displays the form for import of the zip file with qti2. * This function displays the form for import of the zip file with qti2.
* *
@ -179,9 +181,16 @@ function aiken_import_exercise($file)
$exercise->exercise = $exercise_info['name']; $exercise->exercise = $exercise_info['name'];
$exercise->save(); $exercise->save();
$last_exercise_id = $exercise->selectId(); $last_exercise_id = $exercise->selectId();
error_log('--------------start---');
$counter = 0;
$tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION);
$tableAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER);
if (!empty($last_exercise_id)) { if (!empty($last_exercise_id)) {
// For each question found... // For each question found...
$courseId = api_get_course_int_id();
foreach ($exercise_info['question'] as $key => $question_array) { foreach ($exercise_info['question'] as $key => $question_array) {
error_log($counter);
$counter++;
// 2.create question // 2.create question
$question = new Aiken2Question(); $question = new Aiken2Question();
$question->type = $question_array['type']; $question->type = $question_array['type'];
@ -191,14 +200,13 @@ function aiken_import_exercise($file)
if (isset($question_array['description'])) { if (isset($question_array['description'])) {
$question->updateDescription($question_array['description']); $question->updateDescription($question_array['description']);
} }
$type = $question->selectType(); $type = $question->selectType();
$question->type = constant($type); $question->type = constant($type);
$question->save($exercise); $question->save($exercise);
$last_question_id = $question->selectId(); $last_question_id = $question->selectId();
//3. Create answer
$answer = new Answer($last_question_id); // 3. Create answer
$answer = new Answer($last_question_id, $courseId, $exercise, false);
$answer->new_nbrAnswers = count($question_array['answer']); $answer->new_nbrAnswers = count($question_array['answer']);
$max_score = 0; $max_score = 0;
@ -211,6 +219,7 @@ function aiken_import_exercise($file)
$key++; $key++;
$answer->new_answer[$key] = $answers['value']; $answer->new_answer[$key] = $answers['value'];
$answer->new_position[$key] = $key; $answer->new_position[$key] = $key;
$answer->new_comment[$key] = '';
// Correct answers ... // Correct answers ...
if (in_array($key, $question_array['correct_answers'])) { if (in_array($key, $question_array['correct_answers'])) {
$answer->new_correct[$key] = 1; $answer->new_correct[$key] = 1;
@ -229,16 +238,41 @@ function aiken_import_exercise($file)
if (!empty($scoreFromFile) && $answer->new_correct[$key]) { if (!empty($scoreFromFile) && $answer->new_correct[$key]) {
$answer->new_weighting[$key] = $scoreFromFile; $answer->new_weighting[$key] = $scoreFromFile;
} }
$params = [
'c_id' => $courseId,
'question_id' => $last_question_id,
'answer' => $answer->new_answer[$key],
'correct' => $answer->new_correct[$key],
'comment' => $answer->new_comment[$key],
'ponderation' => isset($answer->new_weighting[$key]) ? $answer->new_weighting[$key] : '',
'position' => $answer->new_position[$key],
'hotspot_coordinates' => '',
'hotspot_type' => '',
];
$answerId = Database::insert($tableAnswer, $params);
if ($answerId) {
$params = [
'id_auto' => $answerId,
'id' => $answerId,
];
Database::update($tableAnswer, $params, ['iid = ?' => [$answerId]]);
}
} }
if (!empty($scoreFromFile)) { if (!empty($scoreFromFile)) {
$max_score = $scoreFromFile; $max_score = $scoreFromFile;
} }
$answer->save(); //$answer->save();
// Now that we know the question score, set it!
$question->updateWeighting($max_score); $params = ['ponderation' => $max_score];
$question->save($exercise); Database::update(
$tableQuestion,
$params,
['iid = ?' => [$last_question_id]]
);
} }
// Delete the temp dir where the exercise was unzipped // Delete the temp dir where the exercise was unzipped

Loading…
Cancel
Save