MigrationMoodle: Migrate user answers for truefalse questions - refs BT#15992

pull/3127/head
Angel Fernando Quiroz Campos 6 years ago
parent ee86ae0c1b
commit 90d8d6cd2c
  1. 1
      plugin/migrationmoodle/src/Task/UserQuestionAttemptsTask.php
  2. 161
      plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswer.php
  3. 144
      plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswerGapselect.php
  4. 35
      plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswerShortanswer.php
  5. 51
      plugin/migrationmoodle/src/Transformer/Property/UserQuestionAnswerTruefalse.php

@ -89,6 +89,7 @@ class UserQuestionAttemptsTask extends BaseTask
'fraction', 'fraction',
'defaultmark', 'defaultmark',
'questionsummary', 'questionsummary',
'questionid',
], ],
], ],
'marks' => 'fraction', 'marks' => 'fraction',

@ -4,7 +4,6 @@
namespace Chamilo\PluginBundle\MigrationMoodle\Transformer\Property; namespace Chamilo\PluginBundle\MigrationMoodle\Transformer\Property;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\TransformPropertyInterface; use Chamilo\PluginBundle\MigrationMoodle\Interfaces\TransformPropertyInterface;
use Chamilo\PluginBundle\MigrationMoodle\Loader\LessonAnswersShortAnswerLoader;
/** /**
* Class UserQuestionAnswer. * Class UserQuestionAnswer.
@ -18,162 +17,24 @@ class UserQuestionAnswer implements TransformPropertyInterface
*/ */
public function transform(array $data) public function transform(array $data)
{ {
list( list($mQType) = array_values($data);
$mQType,
$mRightAnswer, $userQuestionAnswer = null;
$mResponseSummary,
$mFraction,
$mDefaultMark,
$mQuestionSummary
) = array_values($data);
switch ($mQType) { switch ($mQType) {
case 'shortanswer': case 'shortanswer':
return $this->shortanswer($mRightAnswer, $mResponseSummary, $mFraction, $mDefaultMark); $userQuestionAnswer = new UserQuestionAnswerShortanswer();
break;
case 'gapselect': case 'gapselect':
return $this->gapselect($mRightAnswer, $mResponseSummary, $mDefaultMark, $mQuestionSummary); $userQuestionAnswer = new UserQuestionAnswerGapselect();
break;
case 'truefalse':
$userQuestionAnswer = new UserQuestionAnswerTruefalse();
break;
default: default:
return ''; 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);
} }
} }

@ -0,0 +1,144 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Transformer\Property;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\TransformPropertyInterface;
/**
* Class UserQuestionAnswerGapselect.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Transformer\Property
*/
class UserQuestionAnswerGapselect implements TransformPropertyInterface
{
/**
* @inheritDoc
*/
public function transform(array $data)
{
list(
$mQType,
$mRightAnswer,
$mResponseSummary,
$mFraction,
$mDefaultMark,
$mQuestionSummary,
$mQuestionId
) = array_values($data);
$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;
}
}

@ -0,0 +1,35 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Transformer\Property;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\TransformPropertyInterface;
use Chamilo\PluginBundle\MigrationMoodle\Loader\LessonAnswersShortAnswerLoader;
/**
* Class UserQuestionAnswerShortanswer.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Transformer\Property
*/
class UserQuestionAnswerShortanswer implements TransformPropertyInterface
{
/**
* @inheritDoc
*/
public function transform(array $data)
{
list(
$mQType,
$mRightAnswer,
$mResponseSummary,
$mFraction,
$mDefaultMark,
$mQuestionSummary,
$mQuestionId
) = array_values($data);
$width = LessonAnswersShortAnswerLoader::INPUT_WIDTH;
return "[$mRightAnswer][$mResponseSummary][$mFraction]::$mDefaultMark:$width:0@";
}
}

@ -0,0 +1,51 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Transformer\Property;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\TransformPropertyInterface;
use Doctrine\DBAL\DBALException;
/**
* Class UserQuestionAnswerTruefalse.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Transformer\Property
*/
class UserQuestionAnswerTruefalse implements TransformPropertyInterface
{
/**
* @inheritDoc
*/
public function transform(array $data)
{
list(
$mQType,
$mRightAnswer,
$mResponseSummary,
$mFraction,
$mDefaultMark,
$mQuestionSummary,
$mQuestionId
) = array_values($data);
try {
$connection = \MigrationMoodlePlugin::create()->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'];
}
}
Loading…
Cancel
Save