MDL-68615 questions: fix fiendish default idnumber edge case

This commit is contained in:
Tim Hunt 2020-05-05 15:43:57 +01:00
parent cd391f9922
commit 1d47cb2168
2 changed files with 11 additions and 4 deletions

View File

@ -2407,13 +2407,16 @@ function core_question_find_next_unused_idnumber(?string $oldidnumber, int $cate
[$categoryid], '', 'idnumber, 1');
// Find the next unused idnumber.
$newidnumber = $oldidnumber;
$numberbit = 'X' . $matches[0]; // Need a string here so PHP does not do '0001' + 1 = 2.
$stem = substr($oldidnumber, 0, -strlen($matches[0]));
do {
// If we have got to something9999, insert an extra digit before incrementing.
if (preg_match('~^(.*[^0-9])(9+)$~', $newidnumber, $matches)) {
$newidnumber = $matches[1] . '0' . $matches[2];
if (preg_match('~^(.*[^0-9])(9+)$~', $numberbit, $matches)) {
$numberbit = $matches[1] . '0' . $matches[2];
}
$newidnumber++;
$numberbit++;
$newidnumber = $stem . substr($numberbit, 1);
} while (isset($usedidnumbers[$newidnumber]));
return (string) $newidnumber;

View File

@ -2155,6 +2155,10 @@ class core_questionlib_testcase extends advanced_testcase {
['id9', 'id10'],
['id009', 'id010'],
['id999', 'id1000'],
['0', '1'],
['-1', '-2'],
['1.0E+29', '1.0E+30'], // Idnumbers are strings, not floats.
['1.0E-29', '1.0E-30'], // By the way, this is not a sensible idnumber!
];
}