@@ -1284,11 +1284,6 @@ public function validation($data, $files) {
1284
1284
}
1285
1285
}
1286
1286
1287
- $ penaltyregimeerror = $ this ->validate_penalty_regime ($ data );
1288
- if ($ penaltyregimeerror ) {
1289
- $ errors ['markinggroup ' ] = $ penaltyregimeerror ;
1290
- }
1291
-
1292
1287
$ resultcolumnsjson = trim ($ data ['resultcolumns ' ] ?? '' );
1293
1288
if ($ resultcolumnsjson !== '' ) {
1294
1289
$ resultcolumns = json_decode ($ resultcolumnsjson );
@@ -1324,6 +1319,15 @@ public function validation($data, $files) {
1324
1319
$ errors = $ this ->validate_twigables ();
1325
1320
}
1326
1321
1322
+ if (count ($ errors ) == 0 ) {
1323
+ // Penalty regime is Twiggable, so we have postponed validating
1324
+ // until after validate_twigables.
1325
+ $ penaltyregimeerror = $ this ->validate_penalty_regime ($ data );
1326
+ if ($ penaltyregimeerror ) {
1327
+ $ errors ['markinggroup ' ] = $ penaltyregimeerror ;
1328
+ }
1329
+ }
1330
+
1327
1331
if (count ($ errors ) == 0 && !empty ($ data ['validateonsave ' ])) {
1328
1332
$ testresult = $ this ->validate_sample_answer ($ data );
1329
1333
if ($ testresult ) {
@@ -1461,9 +1465,16 @@ private function validate_penalty_regime($data) {
1461
1465
$ errorstring = '' ;
1462
1466
$ expectedpr = '/[0-9]+(\.[0-9]*)?%?([, ] *[0-9]+(\.[0-9]*)?%?)*([, ] *...)?/ ' ;
1463
1467
$ penaltyregime = trim ($ data ['penaltyregime ' ] ?? '' );
1464
- if (preg_match ('/{{.*}}/ ' , $ penaltyregime )) {
1465
- return '' ; // Looks like it's a Twig expression. Hope for the best.
1468
+
1469
+ // If twigall on, we should Twig expand the penalty regime
1470
+ // before attempting to validate it.
1471
+ if ($ penaltyregime && $ data ['twigall ' ]) {
1472
+ $ question = $ this ->formquestion ;
1473
+ $ jsonparams = $ question ->templateparamsevald ;
1474
+ $ parameters = json_decode ($ jsonparams , true );
1475
+ $ penaltyregime = $ this ->twig_render ($ penaltyregime , $ parameters , true );
1466
1476
}
1477
+
1467
1478
if ($ penaltyregime == '' ) {
1468
1479
$ errorstring = get_string ('emptypenaltyregime ' , 'qtype_coderunner ' );
1469
1480
} else if (!preg_match ($ expectedpr , $ penaltyregime )) {
@@ -1505,10 +1516,11 @@ private function validate_twigables() {
1505
1516
$ question = $ this ->formquestion ;
1506
1517
$ jsonparams = $ question ->templateparamsevald ;
1507
1518
$ parameters = json_decode ($ jsonparams , true );
1508
- $ parameters ['QUESTION ' ] = $ question ;
1519
+ $ parameters ['QUESTION ' ] = $ question ; // Shouldn't really include this but removing it might break existing questions.
1509
1520
1510
1521
// Try twig expanding everything (see question::twig_all), with strict_variables true.
1511
- foreach (['questiontext ' , 'answer ' , 'answerpreload ' , 'globalextra ' , 'prototypeextra ' , 'penaltyregime ' ] as $ field ) {
1522
+ $ twigablefields = qtype_coderunner::twigablefields ();
1523
+ foreach ($ twigablefields as $ field ) {
1512
1524
$ text = $ question ->$ field ;
1513
1525
if (is_array ($ text )) {
1514
1526
$ text = $ text ['text ' ];
0 commit comments