Skip to content

Commit

Permalink
Feature/seeder (#42)
Browse files Browse the repository at this point in the history
* the seeder

* swagger badge
  • Loading branch information
qunabu authored May 5, 2021
1 parent b0ea92a commit 9b0b9fc
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 13 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"autoload": {
"psr-4": {
"EscolaLms\\HeadlessH5P\\": "src",
"EscolaLms\\HeadlessH5P\\Tests\\": "tests"
"EscolaLms\\HeadlessH5P\\Tests\\": "tests",
"EscolaLms\\HeadlessH5P\\Database\\Factories\\": "database/factories",
"EscolaLms\\HeadlessH5P\\Database\\Seeders\\": "database/seeders"
}
},
"license": "Apache-2.0",
Expand All @@ -33,4 +35,4 @@
]
}
}
}
}
119 changes: 119 additions & 0 deletions database/seeders/ContentLibrarySeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace EscolaLms\HeadlessH5P\Database\Seeders;

use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class ContentLibrarySeeder extends Seeder
{
private HeadlessH5PServiceContract $hh5pService;

private function downloadAndSeed($lib, $addContent = false)
{
// eg https://h5p.org/sites/default/files/h5p/exports/interactive-video-2-618.h5p
$url = "https://h5p.org/sites/default/files/h5p/exports/$lib";

$filename = storage_path("app/h5p/temp/$lib");

$dirname = dirname($filename);

if (!is_dir($dirname)) {
mkdir($dirname, 0777, true);
}

if (!is_file($filename)) {
if (file_put_contents($filename, file_get_contents($url))) {
echo "File downloaded from $url \n";
} else {
echo "File downloading failed. \n";
return;
}
}

copy($filename, $this->hh5pService->getRepository()->getUploadedH5pPath());

// Content update is skipped because it is new registration
$content = null;
$skipContent = !$addContent;
$h5p_upgrade_only = false;

if ($this->hh5pService->getValidator()->isValidPackage($skipContent, $h5p_upgrade_only)) {
$this->hh5pService->getStorage()->savePackage($content, null, $skipContent);
} else {
echo "Invalid package $filename \n";
}

@unlink($this->hh5pService->getRepository()->getUploadedH5pPath());

return true;
}

/**
* Execute the console command.
*
* @return void
*/
public function run(HeadlessH5PServiceContract $hh5pService)
{
$addContent = true;

$this->hh5pService = $hh5pService;
$libs = [
"example-content-arts-of-europe-443085.h5p",
"advanced-blanks-example-1-503253.h5p",
"course-presentation-21-21180.h5p",
"interactive-video-2-618.h5p",
"example-content-virtual-tour-360-441814.h5p",
"true-false-question-34806.h5p",
"timeline-3-716.h5p",
"summary-714.h5p",
"speak-the-words-set-example-120784.h5p",
"speak-the-words-73595.h5p",
"example-content-single-choice-set-64682.h5p",
"question-set-616.h5p",
"questionnaire-4-30615.h5p",
"personality-quiz-21254.h5p",
"multiple-choice-713.h5p",
"memory-game-5-708.h5p",
"mark-the-words-2-1408.h5p",
"contact-18-1022298.h5p",
"berries-28-441940.h5p",
"impressive-presentation-7141.h5p",
"image-slider-2-130336.h5p",
"image-sequencing-3-110117.h5p",
"example-content-image-pairing-2-233382.h5p",
"image-juxtaposition-65047.h5p",
"image-hotspots-2-825.h5p",
"iframe-embedder-621.h5p",
"image-multiple-hotspot-question-65081.h5p",
"find-the-hotspot-3024.h5p",
"example-content-find-the-words-557697.h5p",
"flashcards-51-111820.h5p",
"guess-the-answer-2402.h5p",
"fill-in-the-blanks-837.h5p",
"essay-4-166755.h5p",
"drag-the-words-1399.h5p",
"drag-and-drop-712.h5p",
"documentation-tool-3022.h5p",
"chart-7143.h5p",
"collage-3065.h5p",
"h5p-column-34794.h5p",
"dialog-cards-620.h5p",
"dictation-389727.h5p",
"audio-recorder-87-748022.h5p",
"arithmetic-quiz-22-57860.h5p",
"agamotto-80158.h5p",
"advent-blue-snowman-1075566.h5p",
"accordion-6-7138.h5p"
];

foreach ($libs as $lib) {
echo "seeding $lib \n";
$this->downloadAndSeed($lib, $addContent);
}
}
}
23 changes: 12 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Headless H5P Laravel API

[![swagger](https://img.shields.io/badge/documentation-swagger-green)](https://escolalms.github.io/H5P/)
[![codecov](https://codecov.io/gh/EscolaLMS/H5P/branch/main/graph/badge.svg?token=ci4VPQbrOI)](https://codecov.io/gh/EscolaLMS/H5P)
[![phpunit](https://github.com/EscolaLMS/H5P/actions/workflows/test.yml/badge.svg)](https://github.com/EscolaLMS/Core/actions/workflows/test.yml)
[![downloads](https://img.shields.io/packagist/dt/escolalms/headless-h5p)](https://packagist.org/packages/escolalms/headless-h5p)
Expand All @@ -8,7 +9,7 @@

## Features

The lib allows headlessly
The lib allows headlessly

- play all h5p content - exposed all essential data, yet player is needed
- edit all h5p content - exposed all essential data, yet editor is needed
Expand All @@ -19,7 +20,7 @@ The lib allows headlessly

See [Swagger](https://escolalms.github.io/H5P/) documented endpoints.

Some [tests](tests) can also be a great point of start.
Some [tests](tests) can also be a great point of start.

To play the content you can use [EscolaLMS H5P Player](https://github.com/EscolaLMS/H5P-player)

Expand All @@ -34,7 +35,7 @@ You need to publish many of files to be availabe as public link.

`php artisan h5p:link` which creates a symbolic link from `storage/app/h5` and `vendor/h5p/h5p-core` and `vendor/h5p/h5p-editor` to be accesible to public, as follows

```
```
public_path('h5p') => storage_path('app/h5p'),
public_path('h5p-core') => base_path().'vendor/h5p/h5p-core',
public_path('h5p-editor') => base_path().'vendor/h5p/h5p-editor',
Expand All @@ -59,12 +60,12 @@ You can seed library and content with build-in seeders that are accessible with
- `php artisan h5p:seed` to add just libraries
- `php artisan h5p:seed --addContent` to add content with libraries

## Road map
## Road map

- caching
- content export
- some transaltions are missing
- casading delete
- sql foreign keys indexing
- clearup task - deleting temp files, marked for delete
- in some contents, js libs content is invalid
- caching
- content export
- some transaltions are missing
- casading delete
- sql foreign keys indexing
- clearup task - deleting temp files, marked for delete
- in some contents, js libs content is invalid
3 changes: 3 additions & 0 deletions src/Repositories/H5PRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ public function updateContent($content, $contentMainId = null)
H5PContent::findOrFail($content['id'])->update($content);
return $content['id'];
} else {
unset($content['params']);
unset($content['library']);

$newContent = H5PContent::create($content);
return $newContent->id;
}
Expand Down

0 comments on commit 9b0b9fc

Please sign in to comment.