Refactor SettingsManager.php to be used in a course

Renamed to CourseSettingsSchema.php
pull/3844/head
Julio Montoya 4 years ago
parent 6dfae7fe38
commit b32b16f8ac
  1. 12
      public/main/inc/lib/course.lib.php
  2. 4
      src/CoreBundle/Controller/CourseHomeController.php
  3. 7
      src/CoreBundle/Framework/Container.php
  4. 18
      src/CoreBundle/Repository/ResourceNodeRepository.php
  5. 32
      src/CoreBundle/Repository/ResourceRepository.php
  6. 2
      src/CoreBundle/Resources/views/Course/settings.html.twig
  7. 1
      src/CoreBundle/Settings/CourseSettingsSchema.php
  8. 26
      src/CoreBundle/Settings/SettingsManager.php
  9. 9
      src/CourseBundle/Entity/CCourseSetting.php
  10. 2
      src/CourseBundle/Resources/config/services.yml
  11. 99
      src/CourseBundle/Settings/SettingsCourseManager.php

@ -14,7 +14,7 @@ use Chamilo\CoreBundle\ToolChain;
use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
use Chamilo\CourseBundle\Entity\CGroup;
use Chamilo\CourseBundle\Manager\SettingsManager;
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
use ChamiloSession as Session;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManager;
@ -56,18 +56,12 @@ class CourseManager
return self::$em;
}
/**
* @return SettingsManager
*/
public static function getCourseSettingsManager()
public static function getCourseSettingsManager(): SettingsCourseManager
{
return self::$courseSettingsManager;
}
/**
* @param SettingsManager $courseSettingsManager
*/
public static function setCourseSettingsManager($courseSettingsManager)
public static function setCourseSettingsManager(SettingsCourseManager $courseSettingsManager): void
{
self::$courseSettingsManager = $courseSettingsManager;
}

