Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backup/moodle2/backup_qtype_formulas_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ protected function define_question_plugin_structure() {
$formulasanswer = new backup_nested_element('formulas_answer', ['id'], [
'partindex', 'placeholder', 'answermark', 'answertype', 'numbox', 'vars1', 'answer', 'answernotunique', 'vars2',
'correctness', 'unitpenalty', 'postunit', 'ruleid', 'otherrule', 'subqtext', 'subqtextformat', 'feedback',
'feedbackformat', 'partcorrectfb', 'partcorrectfbformat', 'partpartiallycorrectfb', 'partpartiallycorrectfbformat',
'partincorrectfb', 'partincorrectfbformat',
'feedbackformat', 'hidecorrectanswer', 'partcorrectfb', 'partcorrectfbformat', 'partpartiallycorrectfb',
'partpartiallycorrectfbformat', 'partincorrectfb', 'partincorrectfbformat',
]);

// Don't need to annotate ids nor files.
Expand Down
7 changes: 7 additions & 0 deletions backup/moodle2/restore_qtype_formulas_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public function process_formulas_answer($data) {
if (!isset($data->answernotunique)) {
$data->answernotunique = '1';
}
// Older backups might not yet have the hidecorrectanswer field.
if (!isset($data->hidecorrectanswer)) {
$data->hidecorrectanswer = '0';
}
// Insert record.
$newitemid = $DB->insert_record('qtype_formulas_answers', $data);
// Create mapping.
Expand Down Expand Up @@ -194,6 +198,9 @@ public static function convert_backup_to_questiondata(array $backupdata): stdCla
if (!key_exists('answernotunique', $answer)) {
$answer['answernotunique'] = '1';
}
if (!key_exists('hidecorrectanswer', $answer)) {
$answer['hidecorrectanswer'] = '0';
}
if (!key_exists('partindex', $answer)) {
$answer['partindex'] = $i;
}
Expand Down
3 changes: 3 additions & 0 deletions classes/local/formulas_part.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class formulas_part {
/** @var int format constant (FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN or FORMAT_MARKDOWN) */
public int $feedbackformat;

/** @var int whether the correct answer should be hidden from the feedback */
public int $hidecorrectanswer;

/** @var string part's feedback for any correct response */
public string $partcorrectfb;

Expand Down
3 changes: 2 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="question/type/formulas/db" VERSION="20231008" COMMENT="XMLDB file for Moodle question/type/formulas"
<XMLDB PATH="question/type/formulas/db" VERSION="20260731" COMMENT="XMLDB file for Moodle question/type/formulas"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -46,6 +46,7 @@
<FIELD NAME="subqtextformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="feedback" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="feedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="hidecorrectanswer" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="partcorrectfb" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="partcorrectfbformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="partpartiallycorrectfb" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
Expand Down
16 changes: 16 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,5 +546,21 @@ function xmldb_qtype_formulas_upgrade($oldversion = 0) {
upgrade_plugin_savepoint(true, 2023100800, 'qtype', 'formulas');
}

if ($oldversion < 2026073100) {
// Define field hidecorrectanswer to be added to qtype_formulas_answers.
$table = new xmldb_table('qtype_formulas_answers');
$field = new xmldb_field('hidecorrectanswer', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'feedbackformat');

// Conditionally add field.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
// Now fill it with '1' for compatibility with existing questions'.
$DB->set_field('qtype_formulas_answers', 'hidecorrectanswer', '0');
}

// Formulas savepoint reached.
upgrade_plugin_savepoint(true, 2026073100, 'qtype', 'formulas');
}

return true;
}
8 changes: 8 additions & 0 deletions edit_formulas_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ protected function get_per_answer_fields(
);
$repeatedoptions['feedback']['helpbutton'] = ['feedback', 'qtype_formulas'];
$repeatedoptions['feedback']['advanced'] = true;
// Option to hide the correct answer, e. g. because it is included in the general feedback.
$repeated[] = $mform->createElement(
'advcheckbox',
'hidecorrectanswer',
get_string('hidecorrectanswer', 'qtype_formulas'),
);
$repeatedoptions['hidecorrectanswer']['helpbutton'] = ['hidecorrectanswer', 'qtype_formulas'];
$repeatedoptions['hidecorrectanswer']['advanced'] = true;
// Part's combined feedback.
$repeated[] = $mform->createElement(
'editor',
Expand Down
2 changes: 2 additions & 0 deletions lang/en/qtype_formulas.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@
$string['feedback'] = 'Part general feedback';
$string['feedback_help'] = 'This part feedback will be shown to all students. It can include global and local variables that will be replaced by their values.';
$string['globalvarshdr'] = 'Variables';
$string['hidecorrectanswer'] = 'Do not show the correct answer along with the part\'s general feedback.';
$string['hidecorrectanswer_help'] = 'In some cases, the model answer might not be displayed in a way that suits the teacher\'s needs, e. g. for more elaborate questions. The teacher might then use the general feedback to displayed the correct answer according to their wishes. In that case, it can make sense to hide the standard "correct answer" message to avoid redundancy and confusion.';
$string['incorrectfeedback'] = 'For any incorrect response';
$string['incorrectfeedback_help'] = 'This feedback will be shown to students that don\'t get any mark at this part. It can include global and local variables that will be replaced by their values.';
$string['instantiate'] = 'Instantiate';
Expand Down
15 changes: 11 additions & 4 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ class qtype_formulas extends question_type {
* - numbox: number of answers for this part, not including a possible unit field
* - vars1: the part's local variables
* - answer: the model answer(s) for this part
* - answernotunique: whether there is more than one correct answer
* - vars2: the part's grading variables
* - correctness: the part's grading criterion
* - unitpenalty: deduction to be made for wrong units
* - postunit: the unit in which the model answer has been entered
* - ruleid: ruleset used for unit conversion
* - otherrule: additional rules for unit conversion
* - hidecorrectanswer: whether the correct answer should be shown along with the general feedback
*/
const PART_BASIC_FIELDS = ['placeholder', 'answermark', 'answertype', 'numbox', 'vars1', 'answer', 'answernotunique', 'vars2',
'correctness', 'unitpenalty', 'postunit', 'ruleid', 'otherrule'];
'correctness', 'unitpenalty', 'postunit', 'ruleid', 'otherrule', 'hidecorrectanswer'];

/**
* This function returns the "simple" additional fields defined in the qtype_formulas_options
Expand Down Expand Up @@ -287,6 +289,7 @@ public function save_question_options($formdata) {
'answernotunique' => 1,
'correctness' => '',
'ruleid' => 1,
'hidecorrectanswer' => 0,
'subqtext' => '',
'subqtextformat' => FORMAT_HTML,
'feedback' => '',
Expand Down Expand Up @@ -623,16 +626,20 @@ public function import_from_xml($xml, $question, qformat_xml $format, $extra = n
$question->partindex[$i] = $partindex;
}
foreach (self::PART_BASIC_FIELDS as $field) {
// Older questions do not have this field, so we do not want to issue an error message.
// Also, for maximum backwards compatibility, we set the default value to 1. With this,
// nothing changes for old questions.
// Older questions do not have these fields, so we do not want to issue an error message.
// Also, for maximum backwards compatibility, we set the default value to 1 for 'anwernotunique'
// and to 0 for 'hidecorrectanswer'. With this, nothing changes for old questions.
if ($field === 'answernotunique') {
$ifnotexists = '';
$default = '1';
} else if ($field === 'hidecorrectanswer') {
$ifnotexists = '';
$default = '0';
} else {
$ifnotexists = get_string('error_import_missing_field', 'qtype_formulas', $field);
$default = '0';
}

$question->{$field}[$i] = $format->getpath(
$part,
['#', $field, 0, '#', 'text', 0, '#'],
Expand Down
4 changes: 4 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,10 @@ public function correct_response(question_attempt $qa) {
* @return string HTML fragment
*/
public function part_correct_response($part) {
if ($part->hidecorrectanswer) {
return '';
}

$answers = $part->get_correct_response(true);
$answertext = implode('; ', $answers);

Expand Down
3 changes: 3 additions & 0 deletions tests/backup_restore_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ public function test_restore_quiz_with_same_stamp_questions(string $questionname
*/
public static function provide_xml_keys_to_remove(): array {
return [
['hidecorrectanswer'],
['answernotunique'],
['partindex'],
];
Expand Down Expand Up @@ -441,6 +442,7 @@ public function test_restore_of_legacy_backup_with_missing_fields(): void {
'shownumcorrect' => 0,
'answernumbering' => 'none',
'feedbackformat' => FORMAT_HTML,
'hidecorrectanswer' => '0',
'partindex' => null,
'correctfeedback' => '',
'partiallycorrectfeedback' => '',
Expand Down Expand Up @@ -1097,6 +1099,7 @@ public static function provide_edited_option_fields(): array {
['otherrule', '60 s = 1 min'],
['subqtext', 'edited'],
['feedback', 'edited'],
['hidecorrectanswer', '1'],
['partcorrectfb', 'edited'],
['partpartiallycorrectfb', 'edited'],
['partincorrectfb', 'edited'],
Expand Down
2 changes: 1 addition & 1 deletion tests/behat/export.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Feature: Test exporting Formulas questions
When I am on the "Course 1" "core_question > course question export" page
And I set the field "id_format_xml" to "1"
And I press "Export questions to file"
Then following "click here" should download between "6250" and "6500" bytes
Then following "click here" should download between "6500" and "6750" bytes
# If the download step is the last in the scenario then we can sometimes run
# into the situation where the download page causes a http redirect but behat
# has already conducted its reset (generating an error). By putting a logout
Expand Down
Loading
Loading