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