Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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   * Upgrade library code for the numerical question type.
  19   *
  20   * @package    qtype
  21   * @subpackage numerical
  22   * @copyright  2010 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  
  30  /**
  31   * Class for converting attempt data for numerical questions when upgrading
  32   * attempts to the new question engine.
  33   *
  34   * This class is used by the code in question/engine/upgrade/upgradelib.php.
  35   *
  36   * @copyright  2010 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_numerical_qe2_attempt_updater extends question_qtype_attempt_updater {
  40      public function right_answer() {
  41          foreach ($this->question->options->answers as $ans) {
  42              if ($ans->fraction > 0.999) {
  43                  $right = $ans->answer;
  44  
  45                  if (empty($this->question->options->units)) {
  46                      return $right;
  47                  }
  48  
  49                  $unit = reset($this->question->options->units);
  50                  $unit = $unit->unit;
  51                  if (!empty($this->question->options->unitsleft)) {
  52                      return $unit . ' ' . $right;
  53                  } else {
  54                      return $right . ' ' . $unit;
  55                  }
  56              }
  57          }
  58      }
  59  
  60      public function response_summary($state) {
  61          if (strpos($state->answer, '|||||') === false) {
  62              $answer = $state->answer;
  63              $unit = '';
  64          } else {
  65              list($answer, $unit) = explode('|||||', $state->answer, 2);
  66          }
  67  
  68          if (empty($answer) && empty($unit)) {
  69              $resp = null;
  70          } else {
  71              $resp = $answer;
  72          }
  73  
  74          if (!empty($unit)) {
  75              if (!empty($this->question->options->unitsleft)) {
  76                  $resp = trim($unit . ' ' . $resp);
  77              } else {
  78                  $resp = trim($resp . ' ' . $unit);
  79              }
  80          }
  81  
  82          return $resp;
  83      }
  84  
  85      public function was_answered($state) {
  86          return !empty($state->answer);
  87      }
  88  
  89      public function set_first_step_data_elements($state, &$data) {
  90          $data['_separators'] = '.$,';
  91      }
  92  
  93      public function supply_missing_first_step_data(&$data) {
  94          $data['_separators'] = '.$,';
  95      }
  96  
  97      public function set_data_elements_for_step($state, &$data) {
  98          if (empty($state->answer)) {
  99              return;
 100          }
 101          if (strpos($state->answer, '|||||') === false) {
 102              $data['answer'] = $state->answer;
 103          } else {
 104              list($answer, $unit) = explode('|||||', $state->answer, 2);
 105              if (!empty($this->question->options->showunits) &&
 106                      $this->question->options->showunits == 1) {
 107                  // Multichoice units.
 108                  $data['answer'] = $answer;
 109                  $data['unit'] = $unit;
 110              } else if (!empty($this->question->options->unitsleft)) {
 111                  if (!empty($unit)) {
 112                      $data['answer'] = $unit . ' ' . $answer;
 113                  } else {
 114                      $data['answer'] = $answer;
 115                  }
 116              } else {
 117                  if (!empty($unit)) {
 118                      $data['answer'] = $answer . ' ' . $unit;
 119                  } else {
 120                      $data['answer'] = $answer;
 121                  }
 122              }
 123          }
 124      }
 125  }