You are here

opigno_in_house_training_app.install in Opigno in house training app 7

Contains install instructions and logic @ Carrard Cédric

File

opigno_in_house_training_app.install
View source
<?php

/**
 * @file
 * Contains install instructions and logic
 * @ Carrard Cédric
 */

/**
 * Implements hook_install()
 */
function opigno_in_house_training_app_install() {
  $type = node_type_load("in_house_training");

  //Check if content type exists
  if (empty($type)) {
    $type = node_type_set_defaults(array(
      'type' => 'in_house_training',
      'name' => st('In house training'),
      'base' => 'node_content',
      'description' => st('A Training, special content type'),
      'has_title' => '1',
      'title_label' => st('Title'),
      'help' => '',
    ));
    node_type_save($type);
  }
  variable_set('node_submitted_in_house_training', 0);
  if (module_exists('print')) {
    variable_set('print_display_in_house_training', 0);
    variable_set('print_display_urllist_in_house_training', 0);
    variable_set('print_pdf_display_in_house_training', 0);
    variable_set('print_pdf_display_urllist_in_house_training', 0);
  }
  if (module_exists('comment')) {
    variable_set('comment_in_house_training', COMMENT_NODE_CLOSED);
  }
  include_once drupal_get_path('module', 'og') . '/og_ui/og_ui.module';
  if (function_exists('og_ui_node_type_save')) {
    variable_set('og_group_content_type_in_house_training', TRUE);
    og_ui_node_type_save('in_house_training');
    opigno_in_house_training_app_update_og_group_ref();
    if (module_exists('og_access')) {
      og_create_field(OG_CONTENT_ACCESS_FIELD, 'node', 'in_house_training');
    }
  }

  /////////////////////////////////////////////

  //                                        //
  //            CREATE FIELD               //
  //                                      //

  /////////////////////////////////////////
  $field = field_info_field('opigno_iht_dates');
  if (empty($field)) {
    field_create_field(array(
      'active' => 1,
      'cardinality' => -1,
      'deleted' => 0,
      'entity_types' => array(),
      'field_name' => 'opigno_iht_dates',
      'foreign keys' => array(),
      'indexes' => array(),
      'locked' => 0,
      'module' => 'date',
      'settings' => array(
        'cache_count' => 4,
        'cache_enabled' => 0,
        'granularity' => array(
          'day' => 'day',
          'hour' => 'hour',
          'minute' => 'minute',
          'month' => 'month',
          'second' => 0,
          'year' => 'year',
        ),
        'repeat' => 1,
        'timezone_db' => 'UTC',
        'todate' => 'required',
        'tz_handling' => 'site',
      ),
      'translatable' => 0,
      'type' => 'datetime',
    ));
  }

  // if not empty create field_address
  $field = field_info_field('opigno_iht_address');
  if (empty($field)) {
    field_create_field(array(
      'active' => 1,
      'cardinality' => 1,
      'deleted' => 0,
      'entity_types' => array(),
      'field_name' => 'opigno_iht_address',
      'foreign keys' => array(
        'format' => array(
          'columns' => array(
            'format' => 'format',
          ),
          'table' => 'filter_format',
        ),
      ),
      'indexes' => array(
        'format' => array(
          0 => 'format',
        ),
      ),
      'locked' => 0,
      'module' => 'text',
      'settings' => array(
        'max_length' => 255,
      ),
      'translatable' => 0,
      'type' => 'text',
    ));
  }

  // if not empty create field_comment
  $field = field_info_field('opigno_iht_comment');
  if (empty($field)) {
    field_create_field(array(
      'active' => 1,
      'cardinality' => 1,
      'deleted' => 0,
      'entity_types' => array(),
      'field_name' => 'opigno_iht_comment',
      'foreign keys' => array(
        'format' => array(
          'columns' => array(
            'format' => 'format',
          ),
          'table' => 'filter_format',
        ),
      ),
      'indexes' => array(
        'format' => array(
          0 => 'format',
        ),
      ),
      'locked' => 0,
      'module' => 'text',
      'settings' => array(),
      'translatable' => 0,
      'type' => 'text_long',
    ));
  }

  // if not empty create field_status
  $field = field_info_field('opigno_iht_status');
  if (empty($field)) {
    field_create_field(array(
      'active' => 1,
      'cardinality' => 1,
      'deleted' => 0,
      'entity_types' => array(),
      'field_name' => 'opigno_iht_status',
      'foreign keys' => array(),
      'indexes' => array(
        'value' => array(
          0 => 'value',
        ),
      ),
      'locked' => 0,
      'module' => 'list',
      'settings' => array(
        'allowed_values' => array(
          'pending' => 'Pending',
          'completed' => 'Completed',
          'cancelled' => 'Cancelled',
          'rescheduled' => 'Rescheduled',
        ),
        'allowed_values_function' => '',
      ),
      'translatable' => 0,
      'type' => 'list_text',
    ));
  }

  // if not empty create field_status
  $field = field_info_field('opigno_calendar_date');
  if (empty($field)) {
    field_create_field(array(
      'active' => 1,
      'cardinality' => 1,
      'deleted' => 0,
      'entity_types' => array(),
      'field_name' => 'opigno_calendar_date',
      'foreign keys' => array(),
      'indexes' => array(),
      'locked' => 0,
      'module' => 'date',
      'settings' => array(
        'cache_count' => 4,
        'cache_enabled' => 0,
        'granularity' => array(
          'day' => 'day',
          'hour' => 'hour',
          'minute' => 'minute',
          'month' => 'month',
          'second' => 0,
          'year' => 'year',
        ),
        'timezone_db' => 'UTC',
        'todate' => 'optional',
        'tz_handling' => 'site',
      ),
      'translatable' => 0,
      'type' => 'datetime',
    ));
  }

  ///////////////////////////////////////////////

  //                                          //
  //            CREATE INSTANCE              //
  //                                        //

  ///////////////////////////////////////////
  $instance = field_info_instance('node', 'opigno_iht_dates', 'in_house_training');
  if (empty($instance)) {
    field_create_instance(array(
      'bundle' => 'in_house_training',
      'deleted' => 0,
      'description' => '',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'date',
          'settings' => array(),
          'type' => 'hidden',
          'weight' => 8,
        ),
        'teaser' => array(
          'label' => 'above',
          'settings' => array(),
          'type' => 'hidden',
          'weight' => 0,
        ),
      ),
      'entity_type' => 'node',
      'field_name' => 'opigno_iht_dates',
      'label' => 'In house training dates',
      'required' => 0,
      'settings' => array(
        'default_value' => 'now',
        'default_value2' => 'same',
        'default_value_code' => '',
        'default_value_code2' => '',
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'date',
        'settings' => array(
          'increment' => 15,
          'input_format' => 'm/d/Y - H:i:s',
          'input_format_custom' => '',
          'label_position' => 'above',
          'repeat_collapsed' => 1,
          'text_parts' => array(),
          'year_range' => '-3:+3',
        ),
        'type' => 'date_popup',
        'weight' => 32,
      ),
    ));
  }

  // if not empty create instance field_address
  $instance = field_info_instance('node', 'opigno_iht_address', 'in_house_training');
  if (empty($instance)) {
    field_create_instance(array(
      'bundle' => 'in_house_training',
      'default_value' => NULL,
      'deleted' => 0,
      'description' => '',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'simple_gmap',
          'settings' => array(
            'iframe_height' => 200,
            'iframe_width' => 200,
            'include_link' => 0,
            'include_map' => 1,
            'include_static_map' => 0,
            'include_text' => 1,
            'information_bubble' => 1,
            'langcode' => 'en',
            'link_text' => 'View larger map',
            'map_type' => 'm',
            'zoom_level' => 14,
          ),
          'type' => 'simple_gmap',
          'weight' => 2,
        ),
        'teaser' => array(
          'label' => 'above',
          'settings' => array(),
          'type' => 'hidden',
          'weight' => 0,
        ),
      ),
      'entity_type' => 'node',
      'field_name' => 'opigno_iht_address',
      'label' => 'Address',
      'required' => 1,
      'settings' => array(
        'text_processing' => 0,
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'text',
        'settings' => array(
          'size' => 60,
        ),
        'type' => 'text_textfield',
        'weight' => 5,
      ),
    ));
  }

  // if not empty create instance field_comment
  $instance = field_info_instance('node', 'opigno_iht_comment', 'in_house_training');
  if (empty($instance)) {
    field_create_instance(array(
      'bundle' => 'in_house_training',
      'default_value' => NULL,
      'deleted' => 0,
      'description' => '',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'text',
          'settings' => array(),
          'type' => 'text_default',
          'weight' => 0,
        ),
      ),
      'entity_type' => 'node',
      'field_name' => 'opigno_iht_comment',
      'label' => 'Comment',
      'required' => TRUE,
      'settings' => array(
        'text_processing' => 1,
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'module' => 'text',
        'settings' => array(
          'rows' => 5,
        ),
        'type' => 'text_textarea',
        'weight' => 0,
      ),
    ));
  }

  // if not empty create instance field_status
  $instance = field_info_instance('node', 'opigno_iht_status', 'in_house_training');
  if (empty($instance)) {
    field_create_instance(array(
      'bundle' => 'in_house_training',
      'default_value' => NULL,
      'deleted' => 0,
      'description' => '',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'list',
          'settings' => array(),
          'type' => 'list_default',
          'weight' => 6,
        ),
        'teaser' => array(
          'label' => 'above',
          'settings' => array(),
          'type' => 'hidden',
          'weight' => 0,
        ),
      ),
      'entity_type' => 'node',
      'field_name' => 'opigno_iht_status',
      'label' => 'Status',
      'required' => 1,
      'settings' => array(
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'options',
        'settings' => array(),
        'type' => 'options_select',
        'weight' => 4,
      ),
    ));
  }

  // if not empty create instance opigno_calendar_date
  $instance = field_info_instance('node', 'opigno_calendar_date', 'in_house_training');
  if (empty($instance)) {
    field_create_instance(array(
      'bundle' => 'in_house_training',
      'deleted' => 0,
      'description' => '',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'date',
          'settings' => array(
            'format_type' => 'long',
            'fromto' => 'both',
            'multiple_from' => '',
            'multiple_number' => '',
            'multiple_to' => '',
          ),
          'type' => 'date_default',
          'weight' => 4,
        ),
        'teaser' => array(
          'label' => 'above',
          'settings' => array(),
          'type' => 'hidden',
          'weight' => 0,
        ),
      ),
      'entity_type' => 'node',
      'field_name' => 'opigno_calendar_date',
      'label' => 'In house training date',
      'required' => 0,
      'settings' => array(
        'default_value' => 'now',
        'default_value2' => 'same',
        'default_value_code' => '',
        'default_value_code2' => '',
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'date',
        'settings' => array(
          'increment' => 15,
          'input_format' => 'm/d/Y - H:i:s',
          'input_format_custom' => '',
          'label_position' => 'above',
          'text_parts' => array(),
          'year_range' => '-3:+3',
        ),
        'type' => 'date_popup',
        'weight' => 2,
      ),
    ));
  }
  if (function_exists('opigno_lms_refresh_strings_and_import')) {
    opigno_lms_refresh_strings_and_import(array(
      'field',
    ));
  }
  opigno_in_house_app_install_default_permissions();
}

