MDL-20636 Fix @package names and PHPdoc layout.
[moodle.git] / question / behaviour / immediatefeedback / behaviour.php
CommitLineData
d1b7e03d
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
d1b7e03d
TH
18/**
19 * Question behaviour where the student can submit questions one at a
20 * time for immediate feedback.
21 *
017bc1d9
TH
22 * @package qbehaviour
23 * @subpackage immediatefeedback
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
29/**
30 * Question behaviour for immediate feedback.
31 *
32 * Each question has a submit button next to it which the student can use to
33 * submit it. Once the qustion is submitted, it is not possible for the
34 * student to change their answer any more, but the student gets full feedback
35 * straight away.
36 *
017bc1d9
TH
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
39 */
40class qbehaviour_immediatefeedback extends question_behaviour_with_save {
41 const IS_ARCHETYPAL = true;
42
43 public function required_question_definition_type() {
44 return 'question_automatically_gradable';
45 }
46
47 public function get_min_fraction() {
48 return $this->question->get_min_fraction();
49 }
50
51 public function get_expected_data() {
52 if ($this->qa->get_state()->is_active()) {
53 return array(
54 'submit' => PARAM_BOOL,
55 );
56 }
57 return parent::get_expected_data();
58 }
59
60 public function get_right_answer_summary() {
61 return $this->question->get_right_answer_summary();
62 }
63
64 public function process_action(question_attempt_pending_step $pendingstep) {
65 if ($pendingstep->has_behaviour_var('comment')) {
66 return $this->process_comment($pendingstep);
67 } else if ($pendingstep->has_behaviour_var('submit')) {
68 return $this->process_submit($pendingstep);
69 } else if ($pendingstep->has_behaviour_var('finish')) {
70 return $this->process_finish($pendingstep);
71 } else {
72 return $this->process_save($pendingstep);
73 }
74 }
75
76 public function summarise_action(question_attempt_step $step) {
77 if ($step->has_behaviour_var('comment')) {
78 return $this->summarise_manual_comment($step);
79 } else if ($step->has_behaviour_var('finish')) {
80 return $this->summarise_finish($step);
81 } else if ($step->has_behaviour_var('submit')) {
82 return $this->summarise_submit($step);
83 } else {
84 return $this->summarise_save($step);
85 }
86 }
87
88 public function process_submit(question_attempt_pending_step $pendingstep) {
89 if ($this->qa->get_state()->is_finished()) {
90 return question_attempt::DISCARD;
91 }
92
93 if (!$this->is_complete_response($pendingstep)) {
94 $pendingstep->set_state(question_state::$invalid);
95
96 } else {
97 $response = $pendingstep->get_qt_data();
98 list($fraction, $state) = $this->question->grade_response($response);
99 $pendingstep->set_fraction($fraction);
100 $pendingstep->set_state($state);
101 $pendingstep->set_new_response_summary($this->question->summarise_response($response));
102 }
103 return question_attempt::KEEP;
104 }
105
106 public function process_finish(question_attempt_pending_step $pendingstep) {
107 if ($this->qa->get_state()->is_finished()) {
108 return question_attempt::DISCARD;
109 }
110
111 $response = $this->qa->get_last_step()->get_qt_data();
112 if (!$this->question->is_gradable_response($response)) {
113 $pendingstep->set_state(question_state::$gaveup);
114
115 } else {
116 list($fraction, $state) = $this->question->grade_response($response);
117 $pendingstep->set_fraction($fraction);
118 $pendingstep->set_state($state);
119 }
120 $pendingstep->set_new_response_summary($this->question->summarise_response($response));
121 return question_attempt::KEEP;
122 }
123
124 public function process_save(question_attempt_pending_step $pendingstep) {
125 $status = parent::process_save($pendingstep);
126 if ($status == question_attempt::KEEP && $pendingstep->get_state() == question_state::$complete) {
127 $pendingstep->set_state(question_state::$todo);
128 }
129 return $status;
130 }
131}