Skip to content

Commit

Permalink
fix: add new document to save recording info before import on pumukit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurujai committed Dec 16, 2024
1 parent 73ee662 commit 8fd5f37
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 53 deletions.
51 changes: 20 additions & 31 deletions Command/SyncMediaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Pumukit\BlackboardBundle\Services\CollaborateAPICourseRecordings;
use Pumukit\BlackboardBundle\Services\CollaborateAPIRecording;
use Pumukit\BlackboardBundle\Services\CollaborateAPISessionSearch;
use Pumukit\BlackboardBundle\Services\CollaborateCreateRecording;
use Pumukit\BlackboardBundle\Services\LearnAPIAuth;
use Pumukit\BlackboardBundle\Services\LearnAPICourse;
use Pumukit\BlackboardBundle\ValueObject\CollaborateRecording;
Expand All @@ -21,6 +22,7 @@ class SyncMediaCommand extends Command
private CollaborateAPICourseRecordings $collaborateAPICourseRecordings;
private CollaborateAPIRecording $collaborateAPIRecording;
private CollaborateAPISessionSearch $collaborateAPISessionSearch;
private CollaborateCreateRecording $collaborateCreateRecording;

public function __construct(
LearnAPIAuth $learnAPIAuth,
Expand All @@ -29,6 +31,7 @@ public function __construct(
CollaborateAPICourseRecordings $collaborateAPICourseRecordings,
CollaborateAPIRecording $collaborateAPIRecording,
CollaborateAPISessionSearch $collaborateAPISessionSearch,
CollaborateCreateRecording $collaborateCreateRecording,
string $name = null
) {
$this->learnAPIAuth = $learnAPIAuth;
Expand All @@ -37,6 +40,7 @@ public function __construct(
$this->collaborateAPICourseRecordings = $collaborateAPICourseRecordings;
$this->collaborateAPIRecording = $collaborateAPIRecording;
$this->collaborateAPISessionSearch = $collaborateAPISessionSearch;
$this->collaborateCreateRecording = $collaborateCreateRecording;

parent::__construct($name);
}
Expand All @@ -59,57 +63,42 @@ public function execute(InputInterface $input, OutputInterface $output): int

$output->writeln('<info>***** Getting list of courses *****</info>');
$courses = $this->learnAPICourse->getIdsFromCourses($learnToken);
$output->writeln('<info>[COLLABORATE] Courses found '. count($courses) .'</info>');
$output->writeln('<info>[COLLABORATE] Courses found '.count($courses).'</info>');

$courseRecordings = [];
$output->writeln('***** Getting recordings from course *****');
foreach ($courses as $course) {
$courseData = $this->collaborateAPICourseRecordings->getCourseRecordings($collaborateToken, $course);
if(isset($courseData['results']) && 0 !== count($courseData['results'])) {
if (isset($courseData['results']) && 0 !== count($courseData['results'])) {
$courseRecordings[$course] = $courseData['results'];
}
}

foreach ($courseRecordings as $key => $recordings) {
foreach ($recordings as $element) {
$output->writeln('<info>***** Getting data from recording '. $element['id'] .'</info>');
$output->writeln('<info>***** Getting data from recording '.$element['id'].'</info>');

$recording = $this->collaborateAPIRecording->getRecordingData($collaborateToken, $element['id']);
if(!$recording || !isset($recording['mediaDownloadUrl'])) {
if (!$recording || !isset($recording['mediaDownloadUrl'])) {
continue;
}

$downloadUrl = $recording['mediaDownloadUrl'];
$session = $this->collaborateAPISessionSearch->searchBySessionName($collaborateToken, $element['sessionName']);

//$enrollments = $this->collaborateAPISessionSearch->getEnrollmentsBySessionId($collaborateToken, '4d0d37cb688f431e973b6ac19ff10599');
$enrollments = $this->collaborateAPISessionSearch->getEnrollmentsBySessionId($collaborateToken, '2f74e38cb29042a4af2da9d9c8398caa');
var_dump($enrollments);die;
$collaborateRecording = CollaborateRecording::create($element['id'], $key, $element['sessionName'], $downloadUrl);
}
}

die;

$sessions = [];
foreach ($recordingSession as $key => $element) {
$session = $this->collaborateAPISessionSearch->searchBySessionName($collaborateToken, $element);
if(!isset($session['results'][0])) {
continue;
}
$sessions[] = $this->collaborateAPISessionSearch->getEnrollmentsBySessionId($collaborateToken, $session['results'][0]['id']);
}
// TODO: Obtener usuarios moderadores del curso. Crear serie del curso y agregar los moderadores. Autoprovisionar los usuarios.
// TODO: Get info about moderator to assing on PuMuKIT.
// $sessions = $this->collaborateAPISessionSearch->searchSessions($collaborateToken);
//
// $sessionsNames = array_column($sessions['results'], 'name');
// $index = array_search($element['sessionName'], $sessionsNames);
// $sessionId = $sessions['results'][$index]['id'];
//
// $enrollments = $this->collaborateAPISessionSearch->getEnrollmentsBySessionId($collaborateToken, $sessionId);

$users = [];
foreach ($sessions as $session) {
if(!isset($session['results'])) {
continue;
}
$collaborateRecording = CollaborateRecording::create($element['id'], $key, $downloadUrl, $element['sessionName']);
$recording = $this->collaborateCreateRecording->create($collaborateRecording);

foreach($session['results'] as $sessionResult) {
if($sessionResult['launchingRole'] === 'moderator') {
$users[] = $sessionResult['userId'];
}
$output->writeln('<info>Created new collaborate recording with ID '.$recording->recording().'</info>');
}
}

Expand Down
87 changes: 87 additions & 0 deletions Document/CollaborateRecording.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Pumukit\BlackboardBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use MongoDB\BSON\ObjectId;

/**
* @MongoDB\Document(repositoryClass="Pumukit\BlackboardBundle\Repository\CollaborateRecordingRepository")
*/
class CollaborateRecording
{
/**
* @MongoDB\Id
*/
private $id;

/**
* @MongoDB\Field(type="string")
*/
private $recording;

/**
* @MongoDB\Field(type="string")
*/
private $courseUUID;

/**
* @MongoDB\Field(type="string")
*/
private $downloadUrl;

/**
* @MongoDB\Field(type="string")
*/
private $sessionName;

/**
* @MongoDB\Field(type="boolean")
*/
private $imported;

private function __construct(\Pumukit\BlackboardBundle\ValueObject\CollaborateRecording $recording)
{
$this->id = new ObjectId();
$this->recording = $recording->id();
$this->courseUUID = $recording->courseUUID();
$this->downloadUrl = $recording->downloadUrl();
$this->sessionName = $recording->sessionName();
$this->imported = false;
}

public static function create(\Pumukit\BlackboardBundle\ValueObject\CollaborateRecording $recording): CollaborateRecording
{
return new self($recording);
}

public function id()
{
return $this->id;
}

public function recording(): string
{
return $this->recording;
}

public function courseUUID(): string
{
return $this->courseUUID;
}

public function downloadUrl(): string
{
return $this->downloadUrl;
}

public function sessionName(): string
{
return $this->sessionName;
}

public function imported(): bool
{
return $this->imported;
}
}
9 changes: 9 additions & 0 deletions Repository/CollaborateRecordingRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Pumukit\BlackboardBundle\Repository;

use Doctrine\ODM\MongoDB\Repository\DocumentRepository;

class CollaborateRecordingRepository extends DocumentRepository {}
3 changes: 3 additions & 0 deletions Resources/config/pumukit_blackboard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ services:

Pumukit\BlackboardBundle\Services\CollaborateAPISessionSearch:
class: Pumukit\BlackboardBundle\Services\CollaborateAPISessionSearch

Pumukit\BlackboardBundle\Services\CollaborateCreateRecording:
class: Pumukit\BlackboardBundle\Services\CollaborateCreateRecording
1 change: 0 additions & 1 deletion Services/CollaborateAPIConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,4 @@ public function sessionDataUrl(): string
{
return $this->host().self::API_BASE_PATH.'/sessions';
}

}
1 change: 0 additions & 1 deletion Services/CollaborateAPICourseRecordings.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ private function recordings(string $accessToken, string $courseId): string
'Accept' => 'application/json',
],
'query' => [
// 'contextId' => $courseId,
'contextExtId' => $courseId,
],
]);
Expand Down
2 changes: 1 addition & 1 deletion Services/CollaborateAPIRecording.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private function recordings(string $accessToken, string $path): ?string
'headers' => [
'Authorization' => 'Bearer '.$accessToken,
'Accept' => 'application/json',
'Content-Type' => 'application/json'
'Content-Type' => 'application/json',
],
'verify_peer' => true,
]);
Expand Down
12 changes: 4 additions & 8 deletions Services/CollaborateAPISessionSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function __construct(HttpClientInterface $client, CollaborateAPIConfigura
$this->configuration = $configuration;
}

