[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/essay/ -> 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   * Essay question definition class.
  19   *
  20   * @package    qtype
  21   * @subpackage essay
  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  require_once($CFG->dirroot . '/question/type/questionbase.php');
  30  
  31  /**
  32   * Represents an essay question.
  33   *
  34   * @copyright  2009 The Open University
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class qtype_essay_question extends question_with_responses {
  38  
  39      public $responseformat;
  40  
  41      /** @var int Indicates whether an inline response is required ('0') or optional ('1')  */
  42      public $responserequired;
  43  
  44      public $responsefieldlines;
  45      public $attachments;
  46  
  47      /** @var int The number of attachments required for a response to be complete. */
  48      public $attachmentsrequired;
  49  
  50      public $graderinfo;
  51      public $graderinfoformat;
  52      public $responsetemplate;
  53      public $responsetemplateformat;
  54  
  55      public function make_behaviour(question_attempt $qa, $preferredbehaviour) {
  56          return question_engine::make_behaviour('manualgraded', $qa, $preferredbehaviour);
  57      }
  58  
  59      /**
  60       * @param moodle_page the page we are outputting to.
  61       * @return qtype_essay_format_renderer_base the response-format-specific renderer.
  62       */
  63      public function get_format_renderer(moodle_page $page) {
  64          return $page->get_renderer('qtype_essay', 'format_' . $this->responseformat);
  65      }
  66  
  67      public function get_expected_data() {
  68          if ($this->responseformat == 'editorfilepicker') {
  69              $expecteddata = array('answer' => question_attempt::PARAM_RAW_FILES);
  70          } else {
  71              $expecteddata = array('answer' => PARAM_RAW);
  72          }
  73          $expecteddata['answerformat'] = PARAM_ALPHANUMEXT;
  74          if ($this->attachments != 0) {
  75              $expecteddata['attachments'] = question_attempt::PARAM_FILES;
  76          }
  77          return $expecteddata;
  78      }
  79  
  80      public function summarise_response(array $response) {
  81          if (isset($response['answer'])) {
  82              return question_utils::to_plain_text($response['answer'],
  83                      $response['answerformat'], array('para' => false));
  84          } else {
  85              return null;
  86          }
  87      }
  88  
  89      public function get_correct_response() {
  90          return null;
  91      }
  92  
  93      public function is_complete_response(array $response) {
  94          // Determine if the given response has inline text and attachments.
  95          $hasinlinetext = array_key_exists('answer', $response) && ($response['answer'] !== '');
  96          $hasattachments = array_key_exists('attachments', $response)
  97              && $response['attachments'] instanceof question_response_files;
  98  
  99          // Determine the number of attachments present.
 100          if ($hasattachments) {
 101              $attachcount = count($response['attachments']->get_files());
 102          } else {
 103              $attachcount = 0;
 104          }
 105  
 106          // Determine if we have /some/ content to be graded.
 107          $hascontent = $hasinlinetext || ($attachcount > 0);
 108  
 109          // Determine if we meet the optional requirements.
 110          $meetsinlinereq = $hasinlinetext || (!$this->responserequired) || ($this->responseformat == 'noinline');
 111          $meetsattachmentreq = ($attachcount >= $this->attachmentsrequired);
 112  
 113          // The response is complete iff all of our requirements are met.
 114          return $hascontent && $meetsinlinereq && $meetsattachmentreq;
 115      }
 116  
 117      public function is_same_response(array $prevresponse, array $newresponse) {
 118          if (array_key_exists('answer', $prevresponse) && $prevresponse['answer'] !== $this->responsetemplate) {
 119              $value1 = (string) $prevresponse['answer'];
 120          } else {
 121              $value1 = '';
 122          }
 123          if (array_key_exists('answer', $newresponse) && $newresponse['answer'] !== $this->responsetemplate) {
 124              $value2 = (string) $newresponse['answer'];
 125          } else {
 126              $value2 = '';
 127          }
 128          return $value1 === $value2 && ($this->attachments == 0 ||
 129                  question_utils::arrays_same_at_key_missing_is_blank(
 130                  $prevresponse, $newresponse, 'attachments'));
 131      }
 132  
 133      public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
 134          if ($component == 'question' && $filearea == 'response_attachments') {
 135              // Response attachments visible if the question has them.
 136              return $this->attachments != 0;
 137  
 138          } else if ($component == 'question' && $filearea == 'response_answer') {
 139              // Response attachments visible if the question has them.
 140              return $this->responseformat === 'editorfilepicker';
 141  
 142          } else if ($component == 'qtype_essay' && $filearea == 'graderinfo') {
 143              return $options->manualcomment;
 144  
 145          } else {
 146              return parent::check_file_access($qa, $options, $component,
 147                      $filearea, $args, $forcedownload);
 148          }
 149      }
 150  }


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