MDL-20636 Conversion of the description question type.
[moodle.git] / question / type / questionbase.php
CommitLineData
d1b7e03d
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 * This file defines the class {@link question_definition} and its subclasses.
21 *
22 * @package moodlecore
23 * @subpackage questiontypes
24 * @copyright 2009 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28
29/**
30 * The definition of a question of a particular type.
31 *
32 * This class is a close match to the question table in the database.
33 * Definitions of question of a particular type normally subclass one of the
34 * more specific classes {@link question_with_responses},
35 * {@link question_graded_automatically} or {@link question_information_item}.
36 *
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40abstract class question_definition {
41 /** @var integer id of the question in the datase, or null if this question
42 * is not in the database. */
43 public $id;
44
45 /** @var integer question category id. */
46 public $category;
47
48 /** @var integer parent question id. */
49 public $parent = 0;
50
51 /** @var question_type the question type this question is. */
52 public $qtype;
53
54 /** @var string question name. */
55 public $name;
56
57 /** @var string question text. */
58 public $questiontext;
59
60 /** @var integer question test format. */
61 public $questiontextformat;
62
63 /** @var string question general feedback. */
64 public $generalfeedback;
65
66 /** @var number what this quetsion is marked out of, by default. */
67 public $defaultmark = 1;
68
69 /** @var integer How many question numbers this question consumes. */
70 public $length = 1;
71
72 /** @var number penalty factor of this question. */
73 public $penalty = 0;
74
75 /** @var string unique identifier of this question. */
76 public $stamp;
77
78 /** @var string unique identifier of this version of this question. */
79 public $version;
80
81 /** @var boolean whethre this question has been deleted/hidden in the question bank. */
82 public $hidden = 0;
83
84 /** @var integer timestamp when this question was created. */
85 public $timecreated;
86
87 /** @var integer timestamp when this question was modified. */
88 public $timemodified;
89
90 /** @var integer userid of the use who created this question. */
91 public $createdby;
92
93 /** @var integer userid of the use who modified this question. */
94 public $modifiedby;
95
96 /** @var array of question_hints. */
97 public $hints = array();
98
99 /**
100 * Constructor. Normally to get a question, you call
101 * {@link question_bank::load_question()}, but questions can be created
102 * directly, for example in unit test code.
103 * @return unknown_type
104 */
105 public function __construct() {
106 }
107
108 /**
109 * @return the name of the question type (for example multichoice) that this
110 * question is.
111 */
112 public function get_type_name() {
113 return $this->qtype->name();
114 }
115
116 /**
117 * Creat the appropriate behaviour for an attempt at this quetsion,
118 * given the desired (archetypal) behaviour.
119 *
120 * This default implementation will suit most normal graded questions.
121 *
122 * If your question is of a patricular type, then it may need to do something
123 * different. For example, if your question can only be graded manually, then
124 * it should probably return a manualgraded behaviour, irrespective of
125 * what is asked for.
126 *
127 * If your question wants to do somthing especially complicated is some situations,
128 * then you may wish to return a particular behaviour related to the
129 * one asked for. For example, you migth want to return a
130 * qbehaviour_interactive_adapted_for_myqtype.
131 *
132 * @param question_attempt $qa the attempt we are creating an behaviour for.
133 * @param string $preferredbehaviour the requested type of behaviour.
134 * @return question_behaviour the new behaviour object.
135 */
136 public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
137 return question_engine::make_archetypal_behaviour($preferredbehaviour, $qa);
138 }
139
140 /**
141 * Initialise the first step of an attempt at this quetsion.
142 *
143 * For example, the multiple choice question type uses this method to
144 * randomly shuffle the choices, if that option has been set in the question.
145 * It then stores that order by calling $step->set_qt_var(...).
146 *
147 * @param question_attempt_step $step the step to be initialised.
148 */
149 public function init_first_step(question_attempt_step $step) {
150 }
151
152 /**
153 * Generate a brief, plain-text, summary of this question. This is used by
154 * various reports. This should show the particular variant of the question
155 * as presented to students. For example, the calculated quetsion type would
156 * fill in the particular numbers that were presented to the student.
157 * This method will return null if such a summary is not possible, or
158 * inappropriate.
159 * @return string|null a plain text summary of this question.
160 */
2b7da645
TH
161 public function get_question_summary(question_attempt $qa) {
162 return html_to_text($this->format_questiontext($qa), 0, false);
d1b7e03d
TH
163 }
164
165 /**
166 * Some questions can return a negative mark if the student gets it wrong.
167 *
168 * This method returns the lowest mark the question can return, on the
169 * fraction scale. that is, where the maximum possible mark is 1.0.
170 *
171 * @return number minimum fraction this question will ever return.
172 */
173 public function get_min_fraction() {
174 return 0;
175 }
176
177 /**
178 * Given a response, rest the parts that are wrong.
179 * @param array $response a response
180 * @return array a cleaned up response with the wrong bits reset.
181 */
182 public function clear_wrong_from_response(array $response) {
183 return array();
184 }
185
186 /**
187 * Return the number of subparts of this response that are right.
188 * @param array $response a response
189 * @return array with two elements, the number of correct subparts, and
190 * the total number of subparts.
191 */
192 public function get_num_parts_right(array $response) {
193 return array(null, null);
194 }
195
196 /**
197 * @return qtype_renderer the renderer to use for outputting this question.
198 */
199 public function get_renderer() {
c76145d3
TH
200 global $PAGE;
201 return $PAGE->get_renderer('qtype_' . $this->qtype->name());
d1b7e03d
TH
202 }
203
204 /**
205 * What data may be included in the form submission when a student submits
206 * this question in its current state?
207 *
208 * This information is used in calls to optional_param. The parameter name
209 * has {@link question_attempt::get_field_prefix()} automatically prepended.
210 *
211 * @return array|string variable name => PARAM_... constant, or, as a special case
212 * that should only be used in unavoidable, the constant question_attempt::USE_RAW_DATA
213 * meaning take all the raw submitted data belonging to this question.
214 */
215 public abstract function get_expected_data();
216
217 /**
218 * What data would need to be submitted to get this question correct.
219 * If there is more than one correct answer, this method should just
220 * return one possibility.
221 *
222 * @return array parameter name => value.
223 */
224 public abstract function get_correct_response();
225
226 /**
227 * Apply {@link format_text()} to some content with appropriate settings for
228 * this question.
229 *
230 * @param string $text some content that needs to be output.
068b4594
TH
231 * @param question_attempt $qa the question attempt.
232 * @param string $component used for rewriting file area URLs.
233 * @param string $filearea used for rewriting file area URLs.
d1b7e03d
TH
234 * @param boolean $clean Whether the HTML needs to be cleaned. Generally,
235 * parts of the question do not need to be cleaned, and student input does.
236 * @return string the text formatted for output by format_text.
237 */
2b7da645 238 public function format_text($text, $qa, $component, $filearea, $clean = false) {
d1b7e03d
TH
239 $formatoptions = new stdClass;
240 $formatoptions->noclean = !$clean;
241 $formatoptions->para = false;
2b7da645
TH
242// TODO $itemid needs to be an argument too.
243 $text = $qa->rewrite_pluginfile_urls($text, $component, $filearea);
d1b7e03d
TH
244 return format_text($text, $this->questiontextformat, $formatoptions);
245 }
246
247 /** @return the result of applying {@link format_text()} to the question text. */
2b7da645
TH
248 public function format_questiontext($qa) {
249 return $this->format_text($this->questiontext, $qa, 'question', 'questiontext');
d1b7e03d
TH
250 }
251
252 /** @return the result of applying {@link format_text()} to the general feedback. */
2b7da645
TH
253 public function format_generalfeedback($qa) {
254 return $this->format_text($this->generalfeedback, $qa, 'question', 'generalfeedback');
d1b7e03d
TH
255 }
256}
257
258
259/**
260 * This class represents a 'question' that actually does not allow the student
261 * to respond, like the description 'question' type.
262 *
263 * @copyright 2009 The Open University
264 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
265 */
266class question_information_item extends question_definition {
267 public function __construct() {
268 parent::__construct();
269 $this->defaultmark = 0;
270 $this->penalty = 0;
271 $this->length = 0;
272 }
273
274 public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
275 question_engine::load_behaviour_class('informationitem');
276 return new qbehaviour_informationitem($qa, $preferredbehaviour);
277 }
278
279 public function get_expected_data() {
280 return array();
281 }
282
283 public function get_correct_response() {
284 return array();
285 }
286
287 public function get_question_summary() {
288 return null;
289 }
290}
291
292
293/**
294 * Interface that a {@link question_definition} must implement to be usable by
295 * the manual graded behaviour.
296 *
297 * @copyright 2009 The Open University
298 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
299 */
300interface question_manually_gradable {
301 /**
302 * Used by many of the behaviours, to work out whether the student's
303 * response to the question is complete. That is, whether the question attempt
304 * should move to the COMPLETE or INCOMPLETE state.
305 *
306 * @param array $response responses, as returned by {@link question_attempt_step::get_qt_data()}.
307 * @return boolean whether this response is a complete answer to this question.
308 */
309 public function is_complete_response(array $response);
310
311 /**
312 * Use by many of the behaviours to determine whether the student's
313 * response has changed. This is normally used to determine that a new set
314 * of responses can safely be discarded.
315 *
316 * @param array $prevresponse the responses previously recorded for this question,
317 * as returned by {@link question_attempt_step::get_qt_data()}
318 * @param array $newresponse the new responses, in the same format.
319 * @return boolean whether the two sets of responses are the same - that is
320 * whether the new set of responses can safely be discarded.
321 */
322 public function is_same_response(array $prevresponse, array $newresponse);
323
324 /**
325 * Produce a plain text summary of a response.
326 * @param $response a response, as might be passed to {@link grade_response()}.
327 * @return string a plain text summary of that response, that could be used in reports.
328 */
329 public function summarise_response(array $response);
330
331 /**
332 * Categorise the student's response according to the categories defined by
333 * get_possible_responses.
334 * @param $response a response, as might be passed to {@link grade_response()}.
335 * @return array subpartid => {@link question_classified_response} objects.
336 * returns an empty array if no analysis is possible.
337 */
338 public function classify_response(array $response);
339}
340
341
342/**
343 * This class is used in the return value from
344 * {@link question_manually_gradable::classify_response()}.
345 *
346 * @copyright 2010 The Open University
347 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
348 */
349class question_classified_response {
350 /**
351 * @var string the classification of this response the student gave to this
352 * part of the question. Must match one of the responseclasses returned by
353 * {@link question_type::get_possible_responses()}.
354 */
355 public $responseclassid;
356 /** @var string the actual response the student gave to this part. */
357 public $response;
358 /** @var number the fraction this part of the response earned. */
359 public $fraction;
360 /**
361 * Constructor, just an easy way to set the fields.
362 * @param string $responseclassid see the field descriptions above.
363 * @param string $response see the field descriptions above.
364 * @param number $fraction see the field descriptions above.
365 */
366 public function __construct($responseclassid, $response, $fraction) {
367 $this->responseclassid = $responseclassid;
368 $this->response = $response;
369 $this->fraction = $fraction;
370 }
371
372 public static function no_response() {
373 return new question_classified_response(null, null, null);
374 }
375}
376
377
378/**
379 * Interface that a {@link question_definition} must implement to be usable by
380 * the various automatic grading behaviours.
381 *
382 * @copyright 2009 The Open University
383 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
384 */
385interface question_automatically_gradable extends question_manually_gradable {
386 /**
387 * Use by many of the behaviours to determine whether the student
388 * has provided enough of an answer for the question to be graded automatically,
389 * or whether it must be considered aborted.
390 *
391 * @param array $response responses, as returned by {@link question_attempt_step::get_qt_data()}.
392 * @return boolean whether this response can be graded.
393 */
394 public function is_gradable_response(array $response);
395
396 /**
397 * In situations where is_gradable_response() returns false, this method
398 * should generate a description of what the problem is.
399 * @return string the message.
400 */
401 public function get_validation_error(array $response);
402
403 /**
404 * Grade a response to the question, returning a fraction between get_min_fraction() and 1.0,
405 * and the corresponding state CORRECT, PARTIALLY_CORRECT or INCORRECT.
406 * @param array $response responses, as returned by {@link question_attempt_step::get_qt_data()}.
407 * @return array (number, integer) the fraction, and the state.
408 */
409 public function grade_response(array $response);
410
411 /**
412 * Get one of the question hints. The question_attempt is passed in case
413 * the question type wants to do something complex. For example, the
414 * multiple choice with multiple responses question type will turn off most
415 * of the hint options if the student has selected too many opitions.
416 * @param integer $hintnumber Which hint to display. Indexed starting from 0
417 * @param question_attempt $qa The question_attempt.
418 */
419 public function get_hint($hintnumber, question_attempt $qa);
420
421 /**
422 * Generate a brief, plain-text, summary of the correct answer to this question.
423 * This is used by various reports, and can also be useful when testing.
424 * This method will return null if such a summary is not possible, or
425 * inappropriate.
426 * @return string|null a plain text summary of the right answer to this question.
427 */
428 public function get_right_answer_summary();
429}
430
431
432/**
433 * Interface that a {@link question_definition} must implement to be usable by
434 * the interactivecountback behaviour.
435 *
436 * @copyright 2010 The Open University
437 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
438 */
439interface question_automatically_gradable_with_countback extends question_automatically_gradable {
440 /**
441 * Work out a final grade for this attempt, taking into account all the
442 * tries the student made.
443 * @param array $responses the response for each try. Each element of this
444 * array is a response array, as would be passed to {@link grade_response()}.
445 * There may be between 1 and $totaltries responses.
446 * @param integer $totaltries The maximum number of tries allowed.
447 * @return numeric the fraction that should be awarded for this
448 * sequence of response.
449 */
450 public function compute_final_grade($responses, $totaltries);
451}
452
453
454/**
455 * This class represents a real question. That is, one that is not a
456 * {@link question_information_item}.
457 *
458 * @copyright 2009 The Open University
459 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
460 */
461abstract class question_with_responses extends question_definition
462 implements question_manually_gradable {
463 function classify_response(array $response) {
464 return array();
465 }
466}
467
468
469/**
470 * This class represents a question that can be graded automatically.
471 *
472 * @copyright 2009 The Open University
473 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
474 */
475abstract class question_graded_automatically extends question_with_responses
476 implements question_automatically_gradable {
477 /** @var Some question types have the option to show the number of sub-parts correct. */
478 public $shownumcorrect = false;
479
480 public function is_gradable_response(array $response) {
481 return $this->is_complete_response($response);
482 }
483
484 public function get_right_answer_summary() {
485 $correctresponse = $this->get_correct_response();
486 if (empty($correctresponse)) {
487 return null;
488 }
489 return $this->summarise_response($correctresponse);
490 }
491
492 public function get_hint($hintnumber, question_attempt $qa) {
493 if (!isset($this->hints[$hintnumber])) {
494 return null;
495 }
496 return $this->hints[$hintnumber];
497 }
498}
499
500
501/**
502 * This class represents a question that can be graded automatically with
503 * countback grading in interactive mode.
504 *
505 * @copyright 2010 The Open University
506 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
507 */
508abstract class question_graded_automatically_with_countback
509 extends question_graded_automatically
510 implements question_automatically_gradable_with_countback {
511
512 public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
513 if ($preferredbehaviour == 'interactive') {
514 return question_engine::make_behaviour('interactivecountback', $qa, $preferredbehaviour);
515 }
516 return question_engine::make_archetypal_behaviour($preferredbehaviour, $qa);
517 }
518}
519
520
521/**
522 * This class represents a question that can be graded automatically by using
523 * a {@link question_grading_strategy}.
524 *
525 * @copyright 2009 The Open University
526 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
527 */
528abstract class question_graded_by_strategy extends question_graded_automatically {
529 /** @var question_grading_strategy the strategy to use for grading. */
530 protected $gradingstrategy;
531
532 /** @param question_grading_strategy $strategy the strategy to use for grading. */
533 public function __construct(question_grading_strategy $strategy) {
534 parent::__construct();
535 $this->gradingstrategy = $strategy;
536 }
537
538 public function get_correct_response() {
539 $answer = $this->get_correct_answer();
540 if (!$answer) {
541 return array();
542 }
543
544 return array('answer' => $answer->answer);
545 }
546
547 /**
548 * Get an answer that contains the feedback and fraction that should be
549 * awarded for this resonse.
550 * @param array $response a response.
551 * @return question_answer the matching answer.
552 */
553 public function get_matching_answer(array $response) {
554 return $this->gradingstrategy->grade($response);
555 }
556
557 /**
558 * @return question_answer an answer that contains the a response that would
559 * get full marks.
560 */
561 public function get_correct_answer() {
562 return $this->gradingstrategy->get_correct_answer();
563 }
564
565 public function grade_response(array $response) {
566 $answer = $this->get_matching_answer($response);
567 if ($answer) {
568 return array($answer->fraction, question_state::graded_state_for_fraction($answer->fraction));
569 } else {
570 return array(0, question_state::$gradedwrong);
571 }
572 }
573
574 public function classify_response(array $response) {
575 if (empty($response['answer'])) {
576 return array($this->id => question_classified_response::no_response());
577 }
578
579 $ans = $this->get_matching_answer($response);
580 if (!$ans) {
581 return array($this->id => question_classified_response::no_response());
582 }
583 return array($this->id => new question_classified_response(
584 $ans->id, $response['answer'], $ans->fraction));
585 }
586}
587
588
589/**
590 * Class to represent a question answer, loaded from the question_answers table
591 * in the database.
592 *
593 * @copyright 2009 The Open University
594 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
595 */
596class question_answer {
597 /** @var string the answer. */
598 public $answer;
599
600 /** @var number the fraction this answer is worth. */
601 public $fraction;
602
603 /** @var string the feedback for this answer. */
604 public $feedback;
605
606 /**
607 * Constructor.
608 * @param string $answer the answer.
609 * @param number $fraction the fraction this answer is worth.
610 * @param string $feedback the feedback for this answer.
611 */
612 public function __construct($answer, $fraction, $feedback) {
613 $this->answer = $answer;
614 $this->fraction = $fraction;
615 $this->feedback = $feedback;
616 }
617}
618
619
620/**
621 * Class to represent a hint associated with a question.
622 * Used by iteractive mode, etc. A question has an array of these.
623 *
624 * @copyright 2010 The Open University
625 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
626 */
627class question_hint {
628 /** @var The feedback hint to be shown. */
629 public $hint;
630
631 /**
632 * Constructor.
633 * @param string $hint The hint text
634 */
635 public function __construct($hint) {
636 $this->hint = $hint;
637 }
638
639 /**
640 * Create a basic hint from a row loaded from the question_hints table in the database.
641 * @param object $row with $row->hint set.
642 * @return question_hint
643 */
644 public static function load_from_record($row) {
645 return new question_hint($row->hint);
646 }
647
648 /**
649 * Adjust this display options according to the hint settings.
650 * @param question_display_options $options
651 */
652 public function adjust_display_options(question_display_options $options) {
653 // Do nothing.
654 }
655}
656
657
658/**
659 * An extension of {@link question_hint} for questions like match and multiple
660 * choice with multile answers, where there are options for whether to show the
661 * number of parts right at each stage, and to reset the wrong parts.
662 *
663 * @copyright 2010 The Open University
664 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
665 */
666class question_hint_with_parts extends question_hint {
667 /** @var boolean option to show the number of sub-parts of the question that were right. */
668 public $shownumcorrect;
669
670 /** @var boolean option to clear the parts of the question that were wrong on retry. */
671 public $clearwrong;
672
673 /**
674 * Constructor.
675 * @param string $hint The hint text
676 * @param boolean $shownumcorrect whether the number of right parts should be shown
677 * @param boolean $clearwrong whether the wrong parts should be reset.
678 */
679 public function __construct($hint, $shownumcorrect, $clearwrong) {
680 parent::__construct($hint);
681 $this->shownumcorrect = $shownumcorrect;
682 $this->clearwrong = $clearwrong;
683 }
684
685 /**
686 * Create a basic hint from a row loaded from the question_hints table in the database.
687 * @param object $row with $row->hint, ->shownumcorrect and ->clearwrong set.
688 * @return question_hint_with_parts
689 */
690 public static function load_from_record($row) {
691 return new question_hint_with_parts($row->hint, $row->shownumcorrect, $row->clearwrong);
692 }
693
694 public function adjust_display_options(question_display_options $options) {
695 parent::adjust_display_options($options);
696 if ($this->clearwrong) {
697 $options->clearwrong = true;
698 }
699 $options->numpartscorrect = $this->shownumcorrect;
700 }
701}
702
703
704/**
705 * This question_grading_strategy interface. Used to share grading code between
706 * questions that that subclass {@link question_graded_by_strategy}.
707 *
708 * @copyright 2009 The Open University
709 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
710 */
711interface question_grading_strategy {
712 /**
713 * Return a question answer that describes the outcome (fraction and feeback)
714 * for a particular respons.
715 * @param array $response the response.
716 * @return question_answer the answer describing the outcome.
717 */
718 public function grade(array $response);
719
720 /**
721 * @return question_answer an answer that contains the a response that would
722 * get full marks.
723 */
724 public function get_correct_answer();
725}
726
727
728/**
729 * This interface defines the methods that a {@link question_definition} must
730 * implement if it is to be graded by the
731 * {@link question_first_matching_answer_grading_strategy}.
732 *
733 * @copyright 2009 The Open University
734 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
735 */
736interface question_response_answer_comparer {
737 /** @return array of {@link question_answers}. */
738 public function get_answers();
739
740 /**
741 * @param array $response the response.
742 * @param question_answer $answer an answer.
743 * @return boolean whether the response matches the answer.
744 */
745 public function compare_response_with_answer(array $response, question_answer $answer);
746}
747
748
749/**
750 * This grading strategy is used by question types like shortanswer an numerical.
751 * It gets a list of possible answers from the question, and returns the first one
752 * that matches the given response. It returns the first answer with fraction 1.0
753 * when asked for the correct answer.
754 *
755 * @copyright 2009 The Open University
756 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
757 */
758class question_first_matching_answer_grading_strategy implements question_grading_strategy {
759 /**
760 * @var question_response_answer_comparer (presumably also a
761 * {@link question_definition}) the question we are doing the grading for.
762 */
763 protected $question;
764
765 /**
766 * @param question_response_answer_comparer $question (presumably also a
767 * {@link question_definition}) the question we are doing the grading for.
768 */
769 public function __construct(question_response_answer_comparer $question) {
770 $this->question = $question;
771 }
772
773 public function grade(array $response) {
774 foreach ($this->question->get_answers() as $aid => $answer) {
775 if ($this->question->compare_response_with_answer($response, $answer)) {
776 $answer->id = $aid;
777 return $answer;
778 }
779 }
780 return null;
781 }
782
783 public function get_correct_answer() {
784 foreach ($this->question->get_answers() as $answer) {
785 $state = question_state::graded_state_for_fraction($answer->fraction);
786 if ($state == question_state::$gradedright) {
787 return $answer;
788 }
789 }
790 return null;
791 }
792}