curl now mandatory
[moodle.git] / question / type / essay / questiontype.php
CommitLineData
aeb15530 1<?php
516cf3eb 2
3//////////////////
4/// ESSAY ///
5/////////////////
6
7/// QUESTION TYPE CLASS //////////////////
1976496e 8/**
9 * @package questionbank
10 * @subpackage questiontypes
7375c542 11 */
af3830ee 12class question_essay_qtype extends default_questiontype {
516cf3eb 13
14 function name() {
15 return 'essay';
16 }
9fc3100f 17
a2156789 18 function is_manual_graded() {
5114a1ac 19 return true;
a2156789 20 }
21
516cf3eb 22 function save_question_options($question) {
f34488b2 23 global $DB;
5114a1ac 24 $result = true;
25 $update = true;
f34488b2 26 $answer = $DB->get_record("question_answers", array("question" => $question->id));
5114a1ac 27 if (!$answer) {
28 $answer = new stdClass;
29 $answer->question = $question->id;
30 $update = false;
31 }
32 $answer->answer = $question->feedback;
33 $answer->feedback = $question->feedback;
34 $answer->fraction = $question->fraction;
35 if ($update) {
bb4b6010 36 $DB->update_record("question_answers", $answer);
516cf3eb 37 } else {
bb4b6010 38 $answer->id = $DB->insert_record("question_answers", $answer);
516cf3eb 39 }
5114a1ac 40 return $result;
516cf3eb 41 }
42
43 function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
a472184c 44 global $CFG;
516cf3eb 45
a472184c 46 $answers = &$question->options->answers;
47 $readonly = empty($options->readonly) ? '' : 'disabled="disabled"';
9fc3100f 48
54f30678 49 // Only use the rich text editor for the first essay question on a page.
9fc3100f 50
a472184c 51 $formatoptions = new stdClass;
7347c60b 52 $formatoptions->noclean = true;
a472184c 53 $formatoptions->para = false;
9fc3100f 54
a472184c 55 $inputname = $question->name_prefix;
56 $stranswer = get_string("answer", "quiz").': ';
9fc3100f 57
a472184c 58 /// set question text and media
59 $questiontext = format_text($question->questiontext,
60 $question->questiontextformat,
61 $formatoptions, $cmoptions->course);
9fc3100f 62
63 $image = get_question_image($question);
516cf3eb 64
a472184c 65 // feedback handling
66 $feedback = '';
604a6184 67 if ($options->feedback && !empty($answers)) {
a472184c 68 foreach ($answers as $answer) {
69 $feedback = format_text($answer->feedback, '', $formatoptions, $cmoptions->course);
70 }
71 }
9fc3100f 72
a472184c 73 // get response value
53354ec0 74 if (isset($state->responses[''])) {
294ce987 75 $value = $state->responses[''];
516cf3eb 76 } else {
77 $value = "";
78 }
0ac93725 79
a472184c 80 // answer
9fc3100f 81 if (empty($options->readonly)) {
516cf3eb 82 // the student needs to type in their answer so print out a text editor
5083db4e
TH
83 $answer = print_textarea(can_use_html_editor(), 18, 80, 630, 400,
84 $inputname, $value, $cmoptions->course, true);
516cf3eb 85 } else {
86 // it is read only, so just format the students answer and output it
e9ad37ed 87 $safeformatoptions = new stdClass;
88 $safeformatoptions->para = false;
89 $answer = format_text($value, FORMAT_MOODLE,
90 $safeformatoptions, $cmoptions->course);
46a5a85b 91 $answer = '<div class="answerreview">' . $answer . '</div>';
516cf3eb 92 }
9fc3100f 93
a472184c 94 include("$CFG->dirroot/question/type/essay/display.html");
516cf3eb 95 }
96
97 function grade_responses(&$question, &$state, $cmoptions) {
a472184c 98 // All grading takes place in Manual Grading
3f24c397 99
9bd7f37d 100 $state->responses[''] = clean_param($state->responses[''], PARAM_CLEAN);
9fc3100f 101
a472184c 102 $state->raw_grade = 0;
103 $state->penalty = 0;
516cf3eb 104
105 return true;
106 }
fb6179a1 107
21a4ca7d 108
fb6179a1 109 /**
110 * Backup the extra information specific to an essay question - over and above
9fc3100f 111 * what is in the mdl_question table.
112 *
fb6179a1 113 * @param file $bf The backup file to write to.
114 * @param object $preferences the blackup options controlling this backup.
115 * @param $questionid the id of the question being backed up.
116 * @param $level indent level in the backup file - so it can be formatted nicely.
117 */
118 function backup($bf, $preferences, $questionid, $level = 6) {
119 return question_backup_answers($bf, $preferences, $questionid, $level);
120 }
121
b9bd6da4 122 /**
123 * Runs all the code required to set up and save an essay question for testing purposes.
124 * Alternate DB table prefix may be used to facilitate data deletion.
125 */
126 function generate_test($name, $courseid = null) {
127 global $DB;
128 list($form, $question) = parent::generate_test($name, $courseid);
129 $form->questiontext = "What is the purpose of life?";
130 $form->feedback = "feedback";
131 $form->generalfeedback = "General feedback";
132 $form->fraction = 0;
133 $form->penalty = 0;
134
135 if ($courseid) {
136 $course = $DB->get_record('course', array('id' => $courseid));
137 }
138
139 return $this->save_question($question, $form, $course);
140 }
141
fb6179a1 142 // Restore method not needed.
9fc3100f 143}
516cf3eb 144//// END OF CLASS ////
145
146//////////////////////////////////////////////////////////////////////////
147//// INITIATION - Without this line the question type is not in use... ///
148//////////////////////////////////////////////////////////////////////////
a2156789 149question_register_questiontype(new question_essay_qtype());
aeb15530 150