You are here

function opigno_statistics_update_command in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x opigno_statistics.drush.inc \opigno_statistics_update_command()

Callback for the opigno-statistics-update command.

1 string reference to 'opigno_statistics_update_command'
opigno_statistics_drush_command in ./opigno_statistics.drush.inc
Implements custom drush command.

File

./opigno_statistics.drush.inc, line 42
Opigno Statistics drush functions.

Code

function opigno_statistics_update_command($uid = NULL, $gid = NULL) {
  if ($uid && !$gid || !$uid && $gid) {
    $message = 'Should be two params - user ID and a training ID.';
    \Drupal::logger('opigno_statistics')
      ->error($message);
    return 2;
  }
  $achievements_table = 'opigno_learning_path_achievements';
  $achievements_steps_table = 'opigno_learning_path_step_achievements';
  $db_connection = \Drupal::service('database');
  if ($gid) {
    $ids = [
      $gid,
    ];
  }
  else {
    $ids = $db_connection
      ->select('groups_field_data', 'g')
      ->fields('g', [
      'id',
    ])
      ->condition('type', 'learning_path')
      ->execute()
      ->fetchCol();
  }
  if ($ids) {
    $groups = Group::loadMultiple($ids);
    if ($groups) {
      if ($uid && $gid) {
        $db_connection
          ->delete($achievements_table)
          ->condition('uid', $uid)
          ->condition('gid', $gid)
          ->execute();
        $db_connection
          ->delete($achievements_steps_table)
          ->condition('uid', $uid)
          ->condition('gid', $gid)
          ->execute();
      }
      else {
        $db_connection
          ->truncate($achievements_table)
          ->execute();
        $db_connection
          ->truncate($achievements_steps_table)
          ->execute();
      }
      foreach ($groups as $group) {

        // Each training.
        $gid = $group
          ->id();
        $message = 'Group (' . $gid . ') - "' . $group
          ->label() . '"';
        \Drupal::logger('opigno_statistics')
          ->notice($message);
        \Drupal::messenger()
          ->addMessage($message, 'status');
        if ($members = $group
          ->getMembers()) {
          foreach ($members as $group_membership) {

            // Each training member user.
            $user = $group_membership
              ->getUser();
            $member_uid = $user
              ->id();
            if ($uid && $uid != $member_uid) {
              continue;
            }
            $message = ' - user (' . $member_uid . ') - "' . $user
              ->getUsername() . '"';
            \Drupal::logger('opigno_statistics')
              ->notice($message);
            \Drupal::messenger()
              ->addMessage($message, 'status');
            try {
              opigno_learning_path_save_achievements($gid, $member_uid);
            } catch (\Exception $e) {
              \Drupal::logger('itsp_statistics')
                ->error($e
                ->getMessage());
              \Drupal::messenger()
                ->addMessage($e
                ->getMessage(), 'error');
            }
            if ($steps = opigno_learning_path_get_all_steps($gid, $member_uid)) {
              foreach ($steps as $step) {

                // Each training steps.
                try {

                  // Save current step parent achievements.
                  $parent_id = isset($current_step['parent']) ? opigno_learning_path_save_step_achievements($gid, $member_uid, $step['parent']) : 0;

                  // Save current step achievements.
                  opigno_learning_path_save_step_achievements($gid, $member_uid, $step, $parent_id);
                } catch (\Exception $e) {
                  \Drupal::logger('opigno_statistics')
                    ->error($e
                    ->getMessage());
                  \Drupal::messenger()
                    ->addMessage($e
                    ->getMessage(), 'error');
                }
              }
            }
          }
        }
      }
    }
  }
}