<?php

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

/**
 * Implements hook_install().
 */
function archibald_curricula_install() {
  // Curriculum data is often too big to be cached in cache backends like
  // Memcached or Redis. Use the database.
  variable_set('cache_class_cache_curricula', 'DrupalDatabaseCache');
}

/**
 * Implements hook_uninstall().
 */
function archibald_curricula_uninstall() {
  variable_del('cache_class_cache_curricula');
}

/**
 * Implements hook_schema().
 */
function archibald_curricula_schema() {
  $schema = array();
  // Provide a custom cache bin for curricula data. This is mainly to prevent
  // the data from being cleared after a global cache wipe.
  $schema['cache_curricula'] = drupal_get_schema_unprocessed('system', 'cache');
  return $schema;
}

/**
 * Implements hook_requirements().
 */
function archibald_curricula_requirements($phase) {
  $t = get_t();

  if ($phase == 'runtime') {
    $return = array();

    foreach (archibald_curricula_libraries_info() as $library_name => $library_info) {
      $library = libraries_load($library_name);

      $return['archibald_curricula__' . $library_name] = array(
        'title' => $t("Archibald Curricula: %name library", array('%name' => $library_info['name'])),
        'value' => t("Not installed"),
        'description' => $t("In order to use Archibald Curricula, you must first download the !library_name library at !link. Check the README.txt file for more information", array(
            '!library_name' => $library_info['name'],
            '!link' => $library_info['download url'],
          )),
        'severity' => REQUIREMENT_ERROR,
      );

      if (empty($library['installed'])) {
        $return['archibald_curricula__' . $library_name]['value'] = $t("Not installed");
        $return['archibald_curricula__' . $library_name]['severity'] = REQUIREMENT_ERROR;
      }
      else {
        $return['archibald_curricula__' . $library_name]['value'] = $t("!library_name @version available", array(
          '!library_name' => $library_info['name'],
          '@version' => $library['version'],
        ));
        $return['archibald_curricula__' . $library_name]['severity'] = REQUIREMENT_OK;
      }
    }

    return $return;
  }
}

