h5p.install 14.4 KB
Newer Older
falcon's avatar
falcon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

/**
 * @file
 *  h5p.install php file
 *  Install and upgrade instructions for the h5p Drupal module.
 */

/**
 * Implementation of hook_install().
 */
function h5p_install() {
  drupal_install_schema('h5p');
}

/**
 * Implementation of hook_uninstall().
 */
function h5p_uninstall() {
  drupal_uninstall_schema('h5p');
falcon's avatar
falcon committed
21 22
  variable_del('h5p_export');
  variable_del('h5p_revisioning');
falcon's avatar
falcon committed
23 24 25 26 27 28
}

/**
 * Implementation of hook_schema().
 */
function h5p_schema() {
falcon's avatar
falcon committed
29
  $schema['h5p_nodes_libraries'] = array(
falcon's avatar
falcon committed
30 31
    'description' => 'Stores information about what h5p uses what libraries.',
    'fields' => array(
icc's avatar
icc committed
32 33
      'content_id' => array(
        'description' => 'Primary Key: The unique identifier for a node(vid by default).',
falcon's avatar
falcon committed
34 35 36 37 38
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'library_id' => array(
39
        'description' => 'The identifier of a h5p library this content uses.',
falcon's avatar
falcon committed
40 41 42 43
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
44
      'dependency_type' => array(
45 46 47
        'description' => "dynamic, preloaded or editor",
        'type' => 'varchar',
        'length' => 255,
falcon's avatar
falcon committed
48
        'not null' => TRUE,
49
        'default' => 'preloaded',
falcon's avatar
falcon committed
50
      ),
falcon's avatar
falcon committed
51
      'drop_css' => array(
52
        'description' => '1 if the preloaded css from the dependency is to be excluded.',
53 54 55 56 57 58
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
falcon's avatar
falcon committed
59
    ),
icc's avatar
icc committed
60
    'primary key' => array('content_id', 'library_id', 'dependency_type'),
falcon's avatar
falcon committed
61 62
  );

falcon's avatar
falcon committed
63
  $schema['h5p_libraries'] = array(
falcon's avatar
falcon committed
64 65
    'description' => 'Stores information about what h5p uses what libraries.',
    'fields' => array(
falcon's avatar
falcon committed
66
      'library_id' => array(
falcon's avatar
falcon committed
67 68 69 70 71 72
        'description' => 'Primary Key: The id of the library.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'machine_name' => array(
falcon's avatar
falcon committed
73
        'description' => 'The library machine name',
falcon's avatar
falcon committed
74 75 76
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
falcon's avatar
falcon committed
77
        'default' => '',
falcon's avatar
falcon committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      ),
      'title' => array(
        'description' => 'The human readable name of this library',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'major_version' => array(
        'description' => 'The version of this library',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'minor_version' => array(
        'description' => 'The minor version of this library',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'patch_version' => array(
        'description' => 'The patch version of this library',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'runnable' => array(
        'description' => 'Whether or not this library is executable.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
falcon's avatar
falcon committed
112 113 114 115 116 117 118 119
      'fullscreen' => array(
        'description' => 'Indicates if this library can be opened in fullscreen.',
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
falcon's avatar
falcon committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      'embed_types' => array(
        'description' => 'The allowed embed types for this library as a comma separated list',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'preloaded_js' => array(
        'description' => 'The preloaded js for this library as a comma separated list',
        'type' => 'text',
      ),
      'preloaded_css' => array(
        'description' => 'The preloaded css for this library as a comma separated list',
        'type' => 'text',
      ),
135
      'drop_library_css' => array( // TODO: This really needs to be move to h5p_libraries_libraries.
falcon's avatar
falcon committed
136 137 138
        'description' => 'List of libraries that should not have CSS included if this library is used. Comma separated list.',
        'type' => 'text',
      ),
icc's avatar
icc committed
139 140 141 142 143
      'semantics' => array(
        'description' => 'The semantics definition in json format',
        'type' => 'text',
        'not null' => TRUE,
      ),
falcon's avatar
falcon committed
144 145 146
    ),
    'primary key' => array('library_id'),
  );
falcon's avatar
falcon committed
147

falcon's avatar
falcon committed
148
  $schema['h5p_libraries_libraries'] = array(
falcon's avatar
falcon committed
149 150 151 152 153 154 155 156
    'description' => 'Stores information about library dependencies.',
    'fields' => array(
      'library_id' => array(
        'description' => 'Primary Key: The id of a h5p library.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
falcon's avatar
falcon committed
157 158
      'required_library_id' => array(
        'description' => 'Primary Key: The id of a h5p library.',
falcon's avatar
falcon committed
159 160 161 162
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
icc's avatar
icc committed
163 164 165 166
      'dependency_type' => array(
        'description' => 'preloaded, dynamic, or editor',
        'type' => 'varchar',
        'length' => 255,
falcon's avatar
falcon committed
167 168 169
        'not null' => TRUE,
      ),
    ),
falcon's avatar
falcon committed
170
    'primary key' => array('library_id', 'required_library_id'),
falcon's avatar
falcon committed
171
  );
falcon's avatar
falcon committed
172

falcon's avatar
falcon committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  $schema['h5p_libraries_languages'] = array(
    'description' => 'Stores translations for the languages.',
    'fields' => array(
      'library_id' => array(
        'description' => 'Primary Key: The id of a h5p library.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'language_code' => array(
        'description' => 'Primary Key: The language code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'language_json' => array(
        'description' => 'The translations defined in json format',
        'type' => 'text',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('library_id', 'language_code'),
  );

falcon's avatar
falcon committed
197
  $schema['h5p_nodes'] = array(
falcon's avatar
falcon committed
198 199
    'description' => 'Stores information about where the h5p content is stored.',
    'fields' => array(
icc's avatar
icc committed
200 201
      'content_id' => array(
        'description' => 'Primary Key: The unique identifier for this node(vid by default).',
falcon's avatar
falcon committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'description' => 'The {node}.nid of the h5p node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'json_content' => array(
        'description' => 'The content in json format',
        'type' => 'text',
        'not null' => TRUE,
Svein-Tore Griff With's avatar
Svein-Tore Griff With committed
216
        'size' => 'big',
falcon's avatar
falcon committed
217 218 219 220 221 222 223 224
      ),
      'embed_type' => array(
        'description' => 'The embed type for this content',
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'default' => '',
      ),
falcon's avatar
falcon committed
225
      'main_library_id' => array(
icc's avatar
icc committed
226
        'description' => 'The library we first instanciate for this node',
falcon's avatar
falcon committed
227 228
        'type' => 'int',
        'unsigned' => TRUE,
falcon's avatar
falcon committed
229 230
        'not null' => TRUE,
      ),
falcon's avatar
falcon committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
      'content_type' => array(
        'description' => 'Content type as defined in h5p.json',
        'type' => 'varchar',
        'length' => 127,
      ),
      'author' => array(
        'description' => 'Author as defined in h5p.json',
        'type' => 'varchar',
        'length' => 127,
      ),
      'license' => array(
        'description' => 'License as defined in h5p.json',
        'type' => 'varchar',
        'length' => 7,
      ),
      'meta_keywords' => array(
        'description' => 'Keywords as defined in h5p.json',
        'type' => 'text',
      ),
      'meta_description' => array(
        'description' => 'Meta description as defined in h5p.json',
        'type' => 'text',
      ),
falcon's avatar
falcon committed
254
    ),
icc's avatar
icc committed
255
    'primary key' => array('content_id'),
falcon's avatar
falcon committed
256 257 258 259 260 261 262 263 264 265 266
  );
  return $schema;
}

/**
 * Implementation of hook_requirements().
 */
function h5p_requirements($phase) {
  $requirements = array();

  if ($phase == 'runtime') {
icc's avatar
icc committed
267
    // TODO: Add check for zip library
falcon's avatar
falcon committed
268
    // File paths
falcon's avatar
falcon committed
269
    $h5p_path = file_create_path(file_directory_path() . '/' . variable_get('h5p_default_path', 'h5p'));
falcon's avatar
falcon committed
270
    $temp_path = $h5p_path . '/temp';
271
    $export_path = $h5p_path . '/exports';
falcon's avatar
falcon committed
272 273 274 275 276 277 278 279 280 281 282
    if (!file_check_directory($h5p_path, FILE_CREATE_DIRECTORY)) {
      $requirements['h5p_dirs'] = array(
        'value' => t('Missing directory.'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t("The h5p module's package directory %h5p_path is missing.", array('%h5p_path' => $h5p_path)),
      );
    }
    else if (!file_check_directory($temp_path, FILE_CREATE_DIRECTORY)) {
      $requirements['h5p_dirs'] = array(
        'value' => t('Missing temp directory.'),
        'severity' => REQUIREMENT_ERROR,
falcon's avatar
falcon committed
283
        'description' => t("The h5p module's temp directory %temp_path is missing.", array('%temp_path' => $temp_path)),
falcon's avatar
falcon committed
284 285
      );
    }
286 287 288 289 290 291 292
    else if (!file_check_directory($export_path, FILE_CREATE_DIRECTORY)) {
      $requirements['h5p_dirs'] = array(
        'value' => t('Missing export directory.'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t("The h5p module's export directory %export_path is missing.", array('%export_path' => $export_path)),
      );
    }
falcon's avatar
falcon committed
293 294 295 296 297 298 299 300 301 302
    else {
      $requirements['h5p_dirs'] = array(
        'value' => t('Exists (%path).', array('%path' => $h5p_path)),
        'severity' => REQUIREMENT_OK,
      );
    }
    $requirements['h5p_dirs']['title'] = t('h5p module directories');
  }

  return $requirements;
falcon's avatar
falcon committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
}

/**
 * Implementation of hook_update_N().
 */
function h5p_update_6100() {
  $ret = array();
  db_add_field($ret, 'h5p_libraries', 'fullscreen', array(
    'description' => 'Indicates if this library can be opened in fullscreen.',
    'type' => 'int',
    'size' => 'tiny',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  return $ret;
falcon's avatar
falcon committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
}

/**
 * Implementation of hook_update_N().
 */
function h5p_update_6101() {
  $ret = array();
  db_create_table($ret, 'h5p_libraries_languages', array(
    'description' => 'Stores translations for the languages.',
    'fields' => array(
      'library_id' => array(
        'description' => 'Primary Key: The id of a h5p library.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'language_code' => array(
        'description' => 'Primary Key: The language code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'language_json' => array(
        'description' => 'The translations defined in json format',
        'type' => 'text',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('library_id', 'language_code'),
  ));
falcon's avatar
falcon committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
  return $ret;
}

/**
 * Implementation of hook_update_N().
 */
function h5p_update_6102() {
  $ret = array();
  db_add_field($ret, 'h5p_nodes', 'content_type', array(
    'description' => 'Content type as defined in h5p.json',
    'type' => 'varchar',
    'length' => 127,
  ));
  db_add_field($ret, 'h5p_nodes', 'author', array(
    'description' => 'Author as defined in h5p.json',
    'type' => 'varchar',
    'length' => 127,
  ));
  db_add_field($ret, 'h5p_nodes', 'license', array(
    'description' => 'License as defined in h5p.json',
    'type' => 'varchar',
    'length' => 7,
  ));
  db_add_field($ret, 'h5p_nodes', 'meta_keywords', array(
    'description' => 'Keywords as defined in h5p.json',
    'type' => 'text',
  ));
  db_add_field($ret, 'h5p_nodes', 'meta_description', array(
    'description' => 'Meta description as defined in h5p.json',
    'type' => 'text',
  ));
380

Svein-Tore Griff With's avatar
Svein-Tore Griff With committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394
  return $ret;
}

/**
 * Implementation of hook_update_N().
 */
function h5p_update_6103() {
  $ret = array();
  db_change_field($ret, 'h5p_nodes', 'json_content', 'json_content', array(
    'description' => 'The content in json format',
    'type' => 'text',
    'not null' => TRUE,
    'size' => 'big',
  ));
falcon's avatar
falcon committed
395
  return $ret;
396 397 398 399 400 401 402 403
}

/**
 * Implementation of hook_update_N().
 */
function h5p_update_6104() {
  $ret = array();
  db_add_index($ret, 'files', 'filepath', array('filepath'));
404 405 406 407 408
  return $ret;
}

/**
 * Implementation of hook_update_N().
paalj's avatar
paalj committed
409 410 411 412 413
 * 
 * Renames the h5p_nodes_libraries.preloaded field to dependency_type,
 * and converts it to contain an integer.
 * 
 * Updates h5p_nodes_libraries to get all dependencies.
414
 */
paalj's avatar
paalj committed
415
function h5p_update_6105(&$sandbox) {
416
  $ret = array();
417

418
  if (isset($sandbox['content']) === FALSE) {
419
    
paalj's avatar
paalj committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    if (db_column_exists('h5p_nodes_libraries', 'preloaded')) {
      db_drop_primary_key($ret, 'h5p_nodes_libraries');
      db_change_field($ret, 'h5p_nodes_libraries', 'preloaded', 'dependency_type', array(
        'description' => "dynamic, preloaded or editor",
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'preloaded',
      ), array(
        'primary key' => array('content_id', 'library_id', 'dependency_type')
      ));
      
      $result = end($ret);
      if ($result['success'] === FALSE) {
        return $ret; // Do not update nodes if update failed.
      }
436
    }
437
  
438 439 440 441 442 443 444 445 446
    // Update h5p_nodes_libraries to get all dependencies.
    $result = db_query("
      SELECT hn.content_id AS id, 
             hl.machine_name AS libraryName, 
             hl.major_version AS libraryMajorVersion, 
             hl.minor_version AS libraryMinorVersion
      FROM {h5p_nodes} hn
      JOIN {h5p_libraries} hl ON hl.library_id = hn.main_library_id
    ");
447
    
paalj's avatar
paalj committed
448 449 450
    // Update h5p_nodes_libraries since column has changed type
    db_query("UPDATE {h5p_nodes_libraries} set dependency_type='preloaded' where dependency_type='1'");
    db_query("UPDATE {h5p_nodes_libraries} set dependency_type='dynamic' where dependency_type='0'");
451

paalj's avatar
paalj committed
452
    $sandbox['content'] = array();
453 454 455 456 457 458 459
    while ($content = db_fetch_object($result)) {
      $sandbox['content'][] = $content;
    }
    
    $sandbox['current'] = 0;
    $sandbox['max'] = count($sandbox['content']);
  }
paalj's avatar
paalj committed
460
  
461 462 463 464 465 466 467 468
  if ($sandbox['max'] === 0) {
    $ret['#finished'] = 1;
  }
  else {
    h5p_save_content_dependencies($sandbox['content'][$sandbox['current']]);
    $sandbox['current']++;
    $ret['#finished'] = ($sandbox['current'] / $sandbox['max']);
  }
469
  
470
  return $ret;
paalj's avatar
paalj committed
471
}
472 473 474 475 476 477 478 479 480

/**
 * (re)Save all dependencies for given content.
 * This will given content nodes a relation to all libraries used by it's main library.
 *
 * @param object $content
 */
function h5p_save_content_dependencies($content) {
  $i = _h5p_get_instance('interface');
paalj's avatar
paalj committed
481
  $core = _h5p_get_instance('core');
482 483 484 485
  
  $library = $i->loadLibrary($content->libraryName, $content->libraryMajorVersion, $content->libraryMinorVersion);
  
  $dependencies = array();
paalj's avatar
paalj committed
486
  $core->findLibraryDependencies($dependencies, $library);
487
  
488 489
  $i->saveLibraryUsage($content->id, $dependencies);
}