@ -12,9 +12,9 @@ use Chamilo\CoreBundle\ToolChain;
use Chamilo\CourseBundle\Controller\ToolBaseController;
use Chamilo\CourseBundle\Entity\CTool;
use Chamilo\CourseBundle\Manager\SettingsFormFactory;
use Chamilo\CourseBundle\Manager\SettingsManager;
use Chamilo\CourseBundle\Repository\CShortcutRepository;
use Chamilo\CourseBundle\Repository\CToolRepository;
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
use CourseManager;
use Database;
use Display;
@ -242,7 +242,7 @@ class CourseHomeController extends ToolBaseController
*
* @return Response
*/
public function updateAction(Request $request, Course $course, $namespace, SettingsManager $manager, SettingsFormFactory $formFactory)
public function updateSettingsAction(Request $request, Course $course, $namespace, SettingsCourseManager $manager, SettingsFormFactory $formFactory)
{
$schemaAlias = $manager->convertNameSpaceToService($namespace);
$settings = $manager->load($namespace);

@ -56,6 +56,7 @@ use Chamilo\CourseBundle\Repository\CThematicAdvanceRepository;
use Chamilo\CourseBundle\Repository\CThematicPlanRepository;
use Chamilo\CourseBundle\Repository\CThematicRepository;
use Chamilo\CourseBundle\Repository\CWikiRepository;
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
use CourseManager;
use Database;
use Doctrine\ORM\EntityManager;
@ -235,9 +236,9 @@ class Container
return self::$container->get('chamilo.settings.manager');
}
public static function getCourseSettingsManager(): \Chamilo\CourseBundle\Manager\SettingsManager
public static function getCourseSettingsManager(): SettingsCourseManager
{
return self::$container->get(\Chamilo\CourseBundle\Manager\SettingsManager::class);
return self::$container->get(SettingsCourseManager::class);
}
/**
@ -519,7 +520,7 @@ class Container
$em = $container->get('doctrine.orm.entity_manager');
Database::setManager($em);
CourseManager::setEntityManager($em);
CourseManager::setCourseSettingsManager($container->get('Chamilo\CourseBundle\Manager\SettingsManager'));
CourseManager::setCourseSettingsManager($container->get(SettingsCourseManager::class));
// Setting course tool chain (in order to create tools to a course)
CourseManager::setToolList($container->get(ToolChain::class));
/*if ($setSession) {

@ -12,7 +12,6 @@ use Chamilo\CoreBundle\Entity\ResourceLink;
use Chamilo\CoreBundle\Entity\ResourceNode;
use Chamilo\CoreBundle\Entity\ResourceType;
use Chamilo\CoreBundle\Entity\Session;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Gedmo\Tree\Entity\Repository\MaterializedPathRepository;
use League\Flysystem\FilesystemInterface;
@ -107,20 +106,21 @@ class ResourceNodeRepository extends MaterializedPathRepository
->innerJoin('node.resourceFile', 'file')
->innerJoin('node.resourceLinks', 'l')
->where('node.resourceType = :type')
->setParameter('type', $type, Types::STRING)
->andWhere('node.parent = :parentNode')
->setParameter('parentNode', $resourceNode)
->andWhere('file IS NOT NULL')
->andWhere('l.visibility <> :visibility')
->setParameter('visibility', ResourceLink::VISIBILITY_DELETED)
->andWhere('file IS NOT NULL')
;
$params = [];
if (null !== $course) {
$qb
->andWhere('l.course = :course')
->setParameter('course', $course)
;
$qb->andWhere('l.course = :course');
$params['course'] = $course;
}
$params['visibility'] = ResourceLink::VISIBILITY_DELETED;
$params['parentNode'] = $resourceNode;
$params['type'] = $type;
$qb->setParameters($params);
return (int) $qb->getQuery()->getSingleScalarResult();
}

@ -140,6 +140,16 @@ abstract class ResourceRepository extends ServiceEntityRepository
return $this;
}
public function getResourceSettings(): Settings
{
return $this->settings;
}
public function getTemplates(): Template
{
return $this->templates;
}
public function findResourceByTitle(
string $title,
ResourceNode $parentNode,
@ -453,11 +463,7 @@ abstract class ResourceRepository extends ServiceEntityRepository
$qb = $this->createQueryBuilder('resource')
->select('resource')
//->from($className, 'resource')
->innerJoin(
'resource.resourceNode',
'node'
)
->innerJoin('resource.resourceNode', 'node')
->innerJoin('node.resourceLinks', 'links')
->innerJoin('node.resourceType', 'type')
->where('type.name = :type')
@ -494,7 +500,6 @@ abstract class ResourceRepository extends ServiceEntityRepository
{
$qb = $this->createQueryBuilder('resource')
->select('resource')
//->from($className, 'resource')
->innerJoin('resource.resourceNode', 'node')
//->innerJoin('node.resourceLinks', 'links')
//->where('node.resourceType = :type')
@ -537,10 +542,7 @@ abstract class ResourceRepository extends ServiceEntityRepository
$qb = $this->createQueryBuilder('resource')
->select('resource')
->innerJoin(
'resource.resourceNode',
'node'
)
->innerJoin('resource.resourceNode', 'node')
->innerJoin('node.resourceLinks', 'links')
->innerJoin('node.resourceType', 'type')
->where('type.name = :type')
@ -685,16 +687,6 @@ abstract class ResourceRepository extends ServiceEntityRepository
}
}
public function getResourceSettings(): Settings
{
return $this->settings;
}
public function getTemplates(): Template
{
return $this->templates;
}
/**
* @param string $content
*

@ -14,6 +14,7 @@
<hr />
<div class="row">
<div class="col-md-12">
{% if form %}
<div class="box box-primary">
<div class="box-body">
{{ form_errors(form) }}
@ -28,6 +29,7 @@
</form>
</div>
</div>
{% endif %}
</div>
</div>
</div>

@ -12,7 +12,6 @@ use Chamilo\CoreBundle\Form\Type\YesNoType;
use Chamilo\CoreBundle\Tool\AbstractTool;
use Chamilo\CoreBundle\ToolChain;
use Chamilo\CoreBundle\Transformer\ArrayToIdentifierTransformer;
//use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer;
use Sylius\Bundle\SettingsBundle\Schema\AbstractSettingsBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

@ -47,7 +47,7 @@ class SettingsManager implements SettingsManagerInterface
protected array $resolvedSettings = [];
//protected $settings;
/**
* @var null|array<string, \Sylius\Bundle\SettingsBundle\Model\Settings>|mixed[]
* @var null|array<string, Settings>|mixed[]
*/
protected ?array $schemaList;
protected RequestStack $request;
@ -108,22 +108,12 @@ class SettingsManager implements SettingsManagerInterface
return $this->schemaRegistry->all();
}
/**
* @param string $category
*
* @return string
*/
public function convertNameSpaceToService($category)
public function convertNameSpaceToService(string $category): string
{
return 'chamilo_core.settings.'.$category;
}
/**
* @param string $category
*
* @return string
*/
public function convertServiceToNameSpace($category)
public function convertServiceToNameSpace(string $category): string
{
return str_replace('chamilo_core.settings.', '', $category);
}
@ -268,10 +258,6 @@ class SettingsManager implements SettingsManagerInterface
return $settings;
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function update(SettingsInterface $settings): void
{
$namespace = $settings->getSchemaAlias();
@ -574,10 +560,8 @@ class SettingsManager implements SettingsManagerInterface
/**
* Get variables and categories as in 1.11.x.
*
* @return array
*/
private function getVariablesAndCategories()
private function getVariablesAndCategories(): array
{
return [
'Institution' => 'Platform',
@ -1056,6 +1040,6 @@ class SettingsManager implements SettingsManagerInterface
'active_tools_on_create' => 'course',
];
return isset($settings[$variable]) ? $settings[$variable] : $defaultCategory;
return $settings[$variable] ?? $defaultCategory;
}
}

