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