Skip to content

Commit

Permalink
MDL-52553 question: Adding missing required constants
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Dec 18, 2015
1 parent 0dfcc25 commit 342f1ab
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions question/type/multianswer/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,42 @@ function xmldb_qtype_multianswer_upgrade($oldversion) {
// Put any upgrade step following this.

if ($oldversion < 2015100201) {

// Upgrade steps need to be isolated from our APIs, so we should prevent using constants that
// can change over time. This is a simple copy & paste from question/type/multianswer/questiontype.php
// with the following changes, the idea is to prevent regressions as much as possible.
// * All constants prefixed with UPGRADE_2015100201_
// * Removed all constants not required by UPGRADE_2015100201_ANSWER_REGEX
if (!defined('UPGRADE_2015100201_ANSWER_REGEX')) {
define('UPGRADE_2015100201_ANSWER_ALTERNATIVE_FRACTION_REGEX',
'=|%(-?[0-9]+)%');
define('UPGRADE_2015100201_ANSWER_ALTERNATIVE_ANSWER_REGEX',
'.+?(?<!\\\\|&|&amp;)(?=[~#}]|$)');
define('UPGRADE_2015100201_ANSWER_ALTERNATIVE_FEEDBACK_REGEX',
'.*?(?<!\\\\)(?=[~}]|$)');
define('UPGRADE_2015100201_ANSWER_ALTERNATIVE_REGEX',
'(' . UPGRADE_2015100201_ANSWER_ALTERNATIVE_FRACTION_REGEX .')?' .
'(' . UPGRADE_2015100201_ANSWER_ALTERNATIVE_ANSWER_REGEX . ')' .
'(#(' . UPGRADE_2015100201_ANSWER_ALTERNATIVE_FEEDBACK_REGEX .'))?');

// Remaining ANSWER regexes.
define('UPGRADE_2015100201_ANSWER_TYPE_DEF_REGEX',
'(NUMERICAL|NM)|(MULTICHOICE|MC)|(MULTICHOICE_V|MCV)|(MULTICHOICE_H|MCH)|' .
'(SHORTANSWER|SA|MW)|(SHORTANSWER_C|SAC|MWC)|' .
'(MULTICHOICE_S|MCS)|(MULTICHOICE_VS|MCVS)|(MULTICHOICE_HS|MCHS)');
define('UPGRADE_2015100201_ANSWER_START_REGEX',
'\{([0-9]*):(' . UPGRADE_2015100201_ANSWER_TYPE_DEF_REGEX . '):');

define('UPGRADE_2015100201_ANSWER_REGEX',
UPGRADE_2015100201_ANSWER_START_REGEX
. '(' . UPGRADE_2015100201_ANSWER_ALTERNATIVE_REGEX
. '(~'
. UPGRADE_2015100201_ANSWER_ALTERNATIVE_REGEX
. ')*)\}');

define('UPGRADE_2015100201_ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE', 4);
}

// Detect the exact table/field we want to use, coz can be different
// depending of the version we are upgrading from. See MDL-52291 and MDL-52298.
$multichoicetable = 'qtype_multichoice_options';
Expand All @@ -82,9 +118,9 @@ function xmldb_qtype_multianswer_upgrade($oldversion) {
if ($wrapped->qtype == 'multichoice') {
$options = $DB->get_record($multichoicetable, array($multichoicefield => $wrapped->id), '*');
if (isset($options->shuffleanswers)) {
preg_match('/'.ANSWER_REGEX.'/s', $wrapped->questiontext, $answerregs);
if (isset($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE]) &&
$answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE] !== '') {
preg_match('/'.UPGRADE_2015100201_ANSWER_REGEX.'/s', $wrapped->questiontext, $answerregs);
if (isset($answerregs[UPGRADE_2015100201_ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE]) &&
$answerregs[UPGRADE_2015100201_ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE] !== '') {
$DB->set_field($multichoicetable, 'shuffleanswers', '0',
array('id' => $options->id) );
}
Expand Down

0 comments on commit 342f1ab

Please sign in to comment.