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.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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 renderer 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  
  30  /**
  31   * Generates the output for essay questions.
  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_essay_renderer extends qtype_renderer {
  37      public function formulation_and_controls(question_attempt $qa,
  38              question_display_options $options) {
  39  
  40          $question = $qa->get_question();
  41          $responseoutput = $question->get_format_renderer($this->page);
  42  
  43          // Answer field.
  44          $step = $qa->get_last_step_with_qt_var('answer');
  45  
  46          if (!$step->has_qt_var('answer') && empty($options->readonly)) {
  47              // Question has never been answered, fill it with response template.
  48              $step = new question_attempt_step(array('answer'=>$question->responsetemplate));
  49          }
  50  
  51          if (empty($options->readonly)) {
  52              $answer = $responseoutput->response_area_input('answer', $qa,
  53                      $step, $question->responsefieldlines, $options->context);
  54  
  55          } else {
  56              $answer = $responseoutput->response_area_read_only('answer', $qa,
  57                      $step, $question->responsefieldlines, $options->context);
  58          }
  59  
  60          $files = '';
  61          if ($question->attachments) {
  62              if (empty($options->readonly)) {
  63                  $files = $this->files_input($qa, $question->attachments, $options);
  64  
  65              } else {
  66                  $files = $this->files_read_only($qa, $options);
  67              }
  68          }
  69  
  70          $result = '';
  71          $result .= html_writer::tag('div', $question->format_questiontext($qa),
  72                  array('class' => 'qtext'));
  73  
  74          $result .= html_writer::start_tag('div', array('class' => 'ablock'));
  75          $result .= html_writer::tag('div', $answer, array('class' => 'answer'));
  76          $result .= html_writer::tag('div', $files, array('class' => 'attachments'));
  77          $result .= html_writer::end_tag('div');
  78  
  79          return $result;
  80      }
  81  
  82      /**
  83       * Displays any attached files when the question is in read-only mode.
  84       * @param question_attempt $qa the question attempt to display.
  85       * @param question_display_options $options controls what should and should
  86       *      not be displayed. Used to get the context.
  87       */
  88      public function files_read_only(question_attempt $qa, question_display_options $options) {
  89          $files = $qa->get_last_qt_files('attachments', $options->context->id);
  90          $output = array();
  91  
  92          foreach ($files as $file) {
  93              $output[] = html_writer::tag('p', html_writer::link($qa->get_response_file_url($file),
  94                      $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file),
  95                      'moodle', array('class' => 'icon')) . ' ' . s($file->get_filename())));
  96          }
  97          return implode($output);
  98      }
  99  
 100      /**
 101       * Displays the input control for when the student should upload a single file.
 102       * @param question_attempt $qa the question attempt to display.
 103       * @param int $numallowed the maximum number of attachments allowed. -1 = unlimited.
 104       * @param question_display_options $options controls what should and should
 105       *      not be displayed. Used to get the context.
 106       */
 107      public function files_input(question_attempt $qa, $numallowed,
 108              question_display_options $options) {
 109          global $CFG;
 110          require_once($CFG->dirroot . '/lib/form/filemanager.php');
 111  
 112          $pickeroptions = new stdClass();
 113          $pickeroptions->mainfile = null;
 114          $pickeroptions->maxfiles = $numallowed;
 115          $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
 116                  'attachments', $options->context->id);
 117          $pickeroptions->context = $options->context;
 118          $pickeroptions->return_types = FILE_INTERNAL | FILE_CONTROLLED_LINK;
 119  
 120          $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
 121                  'attachments', $options->context->id);
 122          $pickeroptions->accepted_types = $qa->get_question()->filetypeslist;
 123  
 124          $fm = new form_filemanager($pickeroptions);
 125          $filesrenderer = $this->page->get_renderer('core', 'files');
 126  
 127          $text = '';
 128          if (!empty($qa->get_question()->filetypeslist)) {
 129              $text = html_writer::tag('p', get_string('acceptedfiletypes', 'qtype_essay'));
 130              $filetypesutil = new \core_form\filetypes_util();
 131              $filetypes = $qa->get_question()->filetypeslist;
 132              $filetypedescriptions = $filetypesutil->describe_file_types($filetypes);
 133              $text .= $this->render_from_template('core_form/filetypes-descriptions', $filetypedescriptions);
 134          }
 135          return $filesrenderer->render($fm). html_writer::empty_tag(
 136                  'input', array('type' => 'hidden', 'name' => $qa->get_qt_field_name('attachments'),
 137                  'value' => $pickeroptions->itemid)) . $text;
 138      }
 139  
 140      public function manual_comment(question_attempt $qa, question_display_options $options) {
 141          if ($options->manualcomment != question_display_options::EDITABLE) {
 142              return '';
 143          }
 144  
 145          $question = $qa->get_question();
 146          return html_writer::nonempty_tag('div', $question->format_text(
 147                  $question->graderinfo, $question->graderinfo, $qa, 'qtype_essay',
 148                  'graderinfo', $question->id), array('class' => 'graderinfo'));
 149      }
 150  }
 151  
 152  
 153  /**
 154   * A base class to abstract out the differences between different type of
 155   * response format.
 156   *
 157   * @copyright  2011 The Open University
 158   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 159   */
 160  abstract class qtype_essay_format_renderer_base extends plugin_renderer_base {
 161      /**
 162       * Render the students respone when the question is in read-only mode.
 163       * @param string $name the variable name this input edits.
 164       * @param question_attempt $qa the question attempt being display.
 165       * @param question_attempt_step $step the current step.
 166       * @param int $lines approximate size of input box to display.
 167       * @param object $context the context teh output belongs to.
 168       * @return string html to display the response.
 169       */
 170      public abstract function response_area_read_only($name, question_attempt $qa,
 171              question_attempt_step $step, $lines, $context);
 172  
 173      /**
 174       * Render the students respone when the question is in read-only mode.
 175       * @param string $name the variable name this input edits.
 176       * @param question_attempt $qa the question attempt being display.
 177       * @param question_attempt_step $step the current step.
 178       * @param int $lines approximate size of input box to display.
 179       * @param object $context the context teh output belongs to.
 180       * @return string html to display the response for editing.
 181       */
 182      public abstract function response_area_input($name, question_attempt $qa,
 183              question_attempt_step $step, $lines, $context);
 184  
 185      /**
 186       * @return string specific class name to add to the input element.
 187       */
 188      protected abstract function class_name();
 189  }
 190  
 191  /**
 192   * An essay format renderer for essays where the student should not enter
 193   * any inline response.
 194   *
 195   * @copyright  2013 Binghamton University
 196   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 197   */
 198  class qtype_essay_format_noinline_renderer extends plugin_renderer_base {
 199  
 200      protected function class_name() {
 201          return 'qtype_essay_noinline';
 202      }
 203  
 204      public function response_area_read_only($name, $qa, $step, $lines, $context) {
 205          return '';
 206      }
 207  
 208      public function response_area_input($name, $qa, $step, $lines, $context) {
 209          return '';
 210      }
 211  
 212  }
 213  
 214  /**
 215   * An essay format renderer for essays where the student should use the HTML
 216   * editor without the file picker.
 217   *
 218   * @copyright  2011 The Open University
 219   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 220   */
 221  class qtype_essay_format_editor_renderer extends plugin_renderer_base {
 222      protected function class_name() {
 223          return 'qtype_essay_editor';
 224      }
 225  
 226      public function response_area_read_only($name, $qa, $step, $lines, $context) {
 227          return html_writer::tag('div', $this->prepare_response($name, $qa, $step, $context),
 228                  ['class' => $this->class_name() . ' qtype_essay_response readonly',
 229                          'style' => 'min-height: ' . ($lines * 1.5) . 'em;']);
 230          // Height $lines * 1.5 because that is a typical line-height on web pages.
 231          // That seems to give results that look OK.
 232      }
 233  
 234      public function response_area_input($name, $qa, $step, $lines, $context) {
 235          global $CFG;
 236          require_once($CFG->dirroot . '/repository/lib.php');
 237  
 238          $inputname = $qa->get_qt_field_name($name);
 239          $responseformat = $step->get_qt_var($name . 'format');
 240          $id = $inputname . '_id';
 241  
 242          $editor = editors_get_preferred_editor($responseformat);
 243          $strformats = format_text_menu();
 244          $formats = $editor->get_supported_formats();
 245          foreach ($formats as $fid) {
 246              $formats[$fid] = $strformats[$fid];
 247          }
 248  
 249          list($draftitemid, $response) = $this->prepare_response_for_editing(
 250                  $name, $step, $context);
 251  
 252          $editor->set_text($response);
 253          $editor->use_editor($id, $this->get_editor_options($context),
 254                  $this->get_filepicker_options($context, $draftitemid));
 255  
 256          $output = '';
 257          $output .= html_writer::start_tag('div', array('class' =>
 258                  $this->class_name() . ' qtype_essay_response'));
 259  
 260          $output .= html_writer::tag('div', html_writer::tag('textarea', s($response),
 261                  array('id' => $id, 'name' => $inputname, 'rows' => $lines, 'cols' => 60, 'class' => 'form-control')));
 262  
 263          $output .= html_writer::start_tag('div');
 264          if (count($formats) == 1) {
 265              reset($formats);
 266              $output .= html_writer::empty_tag('input', array('type' => 'hidden',
 267                      'name' => $inputname . 'format', 'value' => key($formats)));
 268  
 269          } else {
 270              $output .= html_writer::label(get_string('format'), 'menu' . $inputname . 'format', false);
 271              $output .= ' ';
 272              $output .= html_writer::select($formats, $inputname . 'format', $responseformat, '');
 273          }
 274          $output .= html_writer::end_tag('div');
 275  
 276          $output .= $this->filepicker_html($inputname, $draftitemid);
 277  
 278          $output .= html_writer::end_tag('div');
 279          return $output;
 280      }
 281  
 282      /**
 283       * Prepare the response for read-only display.
 284       * @param string $name the variable name this input edits.
 285       * @param question_attempt $qa the question attempt being display.
 286       * @param question_attempt_step $step the current step.
 287       * @param object $context the context the attempt belongs to.
 288       * @return string the response prepared for display.
 289       */
 290      protected function prepare_response($name, question_attempt $qa,
 291              question_attempt_step $step, $context) {
 292          if (!$step->has_qt_var($name)) {
 293              return '';
 294          }
 295  
 296          $formatoptions = new stdClass();
 297          $formatoptions->para = false;
 298          return format_text($step->get_qt_var($name), $step->get_qt_var($name . 'format'),
 299                  $formatoptions);
 300      }
 301  
 302      /**
 303       * Prepare the response for editing.
 304       * @param string $name the variable name this input edits.
 305       * @param question_attempt_step $step the current step.
 306       * @param object $context the context the attempt belongs to.
 307       * @return string the response prepared for display.
 308       */
 309      protected function prepare_response_for_editing($name,
 310              question_attempt_step $step, $context) {
 311          return array(0, $step->get_qt_var($name));
 312      }
 313  
 314      /**
 315       * @param object $context the context the attempt belongs to.
 316       * @return array options for the editor.
 317       */
 318      protected function get_editor_options($context) {
 319          // Disable the text-editor autosave because quiz has it's own auto save function.
 320          return array('context' => $context, 'autosave' => false);
 321      }
 322  
 323      /**
 324       * @param object $context the context the attempt belongs to.
 325       * @param int $draftitemid draft item id.
 326       * @return array filepicker options for the editor.
 327       */
 328      protected function get_filepicker_options($context, $draftitemid) {
 329          return array('return_types'  => FILE_INTERNAL | FILE_EXTERNAL);
 330      }
 331  
 332      /**
 333       * @param string $inputname input field name.
 334       * @param int $draftitemid draft file area itemid.
 335       * @return string HTML for the filepicker, if used.
 336       */
 337      protected function filepicker_html($inputname, $draftitemid) {
 338          return '';
 339      }
 340  }
 341  
 342  
 343  /**
 344   * An essay format renderer for essays where the student should use the HTML
 345   * editor with the file picker.
 346   *
 347   * @copyright  2011 The Open University
 348   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 349   */
 350  class qtype_essay_format_editorfilepicker_renderer extends qtype_essay_format_editor_renderer {
 351      protected function class_name() {
 352          return 'qtype_essay_editorfilepicker';
 353      }
 354  
 355      protected function prepare_response($name, question_attempt $qa,
 356              question_attempt_step $step, $context) {
 357          if (!$step->has_qt_var($name)) {
 358              return '';
 359          }
 360  
 361          $formatoptions = new stdClass();
 362          $formatoptions->para = false;
 363          $text = $qa->rewrite_response_pluginfile_urls($step->get_qt_var($name),
 364                  $context->id, 'answer', $step);
 365          return format_text($text, $step->get_qt_var($name . 'format'), $formatoptions);
 366      }
 367  
 368      protected function prepare_response_for_editing($name,
 369              question_attempt_step $step, $context) {
 370          return $step->prepare_response_files_draft_itemid_with_text(
 371                  $name, $context->id, $step->get_qt_var($name));
 372      }
 373  
 374      /**
 375       * Get editor options for question response text area.
 376       * @param object $context the context the attempt belongs to.
 377       * @return array options for the editor.
 378       */
 379      protected function get_editor_options($context) {
 380          return question_utils::get_editor_options($context);
 381      }
 382  
 383      /**
 384       * Get the options required to configure the filepicker for one of the editor
 385       * toolbar buttons.
 386       * @deprecated since 3.5
 387       * @param mixed $acceptedtypes array of types of '*'.
 388       * @param int $draftitemid the draft area item id.
 389       * @param object $context the context.
 390       * @return object the required options.
 391       */
 392      protected function specific_filepicker_options($acceptedtypes, $draftitemid, $context) {
 393          debugging('qtype_essay_format_editorfilepicker_renderer::specific_filepicker_options() is deprecated, ' .
 394              'use question_utils::specific_filepicker_options() instead.', DEBUG_DEVELOPER);
 395  
 396          $filepickeroptions = new stdClass();
 397          $filepickeroptions->accepted_types = $acceptedtypes;
 398          $filepickeroptions->return_types = FILE_INTERNAL | FILE_EXTERNAL;
 399          $filepickeroptions->context = $context;
 400          $filepickeroptions->env = 'filepicker';
 401  
 402          $options = initialise_filepicker($filepickeroptions);
 403          $options->context = $context;
 404          $options->client_id = uniqid();
 405          $options->env = 'editor';
 406          $options->itemid = $draftitemid;
 407  
 408          return $options;
 409      }
 410  
 411      /**
 412       * @param object $context the context the attempt belongs to.
 413       * @param int $draftitemid draft item id.
 414       * @return array filepicker options for the editor.
 415       */
 416      protected function get_filepicker_options($context, $draftitemid) {
 417          return question_utils::get_filepicker_options($context, $draftitemid);
 418      }
 419  
 420      protected function filepicker_html($inputname, $draftitemid) {
 421          $nonjspickerurl = new moodle_url('/repository/draftfiles_manager.php', array(
 422              'action' => 'browse',
 423              'env' => 'editor',
 424              'itemid' => $draftitemid,
 425              'subdirs' => false,
 426              'maxfiles' => -1,
 427              'sesskey' => sesskey(),
 428          ));
 429  
 430          return html_writer::empty_tag('input', array('type' => 'hidden',
 431                  'name' => $inputname . ':itemid', 'value' => $draftitemid)) .
 432                  html_writer::tag('noscript', html_writer::tag('div',
 433                      html_writer::tag('object', '', array('type' => 'text/html',
 434                          'data' => $nonjspickerurl, 'height' => 160, 'width' => 600,
 435                          'style' => 'border: 1px solid #000;'))));
 436      }
 437  }
 438  
 439  
 440  /**
 441   * An essay format renderer for essays where the student should use a plain
 442   * input box, but with a normal, proportional font.
 443   *
 444   * @copyright  2011 The Open University
 445   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 446   */
 447  class qtype_essay_format_plain_renderer extends plugin_renderer_base {
 448      /**
 449       * @return string the HTML for the textarea.
 450       */
 451      protected function textarea($response, $lines, $attributes) {
 452          $attributes['class'] = $this->class_name() . ' qtype_essay_response form-control';
 453          $attributes['rows'] = $lines;
 454          $attributes['cols'] = 60;
 455          return html_writer::tag('textarea', s($response), $attributes);
 456      }
 457  
 458      protected function class_name() {
 459          return 'qtype_essay_plain';
 460      }
 461  
 462      public function response_area_read_only($name, $qa, $step, $lines, $context) {
 463          return $this->textarea($step->get_qt_var($name), $lines, array('readonly' => 'readonly'));
 464      }
 465  
 466      public function response_area_input($name, $qa, $step, $lines, $context) {
 467          $inputname = $qa->get_qt_field_name($name);
 468          return $this->textarea($step->get_qt_var($name), $lines, array('name' => $inputname)) .
 469                  html_writer::empty_tag('input', array('type' => 'hidden',
 470                      'name' => $inputname . 'format', 'value' => FORMAT_PLAIN));
 471      }
 472  }
 473  
 474  
 475  /**
 476   * An essay format renderer for essays where the student should use a plain
 477   * input box with a monospaced font. You might use this, for example, for a
 478   * question where the students should type computer code.
 479   *
 480   * @copyright  2011 The Open University
 481   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 482   */
 483  class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
 484      protected function class_name() {
 485          return 'qtype_essay_monospaced';
 486      }
 487  }