You are here

function opigno_module_update_8016 in Opigno module 8

Update module, activity, answer entities and statistic for Skills system.

File

./opigno_module.install, line 999
Opigno module app install/update functionality.

Code

function opigno_module_update_8016() {

  // Install a module.
  \Drupal::service('module_installer')
    ->install([
    'opigno_skills_system',
  ]);

  // Create a new field definition.
  $definition_manager = \Drupal::entityDefinitionUpdateManager();

  // Create new Skills field in opigno_module entity.
  $field = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Activate skills system for this module'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(FALSE)
    ->setDisplayOptions('form', array(
    'type' => 'boolean_checkbox',
    'weight' => 1,
  ));
  $definition_manager
    ->installFieldStorageDefinition('skills_active', 'opigno_module', 'opigno_module', $field);

  // Create new Target Skill field in opigno_module entity.
  $term_storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  $target_skills = $term_storage
    ->loadTree('skills', 0, 1);
  $options = [];
  foreach ($target_skills as $row) {
    $options[$row->tid] = $row->name;
  }
  $field = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Target skill'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setSetting('allowed_values', $options)
    ->setDisplayOptions('form', [
    'type' => 'select',
    'weight' => 3,
  ]);
  $definition_manager
    ->installFieldStorageDefinition('skill_target', 'opigno_module', 'opigno_module', $field);

  // Create new Skills list field in opigno_activity entity.
  $field = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Skills list'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setSetting('target_type', 'taxonomy_term')
    ->setSetting('handler', 'default:taxonomy_term')
    ->setSetting('handler_settings', array(
    'target_bundles' => array(
      'skills' => 'skills',
    ),
  ))
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 3,
  ]);
  $definition_manager
    ->installFieldStorageDefinition('skills_list', 'opigno_activity', 'opigno_module', $field);

  // Create Usage activity field in opigno_activity entity.
  $options = [
    'local' => 'Only in current module',
    'global' => 'In global system of Opigno skills',
  ];
  $field = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Usage of activity'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue('local')
    ->setRequired(TRUE)
    ->setSetting('allowed_values', $options)
    ->setDisplayOptions('form', [
    'type' => 'options_buttons',
    'weight' => 3,
  ]);
  $definition_manager
    ->installFieldStorageDefinition('usage_activity', 'opigno_activity', 'opigno_module', $field);

  // Create skills mode field in opigno_answer entity.
  $field = BaseFieldDefinition::create('integer')
    ->setLabel(t('Skills mode'))
    ->setDescription(t('Show if skill mode is activated.'))
    ->setDefaultValue(0);
  $definition_manager
    ->installFieldStorageDefinition('skills_mode', 'opigno_answer', 'opigno_module', $field);

  // Create table for skills statistic.
  $schema = \Drupal::database()
    ->schema();
  $table_name = 'opigno_skills_statistic';
  if (!$schema
    ->tableExists($table_name)) {
    $table = [
      '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',
      ],
    ];
    $schema
      ->createTable($table_name, $table);
  }

  // Create module global field in opigno_answer entity.
  $field = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Use all suitable activities from Opigno system'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(FALSE)
    ->setDisplayOptions('form', array(
    'type' => 'boolean_checkbox',
    'weight' => 1,
  ));
  $definition_manager
    ->installFieldStorageDefinition('module_global', 'opigno_module', 'opigno_module', $field);
}