View source
<?php
namespace Drupal\opigno_moxtra\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\media\Entity\Media;
use Drupal\opigno_moxtra\MoxtraServiceInterface;
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
class SaveMeetingRecordsQueue extends QueueWorkerBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $moxtraService;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityTypeManagerInterface $entity_type_manager, MoxtraServiceInterface $moxtra_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->moxtraService = $moxtra_service;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('opigno_moxtra.moxtra_api'));
}
protected function getRecordingsFolder($group_id) {
$results =& drupal_static(__FUNCTION__);
if (isset($results[$group_id])) {
return $results[$group_id];
}
$group_folder_tid = _tft_get_group_tid($group_id);
if ($group_folder_tid === NULL) {
return NULL;
}
$recording_folder_name = $this
->t('Recorded Live Meetings');
$recording_folder = NULL;
$storage = $this->entityTypeManager
->getStorage('taxonomy_term');
$children = $storage
->loadChildren($group_folder_tid);
foreach ($children as $child) {
if ($child
->label() === (string) $recording_folder_name) {
$recording_folder = $child;
break;
}
}
if (!isset($recording_folder)) {
$recording_folder = Term::create([
'vid' => 'tft_tree',
'name' => $recording_folder_name,
'parent' => $group_folder_tid,
]);
$recording_folder
->save();
}
return $results[$group_id] = $recording_folder;
}
public function processItem($data) {
$group = $this->entityTypeManager
->getStorage('group')
->load($data->gid);
if (!isset($group)) {
return;
}
if ($group
->getGroupType()
->id() !== 'learning_path') {
return;
}
$meetings = $group
->getContentEntities('opigno_moxtra_meeting_group');
foreach ($meetings as $meeting) {
$owner_id = $meeting
->getOwnerId();
$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;
}
$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']);
$group_id = $group
->id();
$folder = $this
->getRecordingsFolder($group_id);
if (!isset($folder)) {
continue;
}
$fids = \Drupal::entityQuery('media')
->condition('bundle', 'tft_file')
->condition('tft_folder.target_id', $folder
->id())
->execute();
$files = Media::loadMultiple($fids);
foreach ($recordings as $recording) {
$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;
}
}
}
if (!$exists) {
$members = $meeting
->getMembersIds();
if (empty($members)) {
$training = $meeting
->getTraining();
if (isset($training)) {
$members = array_map(function ($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();
}
}
}
}
}