You are here

opigno_lms.install in Opigno LMS 8.2

Same filename and directory in other branches
  1. 8 opigno_lms.install
  2. 7 opigno_lms.install
  3. 3.x 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\file\Entity\File;
use Drupal\media\Entity\MediaType;
use Drupal\user\Entity\User;
use Drupal\Core\StreamWrapper\PrivateStream;
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() {
  $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", "platon");
  $logo_path = $theme_path . "/logo.png";
  $config
    ->getEditable('platon.settings')
    ->set('logo.path', $logo_path)
    ->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("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;
}

/**
 * Implements hook_update_N().
 */
function opigno_lms_update_8001() {

  // Install module.
  \Drupal::service('module_installer')
    ->install([
    'opigno_tour',
    'opigno_alter_entity_autocomplete',
  ]);
}

/**
 * Implements hook_update_N().
 *
 * Updates Opigno Activities Bank (LP Interface) view.
 *
 */
function opigno_lms_update_8002() {
  $config_path = drupal_get_path('module', 'opigno_module') . '/config/optional';

  /* @var Drupal\Core\Config\CachedStorage $config_storage */
  $storage = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $data = $storage
    ->read('views.view.opigno_activities_bank_lp_interface');
  $config_storage
    ->write('views.view.opigno_activities_bank_lp_interface', $data);
}

/**
 * Implements hook_update_N().
 *
 * Update Opigno LP Steps block visibility configuration.
 *
 */
function opigno_lms_update_8003() {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('block.block.lp_steps_block');
  $visibility = $config
    ->get('visibility');

  // Change visibility settings only if admins didn't change them.
  if (isset($visibility['request_path']['pages']) && $visibility['request_path']['pages'] == "/module/*\r\n/group/*/module/*") {
    $visibility['request_path']['pages'] = "/module/*/result*\r\n/group/*/module/*";
    $config
      ->set('visibility', $visibility);
    $config
      ->save(TRUE);
  }
}

/**
 * Update content manager permissions.
 */
function opigno_lms_update_8004() {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('user.role.content_manager');

  // Add new permissions.
  $config_permissions =& $config
    ->get('permissions');
  $issue_permissions = [
    'message all groups',
    'message anyone regardless of groups',
  ];
  foreach ($issue_permissions as $permission) {
    if (!in_array($permission, $config_permissions)) {
      $config_permissions[] = $permission;
    }
  }
  $config
    ->set('permissions', $config_permissions);
  $config
    ->save(TRUE);
}

/**
 * Remove unnecessary media type 'file'.
 */
function opigno_lms_update_8005() {
  try {
    $media_type = MediaType::load('file');
    if ($media_type) {
      $media_type
        ->delete();
    }
  } catch (Exception $e) {
    $message = t("Error removing media type 'file': ");
    \Drupal::logger('opigno_lms')
      ->error($message . $e
      ->getMessage());
    $messenger = \Drupal::messenger();
    $messenger
      ->addError($message . $e
      ->getMessage());
  }
}

Functions

Namesort descending Description
opigno_lms_install Implements hook_install().
opigno_lms_requirements Implements hook_requirements().
opigno_lms_update_8001 Implements hook_update_N().
opigno_lms_update_8002 Implements hook_update_N().
opigno_lms_update_8003 Implements hook_update_N().
opigno_lms_update_8004 Update content manager permissions.
opigno_lms_update_8005 Remove unnecessary media type 'file'.