You are here

function opigno_tincan_modules_user_module_status_presave in Opigno TinCan API 8

Same name and namespace in other branches
  1. 3.x modules/opigno_tincan_modules/opigno_tincan_modules.module \opigno_tincan_modules_user_module_status_presave()

Implements hook_ENTITY_TYPE_presave().

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

modules/opigno_tincan_modules/opigno_tincan_modules.module, line 118
Contains opigno_tincan_modules.module.

Code

function opigno_tincan_modules_user_module_status_presave(EntityInterface $entity) {

  /****
   * - When user finish a module
   * Actor: user
   * Verb: xAPI/passed || xAPI/failed
   * Object: xAPI/module
   * Result: Get final module result
   * Context: Course or Learning Path
   */

  // Check if Tincan PHP library is installed.
  $has_library = opigno_tincan_api_tincanphp_is_installed();
  if (!$has_library) {
    return FALSE;
  }

  /* @var $user_module_status UserModuleStatus */
  $user_module_status = $entity;
  $is_finished = $user_module_status
    ->get('finished')
    ->getValue()[0]['value'];
  if ($is_finished) {

    // If there is a parent, set it.
    $gid = OpignoGroupContext::getCurrentGroupId();
    if (!$gid) {
      return FALSE;
    }

    // Know if the user has passed or failed this module.
    $cid = OpignoGroupContext::getCurrentGroupContentId();
    if (empty($cid)) {
      return FALSE;
    }
    $group_content = OpignoGroupManagedContent::loadByProperties([
      'id' => $cid,
    ]);
    $group_content = reset($group_content);
    $required_score = $group_content
      ->getSuccessScoreMin();
    $user_percent_score = $user_module_status
      ->getScore();
    $passed = $user_percent_score >= $required_score;

    // Statement creation.
    $opigno_module = $user_module_status
      ->getModule();
    $statement = OpignoTinCanApiStatements::statementBaseCreation($passed ? OpignoTincanApiTinCanVerbs::$passed : OpignoTincanApiTinCanVerbs::$failed, OpignoTincanApiTinCanActivityDefinitionTypes::$module, $opigno_module);
    if ($statement === FALSE) {
      return FALSE;
    }

    // Context creation.
    $context = new Context();
    $managed_content = OpignoGroupManagedContent::loadByProperties([
      'group_id' => $gid,
    ]);
    foreach ($managed_content as $content) {
      $content_type_id = $content
        ->getGroupContentTypeId();
      $id = $content
        ->getEntityId();

      // Check if module is a part of Learning Path.
      if ($opigno_module
        ->id() == $id) {
        $parent_id = $gid;
        OpignoTinCanApiStatements::contextSetGrouping($context, [
          $parent_id,
        ], OpignoTincanApiTinCanActivityDefinitionTypes::$group);
        break;
      }
      elseif ($content_type_id == 'ContentTypeCourse') {
        $course_id = _opigno_tincan_modules_is_module_in_course($content, $opigno_module
          ->id());
        if ($course_id) {
          $parent_id = $course_id;
          OpignoTinCanApiStatements::contextSetGrouping($context, [
            $parent_id,
          ], OpignoTincanApiTinCanActivityDefinitionTypes::$course);
          break;
        }
      }
    }
    $language_id = $opigno_module
      ->language()
      ->getId();
    OpignoTinCanApiStatements::contextSetLanguage($context, $language_id);

    // Set result.
    $score_max = $user_module_status
      ->getMaxScore();
    $start_time = $user_module_status
      ->get('started')->value;
    $end_time = $user_module_status
      ->get('finished')->value;
    $duration_s = $end_time - $start_time;

    // Method calculateScore() returns raw result (not in percents).
    $user_score = $user_module_status
      ->calculateScore();

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

    // TODO: issue: $user_score can be grater than $max_score (no logic).
    if ($user_score > $score_max) {
      $score_max = $user_score;
    }

    // must be deleted or implemented in other way.
    $response = NULL;
    OpignoTinCanApiStatements::setResult($statement, $user_score, $score_max, NULL, $passed, $response, $duration_s);

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

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