diff --git a/question/engine/lib.php b/question/engine/lib.php index b65ce4b9c24..93dffb26507 100644 --- a/question/engine/lib.php +++ b/question/engine/lib.php @@ -905,6 +905,47 @@ abstract class question_utils { self::$tens[$number / 10 % 10] . self::$units[$number % 10]; } + /** + * Convert an integer to a letter of alphabet. + * @param int $number an integer between 1 and 26 inclusive. + * Anything else will throw an exception. + * @return string the number converted to upper case letter of alphabet. + */ + public static function int_to_letter($number) { + $alphabet = [ + '1' => 'A', + '2' => 'B', + '3' => 'C', + '4' => 'D', + '5' => 'E', + '6' => 'F', + '7' => 'G', + '8' => 'H', + '9' => 'I', + '10' => 'J', + '11' => 'K', + '12' => 'L', + '13' => 'M', + '14' => 'N', + '15' => 'O', + '16' => 'P', + '17' => 'Q', + '18' => 'R', + '19' => 'S', + '20' => 'T', + '21' => 'U', + '22' => 'V', + '23' => 'W', + '24' => 'X', + '25' => 'Y', + '26' => 'Z' + ]; + if (!is_integer($number) || $number < 1 || $number > count($alphabet)) { + throw new coding_exception('Only integers between 1 and 26 can be converted to letters.', $number); + } + return $alphabet[$number]; + } + /** * Typically, $mark will have come from optional_param($name, null, PARAM_RAW_TRIMMED). * This method copes with: diff --git a/question/engine/tests/questionutils_test.php b/question/engine/tests/questionutils_test.php index b9db0ece675..e60960164f4 100644 --- a/question/engine/tests/questionutils_test.php +++ b/question/engine/tests/questionutils_test.php @@ -177,6 +177,35 @@ class question_utils_test extends advanced_testcase { $this->assertSame('mmmcmxcix', question_utils::int_to_roman(3999)); } + public function test_int_to_letter() { + $this->assertEquals('A', question_utils::int_to_letter(1)); + $this->assertEquals('B', question_utils::int_to_letter(2)); + $this->assertEquals('C', question_utils::int_to_letter(3)); + $this->assertEquals('D', question_utils::int_to_letter(4)); + $this->assertEquals('E', question_utils::int_to_letter(5)); + $this->assertEquals('F', question_utils::int_to_letter(6)); + $this->assertEquals('G', question_utils::int_to_letter(7)); + $this->assertEquals('H', question_utils::int_to_letter(8)); + $this->assertEquals('I', question_utils::int_to_letter(9)); + $this->assertEquals('J', question_utils::int_to_letter(10)); + $this->assertEquals('K', question_utils::int_to_letter(11)); + $this->assertEquals('L', question_utils::int_to_letter(12)); + $this->assertEquals('M', question_utils::int_to_letter(13)); + $this->assertEquals('N', question_utils::int_to_letter(14)); + $this->assertEquals('O', question_utils::int_to_letter(15)); + $this->assertEquals('P', question_utils::int_to_letter(16)); + $this->assertEquals('Q', question_utils::int_to_letter(17)); + $this->assertEquals('R', question_utils::int_to_letter(18)); + $this->assertEquals('S', question_utils::int_to_letter(19)); + $this->assertEquals('T', question_utils::int_to_letter(20)); + $this->assertEquals('U', question_utils::int_to_letter(21)); + $this->assertEquals('V', question_utils::int_to_letter(22)); + $this->assertEquals('W', question_utils::int_to_letter(23)); + $this->assertEquals('X', question_utils::int_to_letter(24)); + $this->assertEquals('Y', question_utils::int_to_letter(25)); + $this->assertEquals('Z', question_utils::int_to_letter(26)); + } + /** * @expectedException moodle_exception */ diff --git a/question/type/ddimageortext/edit_ddimageortext_form.php b/question/type/ddimageortext/edit_ddimageortext_form.php index 63d4c710e4f..b564f44143b 100644 --- a/question/type/ddimageortext/edit_ddimageortext_form.php +++ b/question/type/ddimageortext/edit_ddimageortext_form.php @@ -147,7 +147,7 @@ class qtype_ddimageortext_edit_form extends qtype_ddtoimage_edit_form_base { array('class' => 'dragitemtype')); $options = array(); for ($i = 1; $i <= self::MAX_GROUPS; $i += 1) { - $options[$i] = $i; + $options[$i] = question_utils::int_to_letter($i); } $grouparray[] = $mform->createElement('select', 'draggroup', get_string('group', 'qtype_gapselect'), diff --git a/question/type/ddwtos/tests/behat/edit.feature b/question/type/ddwtos/tests/behat/edit.feature index b0557e96a70..288830234bb 100644 --- a/question/type/ddwtos/tests/behat/edit.feature +++ b/question/type/ddwtos/tests/behat/edit.feature @@ -27,6 +27,9 @@ Feature: Test editing a drag and drop into text questions @javascript Scenario: Edit a drag and drop into text question When I click on "Edit" "link" in the "Drag to text" "table_row" + Then I should see "Choice [[1]]" + And I should see "Choice [[2]]" + And I should see "Choice [[3]]" And I set the following fields to these values: | Question name | Edited question name | And I press "id_submitbutton" diff --git a/question/type/gapselect/edit_form_base.php b/question/type/gapselect/edit_form_base.php index fa99533ef47..72b460061a0 100644 --- a/question/type/gapselect/edit_form_base.php +++ b/question/type/gapselect/edit_form_base.php @@ -182,7 +182,7 @@ class qtype_gapselect_edit_form_base extends question_edit_form { protected function choice_group($mform) { $options = array(); for ($i = 1; $i <= $this->get_maximum_choice_group_number(); $i += 1) { - $options[$i] = $i; + $options[$i] = question_utils::int_to_letter($i); } $grouparray = array(); $grouparray[] = $mform->createElement('text', 'answer', diff --git a/question/type/gapselect/lang/en/qtype_gapselect.php b/question/type/gapselect/lang/en/qtype_gapselect.php index c743a0eeb62..94852405041 100644 --- a/question/type/gapselect/lang/en/qtype_gapselect.php +++ b/question/type/gapselect/lang/en/qtype_gapselect.php @@ -25,7 +25,7 @@ $string['addmorechoiceblanks'] = 'Blanks for {no} more choices'; $string['answer'] = 'Answer'; $string['choices'] = 'Choices'; -$string['choicex'] = 'Choice {no}'; +$string['choicex'] = 'Choice [[{no}]]'; $string['combinedcontrolnamegapselect'] = 'drop down box'; $string['combinedcontrolnamegapselectplural'] = 'drop down boxes'; $string['correctansweris'] = 'The correct answer is: {$a}';