[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/essay/ -> renderer.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 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;
 119  
 120          $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
 121                  'attachments', $options->context->id);
 122  
 123          $fm = new form_filemanager($pickeroptions);
 124          $filesrenderer = $this->page->get_renderer('core', 'files');
 125          return $filesrenderer->render($fm). html_writer::empty_tag(
 126                  'input', array('type' => 'hidden', 'name' => $qa->get_qt_field_name('attachments'),
 127                  'value' => $pickeroptions->itemid));
 128      }
 129  
 130      public function manual_comment(question_attempt $qa, question_display_options $options) {
 131          if ($options->manualcomment != question_display_options::EDITABLE) {
 132              return '';
 133          }
 134  
 135          $question = $qa->get_question();
 136          return html_writer::nonempty_tag('div', $question->format_text(
 137                  $question->graderinfo, $question->graderinfo, $qa, 'qtype_essay',
 138                  'graderinfo', $question->id), array('class' => 'graderinfo'));
 139      }
 140  }
 141  
 142  
 143  /**
 144   * A base class to abstract out the differences between different type of
 145   * response format.
 146   *
 147   * @copyright  2011 The Open University
 148   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 149   */
 150  abstract class qtype_essay_format_renderer_base extends plugin_renderer_base {
 151      /**
 152       * Render the students respone when the question is in read-only mode.
 153       * @param string $name the variable name this input edits.
 154       * @param question_attempt $qa the question attempt being display.
 155       * @param question_attempt_step $step the current step.
 156       * @param int $lines approximate size of input box to display.
 157       * @param object $context the context teh output belongs to.
 158       * @return string html to display the response.
 159       */
 160      public abstract function response_area_read_only($name, question_attempt $qa,
 161              question_attempt_step $step, $lines, $context);
 162  
 163      /**
 164       * Render the students respone when the question is in read-only mode.
 165       * @param string $name the variable name this input edits.
 166       * @param question_attempt $qa the question attempt being display.
 167       * @param question_attempt_step $step the current step.
 168       * @param int $lines approximate size of input box to display.
 169       * @param object $context the context teh output belongs to.
 170       * @return string html to display the response for editing.
 171       */
 172      public abstract function response_area_input($name, question_attempt $qa,
 173              question_attempt_step $step, $lines, $context);
 174  
 175      /**
 176       * @return string specific class name to add to the input element.
 177       */
 178      protected abstract function class_name();
 179  }
 180  
 181  /**
 182   * An essay format renderer for essays where the student should not enter
 183   * any inline response.
 184   *
 185   * @copyright  2013 Binghamton University
 186   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 187   */
 188  class qtype_essay_format_noinline_renderer extends plugin_renderer_base {
 189  
 190      protected function class_name() {
 191          return 'qtype_essay_noinline';
 192      }
 193  
 194      public function response_area_read_only($name, $qa, $step, $lines, $context) {
 195          return '';
 196      }
 197  
 198      public function response_area_input($name, $qa, $step, $lines, $context) {
 199          return '';
 200      }
 201  
 202  }
 203  
 204  /**
 205   * An essay format renderer for essays where the student should use the HTML
 206   * editor without the file picker.
 207   *
 208   * @copyright  2011 The Open University
 209   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 210   */
 211  class qtype_essay_format_editor_renderer extends plugin_renderer_base {
 212      protected function class_name() {
 213          return 'qtype_essay_editor';
 214      }
 215  
 216      public function response_area_read_only($name, $qa, $step, $lines, $context) {
 217          return html_writer::tag('div', $this->prepare_response($name, $qa, $step, $context),
 218                  array('class' => $this->class_name() . ' qtype_essay_response readonly'));
 219      }
 220  
 221      public function response_area_input($name, $qa, $step, $lines, $context) {
 222          global $CFG;
 223          require_once($CFG->dirroot . '/repository/lib.php');
 224  
 225          $inputname = $qa->get_qt_field_name($name);
 226          $responseformat = $step->get_qt_var($name . 'format');
 227          $id = $inputname . '_id';
 228  
 229          $editor = editors_get_preferred_editor($responseformat);
 230          $strformats = format_text_menu();
 231          $formats = $editor->get_supported_formats();
 232          foreach ($formats as $fid) {
 233              $formats[$fid] = $strformats[$fid];
 234          }
 235  
 236          list($draftitemid, $response) = $this->prepare_response_for_editing(
 237                  $name, $step, $context);
 238  
 239          $editor->set_text($response);
 240          $editor->use_editor($id, $this->get_editor_options($context),
 241                  $this->get_filepicker_options($context, $draftitemid));
 242  
 243          $output = '';
 244          $output .= html_writer::start_tag('div', array('class' =>
 245                  $this->class_name() . ' qtype_essay_response'));
 246  
 247          $output .= html_writer::tag('div', html_writer::tag('textarea', s($response),
 248                  array('id' => $id, 'name' => $inputname, 'rows' => $lines, 'cols' => 60)));
 249  
 250          $output .= html_writer::start_tag('div');
 251          if (count($formats) == 1) {
 252              reset($formats);
 253              $output .= html_writer::empty_tag('input', array('type' => 'hidden',
 254                      'name' => $inputname . 'format', 'value' => key($formats)));
 255  
 256          } else {
 257              $output .= html_writer::label(get_string('format'), 'menu' . $inputname . 'format', false);
 258              $output .= ' ';
 259              $output .= html_writer::select($formats, $inputname . 'format', $responseformat, '');
 260          }
 261          $output .= html_writer::end_tag('div');
 262  
 263          $output .= $this->filepicker_html($inputname, $draftitemid);
 264  
 265          $output .= html_writer::end_tag('div');
 266          return $output;
 267      }
 268  
 269      /**
 270       * Prepare the response for read-only display.
 271       * @param string $name the variable name this input edits.
 272       * @param question_attempt $qa the question attempt being display.
 273       * @param question_attempt_step $step the current step.
 274       * @param object $context the context the attempt belongs to.
 275       * @return string the response prepared for display.
 276       */
 277      protected function prepare_response($name, question_attempt $qa,
 278              question_attempt_step $step, $context) {
 279          if (!$step->has_qt_var($name)) {
 280              return '';
 281          }
 282  
 283          $formatoptions = new stdClass();
 284          $formatoptions->para = false;
 285          return format_text($step->get_qt_var($name), $step->get_qt_var($name . 'format'),
 286                  $formatoptions);
 287      }
 288  
 289      /**
 290       * Prepare the response for editing.
 291       * @param string $name the variable name this input edits.
 292       * @param question_attempt_step $step the current step.
 293       * @param object $context the context the attempt belongs to.
 294       * @return string the response prepared for display.
 295       */
 296      protected function prepare_response_for_editing($name,
 297              question_attempt_step $step, $context) {
 298          return array(0, $step->get_qt_var($name));
 299      }
 300  
 301      /**
 302       * @param object $context the context the attempt belongs to.
 303       * @return array options for the editor.
 304       */
 305      protected function get_editor_options($context) {
 306          // Disable the text-editor autosave because quiz has it's own auto save function.
 307          return array('context' => $context, 'autosave' => false);
 308      }
 309  
 310      /**
 311       * @param object $context the context the attempt belongs to.
 312       * @param int $draftitemid draft item id.
 313       * @return array filepicker options for the editor.
 314       */
 315      protected function get_filepicker_options($context, $draftitemid) {
 316          return array('return_types'  => FILE_INTERNAL | FILE_EXTERNAL);
 317      }
 318  
 319      /**
 320       * @param string $inputname input field name.
 321       * @param int $draftitemid draft file area itemid.
 322       * @return string HTML for the filepicker, if used.
 323       */
 324      protected function filepicker_html($inputname, $draftitemid) {
 325          return '';
 326      }
 327  }
 328  
 329  
 330  /**
 331   * An essay format renderer for essays where the student should use the HTML
 332   * editor with the file picker.
 333   *
 334   * @copyright  2011 The Open University
 335   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 336   */
 337  class qtype_essay_format_editorfilepicker_renderer extends qtype_essay_format_editor_renderer {
 338      protected function class_name() {
 339          return 'qtype_essay_editorfilepicker';
 340      }
 341  
 342      protected function prepare_response($name, question_attempt $qa,
 343              question_attempt_step $step, $context) {
 344          if (!$step->has_qt_var($name)) {
 345              return '';
 346          }
 347  
 348          $formatoptions = new stdClass();
 349          $formatoptions->para = false;
 350          $text = $qa->rewrite_response_pluginfile_urls($step->get_qt_var($name),
 351                  $context->id, 'answer', $step);
 352          return format_text($text, $step->get_qt_var($name . 'format'), $formatoptions);
 353      }
 354  
 355      protected function prepare_response_for_editing($name,
 356              question_attempt_step $step, $context) {
 357          return $step->prepare_response_files_draft_itemid_with_text(
 358                  $name, $context->id, $step->get_qt_var($name));
 359      }
 360  
 361      protected function get_editor_options($context) {
 362          // Disable the text-editor autosave because quiz has it's own auto save function.
 363          return array(
 364              'subdirs' => 0,
 365              'maxbytes' => 0,
 366              'maxfiles' => -1,
 367              'context' => $context,
 368              'noclean' => 0,
 369              'trusttext'=> 0,
 370              'autosave' => false
 371          );
 372      }
 373  
 374      /**
 375       * Get the options required to configure the filepicker for one of the editor
 376       * toolbar buttons.
 377       * @param mixed $acceptedtypes array of types of '*'.
 378       * @param int $draftitemid the draft area item id.
 379       * @param object $context the context.
 380       * @return object the required options.
 381       */
 382      protected function specific_filepicker_options($acceptedtypes, $draftitemid, $context) {
 383          $filepickeroptions = new stdClass();
 384          $filepickeroptions->accepted_types = $acceptedtypes;
 385          $filepickeroptions->return_types = FILE_INTERNAL | FILE_EXTERNAL;
 386          $filepickeroptions->context = $context;
 387          $filepickeroptions->env = 'filepicker';
 388  
 389          $options = initialise_filepicker($filepickeroptions);
 390          $options->context = $context;
 391          $options->client_id = uniqid();
 392          $options->env = 'editor';
 393          $options->itemid = $draftitemid;
 394  
 395          return $options;
 396      }
 397  
 398      protected function get_filepicker_options($context, $draftitemid) {
 399          global $CFG;
 400  
 401          return array(
 402              'image' => $this->specific_filepicker_options(array('image'),
 403                              $draftitemid, $context),
 404              'media' => $this->specific_filepicker_options(array('video', 'audio'),
 405                              $draftitemid, $context),
 406              'link'  => $this->specific_filepicker_options('*',
 407                              $draftitemid, $context),
 408          );
 409      }
 410  
 411      protected function filepicker_html($inputname, $draftitemid) {
 412          $nonjspickerurl = new moodle_url('/repository/draftfiles_manager.php', array(
 413              'action' => 'browse',
 414              'env' => 'editor',
 415              'itemid' => $draftitemid,
 416              'subdirs' => false,
 417              'maxfiles' => -1,
 418              'sesskey' => sesskey(),
 419          ));
 420  
 421          return html_writer::empty_tag('input', array('type' => 'hidden',
 422                  'name' => $inputname . ':itemid', 'value' => $draftitemid)) .
 423                  html_writer::tag('noscript', html_writer::tag('div',
 424                      html_writer::tag('object', '', array('type' => 'text/html',
 425                          'data' => $nonjspickerurl, 'height' => 160, 'width' => 600,
 426                          'style' => 'border: 1px solid #000;'))));
 427      }
 428  }
 429  
 430  
 431  /**
 432   * An essay format renderer for essays where the student should use a plain
 433   * input box, but with a normal, proportional font.
 434   *
 435   * @copyright  2011 The Open University
 436   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 437   */
 438  class qtype_essay_format_plain_renderer extends plugin_renderer_base {
 439      /**
 440       * @return string the HTML for the textarea.
 441       */
 442      protected function textarea($response, $lines, $attributes) {
 443          $attributes['class'] = $this->class_name() . ' qtype_essay_response';
 444          $attributes['rows'] = $lines;
 445          $attributes['cols'] = 60;
 446          return html_writer::tag('textarea', s($response), $attributes);
 447      }
 448  
 449      protected function class_name() {
 450          return 'qtype_essay_plain';
 451      }
 452  
 453      public function response_area_read_only($name, $qa, $step, $lines, $context) {
 454          return $this->textarea($step->get_qt_var($name), $lines, array('readonly' => 'readonly'));
 455      }
 456  
 457      public function response_area_input($name, $qa, $step, $lines, $context) {
 458          $inputname = $qa->get_qt_field_name($name);
 459          return $this->textarea($step->get_qt_var($name), $lines, array('name' => $inputname)) .
 460                  html_writer::empty_tag('input', array('type' => 'hidden',
 461                      'name' => $inputname . 'format', 'value' => FORMAT_PLAIN));
 462      }
 463  }
 464  
 465  
 466  /**
 467   * An essay format renderer for essays where the student should use a plain
 468   * input box with a monospaced font. You might use this, for example, for a
 469   * question where the students should type computer code.
 470   *
 471   * @copyright  2011 The Open University
 472   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 473   */
 474  class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
 475      protected function class_name() {
 476          return 'qtype_essay_monospaced';
 477      }
 478  }


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