behaviourtypebase.php 6.0 KB

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