You are here

opigno_lms.install in Opigno LMS 3.x

Same filename and directory in other branches
  1. 8.2 opigno_lms.install
  2. 8 opigno_lms.install
  3. 7 opigno_lms.install

Install, update and uninstall functions for the "opigno_lms" installation profile.

File

opigno_lms.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the "opigno_lms" installation
 * profile.
 */
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Database\Database;
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;

/**
 * Implements hook_install().
 *
 * Performs actions to set up the site for this profile.
 *
 * @see system_install()
 *
 * @throws \Drupal\Core\Extension\MissingDependencyException
 */
function opigno_lms_install() {
  \Drupal::service('module_installer')
    ->install([
    'opigno_alter_entity_autocomplete',
  ]);
  $config = \Drupal::configFactory();

  // Disable the user pictures on nodes.
  $config
    ->getEditable('system.theme.global')
    ->set('features.node_user_picture', FALSE)
    ->save(TRUE);

  // Allow visitor account creation, but with administrative approval.
  $config
    ->getEditable('user.settings')
    ->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
    ->save(TRUE);

  // Set default home page.
  $config
    ->getEditable('system.site')
    ->set('page.front', '/node')
    ->save(TRUE);

  // Set theme logo path.
  $theme_path = drupal_get_path("theme", "aristotle");
  $logo_path = $theme_path . "/assets/Logo-Opigno-white.svg";
  $config
    ->getEditable('aristotle.settings')
    ->set('logo.path', $logo_path)
    ->save(TRUE);
  $logo_second = $theme_path . "/assets/Logo-Opigno-3-dark.svg";
  $config
    ->getEditable('aristotle.settings')
    ->set('logo_path2', $logo_second)
    ->save(TRUE);

  // Assign user 1 the "administrator" role.
  $user = User::load(1);
  $user
    ->addRole('administrator');
  $user
    ->save();

  // Copy user login page background image to public folder.
  $image = file_get_contents(drupal_get_path("theme", "aristotle") . "/assets/Trient-0014.jpg");
  file_save_data($image, 'public://Trient-0014.jpg', TRUE);

  // Copy user login page background image to public folder.
  $image = file_get_contents(drupal_get_path("profile", "opigno_lms") . "/images/Opigno_Image_connexion_1600-829_2.jpg");
  file_save_data($image, 'public://Opigno_Image_connexion_1600-829_2.jpg', TRUE);
  $dompdf_autoloaders = [
    'libraries/dompdf/src/Autoloader.php',
    'profiles/opigno_lms/libraries/dompdf/src/Autoloader.php',
  ];
  foreach ($dompdf_autoloaders as $dompdf_autoloader) {
    if (file_exists($dompdf_autoloader)) {

      // Load dompdf for the entity_print install.
      require_once $dompdf_autoloader;
      \Dompdf\Autoloader::register();

      // Install entity_print.
      \Drupal::service('module_installer')
        ->install([
        'entity_print',
      ]);
      break;
    }
  }
}

/**
 * Implements hook_requirements().
 */
