You are here

opigno_tincan_activities.module in Opigno TinCan API 3.x

Same filename and directory in other branches
  1. 8 modules/opigno_tincan_activities/opigno_tincan_activities.module

File

modules/opigno_tincan_activities/opigno_tincan_activities.module
View source
<?php

/**
 * @file
 * Contains opigno_tincan_activities.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\opigno_module\Entity\OpignoActivityInterface;
use Drupal\opigno_module\Entity\OpignoAnswerInterface;
use Drupal\opigno_group_manager\OpignoGroupContext;
use Drupal\opigno_tincan_api\OpignoTinCanApiStatements;
use Drupal\opigno_tincan_api\OpignoTincanApiTinCanActivityDefinitionTypes;
use Drupal\opigno_tincan_api\OpignoTincanApiTinCanInteractionTypes;
use Drupal\opigno_tincan_api\OpignoTincanApiTinCanVerbs;
use TinCan\Context;
use TinCan\Statement;

/**
 * Implements hook_help().
 */
function opigno_tincan_activities_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the opigno_tincan_api_activities module.
    case 'help.page.opigno_tincan_activities':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Functionality for sending tincan statements after user answered the activity.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 *
 * At this stage Tincan Api send statements after user answered activity.
 */
function opigno_tincan_activities_opigno_answer_insert(EntityInterface $entity) {

  // Check if Tincan PHP library is installed.
  $has_library = opigno_tincan_api_tincanphp_is_installed();
  if (!$has_library) {
    return;
  }
  $route = \Drupal::routeMatch();
  if ($route
    ->getRouteName() == 'opigno_module.group.answer_form') {
    $answer = $entity;

    // Create and send tincan statements.
    _opigno_tincan_activities_create_and_send_statements($answer);
  }
}

/**
 * The main function for building tincan statement object for opigno activity.
 *
 * @param \Drupal\opigno_module\Entity\OpignoAnswerInterface $answer
 *   Answer object.
 *
 * @throws \Exception
 */
function _opigno_tincan_activities_create_and_send_statements(OpignoAnswerInterface $answer) {
  $opigno_module = $answer
    ->getModule();
  $activity = $answer
    ->getActivity();
  $statement_properties = opigno_tincan_activities_get_verb_and_definition_type($activity
    ->getType());

  // Statement creation.
  $statement = OpignoTinCanApiStatements::statementBaseCreation($statement_properties['verb'], $statement_properties['definition_type'], $activity);
  if ($statement === FALSE) {
    return;
  }

  // Learning path is the main group for activities.
  $learning_path_id = OpignoGroupContext::getCurrentGroupId();

  // Context creation.
  $context = new Context();
  OpignoTinCanApiStatements::contextSetParents($context, [
    $opigno_module
      ->id(),
  ], OpignoTincanApiTinCanActivityDefinitionTypes::$module);
  OpignoTinCanApiStatements::contextSetGrouping($context, [
    $learning_path_id,
  ], OpignoTincanApiTinCanActivityDefinitionTypes::$group);
  $language_id = $opigno_module
    ->language()
    ->getId();
  OpignoTinCanApiStatements::contextSetLanguage($context, $language_id);

  // Set result.
  _opigno_tincan_activities_set_results($statement, $activity, $answer);

  // Set statement context.
  $statement
    ->setContext($context);

  // Sending statement.
  OpignoTinCanApiStatements::sendStatement($statement);
}

/**
 * Set answer result to tincan statement.
 *
 * @param \TinCan\Statement $statement
 *   Tincan statement object.
 * @param \Drupal\opigno_module\Entity\OpignoActivityInterface $activity
 *   Opigno activity object.
 * @param \Drupal\opigno_module\Entity\OpignoAnswerInterface $answer
 *   Opigno answer object.
 */
