-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathIndependentActivityController.php
1077 lines (971 loc) · 40.8 KB
/
IndependentActivityController.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\IndependentActivityCreateRequest;
use App\Http\Requests\V1\IndependentActivityEditRequest;
use App\Http\Requests\V1\OrganizationIndependentActivityRequest;
use App\Http\Requests\V1\MoveIndependentActivityIntoPlaylistRequest;
use App\Http\Resources\V1\H5pOrganizationResource;
use App\Http\Resources\V1\IndependentActivityResource;
use App\Http\Resources\V1\IndependentActivityDetailResource;
use App\Http\Resources\V1\H5pIndependentActivityResource;
use App\Jobs\CloneIndependentActivity;
use App\Jobs\CopyIndependentActivityIntoPlaylist;
use App\Jobs\MoveIndependentActivityIntoPlaylist;
use App\Jobs\ConvertActvityIntoIndependentActivity;
use App\Models\H5pBrightCoveVideoContents;
use App\Models\IndependentActivity;
use App\Models\ActivityItem;
use App\Models\Playlist;
use App\Models\Activity;
use App\Repositories\IndependentActivity\IndependentActivityRepositoryInterface;
use App\Repositories\ActivityItem\ActivityItemRepositoryInterface;
use App\Repositories\H5pContent\H5pContentRepositoryInterface;
use App\Services\CurrikiGo\LMSIntegrationServiceInterface;
use Djoudi\LaravelH5p\Events\H5pEvent;
use Djoudi\LaravelH5p\Exceptions\H5PException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Arr;
use H5pCore;
use App\Models\Organization;
use App\Jobs\ExportIndependentActivity;
use App\Jobs\ImportIndependentActivity;
use App\Http\Requests\V1\IndependentActivityUploadImportRequest;
use Djoudi\LaravelH5p\Eloquents\H5pContent;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ZipArchive;
/**
* @group 6. Independent Activity
*
* APIs for independent activity management
*/
class IndependentActivityController extends Controller
{
private $independentActivityRepository;
private $h5pContentRepository;
/**
* IndependentActivityController constructor.
*
* @param IndependentActivityRepositoryInterface $independentActivityRepository
* @param H5pContentRepositoryInterface $h5pContentRepository
* @param ActivityItemRepositoryInterface $activityItemRepository
* @param LMSIntegrationServiceInterface $lms,
*/
public function __construct(
IndependentActivityRepositoryInterface $independentActivityRepository,
H5pContentRepositoryInterface $h5pContentRepository,
ActivityItemRepositoryInterface $activityItemRepository,
LMSIntegrationServiceInterface $lms
)
{
$this->independentActivityRepository = $independentActivityRepository;
$this->h5pContentRepository = $h5pContentRepository;
$this->activityItemRepository = $activityItemRepository;
$this->lms = $lms;
}
/**
* Get Independent Activities
*
* Get a list of independent activities
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @bodyParam query string Query to search independent activity against Example: Video
* @bodyParam size integer Size to show per page records Example: 10
* @bodyParam order_by_column string To sort data with specific column Example: title
* @bodyParam order_by_type string To sort data in ascending or descending order Example: asc
*
* @responseFile responses/independent-activity/independent-activities.json
*
* @param OrganizationIndependentActivityRequest $request
* @param Organization $suborganization
* @return Response
* @throws AuthorizationException
*/
public function index(OrganizationIndependentActivityRequest $request, Organization $suborganization)
{
$this->authorize('viewAuthor', [IndependentActivity::class, $suborganization]);
$authenticated_user = auth()->user();
return IndependentActivityResource::collection($this->independentActivityRepository->getAuthUserIndependentActivities($request->all(), $suborganization, $authenticated_user));
}
/**
* Get All Organization Independent Activities
*
* Get a list of the independent activities of an organization.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @bodyParam query string Query to search independent activity against Example: Video
* @bodyParam size integer Size to show per page records Example: 10
* @bodyParam order_by_column string To sort data with specific column Example: title
* @bodyParam order_by_type string To sort data in ascending or descending order Example: asc
*
* @responseFile responses/independent-activity/independent-activities.json
*
* @param OrganizationIndependentActivityRequest $request
* @param Organization $suborganization
* @return Response
*/
public function getOrgIndependentActivities(OrganizationIndependentActivityRequest $request, Organization $suborganization)
{
$this->authorize('viewAny', [IndependentActivity::class, $suborganization]);
return IndependentActivityResource::collection($this->independentActivityRepository->getAll($request->all(), $suborganization));
}
/**
* Upload Independent Activity thumbnail
*
* Upload thumbnail image for an independent activity
*
* @bodyParam thumb image required Thumbnail image to upload Example: (binary)
*
* @response {
* "thumbUrl": "/storage/independent-activities/1fqwe2f65ewf465qwe46weef5w5eqwq.png"
* }
*
* @response 400 {
* "errors": [
* "Invalid image."
* ]
* }
*
* @param Request $request
* @return Response
*/
public function uploadThumb(Request $request)
{
$validator = Validator::make($request->all(), [
'thumb' => 'required|image|max:102400',
]);
if ($validator->fails()) {
return response([
'errors' => ['Invalid image.']
], 400);
}
$path = $request->file('thumb')->store('/public/independent-activities');
return response([
'thumbUrl' => Storage::url($path),
], 200);
}
/**
* Create Independent Activity
*
* Create a new independent activity.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @bodyParam title string required The title of a activity Example: Science of Golf: Why Balls Have Dimples
* @bodyParam type string required The type of a activity Example: h5p
* @bodyParam content string required The content of a activity Example:
* @bodyParam order integer The order number of a activity Example: 2
* @bodyParam h5p_content_id integer The Id of H5p content Example: 59
* @bodyParam thumb_url string The image url of thumbnail Example: null
* @bodyParam subject_id array The Ids of a subject Example: [1, 2]
* @bodyParam education_level_id array The Ids of a education level Example: [1, 2]
* @bodyParam author_tag_id array The Ids of a author tag Example: [1, 2]
* @bodyParam organization_visibility_type_id integer required Id of the organization visibility type Example: 1
*
* @responseFile 201 responses/independent-activity/independent-activity.json
*
* @response 500 {
* "errors": [
* "Could not create independent activity. Please try again later."
* ]
* }
*
* @param IndependentActivityCreateRequest $request
* @param Organization $suborganization
* @return Response
* @throws AuthorizationException
*/
public function store(IndependentActivityCreateRequest $request, Organization $suborganization)
{
$this->authorize('create', [IndependentActivity::class, $suborganization]);
$data = $request->validated();
$authenticatedUser = auth()->user();
$independentActivity = $this->independentActivityRepository->createIndependentActivity($authenticatedUser, $suborganization, $data);
if ($independentActivity) {
return response([
'independent-activity' => new IndependentActivityResource($independentActivity->refresh()),
], 201);
}
return response([
'errors' => ['Could not create independent activity. Please try again later.'],
], 500);
}
/**
* Get Independent Activity
*
* Get the specified independent activity.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @urlParam independent_activity required The Id of a independent activity Example: 1
*
* @responseFile responses/independent-activity/independent-activity.json
*
* @response 400 {
* "errors": [
* "Invalid organization or independent activity id."
* ]
* }
*
* @param Organization $suborganization
* @param IndependentActivity $independent_activity
* @return Response
*/
public function show(Organization $suborganization, IndependentActivity $independent_activity)
{
$this->authorize('view', $independent_activity);
if ($independent_activity->organization_id !== $suborganization->id) {
return response([
'errors' => ['Invalid organization or independent activity id.'],
], 400);
}
return response([
'independent-activity' => new IndependentActivityResource($independent_activity),
], 200);
}
/**
* Update Independent Activity
*
* Update the specified independent activity.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @urlParam independent_activity required The Id of a independent activity Example: 1
* @bodyParam title string required The title of a activity Example: Science of Golf: Why Balls Have Dimples
* @bodyParam type string required The type of a activity Example: h5p
* @bodyParam content string required The content of a activity Example:
* @bodyParam shared bool The status of share of a activity Example: false
* @bodyParam order integer The order number of a activity Example: 2
* @bodyParam h5p_content_id integer The Id of H5p content Example: 59
* @bodyParam thumb_url string The image url of thumbnail Example: null
* @bodyParam subject_id array The Ids of a subject Example: [1, 2]
* @bodyParam education_level_id array The Ids of a education level Example: [1, 2]
* @bodyParam author_tag_id array The Ids of a author tag Example: [1, 2]
* @bodyParam organization_visibility_type_id integer required Id of the organization visibility type Example: 1
*
* @responseFile responses/independent-activity/independent-activity.json
*
* @response 400 {
* "errors": [
* "Invalid organization or independent activity id."
* ]
* }
*
* @response 500 {
* "errors": [
* "Failed to update independent activity."
* ]
* }
*
* @param IndependentActivityEditRequest $request
* @param Organization $suborganization
* @param IndependentActivity $independent_activity
* @return Response
*/
public function update(
IndependentActivityEditRequest $request,
Organization $suborganization,
IndependentActivity $independent_activity
)
{
$this->authorize('update', $independent_activity);
if ($independent_activity->organization_id !== $suborganization->id) {
return response([
'errors' => ['Invalid organization or independent activity id.'],
], 400);
}
$validated = $request->validated();
return \DB::transaction(function () use ($validated, $independent_activity) {
$attributes = Arr::except($validated, ['data', 'subject_id', 'education_level_id', 'author_tag_id']);
$is_updated = $this->independentActivityRepository->update($attributes, $independent_activity->id);
if ($is_updated) {
if (isset($validated['subject_id'])) {
$independent_activity->subjects()->sync($validated['subject_id']);
}
if (isset($validated['education_level_id'])) {
$independent_activity->educationLevels()->sync($validated['education_level_id']);
}
if (isset($validated['author_tag_id'])) {
$independent_activity->authorTags()->sync($validated['author_tag_id']);
}
if (isset($validated['data'])) {
// H5P meta is in 'data' index of the payload.
$this->update_h5p($validated['data'], $independent_activity->h5p_content_id);
}
$updated_independent_activity = new IndependentActivityResource($this->independentActivityRepository->find($independent_activity->id));
return response([
'independent-activity' => $updated_independent_activity,
], 200);
}
return response([
'errors' => ['Failed to update independent activity.'],
], 500);
});
}
/**
* Update H5P
*
* Update H5P Content
*
* @urlParam id required The Id of hp5 content Example: 1
*
* @param $request
* @param int $id
*
* @return mixed
* @throws H5PException
*/
public function update_h5p($request, $id)
{
$h5p = App::make('LaravelH5p');
$core = $h5p::$core;
$editor = $h5p::$h5peditor;
$request['action'] = 'create';
$event_type = 'update';
$content = $h5p->load_content($id);
$content['disable'] = H5PCore::DISABLE_NONE;
$oldLibrary = $content['library'];
$oldParams = json_decode($content['params']);
$content['library'] = $core->libraryFromString($request['library']);
if (!$content['library']) {
throw new H5PException('Invalid library.');
}
// Check if library exists.
$content['library']['libraryId'] = $core->h5pF->getLibraryId(
$content['library']['machineName'],
$content['library']['majorVersion'],
$content['library']['minorVersion']
);
if (!$content['library']['libraryId']) {
throw new H5PException('No such library');
}
$content['params'] = $request['parameters'];
$params = json_decode($content['params']);
// $content['title'] = $params->metadata->title;
if ($params === NULL) {
throw new H5PException('Invalid parameters');
}
$content['params'] = json_encode($params->params);
$content['metadata'] = $params->metadata;
// Trim title and check length
$trimmed_title = empty($content['metadata']->title) ? '' : trim($content['metadata']->title);
if ($trimmed_title === '') {
throw new H5PException('Missing title');
}
if (strlen($trimmed_title) > 255) {
throw new H5PException('Title is too long. Must be 256 letters or shorter.');
}
// Set disabled features
$set = array(
H5PCore::DISPLAY_OPTION_FRAME => filter_input(INPUT_POST, 'frame', FILTER_VALIDATE_BOOLEAN),
H5PCore::DISPLAY_OPTION_DOWNLOAD => filter_input(INPUT_POST, 'download', FILTER_VALIDATE_BOOLEAN),
H5PCore::DISPLAY_OPTION_EMBED => filter_input(INPUT_POST, 'embed', FILTER_VALIDATE_BOOLEAN),
H5PCore::DISPLAY_OPTION_COPYRIGHT => filter_input(INPUT_POST, 'copyright', FILTER_VALIDATE_BOOLEAN),
);
$content['disable'] = $core->getStorableDisplayOptions($set, $content['disable']);
// Save new content
$core->saveContent($content);
// Move images and find all content dependencies
$editor->processParameters($content['id'], $content['library'], $params->params, $oldLibrary, $oldParams);
return $content['id'];
}
/**
* Get Independent Activity Detail
*
* Get the specified independent activity in detail.
*
* @urlParam independent_activity required The Id of a independent activity Example: 1
*
* @responseFile responses/independent-activity/independent-activity-with-detail.json
*
* @param IndependentActivity $independent_activity
* @return Response
*/
public function detail(IndependentActivity $independent_activity)
{
$this->authorize('view', $independent_activity);
$data = ['h5p_parameters' => null, 'user_name' => null, 'user_id' => null];
if ($independent_activity->user) {
$data['user_name'] = $independent_activity->user;
$data['user_id'] = $independent_activity->user->id;
}
if ($independent_activity->type === 'h5p') {
$h5p = App::make('LaravelH5p');
$core = $h5p::$core;
$editor = $h5p::$h5peditor;
$content = $h5p->load_content($independent_activity->h5p_content_id);
$library = $content['library'] ? \H5PCore::libraryToString($content['library']) : 0;
$data['h5p_parameters'] = '{"params":' . $core->filterParameters($content) . ',"metadata":' . json_encode((object)$content['metadata']) . '}';
}
return response([
'independent-activity' => new IndependentActivityDetailResource($independent_activity, $data),
], 200);
}
/**
* Share Independent Activity
*
* Share the specified independent activity.
*
* @urlParam independent_activity required The Id of a independent activity Example: 1
*
* @responseFile responses/independent-activity/independent-activity.json
*
* @response 500 {
* "errors": [
* "Failed to share independent activity."
* ]
* }
*
* @param IndependentActivity $independent_activity
* @return Response
*/
public function share(IndependentActivity $independent_activity)
{
$this->authorize('share', $independent_activity);
$is_updated = $this->independentActivityRepository->update([
'shared' => true,
], $independent_activity->id);
if ($is_updated) {
$updated_independent_activity = new IndependentActivityResource($this->independentActivityRepository->find($independent_activity->id));
return response([
'independent-activity' => $updated_independent_activity,
], 200);
}
return response([
'errors' => ['Failed to share independent activity.'],
], 500);
}
/**
* Remove Share Independent Activity
*
* Remove share the specified independent activity.
*
* @urlParam independent_activity required The Id of a independent activity Example: 1
*
* @responseFile responses/independent-activity/independent-activity.json
*
* @response 500 {
* "errors": [
* "Failed to remove share independent activity."
* ]
* }
*
* @param IndependentActivity $independent_activity
* @return Response
*/
public function removeShare(IndependentActivity $independent_activity)
{
$this->authorize('share', $independent_activity);
$is_updated = $this->independentActivityRepository->update([
'shared' => false,
], $independent_activity->id);
if ($is_updated) {
$updated_independent_activity = new IndependentActivityResource($this->independentActivityRepository->find($independent_activity->id));
return response([
'independent-activity' => $updated_independent_activity,
], 200);
}
return response([
'errors' => ['Failed to remove share independent activity.'],
], 500);
}
/**
* Remove Independent Activity
*
* Remove the specified independent activity.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @urlParam independent_activity required The Id of a independent activity Example: 1
*
* @response {
* "message": "Independent activity has been deleted successfully."
* }
*
* @response 500 {
* "errors": [
* "Failed to delete independent activity."
* ]
* }
*
* @param Organization $suborganization
* @param IndependentActivity $independent_activity
* @return Response
*/
public function destroy(Organization $suborganization, IndependentActivity $independent_activity)
{
$this->authorize('delete', $independent_activity);
if ($independent_activity->organization_id !== $suborganization->id) {
return response([
'errors' => ['Invalid organization or independent activity id.'],
], 400);
}
$is_deleted = $this->independentActivityRepository->delete($independent_activity->id);
if ($is_deleted) {
return response([
'message' => 'Independent activity has been deleted successfully.',
], 200);
}
return response([
'errors' => ['Failed to delete independent activity.'],
], 500);
}
/**
* Clone Independent Activity
*
* Clone the specified independent activity of an suborganization.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @urlParam independent_activity required The Id of a independent activity Example: 1
*
* @response {
* "message": "Independent Activity is being cloned|duplicated in background!"
* }
*
* @response 400 {
* "errors": [
* "Not a Public Independent Activity."
* ]
* }
*
* @response 500 {
* "errors": [
* "Failed to clone independent activity."
* ]
* }
*
* @param Request $request
* @param Organization $suborganization
* @param IndependentActivity $independent_activity
* @return Response
*/
public function clone(Request $request, Organization $suborganization, IndependentActivity $independent_activity)
{
$this->authorize('clone', $independent_activity);
CloneIndependentActivity::dispatch($suborganization, $independent_activity, $request->bearerToken())->delay(now()->addSecond());
$isDuplicate = ($independent_activity->organization_id == $suborganization->id);
$process = ($isDuplicate) ? "duplicate" : "clone";
return response([
"message" => "Your request to $process independent activity [$independent_activity->title] has been received and is being processed. <br>
You will be alerted in the notification section in the title bar when complete.",
], 200);
}
/**
* H5P Independent Activity
*
* @urlParam independent_activity required The Id of an independent activity Example: 1
*
* @responseFile responses/independent-activity/independent-activity-h5p.json
*
* @param IndependentActivity $independent_activity
* @return Response
*/
public function h5p(IndependentActivity $independent_activity)
{
$this->authorize('view', $independent_activity);
$h5p = App::make('LaravelH5p');
$core = $h5p::$core;
$settings = $h5p::get_editor($content = null, 'preview');
$content = $h5p->load_content($independent_activity->h5p_content_id);
$content['disable'] = config('laravel-h5p.h5p_preview_flag');
$embed = $h5p->get_embed($content, $settings);
$embed_code = $embed['embed'];
$settings = $embed['settings'];
$user = Auth::user();
// create event dispatch
event(new H5pEvent(
'content',
NULL,
$content['id'],
$content['title'],
$content['library']['name'],
$content['library']['majorVersion'] . '.' . $content['library']['minorVersion']
));
$user_data = $user->only(['id', 'name', 'email']);
$h5p_data = ['settings' => $settings, 'user' => $user_data, 'embed_code' => $embed_code];
return response([
'independent-activity' => new H5pIndependentActivityResource($independent_activity, $h5p_data)
], 200);
}
//download inpendent activity
public function h5pActivity(Request $request){
$h5pcontent = H5pContent::find($request->id);
$independent_activity = $h5pcontent->independentActivity;
$this->authorize('view', $independent_activity);
$zip = new ZipArchive;
$h5p = App::make('LaravelH5p');
$core = $h5p::$core;
$settings = $h5p::get_editor($content = null, 'preview');
$content = $h5p->load_content($independent_activity->h5p_content_id);
$content['disable'] = config('laravel-h5p.h5p_preview_flag');
$embed = $h5p->get_embed($content, $settings);
$embed_code = $embed['embed'];
$settings = $embed['settings'];
$user = Auth::user();
// create event dispatch
event(new H5pEvent(
'content',
NULL,
$content['id'],
$content['title'],
$content['library']['name'],
$content['library']['majorVersion'] . '.' . $content['library']['minorVersion']
));
$user_data = $user->only(['id', 'name', 'email']);
$h5p_data = ['settings' => $settings, 'user' => $user_data, 'embed_code' => $embed_code];
$data[] = $h5p_data;
$data[] = $independent_activity;
Storage::disk('public')->put('/exports/'.$request->id.'/'.$request->id.'-h5p.json', json_encode($data));
$rootPath = storage_path('app/public/exports/'.$request->id);
return response([
'url'=> url('storage/exports/'.$request->id.'/'.$request->id.'-h5p.json'),
'name'=> $request->id
], 200);
$zipFileName = $request->id.'.zip';
$zip->open(storage_path('app/public/exports/'.$request->id.'.zip'), ZipArchive::CREATE );
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
return url('storage/exports/'.$zipFileName);
}
/**
* Get H5P Resource Settings
*
* Get H5P Resource Settings for an independent activity
*
* @urlParam independent_activity required The Id of an independent activity Example: 1
*
* @responseFile responses/h5p/independent-h5p-resource-settings-open.json
*
* @response 500 {
* "errors": [
* "Independent Activity doesn't belong to this user."
* ]
* }
*
* @param IndependentActivity $independent_activity
* @return Response
*/
public function getH5pResourceSettings(IndependentActivity $independent_activity)
{
if ($independent_activity->type === 'h5p') {
$h5p = App::make('LaravelH5p');
$core = $h5p::$core;
$settings = $h5p::get_editor($content = null, 'preview');
$content = $h5p->load_content($independent_activity->h5p_content_id);
$content['disable'] = config('laravel-h5p.h5p_preview_flag');
$embed = $h5p->get_embed($content, $settings);
$embed_code = $embed['embed'];
$settings = $embed['settings'];
$user_data = null;
$h5p_data = ['settings' => $settings, 'user' => $user_data, 'embed_code' => $embed_code];
}
$brightcoveContentData = H5pBrightCoveVideoContents::where('h5p_content_id', $independent_activity->h5p_content_id)->first();
$brightcoveData = null;
if ($brightcoveContentData && $brightcoveContentData->brightcove_api_setting_id) {
$bcAPISettingRepository = $this->bcAPISettingRepository->find($brightcoveContentData->brightcove_api_setting_id);
$brightcoveData = ['videoId' => $brightcoveContentData->brightcove_video_id, 'accountId' => $bcAPISettingRepository->account_id];
}
return response([
'h5p' => $h5p_data,
'activity' => new IndependentActivityResource($independent_activity),
'organization' => new H5pOrganizationResource($independent_activity->organization),
'brightcoveData' => $brightcoveData,
], 200);
}
/**
* Get H5P Resource Settings (Shared)
*
* Get H5P Resource Settings for a shared independent activity
*
* @urlParam independent_activity required The Id of an independent activity
*
* @responseFile responses/h5p/independent-h5p-resource-settings-open.json
*
* @response 400 {
* "errors": [
* "Independent Activity is not shareable."
* ]
* }
*
* @param IndependentActivity $independent_activity
* @return Response
*/
public function getH5pResourceSettingsShared(IndependentActivity $independent_activity)
{
// 3 is for indexing approved - see IndependentActivity Model @indexing property
if ($independent_activity->shared) {
$h5p = App::make('LaravelH5p');
$core = $h5p::$core;
$settings = $h5p::get_editor();
$content = $h5p->load_content($independent_activity->h5p_content_id);
$content['disable'] = config('laravel-h5p.h5p_preview_flag');
$embed = $h5p->get_embed($content, $settings);
$embed_code = $embed['embed'];
$settings = $embed['settings'];
$user_data = null;
$h5p_data = ['settings' => $settings, 'user' => $user_data, 'embed_code' => $embed_code];
return response([
'h5p' => $h5p_data,
'independent-activity' => new IndependentActivityResource($independent_activity)
], 200);
}
return response([
'errors' => ['Independent Activity is not shareable.']
], 400);
}
/**
* @uses One time script to populate all missing order number
*/
public function populateOrderNumber()
{
$this->activityRepository->populateOrderNumber();
}
/**
* Get Independent Activity Search Preview
*
* Get the specified independent activity search preview.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @urlParam independent_activity required The Id of an independent activity Example: 1
*
* @responseFile responses/h5p/independent-h5p-resource-settings-open.json
*
* @param Organization $suborganization
* @param IndependentActivity $independent_activity
* @return Response
*/
public function searchPreview(Organization $suborganization, IndependentActivity $independent_activity)
{
$this->authorize('searchPreview', [$independent_activity, $suborganization]);
$h5p = App::make('LaravelH5p');
$core = $h5p::$core;
$settings = $h5p::get_editor();
$content = $h5p->load_content($independent_activity->h5p_content_id);
$content['disable'] = config('laravel-h5p.h5p_preview_flag');
$embed = $h5p->get_embed($content, $settings);
$embed_code = $embed['embed'];
$settings = $embed['settings'];
$user_data = null;
$h5p_data = ['settings' => $settings, 'user' => $user_data, 'embed_code' => $embed_code];
return response([
'h5p' => $h5p_data,
'activity' => new IndependentActivityResource($independent_activity),
], 200);
}
/**
* Download XApi File
*
* This is an API for to download the XAPI zip for the attempted independent activity
*
* @urlParam independent_activity required id, title, slug of an independent_activity
*
* @param Request $request
* @param IndependentActivity $independent_activity
*
* @return download file download for the independent activity XAPI zip download
*/
public function getXAPIFileForIndepActivity(Request $request, IndependentActivity $independent_activity)
{
return Storage::download($this->lms->getXAPIFileForIndepActivity($independent_activity));
}
/**
* Export Independent Activity
*
* Export the specified activity of a user.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @urlParam independent_activity required The Id of a independent_activity Example: 1
*
* @response {
* "message": "Your request to export independent Activity [title] has been received and is being processed."
* }
*
* @param Request $request
* @param Organization $suborganization
* @param IndependentActivity $independent_activity
* @return Response
*/
public function exportIndependentActivity(Request $request, Organization $suborganization, IndependentActivity $independent_activity)
{
$this->authorize('export', $independent_activity);
// pushed cloning of activity in background
ExportIndependentActivity::dispatch(auth()->user(), $independent_activity, $suborganization)->delay(now()->addSecond());
return response([
'message' => "Your request to export independent Activity [$independent_activity->title] has been received and is being processed. <br>
You will be alerted in the notification section in the title bar when complete.",
], 200);
}
/**
* Import Independent Activity
*
* Import the specified independent activity of a user.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @param IndependentActivityUploadImportRequest $IndependentActivityUploadImportRequest
* @param Organization $suborganization
* @response {
* "message": "Your request to import independent activity has been received and is being processed."
* }
*
* @return Response
*/
public function importIndependentActivity(IndependentActivityUploadImportRequest $IndependentActivityUploadImportRequest, Organization $suborganization)
{
$this->authorize('import', [IndependentActivity::class, $suborganization]);
$IndependentActivityUploadImportRequest->validated();
$path = $IndependentActivityUploadImportRequest->file('independent_activity')->store('public/imports');
ImportIndependentActivity::dispatch(auth()->user(), Storage::url($path), $suborganization->id)->delay(now()->addSecond());
return response([
'message' => "Your request to import independent activity has been received and is being processed. <br>
You will be alerted in the notification section in the title bar when complete.",
], 200);
}
/**
* Independent Activity Indexing
*
* Modify the index value of an independent activity.
*
* @urlParam independent_activity required The Id of a independent_activity Example: 1
* @urlParam index required New Integer Index Value, 1 => 'REQUESTED', 2 => 'NOT APPROVED', 3 => 'APPROVED'. Example: 3
*
* @response {
* "message": "Library status changed successfully!",
* }
*
* @response 500 {
* "errors": [
* "Invalid index value provided."
* ]
* }
*
* @param IndependentActivity $independent_activity
* @param $index
* @return Application|ResponseFactory|Response
* @throws GeneralException
*/
public function updateIndex(IndependentActivity $independent_activity, $index)
{
return response(['message' => $this->independentActivityRepository->updateIndex($independent_activity, $index)], 200);
}
/**
* Copy Independent Activity into Playlist
*
* Clone the specified independent activity of an suborganization and link with a playlist.
*
* @urlParam suborganization required The Id of a suborganization Example: 1
* @urlParam independent_activity required The Id of a independent activity Example: 1
* @urlParam playlist required The Id of a playlist Example: 1
*
* @response {
* "message": "Independent Activity is being copied in background!"
* }
*
* @response 400 {
* "errors": [
* "Not a Public Independent Activity."
* ]
* }
*
* @response 500 {
* "errors": [
* "Failed to copy independent activity."
* ]
* }
*
* @param Request $request
* @param Organization $suborganization
* @param IndependentActivity $independent_activity
* @param Playlist $playlist
* @return Response
*/
public function copyIndependentActivityIntoPlaylist(Request $request, Organization $suborganization, IndependentActivity $independent_activity, Playlist $playlist)
{
CopyIndependentActivityIntoPlaylist::dispatch($suborganization, $independent_activity, $playlist, $request->bearerToken())->delay(now()->addSecond());