MDL-76362 qtype_calculated*: Address issues with null strings

This commit is contained in:
Andrew Nicols 2023-01-06 22:26:45 +08:00
parent 5fbd2eac32
commit 71c1fa0d8e
2 changed files with 19 additions and 12 deletions

View File

@ -27,22 +27,22 @@
class qtype_calculated_qe2_attempt_updater extends question_qtype_attempt_updater {
protected $selecteditem = null;
/** @var array variable name => value */
protected $values;
protected $values = [];
/** @var array variable names wrapped in {...}. Used by {@link substitute_values()}. */
protected $search;
protected $search = [];
/**
* @var array variable values, with negative numbers wrapped in (...).
* Used by {@link substitute_values()}.
*/
protected $safevalue;
protected $safevalue = [];
/**
* @var array variable values, with negative numbers wrapped in (...).
* Used by {@link substitute_values()}.
*/
protected $prettyvalue;
protected $prettyvalue = [];
public function question_summary() {
return ''; // Done later, after we know which dataset is used.
@ -260,7 +260,7 @@ class qtype_calculated_qe2_attempt_updater extends question_qtype_attempt_update
* corresponding value.
*/
protected function substitute_values_for_eval($expression) {
return str_replace($this->search ?? '', $this->safevalue ?? '', $expression ?? '');
return str_replace($this->search, $this->safevalue, $expression ?? '');
}
/**
@ -272,7 +272,7 @@ class qtype_calculated_qe2_attempt_updater extends question_qtype_attempt_update
* corresponding value.
*/
protected function substitute_values_pretty($text) {
return str_replace($this->search ?? '', $this->prettyvalue ?? '', $text ?? '');
return str_replace($this->search, $this->prettyvalue, $text ?? '');
}
/**
@ -282,6 +282,9 @@ class qtype_calculated_qe2_attempt_updater extends question_qtype_attempt_update
* @return string the text with values substituted.
*/
public function replace_expressions_in_text($text, $length = null, $format = null) {
if ($text === null || $text === '') {
return $text;
}
$vs = $this; // Can't see to use $this in a PHP closure.
$text = preg_replace_callback(
'~\{=([^{}]*(?:\{[^{}]+}[^{}]*)*)}~',

View File

@ -27,22 +27,22 @@
class qtype_calculatedmulti_qe2_attempt_updater extends question_qtype_attempt_updater {
protected $selecteditem = null;
/** @var array variable name => value */
protected $values;
protected $values = [];
/** @var array variable names wrapped in {...}. Used by {@link substitute_values()}. */
protected $search;
protected $search = [];
/**
* @var array variable values, with negative numbers wrapped in (...).
* Used by {@link substitute_values()}.
*/
protected $safevalue;
protected $safevalue = [];
/**
* @var array variable values, with negative numbers wrapped in (...).
* Used by {@link substitute_values()}.
*/
protected $prettyvalue;
protected $prettyvalue = [];
protected $order;
@ -284,7 +284,7 @@ class qtype_calculatedmulti_qe2_attempt_updater extends question_qtype_attempt_u
* corresponding value.
*/
protected function substitute_values_for_eval($expression) {
return str_replace($this->search ?? '', $this->safevalue ?? '', $expression ?? '');
return str_replace($this->search, $this->safevalue, $expression ?? '');
}
/**
@ -296,7 +296,7 @@ class qtype_calculatedmulti_qe2_attempt_updater extends question_qtype_attempt_u
* corresponding value.
*/
protected function substitute_values_pretty($text) {
return str_replace($this->search ?? '', $this->prettyvalue ?? '', $text ?? '');
return str_replace($this->search, $this->prettyvalue, $text ?? '');
}
/**
@ -306,6 +306,10 @@ class qtype_calculatedmulti_qe2_attempt_updater extends question_qtype_attempt_u
* @return string the text with values substituted.
*/
public function replace_expressions_in_text($text, $length = null, $format = null) {
if ($text === null || $text === '') {
return $text;
}
$vs = $this; // Can't see to use $this in a PHP closure.
$text = preg_replace_callback(
qtype_calculated::FORMULAS_IN_TEXT_REGEX,