OpignoTincanLiveMeeting.php in Opigno TinCan API 8
File
modules/opigno_tincan_live_meeting/src/EventSubscriber/OpignoTincanLiveMeeting.php
View source
<?php
namespace Drupal\opigno_tincan_live_meeting\EventSubscriber;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\opigno_moxtra\Entity\Meeting;
use Drupal\opigno_moxtra\MoxtraServiceInterface;
use Drupal\opigno_tincan_api\OpignoTinCanApiStatements;
use Drupal\opigno_tincan_api\OpignoTincanApiTinCanActivityDefinitionTypes;
use Drupal\opigno_tincan_api\OpignoTincanApiTinCanVerbs;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use TinCan\Context;
class OpignoTincanLiveMeeting implements EventSubscriberInterface {
protected $user;
protected $route;
protected $moxtraService;
public function __construct(AccountInterface $current_user, RouteMatchInterface $route, MoxtraServiceInterface $moxtra_service) {
$this->user = $current_user;
$this->route = $route;
$this->moxtraService = $moxtra_service;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('current_user'), $container
->get('current_route_match'), $container
->get('opigno_moxtra.moxtra_api'));
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'tincanLiveMeeting',
];
return $events;
}
public function tincanLiveMeeting(Event $event) {
$has_library = opigno_tincan_api_tincanphp_is_installed();
if (!$has_library) {
return;
}
$route_name = $this->route
->getRouteName();
if ($route_name != 'opigno_moxtra.meeting') {
return;
}
$meeting = $this->route
->getParameter('opigno_moxtra_meeting');
$was_send = $this
->wasStatementSend($meeting);
if ($was_send) {
return;
}
$owner_id = $meeting
->getOwnerId();
$session_key = $meeting
->getSessionKey();
$info = $this->moxtraService
->getMeetingInfo($owner_id, $session_key);
$status = !empty($info['data']) ? $info['data']['status'] : FALSE;
if ($status == 'SESSION_ENDED') {
$participants = $info['data']['participants'];
$is_participant = FALSE;
foreach ($participants as $participant) {
if ($participant['unique_id'] == $this->user
->id()) {
$is_participant = TRUE;
}
}
if ($is_participant) {
$this
->createAndSendTincanStatementsForMeeting($meeting, $info);
}
}
}
protected function createAndSendTincanStatementsForMeeting(Meeting $meeting, array $response_data) {
$statement = OpignoTinCanApiStatements::statementBaseCreation(OpignoTincanApiTinCanVerbs::$attended, OpignoTincanApiTinCanActivityDefinitionTypes::$meeting, $meeting);
if ($statement === FALSE) {
return;
}
$context = new Context();
$group = $meeting
->getTraining();
$parent_ids = [
$group
->id(),
];
OpignoTinCanApiStatements::contextSetGrouping($context, $parent_ids);
OpignoTinCanApiStatements::contextSetLanguage($context, $meeting
->language()
->getId());
$start_date = $response_data['data']['starts'];
$end_date = $response_data['data']['ends'];
$duration_s = strtotime($end_date) - strtotime($start_date);
OpignoTinCanApiStatements::setResult($statement, NULL, NULL, NULL, NULL, NULL, abs($duration_s));
$statement
->setContext($context);
$result = OpignoTinCanApiStatements::sendStatement($statement);
if ($result) {
$this
->saveSendingStatement($meeting);
}
}
protected function wasStatementSend(Meeting $meeting) {
$db_connection = \Drupal::service('database');
$user = $this->user;
$query = $db_connection
->select('opigno_tincan_live_meeting', 'otl')
->fields('otl')
->condition('otl.uid', $user
->id())
->condition('otl.meeting_id', $meeting
->id());
$result = $query
->execute()
->fetchObject();
if ($result) {
return TRUE;
}
else {
return FALSE;
}
}
protected function saveSendingStatement(Meeting $meeting) {
$db_connection = \Drupal::service('database');
$user = $this->user;
$query = $db_connection
->insert('opigno_tincan_live_meeting')
->fields([
'uid' => $user
->id(),
'meeting_id' => $meeting
->id(),
]);
$query
->execute();
}
}