You are here

opigno_module.install in Opigno module 3.x

Same filename and directory in other branches
  1. 8 opigno_module.install

Opigno module app install/update functionality.

File

opigno_module.install
View source
<?php

/**
 * @file
 * Opigno module app install/update functionality.
 */
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\opigno_module\Form\ModuleH5PAdminSettingsForm;

/**
 * Implements hook_schema().
 */
function opigno_module_schema() {

  // Create the opigno module relationship table.
  $schema['opigno_module_relationship'] = [
    'description' => 'Table storing what activities belong to what modules',
    'fields' => [
      'omr_id' => [
        'type' => 'serial',
        'size' => 'normal',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The primary identifier of this relationship.',
      ],
      'omr_pid' => [
        'type' => 'int',
        'size' => 'normal',
        'unsigned' => TRUE,
        'not null' => FALSE,
        'default' => NULL,
        'description' => 'The parent relationship of this relationship.',
      ],
      'parent_id' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The Module that this activity belongs to.',
      ],
      'parent_vid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The Module version that this activity belongs to.',
      ],
      'child_id' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The Activity ID.',
      ],
      'child_vid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The Activity version ID.',
      ],
      'activity_status' => [
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
        'description' => 'The status of the Activity in this Module. 0=random, 1=always',
      ],
      'weight' => [
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The weight of this Activity in the Module.',
      ],
      'max_score' => [
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The max score of the Activity in this Module.',
      ],
      'auto_update_max_score' => [
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Boolean indicating whether updates to the Activity will update the max score of the Activity in the Module.',
      ],
      'group_id' => [
        'type' => 'int',
        'size' => 'normal',
        'description' => "Training ID",
      ],
    ],
    'primary key' => [
      'omr_id',
    ],
    'unique keys' => [
      'parent_child' => [
        'parent_id',
        'parent_vid',
        'child_id',
        'child_vid',
      ],
    ],
    'indexes' => [
      'parent_vid' => [
        'parent_vid',
      ],
      'child_vid' => [
        'child_vid',
      ],
      'parent_id' => [
        'parent_id',
      ],
      'child_id' => [
        'child_id',
      ],
      'group_id' => [
        'group_id',
      ],
    ],
  ];
  $schema['opigno_module_result_options'] = [
    'description' => 'Table storing result options for module.',
    'fields' => [
      'option_id' => [
        'type' => 'serial',
        'size' => 'normal',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The primary identifier for the range.',
      ],
      'module_id' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Module identifier.',
      ],
      'module_vid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Module revision identifier.',
      ],
      'option_start' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Score range low value.',
      ],
      'option_end' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'default' => 0,
        'description' => 'Score range high value.',
      ],
      'option_name' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'The name of this range.',
      ],
      'option_summary' => [
        'type' => 'text',
        'description' => 'The text to show when this range is met.',
      ],
      'option_summary_format' => [
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'Text format of the range text.',
      ],
    ],
    'primary key' => [
      'option_id',
    ],
    'indexes' => [
      'module_id' => [
        'module_id',
        'module_vid',
      ],
    ],
  ];
  $schema['opigno_module_badges'] = [
    'description' => 'Opigno Module Badges',
    'fields' => [
      'id' => [
        'type' => 'serial',
        'not null' => TRUE,
      ],
      'uid' => [
        'description' => 'User ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'gid' => [
        'description' => 'Training ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'entity_id' => [
        'description' => 'Entity ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'typology' => [
        'description' => 'Typology',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'badges' => [
        'description' => 'Badges count',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => [
      'id',
    ],
    'indexes' => [
      'uid' => [
        'uid',
      ],
      'gid' => [
        'gid',
      ],
      'entity_id' => [
        'entity_id',
      ],
      'typology' => [
        'typology',
      ],
    ],
    'foreign keys' => [
      'users' => [
        'uid' => 'uid',
      ],
      'entity_id' => [
        'entity_id',
      ],
      'typology' => [
        'typology',
      ],
    ],
  ];

  // Create table for skills statistic.
  $schema['opigno_skills_statistic'] = [
    'description' => 'Opigno Skills Statistic',
    'fields' => [
      'id' => [
        'type' => 'serial',
        'not null' => TRUE,
      ],
      'tid' => [
        'description' => 'Term ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'uid' => [
        'description' => 'User ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'score' => [
        'description' => 'Score',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ],
      'progress' => [
        'description' => 'Progress',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ],
      'stage' => [
        'description' => 'Stage',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ],
    ],
    'primary key' => [
      'id',
    ],
  ];
  return $schema;
}

/**
 * Implements hook_install().
 */
function opigno_module_install() {

  // Give an access to specific Opigno module permissions.
  $issueAccessPermissions = [
    RoleInterface::AUTHENTICATED_ID => [
      'view published module entities',
      'access editor',
    ],
    'content_manager' => [
      'access media_entity_browser_badge_images entity browser pages',
    ],
  ];
  foreach ($issueAccessPermissions as $role => $permissions) {
    $role = Role::load($role);
    if (!$role instanceof RoleInterface) {
      continue;
    }
    foreach ($permissions as $permission) {
      $role
        ->grantPermission($permission);
    }
    try {
      $role
        ->save();
    } catch (EntityStorageException $e) {
      watchdog_exception('opigno_module_exception', $e);
    }
  }
  $not_recommended = ModuleH5PAdminSettingsForm::disabledByDefault();
  $config = \Drupal::service('config.factory')
    ->getEditable('opigno_module.settings');
  $config
    ->set('disabled_h5p', array_values($not_recommended));
  $config
    ->save();
}

/**
 * Implements hook_requirements().
 */
function opigno_module_requirements($phase) {
  $requirements = [];
  $message = t('pdf.js library is not installed. Please install it from <a href="@library">here</a> and place in <em>libraries/</em> folder', [
    '@library' => 'http://mozilla.github.io/pdf.js/getting_started/',
  ]);
  $pdf_js_library = file_exists('libraries/pdf.js/build/pdf.js') && file_exists('libraries/pdf.js/build/pdf.worker.js');
  if ($phase == 'runtime') {
    if (!$pdf_js_library) {
      $requirements += [
        'opigno_module' => [
          'title' => t('Slide activity'),
          'value' => $message,
          'severity' => REQUIREMENT_ERROR,
        ],
      ];
    }
  }
  return $requirements;
}

/**
 * Update views 'groups list'.
 */
function opigno_module_update_9001() {
  _opigno_lms_apply_update('module', 'opigno_module', 'config/optional', [
    'views.view.groups_list',
  ]);
  _opigno_lms_apply_update('module', 'opigno_module', 'config/rewrite', [
    'views.view.user_admin_people',
  ]);
}

Functions

Namesort descending Description
opigno_module_install Implements hook_install().
opigno_module_requirements Implements hook_requirements().
opigno_module_schema Implements hook_schema().
opigno_module_update_9001 Update views 'groups list'.