Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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\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) && strpos($this->result->correctpattern, '{case_matters=false}') !== null) {
  58                  $casesensitive = false;
  59          }
  60  
  61          $values = [];
  62          // Add all possibilities from $additionals.
  63          if (isset($extensions['https://h5p.org/x-api/alternatives'])) {
  64              foreach ($extensions['https://h5p.org/x-api/alternatives'] as $key => $value) {
  65                  if (!is_array($value)) {
  66                      $value = [$value];
  67                  }
  68                  $values[$key] = ($casesensitive) ? $value : array_change_key_case($value);
  69              }
  70          }
  71          // Add possibilities from correctpattern.
  72          foreach ($correctpatterns as $correctpattern) {
  73              foreach ($correctpattern as $key => $pattern) {
  74                  // The xAPI admits more params a part form values.
  75                  // For now this extra information is not used in reporting
  76                  // but it is posible future H5P types need them.
  77                  $value = preg_replace('/\{.+=.*\}/', '', $pattern);
  78                  $value = ($casesensitive) ? $value : strtolower($value);
  79                  if (!isset($values[$key])) {
  80                      $values[$key] = [];
  81                  }
  82                  if (!in_array($value, $values[$key])) {
  83                      array_unshift($values[$key], $value);
  84                  }
  85              }
  86          }
  87  
  88          // Generate options.
  89          $options = [];
  90          $num = 1;
  91          foreach ($values as $key => $value) {
  92              $option = (object)[
  93                  'id' => $key,
  94                  'description' => get_string('result_fill-in_gap', 'mod_h5pactivity', $num),
  95              ];
  96  
  97              $gapresponse = $this->response[$key] ?? null;
  98              $gapresponse = ($casesensitive) ? $gapresponse : strtolower($gapresponse);
  99              if ($gapresponse !== null && in_array($gapresponse, $value)) {
 100                  $state = parent::CORRECT;
 101              } else {
 102                  $state = parent::INCORRECT;
 103              }
 104              $option->useranswer = $this->get_answer($state, $gapresponse);
 105  
 106              $option->correctanswer = $this->get_answer(parent::TEXT, implode(' / ', $value));
 107  
 108              $options[] = $option;
 109              $num++;
 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  }