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