Skip to content

Commit

Permalink
Merge pull request #49 from jorisdugue/feat/dev-h5p
Browse files Browse the repository at this point in the history
feat(chore, docs): upgrade deps and continue to cleanup the code
  • Loading branch information
jorisdugue authored Sep 21, 2023
2 parents 6ff62ca + a3c524e commit 817fba4
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ public/index.php
.idea/*
.idea
composer.lock
.DS_Store
.thumbs
1 change: 0 additions & 1 deletion Controller/H5PAJAXController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Studit\H5PBundle\Core\H5POptions;
use Studit\H5PBundle\Event\H5PEvents;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down
1 change: 0 additions & 1 deletion Editor/EditorAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\NotSupported;
use Studit\H5PBundle\Core\H5PUtils;
use Studit\H5PBundle\Entity\EventRepository;
use Studit\H5PBundle\Entity\LibraryRepository;
Expand Down
22 changes: 15 additions & 7 deletions Editor/EditorStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ class EditorStorage implements \H5peditorStorage
* @param EntityManagerInterface $entityManager
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(H5POptions $options, Filesystem $filesystem, AuthorizationCheckerInterface $authorizationChecker, EntityManagerInterface $entityManager, EventDispatcherInterface $eventDispatcher)
{
public function __construct(
H5POptions $options,
Filesystem $filesystem,
AuthorizationCheckerInterface $authorizationChecker,
EntityManagerInterface $entityManager,
EventDispatcherInterface $eventDispatcher
) {
$this->options = $options;
$this->filesystem = $filesystem;
$this->authorizationChecker = $authorizationChecker;
Expand Down Expand Up @@ -98,7 +103,7 @@ public function getLanguage($machineName, $majorVersion, $minorVersion, $languag
* @return string[] Translation in JSON format
* @throws NotSupported
*/
public function getAvailableLanguages($machineName, $majorVersion, $minorVersion)
public function getAvailableLanguages($machineName, $majorVersion, $minorVersion): array
{
/** @var LibrariesLanguagesRepository $repo */
$repo = $this->entityManager->getRepository('Studit\H5PBundle\Entity\LibrariesLanguages');
Expand Down Expand Up @@ -129,7 +134,7 @@ public function keepFile($path)
* @param array $libraries List of library names + version to load info for
* @return array List of all libraries loaded
*/
public function getLibraries($libraries = null)
public function getLibraries($libraries = null): array
{
$canCreateRestricted = $this->authorizationChecker->isGranted('ROLE_H5P_CREATE_RESTRICTED_CONTENT_TYPES');
if ($libraries !== null) {
Expand All @@ -148,7 +153,10 @@ public function getLibraries($libraries = null)
// Mark old ones
// This is the newest
if (
($library->majorVersion === $existingLibrary->majorVersion && $library->minorVersion > $existingLibrary->minorVersion) ||
(
$library->majorVersion === $existingLibrary->majorVersion &&
$library->minorVersion > $existingLibrary->minorVersion
) ||
($library->majorVersion > $existingLibrary->majorVersion)
) {
$existingLibrary->isOld = true;
Expand All @@ -169,7 +177,7 @@ public function getLibraries($libraries = null)
* @param $canCreateRestricted
* @return array
*/
private function getLibrariesWithDetails($libraries, $canCreateRestricted)
private function getLibrariesWithDetails($libraries, $canCreateRestricted): array
{
$librariesWithDetails = [];
foreach ($libraries as $library) {
Expand All @@ -181,7 +189,7 @@ private function getLibrariesWithDetails($libraries, $canCreateRestricted)
$library->tutorialUrl = $details->getTutorialUrl();
$library->title = $details->getTitle();
$library->runnable = $details->isRunnable();
$library->restricted = $canCreateRestricted ? false : ($details->isRestricted() === '1');
$library->restricted = !$canCreateRestricted && $details->isRestricted();
$library->metadataSettings = json_decode($details->getMetadataSettings());
$librariesWithDetails[] = $library;
}
Expand Down
6 changes: 5 additions & 1 deletion Editor/LibraryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public function storeLibraryData($library, $parameters, Content $content = null)
$libraryData = Utilities::getLibraryProperties($library);
/** @var LibraryRepository $libraryRepo */
$libraryRepo = $this->entityManager->getRepository('Studit\H5PBundle\Entity\Library');
$libraryData['libraryId'] = $libraryRepo->findIdBy($libraryData['machineName'], $libraryData['majorVersion'], $libraryData['minorVersion']);
$libraryData['libraryId'] = $libraryRepo->findIdBy(
$libraryData['machineName'],
$libraryData['majorVersion'],
$libraryData['minorVersion']
);
if ($content) {
$oldLibrary = [
'name' => $content->getLibrary()->getMachineName(),
Expand Down
3 changes: 1 addition & 2 deletions Editor/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ public static function getLibraryProperties($library)
$matches = [];
preg_match_all('/(.+)\s(\d+)\.(\d+)$/', $library, $matches);
if (count($matches) == 4) {
$libraryData = [
return [
'name' => $matches[1][0],
'machineName' => $matches[1][0],
'majorVersion' => $matches[2][0],
'minorVersion' => $matches[3][0],
];
return $libraryData;
}
return false;
}
Expand Down
24 changes: 18 additions & 6 deletions Entity/LibraryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function countContentLibrary($libraryId)
public function findLatestLibraryVersions(): array
{
$major_versions_sql = <<< EOT
SELECT hl.machine_name,
SELECT hl.machine_name,
MAX(hl.major_version) AS major_version
FROM h5p_library hl
WHERE hl.runnable = true
Expand Down Expand Up @@ -84,8 +84,7 @@ public function findLatestLibraryVersions(): array
$em = $this->getEntityManager();
/** @var Statement $stmt */
$stmt = $em->getConnection()->prepare($sql);
$result = $stmt->executeQuery();
$libraryVersions = $result->fetchAllAssociative();
$libraryVersions = $stmt->executeQuery()->fetchAllAssociative();
foreach ($libraryVersions as &$libraryVersion) {
$libraryVersion = (object)$libraryVersion;
}
Expand All @@ -96,7 +95,11 @@ public function findHasSemantics($machineName, $majorVersion, $minorVersion)
$qb = $this->createQueryBuilder('l')
->select('l')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion and l.semantics is not null')
->setParameters(['machineName' => $machineName, 'majorVersion' => $majorVersion, 'minorVersion' => $minorVersion]);
->setParameters([
'machineName' => $machineName,
'majorVersion' => $majorVersion,
'minorVersion' => $minorVersion
]);
try {
$library = $qb->getQuery()->getSingleResult();
} catch (NoResultException $e) {
Expand Down Expand Up @@ -128,7 +131,11 @@ public function findIdBy($machineName, $majorVersion, $minorVersion)
$qb = $this->createQueryBuilder('l')
->select('l.id')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion and l.semantics is not null')
->setParameters(['machineName' => $machineName, 'majorVersion' => $majorVersion, 'minorVersion' => $minorVersion]);
->setParameters([
'machineName' => $machineName,
'majorVersion' => $majorVersion,
'minorVersion' => $minorVersion
]);
try {
return $qb->getQuery()->getSingleScalarResult();
} catch (NoResultException $e) {
Expand All @@ -140,7 +147,12 @@ public function isPatched($library): bool
$qb = $this->createQueryBuilder('l')
->select('COUNT(l)')
->where('l.machineName = :machineName and l.majorVersion = :majorVersion and l.minorVersion = :minorVersion and l.patchVersion < :patchVersion')
->setParameters(['machineName' => $library['machineName'], 'majorVersion' => $library['majorVersion'], 'minorVersion' => $library['minorVersion'], 'patchVersion' => $library['patchVersion']]);
->setParameters([
'machineName' => $library['machineName'],
'majorVersion' => $library['majorVersion'],
'minorVersion' => $library['minorVersion'],
'patchVersion' => $library['patchVersion']
]);
return $qb->getQuery()->getSingleScalarResult() > 0;
}
}
83 changes: 56 additions & 27 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
# H5PBundle
Bundle to integrate H5P into Symfony. This bundle is a port of the H5P Drupal module. For more info about H5P see [H5P.org](https://h5p.org)

For Symfony 3.X => [H5PBundle for Symfony 2.X and 3.X](https://github.com/Emmedy/h5p-bundle)
Bundle to integrate H5P into Symfony. This bundle is a port of the H5P Drupal module. For more info about H5P
see [H5P.org](https://h5p.org)

This bundle was tested on Symfony 4.X, Symfony 5.X, Symfony 6.X, Symfony 7.X-dev
This bundle was tested on :

- Symfony 4.X
- Symfony 5.X,
- Symfony 6.X,
- Symfony 7.X-dev

| Version Supported | Symfony 3 | Symfony 4 | Symfony 5 | Symfony 6 | Symfony 7 |
|-------------------|---------------------------------------------------------------------------|-----------|-----------|-----------|-----------|
| 2.X | &#x274C; | &#x274C; | &#x2705; | &#x2705; | &#x2705; |
| 1.X | [H5PBundle for Symfony 2.X and 3.X](https://github.com/Emmedy/h5p-bundle) | &#x2705; | &#x2705; | &#x274C; | &#x274C; |

Prerequisite
------------

You need install doctrine annotations and orm before running this installation

```bash
composer require orm
composer require doctrine/annotations
Expand All @@ -18,78 +29,91 @@ Installation
------------

Install with composer

``` bash
composer require jorisdugue/h5p-bundle
```

Enable the bundle in `AppKernel.php`

```php
return [
// ...
\Studit\H5PBundle\StuditH5PBundle::class => ['all' => true]
]
```

**Warning for Windows** : You need launch the cmd in administrator or using option ``-c``

Add the H5P assets to the bundle

``` bash
php bin/console h5p-bundle:IncludeAssetsCommand
php bin/console assets:install --symlink
```

and reexecute ...

```bash
# For linux and mac only or windows admin
php bin/console h5p-bundle:IncludeAssetsCommand
# For all without admin perms
php bin/console h5p-bundle:IncludeAssetsCommand -c
```

Add required tables and relations to the database

``` bash
php bin/console doctrine:schema:update --force
```
or

or

````bash
php bin/console make:migrations
````

Enable the routing in `routes.yaml`

```yaml
studit_h5p.demo:
resource: "@StuditH5PBundle/Resources/config/routing_demo.yml"
prefix: /
resource: "@StuditH5PBundle/Resources/config/routing_demo.yml"
prefix: /

studit_h5p:
resource: "@StuditH5PBundle/Resources/config/routes.yaml"
prefix: /
resource: "@StuditH5PBundle/Resources/config/routes.yaml"
prefix: /
```
studit_h5p.demo is optional. It can be used as an example how to use H5P within Symfony and test if this bundle is working properly.
studit_h5p.demo is optional. It can be used as an example how to use H5P within Symfony and test if this bundle is
working properly.
Configuration
-------------
For SF4:
For SF4:
Configure the bundle in `services.yaml`. (Watch for the underscore between h5 and p)

```yaml
parameters:
studit_h5_p:
use_permission: true # This is false by default to let the demo work out of the box.
storage_dir: / # Location to store all H5P libraries and files
web_dir: public # Location of the public web directory
export: 3 #for all
embed: 3 #for all
studit_h5_p:
use_permission: true # This is false by default to let the demo work out of the box.
storage_dir: / # Location to store all H5P libraries and files
web_dir: public # Location of the public web directory
export: 3 #for all
embed: 3 #for all
```

For Symfony 5:
Configure the bundle in `packages > h5pBundle.yml`.

```yaml
studit_h5_p:
use_permission: true # This is false by default to let the demo work out of the box.
storage_dir: / # Location to store all H5P libraries and files
web_dir: public # Location of the public web directory
export: 3 #for all
embed: 3 #for all
use_permission: true # This is false by default to let the demo work out of the box.
storage_dir: / # Location to store all H5P libraries and files
web_dir: public # Location of the public web directory
export: 3 #for all
embed: 3 #for all
```

For all configurations see [Configuration.php](DependencyInjection/Configuration.php)
Expand All @@ -112,28 +136,33 @@ First add a virtual host that points to you project. Then in your browser go to

Todo
-------------
Working:
Working:

- Store usage data and points (only if user is connected)
- Download a H5P
- Upload H5P
- Update / Install H5P library
- Store usage data and points

Not everything is ported yet. The following things still need to be done:

* Upload library. Currently only H5P default libraries can be selected from Hub. (need custom h5p for testing )

Changelog:
Changelog:
-------------

- Using dev version with restrict tag for prevent Break Change
- Implement missing road and resolve compatibility of H5P-editor
- Fix bug and update the readme
- Fix many bug ... and update to SF5 :)
- Fix bug with missing link img
- Fix many bug ... and update to SF5 :)
- Fix bug with missing link img
- Fix Download package
- Store usage data and points


Developing:
-------------
Run the static analyzer like that:

```sh
php -d memory_limit=-1 vendor/bin/phpstan.phar analyze .
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"php": ">= 7.2.5",
"doctrine/orm": "^2.14.1",
"guzzlehttp/guzzle": "^7.8",
"h5p/h5p-core": "dev-master#51601da",
"h5p/h5p-core": "dev-master#6217d57",
"h5p/h5p-editor": "^1.25",
"symfony/framework-bundle": "~5.0|~6.0|~7.0",
"symfony/serializer": "~5.0|~6.0|~7.0",
Expand Down

0 comments on commit 817fba4

Please sign in to comment.