MDL-63185 mod_quiz: replace existing tests to use new step
[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
92 public function get_correct_response() {
93 return null;
94 }
95
96 public function is_complete_response(array $response) {
ee9e7ee3 97 // Determine if the given response has online text and attachments.
a4f765eb
KT
98 $hasinlinetext = array_key_exists('answer', $response) && ($response['answer'] !== '');
99 $hasattachments = array_key_exists('attachments', $response)
100 && $response['attachments'] instanceof question_response_files;
101
102 // Determine the number of attachments present.
103 if ($hasattachments) {
94cb5a66
LB
104 // Check the filetypes.
105 $filetypesutil = new \core_form\filetypes_util();
106 $whitelist = $filetypesutil->normalize_file_types($this->filetypeslist);
107 $wrongfiles = array();
108 foreach ($response['attachments']->get_files() as $file) {
109 if (!$filetypesutil->is_allowed_file_type($file->get_filename(), $whitelist)) {
110 $wrongfiles[] = $file->get_filename();
111 }
112 }
113 if ($wrongfiles) { // At least one filetype is wrong.
114 return false;
115 }
a4f765eb
KT
116 $attachcount = count($response['attachments']->get_files());
117 } else {
118 $attachcount = 0;
119 }
120
121 // Determine if we have /some/ content to be graded.
122 $hascontent = $hasinlinetext || ($attachcount > 0);
123
124 // Determine if we meet the optional requirements.
125 $meetsinlinereq = $hasinlinetext || (!$this->responserequired) || ($this->responseformat == 'noinline');
126 $meetsattachmentreq = ($attachcount >= $this->attachmentsrequired);
127
128 // The response is complete iff all of our requirements are met.
129 return $hascontent && $meetsinlinereq && $meetsattachmentreq;
6d03299d
TH
130 }
131
94cb5a66
LB
132 public function is_gradable_response(array $response) {
133 // Determine if the given response has online text and attachments.
134 if (array_key_exists('answer', $response) && ($response['answer'] !== '')) {
135 return true;
136 } else if (array_key_exists('attachments', $response)
137 && $response['attachments'] instanceof question_response_files) {
138 return true;
139 } else {
140 return false;
141 }
142 }
143
6d03299d 144 public function is_same_response(array $prevresponse, array $newresponse) {
60527d0c 145 if (array_key_exists('answer', $prevresponse) && $prevresponse['answer'] !== $this->responsetemplate) {
afb1b3d0 146 $value1 = (string) $prevresponse['answer'];
60527d0c
JMV
147 } else {
148 $value1 = '';
149 }
150 if (array_key_exists('answer', $newresponse) && $newresponse['answer'] !== $this->responsetemplate) {
afb1b3d0 151 $value2 = (string) $newresponse['answer'];
60527d0c
JMV
152 } else {
153 $value2 = '';
154 }
155 return $value1 === $value2 && ($this->attachments == 0 ||
cd3557e6
TH
156 question_utils::arrays_same_at_key_missing_is_blank(
157 $prevresponse, $newresponse, 'attachments'));
6d03299d 158 }
caeeff07
TH
159
160 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
161 if ($component == 'question' && $filearea == 'response_attachments') {
162 // Response attachments visible if the question has them.
163 return $this->attachments != 0;
164
48d9c17d
TH
165 } else if ($component == 'question' && $filearea == 'response_answer') {
166 // Response attachments visible if the question has them.
167 return $this->responseformat === 'editorfilepicker';
168
783af252 169 } else if ($component == 'qtype_essay' && $filearea == 'graderinfo') {
cf0b9432 170 return $options->manualcomment && $args[0] == $this->id;
783af252 171
caeeff07 172 } else {
121fd4c1
TH
173 return parent::check_file_access($qa, $options, $component,
174 $filearea, $args, $forcedownload);
caeeff07
TH
175 }
176 }
6d03299d 177}