Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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 onto image question renderer class.
  19   *
  20   * @package    qtype_ddimageortext
  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  /**
  29   * Generates the output for drag-and-drop onto image questions.
  30   *
  31   * @copyright  2010 The Open University
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class qtype_ddtoimage_renderer_base extends qtype_with_combined_feedback_renderer {
  35  
  36      public function clear_wrong(question_attempt $qa) {
  37          $question = $qa->get_question();
  38          $response = $qa->get_last_qt_data();
  39  
  40          if (!empty($response)) {
  41              $cleanresponse = $question->clear_wrong_from_response($response);
  42          } else {
  43              $cleanresponse = $response;
  44          }
  45          $cleanresponsehtml = '';
  46          foreach ($cleanresponse as $fieldname => $value) {
  47              list (, $html) = $this->hidden_field_for_qt_var($qa, $fieldname, $value);
  48              $cleanresponsehtml .= $html;
  49          }
  50          return $cleanresponsehtml;
  51      }
  52  
  53      public function formulation_and_controls(question_attempt $qa,
  54              question_display_options $options) {
  55  
  56          $question = $qa->get_question();
  57          $response = $qa->get_last_qt_data();
  58  
  59          $questiontext = $question->format_questiontext($qa);
  60  
  61          $dropareaclass = 'droparea';
  62          $draghomesclass = 'draghomes';
  63          if ($options->readonly) {
  64              $dropareaclass .= ' readonly';
  65              $draghomesclass .= ' readonly';
  66          }
  67  
  68          $output = html_writer::div($questiontext, 'qtext');
  69  
  70          $output .= html_writer::start_div('ddarea');
  71          $output .= html_writer::start_div($dropareaclass);
  72          $output .= html_writer::img(self::get_url_for_image($qa, 'bgimage'), get_string('dropbackground', 'qtype_ddmarker'),
  73                  ['class' => 'dropbackground img-fluid w-100']);
  74  
  75          $output .= html_writer::div('', 'dropzones');
  76          $output .= html_writer::end_div();
  77          $output .= html_writer::start_div($draghomesclass);
  78  
  79          $dragimagehomes = '';
  80          foreach ($question->choices as $groupno => $group) {
  81              $dragimagehomesgroup = '';
  82              $orderedgroup = $question->get_ordered_choices($groupno);
  83              foreach ($orderedgroup as $choiceno => $dragimage) {
  84                  $dragimageurl = self::get_url_for_image($qa, 'dragimage', $dragimage->id);
  85                  $classes = [
  86                          'group' . $groupno,
  87                          'draghome',
  88                          'user-select-none',
  89                          'choice' . $choiceno
  90                  ];
  91                  if ($dragimage->infinite) {
  92                      $classes[] = 'infinite';
  93                  }
  94                  if ($dragimageurl === null) {
  95                      $dragimagehomesgroup .= html_writer::div($dragimage->text, join(' ', $classes), ['src' => $dragimageurl]);
  96                  } else {
  97                      $dragimagehomesgroup .= html_writer::img($dragimageurl, $dragimage->text, ['class' => join(' ', $classes)]);
  98                  }
  99              }
 100              $dragimagehomes .= html_writer::div($dragimagehomesgroup, 'dragitemgroup' . $groupno);
 101          }
 102  
 103          $output .= $dragimagehomes;
 104          $output .= html_writer::end_div();
 105  
 106          // Note, the mobile app implementation of ddimageortext relies on extracting the
 107          // blob of places data out of the rendered HTML, which makes it impossible
 108          // to clean up this structure of otherwise unnecessary stuff.
 109          $placeinfoforjsandmobileapp = [];
 110          foreach ($question->places as $placeno => $place) {
 111              $varname = $question->field($placeno);
 112              [$fieldname, $html] = $this->hidden_field_for_qt_var($qa, $varname, null,
 113                      ['placeinput', 'place' . $placeno, 'group' . $place->group]);
 114              $output .= $html;
 115              $placeinfo = (object) (array) $place;
 116              $placeinfo->fieldname = $fieldname;
 117              $placeinfoforjsandmobileapp[$placeno] = $placeinfo;
 118          }
 119  
 120          $output .= html_writer::end_div();
 121  
 122          $this->page->requires->string_for_js('blank', 'qtype_ddimageortext');
 123          $this->page->requires->js_call_amd('qtype_ddimageortext/question', 'init',
 124                  [$qa->get_outer_question_div_unique_id(), $options->readonly, $placeinfoforjsandmobileapp]);
 125  
 126          if ($qa->get_state() == question_state::$invalid) {
 127              $output .= html_writer::div($question->get_validation_error($qa->get_last_qt_data()), 'validationerror');
 128          }
 129          return $output;
 130      }
 131  
 132      /**
 133       * Returns the URL for an image
 134       *
 135       * @param object $qa Question attempt object
 136       * @param string $filearea File area descriptor
 137       * @param int $itemid Item id to get
 138       * @return string Output url, or null if not found
 139       */
 140      protected static function get_url_for_image(question_attempt $qa, $filearea, $itemid = 0) {
 141          $question = $qa->get_question();
 142          $qubaid = $qa->get_usage_id();
 143          $slot = $qa->get_slot();
 144          $fs = get_file_storage();
 145          if ($filearea == 'bgimage') {
 146              $itemid = $question->id;
 147          }
 148          $componentname = $question->qtype->plugin_name();
 149          $draftfiles = $fs->get_area_files($question->contextid, $componentname,
 150                                                                          $filearea, $itemid, 'id');
 151          if ($draftfiles) {
 152              foreach ($draftfiles as $file) {
 153                  if ($file->is_directory()) {
 154                      continue;
 155                  }
 156                  $url = moodle_url::make_pluginfile_url($question->contextid, $componentname,
 157                                              $filearea, "$qubaid/$slot/{$itemid}", '/',
 158                                              $file->get_filename());
 159                  return $url->out();
 160              }
 161          }
 162          return null;
 163      }
 164  
 165      /**
 166       * Returns a hidden field for a qt variable
 167       *
 168       * @param object $qa Question attempt object
 169       * @param string $varname The hidden var name
 170       * @param string $value The hidden value
 171       * @param array $classes Any additional css classes to apply
 172       * @return array Array with field name and the html of the tag
 173       */
 174      protected function hidden_field_for_qt_var(question_attempt $qa, $varname, $value = null,
 175                                                  $classes = null) {
 176          if ($value === null) {
 177              $value = $qa->get_last_qt_var($varname);
 178          }
 179          $fieldname = $qa->get_qt_field_name($varname);
 180          $attributes = array('type' => 'hidden',
 181                                  'id' => str_replace(':', '_', $fieldname),
 182                                  'name' => $fieldname,
 183                                  'value' => $value);
 184          if ($classes !== null) {
 185              $attributes['class'] = join(' ', $classes);
 186          }
 187          return array($fieldname, html_writer::empty_tag('input', $attributes)."\n");
 188      }
 189  
 190      public function specific_feedback(question_attempt $qa) {
 191          return $this->combined_feedback($qa);
 192      }
 193  
 194      public function correct_response(question_attempt $qa) {
 195          return '';
 196      }
 197  }