weekly release 3.10.3+
[moodle.git] / question / behaviour / behaviourtypebase.php
CommitLineData
f3460297
TH
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 * Defines the question behaviour type base class
19 *
20 * @package core
21 * @subpackage questionbehaviours
22 * @copyright 2012 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29
30/**
31 * This class represents the type of behaviour, rather than the instance of the
32 * behaviour which control a particular question attempt.
33 *
34 * @copyright 2012 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37abstract class question_behaviour_type {
38 /**
39 * Certain behaviours are definitive of a way that questions can behave when
40 * attempted. For example deferredfeedback model, interactive model, etc.
41 * These are the options that should be listed in the user-interface, and
42 * for these behaviours this method should return true. Other behaviours are
43 * more implementation details, for example the informationitem behaviours,
44 * or a special subclass like interactive_adapted_for_my_qtype. These
45 * behaviours should return false.
46 * @return bool whether this is an archetypal behaviour.
47 */
48 public function is_archetypal() {
49 return false;
50 }
51
52 /**
53 * Override this method if there are some display options that do not make
54 * sense 'during the attempt'.
55 * @return array of {@link question_display_options} field names, that are
56 * not relevant to this behaviour before a 'finish' action.
57 */
58 public function get_unused_display_options() {
59 return array();
60 }
61
47be39ef
TH
62 /**
63 * With this behaviour, is it possible that a question might finish as the student
64 * interacts with it, without a call to the {@link question_attempt::finish()} method?
65 * @return bool whether with this behaviour, questions may finish naturally.
66 */
67 public function can_questions_finish_during_the_attempt() {
68 return false;
69 }
70
f3460297
TH
71 /**
72 * Adjust a random guess score for a question using this model. You have to
73 * do this without knowing details of the specific question, or which usage
74 * it is in.
75 * @param number $fraction the random guess score from the question type.
76 * @return number the adjusted fraction.
77 */
78 public function adjust_random_guess_score($fraction) {
79 return $fraction;
80 }
474aa125
TH
81
82 /**
83 * Get summary information about a queston usage.
84 *
85 * Behaviours are not obliged to do anything here, but this is an opportunity
86 * to provide additional information that can be displayed in places like
87 * at the top of the quiz review page.
88 *
89 * In the return value, the array keys should be identifiers of the form
90 * qbehaviour_behaviourname_meaningfullkey. For qbehaviour_deferredcbm_highsummary.
91 * The values should be arrays with two items, title and content. Each of these
92 * should be either a string, or a renderable.
93 *
94 * To understand how to implement this method, look at the CBM behaviours,
95 * and their unit tests.
96 *
97 * @param question_usage_by_activity $quba the usage to provide summary data for.
474aa125
TH
98 * @return array as described above.
99 */
100 public function summarise_usage(question_usage_by_activity $quba,
101 question_display_options $options) {
102 return array();
103 }
99caa248
JP
104
105 /**
106 * Does this question behaviour accept multiple submissions of responses within one attempt eg. multiple tries for the
107 * interactive or adaptive question behaviours.
108 *
109 * @return bool
110 */
111 public function allows_multiple_submitted_responses() {
112 return false;
113 }
f3460297
TH
114}
115
116
117/**
118 * This class exists to allow behaviours that worked in Moodle 2.3 to continue
119 * to work. It implements the question_behaviour_type API for the other behaviour
120 * as much as possible in a backwards-compatible way.
121 *
122 * @copyright 2012 The Open University
123 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
124 */
125class question_behaviour_type_fallback extends question_behaviour_type {
126
127 /** @var string the behaviour class name. */
128 protected $behaviourclass;
129
130 /**
131 * @param string $behaviourtype the type of behaviour we are providing a fallback for.
132 */
133 public function __construct($behaviour) {
134 question_engine::load_behaviour_class($behaviour);
135 $this->behaviourclass = 'qbehaviour_' . $behaviour;
136 }
137
138 public function is_archetypal() {
139 return constant($this->behaviourclass . '::IS_ARCHETYPAL');
140 }
141
142 /**
143 * Override this method if there are some display options that do not make
144 * sense 'during the attempt'.
145 * @return array of {@link question_display_options} field names, that are
146 * not relevant to this behaviour before a 'finish' action.
147 */
148 public function get_unused_display_options() {
149 return call_user_func(array($this->behaviourclass, 'get_unused_display_options'));
150 }
151
152 /**
153 * Adjust a random guess score for a question using this model. You have to
154 * do this without knowing details of the specific question, or which usage
155 * it is in.
156 * @param number $fraction the random guess score from the question type.
157 * @return number the adjusted fraction.
158 */
159 public function adjust_random_guess_score($fraction) {
160 return call_user_func(array($this->behaviourclass, 'adjust_random_guess_score'),
161 $fraction);
162 }
163}