MDL-14624:
[moodle-linuxchix.git] / question / type / essay / questiontype.php
blobf45eff0646e8fad4e7cb6ae31df969a8d1010226
1 <?php // $Id$
3 //////////////////
4 /// ESSAY ///
5 /////////////////
7 /// QUESTION TYPE CLASS //////////////////
8 /**
9 * @package questionbank
10 * @subpackage questiontypes
12 class question_essay_qtype extends default_questiontype {
14 function name() {
15 return 'essay';
18 function is_manual_graded() {
19 return true;
22 function is_usable_by_random() {
23 return false;
26 function save_question_options($question) {
27 $result = true;
28 $update = true;
29 $answer = get_record("question_answers", "question", $question->id);
30 if (!$answer) {
31 $answer = new stdClass;
32 $answer->question = $question->id;
33 $update = false;
35 $answer->answer = $question->feedback;
36 $answer->feedback = $question->feedback;
37 $answer->fraction = $question->fraction;
38 if ($update) {
39 if (!update_record("question_answers", $answer)) {
40 $result = new stdClass;
41 $result->error = "Could not update quiz answer!";
43 } else {
44 if (!$answer->id = insert_record("question_answers", $answer)) {
45 $result = new stdClass;
46 $result->error = "Could not insert quiz answer!";
49 return $result;
52 function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
53 global $CFG;
54 static $htmleditorused = false;
56 $answers = &$question->options->answers;
57 $readonly = empty($options->readonly) ? '' : 'disabled="disabled"';
59 // Only use the rich text editor for the first essay question on a page.
60 $usehtmleditor = can_use_html_editor() && !$htmleditorused;
62 $formatoptions = new stdClass;
63 $formatoptions->noclean = true;
64 $formatoptions->para = false;
66 $inputname = $question->name_prefix;
67 $stranswer = get_string("answer", "quiz").': ';
69 /// set question text and media
70 $questiontext = format_text($question->questiontext,
71 $question->questiontextformat,
72 $formatoptions, $cmoptions->course);
74 $image = get_question_image($question);
76 // feedback handling
77 $feedback = '';
78 if ($options->feedback && !empty($answers)) {
79 foreach ($answers as $answer) {
80 $feedback = format_text($answer->feedback, '', $formatoptions, $cmoptions->course);
84 // get response value
85 if (isset($state->responses[''])) {
86 $value = stripslashes_safe($state->responses['']);
87 } else {
88 $value = "";
91 // answer
92 if (empty($options->readonly)) {
93 // the student needs to type in their answer so print out a text editor
94 $answer = print_textarea($usehtmleditor, 18, 80, 630, 400, $inputname, $value, $cmoptions->course, true);
95 } else {
96 // it is read only, so just format the students answer and output it
97 $safeformatoptions = new stdClass;
98 $safeformatoptions->para = false;
99 $answer = format_text($value, FORMAT_MOODLE,
100 $safeformatoptions, $cmoptions->course);
101 $answer = '<div class="answerreview">' . $answer . '</div>';
104 include("$CFG->dirroot/question/type/essay/display.html");
106 if ($usehtmleditor && empty($options->readonly)) {
107 use_html_editor($inputname);
108 $htmleditorused = true;
112 function grade_responses(&$question, &$state, $cmoptions) {
113 // All grading takes place in Manual Grading
115 $state->responses[''] = clean_param($state->responses[''], PARAM_CLEAN);
117 $state->raw_grade = 0;
118 $state->penalty = 0;
120 return true;
124 * Backup the extra information specific to an essay question - over and above
125 * what is in the mdl_question table.
127 * @param file $bf The backup file to write to.
128 * @param object $preferences the blackup options controlling this backup.
129 * @param $questionid the id of the question being backed up.
130 * @param $level indent level in the backup file - so it can be formatted nicely.
132 function backup($bf, $preferences, $questionid, $level = 6) {
133 return question_backup_answers($bf, $preferences, $questionid, $level);
136 // Restore method not needed.
138 //// END OF CLASS ////
140 //////////////////////////////////////////////////////////////////////////
141 //// INITIATION - Without this line the question type is not in use... ///
142 //////////////////////////////////////////////////////////////////////////
143 question_register_questiontype(new question_essay_qtype());