MDL-49041 qtype_multianswer: don't reveal marks on partial responses
[moodle.git] / question / behaviour / interactive / behaviour.php
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/>.
17 /**
18  * Question behaviour where the student can submit questions one at a
19  * time for immediate feedback.
20  *
21  * @package    qbehaviour
22  * @subpackage interactive
23  * @copyright  2009 The Open University
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
28 defined('MOODLE_INTERNAL') || die();
31 /**
32  * Question behaviour for the interactive model.
33  *
34  * Each question has a submit button next to it which the student can use to
35  * submit it. Once the qustion is submitted, it is not possible for the
36  * student to change their answer any more, but the student gets full feedback
37  * straight away.
38  *
39  * @copyright  2009 The Open University
40  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41  */
42 class qbehaviour_interactive extends question_behaviour_with_multiple_tries {
43     /**
44      * Special value used for {@link question_display_options::$readonly when
45      * we are showing the try again button to the student during an attempt.
46      * The particular number was chosen randomly. PHP will treat it the same
47      * as true, but in the renderer we reconginse it display the try again
48      * button enabled even though the rest of the question is disabled.
49      * @var integer
50      */
51     const READONLY_EXCEPT_TRY_AGAIN = 23485299;
53     public function is_compatible_question(question_definition $question) {
54         return $question instanceof question_automatically_gradable;
55     }
57     public function get_right_answer_summary() {
58         return $this->question->get_right_answer_summary();
59     }
61     /**
62      * @return bool are we are currently in the try_again state.
63      */
64     protected function is_try_again_state() {
65         $laststep = $this->qa->get_last_step();
66         return $this->qa->get_state()->is_active() && $laststep->has_behaviour_var('submit') &&
67                 $laststep->has_behaviour_var('_triesleft');
68     }
70     public function adjust_display_options(question_display_options $options) {
71         // We only need different behaviour in try again states.
72         if (!$this->is_try_again_state()) {
73             parent::adjust_display_options($options);
74             if ($this->qa->get_state() == question_state::$invalid &&
75                     $options->marks == question_display_options::MARK_AND_MAX) {
76                 $options->marks = question_display_options::MAX_ONLY;
77             }
78             return;
79         }
81         // Let the hint adjust the options.
82         $hint = $this->get_applicable_hint();
83         if (!is_null($hint)) {
84             $hint->adjust_display_options($options);
85         }
87         // Now call the base class method, but protect some fields from being overwritten.
88         $save = clone($options);
89         parent::adjust_display_options($options);
90         $options->feedback = $save->feedback;
91         $options->numpartscorrect = $save->numpartscorrect;
93         // In a try-again state, everything except the try again button
94         // Should be read-only. This is a mild hack to achieve this.
95         if (!$options->readonly) {
96             $options->readonly = self::READONLY_EXCEPT_TRY_AGAIN;
97         }
98     }
100     public function get_applicable_hint() {
101         if (!$this->is_try_again_state()) {
102             return null;
103         }
104         return $this->question->get_hint(count($this->question->hints) -
105                 $this->qa->get_last_behaviour_var('_triesleft'), $this->qa);
106     }
108     public function get_expected_data() {
109         if ($this->is_try_again_state()) {
110             return array(
111                 'tryagain' => PARAM_BOOL,
112             );
113         } else if ($this->qa->get_state()->is_active()) {
114             return array(
115                 'submit' => PARAM_BOOL,
116             );
117         }
118         return parent::get_expected_data();
119     }
121     public function get_expected_qt_data() {
122         $hint = $this->get_applicable_hint();
123         if (!empty($hint->clearwrong)) {
124             return $this->question->get_expected_data();
125         }
126         return parent::get_expected_qt_data();
127     }
129     public function get_state_string($showcorrectness) {
130         $state = $this->qa->get_state();
131         if (!$state->is_active() || $state == question_state::$invalid) {
132             return parent::get_state_string($showcorrectness);
133         }
135         if ($this->is_try_again_state()) {
136             return get_string('notcomplete', 'qbehaviour_interactive');
137         } else {
138             return get_string('triesremaining', 'qbehaviour_interactive',
139                     $this->qa->get_last_behaviour_var('_triesleft'));
140         }
141     }
143     public function init_first_step(question_attempt_step $step, $variant) {
144         parent::init_first_step($step, $variant);
145         $step->set_behaviour_var('_triesleft', count($this->question->hints) + 1);
146     }
148     public function process_action(question_attempt_pending_step $pendingstep) {
149         if ($pendingstep->has_behaviour_var('finish')) {
150             return $this->process_finish($pendingstep);
151         }
152         if ($this->is_try_again_state()) {
153             if ($pendingstep->has_behaviour_var('tryagain')) {
154                 return $this->process_try_again($pendingstep);
155             } else {
156                 return question_attempt::DISCARD;
157             }
158         } else {
159             if ($pendingstep->has_behaviour_var('comment')) {
160                 return $this->process_comment($pendingstep);
161             } else if ($pendingstep->has_behaviour_var('submit')) {
162                 return $this->process_submit($pendingstep);
163             } else {
164                 return $this->process_save($pendingstep);
165             }
166         }
167     }
169     public function summarise_action(question_attempt_step $step) {
170         if ($step->has_behaviour_var('comment')) {
171             return $this->summarise_manual_comment($step);
172         } else if ($step->has_behaviour_var('finish')) {
173             return $this->summarise_finish($step);
174         } else if ($step->has_behaviour_var('tryagain')) {
175             return get_string('tryagain', 'qbehaviour_interactive');
176         } else if ($step->has_behaviour_var('submit')) {
177             return $this->summarise_submit($step);
178         } else {
179             return $this->summarise_save($step);
180         }
181     }
183     public function process_try_again(question_attempt_pending_step $pendingstep) {
184         $pendingstep->set_state(question_state::$todo);
185         return question_attempt::KEEP;
186     }
188     public function process_submit(question_attempt_pending_step $pendingstep) {
189         if ($this->qa->get_state()->is_finished()) {
190             return question_attempt::DISCARD;
191         }
193         if (!$this->is_complete_response($pendingstep)) {
194             $pendingstep->set_state(question_state::$invalid);
196         } else {
197             $triesleft = $this->qa->get_last_behaviour_var('_triesleft');
198             $response = $pendingstep->get_qt_data();
199             list($fraction, $state) = $this->question->grade_response($response);
200             if ($state == question_state::$gradedright || $triesleft == 1) {
201                 $pendingstep->set_state($state);
202                 $pendingstep->set_fraction($this->adjust_fraction($fraction, $pendingstep));
204             } else {
205                 $pendingstep->set_behaviour_var('_triesleft', $triesleft - 1);
206                 $pendingstep->set_state(question_state::$todo);
207             }
208             $pendingstep->set_new_response_summary($this->question->summarise_response($response));
209         }
210         return question_attempt::KEEP;
211     }
213     protected function adjust_fraction($fraction, question_attempt_pending_step $pendingstep) {
214         $totaltries = $this->qa->get_step(0)->get_behaviour_var('_triesleft');
215         $triesleft = $this->qa->get_last_behaviour_var('_triesleft');
217         $fraction -= ($totaltries - $triesleft) * $this->question->penalty;
218         $fraction = max($fraction, 0);
219         return $fraction;
220     }
222     public function process_finish(question_attempt_pending_step $pendingstep) {
223         if ($this->qa->get_state()->is_finished()) {
224             return question_attempt::DISCARD;
225         }
227         $response = $this->qa->get_last_qt_data();
228         if (!$this->question->is_gradable_response($response)) {
229             $pendingstep->set_state(question_state::$gaveup);
231         } else {
232             list($fraction, $state) = $this->question->grade_response($response);
233             $pendingstep->set_fraction($this->adjust_fraction($fraction, $pendingstep));
234             $pendingstep->set_state($state);
235         }
236         $pendingstep->set_new_response_summary($this->question->summarise_response($response));
237         return question_attempt::KEEP;
238     }
240     public function process_save(question_attempt_pending_step $pendingstep) {
241         $status = parent::process_save($pendingstep);
242         if ($status == question_attempt::KEEP &&
243                 $pendingstep->get_state() == question_state::$complete) {
244             $pendingstep->set_state(question_state::$todo);
245         }
246         return $status;
247     }