[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/ddmarker/ -> 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 markers question renderer class.
  19   *
  20   * @package   qtype_ddmarker
  21   * @copyright 2012 The Open University
  22   * @author    Jamie Pratt <me@jamiep.org>
  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/rendererbase.php');
  30  require_once($CFG->dirroot . '/question/type/ddimageortext/rendererbase.php');
  31  
  32  
  33  /**
  34   * Generates the output for drag-and-drop markers questions.
  35   *
  36   * @copyright  2010 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_ddmarker_renderer extends qtype_ddtoimage_renderer_base {
  40      public function formulation_and_controls(question_attempt $qa,
  41              question_display_options $options) {
  42          global $PAGE, $OUTPUT;
  43  
  44          $question = $qa->get_question();
  45          $response = $qa->get_last_qt_data();
  46  
  47          $questiontext = $question->format_questiontext($qa);
  48  
  49          $output = html_writer::tag('div', $questiontext, array('class' => 'qtext'));
  50  
  51          $bgimage = self::get_url_for_image($qa, 'bgimage');
  52  
  53          $img = html_writer::empty_tag('img', array(
  54                  'src' => $bgimage, 'class' => 'dropbackground',
  55                  'alt' => get_string('dropbackground', 'qtype_ddmarker')));
  56  
  57          $droparea = html_writer::tag('div', $img, array('class' => 'droparea'));
  58  
  59          $draghomes = '';
  60          $orderedgroup = $question->get_ordered_choices(1);
  61          $componentname = $question->qtype->plugin_name();
  62          $hiddenfields = '';
  63          foreach ($orderedgroup as $choiceno => $drag) {
  64              $classes = array('draghome',
  65                               "choice{$choiceno}");
  66              if ($drag->infinite) {
  67                  $classes[] = 'infinite';
  68              } else {
  69                  $classes[] = 'dragno'.$drag->noofdrags;
  70              }
  71              $targeticonhtml =
  72                  $OUTPUT->pix_icon('crosshairs', '', $componentname, array('class' => 'target'));
  73  
  74              $markertextattrs = array('class' => 'markertext');
  75              $markertext = html_writer::tag('span', $drag->text, $markertextattrs);
  76              $draghomesattrs = array('class' => join(' ', $classes));
  77              $draghomes .= html_writer::tag('span', $targeticonhtml . $markertext, $draghomesattrs);
  78              $hiddenfields .= $this->hidden_field_choice($qa, $choiceno, $drag->infinite, $drag->noofdrags);
  79          }
  80  
  81          $dragitemsclass = 'dragitems';
  82          if ($options->readonly) {
  83              $dragitemsclass .= ' readonly';
  84          }
  85  
  86          $dragitems = html_writer::tag('div', $draghomes, array('class' => $dragitemsclass));
  87          $dropzones = html_writer::tag('div', '', array('class' => 'dropzones'));
  88          $texts = html_writer::tag('div', '', array('class' => 'markertexts'));
  89          $output .= html_writer::tag('div',
  90                                      $droparea.$dragitems.$dropzones . $texts,
  91                                      array('class' => 'ddarea'));
  92  
  93          if ($question->showmisplaced && $qa->get_state()->is_finished()) {
  94              $visibledropzones = $question->get_drop_zones_without_hit($response);
  95          } else {
  96              $visibledropzones = array();
  97          }
  98  
  99          $topnode = 'div#q'.$qa->get_slot();
 100          $params = array('dropzones' => $visibledropzones,
 101                          'topnode' => $topnode,
 102                          'readonly' => $options->readonly);
 103  
 104          $PAGE->requires->yui_module('moodle-qtype_ddmarker-dd',
 105                                          'M.qtype_ddmarker.init_question',
 106                                          array($params));
 107  
 108          if ($qa->get_state() == question_state::$invalid) {
 109              $output .= html_writer::nonempty_tag('div',
 110                                          $question->get_validation_error($qa->get_last_qt_data()),
 111                                          array('class' => 'validationerror'));
 112          }
 113  
 114          if ($question->showmisplaced && $qa->get_state()->is_finished()) {
 115              $wrongparts = $question->get_drop_zones_without_hit($response);
 116              if (count($wrongparts) !== 0) {
 117                  $wrongpartsstringspans = array();
 118                  foreach ($wrongparts as $wrongpart) {
 119                      $wrongpartsstringspans[] = html_writer::nonempty_tag('span',
 120                                      $wrongpart->markertext, array('class' => 'wrongpart'));
 121                  }
 122                  $wrongpartsstring = join(', ', $wrongpartsstringspans);
 123                  $output .= html_writer::nonempty_tag('span',
 124                                                      get_string('followingarewrongandhighlighted',
 125                                                                  'qtype_ddmarker',
 126                                                                  $wrongpartsstring),
 127                                                      array('class' => 'wrongparts'));
 128              }
 129          }
 130  
 131          $output .= html_writer::tag('div', $hiddenfields, array('class' => 'ddform'));
 132          return $output;
 133      }
 134      protected function hidden_field_choice(question_attempt $qa, $choiceno, $infinite, $noofdrags, $value = null) {
 135          $varname = 'c'.$choiceno;
 136          $classes = array('choices', 'choice'.$choiceno, 'noofdrags'.$noofdrags);
 137          if ($infinite) {
 138              $classes[] = 'infinite';
 139          }
 140          list(, $html) = $this->hidden_field_for_qt_var($qa, $varname, null, $classes);
 141          return $html;
 142      }
 143  
 144      protected function hint(question_attempt $qa, question_hint $hint) {
 145          $output = '';
 146          $question = $qa->get_question();
 147          $response = $qa->get_last_qt_data();
 148          if ($hint->statewhichincorrect) {
 149              $wrongdrags = $question->get_wrong_drags($response);
 150              $wrongparts = array();
 151              foreach ($wrongdrags as $wrongdrag) {
 152                  $wrongparts[] = html_writer::nonempty_tag('span',
 153                                                  $wrongdrag, array('class' => 'wrongpart'));
 154              }
 155              $output .= html_writer::nonempty_tag('div',
 156                      get_string('followingarewrong', 'qtype_ddmarker', join(', ', $wrongparts)),
 157                      array('class' => 'wrongparts'));
 158          }
 159          $output .= parent::hint($qa, $hint);
 160          return $output;
 161      }
 162  }


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