Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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 for the case when the student's answer is just
  19   * saved until they submit the whole attempt, and then it is graded.
  20   *
  21   * @package    qbehaviour
  22   * @subpackage deferredfeedback
  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 deferred feedback.
  33   *
  34   * The student enters their response during the attempt, and it is saved. Later,
  35   * when the whole attempt is finished, their answer is graded.
  36   *
  37   * @copyright  2009 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qbehaviour_deferredfeedback extends question_behaviour_with_save {
  41      public function is_compatible_question(question_definition $question) {
  42          return $question instanceof question_automatically_gradable;
  43      }
  44  
  45      public function get_min_fraction() {
  46          return $this->question->get_min_fraction();
  47      }
  48  
  49      public function get_right_answer_summary() {
  50          return $this->question->get_right_answer_summary();
  51      }
  52  
  53      public function process_action(question_attempt_pending_step $pendingstep) {
  54          if ($pendingstep->has_behaviour_var('comment')) {
  55              return $this->process_comment($pendingstep);
  56          } else if ($pendingstep->has_behaviour_var('finish')) {
  57              return $this->process_finish($pendingstep);
  58          } else {
  59              return $this->process_save($pendingstep);
  60          }
  61      }
  62  
  63      /*
  64       * Like the parent method, except that when a respones is gradable, but not
  65       * completely, we move it to the invalid state.
  66       *
  67       * TODO refactor, to remove the duplication.
  68       */
  69      public function process_save(question_attempt_pending_step $pendingstep) {
  70          if ($this->qa->get_state()->is_finished()) {
  71              return question_attempt::DISCARD;
  72          } else if (!$this->qa->get_state()->is_active()) {
  73              throw new coding_exception('Question is not active, cannot process_actions.');
  74          }
  75  
  76          if ($this->is_same_response($pendingstep)) {
  77              return question_attempt::DISCARD;
  78          }
  79  
  80          if ($this->is_complete_response($pendingstep)) {
  81              $pendingstep->set_state(question_state::$complete);
  82          } else if ($this->question->is_gradable_response($pendingstep->get_qt_data())) {
  83              $pendingstep->set_state(question_state::$invalid);
  84          } else {
  85              $pendingstep->set_state(question_state::$todo);
  86          }
  87          return question_attempt::KEEP;
  88      }
  89  
  90      public function summarise_action(question_attempt_step $step) {
  91          if ($step->has_behaviour_var('comment')) {
  92              return $this->summarise_manual_comment($step);
  93          } else if ($step->has_behaviour_var('finish')) {
  94              return $this->summarise_finish($step);
  95          } else {
  96              return $this->summarise_save($step);
  97          }
  98      }
  99  
 100      public function process_finish(question_attempt_pending_step $pendingstep) {
 101          if ($this->qa->get_state()->is_finished()) {
 102              return question_attempt::DISCARD;
 103          }
 104  
 105          $response = $this->qa->get_last_step()->get_qt_data();
 106          if (!$this->question->is_gradable_response($response)) {
 107              $pendingstep->set_state(question_state::$gaveup);
 108          } else {
 109              list($fraction, $state) = $this->question->grade_response($response);
 110              $pendingstep->set_fraction($fraction);
 111              $pendingstep->set_state($state);
 112          }
 113          $pendingstep->set_new_response_summary($this->question->summarise_response($response));
 114          return question_attempt::KEEP;
 115      }
 116  }