Skip to content

Commit

Permalink
MDL-69101 Essay/Moodle XML: recent new settings not imported/exported
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Feb 3, 2021
1 parent 9dabd07 commit 99778a6
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 21 deletions.
17 changes: 16 additions & 1 deletion question/format/xml/format.php
Expand Up @@ -770,10 +770,18 @@ public function import_essay($question) {
array('#', 'responsefieldlines', 0, '#'), 15);
$qo->responserequired = $this->getpath($question,
array('#', 'responserequired', 0, '#'), 1);
$qo->minwordlimit = $this->getpath($question,
array('#', 'minwordlimit', 0, '#'), null);
$qo->maxwordlimit = $this->getpath($question,
array('#', 'maxwordlimit', 0, '#'), null);
$qo->attachments = $this->getpath($question,
array('#', 'attachments', 0, '#'), 0);
$qo->attachmentsrequired = $this->getpath($question,
array('#', 'attachmentsrequired', 0, '#'), 0);
$qo->filetypeslist = $this->getpath($question,
array('#', 'filetypeslist', 0, '#'), null);
$qo->maxbytes = $this->getpath($question,
array('#', 'maxbytes', 0, '#'), null);
$qo->graderinfo = $this->import_text_with_files($question,
array('#', 'graderinfo', 0), '', $this->get_format($qo->questiontextformat));
$qo->responsetemplate['text'] = $this->getpath($question,
Expand Down Expand Up @@ -1168,7 +1176,6 @@ protected function presave_process($content) {
* @return string xml segment
*/
public function writequestion($question) {
global $CFG, $OUTPUT;

$invalidquestion = false;
$fs = get_file_storage();
Expand Down Expand Up @@ -1348,10 +1355,18 @@ public function writequestion($question) {
"</responserequired>\n";
$expout .= " <responsefieldlines>" . $question->options->responsefieldlines .
"</responsefieldlines>\n";
$expout .= " <minwordlimit>" . $question->options->minwordlimit .
"</minwordlimit>\n";
$expout .= " <maxwordlimit>" . $question->options->maxwordlimit .
"</maxwordlimit>\n";
$expout .= " <attachments>" . $question->options->attachments .
"</attachments>\n";
$expout .= " <attachmentsrequired>" . $question->options->attachmentsrequired .
"</attachmentsrequired>\n";
$expout .= " <maxbytes>" . $question->options->maxbytes .
"</maxbytes>\n";
$expout .= " <filetypeslist>" . $question->options->filetypeslist .
"</filetypeslist>\n";
$expout .= " <graderinfo " .
$this->format($question->options->graderinfoformat) . ">\n";
$expout .= $this->writetext($question->options->graderinfo, 3);
Expand Down
Expand Up @@ -69,8 +69,12 @@
<responseformat>editor</responseformat>
<responserequired>1</responserequired>
<responsefieldlines>10</responsefieldlines>
<minwordlimit></minwordlimit>
<maxwordlimit></maxwordlimit>
<attachments>0</attachments>
<attachmentsrequired>0</attachmentsrequired>
<maxbytes>0</maxbytes>
<filetypeslist></filetypeslist>
<graderinfo format="html">
<text></text>
</graderinfo>
Expand Down
83 changes: 83 additions & 0 deletions question/format/xml/tests/xmlformat_test.php
Expand Up @@ -406,8 +406,12 @@ public function test_import_essay_20() {
$expectedq->responseformat = 'editor';
$expectedq->responserequired = 1;
$expectedq->responsefieldlines = 15;
$expectedq->minwordlimit = null;
$expectedq->maxwordlimit = null;
$expectedq->attachments = 0;
$expectedq->attachmentsrequired = 0;
$expectedq->maxbytes = 0;
$expectedq->filetypeslist = null;
$expectedq->graderinfo['text'] = '';
$expectedq->graderinfo['format'] = FORMAT_MOODLE;
$expectedq->responsetemplate['text'] = '';
Expand Down Expand Up @@ -465,8 +469,79 @@ public function test_import_essay_21() {
$expectedq->responseformat = 'monospaced';
$expectedq->responserequired = 0;
$expectedq->responsefieldlines = 42;
$expectedq->minwordlimit = null;
$expectedq->maxwordlimit = null;
$expectedq->attachments = -1;
$expectedq->attachmentsrequired = 1;
$expectedq->maxbytes = 0;
$expectedq->filetypeslist = null;
$expectedq->graderinfo['text'] = '<p>Grade <b>generously</b>!</p>';
$expectedq->graderinfo['format'] = FORMAT_HTML;
$expectedq->responsetemplate['text'] = '<p>Here is something <b>really</b> interesting.</p>';
$expectedq->responsetemplate['format'] = FORMAT_HTML;
$expectedq->tags = array('tagEssay', 'tagEssay21', 'tagTest');

$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
}

public function test_import_essay_311() {
$xml = ' <question type="essay">
<name>
<text>An essay</text>
</name>
<questiontext format="moodle_auto_format">
<text>Write something.</text>
</questiontext>
<generalfeedback>
<text>I hope you wrote something interesting.</text>
</generalfeedback>
<defaultgrade>1</defaultgrade>
<penalty>0</penalty>
<hidden>0</hidden>
<responseformat>monospaced</responseformat>
<responserequired>0</responserequired>
<responsefieldlines>42</responsefieldlines>
<minwordlimit>10</minwordlimit>
<maxwordlimit>20</maxwordlimit>
<attachments>-1</attachments>
<attachmentsrequired>1</attachmentsrequired>
<maxbytes>52428800</maxbytes>
<filetypeslist>.pdf,.zip.,.docx</filetypeslist>
<graderinfo format="html">
<text><![CDATA[<p>Grade <b>generously</b>!</p>]]></text>
</graderinfo>
<responsetemplate format="html">
<text><![CDATA[<p>Here is something <b>really</b> interesting.</p>]]></text>
</responsetemplate>
<tags>
<tag><text>tagEssay</text></tag>
<tag><text>tagEssay21</text></tag>
<tag><text>tagTest</text></tag>
</tags>
</question>';
$xmldata = xmlize($xml);

$importer = new qformat_xml();
$q = $importer->import_essay($xmldata['question']);

$expectedq = new stdClass();
$expectedq->qtype = 'essay';
$expectedq->name = 'An essay';
$expectedq->questiontext = 'Write something.';
$expectedq->questiontextformat = FORMAT_MOODLE;
$expectedq->generalfeedback = 'I hope you wrote something interesting.';
$expectedq->defaultmark = 1;
$expectedq->length = 1;
$expectedq->penalty = 0;
$expectedq->responseformat = 'monospaced';
$expectedq->responserequired = 0;
$expectedq->responsefieldlines = 42;
$expectedq->minwordlimit = 10;
$expectedq->maxwordlimit = 20;
$expectedq->attachments = -1;
$expectedq->attachmentsrequired = 1;
$expectedq->maxbytes = 52428800; // 50MB.
$expectedq->filetypeslist = '.pdf,.zip.,.docx';
$expectedq->graderinfo['text'] = '<p>Grade <b>generously</b>!</p>';
$expectedq->graderinfo['format'] = FORMAT_HTML;
$expectedq->responsetemplate['text'] = '<p>Here is something <b>really</b> interesting.</p>';
Expand Down Expand Up @@ -497,12 +572,16 @@ public function test_export_essay() {
$qdata->options->responseformat = 'monospaced';
$qdata->options->responserequired = 0;
$qdata->options->responsefieldlines = 42;
$qdata->options->minwordlimit = 10;
$qdata->options->maxwordlimit = 20;
$qdata->options->attachments = -1;
$qdata->options->attachmentsrequired = 1;
$qdata->options->graderinfo = '<p>Grade <b>generously</b>!</p>';
$qdata->options->graderinfoformat = FORMAT_HTML;
$qdata->options->responsetemplate = '<p>Here is something <b>really</b> interesting.</p>';
$qdata->options->responsetemplateformat = FORMAT_HTML;
$qdata->options->maxbytes = 52428800; // 50MB.
$qdata->options->filetypeslist = '.pdf,.zip.,.docx';
$exporter = new qformat_xml();
$xml = $exporter->writequestion($qdata);

Expand All @@ -524,8 +603,12 @@ public function test_export_essay() {
<responseformat>monospaced</responseformat>
<responserequired>0</responserequired>
<responsefieldlines>42</responsefieldlines>
<minwordlimit>10</minwordlimit>
<maxwordlimit>20</maxwordlimit>
<attachments>-1</attachments>
<attachmentsrequired>1</attachmentsrequired>
<maxbytes>52428800</maxbytes>
<filetypeslist>.pdf,.zip.,.docx</filetypeslist>
<graderinfo format="html">
<text><![CDATA[<p>Grade <b>generously</b>!</p>]]></text>
</graderinfo>
Expand Down
6 changes: 3 additions & 3 deletions question/type/essay/questiontype.php
Expand Up @@ -65,12 +65,12 @@ public function save_question_options($formdata) {
$options->responseformat = $formdata->responseformat;
$options->responserequired = $formdata->responserequired;
$options->responsefieldlines = $formdata->responsefieldlines;
$options->minwordlimit = isset($formdata->minwordenabled) ? $formdata->minwordlimit : 0;
$options->maxwordlimit = isset($formdata->maxwordenabled) ? $formdata->maxwordlimit : 0;
$options->minwordlimit = isset($formdata->minwordenabled) ? $formdata->minwordlimit : null;
$options->maxwordlimit = isset($formdata->maxwordenabled) ? $formdata->maxwordlimit : null;
$options->attachments = $formdata->attachments;
$options->attachmentsrequired = $formdata->attachmentsrequired;
if (!isset($formdata->filetypeslist)) {
$options->filetypeslist = "";
$options->filetypeslist = null;
} else {
$options->filetypeslist = $formdata->filetypeslist;
}
Expand Down
2 changes: 1 addition & 1 deletion question/type/essay/tests/behat/export.feature
Expand Up @@ -29,7 +29,7 @@ Feature: Test exporting Essay questions
When I navigate to "Question bank > Export" in current page administration
And I set the field "id_format_xml" to "1"
And I press "Export questions to file"
Then following "click here" should download between "2600" and "2850" bytes
Then following "click here" should download between "3000" and "3500" 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
5 changes: 3 additions & 2 deletions question/type/essay/tests/fixtures/testquestion.moodle.xml
Expand Up @@ -25,10 +25,11 @@
<responseformat>editor</responseformat>
<responserequired>1</responserequired>
<responsefieldlines>15</responsefieldlines>
<minwordlimit>0</minwordlimit>
<maxwordlimit>0</maxwordlimit>
<minwordlimit></minwordlimit>
<maxwordlimit></maxwordlimit>
<attachments>0</attachments>
<attachmentsrequired>0</attachmentsrequired>
<maxbytes>0</maxbytes>
<filetypeslist></filetypeslist>
<graderinfo format="html">
<text></text>
Expand Down
19 changes: 7 additions & 12 deletions question/type/essay/tests/helper.php
Expand Up @@ -51,11 +51,12 @@ protected function initialise_essay_question() {
$q->responseformat = 'editor';
$q->responserequired = 1;
$q->responsefieldlines = 10;
$q->minwordlimit = 0;
$q->maxwordlimit = 0;
$q->minwordlimit = null;
$q->maxwordlimit = null;
$q->attachments = 0;
$q->attachmentsrequired = 0;
$q->filetypeslist = '';
$q->maxbytes = 0;
$q->filetypeslist = null;
$q->graderinfo = '';
$q->graderinfoformat = FORMAT_HTML;
$q->qtype = question_bank::get_qtype('essay');
Expand Down Expand Up @@ -88,12 +89,10 @@ public function get_essay_question_form_data_editor() {
$fromform->responseformat = 'editor';
$fromform->responserequired = 1;
$fromform->responsefieldlines = 10;
$fromform->minwordlimit = 0;
$fromform->maxwordlimit = 0;
$fromform->attachments = 0;
$fromform->attachmentsrequired = 0;
$fromform->filetypeslist = '';
$fromform->maxbytes = 0;
$fromform->filetypeslist = ''; // Although once saved in the DB, this becomes null, the form returns '' here.
$fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
$fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);

Expand Down Expand Up @@ -142,12 +141,10 @@ public function get_essay_question_form_data_editorfilepicker() {
$fromform->responseformat = 'editorfilepicker';
$fromform->responserequired = 1;
$fromform->responsefieldlines = 10;
$fromform->minwordlimit = 0;
$fromform->maxwordlimit = 0;
$fromform->attachments = 3;
$fromform->attachmentsrequired = 0;
$fromform->filetypeslist = '';
$fromform->maxbytes = 0;
$fromform->filetypeslist = ''; // Although once saved in the DB, this becomes null, the form returns '' here.
$fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
$fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);

Expand Down Expand Up @@ -181,12 +178,10 @@ public function get_essay_question_form_data_plain() {
$fromform->responseformat = 'plain';
$fromform->responserequired = 1;
$fromform->responsefieldlines = 10;
$fromform->minwordlimit = 0;
$fromform->maxwordlimit = 0;
$fromform->attachments = 0;
$fromform->attachmentsrequired = 0;
$fromform->filetypeslist = '';
$fromform->maxbytes = 0;
$fromform->filetypeslist = ''; // Although once saved in the DB, this becomes null, the form returns '' here.
$fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
$fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);

Expand Down
4 changes: 2 additions & 2 deletions question/type/essay/tests/walkthrough_test.php
Expand Up @@ -519,7 +519,7 @@ public function test_deferred_feedback_html_editor_with_files_attempt_wrong_file

// Start attempt at the question.
$q = question_bank::load_question($question->id);
$q->filetypeslist = ("pdf, docx");
$q->filetypeslist = '.pdf,.docx';
$this->start_attempt_at_question($q, 'deferredfeedback', 1);

$this->check_current_state(question_state::$todo);
Expand Down Expand Up @@ -578,7 +578,7 @@ public function test_deferred_feedback_html_editor_with_files_attempt_correct_fi

// Start attempt at the question.
$q = question_bank::load_question($question->id);
$q->filetypeslist = ("txt, docx");
$q->filetypeslist = '.txt,.docx';
$this->start_attempt_at_question($q, 'deferredfeedback', 1);

$this->check_current_state(question_state::$todo);
Expand Down

0 comments on commit 99778a6

Please sign in to comment.