MDL-20636 Essay questions can now handle files in the HTML editor. #216
[moodle.git] / question / type / essay / question.php
CommitLineData
6d03299d
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
6d03299d
TH
18/**
19 * Essay question definition class.
20 *
7764183a
TH
21 * @package qtype
22 * @subpackage essay
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
6d03299d
TH
25 */
26
27
a17b297d
TH
28defined('MOODLE_INTERNAL') || die();
29
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 {
894e8b4e
TH
38 public $responseformat;
39 public $responsefieldlines;
40 public $attachments;
41 public $graderinfo;
42 public $graderinfoformat;
43
6d03299d
TH
44 public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
45 question_engine::load_behaviour_class('manualgraded');
46 return new qbehaviour_manualgraded($qa, $preferredbehaviour);
47 }
48
b36d2d06
TH
49 /**
50 * @param moodle_page the page we are outputting to.
51 * @return qtype_essay_format_renderer_base the response-format-specific renderer.
52 */
53 public function get_format_renderer(moodle_page $page) {
54 return $page->get_renderer('qtype_essay', 'format_' . $this->responseformat);
55 }
56
6d03299d 57 public function get_expected_data() {
48d9c17d
TH
58 if ($this->responseformat == 'editorfilepicker') {
59 $expecteddata = array('answer' => question_attempt::PARAM_CLEANHTML_FILES);
60 } else {
61 $expecteddata = array('answer' => PARAM_CLEANHTML);
62 }
63 $expecteddata['answerformat'] = PARAM_FORMAT;
caeeff07
TH
64 if ($this->attachments != 0) {
65 $expecteddata['attachments'] = question_attempt::PARAM_FILES;
66 }
67 return $expecteddata;
6d03299d
TH
68 }
69
70 public function summarise_response(array $response) {
71 if (isset($response['answer'])) {
72 $formatoptions = new stdClass();
73 $formatoptions->para = false;
74 return shorten_text(html_to_text(format_text($response['answer'], FORMAT_HTML, $formatoptions), 0, false), 200);
75 } else {
76 return null;
77 }
78 }
79
80 public function get_correct_response() {
81 return null;
82 }
83
84 public function is_complete_response(array $response) {
85 return !empty($response['answer']);
86 }
87
88 public function is_same_response(array $prevresponse, array $newresponse) {
89 return question_utils::arrays_same_at_key_missing_is_blank(
cd3557e6
TH
90 $prevresponse, $newresponse, 'answer') && ($this->attachments == 0 ||
91 question_utils::arrays_same_at_key_missing_is_blank(
92 $prevresponse, $newresponse, 'attachments'));
6d03299d 93 }
caeeff07
TH
94
95 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
96 if ($component == 'question' && $filearea == 'response_attachments') {
97 // Response attachments visible if the question has them.
98 return $this->attachments != 0;
99
48d9c17d
TH
100 } else if ($component == 'question' && $filearea == 'response_answer') {
101 // Response attachments visible if the question has them.
102 return $this->responseformat === 'editorfilepicker';
103
783af252
TH
104 } else if ($component == 'qtype_essay' && $filearea == 'graderinfo') {
105 return $options->manualcomment;
106
caeeff07
TH
107 } else {
108 return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload);
109 }
110 }
6d03299d 111}