Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/claroline/ScormBundle int…
Browse files Browse the repository at this point in the history
…o bugfix

Conflicts:
	Controller/ScormController.php
	Resources/views/scorm2004.html.twig
  • Loading branch information
kitan1982 committed Oct 19, 2015
2 parents 5a00fac + 490b050 commit d063274
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 55 deletions.
96 changes: 70 additions & 26 deletions Controller/ScormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
use Claroline\ScormBundle\Manager\ScormManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
Expand All @@ -36,16 +38,18 @@ class ScormController extends Controller
private $eventDispatcher;
private $om;
private $request;
private $router;
private $scormManager;
private $tokenStorage;
private $authorization;

/**
* @DI\InjectParams({
* "eventDispatcher" = @DI\Inject("event_dispatcher"),
* "om" = @DI\Inject("claroline.persistence.object_manager"),
* "requestStack" = @DI\Inject("request_stack"),
* "scormManager" = @DI\Inject("claroline.manager.scorm_manager"),
* "eventDispatcher" = @DI\Inject("event_dispatcher"),
* "om" = @DI\Inject("claroline.persistence.object_manager"),
* "requestStack" = @DI\Inject("request_stack"),
* "router" = @DI\Inject("router"),
* "scormManager" = @DI\Inject("claroline.manager.scorm_manager"),
* "authorization" = @DI\Inject("security.authorization_checker"),
* "tokenStorage" = @DI\Inject("security.token_storage")
* })
Expand All @@ -54,6 +58,7 @@ public function __construct(
EventDispatcherInterface $eventDispatcher,
ObjectManager $om,
RequestStack $requestStack,
RouterInterface $router,
ScormManager $scormManager,
TokenStorageInterface $tokenStorage,
AuthorizationCheckerInterface $authorization
Expand All @@ -62,15 +67,17 @@ public function __construct(
$this->eventDispatcher = $eventDispatcher;
$this->om = $om;
$this->request = $requestStack;
$this->router = $router;
$this->scormManager = $scormManager;
$this->tokenStorage = $tokenStorage;
$this->authorization = $authorization;
}

/**
* @EXT\Route(
* "/render/scorm/12/{scormId}",
* name = "claro_render_scorm_12_resource"
* "/render/scorm/12/{scormId}/mode/{mode}",
* name = "claro_render_scorm_12_resource",
* defaults={"mode"=0}
* )
* @EXT\ParamConverter(
* "scorm",
Expand All @@ -83,14 +90,16 @@ public function __construct(
*
* @return Response
*/
public function renderScorm12ResourceAction(Scorm12Resource $scorm)
public function renderScorm12ResourceAction(Scorm12Resource $scorm, $mode = 0)
{
$this->checkAccess('OPEN', $scorm);
$user = $this->tokenStorage->getToken()->getUser();
$isAnon = ($user === 'anon.');
$rootScos = array();
$trackings = array();
$scos = $scorm->getScos();
$nbActiveScos = 0;
$lastActiveSco = null;

if (!$isAnon) {
$scosTracking = $this->scormManager
Expand All @@ -111,16 +120,32 @@ public function renderScorm12ResourceAction(Scorm12Resource $scorm)
$trackings[$sco->getId()] = $this->scormManager
->createScorm12ScoTracking($user, $sco);
}

if (!$sco->getIsBlock()) {
$nbActiveScos++;
$lastActiveSco = $sco;
}
}

if ($mode === 0 && $nbActiveScos === 1) {

return new RedirectResponse(
$this->router->generate(
'claro_render_scorm_12_sco',
array('scoId' => $lastActiveSco->getId())
)
);
} else {

return array(
'resource' => $scorm,
'_resource' => $scorm,
'scos' => $rootScos,
'workspace' => $scorm->getResourceNode()->getWorkspace(),
'trackings' => $trackings,
'isAnon' => $isAnon
);
return array(
'resource' => $scorm,
'_resource' => $scorm,
'scos' => $rootScos,
'workspace' => $scorm->getResourceNode()->getWorkspace(),
'trackings' => $trackings,
'isAnon' => $isAnon
);
}
}

