1<?php
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
17/**
18 * Question behaviour where the student can submit questions one at a
19 * time for immediate feedback, with certainty based marking.
20 *
21 * @package    qbehaviour
22 * @subpackage immediatecbm
23 * @copyright  2009 The Open University
24 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28defined('MOODLE_INTERNAL') || die();
29
30require_once(__DIR__ . '/../immediatefeedback/behaviour.php');
31
32
33/**
34 * Question behaviour for immediate feedback with CBM.
35 *
36 * Each question has a submit button next to it along with some radio buttons
37 * to input a certainty, that is, how sure they are that they are right.
38 * The student can submit their answer at any time for immediate feedback.
39 * Once the qustion is submitted, it is not possible for the student to change
40 * their answer any more. The student's degree of certainty affects their score.
41 *
42 * @copyright  2009 The Open University
43 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 */
45class qbehaviour_immediatecbm extends qbehaviour_immediatefeedback {
46    public function get_min_fraction() {
47        return question_cbm::adjust_fraction(parent::get_min_fraction(), question_cbm::HIGH);
48    }
49
50    public function get_max_fraction() {
51        return question_cbm::adjust_fraction(parent::get_max_fraction(), question_cbm::HIGH);
52    }
53
54    public function get_expected_data() {
55        if ($this->qa->get_state()->is_active()) {
56            return array(
57                'submit' => PARAM_BOOL,
58                'certainty' => PARAM_INT,
59            );
60        }
61        return parent::get_expected_data();
62    }
63
64    public function get_right_answer_summary() {
65        $summary = parent::get_right_answer_summary();
66        return question_cbm::summary_with_certainty($summary, question_cbm::HIGH);
67    }
68
69    public function get_correct_response() {
70        if ($this->qa->get_state()->is_active()) {
71            return array('certainty' => question_cbm::HIGH);
72        }
73        return array();
74    }
75
76    protected function get_our_resume_data() {
77        $lastcertainty = $this->qa->get_last_behaviour_var('certainty');
78        if ($lastcertainty) {
79            return array('-certainty' => $lastcertainty);
80        } else {
81            return array();
82        }
83    }
84
85    protected function is_same_response(question_attempt_step $pendingstep) {
86        return parent::is_same_response($pendingstep) &&
87                $this->qa->get_last_behaviour_var('certainty') ==
88                        $pendingstep->get_behaviour_var('certainty');
89    }
90
91    protected function is_complete_response(question_attempt_step $pendingstep) {
92        return parent::is_complete_response($pendingstep) &&
93                $pendingstep->has_behaviour_var('certainty');
94    }
95
96    public function process_submit(question_attempt_pending_step $pendingstep) {
97        if ($this->qa->get_state()->is_finished()) {
98            return question_attempt::DISCARD;
99        }
100
101        if (!$this->question->is_gradable_response($pendingstep->get_qt_data()) ||
102                !$pendingstep->has_behaviour_var('certainty')) {
103            $pendingstep->set_state(question_state::$invalid);
104            return question_attempt::KEEP;
105        }
106
107        return $this->do_grading($pendingstep, $pendingstep);
108    }
109
110    public function process_finish(question_attempt_pending_step $pendingstep) {
111        if ($this->qa->get_state()->is_finished()) {
112            return question_attempt::DISCARD;
113        }
114
115        $laststep = $this->qa->get_last_step();
116        return $this->do_grading($laststep, $pendingstep);
117    }
118
119    protected function do_grading(question_attempt_step $responsesstep,
120            question_attempt_pending_step $pendingstep) {
121        if (!$this->question->is_gradable_response($responsesstep->get_qt_data())) {
122            $pendingstep->set_state(question_state::$gaveup);
123
124        } else {
125            $response = $responsesstep->get_qt_data();
126            list($fraction, $state) = $this->question->grade_response($response);
127
128            if ($responsesstep->has_behaviour_var('certainty')) {
129                $certainty = $responsesstep->get_behaviour_var('certainty');
130            } else {
131                $certainty = question_cbm::default_certainty();
132                $pendingstep->set_behaviour_var('_assumedcertainty', $certainty);
133            }
134
135            $pendingstep->set_behaviour_var('_rawfraction', $fraction);
136            $pendingstep->set_fraction(question_cbm::adjust_fraction($fraction, $certainty));
137            $pendingstep->set_state($state);
138            $pendingstep->set_new_response_summary(question_cbm::summary_with_certainty(
139                    $this->question->summarise_response($response),
140                    $responsesstep->get_behaviour_var('certainty')));
141        }
142        return question_attempt::KEEP;
143    }
144
145    public function summarise_action(question_attempt_step $step) {
146        $summary = parent::summarise_action($step);
147        if ($step->has_behaviour_var('certainty')) {
148            $summary = question_cbm::summary_with_certainty($summary,
149                    $step->get_behaviour_var('certainty'));
150        }
151        return $summary;
152    }
153}
154