weekly release 3.5dev
[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
6d03299d 55 public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
f3460297 56 return question_engine::make_behaviour('manualgraded', $qa, $preferredbehaviour);
6d03299d
TH
57 }
58
b36d2d06
TH
59 /**
60 * @param moodle_page the page we are outputting to.
61 * @return qtype_essay_format_renderer_base the response-format-specific renderer.
62 */
63 public function get_format_renderer(moodle_page $page) {
64 return $page->get_renderer('qtype_essay', 'format_' . $this->responseformat);
65 }
66
6d03299d 67 public function get_expected_data() {
48d9c17d 68 if ($this->responseformat == 'editorfilepicker') {
2d2018ab 69 $expecteddata = array('answer' => question_attempt::PARAM_RAW_FILES);
c0d12fc1
TH
70 } else {
71 $expecteddata = array('answer' => PARAM_RAW);
48d9c17d 72 }
405aca35 73 $expecteddata['answerformat'] = PARAM_ALPHANUMEXT;
caeeff07
TH
74 if ($this->attachments != 0) {
75 $expecteddata['attachments'] = question_attempt::PARAM_FILES;
76 }
77 return $expecteddata;
6d03299d
TH
78 }
79
80 public function summarise_response(array $response) {
81 if (isset($response['answer'])) {
e2b388c1
TH
82 return question_utils::to_plain_text($response['answer'],
83 $response['answerformat'], array('para' => false));
6d03299d
TH
84 } else {
85 return null;
86 }
87 }
88
89 public function get_correct_response() {
90 return null;
91 }
92
93 public function is_complete_response(array $response) {
ee9e7ee3 94 // Determine if the given response has online text and attachments.
a4f765eb
KT
95 $hasinlinetext = array_key_exists('answer', $response) && ($response['answer'] !== '');
96 $hasattachments = array_key_exists('attachments', $response)
97 && $response['attachments'] instanceof question_response_files;
98
99 // Determine the number of attachments present.
100 if ($hasattachments) {
101 $attachcount = count($response['attachments']->get_files());
102 } else {
103 $attachcount = 0;
104 }
105
106 // Determine if we have /some/ content to be graded.
107 $hascontent = $hasinlinetext || ($attachcount > 0);
108
109 // Determine if we meet the optional requirements.
110 $meetsinlinereq = $hasinlinetext || (!$this->responserequired) || ($this->responseformat == 'noinline');
111 $meetsattachmentreq = ($attachcount >= $this->attachmentsrequired);
112
113 // The response is complete iff all of our requirements are met.
114 return $hascontent && $meetsinlinereq && $meetsattachmentreq;
6d03299d
TH
115 }
116
117 public function is_same_response(array $prevresponse, array $newresponse) {
60527d0c 118 if (array_key_exists('answer', $prevresponse) && $prevresponse['answer'] !== $this->responsetemplate) {
afb1b3d0 119 $value1 = (string) $prevresponse['answer'];
60527d0c
JMV
120 } else {
121 $value1 = '';
122 }
123 if (array_key_exists('answer', $newresponse) && $newresponse['answer'] !== $this->responsetemplate) {
afb1b3d0 124 $value2 = (string) $newresponse['answer'];
60527d0c
JMV
125 } else {
126 $value2 = '';
127 }
128 return $value1 === $value2 && ($this->attachments == 0 ||
cd3557e6
TH
129 question_utils::arrays_same_at_key_missing_is_blank(
130 $prevresponse, $newresponse, 'attachments'));
6d03299d 131 }
caeeff07
TH
132
133 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
134 if ($component == 'question' && $filearea == 'response_attachments') {
135 // Response attachments visible if the question has them.
136 return $this->attachments != 0;
137
48d9c17d
TH
138 } else if ($component == 'question' && $filearea == 'response_answer') {
139 // Response attachments visible if the question has them.
140 return $this->responseformat === 'editorfilepicker';
141
783af252 142 } else if ($component == 'qtype_essay' && $filearea == 'graderinfo') {
cf0b9432 143 return $options->manualcomment && $args[0] == $this->id;
783af252 144
caeeff07 145 } else {
121fd4c1
TH
146 return parent::check_file_access($qa, $options, $component,
147 $filearea, $args, $forcedownload);
caeeff07
TH
148 }
149 }
6d03299d 150}