public function searchBySessionName(string $accessToken, string $sessionName): ?array
public function searchSessions(string $accessToken): ?array
{
$sessionResponse = $this->sessionByName($accessToken, $sessionName);
$sessionResponse = $this->session($accessToken);

return json_decode($sessionResponse, true);
}
Expand All @@ -30,17 +30,14 @@ public function getEnrollmentsBySessionId(string $accessToken, string $sessionId
return json_decode($sessionResponse, true);
}

private function sessionByName(string $accessToken, string $sessionName): ?string
private function session(string $accessToken): ?string
{
try {
$response = $this->client->request('GET', $this->configuration->sessionDataUrl(), [
'headers' => [
'Authorization' => 'Bearer '.$accessToken,
'Accept' => 'application/json',
],
// 'query' => [
// 'name' => $sessionName,
// ],
]);

if (Response::HTTP_OK !== $response->getStatusCode()) {
Expand All @@ -60,7 +57,7 @@ private function sessionById(string $accessToken, string $sesionId): ?string
'headers' => [
'Authorization' => 'Bearer '.$accessToken,
'Accept' => 'application/json',
]
],
]);

if (Response::HTTP_OK !== $response->getStatusCode()) {
Expand All @@ -72,5 +69,4 @@ private function sessionById(string $accessToken, string $sesionId): ?string
throw new \Exception($exception->getMessage());
}
}

}
38 changes: 38 additions & 0 deletions Services/CollaborateCreateRecording.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Pumukit\BlackboardBundle\Services;

