Skip to content

Commit

Permalink
add adding contexts to lti platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
emmachughes committed Jan 3, 2025
1 parent cfb9ca4 commit 06f09a1
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace App\Http\Controllers\Admin;

use App\Http\Requests\AddContextToLtiPlatformRequest;
use App\Http\Requests\StoreLtiPlatformRequest;
use App\Http\Requests\UpdateLtiPlatformRequest;
use App\Models\Context;
use App\Models\LtiPlatform;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

use function redirect;
use function route;
use function to_route;

Expand Down Expand Up @@ -71,4 +74,30 @@ public function destroy(LtiPlatform $platform, Request $request): Response

return to_route('admin.lti-platforms.index');
}

public function contexts(LtiPlatform $platform): View
{
// @phpstan-ignore larastan.noUnnecessaryCollectionCall
$availableContexts = Context::all()
->diff($platform->contexts)
->mapWithKeys(fn (Context $context) => [$context->id => $context->name]);

return view('admin.lti-platforms.contexts', [
'available_contexts' => $availableContexts,
'platform' => $platform,
]);
}

public function addContext(
LtiPlatform $platform,
AddContextToLtiPlatformRequest $request,
): RedirectResponse {
$context = Context::where('id', $request->validated('context'))
->firstOrFail();

$platform->contexts()->attach($context);

return redirect()->back()
->with('alert', trans('messages.context-added-to-lti-platform'));
}
}
3 changes: 1 addition & 2 deletions sourcecode/hub/app/Http/Controllers/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public function history(Content $content): View

