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] [Versions 39 and 310]

   1  <?php
   2  
   3  namespace Moodle;
   4  
   5  /**
   6   * Utility class for handling metadata
   7   */
   8  abstract class H5PMetadata {
   9  
  10    private static $fields = array(
  11      'title' => array(
  12        'type' => 'text',
  13        'maxLength' => 255
  14      ),
  15      'authors' => array(
  16        'type' => 'json'
  17      ),
  18      'changes' => array(
  19        'type' => 'json'
  20      ),
  21      'source' => array(
  22        'type' => 'text',
  23        'maxLength' => 255
  24      ),
  25      'license' => array(
  26        'type' => 'text',
  27        'maxLength' => 32
  28      ),
  29      'licenseVersion' => array(
  30        'type' => 'text',
  31        'maxLength' => 10
  32      ),
  33      'licenseExtras' => array(
  34        'type' => 'text',
  35        'maxLength' => 5000
  36      ),
  37      'authorComments' => array(
  38        'type' => 'text',
  39        'maxLength' => 5000
  40      ),
  41      'yearFrom' => array(
  42        'type' => 'int'
  43      ),
  44      'yearTo' => array(
  45        'type' => 'int'
  46      ),
  47      'defaultLanguage' => array(
  48        'type' => 'text',
  49        'maxLength' => 32,
  50      )
  51    );
  52  
  53    /**
  54     * JSON encode metadata
  55     *
  56     * @param object $content
  57     * @return string
  58     */
  59    public static function toJSON($content) {
  60      // Note: deliberatly creating JSON string "manually" to improve performance
  61      return
  62        '{"title":' . (isset($content->title) ? json_encode($content->title) : 'null') .
  63        ',"authors":' . (isset($content->authors) ? $content->authors : 'null') .
  64        ',"source":' . (isset($content->source) ? '"' . $content->source . '"' : 'null') .
  65        ',"license":' . (isset($content->license) ? '"' . $content->license . '"' : 'null') .
  66        ',"licenseVersion":' . (isset($content->license_version) ? '"' . $content->license_version . '"' : 'null') .
  67        ',"licenseExtras":' . (isset($content->license_extras) ? json_encode($content->license_extras) : 'null') .
  68        ',"yearFrom":' . (isset($content->year_from) ? $content->year_from : 'null') .
  69        ',"yearTo":' .  (isset($content->year_to) ? $content->year_to : 'null') .
  70        ',"changes":' . (isset($content->changes) ? $content->changes : 'null') .
  71        ',"defaultLanguage":' . (isset($content->default_language) ? '"' . $content->default_language . '"' : 'null') .
  72        ',"authorComments":' . (isset($content->author_comments) ? json_encode($content->author_comments) : 'null') . '}';
  73    }
  74  
  75    /**
  76     * Make the metadata into an associative array keyed by the property names
  77     * @param mixed $metadata Array or object containing metadata
  78     * @param bool $include_title
  79     * @param bool $include_missing For metadata fields not being set, skip 'em.
  80     *                             Relevant for content upgrade
  81     * @param array $types
  82     * @return array
  83     */
  84    public static function toDBArray($metadata, $include_title = true, $include_missing = true, &$types = array()) {
  85      $fields = array();
  86  
  87      if (!is_array($metadata)) {
  88        $metadata = (array) $metadata;
  89      }
  90  
  91      foreach (self::$fields as $key => $config) {
  92  
  93        // Ignore title?
  94        if ($key === 'title' && !$include_title) {
  95          continue;
  96        }
  97  
  98        $exists = array_key_exists($key, $metadata);
  99  
 100        // Don't include missing fields
 101        if (!$include_missing && !$exists) {
 102          continue;
 103        }
 104  
 105        $value = $exists ? $metadata[$key] : null;
 106  
 107        // lowerCamelCase to snake_case
 108        $db_field_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key));
 109  
 110        switch ($config['type']) {
 111          case 'text':
 112            if ($value !== null && strlen($value) > $config['maxLength']) {
 113              $value = mb_substr($value, 0, $config['maxLength']);
 114            }
 115            $types[] = '%s';
 116            break;
 117  
 118          case 'int':
 119            $value = ($value !== null) ? intval($value) : null;
 120            $types[] = '%d';
 121            break;
 122  
 123          case 'json':
 124            $value = ($value !== null) ? json_encode($value) : null;
 125            $types[] = '%s';
 126            break;
 127        }
 128  
 129        $fields[$db_field_name] = $value;
 130      }
 131  
 132      return $fields;
 133    }
 134  
 135    /**
 136     * The metadataSettings field in libraryJson uses 1 for true and 0 for false.
 137     * Here we are converting these to booleans, and also doing JSON encoding.
 138     * This is invoked before the library data is beeing inserted/updated to DB.
 139     *
 140     * @param array $metadataSettings
 141     * @return string
 142     */
 143    public static function boolifyAndEncodeSettings($metadataSettings) {
 144      // Convert metadataSettings values to boolean
 145      if (isset($metadataSettings['disable'])) {
 146        $metadataSettings['disable'] = $metadataSettings['disable'] === 1;
 147      }
 148      if (isset($metadataSettings['disableExtraTitleField'])) {
 149        $metadataSettings['disableExtraTitleField'] = $metadataSettings['disableExtraTitleField'] === 1;
 150      }
 151  
 152      return json_encode($metadataSettings);
 153    }
 154  }