Merge branch 'MDL-68562_310' of https://github.com/mkassaei/moodle into MOODLE_310_STABLE

This commit is contained in:
Adrian Greeve 2020-09-30 11:12:34 +08:00
commit 64ba1753a8
12 changed files with 83 additions and 4 deletions

View file

@ -51,7 +51,8 @@ class backup_qtype_essay_plugin extends backup_qtype_plugin {
$essay = new backup_nested_element('essay', array('id'), array(
'responseformat', 'responserequired', 'responsefieldlines',
'attachments', 'attachmentsrequired', 'graderinfo',
'graderinfoformat', 'responsetemplate', 'responsetemplateformat', 'filetypeslist'));
'graderinfoformat', 'responsetemplate', 'responsetemplateformat',
'filetypeslist', 'maxbytes'));
// Now the own qtype tree.
$pluginwrapper->add_child($essay);

View file

@ -17,6 +17,7 @@
<FIELD NAME="graderinfoformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="The text format for graderinfo."/>
<FIELD NAME="responsetemplate" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="The template to pre-populate student's response field during attempt."/>
<FIELD NAME="responsetemplateformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="The text format for responsetemplate."/>
<FIELD NAME="maxbytes" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Maximum size of attached files in bytes."/>
<FIELD NAME="filetypeslist" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="What attachment file type a student is allowed to include with their response. * or empty means unlimited."/>
</FIELDS>
<KEYS>

View file

