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.
This commit is contained in:
Tim Hunt 2011-11-02 11:46:26 +00:00
parent 78fc17ebdf
commit de529cd253
6 changed files with 53 additions and 16 deletions

View File

@ -1779,7 +1779,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
@ -1800,6 +1800,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;
}

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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 File

@ -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('