Skip to content

Commit

Permalink
MDL-76362 qtype: Update formatting of qtype_calculated* upgradelib
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jan 23, 2023
1 parent 12b36d2 commit 5fbd2ea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 61 deletions.
49 changes: 21 additions & 28 deletions question/type/calculated/db/upgradelib.php
Expand Up @@ -14,25 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Upgrade library code for the calculated question type.
*
* @package qtype
* @subpackage calculated
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


defined('MOODLE_INTERNAL') || die();


/**
* Class for converting attempt data for calculated questions when upgrading
* attempts to the new question engine.
*
* This class is used by the code in question/engine/upgrade/upgradelib.php.
*
* @package qtype_calculated
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand Down Expand Up @@ -86,7 +74,7 @@ protected function parse_response($state) {
throw new coding_exception("Brokes state {$state->id} for calculated
question {$state->question}. (It did not specify a dataset.");
}
list($datasetbit, $realanswer) = explode('-', $state->answer, 2);
[$datasetbit, $realanswer] = explode('-', $state->answer, 2);
$selecteditem = substr($datasetbit, 7);

if (is_null($this->selecteditem)) {
Expand All @@ -98,21 +86,21 @@ protected function parse_response($state) {
}

if (!$realanswer) {
return array('', '');
return ['', ''];
}

if (strpos($realanswer, '|||||') === false) {
$answer = $realanswer;
$unit = '';
} else {
list($answer, $unit) = explode('|||||', $realanswer, 2);
[$answer, $unit] = explode('|||||', $realanswer, 2);
}

return array($answer, $unit);
return [$answer, $unit];
}

public function response_summary($state) {
list($answer, $unit) = $this->parse_response($state);
[$answer, $unit] = $this->parse_response($state);

if (empty($answer) && empty($unit)) {
$resp = null;
Expand Down Expand Up @@ -157,9 +145,11 @@ public function set_data_elements_for_step($state, &$data) {
return;
}

list($answer, $unit) = $this->parse_response($state);
if (!empty($this->question->options->showunits) &&
$this->question->options->showunits == 1) {
[$answer, $unit] = $this->parse_response($state);
if (
!empty($this->question->options->showunits) &&
$this->question->options->showunits == 1
) {
// Multichoice units.
$data['answer'] = $answer;
$data['unit'] = $unit;
Expand All @@ -185,9 +175,9 @@ public function load_dataset($selecteditem) {
$this->question->id, $selecteditem);

// Prepare an array for {@link substitute_values()}.
$this->search = array();
$this->safevalue = array();
$this->prettyvalue = array();
$this->search = [];
$this->safevalue = [];
$this->prettyvalue = [];
foreach ($this->values as $name => $value) {
if (!is_numeric($value)) {
$a = new stdClass();
Expand Down Expand Up @@ -293,10 +283,13 @@ protected function substitute_values_pretty($text) {
*/
public function replace_expressions_in_text($text, $length = null, $format = null) {
$vs = $this; // Can't see to use $this in a PHP closure.
$text = preg_replace_callback('~\{=([^{}]*(?:\{[^{}]+}[^{}]*)*)}~',
function ($matches) use ($vs, $format, $length) {
return $vs->format_float($vs->calculate($matches[1]), $length, $format);
}, $text ?? '');
$text = preg_replace_callback(
'~\{=([^{}]*(?:\{[^{}]+}[^{}]*)*)}~',
function ($matches) use ($vs, $format, $length) {
return $vs->format_float($vs->calculate($matches[1]), $length, $format);
},
$text
);
return $this->substitute_values_pretty($text);
}
}
59 changes: 26 additions & 33 deletions question/type/calculatedmulti/db/upgradelib.php
Expand Up @@ -14,25 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Upgrade library code for the calculated multiple-choice question type.
*
* @package qtype
* @subpackage calculatedmulti
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


defined('MOODLE_INTERNAL') || die();


