PageRenderTime 77ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/numerical/question.php

https://github.com/kpike/moodle
PHP | 356 lines | 238 code | 59 blank | 59 comment | 72 complexity | 78ee0163e000f18f08ab86b34349dd78 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. * Numerical question definition class.
  18. *
  19. * @package qtype
  20. * @subpackage numerical
  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. require_once($CFG->dirroot . '/question/type/numerical/questiontype.php');
  26. /**
  27. * Represents a numerical question.
  28. *
  29. * @copyright 2009 The Open University
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class qtype_numerical_question extends question_graded_automatically {
  33. /** @var array of question_answer. */
  34. public $answers = array();
  35. /** @var int one of the constants UNITNONE, UNITRADIO, UNITSELECT or UNITINPUT. */
  36. public $unitdisplay;
  37. /** @var int one of the constants UNITGRADEDOUTOFMARK or UNITGRADEDOUTOFMAX. */
  38. public $unitgradingtype;
  39. /** @var number the penalty for a missing or unrecognised unit. */
  40. public $unitpenalty;
  41. /** @var qtype_numerical_answer_processor */
  42. public $ap;
  43. public function get_expected_data() {
  44. $expected = array('answer' => PARAM_RAW_TRIMMED);
  45. if ($this->has_separate_unit_field()) {
  46. $expected['unit'] = PARAM_RAW_TRIMMED;
  47. }
  48. return $expected;
  49. }
  50. public function has_separate_unit_field() {
  51. return $this->unitdisplay == qtype_numerical::UNITRADIO ||
  52. $this->unitdisplay == qtype_numerical::UNITSELECT;
  53. }
  54. public function start_attempt(question_attempt_step $step, $variant) {
  55. $step->set_qt_var('_separators',
  56. $this->ap->get_point() . '$' . $this->ap->get_separator());
  57. }
  58. public function apply_attempt_state(question_attempt_step $step) {
  59. list($point, $separator) = explode('$', $step->get_qt_var('_separators'));
  60. $this->ap->set_characters($point, $separator);
  61. }
  62. public function summarise_response(array $response) {
  63. if (isset($response['answer'])) {
  64. $resp = $response['answer'];
  65. } else {
  66. $resp = null;
  67. }
  68. if ($this->has_separate_unit_field() && !empty($response['unit'])) {
  69. $resp = $this->ap->add_unit($resp, $response['unit']);
  70. }
  71. return $resp;
  72. }
  73. public function is_gradable_response(array $response) {
  74. return array_key_exists('answer', $response) &&
  75. ($response['answer'] || $response['answer'] === '0' || $response['answer'] === 0);
  76. }
  77. public function is_complete_response(array $response) {
  78. if (!$this->is_gradable_response($response)) {
  79. return false;
  80. }
  81. list($value, $unit) = $this->ap->apply_units($response['answer']);
  82. if (is_null($value)) {
  83. return false;
  84. }
  85. if ($this->unitdisplay != qtype_numerical::UNITINPUT && $unit) {
  86. return false;
  87. }
  88. if ($this->has_separate_unit_field() && empty($response['unit'])) {
  89. return false;
  90. }
  91. if ($this->ap->contains_thousands_seaparator($response['answer'])) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. public function get_validation_error(array $response) {
  97. if (!$this->is_gradable_response($response)) {
  98. return get_string('pleaseenterananswer', 'qtype_numerical');
  99. }
  100. list($value, $unit) = $this->ap->apply_units($response['answer']);
  101. if (is_null($value)) {
  102. return get_string('invalidnumber', 'qtype_numerical');
  103. }
  104. if ($this->unitdisplay != qtype_numerical::UNITINPUT && $unit) {
  105. return get_string('invalidnumbernounit', 'qtype_numerical');
  106. }
  107. if ($this->has_separate_unit_field() && empty($response['unit'])) {
  108. return get_string('unitnotselected', 'qtype_numerical');
  109. }
  110. if ($this->ap->contains_thousands_seaparator($response['answer'])) {
  111. return get_string('pleaseenteranswerwithoutthousandssep', 'qtype_numerical',
  112. $this->ap->get_separator());
  113. }
  114. return '';
  115. }
  116. public function is_same_response(array $prevresponse, array $newresponse) {
  117. if (!question_utils::arrays_same_at_key_missing_is_blank(
  118. $prevresponse, $newresponse, 'answer')) {
  119. return false;
  120. }
  121. if ($this->has_separate_unit_field()) {
  122. return question_utils::arrays_same_at_key_missing_is_blank(
  123. $prevresponse, $newresponse, 'unit');
  124. }
  125. return false;
  126. }
  127. public function get_correct_response() {
  128. $answer = $this->get_correct_answer();
  129. if (!$answer) {
  130. return array();
  131. }
  132. $response = array('answer' => str_replace('.', $this->ap->get_point(), $answer->answer));
  133. if ($this->has_separate_unit_field()) {
  134. $response['unit'] = $this->ap->get_default_unit();
  135. } else if ($this->unitdisplay == qtype_numerical::UNITINPUT) {
  136. $response['answer'] = $this->ap->add_unit($answer->answer);
  137. }
  138. return $response;
  139. }
  140. /**
  141. * Get an answer that contains the feedback and fraction that should be
  142. * awarded for this resonse.
  143. * @param number $value the numerical value of a response.
  144. * @param number $multiplier for the unit the student gave, if any. When no
  145. * unit was given, or an unrecognised unit was given, $multiplier will be null.
  146. * @return question_answer the matching answer.
  147. */
  148. public function get_matching_answer($value, $multiplier) {
  149. if (!is_null($multiplier)) {
  150. $scaledvalue = $value * $multiplier;
  151. } else {
  152. $scaledvalue = $value;
  153. }
  154. foreach ($this->answers as $aid => $answer) {
  155. if ($answer->within_tolerance($scaledvalue)) {
  156. $answer->unitisright = !is_null($multiplier);
  157. return $answer;
  158. } else if ($answer->within_tolerance($value)) {
  159. $answer->unitisright = false;
  160. return $answer;
  161. }
  162. }
  163. return null;
  164. }
  165. public function get_correct_answer() {
  166. foreach ($this->answers as $answer) {
  167. $state = question_state::graded_state_for_fraction($answer->fraction);
  168. if ($state == question_state::$gradedright) {
  169. return $answer;
  170. }
  171. }
  172. return null;
  173. }
  174. /**
  175. * Adjust the fraction based on whether the unit was correct.
  176. * @param number $fraction
  177. * @param bool $unitisright
  178. * @return number
  179. */
  180. public function apply_unit_penalty($fraction, $unitisright) {
  181. if ($unitisright) {
  182. return $fraction;
  183. }
  184. if ($this->unitgradingtype == qtype_numerical::UNITGRADEDOUTOFMARK) {
  185. $fraction -= $this->unitpenalty * $fraction;
  186. } else if ($this->unitgradingtype == qtype_numerical::UNITGRADEDOUTOFMAX) {
  187. $fraction -= $this->unitpenalty;
  188. }
  189. return max($fraction, 0);
  190. }
  191. public function grade_response(array $response) {
  192. if ($this->has_separate_unit_field()) {
  193. $selectedunit = $response['unit'];
  194. } else {
  195. $selectedunit = null;
  196. }
  197. list($value, $unit, $multiplier) = $this->ap->apply_units(
  198. $response['answer'], $selectedunit);
  199. $answer = $this->get_matching_answer($value, $multiplier);
  200. if (!$answer) {
  201. return array(0, question_state::$gradedwrong);
  202. }
  203. $fraction = $this->apply_unit_penalty($answer->fraction, $answer->unitisright);
  204. return array($fraction, question_state::graded_state_for_fraction($fraction));
  205. }
  206. public function classify_response(array $response) {
  207. if (empty($response['answer'])) {
  208. return array($this->id => question_classified_response::no_response());
  209. }
  210. if ($this->has_separate_unit_field()) {
  211. $selectedunit = $response['unit'];
  212. } else {
  213. $selectedunit = null;
  214. }
  215. list($value, $unit, $multiplier) = $this->ap->apply_units($response['answer'], $selectedunit);
  216. $ans = $this->get_matching_answer($value, $multiplier);
  217. if (!$ans) {
  218. return array($this->id => question_classified_response::no_response());
  219. }
  220. $resp = $response['answer'];
  221. if ($this->has_separate_unit_field()) {
  222. $resp = $this->ap->add_unit($resp, $unit);
  223. }
  224. return array($this->id => new question_classified_response($ans->id,
  225. $resp,
  226. $this->apply_unit_penalty($ans->fraction, $ans->unitisright)));
  227. }
  228. public function check_file_access($qa, $options, $component, $filearea, $args,
  229. $forcedownload) {
  230. if ($component == 'question' && $filearea == 'answerfeedback') {
  231. $question = $qa->get_question();
  232. $currentanswer = $qa->get_last_qt_var('answer');
  233. if ($this->has_separate_unit_field()) {
  234. $selectedunit = $qa->get_last_qt_var('unit');
  235. } else {
  236. $selectedunit = null;
  237. }
  238. list($value, $unit, $multiplier) = $question->ap->apply_units(
  239. $currentanswer, $selectedunit);
  240. $answer = $question->get_matching_answer($value, $multiplier);
  241. $answerid = reset($args); // itemid is answer id.
  242. return $options->feedback && $answerid == $answer->id;
  243. } else if ($component == 'question' && $filearea == 'hint') {
  244. return $this->check_hint_file_access($qa, $options, $args);
  245. } else {
  246. return parent::check_file_access($qa, $options, $component, $filearea,
  247. $args, $forcedownload);
  248. }
  249. }
  250. }
  251. /**
  252. * Subclass of {@link question_answer} with the extra information required by
  253. * the numerical question type.
  254. *
  255. * @copyright 2009 The Open University
  256. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  257. */
  258. class qtype_numerical_answer extends question_answer {
  259. /** @var float allowable margin of error. */
  260. public $tolerance;
  261. /** @var integer|string see {@link get_tolerance_interval()} for the meaning of this value. */
  262. public $tolerancetype = 2;
  263. public function __construct($id, $answer, $fraction, $feedback, $feedbackformat, $tolerance) {
  264. parent::__construct($id, $answer, $fraction, $feedback, $feedbackformat);
  265. $this->tolerance = abs($tolerance);
  266. }
  267. public function get_tolerance_interval() {
  268. if ($this->answer === '*') {
  269. throw new coding_exception('Cannot work out tolerance interval for answer *.');
  270. }
  271. // We need to add a tiny fraction depending on the set precision to make
  272. // the comparison work correctly, otherwise seemingly equal values can
  273. // yield false. See MDL-3225.
  274. $tolerance = (float) $this->tolerance + pow(10, -1 * ini_get('precision'));
  275. switch ($this->tolerancetype) {
  276. case 1: case 'relative':
  277. $range = abs($this->answer) * $tolerance;
  278. return array($this->answer - $range, $this->answer + $range);
  279. case 2: case 'nominal':
  280. $tolerance = $this->tolerance + pow(10, -1 * ini_get('precision')) *
  281. max(1, abs($this->answer));
  282. return array($this->answer - $tolerance, $this->answer + $tolerance);
  283. case 3: case 'geometric':
  284. $quotient = 1 + abs($tolerance);
  285. return array($this->answer / $quotient, $this->answer * $quotient);
  286. default:
  287. throw new coding_exception('Unknown tolerance type ' . $this->tolerancetype);
  288. }
  289. }
  290. public function within_tolerance($value) {
  291. if ($this->answer === '*') {
  292. return true;
  293. }
  294. list($min, $max) = $this->get_tolerance_interval();
  295. return $min <= $value && $value <= $max;
  296. }
  297. }