weekly release 2.6dev
[moodle.git] / question / behaviour / behaviourbase.php
CommitLineData
d1b7e03d 1<?php
d1b7e03d
TH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
d1b7e03d
TH
17/**
18 * Defines the quetsion behaviour base class
19 *
017bc1d9 20 * @package moodlecore
d1b7e03d 21 * @subpackage questionbehaviours
017bc1d9
TH
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
d1b7e03d
TH
30/**
31 * The base class for question behaviours.
32 *
33 * A question behaviour is used by the question engine, specifically by
34 * a {@link question_attempt} to manage the flow of actions a student can take
35 * as they work through a question, and later, as a teacher manually grades it.
36 * In turn, the behaviour will delegate certain processing to the
37 * relevant {@link question_definition}.
38 *
017bc1d9
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_behaviour {
43 /**
44 * Certain behaviours are definitive of a way that questions can
45 * behave when attempted. For example deferredfeedback model, interactive
46 * model, etc. These are the options that should be listed in the
47 * user-interface. These models should define the class constant
48 * IS_ARCHETYPAL as true. Other models are more implementation details, for
49 * example the informationitem model, or a special subclass like
50 * interactive_adapted_for_my_qtype. These models should IS_ARCHETYPAL as
51 * false.
52 * @var boolean
53 */
54 const IS_ARCHETYPAL = false;
55
56 /** @var question_attempt the question attempt we are managing. */
57 protected $qa;
58 /** @var question_definition shortcut to $qa->get_question(). */
59 protected $question;
60
61 /**
62 * Normally you should not call this constuctor directly. The appropriate
63 * behaviour object is created automatically as part of
64 * {@link question_attempt::start()}.
65 * @param question_attempt $qa the question attempt we will be managing.
66 * @param string $preferredbehaviour the type of behaviour that was actually
67 * requested. This information is not needed in most cases, the type of
68 * subclass is enough, but occasionally it is needed.
69 */
70 public function __construct(question_attempt $qa, $preferredbehaviour) {
71 $this->qa = $qa;
72 $this->question = $qa->get_question();
15d660a8 73 if (!$this->is_compatible_question($this->question)) {
88f0eb15 74 throw new coding_exception('This behaviour (' . $this->get_name() .
d1b7e03d
TH
75 ') cannot work with this question (' . get_class($this->question) . ')');
76 }
77 }
78
15d660a8
TH
79 /**
80 * Some behaviours can only work with certing types of question. This method
81 * allows the behaviour to verify that a question is compatible.
3d17cd3f
TH
82 *
83 * This implementation is only provided for backwards-compatibility. You should
84 * override this method if you are implementing a behaviour.
85 *
15d660a8
TH
86 * @param question_definition $question the question.
87 */
06616f4b 88 public abstract function is_compatible_question(question_definition $question);
d1b7e03d
TH
89
90 /**
91 * @return string the name of this behaviour. For example the name of
92 * qbehaviour_mymodle is 'mymodel'.
93 */
94 public function get_name() {
95 return substr(get_class($this), 11);
96 }
97
98 /**
99 * 'Override' this method if there are some display options that do not make
100 * sense 'during the attempt'.
101 * @return array of {@link question_display_options} field names, that are
102 * not relevant to this behaviour before a 'finish' action.
103 */
104 public static function get_unused_display_options() {
105 return array();
106 }
107
108 /**
109 * Cause the question to be renderered. This gets the appropriate behaviour
110 * renderer using {@link get_renderer()}, and adjusts the display
111 * options using {@link adjust_display_options()} and then calls
112 * {@link core_question_renderer::question()} to do the work.
113 * @param question_display_options $options controls what should and should not be displayed.
114 * @param unknown_type $number the question number to display.
115 * @param core_question_renderer $qoutput the question renderer that will coordinate everything.
116 * @param qtype_renderer $qtoutput the question type renderer that will be helping.
117 * @return HTML fragment.
118 */
119 public function render(question_display_options $options, $number,
120 core_question_renderer $qoutput, qtype_renderer $qtoutput) {
2daffca5 121 $behaviouroutput = $this->get_renderer($qoutput->get_page());
d1b7e03d
TH
122 $options = clone($options);
123 $this->adjust_display_options($options);
124 return $qoutput->question($this->qa, $behaviouroutput, $qtoutput, $options, $number);
125 }
126
7a719748
TH
127 /**
128 * Checks whether the users is allow to be served a particular file.
129 * @param question_display_options $options the options that control display of the question.
130 * @param string $component the name of the component we are serving files for.
131 * @param string $filearea the name of the file area.
132 * @param array $args the remaining bits of the file path.
f7970e3c
TH
133 * @param bool $forcedownload whether the user must be forced to download the file.
134 * @return bool true if the user can access this file.
7a719748
TH
135 */
136 public function check_file_access($options, $component, $filearea, $args, $forcedownload) {
137 $this->adjust_display_options($options);
1631ceea
TH
138 return $this->question->check_file_access($this->qa, $options, $component,
139 $filearea, $args, $forcedownload);
7a719748
TH
140 }
141
d1b7e03d 142 /**
2daffca5 143 * @param moodle_page $page the page to render for.
d1b7e03d
TH
144 * @return qbehaviour_renderer get the appropriate renderer to use for this model.
145 */
2daffca5
TH
146 public function get_renderer(moodle_page $page) {
147 return $page->get_renderer(get_class($this));
d1b7e03d
TH
148 }
149
150 /**
151 * Make any changes to the display options before a question is rendered, so
152 * that it can be displayed in a way that is appropriate for the statue it is
153 * currently in. For example, by default, if the question is finished, we
154 * ensure that it is only ever displayed read-only.
155 * @param question_display_options $options the options to adjust. Just change
156 * the properties of this object - objects are passed by referece.
157 */
158 public function adjust_display_options(question_display_options $options) {
159 if (!$this->qa->has_marks()) {
160 $options->correctness = false;
161 $options->numpartscorrect = false;
162 }
163 if ($this->qa->get_state()->is_finished()) {
164 $options->readonly = true;
165 $options->numpartscorrect = $options->numpartscorrect &&
166 $this->qa->get_state()->is_partially_correct() &&
167 !empty($this->question->shownumcorrect);
168 } else {
169 $options->hide_all_feedback();
170 }
171 }
172
173 /**
174 * Get the most applicable hint for the question in its current state.
175 * @return question_hint the most applicable hint, or null, if none.
176 */
177 public function get_applicable_hint() {
178 return null;
179 }
180
181 /**
182 * What is the minimum fraction that can be scored for this question.
ef31a283 183 * Normally this will be based on $this->question->get_min_fraction(),
d1b7e03d
TH
184 * but may be modified in some way by the model.
185 *
186 * @return number the minimum fraction when this question is attempted under
187 * this model.
188 */
189 public function get_min_fraction() {
190 return 0;
191 }
192
193 /**
194 * Adjust a random guess score for a question using this model. You have to
195 * do this without knowing details of the specific question, or which usage
196 * it is in.
197 * @param number $fraction the random guess score from the question type.
198 * @return number the adjusted fraction.
199 */
200 public static function adjust_random_guess_score($fraction) {
201 return $fraction;
202 }
203
204 /**
205 * Return an array of the behaviour variables that could be submitted
206 * as part of a question of this type, with their types, so they can be
207 * properly cleaned.
208 * @return array variable name => PARAM_... constant.
209 */
210 public function get_expected_data() {
211 if (!$this->qa->get_state()->is_finished()) {
212 return array();
213 }
214
215 $vars = array('comment' => PARAM_RAW);
216 if ($this->qa->get_max_mark()) {
217 $vars['mark'] = question_attempt::PARAM_MARK;
61cca0b7 218 $vars['maxmark'] = PARAM_FLOAT;
d1b7e03d
TH
219 }
220 return $vars;
221 }
222
223 /**
224 * Return an array of question type variables for the question in its current
225 * state. Normally, if {@link adjust_display_options()} would set
226 * {@link question_display_options::$readonly} to true, then this method
227 * should return an empty array, otherwise it should return
228 * $this->question->get_expected_data(). Thus, there should be little need to
229 * override this method.
230 * @return array|string variable name => PARAM_... constant, or, as a special case
231 * that should only be used in unavoidable, the constant question_attempt::USE_RAW_DATA
232 * meaning take all the raw submitted data belonging to this question.
233 */
234 public function get_expected_qt_data() {
235 $fakeoptions = new question_display_options();
236 $fakeoptions->readonly = false;
237 $this->adjust_display_options($fakeoptions);
238 if ($fakeoptions->readonly) {
239 return array();
240 } else {
241 return $this->question->get_expected_data();
242 }
243 }
244
245 /**
246 * Return an array of any im variables, and the value required to get full
247 * marks.
248 * @return array variable name => value.
249 */
250 public function get_correct_response() {
251 return array();
252 }
253
254 /**
255 * Generate a brief, plain-text, summary of this question. This is used by
256 * various reports. This should show the particular variant of the question
257 * as presented to students. For example, the calculated quetsion type would
258 * fill in the particular numbers that were presented to the student.
259 * This method will return null if such a summary is not possible, or
260 * inappropriate.
261 *
262 * Normally, this method delegates to {question_definition::get_question_summary()}.
263 *
264 * @return string|null a plain text summary of this question.
265 */
266 public function get_question_summary() {
ec3d4ef5 267 return $this->question->get_question_summary();
d1b7e03d
TH
268 }
269
270 /**
271 * Generate a brief, plain-text, summary of the correct answer to this question.
272 * This is used by various reports, and can also be useful when testing.
273 * This method will return null if such a summary is not possible, or
274 * inappropriate.
275 *
276 * @return string|null a plain text summary of the right answer to this question.
277 */
278 public function get_right_answer_summary() {
279 return null;
280 }
281
282 /**
283 * Used by {@link start_based_on()} to get the data needed to start a new
284 * attempt from the point this attempt has go to.
285 * @return array name => value pairs.
286 */
287 public function get_resume_data() {
288 $olddata = $this->qa->get_step(0)->get_all_data();
289 $olddata = $this->qa->get_last_qt_data() + $olddata;
290 $olddata = $this->get_our_resume_data() + $olddata;
291 return $olddata;
292 }
293
294 /**
295 * Used by {@link start_based_on()} to get the data needed to start a new
296 * attempt from the point this attempt has go to.
297 * @return unknown_type
298 */
299 protected function get_our_resume_data() {
300 return array();
301 }
302
303 /**
304 * @return array subpartid => object with fields
1631ceea
TH
305 * ->responseclassid matches one of the values returned from
306 * quetion_type::get_possible_responses.
d1b7e03d
TH
307 * ->response the actual response the student gave to this part, as a string.
308 * ->fraction the credit awarded for this subpart, may be null.
309 * returns an empty array if no analysis is possible.
310 */
311 public function classify_response() {
312 return $this->question->classify_response($this->qa->get_last_qt_data());
313 }
314
315 /**
316 * Generate a brief textual description of the current state of the question,
317 * normally displayed under the question number.
318 *
f7970e3c 319 * @param bool $showcorrectness Whether right/partial/wrong states should
d1b7e03d
TH
320 * be distinguised.
321 * @return string a brief summary of the current state of the qestion attempt.
322 */
323 public function get_state_string($showcorrectness) {
324 return $this->qa->get_state()->default_string($showcorrectness);
325 }
326
c7df5006 327 public abstract function summarise_action(question_attempt_step $step);
d1b7e03d
TH
328
329 /**
ef31a283
TH
330 * Initialise the first step in a question attempt when a new
331 * {@link question_attempt} is being started.
d1b7e03d 332 *
ef31a283 333 * This method must call $this->question->start_attempt($step), and may
d1b7e03d
TH
334 * perform additional processing if the model requries it.
335 *
ef31a283
TH
336 * @param question_attempt_step $step the first step of the
337 * question_attempt being started.
1da821bb 338 * @param int $variant which variant of the question to use.
d1b7e03d 339 */
1da821bb
TH
340 public function init_first_step(question_attempt_step $step, $variant) {
341 $this->question->start_attempt($step, $variant);
d1b7e03d
TH
342 }
343
344 /**
345 * Checks whether two manual grading actions are the same. That is, whether
346 * the comment, and the mark (if given) is the same.
347 *
348 * @param question_attempt_step $pendingstep contains the new responses.
f7970e3c 349 * @return bool whether the new response is the same as we already have.
d1b7e03d
TH
350 */
351 protected function is_same_comment($pendingstep) {
352 $previouscomment = $this->qa->get_last_behaviour_var('comment');
353 $newcomment = $pendingstep->get_behaviour_var('comment');
354
355 if (is_null($previouscomment) && !html_is_blank($newcomment) ||
356 $previouscomment != $newcomment) {
357 return false;
358 }
359
360 // So, now we know the comment is the same, so check the mark, if present.
361 $previousfraction = $this->qa->get_fraction();
362 $newmark = $pendingstep->get_behaviour_var('mark');
363
364 if (is_null($previousfraction)) {
365 return is_null($newmark) || $newmark === '';
366 } else if (is_null($newmark) || $newmark === '') {
367 return false;
368 }
369
370 $newfraction = $newmark / $pendingstep->get_behaviour_var('maxmark');
371
372 return abs($newfraction - $previousfraction) < 0.0000001;
373 }
374
375 /**
376 * The main entry point for processing an action.
377 *
378 * All the various operations that can be performed on a
379 * {@link question_attempt} get channeled through this function, except for
380 * {@link question_attempt::start()} which goes to {@link init_first_step()}.
381 * {@link question_attempt::finish()} becomes an action with im vars
382 * finish => 1, and manual comment/grade becomes an action with im vars
383 * comment => comment text, and mark => ..., max_mark => ... if the question
384 * is graded.
385 *
386 * This method should first determine whether the action is significant. For
387 * example, if no actual action is being performed, but instead the current
388 * responses are being saved, and there has been no change since the last
389 * set of responses that were saved, this the action is not significatn. In
390 * this case, this method should return {@link question_attempt::DISCARD}.
391 * Otherwise it should return {@link question_attempt::KEEP}.
392 *
393 * If the action is significant, this method should also perform any
394 * necessary updates to $pendingstep. For example, it should call
395 * {@link question_attempt_step::set_state()} to set the state that results
396 * from this action, and if this is a grading action, it should call
397 * {@link question_attempt_step::set_fraction()}.
398 *
399 * This method can also call {@link question_attempt_step::set_behaviour_var()} to
400 * store additional infomation. There are two main uses for this. This can
401 * be used to store the result of any randomisation done. It is important to
402 * store the result of randomisation once, and then in future use the same
403 * outcome if the actions are ever replayed. This is how regrading works.
404 * The other use is to cache the result of expensive computations performed
405 * on the raw response data, so that subsequent display and review of the
406 * question does not have to repeat the same expensive computations.
407 *
408 * Often this method is implemented as a dispatching method that examines
409 * the pending step to determine the kind of action being performed, and
410 * then calls a more specific method like {@link process_save()} or
411 * {@link process_comment()}. Look at some of the standard behaviours
412 * for examples.
413 *
414 * @param question_attempt_pending_step $pendingstep a partially initialised step
1631ceea
TH
415 * containing all the information about the action that is being peformed. This
416 * information can be accessed using {@link question_attempt_step::get_behaviour_var()}.
f7970e3c 417 * @return bool either {@link question_attempt::KEEP} or {@link question_attempt::DISCARD}
d1b7e03d
TH
418 */
419 public abstract function process_action(question_attempt_pending_step $pendingstep);
420
0a606a2b
TH
421 /**
422 * Auto-saved data. By default this does nothing. interesting processing is
423 * done in {@link question_behaviour_with_save}.
424 *
425 * @param question_attempt_pending_step $pendingstep a partially initialised step
426 * containing all the information about the action that is being peformed. This
427 * information can be accessed using {@link question_attempt_step::get_behaviour_var()}.
428 * @return bool either {@link question_attempt::KEEP} or {@link question_attempt::DISCARD}
429 */
430 public function process_autosave(question_attempt_pending_step $pendingstep) {
431 return question_attempt::DISCARD;
432 }
433
d1b7e03d
TH
434 /**
435 * Implementation of processing a manual comment/grade action that should
436 * be suitable for most subclasses.
437 * @param question_attempt_pending_step $pendingstep a partially initialised step
438 * containing all the information about the action that is being peformed.
f7970e3c 439 * @return bool either {@link question_attempt::KEEP}
d1b7e03d
TH
440 */
441 public function process_comment(question_attempt_pending_step $pendingstep) {
442 if (!$this->qa->get_state()->is_finished()) {
443 throw new coding_exception('Cannot manually grade a question before it is finshed.');
444 }
445
446 if ($this->is_same_comment($pendingstep)) {
447 return question_attempt::DISCARD;
448 }
449
450 if ($pendingstep->has_behaviour_var('mark')) {
1631ceea
TH
451 $fraction = $pendingstep->get_behaviour_var('mark') /
452 $pendingstep->get_behaviour_var('maxmark');
d1b7e03d
TH
453 if ($pendingstep->get_behaviour_var('mark') === '') {
454 $fraction = null;
455 } else if ($fraction > 1 || $fraction < $this->qa->get_min_fraction()) {
456 throw new coding_exception('Score out of range when processing ' .
15dd7727 457 'a manual grading action.', 'Question ' . $this->question->id .
ee326d5e 458 ', slot ' . $this->qa->get_slot() . ', fraction ' . $fraction);
d1b7e03d
TH
459 }
460 $pendingstep->set_fraction($fraction);
461 }
462
1631ceea
TH
463 $pendingstep->set_state($this->qa->get_state()->corresponding_commented_state(
464 $pendingstep->get_fraction()));
d1b7e03d
TH
465 return question_attempt::KEEP;
466 }
467
ee326d5e
TH
468 /**
469 * Validate that the manual grade submitted for a particular question is in range.
470 * @param int $qubaid the question_usage id.
471 * @param int $slot the slot number within the usage.
472 * @return bool whether the submitted data is in range.
473 */
474 public static function is_manual_grade_in_range($qubaid, $slot) {
475 $prefix = 'q' . $qubaid . ':' . $slot . '_';
2c76c894 476 $mark = question_utils::optional_param_mark($prefix . '-mark');
61cca0b7
RT
477 $maxmark = optional_param($prefix . '-maxmark', null, PARAM_FLOAT);
478 $minfraction = optional_param($prefix . ':minfraction', null, PARAM_FLOAT);
ee326d5e
TH
479 return is_null($mark) || ($mark >= $minfraction * $maxmark && $mark <= $maxmark);
480 }
481
d1b7e03d
TH
482 /**
483 * @param $comment the comment text to format. If omitted,
484 * $this->qa->get_manual_comment() is used.
7a719748 485 * @param $commentformat the format of the comment, one of the FORMAT_... constants.
d1b7e03d
TH
486 * @return string the comment, ready to be output.
487 */
7a719748 488 public function format_comment($comment = null, $commentformat = null) {
0ff4bd08 489 $formatoptions = new stdClass();
d1b7e03d
TH
490 $formatoptions->noclean = true;
491 $formatoptions->para = false;
492
493 if (is_null($comment)) {
7a719748 494 list($comment, $commentformat) = $this->qa->get_manual_comment();
d1b7e03d
TH
495 }
496
7a719748 497 return format_text($comment, $commentformat, $formatoptions);
d1b7e03d
TH
498 }
499
500 /**
501 * @return string a summary of a manual comment action.
502 * @param unknown_type $step
503 */
504 protected function summarise_manual_comment($step) {
0ff4bd08 505 $a = new stdClass();
d1b7e03d
TH
506 if ($step->has_behaviour_var('comment')) {
507 $a->comment = shorten_text(html_to_text($this->format_comment(
508 $step->get_behaviour_var('comment')), 0, false), 200);
509 } else {
510 $a->comment = '';
511 }
512
513 $mark = $step->get_behaviour_var('mark');
514 if (is_null($mark) || $mark === '') {
515 return get_string('commented', 'question', $a->comment);
516 } else {
517 $a->mark = $mark / $step->get_behaviour_var('maxmark') * $this->qa->get_max_mark();
518 return get_string('manuallygraded', 'question', $a);
519 }
520 }
521
522 public function summarise_start($step) {
523 return get_string('started', 'question');
524 }
525
526 public function summarise_finish($step) {
527 return get_string('attemptfinished', 'question');
528 }
529}
530
531
532/**
533 * A subclass of {@link question_behaviour} that implements a save
534 * action that is suitable for most questions that implement the
535 * {@link question_manually_gradable} interface.
536 *
f7970e3c
TH
537 * @copyright 2009 The Open University
538 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
539 */
540abstract class question_behaviour_with_save extends question_behaviour {
541 public function required_question_definition_type() {
542 return 'question_manually_gradable';
543 }
544
545 /**
546 * Work out whether the response in $pendingstep are significantly different
547 * from the last set of responses we have stored.
548 * @param question_attempt_step $pendingstep contains the new responses.
f7970e3c 549 * @return bool whether the new response is the same as we already have.
d1b7e03d
TH
550 */
551 protected function is_same_response(question_attempt_step $pendingstep) {
552 return $this->question->is_same_response(
553 $this->qa->get_last_step()->get_qt_data(), $pendingstep->get_qt_data());
554 }
555
556 /**
557 * Work out whether the response in $pendingstep represent a complete answer
558 * to the question. Normally this will call
559 * {@link question_manually_gradable::is_complete_response}, but some
560 * behaviours, for example the CBM ones, have their own parts to the
561 * response.
562 * @param question_attempt_step $pendingstep contains the new responses.
f7970e3c 563 * @return bool whether the new response is complete.
d1b7e03d
TH
564 */
565 protected function is_complete_response(question_attempt_step $pendingstep) {
566 return $this->question->is_complete_response($pendingstep->get_qt_data());
567 }
568
0a606a2b
TH
569 public function process_autosave(question_attempt_pending_step $pendingstep) {
570 // If already finished. Nothing to do.
571 if ($this->qa->get_state()->is_finished()) {
572 return question_attempt::DISCARD;
573 }
574
575 // If the new data is the same as we already have, then we don't need it.
576 if ($this->is_same_response($pendingstep)) {
577 return question_attempt::DISCARD;
578 }
579
580 // Repeat that test discarding any existing autosaved data.
581 if ($this->qa->has_autosaved_step()) {
582 $this->qa->discard_autosaved_step();
583 if ($this->is_same_response($pendingstep)) {
584 return question_attempt::DISCARD;
585 }
586 }
587
588 // OK, we need to save.
589 return $this->process_save($pendingstep);
590 }
591
d1b7e03d
TH
592 /**
593 * Implementation of processing a save action that should be suitable for
594 * most subclasses.
595 * @param question_attempt_pending_step $pendingstep a partially initialised step
596 * containing all the information about the action that is being peformed.
f7970e3c 597 * @return bool either {@link question_attempt::KEEP} or {@link question_attempt::DISCARD}
d1b7e03d
TH
598 */
599 public function process_save(question_attempt_pending_step $pendingstep) {
600 if ($this->qa->get_state()->is_finished()) {
601 return question_attempt::DISCARD;
602 } else if (!$this->qa->get_state()->is_active()) {
88f0eb15 603 throw new coding_exception('Question is not active, cannot process_actions.');
d1b7e03d
TH
604 }
605
606 if ($this->is_same_response($pendingstep)) {
607 return question_attempt::DISCARD;
608 }
609
610 if ($this->is_complete_response($pendingstep)) {
611 $pendingstep->set_state(question_state::$complete);
612 } else {
613 $pendingstep->set_state(question_state::$todo);
614 }
615 return question_attempt::KEEP;
616 }
617
618 public function summarise_submit(question_attempt_step $step) {
619 return get_string('submitted', 'question',
620 $this->question->summarise_response($step->get_qt_data()));
621 }
622
623 public function summarise_save(question_attempt_step $step) {
624 $data = $step->get_submitted_data();
625 if (empty($data)) {
626 return $this->summarise_start($step);
627 }
628 return get_string('saved', 'question',
629 $this->question->summarise_response($step->get_qt_data()));
630 }
631
632
633 public function summarise_finish($step) {
634 $data = $step->get_qt_data();
635 if ($data) {
636 return get_string('attemptfinishedsubmitting', 'question',
637 $this->question->summarise_response($data));
638 }
639 return get_string('attemptfinished', 'question');
640 }
641}
642
643
644/**
645 * This helper class contains the constants and methods required for
646 * manipulating scores for certainly based marking.
647 *
f7970e3c
TH
648 * @copyright 2009 The Open University
649 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
650 */
651abstract class question_cbm {
652 /**#@+ @var integer named constants for the certainty levels. */
653 const LOW = 1;
654 const MED = 2;
655 const HIGH = 3;
656 /**#@-*/
657
658 /** @var array list of all the certainty levels. */
659 public static $certainties = array(self::LOW, self::MED, self::HIGH);
660
661 /**#@+ @var array coefficients used to adjust the fraction based on certainty.. */
662 protected static $factor = array(
663 self::LOW => 0.333333333333333,
664 self::MED => 1.333333333333333,
665 self::HIGH => 3,
666 );
667 protected static $offset = array(
668 self::LOW => 0,
669 self::MED => -0.666666666666667,
670 self::HIGH => -2,
671 );
672 /**#@-*/
673
674 /**
f7970e3c 675 * @return int the default certaintly level that should be assuemd if
d1b7e03d
TH
676 * the student does not choose one.
677 */
678 public static function default_certainty() {
679 return self::LOW;
680 }
681
682 /**
683 * Given a fraction, and a certainly, compute the adjusted fraction.
684 * @param number $fraction the raw fraction for this question.
f7970e3c 685 * @param int $certainty one of the certainly level constants.
d1b7e03d
TH
686 * @return number the adjusted fraction taking the certainly into account.
687 */
688 public static function adjust_fraction($fraction, $certainty) {
689 return self::$offset[$certainty] + self::$factor[$certainty] * $fraction;
690 }
691
692 /**
f7970e3c 693 * @param int $certainty one of the LOW/MED/HIGH constants.
d1b7e03d
TH
694 * @return string a textual desciption of this certainly.
695 */
696 public static function get_string($certainty) {
697 return get_string('certainty' . $certainty, 'qbehaviour_deferredcbm');
698 }
699
700 public static function summary_with_certainty($summary, $certainty) {
701 if (is_null($certainty)) {
702 return $summary;
703 }
704 return $summary . ' [' . self::get_string($certainty) . ']';
705 }
706}