Skip to content

Commit

Permalink
make CA compatible with Edlib 3
Browse files Browse the repository at this point in the history
  • Loading branch information
emmachughes committed Oct 3, 2023
1 parent edaa0bb commit 6bd93ae
Show file tree
Hide file tree
Showing 34 changed files with 432 additions and 502 deletions.
11 changes: 11 additions & 0 deletions sourcecode/apis/contentauthor/app/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use function libxml_use_internal_errors;
use function preg_replace_callback;
use function route;

use const LIBXML_HTML_NOIMPLIED;

Expand Down Expand Up @@ -246,6 +247,16 @@ public function setVersionId(string $versionId): void
$this->version_id = $versionId;
}

public function getUrl(): string
{
return route('article.show', [$this->id]);
}

public function getMachineName(): string
{
return 'Article';
}

/**
* Used by Eloquent to get primary key type.
* UUID Identified as a string.
Expand Down
36 changes: 36 additions & 0 deletions sourcecode/apis/contentauthor/app/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\EdlibResource\CaEdlibResource;
use App\Http\Libraries\License;
use App\Libraries\DataObjects\ContentTypeDataObject;
use App\Libraries\DataObjects\LtiContent;
use App\Libraries\DataObjects\ResourceUserDataObject;
use App\Libraries\H5P\Interfaces\H5PAdapterInterface;
use App\Traits\Attributable;
Expand All @@ -25,6 +26,11 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

use function htmlspecialchars_decode;

use const ENT_HTML5;
use const ENT_QUOTES;

/**
* @property string|int $id
* @property Carbon $created_at
Expand All @@ -39,6 +45,7 @@
* @property string $node_id
* @property Collection $collaborators
* @property bool $is_draft
* @property-read string|null $title_clean
* @property-read NdlaIdMapper|null $ndlaMapper
*
* @method static Collection findMany($ids, $columns = ['*'])
Expand Down Expand Up @@ -90,6 +97,22 @@ abstract public function getContentOwnerId();

abstract public function getISO6393Language();

/**
* Get the URL for displaying the content
*/
abstract public function getUrl(): string;

abstract public function getMachineName(): string;

public function getTitleCleanAttribute(): string|null
{
if ($this->title === null) {
return null;

Check warning on line 110 in sourcecode/apis/contentauthor/app/Content.php

View check run for this annotation

Codecov / codecov/patch

sourcecode/apis/contentauthor/app/Content.php#L110

Added line #L110 was not covered by tests
}

return htmlspecialchars_decode($this->title, ENT_HTML5 | ENT_QUOTES);
}

public function isOwner($currentUserId): bool
{
if ($currentUserId === false || $this->getContentOwnerId() != $currentUserId) {
Expand Down Expand Up @@ -484,6 +507,19 @@ public function getEdlibDataObject(): CaEdlibResource
);
}

public function toLtiContent(): LtiContent
{
return new LtiContent(
id: $this->id,
url: $this->getUrl(),
title: $this->title_clean,
machineName: $this->getMachineName(),
hasScore: ($this->getMaxScore() ?? 0) > 0,
editUrl: $this->getEditUrl(),
titleHtml: $this->title,
);
}

public static function getContentTypeInfo(string $contentType): ?ContentTypeDataObject
{
return null;
Expand Down

This file was deleted.

12 changes: 12 additions & 0 deletions sourcecode/apis/contentauthor/app/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Illuminate\Http\Request;
use Iso639p3;

use function route;

/**
* @property string $id
* @property string $gametype
Expand Down Expand Up @@ -123,6 +125,16 @@ public function setVersionId(string $versionId): void
$this->version_id = $versionId;
}

public function getUrl(): string
{
return route('game.show', [$this->id]);
}

public function getMachineName(): string
{
return 'Game';
}

public static function getContentTypeInfo(string $contentType): ?ContentTypeDataObject
{
return new ContentTypeDataObject('Game', $contentType, 'Millionaire mini game', "mui:VideogameAsset");
Expand Down
18 changes: 12 additions & 6 deletions sourcecode/apis/contentauthor/app/H5PContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Illuminate\Support\Str;
use Iso639p3;

use function route;

/**
* @property string $user_id
* @property int $library_id
Expand All @@ -35,7 +37,6 @@
* @property string $description
* @property string $content_create_mode
* @property string $language_iso_639_3
* @property string|null $title_clean
* @property ?int $max_score
* @property int $bulk_calculated
*
Expand Down Expand Up @@ -68,11 +69,6 @@ class H5PContent extends Content implements VersionableObject
'is_draft' => 'boolean',
];

public function getTitleCleanAttribute(): string|null
{
return htmlspecialchars_decode($this->title, ENT_HTML5 | ENT_QUOTES);
}

/**
* @return HasMany<H5PCollaborator>
*/
Expand Down Expand Up @@ -358,4 +354,14 @@ public static function getContentTypeInfo(string $contentType): ?ContentTypeData

return new ContentTypeDataObject("H5P", $contentType, $library->title, $icon);
}

