You are here

public function UserModuleStatus::getRandomActivitiesMaxScore in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Entity/UserModuleStatus.php \Drupal\opigno_module\Entity\UserModuleStatus::getRandomActivitiesMaxScore()

Calculate max score for the Module with enabled "Random activities" option.

1 call to UserModuleStatus::getRandomActivitiesMaxScore()
UserModuleStatus::calculateMaxScore in src/Entity/UserModuleStatus.php

File

src/Entity/UserModuleStatus.php, line 410

Class

UserModuleStatus
Defines the User module status entity.

Namespace

Drupal\opigno_module\Entity

Code

public function getRandomActivitiesMaxScore() {
  $max_score = 0;

  /* @var $db_connection \Drupal\Core\Database\Connection */
  $db_connection = \Drupal::service('database');

  /* @var $module \Drupal\opigno_module\Entity\OpignoModule */
  $module = $this
    ->getModule();
  $random_activities_count = $module
    ->getRandomActivitiesCount();

  // Get activities from answers.
  $activities_query = $db_connection
    ->select('opigno_answer_field_data', 'oafd')
    ->fields('oafd', [
    'activity',
  ])
    ->condition('oafd.user_id', $this
    ->getOwnerId())
    ->condition('oafd.user_module_status', $this
    ->id())
    ->range(0, $random_activities_count);
  $results = $activities_query
    ->execute()
    ->fetchAllAssoc('activity');

  // Get max score for activities.
  if ($aids = array_keys($results)) {
    $score_query = $db_connection
      ->select('opigno_module_relationship', 'omr')
      ->fields('omr', [
      'max_score',
      'activity_status',
    ])
      ->condition('omr.parent_id', $module
      ->id())
      ->condition('omr.child_id', $aids, 'IN');
    $score_results = $score_query
      ->execute()
      ->fetchAll();
    if (!empty($score_results)) {
      foreach ($score_results as $score_result) {
        if ($score_result->activity_status == 1) {
          $max_score += $score_result->max_score;
        }
      }
    }
  }
  return $max_score;
}