Merge branch 'MDL-70041-310' of git://github.com/mihailges/moodle into MOODLE_310_STABLE
[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 46
95d1f1f1
MK
47 /** @var int maximum file size in bytes */
48 public $maxbytes;
49
a4f765eb
KT
50 /** @var int The number of attachments required for a response to be complete. */
51 public $attachmentsrequired;
52
894e8b4e
TH
53 public $graderinfo;
54 public $graderinfoformat;
60527d0c
JMV
55 public $responsetemplate;
56 public $responsetemplateformat;
894e8b4e 57
94cb5a66
LB
58 /** @var array The string array of file types accepted upon file submission. */
59 public $filetypeslist;
60
6d03299d 61 public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
f3460297 62 return question_engine::make_behaviour('manualgraded', $qa, $preferredbehaviour);
6d03299d
TH
63 }
64
b36d2d06
TH
65 /**
66 * @param moodle_page the page we are outputting to.
67 * @return qtype_essay_format_renderer_base the response-format-specific renderer.
68 */
69 public function get_format_renderer(moodle_page $page) {
70 return $page->get_renderer('qtype_essay', 'format_' . $this->responseformat);
71 }
72
6d03299d 73 public function get_expected_data() {
48d9c17d 74 if ($this->responseformat == 'editorfilepicker') {
2d2018ab 75 $expecteddata = array('answer' => question_attempt::PARAM_RAW_FILES);
c0d12fc1
TH
76 } else {
77 $expecteddata = array('answer' => PARAM_RAW);
48d9c17d 78 }
405aca35 79 $expecteddata['answerformat'] = PARAM_ALPHANUMEXT;
caeeff07
TH
80 if ($this->attachments != 0) {
81 $expecteddata['attachments'] = question_attempt::PARAM_FILES;
82 }
83 return $expecteddata;
6d03299d
TH
84 }
85
86 public function summarise_response(array $response) {
87 if (isset($response['answer'])) {
e2b388c1
TH
88 return question_utils::to_plain_text($response['answer'],
89 $response['answerformat'], array('para' => false));
6d03299d
TH
90 } else {
91 return null;
92 }
93 }
94
71fc74ce
SL
95 public function un_summarise_response(string $summary) {
96 if (!empty($summary)) {
9b1fc262 97 return ['answer' => text_to_html($summary)];
71fc74ce
SL
98 } else {
99 return [];
100 }
101 }
102
6d03299d
TH
103 public function get_correct_response() {
104 return null;
105 }
106
107 public function is_complete_response(array $response) {
ee9e7ee3 108 // Determine if the given response has online text and attachments.
a4f765eb
KT
109 $hasinlinetext = array_key_exists('answer', $response) && ($response['answer'] !== '');
110 $hasattachments = array_key_exists('attachments', $response)
111 && $response['attachments'] instanceof question_response_files;
112
113 // Determine the number of attachments present.
114 if ($hasattachments) {
94cb5a66
LB
115 // Check the filetypes.
116 $filetypesutil = new \core_form\filetypes_util();
bd686af0 117 $allowlist = $filetypesutil->normalize_file_types($this->filetypeslist);
94cb5a66
LB
118 $wrongfiles = array();
119 foreach ($response['attachments']->get_files() as $file) {
bd686af0 120 if (!$filetypesutil->is_allowed_file_type($file->get_filename(), $allowlist)) {
94cb5a66
LB
121 $wrongfiles[] = $file->get_filename();
122 }
123 }
124 if ($wrongfiles) { // At least one filetype is wrong.
125 return false;
126 }
a4f765eb
KT
127 $attachcount = count($response['attachments']->get_files());
128 } else {
129 $attachcount = 0;
130 }
131
132 // Determine if we have /some/ content to be graded.
133 $hascontent = $hasinlinetext || ($attachcount > 0);
134
135 // Determine if we meet the optional requirements.
136 $meetsinlinereq = $hasinlinetext || (!$this->responserequired) || ($this->responseformat == 'noinline');
137 $meetsattachmentreq = ($attachcount >= $this->attachmentsrequired);
138
139 // The response is complete iff all of our requirements are met.
140 return $hascontent && $meetsinlinereq && $meetsattachmentreq;
6d03299d
TH
141 }
142
94cb5a66
LB
143 public function is_gradable_response(array $response) {
144 // Determine if the given response has online text and attachments.
145 if (array_key_exists('answer', $response) && ($response['answer'] !== '')) {
146 return true;
147 } else if (array_key_exists('attachments', $response)
148 && $response['attachments'] instanceof question_response_files) {
149 return true;
150 } else {
151 return false;
152 }
153 }
154
6d03299d 155 public function is_same_response(array $prevresponse, array $newresponse) {
60527d0c 156 if (array_key_exists('answer', $prevresponse) && $prevresponse['answer'] !== $this->responsetemplate) {
afb1b3d0 157 $value1 = (string) $prevresponse['answer'];
60527d0c
JMV
158 } else {
159 $value1 = '';
160 }
161 if (array_key_exists('answer', $newresponse) && $newresponse['answer'] !== $this->responsetemplate) {
afb1b3d0 162 $value2 = (string) $newresponse['answer'];
60527d0c
JMV
163 } else {
164 $value2 = '';
165 }
166 return $value1 === $value2 && ($this->attachments == 0 ||
cd3557e6
TH
167 question_utils::arrays_same_at_key_missing_is_blank(
168 $prevresponse, $newresponse, 'attachments'));
6d03299d 169 }
caeeff07
TH
170
171 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
172 if ($component == 'question' && $filearea == 'response_attachments') {
173 // Response attachments visible if the question has them.
174 return $this->attachments != 0;
175
48d9c17d
TH
176 } else if ($component == 'question' && $filearea == 'response_answer') {
177 // Response attachments visible if the question has them.
178 return $this->responseformat === 'editorfilepicker';
179
783af252 180 } else if ($component == 'qtype_essay' && $filearea == 'graderinfo') {
cf0b9432 181 return $options->manualcomment && $args[0] == $this->id;
783af252 182
caeeff07 183 } else {
121fd4c1
TH
184 return parent::check_file_access($qa, $options, $component,
185 $filearea, $args, $forcedownload);
caeeff07
TH
186 }
187 }
6d03299d 188}