MDL-32188 question CBM: alter score handling.
[moodle.git] / question / behaviour / deferredcbm / behaviour.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 * Question behaviour that is like the deferred feedback model, but with
c4efeb2b 19 * certainty based marking. That is, in addition to the other controls, there are
d1b7e03d
TH
20 * where the student can indicate how certain they are that their answer is right.
21 *
017bc1d9
TH
22 * @package qbehaviour
23 * @subpackage deferredcbm
24 * @copyright 2009 The Open University
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
26 */
27
28
a17b297d
TH
29defined('MOODLE_INTERNAL') || die();
30
d1b7e03d
TH
31require_once(dirname(__FILE__) . '/../deferredfeedback/behaviour.php');
32
017bc1d9 33
d1b7e03d
TH
34/**
35 * Question behaviour for deferred feedback with certainty based marking.
36 *
37 * The student enters their response during the attempt, along with a certainty,
38 * that is, how sure they are that they are right, and it is saved. Later,
39 * when the whole attempt is finished, their answer is graded. Their degree
40 * of certainty affects their score.
41 *
017bc1d9
TH
42 * @copyright 2009 The Open University
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
44 */
45class qbehaviour_deferredcbm extends qbehaviour_deferredfeedback {
46 const IS_ARCHETYPAL = true;
47
d1b7e03d 48 public function get_min_fraction() {
fb74929e 49 return question_cbm::adjust_fraction(0, question_cbm::HIGH);
d1b7e03d
TH
50 }
51
4e3d8293 52 public function get_max_fraction() {
fb74929e 53 return question_cbm::adjust_fraction(1, question_cbm::HIGH);
4e3d8293
TH
54 }
55
d1b7e03d
TH
56 public function get_expected_data() {
57 if ($this->qa->get_state()->is_active()) {
58 return array('certainty' => PARAM_INT);
59 }
60 return parent::get_expected_data();
61 }
62
63 public function get_right_answer_summary() {
64 $summary = parent::get_right_answer_summary();
65 return $summary . ' [' . question_cbm::get_string(question_cbm::HIGH) . ']';
66 }
67
68 public function get_correct_response() {
69 if ($this->qa->get_state()->is_active()) {
70 return array('certainty' => question_cbm::HIGH);
71 }
72 return array();
73 }
74
75 protected function get_our_resume_data() {
76 $lastcertainty = $this->qa->get_last_behaviour_var('certainty');
77 if ($lastcertainty) {
78 return array('-certainty' => $lastcertainty);
79 } else {
80 return array();
81 }
82 }
83
072db71c 84 protected function is_same_response(question_attempt_step $pendingstep) {
d1b7e03d 85 return parent::is_same_response($pendingstep) &&
1631ceea
TH
86 $this->qa->get_last_behaviour_var('certainty') ==
87 $pendingstep->get_behaviour_var('certainty');
d1b7e03d
TH
88 }
89
072db71c 90 protected function is_complete_response(question_attempt_step $pendingstep) {
1631ceea
TH
91 return parent::is_complete_response($pendingstep) &&
92 $pendingstep->has_behaviour_var('certainty');
d1b7e03d
TH
93 }
94
95 public function process_finish(question_attempt_pending_step $pendingstep) {
96 $status = parent::process_finish($pendingstep);
97 if ($status == question_attempt::KEEP) {
98 $fraction = $pendingstep->get_fraction();
99 if ($this->qa->get_last_step()->has_behaviour_var('certainty')) {
100 $certainty = $this->qa->get_last_step()->get_behaviour_var('certainty');
101 } else {
102 $certainty = question_cbm::default_certainty();
103 $pendingstep->set_behaviour_var('_assumedcertainty', $certainty);
104 }
105 if (!is_null($fraction)) {
106 $pendingstep->set_behaviour_var('_rawfraction', $fraction);
107 $pendingstep->set_fraction(question_cbm::adjust_fraction($fraction, $certainty));
108 }
109 $pendingstep->set_new_response_summary(
110 question_cbm::summary_with_certainty($pendingstep->get_new_response_summary(),
111 $this->qa->get_last_step()->get_behaviour_var('certainty')));
112 }
113 return $status;
114 }
115
116 public function summarise_action(question_attempt_step $step) {
117 $summary = parent::summarise_action($step);
118 if ($step->has_behaviour_var('certainty')) {
119 $summary = question_cbm::summary_with_certainty($summary,
120 $step->get_behaviour_var('certainty'));
121 }
122 return $summary;
123 }
d1b7e03d 124}