Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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  use renderer_base;
  31  
  32  /**
  33   * Class to display H5P matching result.
  34   *
  35   * @copyright 2020 Ferran Recio
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class matching extends result {
  39  
  40      /**
  41       * Return the options data structure.
  42       *
  43       * @return array of options
  44       */
  45      protected function export_options(): ?array {
  46          // Suppose H5P choices have only list of valid answers.
  47          $correctpattern = reset($this->correctpattern);
  48  
  49          $additionals = $this->additionals;
  50  
  51          // Get sources (options).
  52          if (isset($additionals->source)) {
  53              $options = $this->get_descriptions($additionals->source);
  54          } else {
  55              $options = [];
  56          }
  57  
  58          // Get targets.
  59          if (isset($additionals->target)) {
  60              $targets = $this->get_descriptions($additionals->target);
  61          } else {
  62              $targets = [];
  63          }
  64  
  65          // Correct answers.
  66          foreach ($correctpattern as $pattern) {
  67              if (!is_array($pattern) || count($pattern) != 2) {
  68                  continue;
  69              }
  70              // One pattern must be from options and the other from targets.
  71              if (isset($options[$pattern[0]]) && isset($targets[$pattern[1]])) {
  72                  $option = $options[$pattern[0]];
  73                  $target = $targets[$pattern[1]];
  74              } else if (isset($targets[$pattern[0]]) && isset($options[$pattern[1]])) {
  75                  $option = $options[$pattern[1]];
  76                  $target = $targets[$pattern[0]];
  77              } else {
  78                  $option = null;
  79              }
  80              if ($option) {
  81                  $option->correctanswer = $this->get_answer(parent::TEXT, $target->description);
  82                  $option->correctanswerid = $target->id;
  83              }
  84          }
  85  
  86          // User responses.
  87          foreach ($this->response as $response) {
  88              if (!is_array($response) || count($response) != 2) {
  89                  continue;
  90              }
  91              // One repsonse must be from options and the other from targets.
  92              if (isset($options[$response[0]]) && isset($targets[$response[1]])) {
  93                  $option = $options[$response[0]];
  94                  $target = $targets[$response[1]];
  95                  $answer = $response[1];
  96              } else if (isset($targets[$response[0]]) && isset($options[$response[1]])) {
  97                  $option = $options[$response[1]];
  98                  $target = $targets[$response[0]];
  99                  $answer = $response[0];
 100              } else {
 101                  $option = null;
 102              }
 103              if ($option) {
 104                  if (isset($option->correctanswerid) && $option->correctanswerid == $answer) {
 105                      $state = parent::CORRECT;
 106                  } else {
 107                      $state = parent::INCORRECT;
 108                  }
 109                  $option->useranswer = $this->get_answer($state, $target->description);
 110              }
 111          }
 112          return $options;
 113      }
 114  
 115      /**
 116       * Return a label for result user options/choices
 117       *
 118       * Specific result types can override this method to customize
 119       * the result options table header.
 120       *
 121       * @return string to use in options table
 122       */
 123      protected function get_optionslabel(): string {
 124          return get_string('result_matching', 'mod_h5pactivity');
 125      }
 126  }