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