/**
* Class for converting attempt data for calculated multiple-choice questions
* when upgrading attempts to the new question engine.
*
* This class is used by the code in question/engine/upgrade/upgradelib.php.
*
* @package qtype_calculatedmulti
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand Down Expand Up @@ -69,9 +57,8 @@ public function right_answer() {
return $this->to_text($this->replace_expressions_in_text($ans->answer));
}
}

} else {
$rightbits = array();
$rightbits = [];
foreach ($this->question->options->answers as $ans) {
if ($ans->fraction >= 0.000001) {
$rightbits[] = $this->to_text($this->replace_expressions_in_text($ans->answer));
Expand All @@ -87,7 +74,7 @@ protected function explode_answer($state) {
throw new coding_exception("Brokes state {$state->id} for calcluatedmulti
question {$state->question}. (It did not specify a dataset.");
}
list($datasetbit, $answer) = explode('-', $state->answer, 2);
[$datasetbit, $answer] = explode('-', $state->answer, 2);
$selecteditem = substr($datasetbit, 7);

if (is_null($this->selecteditem)) {
Expand All @@ -99,7 +86,7 @@ protected function explode_answer($state) {
}

if (strpos($answer, ':') !== false) {
list($order, $responses) = explode(':', $answer);
[$order, $responses] = explode(':', $answer);
return $responses;
} else {
// Sometimes, a bug means that a state is missing the <order>: bit,
Expand All @@ -116,7 +103,8 @@ public function response_summary($state) {
if (is_numeric($responses)) {
if (array_key_exists($responses, $this->question->options->answers)) {
return $this->to_text($this->replace_expressions_in_text(
$this->question->options->answers[$responses]->answer));
$this->question->options->answers[$responses]->answer
));
} else {
$this->logger->log_assumption("Dealing with a place where the
student selected a choice that was later deleted for
Expand All @@ -126,15 +114,15 @@ public function response_summary($state) {
} else {
return null;
}

} else {
if (!empty($responses)) {
$responses = explode(',', $responses);
$bits = array();
$bits = [];
foreach ($responses as $response) {
if (array_key_exists($response, $this->question->options->answers)) {
$bits[] = $this->to_text($this->replace_expressions_in_text(
$this->question->options->answers[$response]->answer));
$this->question->options->answers[$response]->answer
));
} else {
$this->logger->log_assumption("Dealing with a place where the
student selected a choice that was later deleted for
Expand All @@ -161,15 +149,16 @@ public function was_answered($state) {
public function set_first_step_data_elements($state, &$data) {
$this->explode_answer($state);
$this->updater->qa->questionsummary = $this->to_text(
$this->replace_expressions_in_text($this->question->questiontext));
$this->replace_expressions_in_text($this->question->questiontext)
);
$this->updater->qa->rightanswer = $this->right_answer($this->question);

foreach ($this->values as $name => $value) {
$data['_var_' . $name] = $value;
}

list($datasetbit, $answer) = explode('-', $state->answer, 2);
list($order, $responses) = explode(':', $answer);
[$datasetbit, $answer] = explode('-', $state->answer, 2);
[$order, $responses] = explode(':', $answer);
$data['_order'] = $order;
$this->order = explode(',', $order);
}
Expand All @@ -189,7 +178,6 @@ public function set_data_elements_for_step($state, &$data) {
$data['answer'] = '-1';
}
}

} else {
$responses = explode(',', $responses);
foreach ($this->order as $key => $ansid) {
Expand All @@ -206,12 +194,14 @@ public function load_dataset($selecteditem) {
$this->selecteditem = $selecteditem;
$this->updater->qa->variant = $selecteditem;
$this->values = $this->qeupdater->load_dataset(
$this->question->id, $selecteditem);
$this->question->id,
$selecteditem
);

// Prepare an array for {@link substitute_values()}.
$this->search = array();
$this->safevalue = array();
$this->prettyvalue = array();
$this->search = [];
$this->safevalue = [];
$this->prettyvalue = [];
foreach ($this->values as $name => $value) {
if (!is_numeric($value)) {
$a = new stdClass();
Expand Down Expand Up @@ -317,10 +307,13 @@ protected function substitute_values_pretty($text) {
*/
public function replace_expressions_in_text($text, $length = null, $format = null) {
$vs = $this; // Can't see to use $this in a PHP closure.
$text = preg_replace_callback(qtype_calculated::FORMULAS_IN_TEXT_REGEX,
function ($matches) use ($vs, $format, $length) {
return $vs->format_float($vs->calculate($matches[1]), $length, $format);
}, $text ?? '');
$text = preg_replace_callback(
qtype_calculated::FORMULAS_IN_TEXT_REGEX,
function ($matches) use ($vs, $format, $length) {
return $vs->format_float($vs->calculate($matches[1]), $length, $format);
},
$text
);
return $this->substitute_values_pretty($text);
}
}

0 comments on commit 5fbd2ea

Please sign in to comment.