function _opigno_tincan_activities_set_results(Statement $statement, OpignoActivityInterface $activity, OpignoAnswerInterface $answer) {
  $opigno_module = $answer
    ->getModule();

  /** @var Drupal\Core\Entity\ContentEntityType $activity_type */
  $activity_type = $activity
    ->getType();

  // Max score has every activity. Can be 0.
  $score_max = _opigno_tincan_activities_get_max_score($activity, $opigno_module);

  // Get user score in raw points.
  if ($answer
    ->isEvaluated()) {
    $user_score = $answer
      ->getScore();
  }
  else {
    $user_score = NULL;
  }

  // Raw score can not be negative.
  $user_score = $user_score > 0 ? $user_score : 0;

  // Max score must always must be grater than user score.
  if ($user_score > $score_max) {
    $score_max = $user_score;
  }

  /******
   ** Create result for each activity type separately.
   ***/
  if ($activity_type == 'opigno_file_upload') {

    // Set result into statement.
    OpignoTinCanApiStatements::setResult($statement, $user_score, $score_max, NULL, NULL, NULL, NULL);

    // Set the interaction type properties
    // TODO: maybe there is a reason to move this function to opigno_tincan_api.
    $objectDefinition = _opigno_tincan_activities_get_object_definition($statement);
    $objectDefinition
      ->setInteractionType(OpignoTincanApiTinCanInteractionTypes::$other);
  }
  elseif ($activity_type == 'opigno_h5p') {

    // Set result into statement.
    OpignoTinCanApiStatements::setResult($statement, $user_score, $score_max, NULL, NULL, NULL, NULL);

    // Set the interaction type properties
    // TODO: maybe there is a reason to move this function to opigno_tincan_api.
    $objectDefinition = _opigno_tincan_activities_get_object_definition($statement);
    $objectDefinition
      ->setInteractionType(OpignoTincanApiTinCanInteractionTypes::$other);
  }
  elseif ($activity_type == 'opigno_long_answer') {

    // Get user answer from field.
    $response = $answer
      ->get('opigno_body')
      ->getString();

    // Set result into statement.
    OpignoTinCanApiStatements::setResult($statement, $user_score, $score_max, NULL, NULL, $response, NULL);

    // Set the interaction type properties
    // TODO: maybe there is a reason to move this function to opigno_tincan_api.
    $objectDefinition = _opigno_tincan_activities_get_object_definition($statement);
    $objectDefinition
      ->setInteractionType(OpignoTincanApiTinCanInteractionTypes::$long_fill_in);
  }
  elseif ($activity_type == 'opigno_scorm') {

    // Set result into statement.
    OpignoTinCanApiStatements::setResult($statement, $user_score, $score_max, NULL, NULL, NULL, NULL);

    // Set the interaction type properties
    // TODO: maybe there is a reason to move this function to opigno_tincan_api.
    $objectDefinition = _opigno_tincan_activities_get_object_definition($statement);
    $objectDefinition
      ->setInteractionType(OpignoTincanApiTinCanInteractionTypes::$other);
  }
  elseif ($activity_type == 'opigno_slide') {

    // Set result into statement.
    OpignoTinCanApiStatements::setResult($statement, $user_score, $score_max, NULL, NULL, NULL, NULL);

    // Set the interaction type properties
    // TODO: maybe there is a reason to move this function to opigno_tincan_api.
    $objectDefinition = _opigno_tincan_activities_get_object_definition($statement);
    $objectDefinition
      ->setInteractionType(OpignoTincanApiTinCanInteractionTypes::$other);
  }
  elseif ($activity_type == 'opigno_tincan') {

    // Set result into statement.
    OpignoTinCanApiStatements::setResult($statement, $user_score, $score_max, NULL, NULL, NULL, NULL);

    // Set the interaction type properties
    // TODO: maybe there is a reason to move this function to opigno_tincan_api.
    $objectDefinition = _opigno_tincan_activities_get_object_definition($statement);
    $objectDefinition
      ->setInteractionType(OpignoTincanApiTinCanInteractionTypes::$other);
  }
  elseif ($activity_type == 'opigno_video') {

    // Set result into statement.
    OpignoTinCanApiStatements::setResult($statement, $user_score, $score_max, NULL, NULL, NULL, NULL);

    // Set the interaction type properties
    // TODO: maybe there is a reason to move this function to opigno_tincan_api.
    $objectDefinition = _opigno_tincan_activities_get_object_definition($statement);
    $objectDefinition
      ->setInteractionType(OpignoTincanApiTinCanInteractionTypes::$other);
  }
}

