MDL-20636 Fix if the use of the File API so images work in questions.
[moodle.git] / question / type / essay / questiontype.php
1 <?php
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/>.
18 //////////////////
19 ///   ESSAY   ///
20 /////////////////
22 /// QUESTION TYPE CLASS //////////////////
23 /**
24  * @package questionbank
25  * @subpackage questiontypes
26  */
27 class question_essay_qtype extends default_questiontype {
29     function name() {
30         return 'essay';
31     }
33     function is_manual_graded() {
34         return true;
35     }
37     function save_question_options($question) {
38         global $DB;
39         $context = $question->context;
41         $answer = $DB->get_record('question_answers', array('question' => $question->id));
42         if (!$answer) {
43             $answer = new stdClass;
44             $answer->question = $question->id;
45             $answer->answer = '';
46             $answer->feedback = '';
47             $answer->id = $DB->insert_record('question_answers', $answer);
48         }
50         $answer->feedback = $question->feedback['text'];
51         $answer->feedbackformat = $question->feedback['format'];
52         $answer->answer = $answer->feedback;
53         $answer->answerformat = $question->feedback['format'];
54         $answer->fraction = $question->fraction;
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);
61         return true;
62     }
64     function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
65         global $CFG;
67         $context = $this->get_context_by_category_id($question->category);
69         $answers  = &$question->options->answers;
70         $readonly = empty($options->readonly) ? '' : 'disabled="disabled"';
72         // Only use the rich text editor for the first essay question on a page.
74         $formatoptions = new stdClass;
75         $formatoptions->noclean = true;
76         $formatoptions->para    = false;
78         $inputname = $question->name_prefix;
79         $stranswer = get_string("answer", "quiz").': ';
81         /// set question text and media
82         $questiontext = format_text($question->questiontext,
83                                    $question->questiontextformat,
84                                    $formatoptions, $cmoptions->course);
86         // feedback handling
87         $feedback = '';
88         if ($options->feedback && !empty($answers)) {
89             foreach ($answers as $answer) {
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);
92             }
93         }
95         // get response value
96         if (isset($state->responses[''])) {
97             $value = $state->responses[''];
98         } else {
99             $value = '';
100         }
102         // answer
103         if (empty($options->readonly)) {
104             // the student needs to type in their answer so print out a text editor
105             $answer = print_textarea(can_use_html_editor(), 18, 80, 630, 400,
106                     $inputname, $value, $cmoptions->course, true);
107         } else {
108             // it is read only, so just format the students answer and output it
109             $safeformatoptions = new stdClass;
110             $safeformatoptions->para = false;
111             $answer = format_text($value, FORMAT_MOODLE,
112                                   $safeformatoptions, $cmoptions->course);
113             $answer = '<div class="answerreview">' . $answer . '</div>';
114         }
116         include("$CFG->dirroot/question/type/essay/display.html");
117     }
119     function grade_responses(&$question, &$state, $cmoptions) {
120         // All grading takes place in Manual Grading
122         $state->responses[''] = clean_param($state->responses[''], PARAM_CLEAN);
124         $state->raw_grade = 0;
125         $state->penalty = 0;
127         return true;
128     }
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;
143         if ($courseid) {
144             $course = $DB->get_record('course', array('id' => $courseid));
145         }
147         return $this->save_question($question, $form);
148     }
150     function move_files($questionid, $oldcontextid, $newcontextid) {
151         parent::move_files($questionid, $oldcontextid, $newcontextid);
152         $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid);
153     }
155     protected function delete_files($questionid, $contextid) {
156         parent::delete_files($questionid, $contextid);
157         $this->delete_files_in_answers($questionid, $contextid);
158     }
160     function check_file_access($question, $state, $options, $contextid, $component,
161             $filearea, $args) {
162         if ($component == 'question' && $filearea == 'answerfeedback') {
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             }
172             return $options->feedback && !empty($response);
174         } else {
175             return parent::check_file_access($question, $state, $options, $contextid, $component,
176                     $filearea, $args);
177         }
178     }
179     // Restore method not needed.
181 //// END OF CLASS ////
183 //////////////////////////////////////////////////////////////////////////
184 //// INITIATION - Without this line the question type is not in use... ///
185 //////////////////////////////////////////////////////////////////////////
186 question_register_questiontype(new question_essay_qtype());