weekly release 3.10dev
[moodle.git] / question / type / essay / question.php
CommitLineData
6d03299d 1<?php
6d03299d
TH
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
6d03299d
TH
17/**
18 * Essay question definition class.
19 *
7764183a
TH
20 * @package qtype
21 * @subpackage essay
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6d03299d
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
97562c4d 29require_once($CFG->dirroot . '/question/type/questionbase.php');
a17b297d 30
6d03299d
TH
31/**
32 * Represents an essay question.
33 *
7764183a
TH
34 * @copyright 2009 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6d03299d
TH
36 */
37class qtype_essay_question extends question_with_responses {
a4f765eb 38
894e8b4e 39 public $responseformat;
a4f765eb
KT
40
41 /** @var int Indicates whether an inline response is required ('0') or optional ('1') */
42 public $responserequired;
43
894e8b4e
TH
44 public $responsefieldlines;
45 public $attachments;
a4f765eb
KT
46
47 /** @var int The number of attachments required for a response to be complete. */
48 public $attachmentsrequired;
49
894e8b4e
TH
50 public $graderinfo;
51 public $graderinfoformat;
60527d0c
JMV
52 public $responsetemplate;
53 public $responsetemplateformat;
894e8b4e 54
94cb5a66
LB
55 /** @var array The string array of file types accepted upon file submission. */
56 public $filetypeslist;
57
6d03299d 58 public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
f3460297 59 return question_engine::make_behaviour('manualgraded', $qa, $preferredbehaviour);
6d03299d
TH
60 }
61
b36d2d06
TH
62 /**
63 * @param moodle_page the page we are outputting to.
64 * @return qtype_essay_format_renderer_base the response-format-specific renderer.
65 */
66 public function get_format_renderer(moodle_page $page) {
67 return $page->get_renderer('qtype_essay', 'format_' . $this->responseformat);
68 }
69
6d03299d 70 public function get_expected_data() {
48d9c17d 71 if ($this->responseformat == 'editorfilepicker') {
2d2018ab 72 $expecteddata = array('answer' => question_attempt::PARAM_RAW_FILES);
c0d12fc1
TH
73 } else {
74 $expecteddata = array('answer' => PARAM_RAW);
48d9c17d 75 }
405aca35 76 $expecteddata['answerformat'] = PARAM_ALPHANUMEXT;
caeeff07
TH
77 if ($this->attachments != 0) {
78 $expecteddata['attachments'] = question_attempt::PARAM_FILES;
79 }
80 return $expecteddata;
6d03299d
TH
81 }
82
83 public function summarise_response(array $response) {
84 if (isset($response['answer'])) {
e2b388c1
TH
85 return question_utils::to_plain_text($response['answer'],
86 $response['answerformat'], array('para' => false));
6d03299d
TH
87 } else {
88 return null;
89 }
90 }
91
71fc74ce
SL
92 public function un_summarise_response(string $summary) {
93 if (!empty($summary)) {
9b1fc262 94 return ['answer' => text_to_html($summary)];
71fc74ce
SL
95 } else {
96 return [];
97 }
98 }
99
6d03299d
TH
100 public function get_correct_response() {
101 return null;
102 }
103
104 public function is_complete_response(array $response) {
ee9e7ee3 105 // Determine if the given response has online text and attachments.
a4f765eb
KT
106 $hasinlinetext = array_key_exists('answer', $response) && ($response['answer'] !== '');
107 $hasattachments = array_key_exists('attachments', $response)
108 && $response['attachments'] instanceof question_response_files;
109
110 // Determine the number of attachments present.
111 if ($hasattachments) {
94cb5a66
LB
112 // Check the filetypes.
113 $filetypesutil = new \core_form\filetypes_util();
114 $whitelist = $filetypesutil->normalize_file_types($this->filetypeslist);
115 $wrongfiles = array();
116 foreach ($response['attachments']->get_files() as $file) {
117 if (!$filetypesutil->is_allowed_file_type($file->get_filename(), $whitelist)) {
118 $wrongfiles[] = $file->get_filename();
119 }
120 }
121 if ($wrongfiles) { // At least one filetype is wrong.
122 return false;
123 }
a4f765eb
KT
124 $attachcount = count($response['attachments']->get_files());
125 } else {
126 $attachcount = 0;
127 }
128
129 // Determine if we have /some/ content to be graded.
130 $hascontent = $hasinlinetext || ($attachcount > 0);
131
132 // Determine if we meet the optional requirements.
133 $meetsinlinereq = $hasinlinetext || (!$this->responserequired) || ($this->responseformat == 'noinline');
134 $meetsattachmentreq = ($attachcount >= $this->attachmentsrequired);
135
136 // The response is complete iff all of our requirements are met.
137 return $hascontent && $meetsinlinereq && $meetsattachmentreq;
6d03299d
TH
138 }
139
94cb5a66
LB
140 public function is_gradable_response(array $response) {
141 // Determine if the given response has online text and attachments.
142 if (array_key_exists('answer', $response) && ($response['answer'] !== '')) {
143 return true;
144 } else if (array_key_exists('attachments', $response)
145 && $response['attachments'] instanceof question_response_files) {
146 return true;
147 } else {
148 return false;
149 }
150 }
151
6d03299d 152 public function is_same_response(array $prevresponse, array $newresponse) {
60527d0c 153 if (array_key_exists('answer', $prevresponse) && $prevresponse['answer'] !== $this->responsetemplate) {
afb1b3d0 154 $value1 = (string) $prevresponse['answer'];
60527d0c
JMV
155 } else {
156 $value1 = '';
157 }
158 if (array_key_exists('answer', $newresponse) && $newresponse['answer'] !== $this->responsetemplate) {
afb1b3d0 159 $value2 = (string) $newresponse['answer'];
60527d0c
JMV
160 } else {
161 $value2 = '';
162 }
163 return $value1 === $value2 && ($this->attachments == 0 ||
cd3557e6
TH
164 question_utils::arrays_same_at_key_missing_is_blank(
165 $prevresponse, $newresponse, 'attachments'));
6d03299d 166 }
caeeff07
TH
167
168 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
169 if ($component == 'question' && $filearea == 'response_attachments') {
170 // Response attachments visible if the question has them.
171 return $this->attachments != 0;
172
48d9c17d
TH
173 } else if ($component == 'question' && $filearea == 'response_answer') {
174 // Response attachments visible if the question has them.
175 return $this->responseformat === 'editorfilepicker';
176
783af252 177 } else if ($component == 'qtype_essay' && $filearea == 'graderinfo') {
cf0b9432 178 return $options->manualcomment && $args[0] == $this->id;
783af252 179
caeeff07 180 } else {
121fd4c1
TH
181 return parent::check_file_access($qa, $options, $component,
182 $filearea, $args, $forcedownload);
caeeff07
TH
183 }
184 }
6d03299d 185}