function opigno_lms_requirements($phase) {
  $requirements = [];

  // Test if the Private File System Path is set.
  if ($phase == 'runtime') {
    $file_private_path_base_path = PrivateStream::basePath();
    if ($file_private_path_base_path === NULL) {
      $requirements['file_private_system'] = [
        'title' => 'Private Files System',
        'value' => t('Private file system path not set'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('Your uploaded files are not fully protected because you did not set a Private File Directory.
                          You need to set an existing local file system path for storing private files.
                          It should be writable by Drupal and not accessible over the web. This must be changed in settings.php.
                          More info: https://opigno.atlassian.net/wiki/spaces/OUM20/pages/743636993/Enable+private+file+system'),
      ];
    }
    else {
      $requirements['file_private_system'] = [
        'title' => 'Private Files System',
        'value' => t('Private file system path is set'),
        'severity' => REQUIREMENT_OK,
      ];
    }
  }
  return $requirements;
}

/**
 * Uninstall redundant modules.
 */
function opigno_lms_update_9001() {
  _opigno_lms_update_module_disable([
    'opigno_tour',
    'multiselect',
    'openapi',
    'openapi_ui',
    'openapi_ui_swagger',
    'views_role_based_global_text',
  ]);
  _opigno_lms_apply_delete([
    'tour.tour.views-ui',
    'tour.tour.training_edit',
    'tour.tour.training',
    'tour.tour.search-api-server-form',
    'tour.tour.search-api-server',
    'tour.tour.search-api-index-processors',
    'tour.tour.search-api-index-form',
    'tour.tour.search-api-index-fields',
    'tour.tour.search-api-index',
    'tour.tour.locale',
    'tour.tour.language-edit',
    'tour.tour.language-add',
    'tour.tour.language',
    'tour.tour.front',
    'tour.tour.catalogue',
    'tour.tour.achievements',
    'core.entity_view_mode.tour.token',
  ]);
  $schema = Database::getConnection()
    ->schema();
  $schema
    ->dropTable('opigno_tour_user_routes');
}

/**
 * Import Aristotle theme configs.
 */
function opigno_lms_update_9002() {
  _opigno_lms_apply_update('theme', 'aristotle', 'config/optional', [
    'aristotle.settings',
    'block.block.opigno_admin',
    'block.block.messages',
  ]);
}

/**
 * Install opigno social.
 */
function opigno_lms_update_9003() {
  \Drupal::service('module_installer')
    ->install([
    'opigno_social',
  ]);
}

/**
 * Install theme.
 */
function opigno_lms_update_9004() {
  $theme = 'aristotle';
  \Drupal::service('theme_installer')
    ->install([
    $theme,
  ]);
  \Drupal::configFactory()
    ->getEditable('system.theme')
    ->set('default', $theme)
    ->set('admin', $theme)
    ->save();
  drupal_flush_all_caches();
}

/**
 * Import updated configs.
 */
function opigno_lms_update_9005() {
  _opigno_lms_apply_update('profile', 'opigno_lms', 'config/install', [
    'block.block.opigno_training_catalog_filter',
    'aristotle.settings',
    'block.block.mainnavigation',
    'block.block.opigno_admin',
    'block.block.primaryadminactions',
    'block.block.sitebranding',
    'block.block.tabs',
  ]);
  _opigno_lms_apply_delete([
    'block.block.opignoadmin',
  ]);

  // Copy user login page background image to public folder.
  $image = file_get_contents(drupal_get_path("theme", "aristotle") . "/assets/Trient-0014.jpg");
  file_save_data($image, 'public://Trient-0014.jpg', TRUE);
}

/**
 * Remove the platon block and deprecated dashboard.
 */
function opigno_lms_update_9006() {
  try {
    \Drupal::service('theme_installer')
      ->uninstall([
      'platon',
    ]);
  } catch (\Exception $e) {
  }
  _opigno_lms_apply_update('module', 'opigno_lms', 'config/optional', [
    'block.block.dashboard_views_block_opigno_notifications_block_unread_dashboard',
    'block.block.dashboard_views_block_private_message_block_dashboard',
    'block.block.views_block__private_message_block_all',
    'block.block.dashboard_views_block_who_s_online_who_s_online_block',
    'block.block.dashboard_views_block_who_s_new_block_1',
    'block.block.dashboard_views_block_opigno_score_modules_dashboard_notavaluated_modules',
    'block.block.dashboard_views_block_opigno_group_members_block_pending',
    'block.block.dashboard_views_block_opigno_calendar_month_block',
    'block.block.views_block__opigno_calendar_month_block',
    'block.block.dashboard_views_block_latest_active_trainings_block',
    'block.block.dashboard_views_block_content_recent_block_1',
    'block.block.dashboard_views_block_comments_recent_block_1',
    'block.block.dashboard_calendar_legend_block',
    'block.block.dashboard_search_form_block',
  ]);
  _opigno_lms_apply_delete([
    'platon.settings',
    'block.block.platon_breadcrumbs',
    'block.block.platon_content',
    'block.block.platon_local_tasks',
    'block.block.platon_messages',
    'block.block.platon_page_title',
    'block.block.platon_main_menu',
    'block.block.addclassaction',
    'block.block.platon_footer',
    'block.block.platon_branding',
    'block.block.platon_local_actions',
    'block.block.privatemessageactions',
    'block.block.privatemessageinbox',
    'block.block.privatemessagenotification',
    'block.block.learningpathprogress',
  ]);
  $theme_data = \Drupal::config('core.extension')
    ->get('theme');
  unset($theme_data['platon']);
  \Drupal::configFactory()
    ->getEditable('core.extension')
    ->set('theme', $theme_data)
    ->save();
  drupal_flush_all_caches();
}

/**
 * Update dashboard settings.
 */
function opigno_lms_update_9007() {
  _opigno_lms_apply_update('profile', 'opigno_lms', 'config/install', [
    'opigno_dashboard.settings',
  ]);
}

/**
 * Update module configs.
 */
function opigno_lms_update_9008() {
  _opigno_lms_apply_update('module', 'opigno_h5p', 'config/optional', [
    'core.entity_view_display.opigno_activity.opigno_h5p.activity',
    'core.entity_view_display.opigno_activity.opigno_h5p.default',
  ]);
  _opigno_lms_apply_update('module', 'opigno_long_answer', 'config/optional', [
    'core.entity_view_display.opigno_activity.opigno_long_answer.default',
  ]);
  _opigno_lms_apply_update('module', 'opigno_scorm_activity', 'config/optional', [
    'core.entity_view_display.opigno_activity.opigno_scorm.default',
  ]);
  _opigno_lms_apply_update('module', 'opigno_tincan_activity', 'config/optional', [
    'core.entity_view_display.opigno_activity.opigno_tincan.default',
  ]);
  _opigno_lms_apply_update('module', 'opigno_module', 'config/optional', [
    'core.entity_view_display.opigno_activity.opigno_slide.default',
  ]);
}

/**
 * Update theme logo.
 */
function opigno_lms_update_9009() {
  _opigno_lms_apply_update('profile', 'opigno_lms', 'config/install', [
    'aristotle.settings',
  ]);
}

/**
 * Fix the last update number for opigno_certificate module.
 */
function opigno_lms_update_9010() {
  drupal_set_installed_schema_version('opigno_certificate', 8025);
  if ((int) drupal_get_installed_schema_version('opigno_learning_path') > 9005) {
    drupal_set_installed_schema_version('opigno_learning_path', 9005);
  }
  _opigno_lms_apply_update('module', 'opigno_certificate', 'config/install', [
    'core.date_format.certificate_date',
  ]);
}

/**
 * Configs update helper function.
 */
function _opigno_lms_apply_update($type, $name, $path, $ymls) {
  $theme_path = sprintf("%s/%s/", drupal_get_path($type, $name), $path);
  $config_factory = \Drupal::configFactory();
  $config_storage = \Drupal::service('config.storage');
  $configs = [];
  foreach ($ymls as $yml) {
    $configs[$yml] = $theme_path;
  }
  foreach ($configs as $config => $config_path) {
    $source = new FileStorage($config_path);
    $data = $source
      ->read($config);
    if (is_array($data)) {
      $config_factory
        ->getEditable($config)
        ->setData($data)
        ->save(TRUE);
      $config_storage
        ->write($config, $data);
    }
    else {
      \Drupal::messenger()
        ->addWarning(t('Incorrect data of @config', [
        '@config' => $config,
      ]));
    }
  }
}

/**
 * Configs remove helper function.
 */
function _opigno_lms_apply_delete($configs) {
  $config_factory = \Drupal::configFactory();

  /** @var StorageInterface $config_storage */
  $config_storage = \Drupal::service('config.storage');
  foreach ($configs as $config) {
    $config_factory
      ->getEditable($config)
      ->delete();
    $config_storage
      ->delete($config);
  }
}

/**
 * Module disable helper function.
 */
function _opigno_lms_update_module_disable($modules) {
  $module_data = \Drupal::config('core.extension')
    ->get('module');
  foreach ($modules as $item) {
    \Drupal::database()
      ->delete('key_value')
      ->condition('collection', 'system.schema')
      ->condition('name', $item)
      ->execute();
    unset($module_data[$item]);
  }
  \Drupal::configFactory()
    ->getEditable('core.extension')
    ->set('module', $module_data)
    ->save();
  \Drupal::service('cache.config')
    ->invalidateAll();
  foreach ($modules as $item) {
    if (\Drupal::moduleHandler()
      ->moduleExists($item)) {
      \Drupal::service('module_installer')
        ->uninstall([
        $item,
      ]);
    }
  }
  drupal_flush_all_caches();
}

/**
 * Update dependencies.
 */
function opigno_lms_update_dependencies() {
  $dependencies['opigno_calendar'][9001] = [
    'opigno_lms' => 9001,
  ];
  $dependencies['opigno_learning_path'][9001] = [
    'opigno_catalog' => 9001,
    'opigno_lms' => 9001,
  ];
  return $dependencies;
}

Functions

Namesort descending Description
opigno_lms_install Implements hook_install().
opigno_lms_requirements Implements hook_requirements().
opigno_lms_update_9001 Uninstall redundant modules.
opigno_lms_update_9002 Import Aristotle theme configs.
opigno_lms_update_9003 Install opigno social.
opigno_lms_update_9004 Install theme.
opigno_lms_update_9005 Import updated configs.
opigno_lms_update_9006 Remove the platon block and deprecated dashboard.
opigno_lms_update_9007 Update dashboard settings.
opigno_lms_update_9008 Update module configs.
opigno_lms_update_9009 Update theme logo.
opigno_lms_update_9010 Fix the last update number for opigno_certificate module.
opigno_lms_update_dependencies Update dependencies.
_opigno_lms_apply_delete Configs remove helper function.
_opigno_lms_apply_update Configs update helper function.
_opigno_lms_update_module_disable Module disable helper function.