function QuizResult::copyToQuizResult in Quiz 8.5
Same name and namespace in other branches
- 8.6 src/Entity/QuizResult.php \Drupal\quiz\Entity\QuizResult::copyToQuizResult()
- 6.x src/Entity/QuizResult.php \Drupal\quiz\Entity\QuizResult::copyToQuizResult()
Copy this result's answers onto another Quiz result, based on the new Quiz result's settings.
Parameters
QuizResult $result_new: An empty QuizResult.
File
- src/
Entity/ QuizResult.php, line 593
Class
- QuizResult
- Defines the Quiz entity class.
Namespace
Drupal\quiz\EntityCode
function copyToQuizResult(QuizResult $result_new) {
// Re-take all the questions.
foreach ($this
->getLayout() as $qra) {
// @todo build_on_last = correct not working
if (($result_new->build_on_last == 'all' || $qra
->isCorrect()) && !$qra
->isSkipped()) {
// Populate answer.
$duplicate = $qra
->createDuplicate();
}
else {
// Create new answer.
$duplicate = QuizResultAnswer::create([
'type' => $qra
->bundle(),
]);
foreach ($qra
->getFields() as $name => $field) {
/* @var $field Drupal\Core\Field\FieldItemList */
if ($name != 'result_answer_id' && is_a($field
->getFieldDefinition(), '\\Drupal\\Core\\Field\\BaseFieldDefinition')) {
// Copy any base fields, but not the answer.
$duplicate
->set($name, $field
->getValue());
}
}
}
// Set new result ID.
$duplicate
->set('result_id', $result_new
->id());
$duplicate
->save();
}
}