Skip to content

Commit 85ecff4

Browse files
committed
Add courseGroupRegistration source attribute. Deprecate attendees in courseGroup source attribute.
1 parent 6c6e30f commit 85ecff4

3 files changed

Lines changed: 95 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add courseGroupRegistration source attribute. Deprecate attendees in courseGroup source attribute.
6+
57
## v0.2.9
68

79
- Add local data attribute attendeeWaitingList

src/EventSubscriber/CourseEventSubscriber.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CourseEventSubscriber extends AbstractLocalDataEventSubscriber
1818
public const ATTENDEES_SOURCE_DATA_ATTRIBUTE = 'attendees';
1919
public const ATTENDEE_WAITING_LIST_SOURCE_DATA_ATTRIBUTE = 'attendeeWaitingList';
2020
public const COURSE_GROUPS_SOURCE_DATA_ATTRIBUTE = 'courseGroups';
21+
public const COURSE_GROUP_REGISTRATIONS_SOURCE_DATA_ATTRIBUTE = 'courseGroupRegistrations';
2122
public const DESCRIPTION_SOURCE_DATA_ATTRIBUTE = 'description';
2223
public const STATUS_WITHIN_CURRICULUM_URL_SOURCE_DATA_ATTRIBUTE = 'statusWithinCurriculumUrl';
2324
public const OBJECTIVE_SOURCE_DATA_ATTRIBUTE = 'objective';
@@ -60,9 +61,13 @@ protected function getAttributeValue(LocalDataPostEvent $postEvent, array $attri
6061
$course->getIdentifier(), CourseRegistrationResource::REGISTRATION_STATUS_WAITING_LIST);
6162

