Skip to content

Commit

Permalink
Adds SCORM 2004 resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Anh Thao PHAM committed Jul 14, 2014
1 parent e5ce122 commit 44b9a5b
Show file tree
Hide file tree
Showing 28 changed files with 4,615 additions and 238 deletions.
156 changes: 156 additions & 0 deletions Controller/ScormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use Claroline\ScormBundle\Entity\Scorm12Resource;
use Claroline\ScormBundle\Entity\Scorm12Sco;
use Claroline\ScormBundle\Entity\Scorm12ScoTracking;
use Claroline\ScormBundle\Entity\Scorm2004Resource;
use Claroline\ScormBundle\Entity\Scorm2004Sco;
use Claroline\ScormBundle\Entity\Scorm2004ScoTracking;
use Claroline\ScormBundle\Event\Log\LogScorm12ResultEvent;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -51,6 +54,7 @@ public function __construct(
$this->om = $om;
$this->securityContext = $securityContext;
$this->scorm12ScoTrackingRepo = $om->getRepository('ClarolineScormBundle:Scorm12ScoTracking');
$this->scorm2004ScoTrackingRepo = $om->getRepository('ClarolineScormBundle:Scorm2004ScoTracking');
$this->userRepo = $om->getRepository('ClarolineCoreBundle:User');
}

Expand Down Expand Up @@ -361,6 +365,141 @@ private function logScorm12ScoResult(
$this->eventDispatcher->dispatch('log', $event);
}

/**
* @EXT\Route(
* "/render/scorm/2004/{scormId}",
* name = "claro_render_scorm_2004_resource"
* )
* @EXT\Method("GET")
* @EXT\ParamConverter(
* "scorm",
* class="ClarolineScormBundle:Scorm2004Resource",
* options={"id" = "scormId", "strictId" = true}
* )
* @EXT\Template("ClarolineScormBundle::scorm2004.html.twig")
*
* @param Scorm2004Resource $scorm
*
* @return Response
*/
public function renderScorm2004ResourceAction(Scorm2004Resource $scorm)
{
$this->checkScorm2004ResourceAccess('OPEN', $scorm);
$user = $this->securityContext->getToken()->getUser();

$scos = $scorm->getScos();
$rootScos = array();

$checkTracking = true;
$createTracking = false;

foreach ($scos as $sco) {
if (is_null($sco->getScoParent())) {
$rootScos[] = $sco;
}

if ($checkTracking) {
$scoTracking = $this->scorm2004ScoTrackingRepo->findOneBy(
array('user' => $user->getId(), 'sco' => $sco->getId())
);
$checkTracking = false;

if (is_null($scoTracking)) {
$createTracking = true;
}
}

if ($createTracking) {
$scoTracking = new Scorm2004ScoTracking();
$scoTracking->setUser($user);
$scoTracking->setSco($sco);
$scoTracking->setScoreRaw(-1);
$scoTracking->setScoreMax(-1);
$scoTracking->setScoreMin(-1);
$scoTracking->setLessonStatus('not attempted');
$scoTracking->setSuspendData('');
$scoTracking->setEntry('ab-initio');
$scoTracking->setLessonLocation('');
$scoTracking->setCredit('no-credit');
$scoTracking->setTotalTime(0);
$scoTracking->setSessionTime(0);
$scoTracking->setLessonMode('normal');
$scoTracking->setExitMode('');
$scoTracking->setBestLessonStatus('not attempted');

$this->om->persist($scoTracking);
}
}

if ($createTracking) {
$this->om->flush();
}

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

/**
* @EXT\Route(
* "/scorm/2004/render/sco/{scoId}",
* name = "claro_render_scorm_2004_sco"
* )
* @EXT\Method("GET")
* @EXT\ParamConverter(
* "scorm2004Sco",
* class="ClarolineScormBundle:Scorm2004Sco",
* options={"id" = "scoId", "strictId" = true}
* )
* @EXT\Template("ClarolineScormBundle::scorm2004MenuSco.html.twig")
*
* @param Scorm2004Sco $scorm2004Sco
*
* @return Response
*/
public function renderScorm2004ScoAction(Scorm2004Sco $scorm2004Sco)
{
$user = $this->securityContext->getToken()->getUser();
$scorm = $scorm2004Sco->getScormResource();
$this->checkScorm2004ResourceAccess('OPEN', $scorm);

$scos = $scorm->getScos();
$entryUrl = $scorm2004Sco->getEntryUrl();

if (is_string($entryUrl) && preg_match('/^http/', $entryUrl)) {
$scormPath = $entryUrl . $scorm2004Sco->getParameters();
} else {
$scormPath = 'uploads/scormresources/'
. $scorm->getHashName()
. DIRECTORY_SEPARATOR
. $scorm2004Sco->getEntryUrl()
. $scorm2004Sco->getParameters();
}
$rootScos = array();

foreach ($scos as $sco) {
if (is_null($sco->getScoParent())) {
$rootScos[] = $sco;
}
}
$scoTracking = $this->scorm2004ScoTrackingRepo->findOneBy(
array('user' => $user->getId(), 'sco' => $scorm2004Sco->getId())
);

return array(
'resource' => $scorm,
'_resource' => $scorm,
'currentSco' => $scorm2004Sco,
'scos' => $rootScos,
'scoTracking' => $scoTracking,
'scormUrl' => $scormPath,
'workspace' => $scorm->getResourceNode()->getWorkspace()
);
}

/**
* Convert time (HHHH:MM:SS.hh) to integer (hundredth of second)
*
Expand Down Expand Up @@ -401,4 +540,21 @@ private function checkAccess($permission, Scorm12Resource $resource)
throw new AccessDeniedException($collection->getErrorsForDisplay());
}
}

/**
* Checks if the current user has the right to perform an action on a Scorm2004Resource.
*
* @param string $permission
* @param Scorm2004Resource $resource
*
* @throws AccessDeniedException
*/
private function checkScorm2004ResourceAccess($permission, Scorm2004Resource $resource)
{
$collection = new ResourceCollection(array($resource->getResourceNode()));

if (!$this->securityContext->isGranted($permission, $collection)) {
throw new AccessDeniedException($collection->getErrorsForDisplay());
}
}
}
50 changes: 50 additions & 0 deletions Entity/Scorm2004Resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <consortium@claroline.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Claroline\ScormBundle\Entity;

use Claroline\CoreBundle\Entity\Resource\AbstractResource;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="Claroline\ScormBundle\Repository\Scorm2004ResourceRepository")
* @ORM\Table(name="claro_scorm_2004_resource")
*/
class Scorm2004Resource extends AbstractResource
{
/**
* @ORM\Column(name="hash_name", length=50)
*/
protected $hashName;

/**
* @ORM\OneToMany(
* targetEntity="Claroline\ScormBundle\Entity\Scorm2004Sco",
* mappedBy="scormResource"
* )
*/
protected $scos;

public function getHashName()
{
return $this->hashName;
}

public function setHashName($hashName)
{
$this->hashName = $hashName;
}

public function getScos()
{
return $this->scos;
}
}
Loading

0 comments on commit 44b9a5b

Please sign in to comment.