LP: Fix lp creation + add tests

pull/3984/head
Julio 4 years ago
parent 41b262d1b6
commit 258428e1d0
  1. 38
      public/main/lp/learnpath.class.php
  2. 3
      src/CoreBundle/Migrations/Schema/V200/Version20201216122012.php
  3. 6
      src/CourseBundle/Repository/CLpItemRepository.php
  4. 18
      src/CourseBundle/Repository/CLpRepository.php
  5. 57
      tests/CourseBundle/Repository/CLpItemRepositoryTest.php
  6. 12
      tests/CourseBundle/Repository/CLpRepositoryTest.php

@ -449,8 +449,7 @@ class learnpath
$title = $exercise->get_formated_title(); $title = $exercise->get_formated_title();
} }
$lpItem = new CLpItem(); $lpItem = (new CLpItem())
$lpItem
->setTitle($title) ->setTitle($title)
->setDescription($description) ->setDescription($description)
->setPath($id) ->setPath($id)
@ -630,6 +629,8 @@ class learnpath
$category = Container::getLpCategoryRepository()->find($categoryId); $category = Container::getLpCategoryRepository()->find($categoryId);
} }
$lpRepo = Container::getLpRepository();
$lp = (new CLp()) $lp = (new CLp())
->setLpType($type) ->setLpType($type)
->setName($name) ->setName($name)
@ -641,24 +642,7 @@ class learnpath
->setParent($courseEntity) ->setParent($courseEntity)
->addCourseLink($courseEntity, $sessionEntity) ->addCourseLink($courseEntity, $sessionEntity)
; ;
$lpRepo->createLp($lp);
$em = Database::getManager();
$em->persist($lp);
$em->flush();
$lpId = $lp->getIid();
if ($lpId) {
// Creates first root item, so all items will be parent of this item.
$lpItem = (new CLpItem())
->setTitle('root')
->setPath('root')
->setLp($lp)
->setItemType('root')
;
$em = Database::getManager();
$em->persist($lpItem);
$em->flush();
}
break; break;
} }
@ -5780,9 +5764,10 @@ class learnpath
$action = 'add_item'; $action = 'add_item';
$item_type = 'dir'; $item_type = 'dir';
$lpItem = new CLpItem(); $lpItem = (new CLpItem())
$lpItem->setTitle(''); ->setTitle('')
$lpItem->setItemType('dir'); ->setItemType('dir')
;
$url = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id; $url = api_get_self().'?'.api_get_cidreq().'&action='.$action.'&type='.$item_type.'&lp_id='.$this->lp_id;
@ -6577,9 +6562,10 @@ class learnpath
$url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath='; $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?'.api_get_cidreq().'&a=upload_file&curdirpath=';
$form->addMultipleUpload($url); $form->addMultipleUpload($url);
$lpItem = new CLpItem(); $lpItem = (new CLpItem())
$lpItem->setTitle(''); ->setTitle('')
$lpItem->setItemType(TOOL_DOCUMENT); ->setItemType(TOOL_DOCUMENT)
;
$new = $this->displayDocumentForm('add', $lpItem); $new = $this->displayDocumentForm('add', $lpItem);
/*$lpItem = new CLpItem(); /*$lpItem = new CLpItem();

@ -120,8 +120,7 @@ final class Version20201216122012 extends AbstractMigrationChamilo
continue; continue;
} }
$rootItem = new CLpItem(); $rootItem = (new CLpItem())
$rootItem
->setTitle('root') ->setTitle('root')
->setPath('root') ->setPath('root')
->setLp($resource) ->setLp($resource)

@ -24,6 +24,12 @@ final class CLpItemRepository extends ServiceEntityRepository
$this->initializeTreeRepository($this->getEntityManager(), $this->getClassMetadata()); $this->initializeTreeRepository($this->getEntityManager(), $this->getClassMetadata());
} }
public function create(CLpItem $item): void
{
$this->getEntityManager()->persist($item);
$this->getEntityManager()->flush();
}
public function getRootItem(int $lpId): ?CLpItem public function getRootItem(int $lpId): ?CLpItem
{ {
return $this->findOneBy([ return $this->findOneBy([

@ -12,8 +12,10 @@ use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Repository\ResourceRepository; use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface; use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface;
use Chamilo\CourseBundle\Entity\CLp; use Chamilo\CourseBundle\Entity\CLp;
use Chamilo\CourseBundle\Entity\CLpItem;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
use Exception;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
final class CLpRepository extends ResourceRepository implements ResourceWithLinkInterface final class CLpRepository extends ResourceRepository implements ResourceWithLinkInterface
@ -23,6 +25,22 @@ final class CLpRepository extends ResourceRepository implements ResourceWithLink
parent::__construct($registry, CLp::class); parent::__construct($registry, CLp::class);
} }
public function createLp(CLp $lp): void
{
if (null !== $lp->getResourceNode()) {
throw new Exception('Lp should not have a resource node during creation');
}
$lpItem = (new CLpItem())
->setTitle('root')
->setPath('root')
->setLp($lp)
->setItemType('root')
;
$lp->getItems()->add($lpItem);
$this->create($lp);
}
public function findAllByCourse( public function findAllByCourse(
Course $course, Course $course,
Session $session = null, Session $session = null,

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\Tests\CourseBundle\Repository;
use Chamilo\CourseBundle\Entity\CLp;
use Chamilo\CourseBundle\Entity\CLpItem;
use Chamilo\CourseBundle\Repository\CLpItemRepository;
use Chamilo\CourseBundle\Repository\CLpRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
class CLpItemRepositoryTest extends AbstractApiTest
{
use ChamiloTestTrait;
public function testCreate(): void
{
self::bootKernel();
$lpRepo = self::getContainer()->get(CLpRepository::class);
$lpItemRepo = self::getContainer()->get(CLpItemRepository::class);
$course = $this->createCourse('new');
$teacher = $this->createUser('teacher');
$lp = (new CLp())
->setName('lp')
->setParent($course)
->setCreator($teacher)
->setLpType(CLp::LP_TYPE)
;
$lpRepo->createLp($lp);
$rootItem = $lpItemRepo->getRootItem($lp->getIid());
$this->assertNotNull($rootItem);
$this->assertSame('root', $rootItem->getPath());
$lpItem = (new CLpItem())
->setDescription('lp')
->setTitle('lp item')
->setLp($lp)
->setItemType('document')
;
$this->assertHasNoEntityViolations($lpItem);
$lpItemRepo->create($lpItem);
$this->assertSame(1, $lp->getItems()->count());
$this->assertSame('lp', (string) $lp);
$this->assertSame(1, $lpRepo->count([]));
$this->assertSame(2, $lpItemRepo->count([]));
}
}

@ -19,23 +19,23 @@ class CLpRepositoryTest extends AbstractApiTest
{ {
self::bootKernel(); self::bootKernel();
$em = $this->getEntityManager();
$repo = self::getContainer()->get(CLpRepository::class); $repo = self::getContainer()->get(CLpRepository::class);
$course = $this->createCourse('new'); $course = $this->createCourse('new');
$teacher = $this->createUser('teacher'); $teacher = $this->createUser('teacher');
$item = (new CLp()) $lp = (new CLp())
->setName('lp') ->setName('lp')
->setParent($course) ->setParent($course)
->setCreator($teacher) ->setCreator($teacher)
->setLpType(CLp::LP_TYPE) ->setLpType(CLp::LP_TYPE)
; ;
$this->assertHasNoEntityViolations($item); $this->assertHasNoEntityViolations($lp);
$em->persist($item); $repo->createLp($lp);
$em->flush();
$this->assertSame('lp', (string) $item); $this->assertNotNull($lp->getResourceNode());
$this->assertSame(1, $lp->getItems()->count());
$this->assertSame('lp', (string) $lp);
$this->assertSame(1, $repo->count([])); $this->assertSame(1, $repo->count([]));
} }
} }

Loading…
Cancel
Save