<?php

/**
 * @file
 * Install, update and uninstall functions for the module.
 */

use Drupal\Core\Config\FileStorage;

/**
 * Implements hook_install().
 *
 * If "anu_lms_permissions" was already installed then necessary import
 * optional configs from that module. Optional configs from that module
 * contains dependencies to "node.type.module_assessment".
 */
function anu_lms_assessments_install() {
  $moduleHandler = \Drupal::service('module_handler');
  if (!$moduleHandler->moduleExists('anu_lms_permissions')) {
    return;
  }

  // It's "Organization: Group node (Quiz)".
  $config_name = 'group.content_type.group_content_type_d342280e2d680';

  $config_storage = \Drupal::service('config.storage');
  if (!$config_storage->exists($config_name)) {
    $config_path = Drupal::service('extension.path.resolver')
      ->getPath('module', 'anu_lms_permissions') . '/config/optional';

    $source = new FileStorage($config_path);
    $config_storage->write($config_name, $source->read($config_name));
  }
}

/**
 * Alter results table.
 */
function anu_lms_assessments_update_8001() {
  $database = \Drupal::database();
  $database->query("ALTER TABLE assessment_result MODIFY name VARCHAR(255)");
}
