Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[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
da6f1f58
TH
81 protected function fill_answer_fields($answer, $questiondata, $key, $context) {
82 $answer = parent::fill_answer_fields($answer, $questiondata, $key, $context);
83 $answer->answer = trim($answer->answer);
84 return $answer;
85 }
86
068b4594
TH
87 protected function initialise_question_instance(question_definition $question, $questiondata) {
88 parent::initialise_question_instance($question, $questiondata);
068b4594 89 $this->initialise_question_answers($question, $questiondata);
516cf3eb 90 }
1a1293ed 91
068b4594
TH
92 public function get_random_guess_score($questiondata) {
93 foreach ($questiondata->options->answers as $aid => $answer) {
94 if ('*' == trim($answer->answer)) {
6f51ed72 95 return $answer->fraction;
96 }
97 }
98 return 0;
99 }
516cf3eb 100
068b4594
TH
101 public function get_possible_responses($questiondata) {
102 $responses = array();
90a36f8c 103
24400682 104 $starfound = false;
068b4594
TH
105 foreach ($questiondata->options->answers as $aid => $answer) {
106 $responses[$aid] = new question_possible_response($answer->answer,
107 $answer->fraction);
24400682
TH
108 if ($answer->answer === '*') {
109 $starfound = true;
110 }
111 }
112
113 if (!$starfound) {
114 $responses[0] = new question_possible_response(
115 get_string('didnotmatchanyanswer', 'question'), 0);
93a501c1 116 }
24400682 117
068b4594 118 $responses[null] = question_possible_response::no_response();
b9bd6da4 119
068b4594 120 return array($questiondata->id => $responses);
b9bd6da4 121 }
516cf3eb 122}