<?php
/**
 * @file
 * Install, update and uninstall functions for the Opigno LMS installation profile.
 */

/**
 * Implements hook_install().
 */
function opigno_lms_install() {
  // Set the default theme.
  theme_enable(array('platon'));
  variable_set('theme_default', 'platon');

  // Set the jQuery version we want to use.
  variable_set('jquery_update_jquery_version', '1.8');

  // Set the DOMPDF library for the certificates, but display a message encouraging the users
  // to use wkhtml.
  variable_set('print_pdf_pdf_tool', 'profiles/opigno_lms/libraries/dompdf/dompdf_config.inc.php');

  // Set login redirect.
  variable_set('login_redirect_status', 1);
  variable_get('login_redirect_parameter_name', 'dashboard');

  // Set front page.
  variable_set('site_frontpage', 'dashboard');

  // Enable blocks.  
  _opigno_lms_install_enable_blocks();

  // Set default user settings.
  _opigno_lms_install_user_settings();

  // Set default OG settings.
  _opigno_lms_install_og_settings();

  // Create default entries in the menu.
  _opigno_lms_install_menu();
}

/** 
 * Helper function to enable default blocks.
 */
function _opigno_lms_install_enable_blocks() {
  $default_theme = variable_get('theme_default', 'platon');
  
  $blocks = array(
    array(
      'module' => 'system',
      'delta' => 'main',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'content',
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'search',
      'delta' => 'form',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => -1,
      'region' => 'search_box',
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'user',
      'delta' => 'login',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'sidebar_first',
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'system',
      'delta' => 'navigation',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'sidebar_first',
      'pages' => '',
      'cache' => -1,
    ),
  );
  $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
  foreach ($blocks as $block) {
    $query->values($block);
  }
  $query->execute();
}

/**
 * Helper function to set default user settings and roles.
 */
function _opigno_lms_install_user_settings() {
  // Enable user picture support and set the default to a square thumbnail option.
  variable_set('user_pictures', '1');
  variable_set('user_picture_dimensions', '1024x1024');
  variable_set('user_picture_file_size', '800');
  variable_set('user_picture_style', 'thumbnail');

  // Allow visitor account creation with administrative approval.
  variable_set('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);

  // Enable default permissions for system roles.
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('access content'));

  // Create a default role for site administrators, with all available permissions assigned.
  $admin_role = user_role_load_by_name('administrator');
  if (empty($admin_role)) {
    $admin_role = new stdClass();
    $admin_role->name = 'administrator';
    $admin_role->weight = 2;
    user_role_save($admin_role);
  }

  user_role_grant_permissions($admin_role->rid, array_keys(module_invoke_all('permission')));

  // Set this as the administrator role.
  variable_set('user_admin_role', $admin_role->rid);

  // Assign user 1 the "administrator" role.
  db_merge('users_roles')
    ->key(array('uid' => 1, 'rid' => $admin_role->rid))
    ->fields(array('uid' => 1, 'rid' => $admin_role->rid))
    ->execute();

  // Create a default role for student managers.
  $student_manager_role = user_role_load_by_name('student manager');
  if (empty($student_manager_role)) {
    $student_manager_role = new stdClass();
    $student_manager_role->name = 'student manager';
    $student_manager_role->weight = 3;
    user_role_save($student_manager_role);
  }
  user_role_grant_permissions($student_manager_role->rid);

  variable_set('opigno_lms_student_manager_role', $student_manager_role->rid);
}

/**
 * Helper function to enable default menu entries.
 */
function _opigno_lms_install_menu() {
  // Create a Home link in the main menu.
  $item = array(
    'link_title' => st('Home'),
    'link_path' => '<front>',
    'menu_name' => 'main-menu',
    'options' => array(
      'attributes' => array(
        'class' => array('icon', 'icon-home'),
      ),
    ),
    'weight' => -50,
  );
  menu_link_save($item);

  // Create links for the apps.
  $item = array(
    'link_title' => st('My courses'),
    'link_path' => 'my-courses',
    'menu_name' => 'main-menu',
    'options' => array(
      'attributes' => array(
        'class' => array('icon', 'icon-courses'),
      ),
    ),
    'weight' => -45,
  );
  menu_link_save($item);

  $item = array(
    'link_title' => st('Course catalogue'),
    'link_path' => 'course-catalogue',
    'menu_name' => 'main-menu',
    'options' => array(
      'attributes' => array(
        'class' => array('icon', 'icon-catalogue'),
      ),
    ),
    'weight' => -40,
  );
  menu_link_save($item);

  $item = array(
    'link_title' => st('Forum'),
    'link_path' => 'forum',
    'menu_name' => 'main-menu',
    'options' => array(
      'attributes' => array(
        'class' => array('icon', 'icon-forum'),
      ),
    ),
    'weight' => -35,
  );
  menu_link_save($item);

  $item = array(
    'link_title' => st('Calendar'),
    'link_path' => 'opigno-calendar',
    'menu_name' => 'main-menu',
    'options' => array(
      'attributes' => array(
        'class' => array('icon', 'icon-calendar'),
      ),
    ),
    'weight' => -30,
  );
  menu_link_save($item);

  $item = array(
    'link_title' => st('Messages'),
    'link_path' => 'messages',
    'menu_name' => 'main-menu',
    'options' => array(
      'attributes' => array(
        'class' => array('icon', 'icon-messages'),
      ),
    ),
    'weight' => -20,
  );
  menu_link_save($item);

  $item = array(
    'link_title' => st('My achievements'),
    'link_path' => 'my-achievements',
    'menu_name' => 'main-menu',
    'options' => array(
      'attributes' => array(
        'class' => array('icon', 'icon-certificates'),
      ),
    ),
    'weight' => -10,
  );
  menu_link_save($item);

  // Create an administration link in the main menu.
  $item = array(
    'link_title' => st('Administration'),
    'link_path' => 'admin/opigno',
    'menu_name' => 'main-menu',
    'options' => array(
      'attributes' => array(
        'class' => array('icon', 'icon-settings'),
      ),
    ),
    'weight' => 50,
  );
  menu_link_save($item);

  // Update the menu router information.
  menu_rebuild();
}

/**
 * Helper function to set default OG settings.
 */
function _opigno_lms_install_og_settings() {
  //$roles = og_roles('node', OPIGNO_COURSE_BUNDLE);
  //dpm($roles, 'roles');
}
