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 401 and 402] [Versions 401 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   * Class for exporting h5p activity data.
  19   *
  20   * @package    mod_h5pactivity
  21   * @since      Moodle 3.9
  22   * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace mod_h5pactivity\external;
  26  
  27  use core\external\exporter;
  28  use renderer_base;
  29  use external_util;
  30  use external_files;
  31  use core_h5p\factory;
  32  use core_h5p\api;
  33  
  34  /**
  35   * Class for exporting h5p activity data.
  36   *
  37   * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class h5pactivity_summary_exporter extends exporter {
  41  
  42      /**
  43       * Properties definition.
  44       *
  45       * @return array
  46       */
  47      protected static function define_properties() {
  48  
  49          return [
  50              'id' => [
  51                  'type' => PARAM_INT,
  52                  'description' => 'The primary key of the record.',
  53              ],
  54              'course' => [
  55                  'type' => PARAM_INT,
  56                  'description' => 'Course id this h5p activity is part of.',
  57              ],
  58              'name' => [
  59                  'type' => PARAM_TEXT,
  60                  'description' => 'The name of the activity module instance.',
  61              ],
  62              'timecreated' => [
  63                  'type' => PARAM_INT,
  64                  'description' => 'Timestamp of when the instance was added to the course.',
  65                  'optional' => true,
  66              ],
  67              'timemodified' => [
  68                  'type' => PARAM_INT,
  69                  'description' => 'Timestamp of when the instance was last modified.',
  70                  'optional' => true,
  71              ],
  72              'intro' => [
  73                  'default' => '',
  74                  'type' => PARAM_RAW,
  75                  'description' => 'H5P activity description.',
  76                  'null' => NULL_ALLOWED,
  77              ],
  78              'introformat' => [
  79                  'choices' => [FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN],
  80                  'type' => PARAM_INT,
  81                  'default' => FORMAT_MOODLE,
  82                  'description' => 'The format of the intro field.',
  83              ],
  84              'grade' => [
  85                  'type' => PARAM_INT,
  86                  'default' => 0,
  87                  'description' => 'The maximum grade for submission.',
  88                  'optional' => true,
  89              ],
  90              'displayoptions' => [
  91                  'type' => PARAM_INT,
  92                  'default' => 0,
  93                  'description' => 'H5P Button display options.',
  94              ],
  95              'enabletracking' => [
  96                  'type' => PARAM_INT,
  97                  'default' => 1,
  98                  'description' => 'Enable xAPI tracking.',
  99              ],
 100              'grademethod' => [
 101                  'type' => PARAM_INT,
 102                  'default' => 1,
 103                  'description' => 'Which H5P attempt is used for grading.',
 104              ],
 105              'contenthash' => [
 106                  'type' => PARAM_ALPHANUM,
 107                  'description' => 'Sha1 hash of file content.',
 108                  'optional' => true,
 109              ],
 110          ];
 111      }
 112  
 113      /**
 114       * Related objects definition.
 115       *
 116       * @return array
 117       */
 118      protected static function define_related() {
 119          return [
 120              'context' => 'context',
 121              'factory' => 'core_h5p\\factory'
 122          ];
 123      }
 124  
 125      /**
 126       * Other properties definition.
 127       *
 128       * @return array
 129       */
 130      protected static function define_other_properties() {
 131          return [
 132              'coursemodule' => [
 133                  'type' => PARAM_INT
 134              ],
 135              'context' => [
 136                  'type' => PARAM_INT
 137              ],
 138              'introfiles' => [
 139                  'type' => external_files::get_properties_for_exporter(),
 140                  'multiple' => true
 141              ],
 142              'package' => [
 143                  'type' => external_files::get_properties_for_exporter(),
 144                  'multiple' => true
 145              ],
 146              'deployedfile' => [
 147                  'optional' => true,
 148                  'description' => 'H5P file deployed.',
 149                  'type' => [
 150                      'filename' => array(
 151                          'type' => PARAM_FILE,
 152                          'description' => 'File name.',
 153                          'optional' => true,
 154                          'null' => NULL_NOT_ALLOWED,
 155                      ),
 156                      'filepath' => array(
 157                          'type' => PARAM_PATH,
 158                          'description' => 'File path.',
 159                          'optional' => true,
 160                          'null' => NULL_NOT_ALLOWED,
 161                      ),
 162                      'filesize' => array(
 163                          'type' => PARAM_INT,
 164                          'description' => 'File size.',
 165                          'optional' => true,
 166                          'null' => NULL_NOT_ALLOWED,
 167                      ),
 168                      'fileurl' => array(
 169                          'type' => PARAM_URL,
 170                          'description' => 'Downloadable file url.',
 171                          'optional' => true,
 172                          'null' => NULL_NOT_ALLOWED,
 173                      ),
 174                      'timemodified' => array(
 175                          'type' => PARAM_INT,
 176                          'description' => 'Time modified.',
 177                          'optional' => true,
 178                          'null' => NULL_NOT_ALLOWED,
 179                      ),
 180                      'mimetype' => array(
 181                          'type' => PARAM_RAW,
 182                          'description' => 'File mime type.',
 183                          'optional' => true,
 184                          'null' => NULL_NOT_ALLOWED,
 185                      )
 186                  ]
 187              ],
 188          ];
 189      }
 190  
 191      /**
 192       * Assign values to the defined other properties.
 193       *
 194       * @param renderer_base $output The output renderer object.
 195       * @return array
 196       */
 197      protected function get_other_values(renderer_base $output) {
 198          $context = $this->related['context'];
 199          $factory = $this->related['factory'];
 200  
 201          $values = [
 202              'coursemodule' => $context->instanceid,
 203              'context' => $context->id,
 204          ];
 205  
 206          $values['introfiles'] = external_util::get_area_files($context->id, 'mod_h5pactivity', 'intro', false, false);
 207  
 208          $values['package'] = external_util::get_area_files($context->id, 'mod_h5pactivity', 'package', false, true);
 209  
 210          // Only if this H5P activity has been deployed, return the exported file.
 211          $fileh5p = api::get_export_info_from_context_id($context->id, $factory, 'mod_h5pactivity', 'package');
 212          if ($fileh5p) {
 213              $values['deployedfile'] = $fileh5p;
 214          }
 215  
 216          return $values;
 217      }
 218  
 219      /**
 220       * Get the formatting parameters for the intro.
 221       *
 222       * @return array with the formatting parameters
 223       */
 224      protected function get_format_parameters_for_intro() {
 225          return [
 226              'component' => 'mod_h5pactivity',
 227              'filearea' => 'intro',
 228              'options' => ['noclean' => true],
 229          ];
 230      }
 231  
 232      /**
 233       * Get the formatting parameters for the package.
 234       *
 235       * @return array with the formatting parameters
 236       */
 237      protected function get_format_parameters_for_package() {
 238          return [
 239              'component' => 'mod_h5pactivity',
 240              'filearea' => 'package',
 241              'itemid' => 0,
 242              'options' => ['noclean' => true],
 243          ];
 244      }
 245  }