- Issue created by @Peter Majmesku
- 🇩🇪Germany Peter Majmesku 🇩🇪Düsseldorf
Please notice: I would like to create a multiple-choice question programmatically.
- 🇩🇪Germany Peter Majmesku 🇩🇪Düsseldorf
I was able to do it like this:
$a = Paragraph::create([ 'type' => 'multichoice', 'multichoice_correct' => 1, 'multichoice_answer' => 'Alternative A', 'multichoice_feedback_chosen' => 'You chose A', 'multichoice_feedback_not_chosen' => 'You did not choose A', 'multichoice_score_chosen' => 1, 'multichoice_score_not_chosen' => 0, ]); $a->save(); $b = Paragraph::create([ 'type' => 'multichoice', 'multichoice_answer' => 'Alternative B', 'multichoice_feedback_chosen' => 'You chose B', 'multichoice_feedback_not_chosen' => 'You did not choose B', 'multichoice_score_chosen' => -1, 'multichoice_score_not_chosen' => 0, ]); $b->save(); $c = Paragraph::create([ 'type' => 'multichoice', 'multichoice_answer' => 'Alternative C', 'multichoice_feedback_chosen' => 'You chose C', 'multichoice_feedback_not_chosen' => 'You did not choose C', 'multichoice_score_chosen' => -1, 'multichoice_score_not_chosen' => 0, ]); $c->save(); $question = QuizQuestion::create([ 'title' => 'MCQ 1 Title', 'type' => 'multichoice', 'choice_multi' => 0, 'choice_random' => 0, 'choice_boolean' => 0, 'body' => 'MCQ 1 body text', ]); $question->get('alternatives')->appendItem($a); $question->get('alternatives')->appendItem($b); $question->get('alternatives')->appendItem($c); $question->save();
Found code examples in the automated tests. See quiz/modules/quiz_multichoice/tests/src/Functional/QuizMultichoiceTestCase.php