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