You are here

public function OpignoTincanQuestionTypeQuestion::getAnsweringForm in Opigno TinCan Question Type 7

Get the form through which the user will answer the question.

Parameters

array $form_state: The FAPI form_state array.

int $rid: The result id.

Return value

array Must return a FAPI array.

File

includes/opigno_tincan_question_type.question.inc, line 233
This file contains the class OpignoTincanQuestionTypeQuestion.

Class

OpignoTincanQuestionTypeQuestion
This class goal is to provide specific TinCan question information.

Code

public function getAnsweringForm(array $form_state, $rid) {
  global $user, $base_path;

  // Init the variables.
  $this
    ->getNodeProperties();
  $markup = '';
  $launch_file = $this->nodeProperties['launch_filename'];

  // Check first if the TinCanPHP library is installed
  // If not, return nothing but an error message
  $libraries = libraries_get_libraries();
  if (!isset($libraries['TinCanPHP'])) {
    drupal_set_message(t('Please install the !tincanphp_library in the <em>sites/all/library/TinCanPHP</em> folder.', array(
      '!tincanphp_library' => l(t('TinCanPHP library'), 'https://github.com/RusticiSoftware/TinCanPHP/releases'),
    )), 'error');
    return array();
  }

  // Connect to the LRS.
  $lrs_endpoint = variable_get('opigno_tincan_api_endpoint', '');
  $lrs_username = variable_get('opigno_tincan_api_username', '');
  $lrs_password = variable_get('opigno_tincan_api_password', '');
  if ((empty($lrs_endpoint) || empty($lrs_username) || empty($lrs_password)) && drupal_valid_path('admin/opigno/system/tincan')) {
    drupal_set_message(t('The module Opigno TinCan API is not configured. Go to !url', array(
      '!url' => l(t('the settings page.'), 'admin/opigno/system/tincan'),
    )));
    unset($_SESSION[self::SESSIONKEY_REGISTRATION]);
  }
  else {

    // If connected to the LRS, create the URI parameters.
    $auth = 'Basic ' . base64_encode($lrs_username . ':' . $lrs_password);
    $actor = array(
      'mbox_sha1sum' => sha1('mailto:' . $user->mail),
      'name' => $user->name,
    );

    // Get the registration UUID that will be used to recognise this answering
    // form. If the registration UUID does not exist, create one.
    $registration = OpignoTincanQuestionTypeResponse::getRegistration($rid);
    if ($registration === FALSE) {
      $registration = Util::getUUID();
    }
    $launch_file .= '?endpoint=' . rawurlencode($lrs_endpoint) . '&auth=' . rawurlencode($auth) . '&actor=' . rawurlencode(json_encode($actor)) . '&registration=' . rawurlencode($registration);
    $_SESSION[self::SESSIONKEY_REGISTRATION] = $registration;
  }

  // Get the file and construct his URI.
  $file = file_load($this->nodeProperties['fid']);
  $filename = $this
    ->getPackageName($file);
  $url = file_create_url(self::PATH_PUBLIC_EXTRACTED_PACKAGE_FOLDER . $filename);
  $markup .= '
      <iframe
        style="width: 100%; min-height: 800px; border: 0;"
        src="' . $url . '/' . $launch_file . '">
      </iframe>';

  // Construct the form.
  $form = parent::getAnsweringForm($form_state, $rid);
  $form['iframe'] = array(
    '#type' => 'markup',
    '#markup' => $markup,
  );
  return $form;
}