MDL-20636 Conversion of the shortanswer question type to the new question engine.
[moodle.git] / question / type / shortanswer / question.php
CommitLineData
068b4594
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Short answer question definition class.
21 *
22 * @package qtype_shortanswer
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28/**
29 * Represents a short answer question.
30 *
31 * @copyright 2009 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class qtype_shortanswer_question extends question_graded_by_strategy
35 implements question_response_answer_comparer {
36 /** @var boolean whether answers should be graded case-sensitively. */
37 public $usecase;
38 /** @var array of question_answer. */
39 public $answers = array();
40
41 public function __construct() {
42 parent::__construct(new question_first_matching_answer_grading_strategy($this));
43 }
44
45 public function get_expected_data() {
46 return array('answer' => PARAM_TRIM);
47 }
48
49 public function summarise_response(array $response) {
50 if (isset($response['answer'])) {
51 return $response['answer'];
52 } else {
53 return null;
54 }
55 }
56
57 public function is_complete_response(array $response) {
58 return array_key_exists('answer', $response) &&
59 ($response['answer'] || $response['answer'] === '0');
60 }
61
62 public function get_validation_error(array $response) {
63 if ($this->is_gradable_response($response)) {
64 return '';
65 }
66 return get_string('pleaseenterananswer', 'qtype_shortanswer');
67 }
68
69 public function is_same_response(array $prevresponse, array $newresponse) {
70 return question_utils::arrays_same_at_key_missing_is_blank(
71 $prevresponse, $newresponse, 'answer');
72 }
73
74 public function get_answers() {
75 return $this->answers;
76 }
77
78 public function compare_response_with_answer(array $response, question_answer $answer) {
79 return self::compare_string_with_wildcard($response['answer'], $answer->answer, !$this->usecase);
80 }
81
82 public static function compare_string_with_wildcard($string, $pattern, $ignorecase) {
83 // Break the string on non-escaped asterisks.
84 $bits = preg_split('/(?<!\\\\)\*/', $pattern);
85 // Escape regexp special characters in the bits.
86 $excapedbits = array();
87 foreach ($bits as $bit) {
88 $excapedbits[] = preg_quote(str_replace('\*', '*', $bit));
89 }
90 // Put it back together to make the regexp.
91 $regexp = '|^' . implode('.*', $excapedbits) . '$|u';
92
93 // Make the match insensitive if requested to.
94 if ($ignorecase) {
95 $regexp .= 'i';
96 }
97
98 return preg_match($regexp, trim($string));
99 }
100}