You are here

function opigno_quiz_app_update_7101 in Opigno Quiz App 7

Enable h5p module

File

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

Code

function opigno_quiz_app_update_7101(&$sandbox) {
  if (!module_exists('h5p')) {
    module_enable(array(
      'h5p',
    ));
  }
  if (!module_exists('quiz_h5p')) {
    module_enable(array(
      'quiz_h5p',
    ));
  }
  if (module_exists("quiz")) {
    $instance = field_info_instance('node', 'quiz_type', 'quiz');
    if (!empty($instance)) {
      $instance['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)";
      field_update_instance($instance);
    }
    $types = array_keys(_quiz_question_get_implementations());
    foreach ($types as $type) {
      variable_set('node_submitted_' . $type, 0);

      // makes sure all question types are og group content
      if (module_exists("og_ui")) {
        include_once drupal_get_path('module', 'og') . '/og_ui/og_ui.module';
        $og_content_type = variable_get('og_group_content_type_' . $type, FALSE);
        if ($og_content_type == FALSE) {
          variable_set('og_group_content_type_' . $type, TRUE);
          og_ui_node_type_save($type);
        }
      }

      // makes all questions to have the body displaying for the question display mode
      $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);
    }

    //For slide display pdf
    $instance = field_info_instance('node', 'slide_pdf', 'quiz_directions');
    if (!empty($instance)) {
      $instance['display']['question']['label'] = "hidden";
      $instance['display']['question']['module'] = "pdf";
      $instance['display']['question']['settings']['height'] = "600px";
      $instance['display']['question']['settings']['keep_pdfjs'] = 1;
      $instance['display']['question']['settings']['width'] = "100%";
      $instance['display']['question']['type'] = "pdf_default";
      $instance['display']['question']['weight'] = 10;
    }
  }

  /* 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 "manager":
        og_role_grant_permissions($index, array(
          'create h5p_content content',
        ));
        og_role_grant_permissions($index, array(
          'update own h5p_content content',
        ));
        og_role_grant_permissions($index, array(
          'update any h5p_content content',
        ));
        og_role_grant_permissions($index, array(
          'delete own h5p_content content',
        ));
        og_role_grant_permissions($index, array(
          'delete any h5p_content content',
        ));
        og_role_grant_permissions($index, array(
          'create opigno_scorm_quiz_question content',
        ));
        og_role_grant_permissions($index, array(
          'update own opigno_scorm_quiz_question content',
        ));
        og_role_grant_permissions($index, array(
          'update any opigno_scorm_quiz_question content',
        ));
        og_role_grant_permissions($index, array(
          'delete own opigno_scorm_quiz_question content',
        ));
        og_role_grant_permissions($index, array(
          'delete any opigno_scorm_quiz_question content',
        ));
        break;
      case "teacher":
        og_role_grant_permissions($index, array(
          'create h5p_content content',
        ));
        og_role_grant_permissions($index, array(
          'update own h5p_content content',
        ));
        og_role_grant_permissions($index, array(
          'delete own h5p_content content',
        ));
        og_role_grant_permissions($index, array(
          'create opigno_scorm_quiz_question content',
        ));
        og_role_grant_permissions($index, array(
          'update own opigno_scorm_quiz_question content',
        ));
        og_role_grant_permissions($index, array(
          'delete own opigno_scorm_quiz_question content',
        ));
        break;
    }
  }

  /* Existing group permissions */
  $existing_courses = node_load_multiple(array(), array(
    'type' => "course",
  ));
  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 "manager":
          og_role_grant_permissions($index, array(
            'create h5p_content content',
          ));
          og_role_grant_permissions($index, array(
            'update own h5p_content content',
          ));
          og_role_grant_permissions($index, array(
            'update any h5p_content content',
          ));
          og_role_grant_permissions($index, array(
            'delete own h5p_content content',
          ));
          og_role_grant_permissions($index, array(
            'delete any h5p_content content',
          ));
          og_role_grant_permissions($index, array(
            'create opigno_scorm_quiz_question content',
          ));
          og_role_grant_permissions($index, array(
            'update own opigno_scorm_quiz_question content',
          ));
          og_role_grant_permissions($index, array(
            'update any opigno_scorm_quiz_question content',
          ));
          og_role_grant_permissions($index, array(
            'delete own opigno_scorm_quiz_question content',
          ));
          og_role_grant_permissions($index, array(
            'delete any opigno_scorm_quiz_question content',
          ));
          break;
        case "teacher":
          og_role_grant_permissions($index, array(
            'create h5p_content content',
          ));
          og_role_grant_permissions($index, array(
            'update own h5p_content content',
          ));
          og_role_grant_permissions($index, array(
            'delete own h5p_content content',
          ));
          og_role_grant_permissions($index, array(
            'create opigno_scorm_quiz_question content',
          ));
          og_role_grant_permissions($index, array(
            'update own opigno_scorm_quiz_question content',
          ));
          og_role_grant_permissions($index, array(
            'delete own opigno_scorm_quiz_question content',
          ));
          break;
      }
    }
  }
  $distpath = drupal_get_path("profile", "opigno_lms");
  if (!empty($distpath)) {

    // Installs H5P Library
    $path = file_get_contents($distpath . "/h5plib/libraries.h5p");
    $temporary_file_path = 'public://' . variable_get('h5p_default_path', 'h5p') . '/temp/' . uniqid('h5p-');
    file_prepare_directory($temporary_file_path, FILE_CREATE_DIRECTORY);
    $temporary_file_name = $temporary_file_path . "/libraries.h5p";
    $file = file_save_data($path, $temporary_file_name, FILE_EXISTS_REPLACE);
    $_SESSION['h5p_upload'] = drupal_realpath($file->uri);
    $_SESSION['h5p_upload_folder'] = drupal_realpath($temporary_file_path);
    $validator = _h5p_get_instance('validator');
    $validator
      ->isValidPackage(TRUE, FALSE);
    $h5p_core = _h5p_get_instance('storage');
    $h5p_core
      ->savePackage(NULL, NULL, TRUE);
    unset($_SESSION['h5p_upload'], $_SESSION['h5p_upload_folder']);
  }
}