[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/numerical/db/ -> upgradelib.php (source)

   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   * TODO update for the changes in Moodle 2.0.
  37   *
  38   * @copyright  2010 The Open University
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class qtype_numerical_qe2_attempt_updater extends question_qtype_attempt_updater {
  42      public function right_answer() {
  43          foreach ($this->question->options->answers as $ans) {
  44              if ($ans->fraction > 0.999) {
  45                  $right = $ans->answer;
  46  
  47                  if (empty($this->question->options->units)) {
  48                      return $right;
  49                  }
  50  
  51                  $unit = reset($this->question->options->units);
  52                  $unit = $unit->unit;
  53                  if (!empty($this->question->options->unitsleft)) {
  54                      return $unit . ' ' . $right;
  55                  } else {
  56                      return $right . ' ' . $unit;
  57                  }
  58              }
  59          }
  60      }
  61  
  62      public function response_summary($state) {
  63          if (strpos($state->answer, '|||||') === false) {
  64              $answer = $state->answer;
  65              $unit = '';
  66          } else {
  67              list($answer, $unit) = explode('|||||', $state->answer, 2);
  68          }
  69  
  70          if (empty($answer) && empty($unit)) {
  71              $resp = null;
  72          } else {
  73              $resp = $answer;
  74          }
  75  
  76          if (!empty($unit)) {
  77              if (!empty($this->question->options->unitsleft)) {
  78                  $resp = trim($unit . ' ' . $resp);
  79              } else {
  80                  $resp = trim($resp . ' ' . $unit);
  81              }
  82          }
  83  
  84          return $resp;
  85      }
  86  
  87      public function was_answered($state) {
  88          return !empty($state->answer);
  89      }
  90  
  91      public function set_first_step_data_elements($state, &$data) {
  92          $data['_separators'] = '.$,';
  93      }
  94  
  95      public function supply_missing_first_step_data(&$data) {
  96          $data['_separators'] = '.$,';
  97      }
  98  
  99      public function set_data_elements_for_step($state, &$data) {
 100          if (empty($state->answer)) {
 101              return;
 102          }
 103          if (strpos($state->answer, '|||||') === false) {
 104              $data['answer'] = $state->answer;
 105          } else {
 106              list($answer, $unit) = explode('|||||', $state->answer, 2);
 107              if (!empty($this->question->options->showunits) &&
 108                      $this->question->options->showunits == 1) {
 109                  // Multichoice units.
 110                  $data['answer'] = $answer;
 111                  $data['unit'] = $unit;
 112              } else if (!empty($this->question->options->unitsleft)) {
 113                  if (!empty($unit)) {
 114                      $data['answer'] = $unit . ' ' . $answer;
 115                  } else {
 116                      $data['answer'] = $answer;
 117                  }
 118              } else {
 119                  if (!empty($unit)) {
 120                      $data['answer'] = $answer . ' ' . $unit;
 121                  } else {
 122                      $data['answer'] = $answer;
 123                  }
 124              }
 125          }
 126      }
 127  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1