/**
Expand Down Expand Up @@ -364,8 +389,9 @@ private function logScorm12ScoResult(

/**
* @EXT\Route(
* "/render/scorm/2004/{scormId}",
* name = "claro_render_scorm_2004_resource"
* "/render/scorm/2004/{scormId}/mode/{mode}",
* name = "claro_render_scorm_2004_resource",
* defaults={"mode"=0}
* )
* @EXT\ParamConverter(
* "scorm",
Expand All @@ -378,14 +404,16 @@ private function logScorm12ScoResult(
*
* @return Response
*/
public function renderScorm2004ResourceAction(Scorm2004Resource $scorm)
public function renderScorm2004ResourceAction(Scorm2004Resource $scorm, $mode = 0)
{
$this->checkScorm2004ResourceAccess('OPEN', $scorm);
$user = $this->tokenStorage->getToken()->getUser();
$isAnon = ($user === 'anon.');
$rootScos = array();
$trackings = array();
$scos = $scorm->getScos();
$nbActiveScos = 0;
$lastActiveSco = null;

if (!$isAnon) {
$scosTracking = $this->scormManager
Expand All @@ -406,16 +434,32 @@ public function renderScorm2004ResourceAction(Scorm2004Resource $scorm)
$trackings[$sco->getId()] = $this->scormManager
->createScorm2004ScoTracking($user, $sco);
}

if (!$sco->getIsBlock()) {
$nbActiveScos++;
$lastActiveSco = $sco;
}
}

