From b6efeb4730505ec008ce09ab562bf9a4f53a37ab Mon Sep 17 00:00:00 2001
From: Nick Phillips <nick.phillips@otago.ac.nz>
Date: Thu, 13 Jul 2017 13:28:52 +1200
Subject: [PATCH] MDL-59447 question: GIFT import to respect multichoice
 defaults

Fix GIFT import to respect default answernumbering for new multichoice
questions (introduced in MDL-50011). Default for shuffleanswers is still
ignored, as this is a rather coarse-grained way to do it, and the wrong
setting for shuffleanswers would irreversibly alter the meaning of some
questions.

This is not an ideal long-term fix, but will save significant effort
for anyone who needs to be able to set answernumbering style for
questions imported in GIFT format.
---
 question/format/gift/format.php               |  4 +++
 .../format/gift/tests/giftformat_test.php     | 32 +++++++++++++++++--
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/question/format/gift/format.php b/question/format/gift/format.php
index 022a5c348ee..8fef5978008 100644
--- a/question/format/gift/format.php
+++ b/question/format/gift/format.php
@@ -338,6 +338,10 @@ class qformat_gift extends qformat_default {
                 return $question;
 
             case 'multichoice':
+                // "Temporary" solution to enable choice of answernumbering on GIFT import
+                // by respecting default set for multichoice questions (MDL-59447)
+                $question->answernumbering = get_config('qtype_multichoice', 'answernumbering');
+
                 if (strpos($answertext, "=") === false) {
                     $question->single = 0; // Multiple answers are enabled if no single answer is 100% correct.
                 } else {
diff --git a/question/format/gift/tests/giftformat_test.php b/question/format/gift/tests/giftformat_test.php
index 2d3b9ab1ca4..90c89ef2d7e 100644
--- a/question/format/gift/tests/giftformat_test.php
+++ b/question/format/gift/tests/giftformat_test.php
@@ -267,7 +267,18 @@ class qformat_gift_test extends question_testcase {
         $this->assert_same_gift($expectedgift, $gift);
     }
 
-    public function test_import_multichoice() {
+    /**
+     * Test import of multichoice question in GIFT format
+     *
+     * @dataProvider numberingstyle_provider
+     *
+     * @param string $numberingstyle multichoice numbering style to set for qtype_multichoice
+     *
+     */
+    public function test_import_multichoice($numberingstyle) {
+        $this->resetAfterTest(true);
+
+        set_config('answernumbering', $numberingstyle, 'qtype_multichoice');
         $gift = "
 // multiple choice with specified feedback for right and wrong answers
 ::Q2:: What's between orange and green in the spectrum?
@@ -293,7 +304,7 @@ class qformat_gift_test extends question_testcase {
             'length' => 1,
             'single' => 1,
             'shuffleanswers' => '1',
-            'answernumbering' => 'abc',
+            'answernumbering' => $numberingstyle,
             'correctfeedback' => array(
                 'text' => '',
                 'format' => FORMAT_MOODLE,
@@ -352,6 +363,23 @@ class qformat_gift_test extends question_testcase {
         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
     }
 
+    /**
+     * Return a list of numbering styles (see question/type/multichoice/questiontype.php
+     * for valid choices)
+     *
+     * @return array Array of 1-element arrays of qtype_multichoice numbering styles
+     */
+    public function numberingstyle_provider() {
+        return [
+            ['abc'],
+            ['ABCD'],
+            ['123'],
+            ['iii'],
+            ['IIII'],
+            ['none']
+        ];
+    }
+
     public function test_import_multichoice_multi() {
         $gift = "
 // multiple choice, multiple response with specified feedback for right and wrong answers