From 4971015e383a56f4c876cc504b497622cf34a84e Mon Sep 17 00:00:00 2001 From: Mahmoud Kassaei Date: Fri, 2 Apr 2021 10:28:48 +0100 Subject: [PATCH] MDL-71262 Questions: Default options for qtype_essay --- question/type/essay/edit_essay_form.php | 13 +++++----- question/type/essay/questiontype.php | 10 ++++++++ question/type/essay/tests/behat/add.feature | 27 ++++++++++++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/question/type/essay/edit_essay_form.php b/question/type/essay/edit_essay_form.php index 2109827cb33..464b3ae7222 100644 --- a/question/type/essay/edit_essay_form.php +++ b/question/type/essay/edit_essay_form.php @@ -43,16 +43,17 @@ class qtype_essay_edit_form extends question_edit_form { $mform->addElement('select', 'responseformat', get_string('responseformat', 'qtype_essay'), $qtype->response_formats()); - $mform->setDefault('responseformat', 'editor'); + $mform->setDefault('responseformat', $this->get_default_value('responseformat', 'editor')); + $mform->addElement('select', 'responserequired', get_string('responserequired', 'qtype_essay'), $qtype->response_required_options()); - $mform->setDefault('responserequired', 1); + $mform->setDefault('responserequired', $this->get_default_value('responserequired', 1)); $mform->hideIf('responserequired', 'responseformat', 'eq', 'noinline'); $mform->addElement('select', 'responsefieldlines', get_string('responsefieldlines', 'qtype_essay'), $qtype->response_sizes()); - $mform->setDefault('responsefieldlines', 10); + $mform->setDefault('responsefieldlines', $this->get_default_value('responsefieldlines', 10)); $mform->hideIf('responsefieldlines', 'responseformat', 'eq', 'noinline'); // Create a text box that can be enabled/disabled for max/min word limits options. @@ -79,11 +80,11 @@ class qtype_essay_edit_form extends question_edit_form { $mform->addElement('select', 'attachments', get_string('allowattachments', 'qtype_essay'), $qtype->attachment_options()); - $mform->setDefault('attachments', 0); + $mform->setDefault('attachments', $this->get_default_value('attachments', 0)); $mform->addElement('select', 'attachmentsrequired', get_string('attachmentsrequired', 'qtype_essay'), $qtype->attachments_required_options()); - $mform->setDefault('attachmentsrequired', 0); + $mform->setDefault('attachmentsrequired', $this->get_default_value('attachmentsrequired', 0)); $mform->addHelpButton('attachmentsrequired', 'attachmentsrequired', 'qtype_essay'); $mform->hideIf('attachmentsrequired', 'attachments', 'eq', 0); @@ -92,7 +93,7 @@ class qtype_essay_edit_form extends question_edit_form { $mform->hideIf('filetypeslist', 'attachments', 'eq', 0); $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'qtype_essay'), $qtype->max_file_size_options()); - $mform->setDefault('maxbytes', '0'); + $mform->setDefault('maxbytes', $this->get_default_value('maxbytes', 0)); $mform->hideIf('maxbytes', 'attachments', 'eq', 0); $mform->addElement('header', 'responsetemplateheader', get_string('responsetemplateheader', 'qtype_essay')); diff --git a/question/type/essay/questiontype.php b/question/type/essay/questiontype.php index 44cc125d3a6..9f89adee55d 100644 --- a/question/type/essay/questiontype.php +++ b/question/type/essay/questiontype.php @@ -51,6 +51,16 @@ class qtype_essay extends question_type { parent::get_question_options($question); } + public function save_defaults_for_new_questions(stdClass $fromform): void { + parent::save_defaults_for_new_questions($fromform); + $this->set_default_value('responseformat', $fromform->responseformat); + $this->set_default_value('responserequired', $fromform->responserequired); + $this->set_default_value('responsefieldlines', $fromform->responsefieldlines); + $this->set_default_value('attachments', $fromform->attachments); + $this->set_default_value('attachmentsrequired', $fromform->attachmentsrequired); + $this->set_default_value('maxbytes', $fromform->maxbytes); + } + public function save_question_options($formdata) { global $DB; $context = $formdata->context; diff --git a/question/type/essay/tests/behat/add.feature b/question/type/essay/tests/behat/add.feature index a0b57e21eaa..d87c9e4c0fb 100644 --- a/question/type/essay/tests/behat/add.feature +++ b/question/type/essay/tests/behat/add.feature @@ -31,5 +31,30 @@ Feature: Test creating an Essay question | Question name | essay-002 | | Question text | Write an essay with 500 words. | | General feedback | This is general feedback | - | Response format | HTML editor | + | id_responseformat | editorfilepicker | Then I should see "essay-002" + + @javascript + Scenario: Create an Essay question for testing some default options + When I add a "Essay" question filling the form with: + | Question name | essay-003 | + | Question text | Write an essay with 500 words. | + | General feedback | This is general feedback | + | id_responseformat | editorfilepicker | + | id_responserequired | 0 | + | id_responsefieldlines | 15 | + | id_attachments | 2 | + | id_attachmentsrequired | 2 | + | id_maxbytes | 10240 | + Then I should see "essay-003" + # Checking that the next new question form displays user preferences settings. + When I press "Create a new question ..." + And I set the field "item_qtype_essay" to "1" + And I click on "Add" "button" in the "Choose a question type to add" "dialogue" + Then the following fields match these values: + | id_responseformat | editorfilepicker | + | id_responserequired | 0 | + | id_responsefieldlines | 15 | + | id_attachments | 2 | + | id_attachmentsrequired | 2 | + | id_maxbytes | 10240 |