/question/type/shortanswer/question.php

https://bitbucket.org/ngmares/moodle · PHP · 142 lines · 82 code · 23 blank · 37 comment · 19 complexity · e8825981aa87cb438245b2fcc223a8e2 MD5 · raw file

  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/>.
  16. /**
  17. * Short answer question definition class.
  18. *
  19. * @package qtype
  20. * @subpackage shortanswer
  21. * @copyright 2009 The Open University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Represents a short answer question.
  27. *
  28. * @copyright 2009 The Open University
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class qtype_shortanswer_question extends question_graded_by_strategy
  32. implements question_response_answer_comparer {
  33. /** @var boolean whether answers should be graded case-sensitively. */
  34. public $usecase;
  35. /** @var array of question_answer. */
  36. public $answers = array();
  37. public function __construct() {
  38. parent::__construct(new question_first_matching_answer_grading_strategy($this));
  39. }
  40. public function get_expected_data() {
  41. return array('answer' => PARAM_RAW_TRIMMED);
  42. }
  43. public function summarise_response(array $response) {
  44. if (isset($response['answer'])) {
  45. return $response['answer'];
  46. } else {
  47. return null;
  48. }
  49. }
  50. public function is_complete_response(array $response) {
  51. return array_key_exists('answer', $response) &&
  52. ($response['answer'] || $response['answer'] === '0');
  53. }
  54. public function get_validation_error(array $response) {
  55. if ($this->is_gradable_response($response)) {
  56. return '';
  57. }
  58. return get_string('pleaseenterananswer', 'qtype_shortanswer');
  59. }
  60. public function is_same_response(array $prevresponse, array $newresponse) {
  61. return question_utils::arrays_same_at_key_missing_is_blank(
  62. $prevresponse, $newresponse, 'answer');
  63. }
  64. public function get_answers() {
  65. return $this->answers;
  66. }
  67. public function compare_response_with_answer(array $response, question_answer $answer) {
  68. return self::compare_string_with_wildcard(
  69. $response['answer'], $answer->answer, !$this->usecase);
  70. }
  71. public static function compare_string_with_wildcard($string, $pattern, $ignorecase) {
  72. // Break the string on non-escaped asterisks.
  73. $bits = preg_split('/(?<!\\\\)\*/', $pattern);
  74. // Escape regexp special characters in the bits.
  75. $excapedbits = array();
  76. foreach ($bits as $bit) {
  77. $excapedbits[] = preg_quote(str_replace('\*', '*', $bit));
  78. }
  79. // Put it back together to make the regexp.
  80. $regexp = '|^' . implode('.*', $excapedbits) . '$|u';
  81. // Make the match insensitive if requested to.
  82. if ($ignorecase) {
  83. $regexp .= 'i';
  84. }
  85. return preg_match($regexp, trim($string));
  86. }
  87. public function get_correct_response() {
  88. $response = parent::get_correct_response();
  89. if ($response) {
  90. $response['answer'] = $this->clean_response($response['answer']);
  91. }
  92. return $response;
  93. }
  94. public function clean_response($answer) {
  95. // Break the string on non-escaped asterisks.
  96. $bits = preg_split('/(?<!\\\\)\*/', $answer);
  97. // Unescape *s in the bits.
  98. $cleanbits = array();
  99. foreach ($bits as $bit) {
  100. $cleanbits[] = str_replace('\*', '*', $bit);
  101. }
  102. // Put it back together with spaces to look nice.
  103. return trim(implode(' ', $cleanbits));
  104. }
  105. public function check_file_access($qa, $options, $component, $filearea,
  106. $args, $forcedownload) {
  107. if ($component == 'question' && $filearea == 'answerfeedback') {
  108. $currentanswer = $qa->get_last_qt_var('answer');
  109. $answer = $qa->get_question()->get_matching_answer(array('answer' => $currentanswer));
  110. $answerid = reset($args); // itemid is answer id.
  111. return $options->feedback && $answerid == $answer->id;
  112. } else if ($component == 'question' && $filearea == 'hint') {
  113. return $this->check_hint_file_access($qa, $options, $args);
  114. } else {
  115. return parent::check_file_access($qa, $options, $component, $filearea,
  116. $args, $forcedownload);
  117. }
  118. }
  119. }