Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-30021 qtype numerical better showunits default when coming from 1.9

This applies to:
1. Upgrade from 1.9,
2. Import of questions exported from 1.9, and
3. Restore of courses backed-up from 1.9.

It also applies to the calculated question type.
  • Loading branch information...
commit 8bf2359662ab4e16e2dcaa7ee86e7c12444dfde2 1 parent 5eb4001
@timhunt timhunt authored
View
7 backup/converter/moodle1/handlerlib.php
@@ -1778,7 +1778,7 @@ protected function write_numerical_options(array $numericaloption) {
* @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 @@ protected function get_default_numerical_options($oldquestiontextformat) {
$options['instructionsformat'] = $oldquestiontextformat;
}
+ // Set a good default, depending on whether there are any units defined.
+ if (empty($units)) {
+ $options['showunits'] = 3;
+ }
+
return $options;
}
View
25 question/format/xml/format.php
@@ -628,8 +628,8 @@ public function import_numerical($question) {
}
}
$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 @@ public function import_numerical($question) {
$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 @@ public function import_calculated($question) {
$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 @@ public function import_calculated($question) {
$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;
View
9 question/type/calculated/backup/moodle1/lib.php
@@ -54,12 +54,13 @@ public function process_question(array $data, array $raw) {
// 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);
View
9 question/type/numerical/backup/moodle1/lib.php
@@ -52,12 +52,13 @@ public function process_question(array $data, array $raw) {
// 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);
View
6 question/type/numerical/db/install.xml
@@ -23,10 +23,10 @@
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="question"/>
<FIELD NAME="question" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="showunits"/>
- <FIELD NAME="showunits" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="display units as multichoice" PREVIOUS="question" NEXT="unitsleft"/>
+ <FIELD NAME="showunits" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="How units are handled: 3) Not used at all, 0) Optional, or 1) must be right or penalty applied." PREVIOUS="question" NEXT="unitsleft"/>
<FIELD NAME="unitsleft" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" COMMENT="display the unit at left as in $1.00" PREVIOUS="showunits" NEXT="unitgradingtype"/>
- <FIELD NAME="unitgradingtype" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="0 no penalty, 1 response grade, 2 total grade" PREVIOUS="unitsleft" NEXT="unitpenalty"/>
- <FIELD NAME="unitpenalty" TYPE="number" LENGTH="12" NOTNULL="true" UNSIGNED="true" DEFAULT="0.1" SEQUENCE="false" DECIMALS="7" PREVIOUS="unitgradingtype"/>
+ <FIELD NAME="unitgradingtype" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" COMMENT="0 no penalty, 1 fraction response grade, 2 fraction total grade" PREVIOUS="unitsleft" NEXT="unitpenalty"/>
+ <FIELD NAME="unitpenalty" TYPE="number" LENGTH="12" NOTNULL="true" UNSIGNED="true" DEFAULT="0.1" SEQUENCE="false" DECIMALS="7" COMMENT="Penalty for getting the unit wrong, when they are being graded." PREVIOUS="unitgradingtype"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="question"/>
View
13 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('
Please sign in to comment.
Something went wrong with that request. Please try again.