diff --git a/backup/converter/moodle1/handlerlib.php b/backup/converter/moodle1/handlerlib.php
index 3e8283a0899..66bf1f1679f 100644
--- a/backup/converter/moodle1/handlerlib.php
+++ b/backup/converter/moodle1/handlerlib.php
@@ -1778,7 +1778,7 @@ abstract class moodle1_qtype_handler extends moodle1_plugin_handler {
* @param int $oldquestiontextformat
* @return array
*/
- protected function get_default_numerical_options($oldquestiontextformat) {
+ protected function get_default_numerical_options($oldquestiontextformat, $units) {
global $CFG;
// replay the upgrade step 2009100100 - new table
@@ -1799,6 +1799,11 @@ abstract class moodle1_qtype_handler extends moodle1_plugin_handler {
$options['instructionsformat'] = $oldquestiontextformat;
}
+ // Set a good default, depending on whether there are any units defined.
+ if (empty($units)) {
+ $options['showunits'] = 3;
+ }
+
return $options;
}
diff --git a/question/format/xml/format.php b/question/format/xml/format.php
index b744ce527a6..5017cd56607 100644
--- a/question/format/xml/format.php
+++ b/question/format/xml/format.php
@@ -628,8 +628,8 @@ class qformat_xml extends qformat_default {
}
}
$qo->unitgradingtype = $this->getpath($question, array('#', 'unitgradingtype', 0, '#'), 0);
- $qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), 0);
- $qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), 0);
+ $qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), 0.1);
+ $qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), null);
$qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
$qo->instructions['text'] = '';
$qo->instructions['format'] = FORMAT_HTML;
@@ -644,6 +644,15 @@ class qformat_xml extends qformat_default {
$instructions, array('0', '#', 'file'), array()));
}
+ if (is_null($qo->showunits)) {
+ // Set a good default, depending on whether there are any units defined.
+ if (empty($qo->unit)) {
+ $qo->showunits = 3; // qtype_numerical::UNITNONE;
+ } else {
+ $qo->showunits = 0; // qtype_numerical::UNITOPTIONAL;
+ }
+ }
+
$this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
return $qo;
@@ -737,7 +746,7 @@ class qformat_xml extends qformat_default {
$qo->unitgradingtype = $this->getpath($question,
array('#', 'unitgradingtype', 0, '#'), 0);
- $qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), 0);
+ $qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), null);
$qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), 0);
$qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
$qo->instructions = $this->getpath($question,
@@ -801,6 +810,16 @@ class qformat_xml extends qformat_default {
$qo->instructions['files'] = $this->import_files($this->getpath($instructions,
array('0', '#', 'file'), array()));
}
+
+ if (is_null($qo->unitpenalty)) {
+ // Set a good default, depending on whether there are any units defined.
+ if (empty($qo->unit)) {
+ $qo->showunits = 3; // qtype_numerical::UNITNONE;
+ } else {
+ $qo->showunits = 0; // qtype_numerical::UNITOPTIONAL;
+ }
+ }
+
$datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
$qo->dataset = array();
$qo->datasetindex= 0;
diff --git a/question/type/calculated/backup/moodle1/lib.php b/question/type/calculated/backup/moodle1/lib.php
index ede1a567895..f2ad04e24cf 100644
--- a/question/type/calculated/backup/moodle1/lib.php
+++ b/question/type/calculated/backup/moodle1/lib.php
@@ -54,12 +54,13 @@ class moodle1_qtype_calculated_handler extends moodle1_qtype_handler {
// convert and write the numerical units and numerical options
if (isset($data['calculated'][0]['numerical_units'])) {
- $numericalunits = $data['calculated'][0]['numerical_units'];
- $numericaloptions = $this->get_default_numerical_options($data['oldquestiontextformat']);
+ $numericalunits = $data['calculated'][0]['numerical_units'];
} else {
- $numericalunits = array();
- $numericaloptions = array();
+ $numericalunits = array();
}
+ $numericaloptions = $this->get_default_numerical_options(
+ $data['oldquestiontextformat'], $numericalunits);
+
$this->write_numerical_units($numericalunits);
$this->write_numerical_options($numericaloptions);
diff --git a/question/type/numerical/backup/moodle1/lib.php b/question/type/numerical/backup/moodle1/lib.php
index eb244584727..06b56983f47 100644
--- a/question/type/numerical/backup/moodle1/lib.php
+++ b/question/type/numerical/backup/moodle1/lib.php
@@ -52,12 +52,13 @@ class moodle1_qtype_numerical_handler extends moodle1_qtype_handler {
// convert and write the numerical units and numerical options
if (isset($data['numerical'][0]['numerical_units'])) {
- $numericalunits = $data['numerical'][0]['numerical_units'];
- $numericaloptions = $this->get_default_numerical_options($data['oldquestiontextformat']);
+ $numericalunits = $data['numerical'][0]['numerical_units'];
} else {
- $numericalunits = array();
- $numericaloptions = array();
+ $numericalunits = array();
}
+ $numericaloptions = $this->get_default_numerical_options(
+ $data['oldquestiontextformat'], $numericalunits);
+
$this->write_numerical_units($numericalunits);
$this->write_numerical_options($numericaloptions);
diff --git a/question/type/numerical/db/install.xml b/question/type/numerical/db/install.xml
index 21b76313ecf..15dcb777298 100644
--- a/question/type/numerical/db/install.xml
+++ b/question/type/numerical/db/install.xml
@@ -23,10 +23,10 @@
-
+
-
-
+
+
diff --git a/question/type/numerical/db/upgrade.php b/question/type/numerical/db/upgrade.php
index 291b6fef48a..09550c69ac4 100644
--- a/question/type/numerical/db/upgrade.php
+++ b/question/type/numerical/db/upgrade.php
@@ -61,11 +61,22 @@ function xmldb_qtype_numerical_upgrade($oldversion) {
// Adding keys to table question_numerical_options
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('question', XMLDB_KEY_FOREIGN, array('question'), 'question', array('id'));
+
// Conditionally launch create table for question_calculated_options
if (!$dbman->table_exists($table)) {
// $dbman->create_table doesnt return a result, we just have to trust it
$dbman->create_table($table);
}
+
+ // Set a better default for questions without units.
+ $DB->execute('
+ UPDATE {question_numerical_options} qno
+ SET showunits = 3
+ WHERE NOT EXISTS (
+ SELECT 1
+ FROM {question_numerical_units} qnu
+ WHERE qnu.question = qno.question)');
+
upgrade_plugin_savepoint(true, 2009100100, 'qtype', 'numerical');
}
@@ -81,7 +92,7 @@ function xmldb_qtype_numerical_upgrade($oldversion) {
$dbman->add_field($table, $field);
}
- // In the past, question_match_sub.questiontext assumed to contain
+ // In the past, question_numerical_options.instructions assumed to contain
// content of the same form as question.questiontextformat. If we are
// using the HTML editor, then convert FORMAT_MOODLE content to FORMAT_HTML.
$rs = $DB->get_recordset_sql('