weekly release 2.9dev
[moodle.git] / question / type / shortanswer / questiontype.php
CommitLineData
aeb15530 1<?php
fe6ce234
DC
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
41a89a07 17/**
068b4594
TH
18 * Question type class for the short answer question type.
19 *
7764183a
TH
20 * @package qtype
21 * @subpackage shortanswer
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
53a4d39f 24 */
516cf3eb 25
516cf3eb 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
068b4594
TH
29require_once($CFG->libdir . '/questionlib.php');
30require_once($CFG->dirroot . '/question/engine/lib.php');
31require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
516cf3eb 32
869309b8 33
068b4594
TH
34/**
35 * The short answer question type.
36 *
7764183a
TH
37 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
068b4594
TH
39 */
40class qtype_shortanswer extends question_type {
41 public function extra_question_fields() {
b4cb0957 42 return array('qtype_shortanswer_options', 'usecase');
516cf3eb 43 }
44
c7df5006 45 public function move_files($questionid, $oldcontextid, $newcontextid) {
5d548d3e
TH
46 parent::move_files($questionid, $oldcontextid, $newcontextid);
47 $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid);
d44480f6 48 $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
fe6ce234
DC
49 }
50
9203b705
TH
51 protected function delete_files($questionid, $contextid) {
52 parent::delete_files($questionid, $contextid);
53 $this->delete_files_in_answers($questionid, $contextid);
d44480f6 54 $this->delete_files_in_hints($questionid, $contextid);
9203b705
TH
55 }
56
068b4594 57 public function save_question_options($question) {
f34488b2 58 global $DB;
0ff4bd08 59 $result = new stdClass();
90a36f8c 60
f0c6c181 61 // Perform sanity checks on fractional grades.
516cf3eb 62 $maxfraction = -1;
69988ed4 63 foreach ($question->answer as $key => $answerdata) {
94a6d656 64 if ($question->fraction[$key] > $maxfraction) {
65 $maxfraction = $question->fraction[$key];
66 }
516cf3eb 67 }
68
f0c6c181
OS
69 if ($maxfraction != 1) {
70 $result->error = get_string('fractionsnomax', 'question', $maxfraction * 100);
71 return $result;
068b4594
TH
72 }
73
f0c6c181 74 parent::save_question_options($question);
69988ed4 75
f0c6c181 76 $this->save_question_answers($question);
516cf3eb 77
f0c6c181 78 $this->save_hints($question);
516cf3eb 79 }
80
068b4594
TH
81 protected function initialise_question_instance(question_definition $question, $questiondata) {
82 parent::initialise_question_instance($question, $questiondata);
068b4594 83 $this->initialise_question_answers($question, $questiondata);
516cf3eb 84 }
1a1293ed 85
068b4594
TH
86 public function get_random_guess_score($questiondata) {
87 foreach ($questiondata->options->answers as $aid => $answer) {
88 if ('*' == trim($answer->answer)) {
6f51ed72 89 return $answer->fraction;
90 }
91 }
92 return 0;
93 }
516cf3eb 94
068b4594
TH
95 public function get_possible_responses($questiondata) {
96 $responses = array();
90a36f8c 97
24400682 98 $starfound = false;
068b4594
TH
99 foreach ($questiondata->options->answers as $aid => $answer) {
100 $responses[$aid] = new question_possible_response($answer->answer,
101 $answer->fraction);
24400682
TH
102 if ($answer->answer === '*') {
103 $starfound = true;
104 }
105 }
106
107 if (!$starfound) {
108 $responses[0] = new question_possible_response(
109 get_string('didnotmatchanyanswer', 'question'), 0);
93a501c1 110 }
24400682 111
068b4594 112 $responses[null] = question_possible_response::no_response();
b9bd6da4 113
068b4594 114 return array($questiondata->id => $responses);
b9bd6da4 115 }
516cf3eb 116}