You are here

function opigno_quiz_app_install in Opigno Quiz App 7

Implements hook_install().

File

./opigno_quiz_app.install, line 11
Defines module install logic.

Code

function opigno_quiz_app_install() {

  // Add the quiz weight field.
  $field = field_info_field('quiz_weight');
  if (empty($field)) {
    field_create_field(array(
      'field_name' => 'quiz_weight',
      'type' => 'number_float',
    ));
  }

  // Add the quiz type field.
  $field = field_info_field('quiz_type');
  if (empty($field)) {
    field_create_field(array(
      'field_name' => 'quiz_type',
      'type' => 'list_text',
      'cardinality' => 1,
      'settings' => array(
        'allowed_values' => array(
          'quiz' => 'Quiz',
          'theory' => 'Theory',
          'mix' => 'Mix',
        ),
        'allowed_values_function' => '',
      ),
    ));
  }

  // Add the og required quiz field.
  $field = field_info_field('course_required_quiz_ref');
  if (empty($field)) {
    field_create_field(array(
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'field_name' => 'course_required_quiz_ref',
      'foreign keys' => array(
        'node' => array(
          'columns' => array(
            'target_id' => 'nid',
          ),
          'table' => 'node',
        ),
      ),
      'indexes' => array(
        'target_id' => array(
          0 => 'target_id',
        ),
      ),
      'module' => 'entityreference',
      'settings' => array(
        'handler' => 'views',
        'handler_settings' => array(
          'behaviors' => array(
            'views-select-list' => array(
              'status' => 0,
            ),
          ),
          'view' => array(
            'args' => array(),
            'display_name' => 'entityreference_1',
            'view_name' => 'opigno_quizzes',
          ),
        ),
        'target_type' => 'node',
      ),
      'type' => 'entityreference',
    ));
  }
  $instance = field_info_instance('node', 'quiz_weight', 'quiz');
  if (empty($instance)) {
    field_create_instance(array(
      'field_name' => 'quiz_weight',
      'entity_type' => 'node',
      'bundle' => 'quiz',
      'label' => "Quiz weight",
      'description' => "Sets the weight of this quiz for the final course assessment. Leave at 0 if this quiz should not be taken into account for the final grade.",
      'required' => TRUE,
      'default_value' => array(
        0 => array(
          'value' => 1,
        ),
      ),
    ));
  }
  $instance = field_info_instance('node', 'quiz_type', 'quiz');
  if (empty($instance)) {
    field_create_instance(array(
      'field_name' => 'quiz_type',
      'entity_type' => 'node',
      'bundle' => 'quiz',
      'label' => "Quiz type",
      'description' => "Sets the type of this lesson. Can be <em>theory</em> (containing theoretical slides), <em>quiz</em> (assessment, exam) and <em>mix</em> (containing both theoretical slides, SCORM packages, H5P rich contents, and assessments)",
      'required' => TRUE,
      'default_value' => array(
        0 => array(
          'value' => 1,
        ),
      ),
    ));
  }
  if (module_exists('opigno_calendar_app')) {
    opigno_quiz_app_enable_calendar_integration();
  }

  // Add the OG audience field.
  include_once drupal_get_path('module', 'og') . '/og_ui/og_ui.module';
  if (function_exists('og_ui_node_type_save')) {
    $types = array();
    if (function_exists('_quiz_question_get_implementations')) {
      $types += array_keys(_quiz_question_get_implementations());
    }
    array_unshift($types, "quiz");
    foreach ($types as $type) {
      variable_set('og_group_content_type_' . $type, TRUE);
      og_ui_node_type_save($type);
      variable_set('node_submitted_' . $type, 0);

      // Disable comments by default.
      if (module_exists('comment')) {
        variable_set('comment_' . $type, COMMENT_NODE_CLOSED);
      }

      // Make the body field be displayed fully on question.
      $instance = field_info_instance('node', 'body', $type);
      if (!empty($instance)) {
        if ($type == "cloze") {
          $instance['display']['question'] = array();
          $instance['display']['question']['label'] = "hidden";
          $instance['display']['question']['type'] = "hidden";
          $instance['display']['question']['weight'] = 0;
          $instance['display']['question']['settings'] = array();
        }
        else {
          $instance['display']['question']['type'] = 'text_default';
        }
        field_update_instance($instance);
      }
      $instance = field_info_instance('node', 'og_group_ref', $type);
      if (!empty($instance)) {
        foreach ($instance['display'] as $index => $display_mode) {
          $instance['display'][$index] = array();
          $instance['display'][$index]['label'] = "hidden";
          $instance['display'][$index]['type'] = "hidden";
          $instance['display'][$index]['weight'] = 3;
          $instance['display'][$index]['settings'] = array();
          field_update_instance($instance);
        }
      }
    }
  }

  //For scorm display the specific field
  $instance = field_info_instance('node', 'opigno_scorm_package', 'opigno_scorm_quiz_question');
  if (!empty($instance)) {
    $instance['display']['question'] = array();
    $instance['display']['question']['label'] = "hidden";
    $instance['display']['question']['type'] = "opigno_scorm_player";
    $instance['display']['question']['weight'] = 10;
    $instance['display']['question']['settings'] = array();
    $instance['display']['question']['module'] = "opigno_scorm_ui";
    field_update_instance($instance);
  }

  // Add the OG content access field.
  if (module_exists('og_access')) {
    og_create_field(OG_CONTENT_ACCESS_FIELD, 'node', 'quiz');
  }

  // If a OG course exists.
  if (defined('OPIGNO_COURSE_BUNDLE')) {
    $instance = field_info_instance('node', 'course_required_quiz_ref', OPIGNO_COURSE_BUNDLE);
    if (empty($instance)) {
      field_create_instance(array(
        'field_name' => 'course_required_quiz_ref',
        'entity_type' => 'node',
        'bundle' => OPIGNO_COURSE_BUNDLE,
        'label' => "Required quiz",
        'description' => "Defines a required quiz for this course. Users cannot finish the course without passing this quiz.",
        'required' => FALSE,
        'default_value' => 0,
      ));
    }
  }
}