[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/ddwtos/ -> 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   * Drag-and-drop words into sentences question renderer class.
  19   *
  20   * @package   qtype_ddwtos
  21   * @copyright 2010 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->dirroot . '/question/type/gapselect/rendererbase.php');
  29  
  30  
  31  /**
  32   * Generates the output for drag-and-drop words into sentences questions.
  33   *
  34   * @copyright  2010 The Open University
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class qtype_ddwtos_renderer extends qtype_elements_embedded_in_question_text_renderer {
  38  
  39      protected function qtext_classname() {
  40          return 'qtext ddwtos_questionid_for_javascript';
  41      }
  42  
  43      public function formulation_and_controls(question_attempt $qa,
  44              question_display_options $options) {
  45          global $PAGE;
  46  
  47          $result = parent::formulation_and_controls($qa, $options);
  48  
  49          $inputids = array();
  50          $question = $qa->get_question();
  51          foreach ($question->places as $placeno => $place) {
  52              $inputids[$placeno] = $this->box_id($qa, $question->field($placeno));
  53          }
  54  
  55          $params = array(
  56              'inputids' => $inputids,
  57              'topnode' => 'div.que.ddwtos#q' . $qa->get_slot(),
  58              'readonly' => $options->readonly
  59          );
  60  
  61          $PAGE->requires->yui_module('moodle-qtype_ddwtos-dd',
  62                  'M.qtype_ddwtos.init_question', array($params));
  63  
  64          return $result;
  65      }
  66  
  67      protected function post_qtext_elements(question_attempt $qa,
  68              question_display_options $options) {
  69          $result = '';
  70          $question = $qa->get_question();
  71  
  72          $dragboxs = '';
  73          foreach ($question->choices as $group => $choices) {
  74              $dragboxs .= $this->drag_boxes($qa, $group,
  75                      $question->get_ordered_choices($group), $options);
  76          }
  77  
  78          $classes = array('answercontainer');
  79          if (!$options->readonly) {
  80              $classes[] = 'notreadonly';
  81          } else {
  82              $classes[] = 'readonly';
  83          }
  84          $result .= html_writer::tag('div', $dragboxs, array('class' => implode(' ', $classes)));
  85  
  86          $classes = array('drags');
  87          if (!$options->readonly) {
  88              $classes[] = 'notreadonly';
  89          } else {
  90              $classes[] = 'readonly';
  91          }
  92          $result .= html_writer::tag('div', '', array('class' => implode(' ', $classes)));
  93  
  94          // We abuse the clear_wrong method to output the hidden form fields we
  95          // want irrespective of whether we are actually clearing the wrong
  96          // bits of the response.
  97          if (!$options->clearwrong) {
  98              $result .= $this->clear_wrong($qa, false);
  99          }
 100          return $result;
 101      }
 102  
 103      protected function embedded_element(question_attempt $qa, $place,
 104              question_display_options $options) {
 105          $question = $qa->get_question();
 106          $group = $question->places[$place];
 107          $boxcontents = '&#160;';
 108  
 109          $value = $qa->get_last_qt_var($question->field($place));
 110  
 111          $attributes = array(
 112              'class' => 'place' . $place . ' drop group' . $group
 113          );
 114  
 115          if ($options->readonly) {
 116              $attributes['class'] .= ' readonly';
 117          } else {
 118              $attributes['tabindex'] = '0';
 119          }
 120  
 121          $feedbackimage = '';
 122          if ($options->correctness) {
 123              $response = $qa->get_last_qt_data();
 124              $fieldname = $question->field($place);
 125              if (array_key_exists($fieldname, $response)) {
 126                  $fraction = (int) ($response[$fieldname] ==
 127                          $question->get_right_choice_for($place));
 128                  $feedbackimage = $this->feedback_image($fraction);
 129              }
 130          }
 131  
 132          return html_writer::tag('span', $boxcontents, $attributes) . ' ' . $feedbackimage;
 133      }
 134  
 135      protected function drag_boxes($qa, $group, $choices, question_display_options $options) {
 136          $boxes = '';
 137          foreach ($choices as $key => $choice) {
 138              // Bug 8632: long text entry causes bug in drag and drop field in IE.
 139              $content = str_replace('-', '&#x2011;', $choice->text);
 140              $content = str_replace(' ', '&#160;', $content);
 141  
 142              $infinite = '';
 143              if ($choice->infinite) {
 144                  $infinite = ' infinite';
 145              }
 146  
 147              $boxes .= html_writer::tag('span', $content, array(
 148                      'class' => 'draghome choice' . $key . ' group' .
 149                              $choice->draggroup . $infinite)) . ' ';
 150          }
 151  
 152          return html_writer::nonempty_tag('div', $boxes,
 153                  array('class' => 'draggrouphomes' . $choice->draggroup));
 154      }
 155  
 156      /**
 157       * Actually, this question type abuses this method to always ouptut the
 158       * hidden fields it needs.
 159       */
 160      public function clear_wrong(question_attempt $qa, $reallyclear = true) {
 161          $question = $qa->get_question();
 162          $response = $qa->get_last_qt_data();
 163  
 164          if (!empty($response) && $reallyclear) {
 165              $cleanresponse = $question->clear_wrong_from_response($response);
 166          } else {
 167              $cleanresponse = $response;
 168          }
 169  
 170          $output = '';
 171          foreach ($question->places as $place => $group) {
 172              $fieldname = $question->field($place);
 173              if (array_key_exists($fieldname, $response)) {
 174                  $value = $response[$fieldname];
 175              } else {
 176                  $value = '0';
 177              }
 178              if (array_key_exists($fieldname, $cleanresponse)) {
 179                  $cleanvalue = $cleanresponse[$fieldname];
 180              } else {
 181                  $cleanvalue = '0';
 182              }
 183              if ($cleanvalue != $value) {
 184                  $output .= html_writer::empty_tag('input', array(
 185                          'type' => 'hidden',
 186                          'id' => $this->box_id($qa, 'p' . $place),
 187                          'value' => s($value))) .
 188                          html_writer::empty_tag('input', array(
 189                          'type' => 'hidden',
 190                          'name' => $qa->get_qt_field_name($fieldname),
 191                          'value' => s($cleanvalue)));
 192              } else {
 193                  $output .= html_writer::empty_tag('input', array(
 194                          'type' => 'hidden',
 195                          'id' => $this->box_id($qa, 'p' . $place),
 196                          'name' => $qa->get_qt_field_name($fieldname),
 197                          'value' => s($value)));
 198              }
 199          }
 200          return $output;
 201      }
 202  
 203  }


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