MDL-20636 Fix if the use of the File API so images work in questions.
[moodle.git] / question / type / essay / questiontype.php
CommitLineData
aeb15530 1<?php
516cf3eb 2
fe6ce234
DC
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
516cf3eb 18//////////////////
19/// ESSAY ///
20/////////////////
21
22/// QUESTION TYPE CLASS //////////////////
1976496e 23/**
24 * @package questionbank
25 * @subpackage questiontypes
7375c542 26 */
af3830ee 27class question_essay_qtype extends default_questiontype {
516cf3eb 28
29 function name() {
30 return 'essay';
31 }
9fc3100f 32
a2156789 33 function is_manual_graded() {
5114a1ac 34 return true;
a2156789 35 }
36
516cf3eb 37 function save_question_options($question) {
f34488b2 38 global $DB;
fe6ce234 39 $context = $question->context;
69988ed4
TH
40
41 $answer = $DB->get_record('question_answers', array('question' => $question->id));
5114a1ac 42 if (!$answer) {
43 $answer = new stdClass;
44 $answer->question = $question->id;
69988ed4
TH
45 $answer->answer = '';
46 $answer->feedback = '';
47 $answer->id = $DB->insert_record('question_answers', $answer);
5114a1ac 48 }
69988ed4
TH
49
50 $answer->feedback = $question->feedback['text'];
fe6ce234 51 $answer->feedbackformat = $question->feedback['format'];
69988ed4 52 $answer->answer = $answer->feedback;
fe6ce234 53 $answer->answerformat = $question->feedback['format'];
5114a1ac 54 $answer->fraction = $question->fraction;
69988ed4
TH
55
56 $answer->feedback = $this->import_or_save_files($question->feedback,
57 $context, 'question', 'answerfeedback', $answer->id);
58 $answer->answer = $answer->feedback;
59 $DB->update_record('question_answers', $answer);
60
61 return true;
516cf3eb 62 }
63
64 function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
a472184c 65 global $CFG;
516cf3eb 66
fe6ce234
DC
67 $context = $this->get_context_by_category_id($question->category);
68
69 $answers = &$question->options->answers;
70 $readonly = empty($options->readonly) ? '' : 'disabled="disabled"';
9fc3100f 71
54f30678 72 // Only use the rich text editor for the first essay question on a page.
9fc3100f 73
fe6ce234 74 $formatoptions = new stdClass;
7347c60b 75 $formatoptions->noclean = true;
a472184c 76 $formatoptions->para = false;
9fc3100f 77
a472184c 78 $inputname = $question->name_prefix;
79 $stranswer = get_string("answer", "quiz").': ';
9fc3100f 80
a472184c 81 /// set question text and media
82 $questiontext = format_text($question->questiontext,
83 $question->questiontextformat,
84 $formatoptions, $cmoptions->course);
9fc3100f 85
a472184c 86 // feedback handling
87 $feedback = '';
604a6184 88 if ($options->feedback && !empty($answers)) {
a472184c 89 foreach ($answers as $answer) {
fe6ce234
DC
90 $feedback = quiz_rewrite_question_urls($answer->feedback, 'pluginfile.php', $context->id, 'qtype_essay', 'feedback', array($state->attempt, $state->question), $answer->id);
91 $feedback = format_text($feedback, $answer->feedbackformat, $formatoptions, $cmoptions->course);
a472184c 92 }
93 }
9fc3100f 94
a472184c 95 // get response value
53354ec0 96 if (isset($state->responses[''])) {
294ce987 97 $value = $state->responses[''];
516cf3eb 98 } else {
a9efae50 99 $value = '';
516cf3eb 100 }
0ac93725 101
a472184c 102 // answer
9fc3100f 103 if (empty($options->readonly)) {
516cf3eb 104 // the student needs to type in their answer so print out a text editor
5083db4e
TH
105 $answer = print_textarea(can_use_html_editor(), 18, 80, 630, 400,
106 $inputname, $value, $cmoptions->course, true);
516cf3eb 107 } else {
108 // it is read only, so just format the students answer and output it
e9ad37ed 109 $safeformatoptions = new stdClass;
110 $safeformatoptions->para = false;
111 $answer = format_text($value, FORMAT_MOODLE,
112 $safeformatoptions, $cmoptions->course);
46a5a85b 113 $answer = '<div class="answerreview">' . $answer . '</div>';
516cf3eb 114 }
9fc3100f 115
a472184c 116 include("$CFG->dirroot/question/type/essay/display.html");
516cf3eb 117 }
118
119 function grade_responses(&$question, &$state, $cmoptions) {
a472184c 120 // All grading takes place in Manual Grading
3f24c397 121
9bd7f37d 122 $state->responses[''] = clean_param($state->responses[''], PARAM_CLEAN);
9fc3100f 123
a472184c 124 $state->raw_grade = 0;
125 $state->penalty = 0;
516cf3eb 126
127 return true;
128 }
fb6179a1 129
b9bd6da4 130 /**
131 * Runs all the code required to set up and save an essay question for testing purposes.
132 * Alternate DB table prefix may be used to facilitate data deletion.
133 */
134 function generate_test($name, $courseid = null) {
135 global $DB;
136 list($form, $question) = parent::generate_test($name, $courseid);
137 $form->questiontext = "What is the purpose of life?";
138 $form->feedback = "feedback";
139 $form->generalfeedback = "General feedback";
140 $form->fraction = 0;
141 $form->penalty = 0;
142
143 if ($courseid) {
144 $course = $DB->get_record('course', array('id' => $courseid));
145 }
146
94dbfb3a 147 return $this->save_question($question, $form);
b9bd6da4 148 }
149
5d548d3e
TH
150 function move_files($questionid, $oldcontextid, $newcontextid) {
151 parent::move_files($questionid, $oldcontextid, $newcontextid);
152 $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid);
fe6ce234
DC
153 }
154
9203b705
TH
155 protected function delete_files($questionid, $contextid) {
156 parent::delete_files($questionid, $contextid);
157 $this->delete_files_in_answers($questionid, $contextid);
158 }
159
fe6ce234
DC
160 function check_file_access($question, $state, $options, $contextid, $component,
161 $filearea, $args) {
162 if ($component == 'question' && $filearea == 'answerfeedback') {
163
164 $answerid = reset($args); // itemid is answer id.
165 $answers = &$question->options->answers;
166 if (isset($state->responses[''])) {
167 $response = $state->responses[''];
168 } else {
169 $response = '';
170 }
171
172 return $options->feedback && !empty($response);
173
174 } else {
175 return parent::check_file_access($question, $state, $options, $contextid, $component,
176 $filearea, $args);
177 }
178 }
fb6179a1 179 // Restore method not needed.
9fc3100f 180}
516cf3eb 181//// END OF CLASS ////
182
183//////////////////////////////////////////////////////////////////////////
184//// INITIATION - Without this line the question type is not in use... ///
185//////////////////////////////////////////////////////////////////////////
a2156789 186question_register_questiontype(new question_essay_qtype());