You are here

public static function LatestActivity::insertGroupActivity in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Entity/LatestActivity.php \Drupal\opigno_learning_path\Entity\LatestActivity::insertGroupActivity()

Creates or updates latest group activity.

Parameters

int $training_id: The training ID.

int $module_id: The module ID.

$user_id: The user ID.

Return value

\Drupal\opigno_learning_path\LatestActivityInterface Created or updated entity.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to LatestActivity::insertGroupActivity()
opigno_learning_path_entity_update in ./opigno_learning_path.module
Implements hook_entity_update().

File

src/Entity/LatestActivity.php, line 201

Class

LatestActivity
Defines the Latest Activity entity.

Namespace

Drupal\opigno_learning_path\Entity

Code

public static function insertGroupActivity($training_id, $module_id, $user_id) {
  $query = \Drupal::entityQuery('opigno_latest_group_activity');
  $ids = $query
    ->condition('training', $training_id)
    ->condition('module', $module_id)
    ->condition('uid', $user_id)
    ->sort('timestamp', 'DESC')
    ->range(0, 1)
    ->execute();
  $id = reset($ids);
  if ($id !== FALSE) {
    $activity = LatestActivity::load($id);
  }
  else {
    $activity = LatestActivity::create();
    $activity
      ->setTrainingId($training_id);
    $activity
      ->setModuleId($module_id);
    $activity
      ->setUserId($user_id);
  }
  $timestamp = \Drupal::time()
    ->getRequestTime();
  $activity
    ->setTimestamp($timestamp);
  $activity
    ->save();
  return $activity;
}