public function getUrl(): string
{
return route('h5p.show', [$this->id]);
}

public function getMachineName(): string
{
return $this->library()->firstOrFail()->name;
}
}
9 changes: 6 additions & 3 deletions sourcecode/apis/contentauthor/app/H5pLti.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace App;

use App\Http\Requests\LTIRequest;
use Cerpus\EdlibResourceKit\Oauth1\CredentialStoreInterface;
use Cerpus\EdlibResourceKit\Oauth1\Exception\ValidationException;
use Cerpus\EdlibResourceKit\Oauth1\ValidatorInterface;

class H5pLti
{
public function __construct(private readonly ValidatorInterface $validator)
{
public function __construct(
private readonly ValidatorInterface $validator,
private readonly CredentialStoreInterface $credentialStore,
) {
}

public function getValidatedLtiRequest(): LTIRequest|null
Expand All @@ -21,7 +24,7 @@ public function getValidatedLtiRequest(): LTIRequest|null
}

try {
$this->validator->validate($ltiRequest);
$this->validator->validate($ltiRequest, $this->credentialStore);

return $ltiRequest;
} catch (ValidationException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
namespace App\Http\Controllers\API\Handler;

use App\Content;
use App\H5PContent;
use App\Http\Requests\H5PStorageRequest;
use App\Libraries\H5P\Helper\H5PPackageProvider;
use App\Libraries\H5P\Packages\H5PBase;
use App\Libraries\H5P\Packages\QuestionSet;
use App\Http\Controllers\H5PController;
use Illuminate\Support\Collection;
use Ramsey\Uuid\Uuid;

class ContentTypeHandler
{
private $data;

public function storeQuestionset($questionsetData)
public function storeQuestionset($questionsetData): H5PContent
{
$this->data = $questionsetData;
$parsedData = $this->parseQuestionsetData();
Expand Down Expand Up @@ -42,7 +44,7 @@ public function storeQuestionset($questionsetData)
return $this->storeContentType($request, $questionsetData['authId']);
}

private function parseQuestionsetData()
private function parseQuestionsetData(): Collection
{
return collect($this->data['questions'])->map(function ($element) {
/** @var H5PBase $contentType */
Expand All @@ -57,10 +59,10 @@ private function parseQuestionsetData()
});
}

private function storeContentType($request, $userId)
private function storeContentType($request, $userId): H5PContent
{
/** @var H5PController $h5p */
$h5p = app(H5PController::class);
return $h5p->persistContent($request, $userId)->toArray();
return $h5p->persistContent($request, $userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,9 @@ public function store(ArticleRequest $request): JsonResponse
// Handles privacy, collaborators, and registering a new version
event(new ArticleWasSaved($article, $request, $emailCollaborators, Session::get('authId'), VersionData::CREATE, Session::all()));

$urlToCore = $this->getRedirectToCoreUrl(
$article->id,
$article->title,
'Article',
$article->givesScore(),
$request->get('redirectToken')
); // Will not return if we have a returnURL

$responseValues = [
'url' => !is_null($urlToCore) ? $urlToCore : route('article.edit', $article->id),
];

return response()->json($responseValues, Response::HTTP_CREATED);
$url = $this->getRedirectToCoreUrl($article->toLtiContent(), $request->get('redirectToken'));

return response()->json(['url' => $url], Response::HTTP_CREATED);
}

/**
Expand Down Expand Up @@ -333,19 +323,11 @@ public function update(ArticleRequest $request, Article $article)

event(new ArticleWasSaved($article, $request, $collaborators, Session::get('authId'), $reason, Session::all()));

$urlToCore = $this->getRedirectToCoreUrl(
$article->id,
$article->title,
'Article',
$article->givesScore(),
$request->get('redirectToken')
); // Will not return if we have a returnURL

$responseValues = [
'url' => !is_null($urlToCore) ? $urlToCore : route('article.edit', $article->id),
];
$url = $this->getRedirectToCoreUrl($article->toLtiContent(), $request->get('redirectToken'));

return response()->json($responseValues, Response::HTTP_CREATED);
return response()->json([
'url' => $url,
], Response::HTTP_CREATED);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,8 @@ public function update(Game $game, ApiQuestionsetRequest $request)
$game->setCollaborators($collaborators)->notifyNewCollaborators();
}

$urlToCore = $this->getRedirectToCoreUrl(
$updatedGame->id,
$updatedGame->title,
"Game",
true,
$request->get('redirectToken')
); // Will not return if we have a returnURL
$url = $this->getRedirectToCoreUrl($game->toLtiContent(), $request->input('redirectToken'));

$responseValues = [
'url' => !is_null($urlToCore) ? $urlToCore : route("game.show", ['game' => $updatedGame->id])
];

return response()->json($responseValues, Response::HTTP_OK);
return response()->json(['url' => $url], Response::HTTP_OK);
}
}
Loading

0 comments on commit 6bd93ae

Please sign in to comment.