Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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.
  20   *
  21   * @package    qbehaviour
  22   * @subpackage immediatefeedback
  23   * @copyright  2009 The Open University
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  
  31  /**
  32   * Question behaviour for immediate feedback.
  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_immediatefeedback extends question_behaviour_with_save {
  43      const IS_ARCHETYPAL = true;
  44  
  45      public function is_compatible_question(question_definition $question) {
  46          return $question instanceof question_automatically_gradable;
  47      }
  48  
  49      public function can_finish_during_attempt() {
  50          return true;
  51      }
  52  
  53      public function get_min_fraction() {
  54          return $this->question->get_min_fraction();
  55      }
  56  
  57      public function get_expected_data() {
  58          if ($this->qa->get_state()->is_active()) {
  59              return array(
  60                  'submit' => PARAM_BOOL,
  61              );
  62          }
  63          return parent::get_expected_data();
  64      }
  65  
  66      public function get_state_string($showcorrectness) {
  67          $state = $this->qa->get_state();
  68          if ($state == question_state::$todo) {
  69              return get_string('notcomplete', 'qbehaviour_immediatefeedback');
  70          } else {
  71              return parent::get_state_string($showcorrectness);
  72          }
  73      }
  74  
  75      public function get_right_answer_summary() {
  76          return $this->question->get_right_answer_summary();
  77      }
  78  
  79      public function process_action(question_attempt_pending_step $pendingstep) {
  80          if ($pendingstep->has_behaviour_var('comment')) {
  81              return $this->process_comment($pendingstep);
  82          } else if ($pendingstep->has_behaviour_var('submit')) {
  83              return $this->process_submit($pendingstep);
  84          } else if ($pendingstep->has_behaviour_var('finish')) {
  85              return $this->process_finish($pendingstep);
  86          } else {
  87              return $this->process_save($pendingstep);
  88          }
  89      }
  90  
  91      public function summarise_action(question_attempt_step $step) {
  92          if ($step->has_behaviour_var('comment')) {
  93              return $this->summarise_manual_comment($step);
  94          } else if ($step->has_behaviour_var('finish')) {
  95              return $this->summarise_finish($step);
  96          } else if ($step->has_behaviour_var('submit')) {
  97              return $this->summarise_submit($step);
  98          } else {
  99              return $this->summarise_save($step);
 100          }
 101      }
 102  
 103      public function process_submit(question_attempt_pending_step $pendingstep) {
 104          if ($this->qa->get_state()->is_finished()) {
 105              return question_attempt::DISCARD;
 106          }
 107  
 108          if (!$this->is_complete_response($pendingstep)) {
 109              $pendingstep->set_state(question_state::$invalid);
 110  
 111          } else {
 112              $response = $pendingstep->get_qt_data();
 113              list($fraction, $state) = $this->question->grade_response($response);
 114              $pendingstep->set_fraction($fraction);
 115              $pendingstep->set_state($state);
 116              $pendingstep->set_new_response_summary($this->question->summarise_response($response));
 117          }
 118          return question_attempt::KEEP;
 119      }
 120  
 121      public function process_finish(question_attempt_pending_step $pendingstep) {
 122          if ($this->qa->get_state()->is_finished()) {
 123              return question_attempt::DISCARD;
 124          }
 125  
 126          $response = $this->qa->get_last_step()->get_qt_data();
 127          if (!$this->question->is_gradable_response($response)) {
 128              $pendingstep->set_state(question_state::$gaveup);
 129  
 130          } else {
 131              list($fraction, $state) = $this->question->grade_response($response);
 132              $pendingstep->set_fraction($fraction);
 133              $pendingstep->set_state($state);
 134          }
 135          $pendingstep->set_new_response_summary($this->question->summarise_response($response));
 136          return question_attempt::KEEP;
 137      }
 138  
 139      public function process_save(question_attempt_pending_step $pendingstep) {
 140          $status = parent::process_save($pendingstep);
 141          if ($status == question_attempt::KEEP &&
 142                  $pendingstep->get_state() == question_state::$complete) {
 143              $pendingstep->set_state(question_state::$todo);
 144          }
 145          return $status;
 146      }
 147  }