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

@ -10,6 +10,8 @@
* @package chamilo.exercise
*/
use Chamilo\CourseBundle\Entity\CQuizAnswer;
/**
* 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->save();
$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)) {
// For each question found...
$courseId = api_get_course_int_id();
foreach ($exercise_info['question'] as $key => $question_array) {
error_log($counter);
$counter++;
// 2.create question
$question = new Aiken2Question();
$question->type = $question_array['type'];
@ -191,14 +200,13 @@ function aiken_import_exercise($file)
if (isset($question_array['description'])) {
$question->updateDescription($question_array['description']);
}
$type = $question->selectType();
$question->type = constant($type);
$question->save($exercise);
$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']);
$max_score = 0;
@ -211,6 +219,7 @@ function aiken_import_exercise($file)
$key++;
$answer->new_answer[$key] = $answers['value'];
$answer->new_position[$key] = $key;
$answer->new_comment[$key] = '';
// Correct answers ...
if (in_array($key, $question_array['correct_answers'])) {
$answer->new_correct[$key] = 1;
@ -229,16 +238,41 @@ function aiken_import_exercise($file)
if (!empty($scoreFromFile) && $answer->new_correct[$key]) {
$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)) {
$max_score = $scoreFromFile;
}
$answer->save();
// Now that we know the question score, set it!
$question->updateWeighting($max_score);
$question->save($exercise);
//$answer->save();
$params = ['ponderation' => $max_score];
Database::update(
$tableQuestion,
$params,
['iid = ?' => [$last_question_id]]
);
}
// Delete the temp dir where the exercise was unzipped

Loading…
Cancel
Save