[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/question/type/truefalse/ -> question.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   * True-false question definition class.
  19   *
  20   * @package    qtype
  21   * @subpackage truefalse
  22   * @copyright  2009 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   * Represents a true-false question.
  32   *
  33   * @copyright  2009 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qtype_truefalse_question extends question_graded_automatically {
  37      public $rightanswer;
  38      public $truefeedback;
  39      public $falsefeedback;
  40      public $trueanswerid;
  41      public $falseanswerid;
  42  
  43      public function get_expected_data() {
  44          return array('answer' => PARAM_INT);
  45      }
  46  
  47      public function get_correct_response() {
  48          return array('answer' => (int) $this->rightanswer);
  49      }
  50  
  51      public function summarise_response(array $response) {
  52          if (!array_key_exists('answer', $response)) {
  53              return null;
  54          } else if ($response['answer']) {
  55              return get_string('true', 'qtype_truefalse');
  56          } else {
  57              return get_string('false', 'qtype_truefalse');
  58          }
  59      }
  60  
  61      public function classify_response(array $response) {
  62          if (!array_key_exists('answer', $response)) {
  63              return array($this->id => question_classified_response::no_response());
  64          }
  65          list($fraction) = $this->grade_response($response);
  66          if ($response['answer']) {
  67              return array($this->id => new question_classified_response(1,
  68                      get_string('true', 'qtype_truefalse'), $fraction));
  69          } else {
  70              return array($this->id => new question_classified_response(0,
  71                      get_string('false', 'qtype_truefalse'), $fraction));
  72          }
  73      }
  74  
  75      public function is_complete_response(array $response) {
  76          return array_key_exists('answer', $response);
  77      }
  78  
  79      public function get_validation_error(array $response) {
  80          if ($this->is_gradable_response($response)) {
  81              return '';
  82          }
  83          return get_string('pleaseselectananswer', 'qtype_truefalse');
  84      }
  85  
  86      public function is_same_response(array $prevresponse, array $newresponse) {
  87          return question_utils::arrays_same_at_key_missing_is_blank(
  88                  $prevresponse, $newresponse, 'answer');
  89      }
  90  
  91      public function grade_response(array $response) {
  92          if ($this->rightanswer == true && $response['answer'] == true) {
  93              $fraction = 1;
  94          } else if ($this->rightanswer == false && $response['answer'] == false) {
  95              $fraction = 1;
  96          } else {
  97              $fraction = 0;
  98          }
  99          return array($fraction, question_state::graded_state_for_fraction($fraction));
 100      }
 101  
 102      public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
 103          if ($component == 'question' && $filearea == 'answerfeedback') {
 104              $answerid = reset($args); // Itemid is answer id.
 105              $response = $qa->get_last_qt_var('answer', '');
 106              return $options->feedback && (
 107                      ($answerid == $this->trueanswerid && $response) ||
 108                      ($answerid == $this->falseanswerid && $response !== ''));
 109  
 110          } else {
 111              return parent::check_file_access($qa, $options, $component, $filearea,
 112                      $args, $forcedownload);
 113          }
 114      }
 115  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1