Skip to content

Commit

Permalink
CA Admin: Fix listing of content versions (#2972)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrieinv authored Feb 14, 2025
1 parent 5ba496b commit a2f8527
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,20 @@ public function contentForLibrary(H5PLibrary $library, Request $request): View
]);
}

public function contentHistory(H5PContent $content): View
public function contentHistory(H5PContent $content, ContentVersion $version = null): View
{
$versions = collect();
$history = [];

if ($content->version_id) {
if ($version !== null && $content->id === $version->content_id) {
$history = $this->getVersions($version, $versions);
} elseif ($content->version_id) {
$data = $content->getVersion();
$history = $data ? $this->getVersions($data, $versions) : [];
}

return view('admin.library-upgrade.content-details', [
'content' => $content,
'latestVersion' => !isset($history[$content->id]['children']),
'requestedVersion' => $version,
'history' => $history,
'hasLock' => ContentLock::notExpiredById($content->id)?->updated_at,
]);
Expand Down Expand Up @@ -240,24 +241,11 @@ private function getVersions(ContentVersion $versionData, Collection $stack, $ge
{
$versionArray = $versionData->toArray();
$versionArray['versionDate'] = $versionData->created_at;
$content = $versionData->getContent();
if (!empty($content)) {
$library = $content->library;
$versionArray['content'] = [
'title' => $content->title,
'created' => $content->created_at->format('Y-m-d H:i:s e'),
'update' => $content->updated_at->format('Y-m-d H:i:s e'),
'version_id' => $content->version_id,
'license' => $content->license,
'language' => $content->language_iso_639_3,
'library_id' => $library->id,
'library' => sprintf('%s %d.%d.%d', $library->name, $library->major_version, $library->minor_version, $library->patch_version),
];
}
$versionArray['content'] = $this->getContentInfo($versionData);
$parent = $versionData->previousVersion;
if (!empty($parent)) {
$this->getVersions($parent, $stack, false);
$versionArray['parent'] = $parent->content_id;
$versionArray['parent'] = $parent->id;
}
$children = $versionData->nextVersions;
if ($children->isNotEmpty()) {
Expand All @@ -266,13 +254,37 @@ private function getVersions(ContentVersion $versionData, Collection $stack, $ge
if ($getChildren) {
$this->getVersions($child, $stack, false);
}
$versionArray['children'][] = $child->content_id;
$versionArray['children'][] = [
'id' => $child->id,
'content_id' => $child->content_id,
'versionDate' => $child->created_at,
'version_purpose' => $child->version_purpose,
'content' => $this->getContentInfo($versionData),
];
}
}
if (!$stack->has($versionData->content_id)) {
$stack->put($versionData->content_id, $versionArray);
if (!$stack->has($versionData->id)) {
$stack->put($versionData->id, $versionArray);
}

return $stack;
}

private function getContentInfo(ContentVersion $version): array
{
$content = $version->getContent();

if (!empty($content)) {
$library = $content->library;
return [
'title' => $content->title,
'license' => $content->license,
'language' => $content->language_iso_639_3,
'library_id' => $library->id,
'library' => sprintf('%s %d.%d.%d', $library->name, $library->major_version, $library->minor_version, $library->patch_version),
];
}

return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@
<td>{{ $content->updated_at->format('Y-m-d H:i:s e') }}</td>
</tr>
<tr>
<th>Latest version</th>
<td>{{ $latestVersion ? 'Yes' : 'No' }}</td>
<th>Latest version id</th>
<td>
@if($requestedVersion && $requestedVersion->id !== $content->version_id)
<a href="{{ route('admin.content-details', [$content->id]) }}">
{{ $content->version_id }}
</a>
@else
{{ $content->version_id }}
@endif
</td>
</tr>
<tr>
<th>Language</th>
Expand Down Expand Up @@ -91,8 +99,9 @@
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Date (Version)</th>
<th>Version id</th>
<th>Content id</th>
<th>Version created</th>
<th>Title</th>
<th>License</th>
<th>Language</th>
Expand All @@ -101,43 +110,40 @@
</tr>
</thead>
<tbody>
@while(true)
@php
if (!isset($itemId)) {
$itemId = $content->id;
} else {
$itemId = !empty($history[$itemId]['parent']) && !empty($history[$history[$itemId]['parent']]) ? $history[$itemId]['parent'] : null;
}
if ($itemId === null) {
break;
}
@endphp
@foreach($history as $historyItem)
@php($versionId = $requestedVersion ? $requestedVersion->id : $content->version_id)
<tr>
<td>
@if ($itemId !== $content->id && isset($history[$itemId]['content']))
<a href="{{ route('admin.content-details', [$history[$itemId]['content_id']]) }}">
{{ $history[$itemId]['content_id'] }}
@if ($historyItem['id'] !== $versionId)
<td>
<a href="{{ route('admin.content-details', [$historyItem['content_id'], $historyItem['id']]) }}">
{{ $historyItem['id'] }}
</a>
@else
{{ $history[$itemId]['content_id'] }}
@endif
</td>
@else
<td>{{ $historyItem['id'] }}</td>
@endif
<td>
<a href="{{ route('admin.content-details', [$historyItem['content_id']]) }}">
{{ $historyItem['content_id'] }}
</a>
</td>
<td>{{ $history[$itemId]['versionDate']->format('Y-m-d H:i:s.u e') }}</td>
<td>{{ $history[$itemId]['content']['title'] ?? '' }}</td>
<td>{{ $history[$itemId]['content']['license'] ?? '' }}</td>
<td>{{ $history[$itemId]['content']['language'] ?? '' }}</td>
<td>{{ $history[$itemId]['version_purpose'] }}</td>
<td>{{ $historyItem['versionDate']->format('Y-m-d H:i:s.u e') }}</td>
<td>{{ $historyItem['content']['title'] ?? '' }}</td>
<td>{{ $historyItem['content']['license'] ?? '' }}</td>
<td>{{ $historyItem['content']['language'] ?? '' }}</td>
<td>{{ $historyItem['version_purpose'] }}</td>
<td>
@if(isset($history[$itemId]['content']) && isset($history[$itemId]['content']['library_id']))
<a href="{{ route('admin.check-library', [$history[$itemId]['content']['library_id']]) }}">
{{ $history[$itemId]['content']['library'] }}
@if(isset($historyItem['content']) && isset($historyItem['content']['library_id']))
<a href="{{ route('admin.check-library', [$historyItem['content']['library_id']]) }}">
{{ $historyItem['content']['library'] }}
</a>
@else
{{ $history[$itemId]['content']['library'] ?? '' }}
{{ $historyItem['content']['library'] ?? '' }}
@endif
</td>
</tr>
@endwhile
@break($historyItem['id'] === $versionId)
@endforeach
</tbody>
</table>
</div>
Expand All @@ -150,8 +156,9 @@
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Date (Version)</th>
<th>Version id</th>
<th>Content id</th>
<th>Version created</th>
<th>Title</th>
<th>License</th>
<th>Language</th>
Expand All @@ -160,39 +167,41 @@
</tr>
</thead>
<tbody>
@empty($history[$content->id]['children'])
<tr>
<td colspan="6">
{{ $latestVersion ? 'This is the latest version' : 'No content found' }}
</td>
</tr>
@else
@foreach($history[$content->id]['children'] as $itemId)
@php($versionId = $requestedVersion ? $requestedVersion->id : $content->version_id)
@if(!empty($history[$versionId]) && !empty($history[$versionId]['children']))
@foreach($history[$versionId]['children'] as $child)
<tr>
<td>
@isset($history[$itemId]['content'])
<a href="{{ route('admin.content-details', [$history[$itemId]['content_id']]) }}">{{ $history[$itemId]['content_id'] }}</a>
@else
{{ $history[$itemId]['external_reference'] }}
@endisset
<a href="{{ route('admin.content-details', [$child['content_id'], $child['id']]) }}">
{{ $child['id'] }}
</a>
</td>
<td>{{ $history[$itemId]['versionDate']->format('Y-m-d H:i:s.u e') }}</td>
<td>{{ $history[$itemId]['content']['title'] ?? '' }}</td>
<td>{{ $history[$itemId]['content']['license'] ?? '' }}</td>
<td>{{ $history[$itemId]['content']['language'] ?? '' }}</td>
<td>{{ $history[$itemId]['version_purpose'] }}</td>
<td>
@if(isset($history[$itemId]['content']) && isset($history[$itemId]['content']['library_id']))
<a href="{{ route('admin.check-library', [$history[$itemId]['content']['library_id']]) }}">
{{ $history[$itemId]['content']['library'] }}
</a>
@else
{{ $history[$itemId]['content']['library'] ?? '' }}
@endif
<a href="{{ route('admin.content-details', [$child['content_id']]) }}">
{{ $child['content_id'] }}
</a>
</td>
@isset($child['content'])
<td>{{ $child['versionDate']->format('Y-m-d H:i:s.u e') }}</td>
<td>{{ $child['content']['title'] ?? '' }}</td>
<td>{{ $child['content']['license'] ?? '' }}</td>
<td>{{ $child['content']['language'] ?? '' }}</td>
<td>{{ $child['version_purpose'] }}</td>
<td>
@isset($child['content']['library_id'])
<a href="{{ route('admin.check-library', [$child['content']['library_id']]) }}">
{{ $child['content']['library'] }}
</a>
@else
{{ $child['content']['library'] ?? '' }}
@endif
</td>
@else
<td colspan="6"></td>
@endisset
</tr>
@endforeach
@endempty
@endif
</tbody>
</table>
</div>
Expand Down
2 changes: 1 addition & 1 deletion sourcecode/apis/contentauthor/routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function () {
->name('admin.check-library');
Route::get('libraries/{library}/content', [AdminH5PDetailsController::class, 'contentForLibrary'])
->name('admin.content-library');
Route::get('content/{content}/details', [AdminH5PDetailsController::class, 'contentHistory'])
Route::get('content/{content}/details/{version?}', [AdminH5PDetailsController::class, 'contentHistory'])
->name('admin.content-details');
Route::get('libraries/{library}/translation/{locale}', [AdminH5PDetailsController::class, 'libraryTranslation'])
->name('admin.library-translation');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,25 @@ public function test_contentHistory(): void
$data = $response->getData();

$this->assertArrayHasKey('content', $data);
$this->assertArrayHasKey('latestVersion', $data);
$this->assertArrayHasKey('requestedVersion', $data);
$this->assertArrayHasKey('history', $data);

$this->assertFalse($data['latestVersion']);
$this->assertNull($data['requestedVersion']);
$this->assertSame($content->id, $data['content']->id);

$this->assertInstanceOf(Collection::class, $data['history']);
$this->assertCount(3, $data['history']);
$this->assertArrayHasKey($parent->id, $data['history']);
$this->assertArrayHasKey($content->id, $data['history']);
$this->assertArrayHasKey($child->id, $data['history']);

$history = $data['history']->get($content->id);
$this->assertNotNull($history);
$this->assertSame($content->version_id, $history['content']['version_id']);
$this->assertEquals($parent->id, $history['parent']);
$this->assertArrayHasKey($parentVersion->id, $data['history']);
$this->assertArrayHasKey($version->id, $data['history']);
$this->assertArrayHasKey($childVersion->id, $data['history']);

$history = $data['history']->get($version->id);
$this->assertIsArray($history);
$this->assertEquals($content->id, $history['content_id']);
$this->assertEquals($parent->version_id, $history['parent']);
$this->assertCount(1, $history['children']);
$this->assertEquals($child->id, $history['children'][0]);
$this->assertEquals($child->version_id, $history['children'][0]['id']);
$this->assertEquals($child->id, $history['children'][0]['content_id']);
}

public function test_contentHistory_noResource(): void
Expand All @@ -322,7 +323,7 @@ public function test_contentHistory_noResource(): void
$data = $response->getData();

$this->assertSame($content->id, $data['content']['id']);
$this->assertTrue($data['latestVersion']);
$this->assertNull($data['requestedVersion']);
$this->assertCount(0, $data['history']);
}

Expand Down

0 comments on commit a2f8527

Please sign in to comment.