@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die();
* @param int $oldversion the version we are upgrading from.
*/
function xmldb_qtype_essay_upgrade($oldversion) {
global $CFG;
global $CFG, $DB;
// Automatically generated Moodle v3.5.0 release upgrade line.
// Put any upgrade step following this.
@ -47,5 +47,21 @@ function xmldb_qtype_essay_upgrade($oldversion) {
// Automatically generated Moodle v3.9.0 release upgrade line.
// Put any upgrade step following this.
$dbman = $DB->get_manager();
if ($oldversion < 2020091600) {
// Define field maxbytes to be added to qtype_essay_options.
$table = new xmldb_table('qtype_essay_options');
$field = new xmldb_field('maxbytes', XMLDB_TYPE_INTEGER, '10', null,
XMLDB_NOTNULL, null, '0', 'responsetemplateformat');
// Conditionally launch add field maxbytes.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Essay savepoint reached.
upgrade_plugin_savepoint(true, 2020091600, 'qtype', 'essay');
}
return true;
}

View file

@ -69,6 +69,10 @@ class qtype_essay_edit_form extends question_edit_form {
$mform->addHelpButton('filetypeslist', 'acceptedfiletypes', 'qtype_essay');
$mform->disabledIf('filetypeslist', 'attachments', 'eq', 0);
$mform->addElement('select', 'maxbytes', get_string('maxbytes', 'qtype_essay'), $qtype->max_file_size_options());
$mform->setDefault('maxbytes', '0');
$mform->disabledIf('maxbytes', 'attachments', 'eq', 0);
$mform->addElement('header', 'responsetemplateheader', get_string('responsetemplateheader', 'qtype_essay'));
$mform->addElement('editor', 'responsetemplate', get_string('responsetemplate', 'qtype_essay'),
array('rows' => 10), array_merge($this->editoroptions, array('maxfiles' => 0)));
@ -93,6 +97,7 @@ class qtype_essay_edit_form extends question_edit_form {
$question->attachments = $question->options->attachments;
$question->attachmentsrequired = $question->options->attachmentsrequired;
$question->filetypeslist = $question->options->filetypeslist;
$question->maxbytes = $question->options->maxbytes;
$draftid = file_get_submitted_draft_itemid('graderinfo');
$question->graderinfo = array();

View file

@ -36,6 +36,7 @@ $string['formatnoinline'] = 'No online text';
$string['formatplain'] = 'Plain text';
$string['graderinfo'] = 'Information for graders';
$string['graderinfoheader'] = 'Grader Information';
$string['maxbytes'] = 'Maximum file size';
$string['mustattach'] = 'When "No online text" is selected, or responses are optional, you must allow at least one attachment.';
$string['mustrequire'] = 'When "No online text" is selected, or responses are optional, you must require at least one attachment.';
$string['mustrequirefewer'] = 'You cannot require more attachments than you allow.';

View file

@ -44,6 +44,9 @@ class qtype_essay_question extends question_with_responses {
public $responsefieldlines;
public $attachments;
/** @var int maximum file size in bytes */
public $maxbytes;
/** @var int The number of attachments required for a response to be complete. */
public $attachmentsrequired;

View file

@ -72,6 +72,7 @@ class qtype_essay extends question_type {
} else {
$options->filetypeslist = $formdata->filetypeslist;
}
$options->maxbytes = $formdata->maxbytes ?? 0;
$options->graderinfo = $this->import_or_save_files($formdata->graderinfo,
$context, 'qtype_essay', 'graderinfo', $formdata->id);
$options->graderinfoformat = $formdata->graderinfo['format'];
@ -93,6 +94,7 @@ class qtype_essay extends question_type {
$question->responsetemplateformat = $questiondata->options->responsetemplateformat;
$filetypesutil = new \core_form\filetypes_util();
$question->filetypeslist = $filetypesutil->normalize_file_types($questiondata->options->filetypeslist);
$question->maxbytes = $questiondata->options->maxbytes;
}
public function delete_question($questionid, $contextid) {
@ -162,6 +164,15 @@ class qtype_essay extends question_type {
);
}
/**
* Return array of the choices that should be offered for the maximum file sizes.
* @return array|lang_string[]|string[]
*/
public function max_file_size_options() {
global $CFG, $COURSE;
return get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
}
public function move_files($questionid, $oldcontextid, $newcontextid) {
parent::move_files($questionid, $oldcontextid, $newcontextid);
$fs = get_file_storage();

View file

@ -122,6 +122,7 @@ class qtype_essay_renderer extends qtype_renderer {
$pickeroptions->accepted_types = $qa->get_question()->filetypeslist;
$fm = new form_filemanager($pickeroptions);
$fm->options->maxbytes = $qa->get_question()->maxbytes;;
$filesrenderer = $this->page->get_renderer('core', 'files');
$text = '';

View file

@ -23,3 +23,7 @@
.que.essay div.qtype_essay_response textarea {
width: 100%;
}
.que.essay .ablock .filemanager .fp-restrictions {
margin-top: 1em;
}

View file

@ -0,0 +1,32 @@
@qtype @qtype_essay
Feature: In an essay question, let the question author choose the maxbytes for attachments
In order to constrain student submissions for marking
As a teacher
I need to choose the appropriate maxbytes for attachments
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | T1 | Teacher1 | teacher1@moodle.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following "question categories" exist:
| contextlevel | reference | name |
| Course | C1 | Test questions |
And the following "questions" exist:
| questioncategory | qtype | name | template | attachments | maxbytes |
| Test questions | essay | essay-1-20MB | editor | 1 | 20971520 |
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I navigate to "Question bank" in current page administration
@javascript @_switch_window
Scenario: Preview an Essay question and see the allowed maximum file sizes and number of attachments.
When I choose "Preview" action for "essay-1-20MB" in the question bank
And I switch to "questionpreview" window
And I should see "Please write a story about a frog."
And I should see "Maximum file size: 20MB, maximum number of files: 1"
And I switch to the main window

View file

@ -89,6 +89,7 @@ class qtype_essay_test_helper extends question_test_helper {
$fromform->attachments = 0;
$fromform->attachmentsrequired = 0;
$fromform->filetypeslist = '';
$fromform->maxbytes = 0;
$fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
$fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);
@ -140,6 +141,7 @@ class qtype_essay_test_helper extends question_test_helper {
$fromform->attachments = 3;
$fromform->attachmentsrequired = 0;
$fromform->filetypeslist = '';
$fromform->maxbytes = 0;
$fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
$fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);
@ -176,6 +178,7 @@ class qtype_essay_test_helper extends question_test_helper {
$fromform->attachments = 0;
$fromform->attachmentsrequired = 0;
$fromform->filetypeslist = '';
$fromform->maxbytes = 0;
$fromform->graderinfo = array('text' => '', 'format' => FORMAT_HTML);
$fromform->responsetemplate = array('text' => '', 'format' => FORMAT_HTML);
@ -209,6 +212,7 @@ class qtype_essay_test_helper extends question_test_helper {
$q->attachments = 3;
$q->attachmentsrequired = 1;
$q->filetypeslist = '';
$q->maxbytes = 0;
return $q;
}

View file

@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'qtype_essay';
$plugin->version = 2020061500;
$plugin->version = 2020091600;
$plugin->requires = 2020060900;