Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 that is like the interactive behaviour, but where the
  19   * student is credited for parts of the question they got right on earlier tries.
  20   *
  21   * @package    qbehaviour
  22   * @subpackage interactivecountback
  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  require_once (__DIR__ . '/../interactive/behaviour.php');
  31  
  32  
  33  /**
  34   * Question behaviour for interactive mode with count-back scoring.
  35   *
  36   * As an example, suppose we have a matching question with 4 parts, and 3 tries
  37   * (penalty 1/3), and the question is worth 12 marks (so, 3 marks for each part).
  38   * Suppose also that:
  39   *  - on the first try, the student gets the first two parts right, and the
  40   *    other two wrong.
  41   *  - on the second try, they are sure they got the first part right, so keep
  42   *    their answer the same, but they change their answer to the second part.
  43   *    They also get the answer to the thrid part right on this try, but still
  44   *    get the 4th part wrong.
  45   *  - On the final try, they get the first 3 parts right, but the 4th part still
  46   *    wrong.
  47   * We want to grade them as follows.
  48   *  - For the first part, they were right first time, and did not change their
  49   *    answer, so we credit that part as right first time: 3/3
  50   *  - For the second part, although they were right first time, they then changed
  51   *    their mind, an only finally got it right on the third try, so 1/3.
  52   *  - For the third part, they got it right on the second try, and then did not
  53   *    change their answer, so 2/3.
  54   *  - For the last part, they were wrong at the last try, so 0/3.
  55   * So, total mark is 6/12. (Really, a fraction of 0.5.)
  56   *
  57   * Of course, the details of the grading are actually up to the particular
  58   * question type. The point is that the final grade can take into account all
  59   * of the tries the student made.
  60   *
  61   * @copyright  2009 The Open University
  62   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  63   */
  64  class qbehaviour_interactivecountback extends qbehaviour_interactive {
  65  
  66      public function is_compatible_question(question_definition $question) {
  67          return $question instanceof question_automatically_gradable_with_countback;
  68      }
  69  
  70      protected function adjust_fraction($fraction, question_attempt_pending_step $pendingstep) {
  71          $totaltries = $this->qa->get_step(0)->get_behaviour_var('_triesleft');
  72  
  73          $responses = array();
  74          $lastsave = array();
  75          foreach ($this->qa->get_step_iterator() as $step) {
  76              if ($step->has_behaviour_var('submit') &&
  77                      $step->get_state() != question_state::$invalid) {
  78                  $responses[] = $step->get_qt_data();
  79                  $lastsave = array();
  80              } else {
  81                  $lastsave = $step->get_qt_data();
  82              }
  83          }
  84          $lastresponse = $pendingstep->get_qt_data();
  85          if (!empty($lastresponse)) {
  86              $responses[] = $lastresponse;
  87          } else if (!empty($lastsave)) {
  88              $responses[] = $lastsave;
  89          }
  90  
  91          return $this->question->compute_final_grade($responses, $totaltries);
  92      }
  93  }