Skip to content

Commit f45ff72

Browse files
committed
integrate new option in question data
1 parent cd38100 commit f45ff72

File tree

9 files changed

+92
-19
lines changed

9 files changed

+92
-19
lines changed

backup/moodle2/backup_qtype_formulas_plugin.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ protected function define_question_plugin_structure() {
5151

5252
$formulasanswers = new backup_nested_element('formulas_answers');
5353
$formulasanswer = new backup_nested_element('formulas_answer', ['id'], [
54-
'partindex', 'placeholder', 'answermark', 'answertype', 'numbox', 'vars1', 'answer', 'answernotunique', 'vars2',
55-
'correctness', 'unitpenalty', 'postunit', 'ruleid', 'otherrule', 'subqtext', 'subqtextformat', 'feedback',
54+
'partindex', 'placeholder', 'answermark', 'answertype', 'numbox', 'vars1', 'answer', 'answernotunique', 'emptyallowed',
55+
'vars2', 'correctness', 'unitpenalty', 'postunit', 'ruleid', 'otherrule', 'subqtext', 'subqtextformat', 'feedback',
5656
'feedbackformat', 'partcorrectfb', 'partcorrectfbformat', 'partpartiallycorrectfb', 'partpartiallycorrectfbformat',
57-
'partincorrectfb', 'partincorrectfbformat', 'emptyallowed',
57+
'partincorrectfb', 'partincorrectfbformat'
5858
]);
5959

6060
// Don't need to annotate ids nor files.

backup/moodle2/restore_qtype_formulas_plugin.class.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ public function process_formulas_answer($data) {
149149
if (!isset($data->answernotunique)) {
150150
$data->answernotunique = '1';
151151
}
152+
// Backups prior to 6.2 do not yet have the emptyallowed field. In that case, we set it
153+
// to false. It should default to true for *new* questions only.
154+
if (!isset($data->emptyallowed)) {
155+
$data->emptyallowed = '0';
156+
}
152157
// Insert record.
153158
$newitemid = $DB->insert_record('qtype_formulas_answers', $data);
154159
// Create mapping.
@@ -195,6 +200,9 @@ public static function convert_backup_to_questiondata(array $backupdata): stdCla
195200
if (!key_exists('answernotunique', $answer)) {
196201
$answer['answernotunique'] = '1';
197202
}
203+
if (!key_exists('emptyallowed', $answer)) {
204+
$answer['emptyallowed'] = '0';
205+
}
198206
if (!key_exists('partindex', $answer)) {
199207
$answer['partindex'] = $i;
200208
}

db/install.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<FIELD NAME="vars1" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
3737
<FIELD NAME="answer" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
3838
<FIELD NAME="answernotunique" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
39+
<FIELD NAME="emptyallowed" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
3940
<FIELD NAME="vars2" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
4041
<FIELD NAME="correctness" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
4142
<FIELD NAME="unitpenalty" TYPE="float" NOTNULL="false" SEQUENCE="false"/>

db/upgrade.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,5 +396,21 @@ function xmldb_qtype_formulas_upgrade($oldversion = 0) {
396396
upgrade_plugin_savepoint(true, 2023100800, 'qtype', 'formulas');
397397
}
398398

399+
if ($oldversion < 2025080100) {
400+
// Define field emptyallowed to be added to qtype_formulas_answers.
401+
$table = new xmldb_table('qtype_formulas_answers');
402+
$field = new xmldb_field('emptyallowed', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'answernotunique');
403+
404+
// Conditionally add field.
405+
if (!$dbman->field_exists($table, $field)) {
406+
$dbman->add_field($table, $field);
407+
// Now fill it with '0' for compatibility with existing questions.
408+
$DB->set_field('qtype_formulas_answers', 'emptyallowed', '0');
409+
}
410+
411+
// Formulas savepoint reached.
412+
upgrade_plugin_savepoint(true, 2025080100, 'qtype', 'formulas');
413+
}
414+
399415
return true;
400416
}

edit_formulas_form.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,6 @@ protected function data_preprocessing($question) {
328328
foreach ($question->options->answers as $key => $answer) {
329329

330330
foreach ($tags as $tag) {
331-
// FIXME: remove this later, when the DB is updated
332-
if ($tag === 'emptyallowed') {
333-
continue;
334-
}
335331
if ($tag === 'unitpenalty' || $tag === 'ruleid') {
336332
$defaultvalues['global' . $tag] = $answer->$tag;
337333
} else {

question.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,7 @@ class qtype_formulas_part {
886886
public int $answernotunique;
887887

888888
/** @var int whether students can leave one or more fields empty */
889-
public int $emptyallowed = 1;
890-
// FIXME: change this once the DB is updated
889+
public int $emptyallowed;
891890

892891
/** @var string definition of the grading criterion */
893892
public string $correctness;

questiontype.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -616,12 +616,16 @@ public function import_from_xml($xml, $question, qformat_xml $format, $extra = n
616616
$question->partindex[$i] = $partindex;
617617
}
618618
foreach (self::PART_BASIC_FIELDS as $field) {
619-
// Older questions do not have this field, so we do not want to issue an error message.
620-
// Also, for maximum backwards compatibility, we set the default value to 1. With this,
621-
// nothing changes for old questions.
619+
// Older questions do not have the 'answernotunique' field, so we do not want to issue an
620+
// error message. For maximum backwards compatibility, we set the default value to 1. With
621+
// this, nothing changes for old questions. Also, questions exported prior to version 6.2
622+
// won't have the 'emptyallowed' field. We will set it to 0 (false) for backwards compatiblity.
622623
if ($field === 'answernotunique') {
623624
$ifnotexists = '';
624625
$default = '1';
626+
} else if ($field === 'emptyallowed') {
627+
$ifnotexists = '';
628+
$default = '0';
625629
} else {
626630
$ifnotexists = get_string('error_import_missing_field', 'qtype_formulas', $field);
627631
$default = '0';
@@ -861,11 +865,6 @@ public function check_and_filter_parts(object $data): object {
861865
$partdata[$i] = (object)['questionid' => $data->id];
862866
// Set the basic fields, e.g. mark, placeholder or definition of local variables.
863867
foreach (self::PART_BASIC_FIELDS as $field) {
864-
// FIXME: remove this later, when the DB is updated
865-
if ($field === 'emptyallowed') {
866-
$partdata[$i]->emptyallowed = 1;
867-
continue;
868-
}
869868
// In the edit form, the part's 'unitpenalty' and 'ruleid' are set via the global options
870869
// 'globalunitpenalty' and 'globalruleid'. When importing a question, they do not need
871870
// special treatment, because they are already stored with the part. Also, all other fields

0 commit comments

Comments
 (0)