use Doctrine\ODM\MongoDB\DocumentManager;
use Pumukit\BlackboardBundle\ValueObject\CollaborateRecording;

class CollaborateCreateRecording
{
private DocumentManager $documentManager;

public function __construct(DocumentManager $documentManager)
{
$this->documentManager = $documentManager;
}

public function create(CollaborateRecording $collaborateRecording): ?\Pumukit\BlackboardBundle\Document\CollaborateRecording
{
if ($this->wasCreated($collaborateRecording->id())) {
return null;
}

$recording = \Pumukit\BlackboardBundle\Document\CollaborateRecording::create($collaborateRecording);
$this->documentManager->persist($recording);
$this->documentManager->flush();

return $recording;
}

private function wasCreated(string $recordingId): bool
{
$recording = $this->documentManager->getRepository(\Pumukit\BlackboardBundle\Document\CollaborateRecording::class)->findBy([
'recordingId' => $recordingId,
]);

return (bool) $recording;
}
}
11 changes: 0 additions & 11 deletions ValueObject/CollaborateRecording.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ final class CollaborateRecording
private string $courseUUID;
private string $downloadUrl;
private string $sessionName;
private string $user;

private function __construct(string $id, string $courseUUID, string $downloadUrl, string $sessionName)
{
Expand All @@ -23,11 +22,6 @@ public static function create(string $id, string $courseUUID, string $downloadUr
return new self($id, $courseUUID, $downloadUrl, $sessionName);
}

public function addUser(string $user): void
{
$this->user = $user;
}

public function id(): string
{
return $this->id;
Expand All @@ -47,9 +41,4 @@ public function sessionName(): string
{
return $this->sessionName;
}

public function user(): ?string
{
return $this->user;
}
}

0 comments on commit 8fd5f37

Please sign in to comment.