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\fillin
  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  use stdClass;
  32  
  33  /**
  34   * Class to display H5P fill-in result.
  35   *
  36   * @copyright 2020 Ferran Recio
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class fillin extends result {
  40  
  41      /**
  42       * Return the options data structure.
  43       *
  44       * @return array of options
  45       */
  46      protected function export_options(): ?array {
  47  
  48          $correctpatterns = $this->correctpattern;
  49  
  50          $additionals = $this->additionals;
  51  
  52          $extensions = (array) $additionals->extensions ?? [];
  53  
  54          // There are two way in which H5P could force case sensitivity, with extensions
  55          // or using options in the correctpatterns. By default it is case sensible.
  56          $casesensitive = $extensions['https://h5p.org/x-api/case-sensitivity'] ?? true;
  57          if ((!empty($this->result->correctpattern)
  58              && strpos($this->result->correctpattern, '{case_matters=false}') !== false)) {
  59              $casesensitive = false;
  60          }
  61  
  62          $values = [];
  63          // Add all possibilities from $additionals.
  64          if (isset($extensions['https://h5p.org/x-api/alternatives'])) {
  65              foreach ($extensions['https://h5p.org/x-api/alternatives'] as $key => $value) {
  66                  if (!is_array($value)) {
  67                      $value = [$value];
  68                  }
  69                  $values[$key] = ($casesensitive) ? $value : array_change_key_case($value);
  70              }
  71          }
  72          // Add possibilities from correctpattern.
  73          foreach ($correctpatterns as $correctpattern) {
  74              foreach ($correctpattern as $key => $pattern) {
  75                  // The xAPI admits more params a part form values.
  76                  // For now this extra information is not used in reporting
  77                  // but it is posible future H5P types need them.
  78                  $value = preg_replace('/\{.+=.*\}/', '', $pattern);
  79                  $value = ($casesensitive) ? $value : strtolower($value);
  80                  if (!isset($values[$key])) {
  81                      $values[$key] = [];
  82                  }
  83                  if (!in_array($value, $values[$key])) {
  84                      array_unshift($values[$key], $value);
  85                  }
  86              }
  87          }
  88  
  89          // Generate options.
  90          $options = [];
  91          $num = 1;
  92          foreach ($values as $key => $value) {
  93              $option = (object)[
  94                  'id' => $key,
  95                  'description' => get_string('result_fill-in_gap', 'mod_h5pactivity', $num),
  96              ];
  97  
  98              $gapresponse = $this->response[$key] ?? null;
  99              $gapresponse = ($casesensitive) ? $gapresponse : strtolower($gapresponse);
 100              if ($gapresponse !== null && in_array($gapresponse, $value)) {
 101                  $state = parent::CORRECT;
 102              } else {
 103                  $state = parent::INCORRECT;
 104              }
 105              $option->useranswer = $this->get_answer($state, $this->response[$key]);
 106  
 107              $option->correctanswer = $this->get_answer(parent::TEXT, implode(' / ', $value));
 108  
 109              $options[] = $option;
 110              $num++;
 111          }
 112  
 113          return $options;
 114      }
 115  
 116      /**
 117       * Return a label for result user options/choices
 118       *
 119       * Specific result types can override this method to customize
 120       * the result options table header.
 121       *
 122       * @return string to use in options table
 123       */
 124      protected function get_optionslabel(): string {
 125          return get_string('result_matching', 'mod_h5pactivity');
 126      }
 127  }