diff --git a/plugin/migrationmoodle/src/Task/UserQuestionAttemptsTask.php b/plugin/migrationmoodle/src/Task/UserQuestionAttemptsTask.php index 6ed88f217b..7a79dc3d1f 100644 --- a/plugin/migrationmoodle/src/Task/UserQuestionAttemptsTask.php +++ b/plugin/migrationmoodle/src/Task/UserQuestionAttemptsTask.php @@ -89,6 +89,7 @@ class UserQuestionAttemptsTask extends BaseTask 'fraction', 'defaultmark', 'questionsummary', + 'questionid', ], ], 'marks' => 'fraction', diff --git a/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswer.php b/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswer.php index 61ebbcddee..9a39a53471 100644 --- a/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswer.php +++ b/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswer.php @@ -4,7 +4,6 @@ namespace Chamilo\PluginBundle\MigrationMoodle\Transformer\Property; use Chamilo\PluginBundle\MigrationMoodle\Interfaces\TransformPropertyInterface; -use Chamilo\PluginBundle\MigrationMoodle\Loader\LessonAnswersShortAnswerLoader; /** * Class UserQuestionAnswer. @@ -18,162 +17,24 @@ class UserQuestionAnswer implements TransformPropertyInterface */ public function transform(array $data) { - list( - $mQType, - $mRightAnswer, - $mResponseSummary, - $mFraction, - $mDefaultMark, - $mQuestionSummary - ) = array_values($data); + list($mQType) = array_values($data); + + $userQuestionAnswer = null; switch ($mQType) { case 'shortanswer': - return $this->shortanswer($mRightAnswer, $mResponseSummary, $mFraction, $mDefaultMark); + $userQuestionAnswer = new UserQuestionAnswerShortanswer(); + break; case 'gapselect': - return $this->gapselect($mRightAnswer, $mResponseSummary, $mDefaultMark, $mQuestionSummary); + $userQuestionAnswer = new UserQuestionAnswerGapselect(); + break; + case 'truefalse': + $userQuestionAnswer = new UserQuestionAnswerTruefalse(); + break; default: return ''; } - } - - /** - * @param string $mRightAnswer - * @param string $mResponseSummary - * @param float $mFraction - * @param float $mDefaultMark - * - * @return string - */ - private function shortanswer($mRightAnswer, $mResponseSummary, $mFraction, $mDefaultMark) - { - $width = LessonAnswersShortAnswerLoader::INPUT_WIDTH; - - return "[$mRightAnswer][$mResponseSummary][$mFraction]::$mDefaultMark:$width:0@"; - } - - /** - * @param string $mRightAnswer - * @param string $mResponseSummary - * @param float $mDefaultMark - * @param string $mQuestionSummary - * - * @return string - */ - private function gapselect($mRightAnswer, $mResponseSummary, $mDefaultMark, $mQuestionSummary) - { - $mRightAnswer = $this->gapselectGetRightAnswers($mRightAnswer); - - $mResponseSummary = $this->gapselectGetResposeSummary($mResponseSummary); - - $mQuestionSummary = explode(";", $mQuestionSummary); - $mQuestionSummary = array_map('trim', $mQuestionSummary); - - $questionText = array_shift($mQuestionSummary); - - $groupsAndOptions = $this->gapselectGetGroupsAndOptions($mQuestionSummary); - - $blanks = $this->gapselectGetBlanks($mRightAnswer, $groupsAndOptions); - - $count = 0; - - foreach ($blanks as $placeholder => $blank) { - $userAnswer = empty($mResponseSummary[$count]) ? '' : $mResponseSummary[$count]; - $replacement = $blank."[$userAnswer][0]"; - - $questionText = str_replace("[[$placeholder]]", $replacement, $questionText); - - $count++; - } - - $scorePerBlank = $mDefaultMark / count($mRightAnswer); - - $optionsScores = array_fill(0, count($mRightAnswer), $scorePerBlank); - $width = array_fill(0, count($mRightAnswer), 300); - - return "$questionText::".implode(',', $optionsScores).':'.implode(',', $width).':0@'; - } - - /** - * @param string $mRightAnswer - * - * @return array - */ - private function gapselectGetRightAnswers($mRightAnswer) - { - $rightAnswers = []; - - $mRightAnswer = explode('} {', $mRightAnswer); - - foreach ($mRightAnswer as $i0 => $item) { - $rightAnswers[$i0 + 1] = trim($item, "{} \t\n\r\x0B"); - } - - return $rightAnswers; - } - - /** - * @param string $mResponseSummary - * - * @return array - */ - private function gapselectGetResposeSummary($mResponseSummary) - { - $mResponseSummary = explode('} {', $mResponseSummary); - - return array_map( - function ($item) { - return trim($item, "{} \t\n\r\x0B"); - }, - $mResponseSummary - ); - } - - /** - * @param array $mQuestionSummary - * - * @return array - */ - private function gapselectGetGroupsAndOptions(array $mQuestionSummary) - { - $groupsAndOptions = []; - - foreach ($mQuestionSummary as $groupAndOptions) { - list($group, $options) = explode(' -> ', $groupAndOptions); - - $group = str_replace(['[', ']'], '', $group); - $options = explode(' / ', trim($options, "{} \t\n\r\x0B")); - - $groupsAndOptions[$group] = $options; - } - - return $groupsAndOptions; - } - - /** - * @param array $rightAnswers - * @param array $groupsAndOptions - * - * @return array - */ - private function gapselectGetBlanks(array $rightAnswers, array $groupsAndOptions) - { - $blanks = []; - - foreach ($rightAnswers as $i => $rightAnswer) { - foreach ($groupsAndOptions as $group => $options) { - if (in_array($rightAnswer, $options)) { - $optionIndex = array_search($rightAnswer, $options); - - unset($options[$optionIndex]); - - $options = array_merge([$rightAnswer], $options); - - $blanks[$i] = '['.implode('|', $options).']'; - } - } - } - return $blanks; + return $userQuestionAnswer->transform($data); } } diff --git a/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswerGapselect.php b/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswerGapselect.php new file mode 100644 index 0000000000..226bc530fe --- /dev/null +++ b/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswerGapselect.php @@ -0,0 +1,144 @@ +gapselectGetRightAnswers($mRightAnswer); + + $mResponseSummary = $this->gapselectGetResposeSummary($mResponseSummary); + + $mQuestionSummary = explode(";", $mQuestionSummary); + $mQuestionSummary = array_map('trim', $mQuestionSummary); + + $questionText = array_shift($mQuestionSummary); + + $groupsAndOptions = $this->gapselectGetGroupsAndOptions($mQuestionSummary); + + $blanks = $this->gapselectGetBlanks($mRightAnswer, $groupsAndOptions); + + $count = 0; + + foreach ($blanks as $placeholder => $blank) { + $userAnswer = empty($mResponseSummary[$count]) ? '' : $mResponseSummary[$count]; + $replacement = $blank."[$userAnswer][0]"; + + $questionText = str_replace("[[$placeholder]]", $replacement, $questionText); + + $count++; + } + + $scorePerBlank = $mDefaultMark / count($mRightAnswer); + + $optionsScores = array_fill(0, count($mRightAnswer), $scorePerBlank); + $width = array_fill(0, count($mRightAnswer), 300); + + return "$questionText::".implode(',', $optionsScores).':'.implode(',', $width).':0@'; + } + + /** + * @param string $mRightAnswer + * + * @return array + */ + private function gapselectGetRightAnswers($mRightAnswer) + { + $rightAnswers = []; + + $mRightAnswer = explode('} {', $mRightAnswer); + + foreach ($mRightAnswer as $i0 => $item) { + $rightAnswers[$i0 + 1] = trim($item, "{} \t\n\r\x0B"); + } + + return $rightAnswers; + } + + /** + * @param string $mResponseSummary + * + * @return array + */ + private function gapselectGetResposeSummary($mResponseSummary) + { + $mResponseSummary = explode('} {', $mResponseSummary); + + return array_map( + function ($item) { + return trim($item, "{} \t\n\r\x0B"); + }, + $mResponseSummary + ); + } + + /** + * @param array $mQuestionSummary + * + * @return array + */ + private function gapselectGetGroupsAndOptions(array $mQuestionSummary) + { + $groupsAndOptions = []; + + foreach ($mQuestionSummary as $groupAndOptions) { + list($group, $options) = explode(' -> ', $groupAndOptions); + + $group = str_replace(['[', ']'], '', $group); + $options = explode(' / ', trim($options, "{} \t\n\r\x0B")); + + $groupsAndOptions[$group] = $options; + } + + return $groupsAndOptions; + } + + /** + * @param array $rightAnswers + * @param array $groupsAndOptions + * + * @return array + */ + private function gapselectGetBlanks(array $rightAnswers, array $groupsAndOptions) + { + $blanks = []; + + foreach ($rightAnswers as $i => $rightAnswer) { + foreach ($groupsAndOptions as $group => $options) { + if (in_array($rightAnswer, $options)) { + $optionIndex = array_search($rightAnswer, $options); + + unset($options[$optionIndex]); + + $options = array_merge([$rightAnswer], $options); + + $blanks[$i] = '['.implode('|', $options).']'; + } + } + } + + return $blanks; + } +} diff --git a/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswerShortanswer.php b/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswerShortanswer.php new file mode 100644 index 0000000000..6c8082f8a5 --- /dev/null +++ b/plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswerShortanswer.php @@ -0,0 +1,35 @@ +getConnection(); + } catch (DBALException $exception) { + throw new \Exception('Unable to start connection.', 0, $exception); + } + + try { + $sql = "SELECT id + FROM mdl_question_answers + WHERE question = ? and answer = ?"; + + $result = $connection->fetchAssoc($sql, [$mQuestionId, $mResponseSummary]); + } catch (DBALException $exception) { + throw new \Exception("Unable to execute query \"{$this->query}\".", 0, $exception); + } + + $connection->close(); + + return $result['id']; + } +}