return array(
'resource' => $scorm,
'_resource' => $scorm,
'scos' => $rootScos,
'workspace' => $scorm->getResourceNode()->getWorkspace(),
'trackings' => $trackings,
'isAnon' => $isAnon
);
if ($mode === 0 && $nbActiveScos === 1) {

return new RedirectResponse(
$this->router->generate(
'claro_render_scorm_2004_sco',
array('scoId' => $lastActiveSco->getId())
)
);
} else {

return array(
'resource' => $scorm,
'_resource' => $scorm,
'scos' => $rootScos,
'workspace' => $scorm->getResourceNode()->getWorkspace(),
'trackings' => $trackings,
'isAnon' => $isAnon
);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Entity/Scorm12Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Scorm12Resource extends AbstractResource
{
/**
* @ORM\Column(name="hash_name", length=50)
* @ORM\Column(name="hash_name")
*/
protected $hashName;

Expand Down
2 changes: 1 addition & 1 deletion Entity/Scorm2004Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Scorm2004Resource extends AbstractResource
{
/**
* @ORM\Column(name="hash_name", length=50)
* @ORM\Column(name="hash_name")
*/
protected $hashName;

Expand Down
13 changes: 6 additions & 7 deletions Listener/Scorm12Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Claroline\ScormBundle\Entity\Scorm12Resource;
use Claroline\ScormBundle\Entity\Scorm12Sco;
use Claroline\ScormBundle\Form\ScormType;
use Claroline\ScormBundle\Manager\ScormManager;
use Claroline\ScormBundle\Listener\Exception\InvalidScormArchiveException;
use JMS\DiExtraBundle\Annotation as DI;
use Symfony\Bundle\TwigBundle\TwigEngine;
Expand Down Expand Up @@ -54,7 +53,6 @@ class Scorm12Listener
private $scorm12ScoTrackingRepo;
private $templating;
private $translator;
private $scormManager;

/**
* @DI\InjectParams({
Expand All @@ -65,8 +63,7 @@ class Scorm12Listener
* "requestStack" = @DI\Inject("request_stack"),
* "router" = @DI\Inject("router"),
* "templating" = @DI\Inject("templating"),
* "translator" = @DI\Inject("translator"),
* "scormManager" = @DI\Inject("claroline.manager.scorm_manager")
* "translator" = @DI\Inject("translator")
* })
*/
public function __construct(
Expand All @@ -90,7 +87,7 @@ public function __construct(
$this->router = $router;
$this->scormResourceRepo = $om->getRepository('ClarolineScormBundle:Scorm12Resource');
$this->scormResourcesPath = $this->container
->getParameter('kernel.root_dir') . '/../web/uploads/scormresources/';
->getParameter('claroline.param.uploads_directory') . '/scormresources/';
$this->scorm12ScoTrackingRepo = $om->getRepository('ClarolineScormBundle:Scorm12ScoTracking');
$this->templating = $templating;
$this->translator = $translator;
Expand Down Expand Up @@ -134,11 +131,12 @@ public function onCreate(CreateResourceEvent $event)
try {
if ($form->isValid()) {
$tmpFile = $form->get('file')->getData();
$workspace = $event->getParent()->getWorkspace();

if ($this->isScormArchive($tmpFile)) {
$scormResource = $this->container
->get('claroline.manager.scorm_manager')
->createScorm12($tmpFile, $form->get('name')->getData());
->createScorm12($tmpFile, $form->get('name')->getData(), $workspace);
$event->setResources(array($scormResource));
$event->stopPropagation();

Expand Down Expand Up @@ -250,6 +248,7 @@ public function onCopy(CopyResourceEvent $event)
public function onDownload(DownloadResourceEvent $event)
{
$event->setItem($this->filePath . $event->getResource()->getHashName());
$event->setExtension('zip');
$event->stopPropagation();
}

Expand Down Expand Up @@ -279,7 +278,7 @@ private function isScormArchive(UploadedFile $file)
/**
* Deletes recursively a directory and its content.
*
* @param $dir The path to the directory to delete.
* @param $dirPath The path to the directory to delete.
*/
private function deleteFiles($dirPath)
{
Expand Down
15 changes: 9 additions & 6 deletions Listener/Scorm2004Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __construct(
$this->router = $router;
$this->scormResourceRepo = $om->getRepository('ClarolineScormBundle:Scorm2004Resource');
$this->scormResourcesPath = $this->container
->getParameter('kernel.root_dir') . '/../web/uploads/scormresources/';
->getParameter('claroline.param.uploads_directory') . '/scormresources/';
$this->scorm2004ScoTrackingRepo = $om->getRepository('ClarolineScormBundle:Scorm2004ScoTracking');
$this->templating = $templating;
$this->translator = $translator;
Expand Down Expand Up @@ -131,6 +131,8 @@ public function onCreate(CreateResourceEvent $event)
try {
if ($form->isValid()) {
$tmpFile = $form->get('file')->getData();
$workspace = $event->getParent()->getWorkspace();
$prefix = 'WORKSPACE_' . $workspace->getId();

if ($this->isScormArchive($tmpFile)) {
$scormResource = new Scorm2004Resource();
Expand All @@ -139,7 +141,7 @@ public function onCreate(CreateResourceEvent $event)
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$hashName = $this->container->get('claroline.utilities.misc')
->generateGuid() . "." . $extension;
$scormResource->setHashName($hashName);
$scormResource->setHashName($prefix . DIRECTORY_SEPARATOR . $hashName);
$scos = $this->generateScosFromScormArchive($tmpFile);

if (count($scos) > 0) {
Expand All @@ -148,9 +150,9 @@ public function onCreate(CreateResourceEvent $event)
} else {
throw new InvalidScormArchiveException('no_sco_in_scorm_archive_message');
}
$this->unzipScormArchive($tmpFile, $hashName);
$this->unzipScormArchive($tmpFile, $hashName, $prefix);
// Move Scorm archive in the files directory
$tmpFile->move($this->filePath, $hashName);
$tmpFile->move($this->filePath . DIRECTORY_SEPARATOR . $prefix, $hashName);

$event->setResources(array($scormResource));
$event->stopPropagation();
Expand Down Expand Up @@ -263,6 +265,7 @@ public function onCopy(CopyResourceEvent $event)
public function onDownload(DownloadResourceEvent $event)
{
$event->setItem($this->filePath . $event->getResource()->getHashName());
$event->setExtension('zip');
$event->stopPropagation();
}

Expand Down Expand Up @@ -295,11 +298,11 @@ private function isScormArchive(UploadedFile $file)
* @param UploadedFile $file
* @param $hashName name of the destination directory
*/
private function unzipScormArchive(UploadedFile $file, $hashName)
private function unzipScormArchive(UploadedFile $file, $hashName, $prefix)
{
$zip = new \ZipArchive();
$zip->open($file);
$destinationDir = $this->scormResourcesPath . $hashName;
$destinationDir = $this->scormResourcesPath . $prefix . DIRECTORY_SEPARATOR . $hashName;

if (!file_exists($destinationDir)) {
mkdir($destinationDir, 0777, true);
Expand Down
16 changes: 9 additions & 7 deletions Manager/ScormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Claroline\ScormBundle\Manager;

use Claroline\CoreBundle\Entity\User;
use Claroline\CoreBundle\Entity\Workspace\Workspace;
use Claroline\CoreBundle\Persistence\ObjectManager;
use Claroline\ScormBundle\Entity\Scorm12Resource;
use Claroline\ScormBundle\Entity\Scorm12Sco;
Expand Down Expand Up @@ -58,18 +59,19 @@ public function __construct(ObjectManager $om, ContainerInterface $container)
$this->scorm2004ScoTrackingRepo =
$om->getRepository('ClarolineScormBundle:Scorm2004ScoTracking');
$this->scormResourcesPath = $this->container
->getParameter('kernel.root_dir') . '/../web/uploads/scormresources/';
->getParameter('claroline.param.uploads_directory') . '/scormresources/';
$this->filePath = $this->container
->getParameter('claroline.param.files_directory') . DIRECTORY_SEPARATOR;
}

public function createScorm12($tmpFile, $name)
public function createScorm12($tmpFile, $name, Workspace $workspace)
{
$prefix = 'WORKSPACE_' . $workspace->getId();
$scormResource = new Scorm12Resource();
$scormResource->setName($name);
$hashName = $this->container->get('claroline.utilities.misc')
->generateGuid() . '.zip';
$scormResource->setHashName($hashName);
$scormResource->setHashName($prefix . DIRECTORY_SEPARATOR . $hashName);
$scos = $this->generateScosFromScormArchive($tmpFile);

if (count($scos) > 0) {
Expand All @@ -78,9 +80,9 @@ public function createScorm12($tmpFile, $name)
} else {
throw new InvalidScormArchiveException('no_sco_in_scorm_archive_message');
}
$this->unzipScormArchive($tmpFile, $hashName);
$this->unzipScormArchive($tmpFile, $hashName, $prefix);
// Move Scorm archive in the files directory
$tmpFile->move($this->filePath, $hashName);
$tmpFile->move($this->filePath . DIRECTORY_SEPARATOR . $prefix, $hashName);

return $scormResource;
}
Expand Down Expand Up @@ -496,11 +498,11 @@ private function persistScos(
* @param UploadedFile $file
* @param $hashName name of the destination directory
*/
private function unzipScormArchive(\SplFileInfo $file, $hashName)
private function unzipScormArchive(\SplFileInfo $file, $hashName, $prefix)
{
$zip = new \ZipArchive();
$zip->open($file);
$destinationDir = $this->scormResourcesPath . $hashName;
$destinationDir = $this->scormResourcesPath . $prefix . DIRECTORY_SEPARATOR . $hashName;

if (!file_exists($destinationDir)) {
mkdir($destinationDir, 0777, true);
Expand Down
Loading

0 comments on commit d063274

Please sign in to comment.