/**
 * Alters the og_group_ref_instance().
 */
function opigno_in_house_training_app_update_og_group_ref() {
  if (module_exists("entityreference_prepopulate")) {
    $instance = field_info_instance('node', 'og_group_ref', 'in_house_training');
    if (!empty($instance)) {
      $instance['default_value_function'] = 'entityreference_prepopulate_field_default_value';
      $instance['settings']['behaviors']['prepopulate'] = array(
        'action' => 'none',
        'action_on_edit' => 0,
        'fallback' => 'none',
        'og_context' => 0,
        'skip_perm' => 0,
        'status' => 1,
      );
    }
    $instance['display'] = array(
      'default' => array(
        'label' => 'hidden',
        'settings' => array(),
        'weight' => 1,
        'type' => 'hidden',
      ),
      'teaser' => array(
        'label' => 'hidden',
        'settings' => array(),
        'type' => 'hidden',
      ),
    );
    field_update_instance($instance);
  }
}

/**
 * Implements hook_schema().
 */
function opigno_in_house_training_app_schema() {
  return array(
    'opigno_in_house_training_results' => array(
      'fields' => array(
        'in_house_training_nid' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'score' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => '0',
        ),
        'status' => array(
          'type' => 'int',
          'not null' => TRUE,
          'default' => '0',
        ),
        'uid' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
      ),
      'indexes' => array(
        'in_house_training_id' => array(
          'in_house_training_nid',
        ),
        'user_id' => array(
          'uid',
        ),
      ),
    ),
  );
}
function opigno_in_house_app_install_default_permissions() {
  $administrator = user_role_load_by_name('administrator');
  $administrator_permissions = array(
    'create in_house_training content',
    'edit own in_house_training content',
    'edit any in_house_training content',
    'delete own in_house_training content',
    'delete any in_house_training content',
    'score in_house_app content',
  );
  user_role_grant_permissions($administrator->rid, $administrator_permissions);
  $student_manager = user_role_load_by_name('student manager');
  $student_manager_permissions = array(
    'create in_house_training content',
    'edit own in_house_training content',
    'delete own in_house_training content',
    'score in_house_app content',
  );
  user_role_grant_permissions($student_manager->rid, $student_manager_permissions);

  /* Default permission */
  og_invalidate_cache();
  $roles = og_roles("node", "course", $gid = 0, $force_group = FALSE, $include_all = TRUE);
  foreach ($roles as $index => $role) {
    switch ($role) {
      case 'student':
        og_role_grant_permissions($index, array(
          'view in_house_app content',
        ));
        break;
      case 'coach':
        og_role_grant_permissions($index, array(
          'view in_house_app content',
        ));
        og_role_grant_permissions($index, array(
          'score in_house_app content',
        ));
        og_role_grant_permissions($index, array(
          'create in_house_training content',
        ));
        og_role_grant_permissions($index, array(
          'update own in_house_training content',
        ));
        og_role_grant_permissions($index, array(
          'delete own in_house_training content',
        ));
        break;
      case "teacher":
        og_role_grant_permissions($index, array(
          'view in_house_app content',
        ));
        og_role_grant_permissions($index, array(
          'score in_house_app content',
        ));
        og_role_grant_permissions($index, array(
          'create in_house_training content',
        ));
        og_role_grant_permissions($index, array(
          'update own in_house_training content',
        ));
        og_role_grant_permissions($index, array(
          'delete own in_house_training content',
        ));
        break;
      case "forum moderator":
        og_role_grant_permissions($index, array(
          'view in_house_app content',
        ));
        break;
      case "manager":
        og_role_grant_permissions($index, array(
          'view in_house_app content',
        ));
        og_role_grant_permissions($index, array(
          'score in_house_app content',
        ));
        og_role_grant_permissions($index, array(
          'create in_house_training content',
        ));
        og_role_grant_permissions($index, array(
          'update own in_house_training content',
        ));
        og_role_grant_permissions($index, array(
          'update any in_house_training content',
        ));
        og_role_grant_permissions($index, array(
          'delete any in_house_training content',
        ));
        og_role_grant_permissions($index, array(
          'delete own in_house_training content',
        ));
        break;
    }
  }

  /* Existing group permissions */
  $existing_courses = opigno_in_house_training_app_get_all_courses();
  foreach ($existing_courses as $courseid => $course) {
    $roles = og_roles("node", "course", $courseid, $force_group = FALSE, $include_all = TRUE);
    foreach ($roles as $index => $role) {
      switch ($role) {
        case 'student':
          og_role_grant_permissions($index, array(
            'view in_house_app content',
          ));
          break;
        case 'coach':
          og_role_grant_permissions($index, array(
            'view in_house_app content',
          ));
          og_role_grant_permissions($index, array(
            'score in_house_app content',
          ));
          og_role_grant_permissions($index, array(
            'create in_house_training content',
          ));
          og_role_grant_permissions($index, array(
            'update own in_house_training content',
          ));
          og_role_grant_permissions($index, array(
            'delete own in_house_training content',
          ));
          break;
        case "teacher":
          og_role_grant_permissions($index, array(
            'view in_house_app content',
          ));
          og_role_grant_permissions($index, array(
            'score in_house_app content',
          ));
          og_role_grant_permissions($index, array(
            'create in_house_training content',
          ));
          og_role_grant_permissions($index, array(
            'update own in_house_training content',
          ));
          og_role_grant_permissions($index, array(
            'delete own in_house_training content',
          ));
          break;
        case "forum moderator":
          og_role_grant_permissions($index, array(
            'view in_house_app content',
          ));
          break;
        case "manager":
          og_role_grant_permissions($index, array(
            'view in_house_app content',
          ));
          og_role_grant_permissions($index, array(
            'score in_house_app content',
          ));
          og_role_grant_permissions($index, array(
            'create in_house_training content',
          ));
          og_role_grant_permissions($index, array(
            'update own in_house_training content',
          ));
          og_role_grant_permissions($index, array(
            'update any in_house_training content',
          ));
          og_role_grant_permissions($index, array(
            'delete any in_house_training content',
          ));
          og_role_grant_permissions($index, array(
            'delete own in_house_training content',
          ));
          break;
      }
    }
  }
}

/*
 *
 * Node_load_multiple is to heavy
 *
 */
function opigno_in_house_training_app_get_all_courses() {
  $courses = array();
  $query = db_select('node', 'n');
  $query
    ->fields('n', array(
    'nid',
  ));
  $query
    ->condition('n.type', "course", '=');
  $result = $query
    ->execute();
  while ($record = $result
    ->fetchAssoc()) {
    $courses[$record['nid']] = $record['nid'];
  }
  return $courses;
}