Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / shortanswer / questiontype.php
1 <?php
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/>.
17 /**
18  * Question type class for the short answer question type.
19  *
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
24  */
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/questionlib.php');
30 require_once($CFG->dirroot . '/question/engine/lib.php');
31 require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
34 /**
35  * The short answer question type.
36  *
37  * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class qtype_shortanswer extends question_type {
41     public function extra_question_fields() {
42         return array('qtype_shortanswer_options', 'usecase');
43     }
45     public function move_files($questionid, $oldcontextid, $newcontextid) {
46         parent::move_files($questionid, $oldcontextid, $newcontextid);
47         $this->move_files_in_answers($questionid, $oldcontextid, $newcontextid);
48         $this->move_files_in_hints($questionid, $oldcontextid, $newcontextid);
49     }
51     protected function delete_files($questionid, $contextid) {
52         parent::delete_files($questionid, $contextid);
53         $this->delete_files_in_answers($questionid, $contextid);
54         $this->delete_files_in_hints($questionid, $contextid);
55     }
57     public function save_question_options($question) {
58         global $DB;
59         $result = new stdClass();
61         // Perform sanity checks on fractional grades.
62         $maxfraction = -1;
63         foreach ($question->answer as $key => $answerdata) {
64             if ($question->fraction[$key] > $maxfraction) {
65                 $maxfraction = $question->fraction[$key];
66             }
67         }
69         if ($maxfraction != 1) {
70             $result->error = get_string('fractionsnomax', 'question', $maxfraction * 100);
71             return $result;
72         }
74         parent::save_question_options($question);
76         $this->save_question_answers($question);
78         $this->save_hints($question);
79     }
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     }
87     protected function initialise_question_instance(question_definition $question, $questiondata) {
88         parent::initialise_question_instance($question, $questiondata);
89         $this->initialise_question_answers($question, $questiondata);
90     }
92     public function get_random_guess_score($questiondata) {
93         foreach ($questiondata->options->answers as $aid => $answer) {
94             if ('*' == trim($answer->answer)) {
95                 return $answer->fraction;
96             }
97         }
98         return 0;
99     }
101     public function get_possible_responses($questiondata) {
102         $responses = array();
104         $starfound = false;
105         foreach ($questiondata->options->answers as $aid => $answer) {
106             $responses[$aid] = new question_possible_response($answer->answer,
107                     $answer->fraction);
108             if ($answer->answer === '*') {
109                 $starfound = true;
110             }
111         }
113         if (!$starfound) {
114             $responses[0] = new question_possible_response(
115                     get_string('didnotmatchanyanswer', 'question'), 0);
116         }
118         $responses[null] = question_possible_response::no_response();
120         return array($questiondata->id => $responses);
121     }