/**
 * Returns tincan activities max score.
 */
function _opigno_tincan_activities_get_max_score($opigno_activity, $opigno_module) {

  /* @var $db_connection \Drupal\Core\Database\Connection */
  $db_connection = \Drupal::service('database');
  $max_score = 0;
  $score_query = $db_connection
    ->select('opigno_module_relationship', 'omr')
    ->fields('omr', [
    'max_score',
  ])
    ->condition('omr.parent_id', $opigno_module
    ->id())
    ->condition('omr.parent_vid', $opigno_module
    ->getRevisionId())
    ->condition('omr.child_id', $opigno_activity
    ->id())
    ->condition('omr.child_vid', $opigno_activity
    ->getRevisionId());
  $score_result = $score_query
    ->execute()
    ->fetchObject();
  if ($score_result) {
    $max_score = $score_result->max_score;
  }
  return $max_score;
}

/**
 * Returns object definition.
 *
 * @param \TinCan\Statement $statement
 *   Statement object.
 *
 * @return \Tincan\ActivityDefinition
 *   Object definition.
 */
function _opigno_tincan_activities_get_object_definition(Statement $statement) {
  if (!$statement
    ->getObject()) {
    $statement
      ->setObject([]);
  }
  if (!$statement
    ->getObject()
    ->getDefinition()) {
    $statement
      ->getObject()
      ->setDefinition([]);
  }
  return $statement
    ->getObject()
    ->getDefinition();
}

/**
 * Implements hook_page_attachments().
 */
function opigno_tincan_activities_page_attachments(array &$attachments) {
  $route = \Drupal::routeMatch();
  if ($route
    ->getRouteName() == 'opigno_module.group.answer_form') {
    $attachments['#attached']['library'][] = 'opigno_tincan_activities/opigno_h5p_activities';
    $url = Url::fromRoute('opigno_tincan_activities.h5p_tincan_relay')
      ->toString();
    $attachments['#attached']['drupalSettings']['h5pTincanRelay']['relayUrl'] = $url;
  }
}

/**
 * Returns activity type verb and definition type.
 */
function opigno_tincan_activities_get_verb_and_definition_type($activity_type) {
  $properties = [];
  switch ($activity_type) {
    case 'opigno_file_upload':
      $verb = OpignoTincanApiTinCanVerbs::$attached;
      $definition_type = OpignoTincanApiTinCanActivityDefinitionTypes::$file;
      break;
    case 'opigno_slide':
      $verb = OpignoTincanApiTinCanVerbs::$viewed;
      $definition_type = OpignoTincanApiTinCanActivityDefinitionTypes::$media;
      break;
    case 'opigno_video':
      $verb = OpignoTincanApiTinCanVerbs::$played;
      $definition_type = OpignoTincanApiTinCanActivityDefinitionTypes::$video;
      break;
    default:
      $verb = OpignoTincanApiTinCanVerbs::$answered;
      $definition_type = OpignoTincanApiTinCanActivityDefinitionTypes::$question;
      break;
  }
  $properties['verb'] = $verb;
  $properties['definition_type'] = $definition_type;
  return $properties;
}

Functions

Namesort descending Description
opigno_tincan_activities_get_verb_and_definition_type Returns activity type verb and definition type.
opigno_tincan_activities_help Implements hook_help().
opigno_tincan_activities_opigno_answer_insert Implements hook_ENTITY_TYPE_insert().
opigno_tincan_activities_page_attachments Implements hook_page_attachments().
_opigno_tincan_activities_create_and_send_statements The main function for building tincan statement object for opigno activity.
_opigno_tincan_activities_get_max_score Returns tincan activities max score.
_opigno_tincan_activities_get_object_definition Returns object definition.
_opigno_tincan_activities_set_results Set answer result to tincan statement.