You are here

opigno_group_manager.install in Opigno group manager 8

Same filename and directory in other branches
  1. 3.x opigno_group_manager.install

Opigno group manager install/update functionality.

File

opigno_group_manager.install
View source
<?php

/**
 * @file
 * Opigno group manager install/update functionality.
 */
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent;
function opigno_group_manager_install() {
  $definition = \Drupal::entityTypeManager()
    ->getDefinition('opigno_group_link');

  // Make field "required_activities" value to be null.
  // @todo: add this settings to BaseFieldDefinition::create() for this field.
  $schema = \Drupal::database()
    ->schema();
  if ($schema
    ->fieldExists($definition
    ->getBaseTable(), 'required_activities')) {
    $schema
      ->changeField($definition
      ->getBaseTable(), 'required_activities', 'required_activities', [
      'type' => 'varchar',
      'length' => 8191,
      'not null' => FALSE,
    ]);
  }
}

/**
 * Add check if the content in the skills system.
 */
function opigno_group_manager_update_8001() {
  $definition_manager = \Drupal::entityDefinitionUpdateManager();
  $field = BaseFieldDefinition::create('boolean')
    ->setLabel(t('In the sills system'))
    ->setDescription(t('Indicate if this module in the skills system.'))
    ->setDefaultValue(0);
  $definition_manager
    ->installFieldStorageDefinition('in_skills_system', 'opigno_group_content', 'opigno_group_manager', $field);
}

/**
 * Add required activities field to Opigno Link entity.
 */
function opigno_group_manager_update_8002() {

  // Create a new field definition.
  $field = BaseFieldDefinition::create('string')
    ->setLabel(t('Required activities'))
    ->setDescription(t('The required activities of previous step'))
    ->setSetting('max_length', 8191);

  // Install the new definition.
  $definition_manager = \Drupal::entityDefinitionUpdateManager();
  $definition_manager
    ->installFieldStorageDefinition('required_activities', 'opigno_group_link', 'opigno_group_link', $field);
}
function opigno_group_manager_update_8003() {

  // Inapplicable update.
}

/**
 * Update required activities field. Set default value to NULL.
 */
function opigno_group_manager_update_8004() {
  $definition = \Drupal::entityTypeManager()
    ->getDefinition('opigno_group_link');
  $schema = \Drupal::database()
    ->schema();
  if ($schema
    ->fieldExists($definition
    ->getBaseTable(), 'required_activities')) {
    $schema
      ->changeField($definition
      ->getBaseTable(), 'required_activities', 'required_activities', [
      'type' => 'varchar',
      'length' => 8191,
      'not null' => FALSE,
    ]);
  }
}

/**
 * Enable Serialization module for training objects export-import can work.
 */
function opigno_group_manager_update_8005() {
  \Drupal::service('module_installer')
    ->install([
    'serialization',
  ]);
}

/**
 * Uninstall ggroup.
 */
function opigno_group_manager_update_8006() {
  $modules = [
    'ggroup',
  ];
  \Drupal::database()
    ->delete('key_value')
    ->condition('collection', 'system.schema')
    ->condition('name', $modules, 'IN')
    ->execute();
  $module_data = \Drupal::config('core.extension')
    ->get('module');
  unset($module_data['ggroup']);
  \Drupal::configFactory()
    ->getEditable('core.extension')
    ->set('module', $module_data)
    ->save();
  \Drupal::service('cache.config')
    ->invalidateAll();
  drupal_flush_all_caches();
}

/**
 * Content relationship cleanup.
 */
function opigno_group_manager_update_8007() {
  $query = \Drupal::database()
    ->select('opigno_group_content', 'op')
    ->fields('op', [
    'id',
  ]);
  $query
    ->leftJoin('groups', 'g', 'op.group_id = g.id');
  $query
    ->where('isnull(g.id) = :condition', [
    ':condition' => TRUE,
  ]);
  $ids = $query
    ->execute()
    ->fetchCol();

  /* @var \Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent[] $opigno_group_managed_contents */
  $opigno_group_managed_contents = OpignoGroupManagedContent::loadMultiple($ids);
  foreach ($opigno_group_managed_contents as $opigno_group_managed_content) {
    $opigno_group_managed_content
      ->delete();
  }
}

/**
 * Remove deleted ILT and Live meetings from opigno_group_content.
 */
function opigno_group_manager_update_8008() {

  // Remove ILTs.
  $query = \Drupal::database()
    ->select('opigno_group_content', 'g')
    ->fields('g', [
    'id',
  ]);
  $query
    ->leftJoin('opigno_ilt', 'i', 'g.entity_id = i.id');
  $query
    ->where('g.group_content_type_id = :condition', [
    ':condition' => 'ContentTypeILT',
  ]);
  $query
    ->isNull('i.id');
  $ids = $query
    ->execute()
    ->fetchCol();

  /* @var \Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent[] $opigno_group_managed_contents */
  $opigno_group_managed_contents = OpignoGroupManagedContent::loadMultiple($ids);
  foreach ($opigno_group_managed_contents as $opigno_group_managed_content) {
    $opigno_group_managed_content
      ->delete();
  }

  // Remove Live meetings.
  $query = \Drupal::database()
    ->select('opigno_group_content', 'g')
    ->fields('g', [
    'id',
  ]);
  $query
    ->leftJoin('opigno_moxtra_meeting', 'i', 'g.entity_id = i.id');
  $query
    ->where('g.group_content_type_id = :condition', [
    ':condition' => 'ContentTypeMeeting',
  ]);
  $query
    ->isNull('i.id');
  $ids = $query
    ->execute()
    ->fetchCol();

  /* @var \Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent[] $opigno_group_managed_contents */
  $opigno_group_managed_contents = OpignoGroupManagedContent::loadMultiple($ids);
  foreach ($opigno_group_managed_contents as $opigno_group_managed_content) {
    $opigno_group_managed_content
      ->delete();
  }
}

Functions

Namesort descending Description
opigno_group_manager_install
opigno_group_manager_update_8001 Add check if the content in the skills system.
opigno_group_manager_update_8002 Add required activities field to Opigno Link entity.
opigno_group_manager_update_8003
opigno_group_manager_update_8004 Update required activities field. Set default value to NULL.
opigno_group_manager_update_8005 Enable Serialization module for training objects export-import can work.
opigno_group_manager_update_8006 Uninstall ggroup.
opigno_group_manager_update_8007 Content relationship cleanup.
opigno_group_manager_update_8008 Remove deleted ILT and Live meetings from opigno_group_content.