public function roles(Content $content): View
{
// TODO: fix annoying phpstan error
//@phpstan-ignore-next-line
// @phpstan-ignore larastan.noUnnecessaryCollectionCall
$availableContexts = Context::all()
->diff($content->contexts)
->mapWithKeys(fn (Context $context) => [$context->id => $context->name]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Models\Context;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AddContextToLtiPlatformRequest extends FormRequest
{
/**
* @return array<mixed>
*/
public function rules(): array
{
return [
'context' => ['required', 'string', Rule::exists(Context::class, 'id')],
];
}
}
2 changes: 1 addition & 1 deletion sourcecode/hub/app/Models/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public function contexts(): BelongsToMany
{
return $this->belongsToMany(Context::class)
->withPivot('role')
->using(ContextPivot::class);
->using(ContentContextPivot::class);
}

public function hasUser(User $user): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use App\Enums\ContentRole;
use Illuminate\Database\Eloquent\Relations\Pivot;

class ContextPivot extends Pivot
class ContentContextPivot extends Pivot
{
protected $casts = [
'role' => ContentRole::class,
Expand Down
19 changes: 9 additions & 10 deletions sourcecode/hub/app/Models/LtiPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ class LtiPlatform extends Model
'deleting' => LtiPlatformDeleting::class,
];

/**
* @return BelongsToMany<Context, $this>
*/
public function contexts(): BelongsToMany
{
return $this->belongsToMany(Context::class, 'lti_platform_context')
->withPivot('role')
->using(ContextPivot::class);
}

protected static function booted(): void
{
static::creating(function (self $ltiPlatform): void {
Expand All @@ -71,6 +61,15 @@ protected static function booted(): void
});
}

/**
* @return BelongsToMany<Context, $this>
*/
public function contexts(): BelongsToMany
{
return $this->belongsToMany(Context::class, 'lti_platform_context')
->using(ContentContextPivot::class);
}

public function getOauth1Credentials(): Credentials
{
return new Credentials($this->key, $this->secret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function up(): void
Schema::create('lti_platform_context', function (Blueprint $table) {
$table->ulid('lti_platform_id');
$table->ulid('context_id');
$table->string('role');

$table->foreign('lti_platform_id')->references('id')->on('lti_platforms');
$table->foreign('context_id')->references('id')->on('contexts');
Expand Down
3 changes: 3 additions & 0 deletions sourcecode/hub/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,8 @@
'add-context' => 'Add context',
'no-available-contexts-to-add' => 'There are no available contexts to add.',
'context-added-to-content' => 'The context was added to the content.',
'context-added-to-lti-platform' => 'The context was added to the LTI platform.',
'context-removed-from-content' => 'The content was removed from the content.',
'edit-lti-platform' => 'Edit LTI platform',
'contexts-for-lti-platform' => 'Contexts for LTI platform ":platform"',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<x-layout>
<x-slot:title>{{ trans('messages.contexts-for-lti-platform', ['platform' => $platform->name]) }}</x-slot:title>

<p><a href="{{ route('admin.lti-platforms.index') }}">Back to LTI platforms</a></p>

@if (count($platform->contexts) > 0)
<ul>
@foreach ($platform->contexts as $context)
<li>{{ $context->name }}</li>
@endforeach
</ul>
@endif

@if (count($available_contexts) > 0)
<x-form action="{{ route('admin.lti-platforms.add-context', [$platform]) }}" method="PUT">
<x-form.field
name="context"
type="select"
emptyOption
required
:label="trans('messages.context')"
:options="$available_contexts"
/>

<x-form.button class="btn-primary">{{ trans('messages.add') }}</x-form.button>
</x-form>
@else
<p>{{ trans('messages.no-available-contexts-to-add') }}</p>
@endif
</x-layout>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<x-layout>
<x-slot:title>{{ trans('messages.edit-lti-platforms') }}</x-slot:title>
<x-slot:title>{{ trans('messages.edit-lti-platform') }}</x-slot:title>

<x-admin.lti-platforms.form
action="{{ route('admin.lti-platforms.update', [$platform]) }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
<dd class="lti-platform-card-enable-sso">{{ $platform->enable_sso ? trans('messages.yes') : trans('messages.no') }}</dd>
<dt>{{ trans('messages.lti-platform-authorizes-edit') }}</dt>
<dd class="lti-platform-card-authorizes-edit">{{ $platform->authorizes_edit ? trans('messages.yes') : trans('messages.no') }}</dd>
<dt><a href="{{ route('admin.lti-platforms.contexts', [$platform]) }}">{{ trans('messages.contexts') }}</a></dt>
<dd class="lti-platform-card-context-count">{{ count($platform->contexts) }}</dd>
</dl>
</div>

Expand Down
10 changes: 10 additions & 0 deletions sourcecode/hub/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@
->name('admin.lti-platforms.edit')
->can('edit', ['platform']);

Route::get('/{platform}/contexts')
->uses([LtiPlatformController::class, 'contexts'])
->name('admin.lti-platforms.contexts')
->can('edit', ['platform']);

Route::put('/{platform}/contexts')
->uses([LtiPlatformController::class, 'addContext'])
->name('admin.lti-platforms.add-context')
->can('edit', ['platform']);

Route::patch('/{platform}')
->uses([LtiPlatformController::class, 'update'])
->name('admin.lti-platforms.update')
Expand Down
28 changes: 28 additions & 0 deletions sourcecode/hub/tests/Browser/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Browser;

use App\Models\Context;
use App\Models\LtiPlatform;
use App\Models\LtiTool;
use App\Models\User;
Expand Down Expand Up @@ -266,4 +267,31 @@ public function testCanEditFlagsForTool(): void
)
);
}

public function testCanAddContextToLtiPlatform(): void
{
LtiPlatform::factory()->create();
$context = Context::factory()->name('ndla_users')->create();
$user = User::factory()->admin()->create();

$this->browse(fn (Browser $browser) => $browser
->loginAs($user->email)
->assertAuthenticated()
->visit('/admin')
->clickLink('Manage LTI platforms')
->within(new LtiPlatformCard(), fn (Browser $card) => $card
->assertSeeIn('@context-count', '0')
->clickLink('Contexts')
)
// Dusk does not support selecting by the choice's label
->select('context', $context->id)
->press('Add')
->assertSee('The context was added to the LTI platform')
->visit('/admin')
->clickLink('Manage LTI platforms')
->within(new LtiPlatformCard(), fn (Browser $card) => $card
->assertSeeIn('@context-count', '1')
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function elements(): array
'@title' => '.lti-platform-card-title',
'@authorizes-edit' => '.lti-platform-card-authorizes-edit',
'@enable-sso' => '.lti-platform-card-enable-sso',
'@context-count' => '.lti-platform-card-context-count',
];
}
}

0 comments on commit 06f09a1

Please sign in to comment.