weekly release 3.10dev
[moodle.git] / question / type / essay / questiontype.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
1976496e 17/**
6d03299d
TH
18 * Question type class for the essay question type.
19 *
d3603157 20 * @package qtype
6d03299d 21 * @subpackage essay
d3603157
TH
22 * @copyright 2005 Mark Nielsen
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
7375c542 24 */
516cf3eb 25
516cf3eb 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
603bd001
PS
29require_once($CFG->libdir . '/questionlib.php');
30
a17b297d 31
6d03299d
TH
32/**
33 * The essay question type.
34 *
d3603157
TH
35 * @copyright 2005 Mark Nielsen
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6d03299d
TH
37 */
38class qtype_essay extends question_type {
39 public function is_manual_graded() {
516cf3eb 40 return true;
41 }
fb6179a1 42
6b5f24d3 43 public function response_file_areas() {
3b3d5e79 44 return array('attachments', 'answer');
6b5f24d3
TH
45 }
46
894e8b4e
TH
47 public function get_question_options($question) {
48 global $DB;
121fd4c1
TH
49 $question->options = $DB->get_record('qtype_essay_options',
50 array('questionid' => $question->id), '*', MUST_EXIST);
894e8b4e
TH
51 parent::get_question_options($question);
52 }
53
54 public function save_question_options($formdata) {
55 global $DB;
56 $context = $formdata->context;
57
58 $options = $DB->get_record('qtype_essay_options', array('questionid' => $formdata->id));
59 if (!$options) {
60 $options = new stdClass();
b36d2d06 61 $options->questionid = $formdata->id;
894e8b4e
TH
62 $options->id = $DB->insert_record('qtype_essay_options', $options);
63 }
64
65 $options->responseformat = $formdata->responseformat;
a4f765eb 66 $options->responserequired = $formdata->responserequired;
894e8b4e
TH
67 $options->responsefieldlines = $formdata->responsefieldlines;
68 $options->attachments = $formdata->attachments;
a4f765eb 69 $options->attachmentsrequired = $formdata->attachmentsrequired;
94cb5a66
LB
70 if (!isset($formdata->filetypeslist)) {
71 $options->filetypeslist = "";
72 } else {
73 $options->filetypeslist = $formdata->filetypeslist;
74 }
894e8b4e
TH
75 $options->graderinfo = $this->import_or_save_files($formdata->graderinfo,
76 $context, 'qtype_essay', 'graderinfo', $formdata->id);
77 $options->graderinfoformat = $formdata->graderinfo['format'];
60527d0c
JMV
78 $options->responsetemplate = $formdata->responsetemplate['text'];
79 $options->responsetemplateformat = $formdata->responsetemplate['format'];
894e8b4e
TH
80 $DB->update_record('qtype_essay_options', $options);
81 }
82
83 protected function initialise_question_instance(question_definition $question, $questiondata) {
84 parent::initialise_question_instance($question, $questiondata);
85 $question->responseformat = $questiondata->options->responseformat;
a4f765eb 86 $question->responserequired = $questiondata->options->responserequired;
894e8b4e
TH
87 $question->responsefieldlines = $questiondata->options->responsefieldlines;
88 $question->attachments = $questiondata->options->attachments;
a4f765eb 89 $question->attachmentsrequired = $questiondata->options->attachmentsrequired;
894e8b4e
TH
90 $question->graderinfo = $questiondata->options->graderinfo;
91 $question->graderinfoformat = $questiondata->options->graderinfoformat;
60527d0c
JMV
92 $question->responsetemplate = $questiondata->options->responsetemplate;
93 $question->responsetemplateformat = $questiondata->options->responsetemplateformat;
94cb5a66
LB
94 $filetypesutil = new \core_form\filetypes_util();
95 $question->filetypeslist = $filetypesutil->normalize_file_types($questiondata->options->filetypeslist);
894e8b4e
TH
96 }
97
19835d4a
TH
98 public function delete_question($questionid, $contextid) {
99 global $DB;
100
101 $DB->delete_records('qtype_essay_options', array('questionid' => $questionid));
102 parent::delete_question($questionid, $contextid);
103 }
104
894e8b4e
TH
105 /**
106 * @return array the different response formats that the question type supports.
107 * internal name => human-readable name.
108 */
b3ae5457 109 public function response_formats() {
894e8b4e
TH
110 return array(
111 'editor' => get_string('formateditor', 'qtype_essay'),
112 'editorfilepicker' => get_string('formateditorfilepicker', 'qtype_essay'),
113 'plain' => get_string('formatplain', 'qtype_essay'),
114 'monospaced' => get_string('formatmonospaced', 'qtype_essay'),
a4f765eb
KT
115 'noinline' => get_string('formatnoinline', 'qtype_essay'),
116 );
117 }
118
119 /**
120 * @return array the choices that should be offerd when asking if a response is required
121 */
122 public function response_required_options() {
123 return array(
124 1 => get_string('responseisrequired', 'qtype_essay'),
125 0 => get_string('responsenotrequired', 'qtype_essay'),
894e8b4e
TH
126 );
127 }
128
129 /**
130 * @return array the choices that should be offered for the input box size.
131 */
b3ae5457 132 public function response_sizes() {
894e8b4e
TH
133 $choices = array();
134 for ($lines = 5; $lines <= 40; $lines += 5) {
135 $choices[$lines] = get_string('nlines', 'qtype_essay', $lines);
136 }
137 return $choices;
138 }
139
140 /**
141 * @return array the choices that should be offered for the number of attachments.
142 */
143 public function attachment_options() {
144 return array(
145 0 => get_string('no'),
146 1 => '1',
147 2 => '2',
148 3 => '3',
149 -1 => get_string('unlimited'),
150 );
151 }
152
a4f765eb
KT
153 /**
154 * @return array the choices that should be offered for the number of required attachments.
155 */
156 public function attachments_required_options() {
157 return array(
158 0 => get_string('attachmentsoptional', 'qtype_essay'),
159 1 => '1',
160 2 => '2',
161 3 => '3'
162 );
163 }
164
c7df5006 165 public function move_files($questionid, $oldcontextid, $newcontextid) {
5d548d3e 166 parent::move_files($questionid, $oldcontextid, $newcontextid);
3b3d5e79
TH
167 $fs = get_file_storage();
168 $fs->move_area_files_to_new_context($oldcontextid,
169 $newcontextid, 'qtype_essay', 'graderinfo', $questionid);
fe6ce234
DC
170 }
171
9203b705
TH
172 protected function delete_files($questionid, $contextid) {
173 parent::delete_files($questionid, $contextid);
3b3d5e79
TH
174 $fs = get_file_storage();
175 $fs->delete_area_files($contextid, 'qtype_essay', 'graderinfo', $questionid);
9203b705 176 }
9fc3100f 177}