Skip to content

Commit

Permalink
Merge tag 'v2.0.2' from jorisdugue/h5p-bundle into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nieck committed Sep 25, 2023
2 parents 152dc28 + 8a17e2e commit 50c1264
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 106 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: CI

on: [push]

jobs:
build-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6
- uses: php-actions/phpstan@v3
with:
memory_limit: -1
17 changes: 14 additions & 3 deletions Command/H5pBundleCleanUpFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

namespace Emmedy\H5PBundle\Command;

use Emmedy\H5PBundle\Core\H5POptions;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class H5pBundleCleanUpFilesCommand extends Command
{

/**
* @var H5POptions $h5POptions
*/
private $h5POptions;

public function __construct(H5POptions $h5POptions)
{
$this->h5POptions = $h5POptions;
parent::__construct();
}

protected static $defaultName = 'h5p-bundle:cleanup-files';
protected function configure()
{
Expand All @@ -31,7 +42,7 @@ private function cleanupFiles(InputInterface $input)
{
$location = $input->getArgument('location');
if (!$location) {
$location = $this->get('emmedy_h5p.options')->getAbsoluteH5PPath() . '/editor';
$location = $this->h5POptions->getAbsoluteH5PPath() . '/editor';
}
\H5PCore::deleteFileTree($location);
}
Expand Down
20 changes: 6 additions & 14 deletions Controller/H5PAJAXController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ public function TranslationsCallback(Request $request)
/**
* Callback Install library from external file
*
* @param string $token Security token
* @param int $content_id Id of content
* @param string $machine_name Machine name of library
* @param Request $request
*
* @return JsonResponse
* @Route("/library-install/")
*/
public function libraryInstallCallback(Request $request)
Expand All @@ -121,11 +119,8 @@ public function libraryInstallCallback(Request $request)
/**
* Callback that returns data for a given library
*
* @param string $machine_name Machine name of library
* @param int $major_version Major version of library
* @param int $minor_version Minor version of library
* @param string $locale Language of your website and plugins for default is English (EN)
* @param Request $request
* @return JsonResponse
*/
private function libraryCallback(Request $request)
{
Expand All @@ -140,7 +135,7 @@ private function libraryCallback(Request $request)
$request->get('majorVersion'),
$request->get('minorVersion'),
$locale,
$this->h5POptions->getOption('storage_dir'),
$this->serviceh5poptions->getOption('storage_dir'),
'',
$locale
);
Expand All @@ -158,10 +153,9 @@ private function libraryCallback(Request $request)
/**
* Callback for uploading a library
*
* @param string $token Editor security token
* @param int $content_id Id of content that is being edited
* @param Request $request
*
* @return JsonResponse
* @throws Exception
* @Route("/library-upload/")
*/
Expand Down Expand Up @@ -194,9 +188,8 @@ public function libraryUploadCallback(Request $request)
/**
* Callback for file uploads.
*
* @param string $token SecuritlibraryCallbacky token
* @param int $content_id Content id
* @param Request $request
* @return JsonResponse
* @Route("/files/")
*/
public function filesCallback(Request $request)
Expand All @@ -220,9 +213,8 @@ public function filesCallback(Request $request)
/**
* Callback for filtering.
*
* @param string $token Security token
* @param int $content_id Content id
* @param Request $request
* @return JsonResponse
* @Route("/filter/")
*/
public function filterCallback(Request $request)
Expand Down
11 changes: 8 additions & 3 deletions Controller/H5PController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Emmedy\H5PBundle\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Emmedy\H5PBundle\Core\H5POptions;
use Emmedy\H5PBundle\Editor\LibraryStorage;
use Emmedy\H5PBundle\Entity\Content;
Expand All @@ -22,13 +23,16 @@ class H5PController extends AbstractController

protected $h5PIntegrations;
protected $libraryStorage;
protected $entityManager;

public function __construct(
H5PIntegration $h5PIntegration,
LibraryStorage $libraryStorage
LibraryStorage $libraryStorage,
EntityManagerInterface $entityManager
) {
$this->h5PIntegrations = $h5PIntegration;
$this->libraryStorage = $libraryStorage;
$this->entityManager = $entityManager;
}

/**
Expand All @@ -37,7 +41,7 @@ public function __construct(
*/
public function listAction()
{
$contents = $this->getDoctrine()->getRepository('Emmedy\H5PBundle\Entity\Content')->findAll();
$contents = $this->entityManager->getRepository('Emmedy\H5PBundle\Entity\Content')->findAll();
return $this->render('@EmmedyH5P/list.html.twig', ['contents' => $contents]);
}
/**
Expand Down Expand Up @@ -78,11 +82,12 @@ public function newAction(Request $request)
{
return $this->handleRequest($request );
}

/**
* @Route("edit/{content}")
* @param Request $request
* @param Content $content
* @return
* @return RedirectResponse|Response
*/
public function editAction(Request $request, Content $content)
{
Expand Down
82 changes: 59 additions & 23 deletions Controller/H5PInteractionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,50 @@
namespace Emmedy\H5PBundle\Controller;


use Doctrine\ORM\EntityManagerInterface;
use Exception;
use H5PCore;
use Emmedy\H5PBundle\Core\H5PIntegration;
use Emmedy\H5PBundle\Core\H5POptions;
use Emmedy\H5PBundle\Entity\Content;
use Emmedy\H5PBundle\Entity\ContentUserData;
use Emmedy\H5PBundle\Service\ResultService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @Route("/h5p/interaction")
*/
class H5PInteractionController extends AbstractController{

protected $entityManager;
protected $resultService;
protected $serializer;
protected $assetsPaths;
protected $options;
protected $h5PIntegration;
protected $h5PCore;
protected $kernel;

public function __construct(EntityManagerInterface $entityManager, ResultService $resultService, SerializerInterface $serializer, Packages $packages, H5POptions $options, H5PIntegration $h5PIntegration, H5PCore $h5PCore, KernelInterface $kernel)
{
$this->entityManager = $entityManager;
$this->resultService = $resultService;
$this->serializer = $serializer;
$this->assetsPaths = $packages;
$this->options = $options;
$this->h5PIntegration = $h5PIntegration;
$this->h5PCore = $h5PCore;
$this->kernel = $kernel;
}

/**
* Access callback for the setFinished feature
*
Expand All @@ -32,9 +62,9 @@ public function setFinished(Request $request, $token)
\H5PCore::ajaxError('Invalid security token');
}
/* @var ResultService $rs */
$rs = $this->get('emmedy_h5p.result_storage');
$result = $rs->handleRequestFinished($request, $this->getUser()->getId());
$em = $this->getDoctrine()->getManager();
$rs = $this->resultService;
$result = $rs->handleRequestFinished($request, $this->getUserId($this->getUser()));
$em = $this->entityManager;
$em->persist($result);
$em->flush();
return new JsonResponse(['success' => true]);
Expand All @@ -61,7 +91,7 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon
$data = $request->get("data");
$preload = $request->get("preload");
$invalidate = $request->get("invalidate");
$em = $this->getDoctrine()->getManager();
$em = $this->entityManager;
if ($data !== NULL && $preload !== NULL && $invalidate !== NULL) {
if(!\H5PCore::validToken('contentuserdata', $request->get("token"))){
return new JsonResponse(['success' => false, 'message' => 'No content']);
Expand All @@ -70,7 +100,7 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon
if ($data === '0'){
//remove data here
/* @var ResultService $rs */
$rs = $this->get('emmedy_h5p.result_storage');
$rs = $this->resultService;
$rs->removeData($contentId, $dataType, $user, $subContentId);
}else{
// Wash values to ensure 0 or 1.
Expand All @@ -85,7 +115,7 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon
'subContentId' => $subContentId,
'mainContent' => $contentId,
'dataId' => $dataType,
'user' => $user->getId()
'user' => $this->getUserId($user),
]
);
if(!$update){
Expand All @@ -94,16 +124,14 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon
* @var ContentUserData $contentUserData
*/
$contentUserData = new ContentUserData();
$contentUserData->setUser($user->getId());
$contentUserData->setUser($this->getUserId($user));
$contentUserData->setData($data);
$contentUserData->setDataId($dataType);
$contentUserData->setSubContentId($subContentId);
$contentUserData->setPreloaded($preload);
$contentUserData->setDeleteOnContentChange($invalidate);
$contentUserData->setTimestamp( time ());
/**
* @var $content Content
*/
/** @var Content|null $content */
$content = $em->getRepository('Emmedy\H5PBundle\Entity\Content')->findOneBy(['id' => $contentId]);
$contentUserData->setMainContent($content);
$em->persist($contentUserData);
Expand All @@ -126,14 +154,14 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon
'subContentId' => $subContentId,
'mainContent' => $contentId,
'dataId' => $dataType,
'user' => $user->getId()
'user' => $this->getUserId($user),
]
);

//decode for read the information
return new JsonResponse([
'success' => true,
'data' => json_decode($this->get('serializer')->serialize($data, 'json')),
'data' => json_decode($this->serializer->serialize($data, 'json')),
]);
}
}
Expand All @@ -154,18 +182,13 @@ public function embedAction(Request $request, Content $content)
],
];
$h5p_content = $content;
if (empty($h5p_content)){
//change url here
$response['#markup'] = '<body style="margin:0"><div style="background: #fafafa url(' . $this->getH5PAssetUrl() . '/h5p-core/images/h5p.svg) no-repeat center;background-size: 50% 50%;width: 100%;height: 100%;"></div><div style="width:100%;position:absolute;top:75%;text-align:center;color:#434343;font-family: Consolas,monaco,monospace">' . t('Content unavailable.') . '</div></body>';
return new Response($response['#markup']);
}
// Grab the core integration settings
$integration = $this->get('emmedy_h5p.integration')->getGenericH5PIntegrationSettings();
$integration = $this->h5PIntegration->getGenericH5PIntegrationSettings();
$content_id_string = 'cid-' . $content->getId();
// Add content specific settings
$integration['contents'][$content_id_string] = $this->get('emmedy_h5p.integration')->getH5PContentIntegrationSettings($content);
$preloaded_dependencies = $this->get('emmedy_h5p.core')->loadContentDependencies($content->getId(), 'preloaded');
$files = $this->get('emmedy_h5p.core')->getDependenciesFiles($preloaded_dependencies, $this->get('emmedy_h5p.options')->getRelativeH5PPath());
$integration['contents'][$content_id_string] = $this->h5PIntegration->getH5PContentIntegrationSettings($content);
$preloaded_dependencies = $this->h5PCore->loadContentDependencies($content->getId(), 'preloaded');
$files = $this->h5PCore->getDependenciesFiles($preloaded_dependencies, $this->options->getRelativeH5PPath());
// Load public files
$jsFilePaths = array_map(function ($asset) {
return $asset->path;
Expand All @@ -174,7 +197,7 @@ public function embedAction(Request $request, Content $content)
return $asset->path;
}, $files['styles']);
// Load core assets
$coreAssets = $this->get('emmedy_h5p.integration')->getCoreAssets();
$coreAssets = $this->h5PIntegration->getCoreAssets();
// Merge assets
$scripts = array_merge($coreAssets['scripts'], $jsFilePaths);
$styles = array_merge($cssFilePaths, $coreAssets['styles']);
Expand All @@ -187,9 +210,22 @@ public function embedAction(Request $request, Content $content)
'title' => "H5P Content {$id}",
];
//include the embed file (provide in h5p-core)
include $this->get('kernel')->getProjectDir().'/vendor/h5p/h5p-core/embed.php';
include $this->kernel->getProjectDir().'/vendor/h5p/h5p-core/embed.php';
$response['#markup'] = ob_get_clean();
//return nes Response HTML
return new Response($response['#markup']);
}

private function getH5PAssetUrl()
{
return $this->assetsPaths->getUrl($this->options->getH5PAssetPath());
}

private function getUserId(UserInterface $user)
{
if (method_exists($user, 'getId')) {
return $user->getId();
}
return $user->getUserIdentifier();
}
}
Loading

0 comments on commit 50c1264

Please sign in to comment.