[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/behaviour/ -> rendererbase.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   * Defines the renderer base class for question behaviours.
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionbehaviours
  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   * Renderer base class for question behaviours.
  32   *
  33   * The methods in this class are mostly called from {@link core_question_renderer}
  34   * which coordinates the overall output of questions.
  35   *
  36   * @copyright  2009 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  abstract class qbehaviour_renderer extends plugin_renderer_base {
  40      /**
  41       * Generate some HTML (which may be blank) that appears in the question
  42       * formulation area, afer the question type generated output.
  43       *
  44       * For example.
  45       * immediatefeedback and interactive mode use this to show the Submit button,
  46       * and CBM use this to display the certainty choices.
  47       *
  48       * @param question_attempt $qa a question attempt.
  49       * @param question_display_options $options controls what should and should not be displayed.
  50       * @return string HTML fragment.
  51       */
  52      public function controls(question_attempt $qa, question_display_options $options) {
  53          return '';
  54      }
  55  
  56      /**
  57       * Generate some HTML (which may be blank) that appears in the outcome area,
  58       * after the question-type generated output.
  59       *
  60       * For example, the CBM models use this to display an explanation of the score
  61       * adjustment that was made based on the certainty selected.
  62       *
  63       * @param question_attempt $qa a question attempt.
  64       * @param question_display_options $options controls what should and should not be displayed.
  65       * @return string HTML fragment.
  66       */
  67      public function feedback(question_attempt $qa, question_display_options $options) {
  68          return '';
  69      }
  70  
  71      public function manual_comment_fields(question_attempt $qa, question_display_options $options) {
  72          $inputname = $qa->get_behaviour_field_name('comment');
  73          $id = $inputname . '_id';
  74          list($commenttext, $commentformat) = $qa->get_current_manual_comment();
  75  
  76          $editor = editors_get_preferred_editor($commentformat);
  77          $strformats = format_text_menu();
  78          $formats = $editor->get_supported_formats();
  79          foreach ($formats as $fid) {
  80              $formats[$fid] = $strformats[$fid];
  81          }
  82  
  83          $commenttext = format_text($commenttext, $commentformat, array('para' => false));
  84  
  85          $editor->set_text($commenttext);
  86          $editor->use_editor($id, array('context' => $options->context));
  87  
  88          $commenteditor = html_writer::tag('div', html_writer::tag('textarea', s($commenttext),
  89                  array('id' => $id, 'name' => $inputname, 'rows' => 10, 'cols' => 60)));
  90  
  91          $editorformat = '';
  92          if (count($formats) == 1) {
  93              reset($formats);
  94              $editorformat .= html_writer::empty_tag('input', array('type' => 'hidden',
  95                      'name' => $inputname . 'format', 'value' => key($formats)));
  96          } else {
  97              $editorformat = html_writer::start_tag('div', array('class' => 'fitem'));
  98              $editorformat .= html_writer::start_tag('div', array('class' => 'fitemtitle'));
  99              $editorformat .= html_writer::tag('label', get_string('format'), array('for'=>'menu'.$inputname.'format'));
 100              $editorformat .= html_writer::end_tag('div');
 101              $editorformat .= html_writer::start_tag('div', array('class' => 'felement fhtmleditor'));
 102              $editorformat .= html_writer::select($formats, $inputname.'format', $commentformat, '');
 103              $editorformat .= html_writer::end_tag('div');
 104              $editorformat .= html_writer::end_tag('div');
 105          }
 106  
 107          $comment = html_writer::tag('div', html_writer::tag('div',
 108                  html_writer::tag('label', get_string('comment', 'question'),
 109                  array('for' => $id)), array('class' => 'fitemtitle')) .
 110                  html_writer::tag('div', $commenteditor, array('class' => 'felement fhtmleditor')),
 111                  array('class' => 'fitem'));
 112          $comment .= $editorformat;
 113  
 114          $mark = '';
 115          if ($qa->get_max_mark()) {
 116              $currentmark = $qa->get_current_manual_mark();
 117              $maxmark = $qa->get_max_mark();
 118  
 119              $fieldsize = strlen($qa->format_max_mark($options->markdp)) - 1;
 120              $markfield = $qa->get_behaviour_field_name('mark');
 121  
 122              $attributes = array(
 123                  'type' => 'text',
 124                  'size' => $fieldsize,
 125                  'name' => $markfield,
 126                  'id'=> $markfield
 127              );
 128              if (!is_null($currentmark)) {
 129                  $attributes['value'] = $currentmark;
 130              }
 131              $a = new stdClass();
 132              $a->max = $qa->format_max_mark($options->markdp);
 133              $a->mark = html_writer::empty_tag('input', $attributes);
 134  
 135              $markrange = html_writer::empty_tag('input', array(
 136                  'type' => 'hidden',
 137                  'name' => $qa->get_behaviour_field_name('maxmark'),
 138                  'value' => $maxmark,
 139              )) . html_writer::empty_tag('input', array(
 140                  'type' => 'hidden',
 141                  'name' => $qa->get_control_field_name('minfraction'),
 142                  'value' => $qa->get_min_fraction(),
 143              )) . html_writer::empty_tag('input', array(
 144                  'type' => 'hidden',
 145                  'name' => $qa->get_control_field_name('maxfraction'),
 146                  'value' => $qa->get_max_fraction(),
 147              ));
 148  
 149              $error = $qa->validate_manual_mark($currentmark);
 150              $errorclass = '';
 151              if ($error !== '') {
 152                  $erroclass = ' error';
 153                  $error = html_writer::tag('span', $error,
 154                          array('class' => 'error')) . html_writer::empty_tag('br');
 155              }
 156  
 157              $mark = html_writer::tag('div', html_writer::tag('div',
 158                          html_writer::tag('label', get_string('mark', 'question'),
 159                          array('for' => $markfield)),
 160                      array('class' => 'fitemtitle')) .
 161                      html_writer::tag('div', $error . get_string('xoutofmax', 'question', $a) .
 162                          $markrange, array('class' => 'felement ftext' . $errorclass)
 163                      ), array('class' => 'fitem'));
 164          }
 165  
 166          return html_writer::tag('fieldset', html_writer::tag('div', $comment . $mark,
 167                  array('class' => 'fcontainer clearfix')), array('class' => 'hidden'));
 168      }
 169  
 170      public function manual_comment_view(question_attempt $qa, question_display_options $options) {
 171          $output = '';
 172          if ($qa->has_manual_comment()) {
 173              $output .= get_string('commentx', 'question', $qa->get_behaviour()->format_comment());
 174          }
 175          if ($options->manualcommentlink) {
 176              $url = new moodle_url($options->manualcommentlink, array('slot' => $qa->get_slot()));
 177              $link = $this->output->action_link($url, get_string('commentormark', 'question'),
 178                      new popup_action('click', $url, 'commentquestion',
 179                      array('width' => 600, 'height' => 800)));
 180              $output .= html_writer::tag('div', $link, array('class' => 'commentlink'));
 181          }
 182          return $output;
 183      }
 184  
 185      /**
 186       * Display the manual comment, and a link to edit it, if appropriate.
 187       *
 188       * @param question_attempt $qa a question attempt.
 189       * @param question_display_options $options controls what should and should not be displayed.
 190       * @return string HTML fragment.
 191       */
 192      public function manual_comment(question_attempt $qa, question_display_options $options) {
 193          if ($options->manualcomment == question_display_options::EDITABLE) {
 194              return $this->manual_comment_fields($qa, $options);
 195  
 196          } else if ($options->manualcomment == question_display_options::VISIBLE) {
 197              return $this->manual_comment_view($qa, $options);
 198  
 199          } else {
 200              return '';
 201          }
 202      }
 203  
 204      /**
 205       * Several behaviours need a submit button, so put the common code here.
 206       * The button is disabled if the question is displayed read-only.
 207       * @param question_display_options $options controls what should and should not be displayed.
 208       * @return string HTML fragment.
 209       */
 210      protected function submit_button(question_attempt $qa, question_display_options $options) {
 211          if (!$qa->get_state()->is_active()) {
 212              return '';
 213          }
 214          $attributes = array(
 215              'type' => 'submit',
 216              'id' => $qa->get_behaviour_field_name('submit'),
 217              'name' => $qa->get_behaviour_field_name('submit'),
 218              'value' => get_string('check', 'question'),
 219              'class' => 'submit btn',
 220          );
 221          if ($options->readonly) {
 222              $attributes['disabled'] = 'disabled';
 223          }
 224          $output = html_writer::empty_tag('input', $attributes);
 225          if (!$options->readonly) {
 226              $this->page->requires->js_init_call('M.core_question_engine.init_submit_button',
 227                      array($attributes['id'], $qa->get_slot()));
 228          }
 229          return $output;
 230      }
 231  
 232      /**
 233       * Return any HTML that needs to be included in the page's <head> when
 234       * questions using this model are used.
 235       * @param $qa the question attempt that will be displayed on the page.
 236       * @return string HTML fragment.
 237       */
 238      public function head_code(question_attempt $qa) {
 239          return '';
 240      }
 241  
 242      /**
 243       * Generate the display of the marks for this question.
 244       * @param question_attempt $qa the question attempt to display.
 245       * @param core_question_renderer $qoutput the renderer for standard parts of questions.
 246       * @param question_display_options $options controls what should and should not be displayed.
 247       * @return HTML fragment.
 248       */
 249      public function mark_summary(question_attempt $qa, core_question_renderer $qoutput,
 250              question_display_options $options) {
 251          return $qoutput->standard_mark_summary($qa, $this, $options);
 252      }
 253  
 254      /**
 255       * Generate the display of the available marks for this question.
 256       * @param question_attempt $qa the question attempt to display.
 257       * @param core_question_renderer $qoutput the renderer for standard parts of questions.
 258       * @param question_display_options $options controls what should and should not be displayed.
 259       * @return HTML fragment.
 260       */
 261      public function marked_out_of_max(question_attempt $qa, core_question_renderer $qoutput,
 262              question_display_options $options) {
 263          return $qoutput->standard_marked_out_of_max($qa, $options);
 264      }
 265  
 266      /**
 267       * Generate the display of the marks for this question out of the available marks.
 268       * @param question_attempt $qa the question attempt to display.
 269       * @param core_question_renderer $qoutput the renderer for standard parts of questions.
 270       * @param question_display_options $options controls what should and should not be displayed.
 271       * @return HTML fragment.
 272       */
 273      public function mark_out_of_max(question_attempt $qa, core_question_renderer $qoutput,
 274              question_display_options $options) {
 275          return $qoutput->standard_mark_out_of_max($qa, $options);
 276      }
 277  }


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