summaryrefslogtreecommitdiff
path: root/src/Http/Controllers/ScormPlayerController.php
blob: 7c801c9a07dc5f0a339a6515a1fe693a156b3352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

namespace Lightscale\ScormPlayer\Http\Controllers;

use Lightscale\ScormPlayer\Models\{
    Scorm,
    ScormSco,
    ScormScoTracking,
};

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Http\Testing\MimeType;

use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;


class ScormPlayerController extends Controller
{

    private const JS_PATH = __DIR__ . '/../../../dist/js/scorm_player.js';

    public function player(Request $request, Scorm $module)
    {
        $sco = $request->query('sco');
        $sco = $module->scos()->findOrFail($sco);

        $route_data = ['sco' => $sco];
        $scorm_api_data = [
            'routes' => [
                'load' => route('scorm-player.scorm.load', $route_data),
            ],
        ];

        $js_uri = route('scorm-player.javascript', [
            'version' => substr(md5(filemtime(self::JS_PATH)), 10, 8)
        ]);

        return view('scorm-player::player', compact(
            'sco',
            'scorm_api_data',
            'js_uri'
        ));
    }

    public function serveModule(string $uuid, string $path)
    {
        $path = Storage::disk('scorm-local')->path("{$uuid}/{$path}");
        $mime = MimeType::from($path);

        try {
            return response()->file($path, [
                'content-type' => $mime,
            ]);
        }
        catch(FileNotFoundException $e) {
            abort(404);
        }
    }

    public function scormLoad(ScormSco $sco)
    {
        $user = Auth::user();

        $tracking = ScormScoTracking::where([
            'sco_id' => $sco->id,
            'user_id' => $user->id,
        ])->first();

        if(!$tracking) {
            $tracking = new ScormScoTracking([
                'uuid' => Str::uuid(),
                'progression' => 0,
            ]);
            $tracking->user()->associate($user);
            $tracking->sco_id = $sco->id;
            $tracking->save();
        }

        $commit_url = route('scorm-player.scorm.commit', [
            'tracking' => $tracking->id,
        ]);

        $scorm_entry = route('scorm-player.serve', [
            'uuid' => $sco->scorm->uuid,
            'path' => $sco->entry_url,
        ]);

        return [
            'tracking_id' => $tracking->id,
            'tracking' => $tracking->getCMIData(),
            'entry_url' => $scorm_entry,
            'commit_url' => $commit_url,
        ];
    }

    public function scormCommit(Request $request, ScormScoTracking $tracking)
    {
        $data = $request->all();

        $tracking->setCMIData($data);

        return [
            'result' => true,
        ];
    }

    public function jsSource(Request $request)
    {
        return response()->file(self::JS_PATH, [
            'Content-Type' => 'application/javascript',
            //'Cache-Control' => $cacheControl,
            //'ETag' => $etag,
        ]);
    }

}