6263
case self::COURSE_GROUPS_SOURCE_DATA_ATTRIBUTE:
63-
return $this->courseProvider->getGroupsByCourse(
64+
return $this->courseProvider->getCourseGroupsByCourse(
6465
$course->getIdentifier(), $postEvent->getOptions());
6566

67+
case self::COURSE_GROUP_REGISTRATIONS_SOURCE_DATA_ATTRIBUTE:
68+
return $this->courseProvider->getCourseGroupRegistrationsByCourse(
69+
$course->getIdentifier());
70+
6671
case self::DESCRIPTION_SOURCE_DATA_ATTRIBUTE:
6772
return $this->courseProvider->getDescriptionByCourse(
6873
$course->getIdentifier(), $postEvent->getOptions());

src/Service/CourseProvider.php

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Dbp\CampusonlineApi\PublicRestApi\Courses\CourseDescriptionApi;
1313
use Dbp\CampusonlineApi\PublicRestApi\Courses\CourseDescriptionResource;
1414
use Dbp\CampusonlineApi\PublicRestApi\Courses\CourseGroupApi;
15+
use Dbp\CampusonlineApi\PublicRestApi\Courses\CourseGroupResource;
1516
use Dbp\CampusonlineApi\PublicRestApi\Courses\CourseRegistrationApi;
1617
use Dbp\CampusonlineApi\PublicRestApi\Courses\CourseRegistrationResource;
1718
use Dbp\CampusonlineApi\PublicRestApi\Courses\CourseResource;
@@ -61,12 +62,12 @@ class CourseProvider implements CourseProviderInterface, LoggerAwareInterface
6162
/**
6263
* array<string, array> request cache for localized type names.
6364
*/
64-
private ?array $localizedTypeNameRequestCache = null;
65+
private array $localizedTypeNameRequestCache = [];
6566

6667
/**
6768
* array<string, array> request cache for localized lectureship function names.
6869
*/
69-
private ?array $localizedLectureshipFunctionNameRequestCache = null;
70+
private array $localizedLectureshipFunctionNameRequestCache = [];
7071

7172
/**
7273
* array<string, CourseRegistrationResource>.
@@ -78,6 +79,11 @@ class CourseProvider implements CourseProviderInterface, LoggerAwareInterface
7879
*/
7980
private array $lectureshipRequestCache = [];
8081

82+
/**
83+
* array<string, CourseGroupResource>.
84+
*/
85+
private array $courseGroupRequestCache = [];
86+
8187
private ?\DateTimeZone $eventTimeZone = null;
8288

8389
public function __construct(
@@ -93,6 +99,11 @@ public function __construct(
9399
public function reset(): void
94100
{
95101
$this->courseApi = null;
102+
$this->localizedLectureshipFunctionNameRequestCache = [];
103+
$this->localizedTypeNameRequestCache = [];
104+
$this->courseRegistrationsRequestCache = [];
105+
$this->lectureshipRequestCache = [];
106+
$this->courseGroupRequestCache = [];
96107
}
97108

98109
/**
@@ -386,15 +397,12 @@ public function getLecturersByCourse(string $courseIdentifier, array $options =
386397
}
387398

388399
/**
389-
* @return array<array>
400+
* @return array<array<string, mixed>>
390401
*/
391-
public function getGroupsByCourse(string $courseIdentifier, array $options = []): array
402+
public function getCourseGroupsByCourse(string $courseIdentifier, array $options = []): array
392403
{
393404
try {
394-
$courseGroupApi = new CourseGroupApi($this->getCourseApi()->getConnection());
395-
396405
$ATTENDEE_PERSON_IDENTIFIERS_KEY = 'attendeeIdentifiers';
397-
$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY = 'attendeeWaitingListIdentifiers';
398406
$LECTURER_PERSON_IDENTIFIERS_KEY = 'lecturerIdentifiers';
399407

400408
$courseGroupPeopleMap = [];
@@ -403,7 +411,6 @@ public function getGroupsByCourse(string $courseIdentifier, array $options = [])
403411
if (null === ($courseGroupPeopleMap[$courseGroupIdentifier] ?? null)) {
404412
$courseGroupPeople = [
405413
$ATTENDEE_PERSON_IDENTIFIERS_KEY => [],
406-
$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY => [],
407414
$LECTURER_PERSON_IDENTIFIERS_KEY => [],
408415
];
409416
$courseGroupPeopleMap[$courseGroupIdentifier] = $courseGroupPeople;
@@ -412,17 +419,13 @@ public function getGroupsByCourse(string $courseIdentifier, array $options = [])
412419
case CourseRegistrationResource::REGISTRATION_STATUS_FIXED:
413420
$courseGroupPeopleMap[$courseGroupIdentifier][$ATTENDEE_PERSON_IDENTIFIERS_KEY][] = $registrationResource->getPersonUid();
414421
break;
415-
case CourseRegistrationResource::REGISTRATION_STATUS_WAITING_LIST:
416-
$courseGroupPeopleMap[$courseGroupIdentifier][$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY][] = $registrationResource->getPersonUid();
417-
break;
418422
}
419423
}
420424
foreach ($this->getLectureshipResourcesCached($courseIdentifier) as $lectureshipResource) {
421425
foreach ($lectureshipResource->getGroupUids() as $courseGroupIdentifier) {
422426
if (null === ($courseGroupPeopleMap[$courseGroupIdentifier] ?? null)) {
423427
$courseGroupPeople = [
424428
$ATTENDEE_PERSON_IDENTIFIERS_KEY => [],
425-
$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY => [],
426429
$LECTURER_PERSON_IDENTIFIERS_KEY => [],
427430
];
428431
$courseGroupPeopleMap[$courseGroupIdentifier] = $courseGroupPeople;
@@ -432,13 +435,59 @@ public function getGroupsByCourse(string $courseIdentifier, array $options = [])
432435
}
433436

434437
$courseGroups = [];
435-
foreach ($courseGroupApi->getCourseGroupsByCourseUid($courseIdentifier) as $courseGroupPeople) {
438+
foreach ($this->getCourseGroupResourcesCached($courseIdentifier) as $courseGroupResource) {
439+
$courseGroups[] = [
440+
'identifier' => $courseGroupResource->getUid(),
441+
'name' => $courseGroupResource->getName(Options::getLanguage($options) ?? self::DEFAULT_LANGUAGE_TAG),
442+
$LECTURER_PERSON_IDENTIFIERS_KEY => $courseGroupPeopleMap[$courseGroupResource->getUid()][$LECTURER_PERSON_IDENTIFIERS_KEY] ?? [],
443+
/*
444+
* @deprecated Replaced by course group registrations. Left for backward compatibility.
445+
*/
446+
$ATTENDEE_PERSON_IDENTIFIERS_KEY => $courseGroupPeopleMap[$courseGroupResource->getUid()][$ATTENDEE_PERSON_IDENTIFIERS_KEY] ?? [],
447+
];
448+
}
449+
450+
return $courseGroups;
451+
} catch (ApiException $apiException) {
452+
throw self::dispatchException($apiException);
453+
}
454+
}
455+
456+
/**
457+
* @return array<array<string, mixed>>
458+
*/
459+
public function getCourseGroupRegistrationsByCourse(?string $courseIdentifier): array
460+
{
461+
try {
462+
$ATTENDEE_PERSON_IDENTIFIERS_KEY = 'attendeeIdentifiers';
463+
$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY = 'attendeeWaitingListIdentifiers';
464+
465+
$courseGroupPeopleMap = [];
466+
foreach ($this->getCourseRegistrationResourcesCached($courseIdentifier) as $registrationResource) {
467+
$courseGroupIdentifier = $registrationResource->getCourseGroupUid();
468+
if (null === ($courseGroupPeopleMap[$courseGroupIdentifier] ?? null)) {
469+
$courseGroupPeople = [
470+
$ATTENDEE_PERSON_IDENTIFIERS_KEY => [],
471+
$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY => [],
472+
];
473+
$courseGroupPeopleMap[$courseGroupIdentifier] = $courseGroupPeople;
474+
}
475+
switch ($registrationResource->getRegistrationStatus()) {
476+
case CourseRegistrationResource::REGISTRATION_STATUS_FIXED:
477+
$courseGroupPeopleMap[$courseGroupIdentifier][$ATTENDEE_PERSON_IDENTIFIERS_KEY][] = $registrationResource->getPersonUid();
478+
break;
479+
case CourseRegistrationResource::REGISTRATION_STATUS_WAITING_LIST:
480+
$courseGroupPeopleMap[$courseGroupIdentifier][$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY][] = $registrationResource->getPersonUid();
481+
break;
482+
}
483+
}
484+
485+
$courseGroups = [];
486+
foreach ($this->getCourseGroupResourcesCached($courseIdentifier) as $courseGroupResource) {
436487
$courseGroups[] = [
437-
'identifier' => $courseGroupPeople->getUid(),
438-
'name' => $courseGroupPeople->getName(Options::getLanguage($options) ?? self::DEFAULT_LANGUAGE_TAG),
439-
$ATTENDEE_PERSON_IDENTIFIERS_KEY => $courseGroupPeopleMap[$courseGroupPeople->getUid()][$ATTENDEE_PERSON_IDENTIFIERS_KEY] ?? [],
440-
$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY => $courseGroupPeopleMap[$courseGroupPeople->getUid()][$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY] ?? [],
441-
$LECTURER_PERSON_IDENTIFIERS_KEY => $courseGroupPeopleMap[$courseGroupPeople->getUid()][$LECTURER_PERSON_IDENTIFIERS_KEY] ?? [],
488+
'identifier' => $courseGroupResource->getUid(),
489+
$ATTENDEE_PERSON_IDENTIFIERS_KEY => $courseGroupPeopleMap[$courseGroupResource->getUid()][$ATTENDEE_PERSON_IDENTIFIERS_KEY] ?? [],
490+
$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY => $courseGroupPeopleMap[$courseGroupResource->getUid()][$ATTENDEE_WAITING_LIST_PERSON_IDENTIFIERS_KEY] ?? [],
442491
];
443492
}
444493

@@ -500,7 +549,7 @@ public function getExpectedPreviousKnowledgeByCourse(string $courseIdentifier, a
500549

501550
public function getLocalizedTypeNameByKey(string $key, array $options = []): ?string
502551
{
503-
if ($this->localizedTypeNameRequestCache === null) {
552+
if ($this->localizedTypeNameRequestCache === []) {
504553
$courseTypeApi = new CourseTypeApi($this->getCourseApi()->getConnection());
505554
foreach ($courseTypeApi->getCourseTypes() as $courseTypeResource) {
506555
if (($currentKey = $courseTypeResource->getKey()) && ($currentName = $courseTypeResource->getName())) {
@@ -514,7 +563,7 @@ public function getLocalizedTypeNameByKey(string $key, array $options = []): ?st
514563

515564
public function getLocalizedLectureshipFunctionNameByKey(string $key, array $options = []): ?string
516565
{
517-
if ($this->localizedLectureshipFunctionNameRequestCache === null) {
566+
if ($this->localizedLectureshipFunctionNameRequestCache === []) {
518567
$lectureshipFunctionsApi = new LectureshipFunctionsApi($this->getCourseApi()->getConnection());
519568
foreach ($lectureshipFunctionsApi->getLectureshipFunctions() as $lectureshipFunction) {
520569
if (($currentKey = $lectureshipFunction->getKey()) && ($currentName = $lectureshipFunction->getName())) {
@@ -825,6 +874,24 @@ private function getLectureshipResourcesCached(string $courseIdentifier): array
825874
return $lectureshipResources;
826875
}
827876

877+
/**
878+
* @return CourseGroupResource[]
879+
*/
880+
private function getCourseGroupResourcesCached(string $courseIdentifier): array
881+
{
882+
if (null === ($courseGroupResources = $this->courseGroupRequestCache[$courseIdentifier] ?? null)) {
883+
$courseGroupApi = new CourseGroupApi($this->getConnection());
884+
try {
885+
$courseGroupResources = iterator_to_array($courseGroupApi->getCourseGroupsByCourseUid($courseIdentifier));
886+
$this->courseGroupRequestCache[$courseIdentifier] = $courseGroupResources;
887+
} catch (ApiException $apiException) {
888+
throw self::dispatchException($apiException);
889+
}
890+
}
891+
892+
return $courseGroupResources;
893+
}
894+
828895
private function getConnection(): Connection
829896
{
830897
return $this->getCourseApi()->getConnection();

0 commit comments

Comments
 (0)