Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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   * Contains class mod_h5pactivity\output\result\matching
  19   *
  20   * @package   mod_h5pactivity
  21   * @copyright 2020 Ferran Recio
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_h5pactivity\output\result;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_h5pactivity\output\result;
  30  
  31  /**
  32   * Class to display H5P matching result.
  33   *
  34   * @copyright 2020 Ferran Recio
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class matching extends result {
  38  
  39      /**
  40       * Return the options data structure.
  41       *
  42       * @return array|null of options
  43       */
  44      protected function export_options(): ?array {
  45          // Suppose H5P choices have only list of valid answers.
  46          $correctpattern = reset($this->correctpattern);
  47  
  48          $additionals = $this->additionals;
  49  
  50          // Get sources (options).
  51          if (isset($additionals->source)) {
  52              $sources = $this->get_descriptions($additionals->source);
  53          } else {
  54              $sources = [];
  55          }
  56  
  57          // Get targets.
  58          if (isset($additionals->target)) {
  59              $targets = $this->get_descriptions($additionals->target);
  60          } else {
  61              $targets = [];
  62          }
  63          // Create original options array.
  64          $options = array_map(function ($source) {
  65              $cloneddraggable = clone $source;
  66              $cloneddraggable->correctanswers = [];
  67              return $cloneddraggable;
  68          }, $sources);
  69  
  70          // Fill options with correct answers flags if they exist.
  71          foreach ($correctpattern as $pattern) {
  72              if (!is_array($pattern) || count($pattern) != 2) {
  73                  continue;
  74              }
  75              // We assume here that the activity is following the convention sets in:
  76              // https://github.com/h5p/h5p-php-report/blob/master/type-processors/matching-processor.class.php
  77              // i.e. source is index 1 and dropzone is index 0.
  78              if (isset($sources[$pattern[1]]) && isset($targets[$pattern[0]])) {
  79                  $target = $targets[$pattern[0]];
  80                  $source = $sources[$pattern[1]];
  81                  $currentoption = $options[$source->id];
  82                  $currentoption->correctanswers[$target->id] = $target->description;
  83              }
  84          }
  85  
  86          // Fill in user responses.
  87          foreach ($this->response as $response) {
  88              if (!is_array($response) || count($response) != 2) {
  89                  continue;
  90              }
  91              if (isset($sources[$response[1]]) && isset($targets[$response[0]])) {
  92                  $source = $sources[$response[1]];
  93                  $target = $targets[$response[0]];
  94                  $answer = $response[0];
  95                  $option = $options[$source->id] ?? null;
  96                  if ($option) {
  97                      if (isset($option->correctanswers[$answer])) {
  98                          $state = parent::CORRECT;
  99                      } else {
 100                          $state = parent::INCORRECT;
 101                      }
 102                      $option->useranswer = $this->get_answer($state, $target->description);
 103                  }
 104              }
 105          }
 106  
 107          // Fill in unchecked options.
 108          foreach ($options as $option) {
 109              if (!isset($option->useranswer)) {
 110                  if (!empty($option->correctanswers)) {
 111                      $option->useranswer = $this->get_answer(parent::INCORRECT,
 112                          get_string('answer_noanswer', 'mod_h5pactivity'));
 113                  } else {
 114                      $option->useranswer = $this->get_answer(parent::CORRECT,
 115                          get_string('answer_noanswer', 'mod_h5pactivity'));
 116                  }
 117              }
 118          }
 119  
 120          // Now flattern correct answers.
 121          foreach ($options as $option) {
 122              $option->correctanswer = $this->get_answer( parent::TEXT, join(', ', $option->correctanswers));
 123              unset($option->correctanswers);
 124          }
 125          return array_values($options);
 126      }
 127  
 128      /**
 129       * Return a label for result user options/choices
 130       *
 131       * Specific result types can override this method to customize
 132       * the result options table header.
 133       *
 134       * @return string to use in options table
 135       */
 136      protected function get_optionslabel(): string {
 137          return get_string('result_matching', 'mod_h5pactivity');
 138      }
 139  }