Merge branch 'MDL-70041-310' of git://github.com/mihailges/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / essay / edit_essay_form.php
CommitLineData
aeb15530 1<?php
fe6ce234
DC
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
271ffe3f 17/**
18 * Defines the editing form for the essay question type.
19 *
b04a4319
TH
20 * @package qtype
21 * @subpackage essay
22 * @copyright 2007 Jamie Pratt me@jamiep.org
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
271ffe3f 24 */
25
b04a4319 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
271ffe3f 30/**
b04a4319
TH
31 * Essay question type editing form.
32 *
33 * @copyright 2007 Jamie Pratt me@jamiep.org
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
271ffe3f 35 */
6e34cd29 36class qtype_essay_edit_form extends question_edit_form {
894e8b4e
TH
37
38 protected function definition_inner($mform) {
39 $qtype = question_bank::get_qtype('essay');
40
a4f765eb
KT
41 $mform->addElement('header', 'responseoptions', get_string('responseoptions', 'qtype_essay'));
42 $mform->setExpanded('responseoptions');
43
894e8b4e 44 $mform->addElement('select', 'responseformat',
b3ae5457 45 get_string('responseformat', 'qtype_essay'), $qtype->response_formats());
894e8b4e
TH
46 $mform->setDefault('responseformat', 'editor');
47
a4f765eb
KT
48 $mform->addElement('select', 'responserequired',
49 get_string('responserequired', 'qtype_essay'), $qtype->response_required_options());
50 $mform->setDefault('responserequired', 1);
51 $mform->disabledIf('responserequired', 'responseformat', 'eq', 'noinline');
52
894e8b4e 53 $mform->addElement('select', 'responsefieldlines',
b3ae5457 54 get_string('responsefieldlines', 'qtype_essay'), $qtype->response_sizes());
894e8b4e 55 $mform->setDefault('responsefieldlines', 15);
a4f765eb 56 $mform->disabledIf('responsefieldlines', 'responseformat', 'eq', 'noinline');
894e8b4e
TH
57
58 $mform->addElement('select', 'attachments',
59 get_string('allowattachments', 'qtype_essay'), $qtype->attachment_options());
60 $mform->setDefault('attachments', 0);
61
a4f765eb
KT
62 $mform->addElement('select', 'attachmentsrequired',
63 get_string('attachmentsrequired', 'qtype_essay'), $qtype->attachments_required_options());
64 $mform->setDefault('attachmentsrequired', 0);
65 $mform->addHelpButton('attachmentsrequired', 'attachmentsrequired', 'qtype_essay');
66 $mform->disabledIf('attachmentsrequired', 'attachments', 'eq', 0);
67
94cb5a66
LB
68 $mform->addElement('filetypes', 'filetypeslist', get_string('acceptedfiletypes', 'qtype_essay'));
69 $mform->addHelpButton('filetypeslist', 'acceptedfiletypes', 'qtype_essay');
70 $mform->disabledIf('filetypeslist', 'attachments', 'eq', 0);
71
95d1f1f1
MK
72 $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'qtype_essay'), $qtype->max_file_size_options());
73 $mform->setDefault('maxbytes', '0');
74 $mform->disabledIf('maxbytes', 'attachments', 'eq', 0);
75
a4f765eb 76 $mform->addElement('header', 'responsetemplateheader', get_string('responsetemplateheader', 'qtype_essay'));
60527d0c
JMV
77 $mform->addElement('editor', 'responsetemplate', get_string('responsetemplate', 'qtype_essay'),
78 array('rows' => 10), array_merge($this->editoroptions, array('maxfiles' => 0)));
79 $mform->addHelpButton('responsetemplate', 'responsetemplate', 'qtype_essay');
80
a4f765eb
KT
81 $mform->addElement('header', 'graderinfoheader', get_string('graderinfoheader', 'qtype_essay'));
82 $mform->setExpanded('graderinfoheader');
894e8b4e
TH
83 $mform->addElement('editor', 'graderinfo', get_string('graderinfo', 'qtype_essay'),
84 array('rows' => 10), $this->editoroptions);
85 }
86
121fd4c1 87 protected function data_preprocessing($question) {
894e8b4e
TH
88 $question = parent::data_preprocessing($question);
89
90 if (empty($question->options)) {
91 return $question;
92 }
93
94 $question->responseformat = $question->options->responseformat;
a4f765eb 95 $question->responserequired = $question->options->responserequired;
894e8b4e
TH
96 $question->responsefieldlines = $question->options->responsefieldlines;
97 $question->attachments = $question->options->attachments;
a4f765eb 98 $question->attachmentsrequired = $question->options->attachmentsrequired;
94cb5a66 99 $question->filetypeslist = $question->options->filetypeslist;
95d1f1f1 100 $question->maxbytes = $question->options->maxbytes;
894e8b4e
TH
101
102 $draftid = file_get_submitted_draft_itemid('graderinfo');
103 $question->graderinfo = array();
104 $question->graderinfo['text'] = file_prepare_draft_area(
3d9645ae 105 $draftid, // Draftid
894e8b4e
TH
106 $this->context->id, // context
107 'qtype_essay', // component
108 'graderinfo', // filarea
109 !empty($question->id) ? (int) $question->id : null, // itemid
110 $this->fileoptions, // options
3d9645ae 111 $question->options->graderinfo // text.
894e8b4e
TH
112 );
113 $question->graderinfo['format'] = $question->options->graderinfoformat;
114 $question->graderinfo['itemid'] = $draftid;
115
60527d0c
JMV
116 $question->responsetemplate = array(
117 'text' => $question->options->responsetemplate,
118 'format' => $question->options->responsetemplateformat,
119 );
120
894e8b4e
TH
121 return $question;
122 }
123
a4f765eb
KT
124 public function validation($fromform, $files) {
125 $errors = parent::validation($fromform, $files);
126
127 // Don't allow both 'no inline response' and 'no attachments' to be selected,
128 // as these options would result in there being no input requested from the user.
129 if ($fromform['responseformat'] == 'noinline' && !$fromform['attachments']) {
130 $errors['attachments'] = get_string('mustattach', 'qtype_essay');
131 }
132
133 // If 'no inline response' is set, force the teacher to require attachments;
134 // otherwise there will be nothing to grade.
135 if ($fromform['responseformat'] == 'noinline' && !$fromform['attachmentsrequired']) {
136 $errors['attachmentsrequired'] = get_string('mustrequire', 'qtype_essay');
137 }
138
139 // Don't allow the teacher to require more attachments than they allow; as this would
140 // create a condition that it's impossible for the student to meet.
141 if ($fromform['attachments'] != -1 && $fromform['attachments'] < $fromform['attachmentsrequired'] ) {
142 $errors['attachmentsrequired'] = get_string('mustrequirefewer', 'qtype_essay');
143 }
144
145 return $errors;
146 }
147
6d03299d 148 public function qtype() {
271ffe3f 149 return 'essay';
150 }
271ffe3f 151}