weekly release 3.5dev
[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
68 $mform->addElement('header', 'responsetemplateheader', get_string('responsetemplateheader', 'qtype_essay'));
60527d0c
JMV
69 $mform->addElement('editor', 'responsetemplate', get_string('responsetemplate', 'qtype_essay'),
70 array('rows' => 10), array_merge($this->editoroptions, array('maxfiles' => 0)));
71 $mform->addHelpButton('responsetemplate', 'responsetemplate', 'qtype_essay');
72
a4f765eb
KT
73 $mform->addElement('header', 'graderinfoheader', get_string('graderinfoheader', 'qtype_essay'));
74 $mform->setExpanded('graderinfoheader');
894e8b4e
TH
75 $mform->addElement('editor', 'graderinfo', get_string('graderinfo', 'qtype_essay'),
76 array('rows' => 10), $this->editoroptions);
77 }
78
121fd4c1 79 protected function data_preprocessing($question) {
894e8b4e
TH
80 $question = parent::data_preprocessing($question);
81
82 if (empty($question->options)) {
83 return $question;
84 }
85
86 $question->responseformat = $question->options->responseformat;
a4f765eb 87 $question->responserequired = $question->options->responserequired;
894e8b4e
TH
88 $question->responsefieldlines = $question->options->responsefieldlines;
89 $question->attachments = $question->options->attachments;
a4f765eb 90 $question->attachmentsrequired = $question->options->attachmentsrequired;
894e8b4e
TH
91
92 $draftid = file_get_submitted_draft_itemid('graderinfo');
93 $question->graderinfo = array();
94 $question->graderinfo['text'] = file_prepare_draft_area(
3d9645ae 95 $draftid, // Draftid
894e8b4e
TH
96 $this->context->id, // context
97 'qtype_essay', // component
98 'graderinfo', // filarea
99 !empty($question->id) ? (int) $question->id : null, // itemid
100 $this->fileoptions, // options
3d9645ae 101 $question->options->graderinfo // text.
894e8b4e
TH
102 );
103 $question->graderinfo['format'] = $question->options->graderinfoformat;
104 $question->graderinfo['itemid'] = $draftid;
105
60527d0c
JMV
106 $question->responsetemplate = array(
107 'text' => $question->options->responsetemplate,
108 'format' => $question->options->responsetemplateformat,
109 );
110
894e8b4e
TH
111 return $question;
112 }
113
a4f765eb
KT
114 public function validation($fromform, $files) {
115 $errors = parent::validation($fromform, $files);
116
117 // Don't allow both 'no inline response' and 'no attachments' to be selected,
118 // as these options would result in there being no input requested from the user.
119 if ($fromform['responseformat'] == 'noinline' && !$fromform['attachments']) {
120 $errors['attachments'] = get_string('mustattach', 'qtype_essay');
121 }
122
123 // If 'no inline response' is set, force the teacher to require attachments;
124 // otherwise there will be nothing to grade.
125 if ($fromform['responseformat'] == 'noinline' && !$fromform['attachmentsrequired']) {
126 $errors['attachmentsrequired'] = get_string('mustrequire', 'qtype_essay');
127 }
128
129 // Don't allow the teacher to require more attachments than they allow; as this would
130 // create a condition that it's impossible for the student to meet.
131 if ($fromform['attachments'] != -1 && $fromform['attachments'] < $fromform['attachmentsrequired'] ) {
132 $errors['attachmentsrequired'] = get_string('mustrequirefewer', 'qtype_essay');
133 }
134
135 return $errors;
136 }
137
6d03299d 138 public function qtype() {
271ffe3f 139 return 'essay';
140 }
271ffe3f 141}