Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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