@ -165,14 +165,7 @@ class CCourseSetting
return $this->category;
}
/**
* Set value.
*
* @param string $value
*
* @return CCourseSetting
*/
public function setValue($value)
public function setValue($value): self
{
$this->value = $value;

@ -9,7 +9,7 @@ services:
- Sylius\Bundle\SettingsBundle\Schema\SchemaInterface
- 'CourseSettings'
Chamilo\CourseBundle\Manager\SettingsManager:
Chamilo\CourseBundle\Settings\SettingsCourseManager:
arguments:
- '@chamilo_course.registry.settings_schema'
- '@doctrine.orm.entity_manager'

@ -4,11 +4,11 @@ declare(strict_types=1);
/* For licensing terms, see /license.txt */
namespace Chamilo\CourseBundle\Manager;
namespace Chamilo\CourseBundle\Settings;
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\SettingsCurrent;
use Chamilo\CoreBundle\Settings\SettingsManager as ChamiloSettingsManager;
use Chamilo\CoreBundle\Settings\SettingsManager;
use Chamilo\CourseBundle\Entity\CCourseSetting;
use Sylius\Bundle\SettingsBundle\Model\Settings;
use Sylius\Bundle\SettingsBundle\Model\SettingsInterface;
@ -17,11 +17,7 @@ use Sylius\Bundle\SettingsBundle\Schema\SettingsBuilder;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Exception\ValidatorException;
/**
* Class SettingsManager
* Course settings manager.
*/
class SettingsManager extends ChamiloSettingsManager
class SettingsCourseManager extends SettingsManager
{
protected Course $course;
@ -43,13 +39,12 @@ class SettingsManager extends ChamiloSettingsManager
$settings = new Settings();
$schemaAliasNoPrefix = $schemaAlias;
$schemaAlias = 'chamilo_course.settings.'.$schemaAlias;
if ($this->schemaRegistry->has($schemaAlias)) {
/** @var SchemaInterface $schema */
$schema = $this->schemaRegistry->get($schemaAlias);
} else {
if (!$this->schemaRegistry->has($schemaAlias)) {
return $settings;
}
/** @var SchemaInterface $schema */
$schema = $this->schemaRegistry->get($schemaAlias);
$settings->setSchemaAlias($schemaAlias);
// We need to get a plain parameters array since we use the options resolver on it
@ -83,7 +78,7 @@ class SettingsManager extends ChamiloSettingsManager
$namespace = $settings->getSchemaAlias();
/** @var SchemaInterface $schema */
$schema = $this->schemaRegistry->get($settings->getSchemaAlias());
$schema = $this->schemaRegistry->get($namespace);
$settingsBuilder = new SettingsBuilder();
$schema->buildSettings($settingsBuilder);
@ -103,7 +98,7 @@ class SettingsManager extends ChamiloSettingsManager
$repo = $this->manager->getRepository(SettingsCurrent::class);
/** @var CCourseSetting[] $persistedParameters */
$persistedParameters = $repo->findBy([
'category' => $settings->getSchemaAlias(),
'category' => $namespace,
]);
$persistedParametersMap = [];
@ -129,89 +124,25 @@ class SettingsManager extends ChamiloSettingsManager
}
$this->manager->flush();
/*$schema = $this->schemaRegistry->getSchema($namespace);
$settingsBuilder = new SettingsBuilder();
$schema->buildSettings($settingsBuilder);
$parameters = $settingsBuilder->resolve($settings->getParameters());
foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) {
if (array_key_exists($parameter, $parameters)) {
$parameters[$parameter] = $transformer->transform($parameters[$parameter]);
}
}
if (isset($this->resolvedSettings[$namespace])) {
$this->resolvedSettings[$namespace]->setParameters($parameters);
}
$persistedParameters = $this->parameterRepository->findBy(
['category' => $namespace, 'cId' => $this->getCourse()->getId()]
);
$persistedParametersMap = [];
foreach ($persistedParameters as $parameter) {
$persistedParametersMap[$parameter->getName()] = $parameter;
}
foreach ($parameters as $name => $value) {
if (isset($persistedParametersMap[$name])) {
$persistedParametersMap[$name]->setValue($value);
} else {
// @var CCourseSetting $parameter
//$parameter = $this->parameterFactory->createNew();
$parameter = new CCourseSetting();
$parameter
->setNamespace($namespace)
->setName($name)
->setValue($value)
->setCId($this->getCourse()->getId())
;
/// @var ConstraintViolationListInterface $errors
$errors = $this->validator->validate($parameter);
if (0 < $errors->count()) {
throw new ValidatorException($errors->get(0)->getMessage());
}
$this->parameterManager->persist($parameter);
}
}
$this->parameterManager->flush();
$this->cache->save($namespace, $parameters);*/
}
/**
* @param string $category
*
* @return string
*/
public function convertNameSpaceToService($category)
public function convertNameSpaceToService(string $category): string
{
return 'chamilo_course.settings.'.$category;
}
/**
* Load parameter from database.
*
* @param string $namespace
*
* @return array
*/
private function getParameters($namespace)
private function getParameters(string $namespace): array
{
$repo = $this->manager->getRepository(CCourseSetting::class);
$parameters = [];
foreach ($repo->findBy([
'category' => $namespace,
]) as $parameter) {
$parameters[$parameter->getTitle()] = $parameter->getValue();
$list = [];
$parameters = $repo->findBy(['category' => $namespace]);
foreach ($parameters as $parameter) {
$list[$parameter->getTitle()] = $parameter->getValue();
}
return $parameters;
return $list;
}
}
Loading…
Cancel
Save