You are here

public function SaveMeetingRecordsQueue::processItem in Opigno Moxtra 8

Same name and namespace in other branches
  1. 3.x src/Plugin/QueueWorker/SaveMeetingRecordsQueue.php \Drupal\opigno_moxtra\Plugin\QueueWorker\SaveMeetingRecordsQueue::processItem()

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

src/Plugin/QueueWorker/SaveMeetingRecordsQueue.php, line 131

Class

SaveMeetingRecordsQueue
Save meeting records on CRON run.

Namespace

Drupal\opigno_moxtra\Plugin\QueueWorker

Code

public function processItem($data) {

  // Get group.
  $group = $this->entityTypeManager
    ->getStorage('group')
    ->load($data->gid);
  if (!isset($group)) {
    return;
  }

  // Check group type.
  if ($group
    ->getGroupType()
    ->id() !== 'learning_path') {
    return;
  }

  /** @var \Drupal\opigno_moxtra\MeetingInterface[] $meetings */
  $meetings = $group
    ->getContentEntities('opigno_moxtra_meeting_group');
  foreach ($meetings as $meeting) {
    $owner_id = $meeting
      ->getOwnerId();

    // Check the live meeting status.
    $session_key = $meeting
      ->getSessionKey();
    if (empty($session_key)) {
      continue;
    }
    $info = $this->moxtraService
      ->getMeetingInfo($owner_id, $session_key);
    $status = $info['data']['status'];
    if ($status !== 'SESSION_ENDED') {
      continue;
    }

    // Check live meeting has recordings.
    $info = $this->moxtraService
      ->getMeetingRecordingInfo($owner_id, $session_key);
    if ((int) $info['data']['count'] === 0) {
      continue;
    }
    $recordings = array_map(function ($recording) {
      return $recording['download_url'];
    }, $info['data']['recordings']);

    // Get the recordings folder.
    $group_id = $group
      ->id();
    $folder = $this
      ->getRecordingsFolder($group_id);
    if (!isset($folder)) {
      continue;
    }

    // Get the files.
    $fids = \Drupal::entityQuery('media')
      ->condition('bundle', 'tft_file')
      ->condition('tft_folder.target_id', $folder
      ->id())
      ->execute();

    /** @var \Drupal\media\MediaInterface[] $files */
    $files = Media::loadMultiple($fids);
    foreach ($recordings as $recording) {

      // Check that file for this live meeting recording
      // is not already exists.
      $exists = FALSE;
      foreach ($files as $file) {
        if (!$file
          ->hasField('opigno_moxtra_recording_link')) {
          continue;
        }
        $link = $file
          ->get('opigno_moxtra_recording_link')
          ->getValue();
        if (!empty($link)) {
          $url = $link[0]['uri'];
          if ($url === $recording) {
            $exists = TRUE;
            break;
          }
        }
      }

      // Save the live meeting recording.
      if (!$exists) {
        $members = $meeting
          ->getMembersIds();
        if (empty($members)) {
          $training = $meeting
            ->getTraining();
          if (isset($training)) {
            $members = array_map(function ($membership) {

              /** @var \Drupal\group\GroupMembership $membership */
              return $membership
                ->getUser()
                ->id();
            }, $training
              ->getMembers());
          }
        }
        $file = Media::create([
          'bundle' => 'tft_file',
          'name' => $meeting
            ->label(),
          'uid' => $owner_id,
          'opigno_moxtra_recording_link' => [
            'uri' => $recording,
          ],
          'tft_folder' => [
            'target_id' => $folder
              ->id(),
          ],
          'tft_members' => $members,
        ]);
        return $file
          ->save();
      }
    }
  }
}