Commit f5b7270e authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Importation de compétence avec une interface + selection du référentiel

parent 93c54200
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -4,9 +4,12 @@ namespace Element;

use Element\Controller\CompetenceImporterController;
use Element\Controller\CompetenceImporterControllerFactory;
use UnicaenPrivilege\Guard\PrivilegeController;
use Element\Form\CompetenceImportation\CompetenceImportationForm;
use Element\Form\CompetenceImportation\CompetenceImportationFormFactory;
use Element\Form\CompetenceImportation\CompetenceImportationHydrator;
use Element\Form\CompetenceImportation\CompetenceImportationHydratorFactory;
use Laminas\Router\Http\Literal;
use Laminas\Router\Http\Segment;
use UnicaenPrivilege\Guard\PrivilegeController;

return [
    'bjyauthorize' => [
@@ -52,10 +55,14 @@ return [
        ],
    ],
    'form_elements' => [
        'factories' => [],
        'factories' => [
            CompetenceImportationForm::class => CompetenceImportationFormFactory::class,
        ],
    ],
    'hydrators' => [
        'factories' => [],
        'factories' => [
            CompetenceImportationHydrator::class => CompetenceImportationHydratorFactory::class,
        ],
    ]

];
 No newline at end of file
+83 −53
Original line number Diff line number Diff line
@@ -2,80 +2,110 @@

namespace Element\Controller;

use Element\Entity\Db\Competence;
use Element\Entity\Db\CompetenceTheme;
use Element\Form\CompetenceImportation\CompetenceImportationFormAwareTrait;
use Element\Service\Competence\CompetenceServiceAwareTrait;
use Element\Service\CompetenceReferentiel\CompetenceReferentielServiceAwareTrait;
use Element\Service\CompetenceTheme\CompetenceThemeServiceAwareTrait;
use Element\Service\CompetenceType\CompetenceTypeServiceAwareTrait;
use Laminas\Http\Response;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class CompetenceImporterController extends AbstractActionController {
class CompetenceImporterController extends AbstractActionController
{
    use CompetenceServiceAwareTrait;
    use CompetenceReferentielServiceAwareTrait;
    use CompetenceThemeServiceAwareTrait;
    use CompetenceTypeServiceAwareTrait;

    use CompetenceImportationFormAwareTrait;

    /** IMPORT ET REMPLACEMENT ****************************************************************************************/

    public function importerAction() : Response
    public function importerAction(): ViewModel
    {
        $file_path = "/var/www/html/data/competence_referens3.csv";
        $content = file_get_contents($file_path);

        $types = [
            'Compétences comportementales' => $this->getCompetenceTypeService()->getCompetenceType(1),
            'Compétences opérationnelles'  => $this->getCompetenceTypeService()->getCompetenceType(2),
            'Connaissances'                => $this->getCompetenceTypeService()->getCompetenceType(3),
        ];

        $lines = explode("\n", $content);
        $nbLine = count($lines);

        for($position = 1 ; $position < $nbLine; $position++) {
            $line = $lines[$position];
            if ($line !== '') {
                $depth = 0;
                for ($i = 0; $i < strlen($line) ; $i++) {
                    if ($line[$i] === ';' and $depth === 0) $line[$i] = '|';
                    if ($line[$i] === "\"") {
                        if ($depth === 0) $depth = 1; else $depth = 0;
        $form = $this->getCompetenceImportationForm();
        $form->setAttribute('action', $this->url()->fromRoute('competence-import', ['mode' => 'preview', 'path' => null], [], true));


        $request = $this->getRequest();
        if ($request->isPost()) {
            $error = [];
            $data = $request->getPost();
            $file = $request->getFiles();

            $fichier_path = $file['fichier']['tmp_name'];
            $mode = $data['mode'];
            $referentiel = $this->getCompetenceReferentielService()->getCompetenceReferentiel($data['referentiel']);

            //reading
            $array = [];
            if ($fichier_path === null or $fichier_path === '') {
                $error[] = "Aucun fichier !";
            } else {
                $handle = fopen($fichier_path, "r");

                while ($content = fgetcsv($handle, 0, ";")) {
                    $array[] = $content;
                }
            }
                $elements = explode("|", $line);
                $domaine = $elements[0];
                $registre = $elements[1];
                $libelle = $elements[2];
                $definition = $elements[3];
                $id = ((int)$elements[4]);

                if ($libelle !== null and $libelle !== '') {
                    //Existe-t-elle ?
                    $theme = $this->getCompetenceThemeService()->getCompetenceThemeByLibelle($domaine);
            $warning = [];
            if ($mode === 'import' and empty($error)) {

                $typesLibelle = [];
                $themesLibelle = [];
                foreach (array_slice($array, 1) as $line) {
                    $typesLibelle[$line[1]] = $line[1];
                    $themesLibelle[$line[0]] = $line[0];
                }
                $types = [];
                foreach ($typesLibelle as $typeLibelle) {
                    $type = $this->getCompetenceTypeService()->getCompetenceTypeByLibelle($typeLibelle);
                    if ($type === null) {
                        $type = $this->getCompetenceTypeService()->createWith($typeLibelle);
                    }
                    $types[$typeLibelle] = $type;
                }

                $themes = [];
                foreach ($themesLibelle as $themeLibelle) {
                    $theme = $this->getCompetenceThemeService()->getCompetenceThemeByLibelle($themeLibelle);
                    if ($theme === null) {
                        $theme = new CompetenceTheme();
                        $theme->setLibelle($domaine);
                        $this->getCompetenceThemeService()->create($theme);
                        $theme = $this->getCompetenceThemeService()->createWith($themeLibelle);
                    }
                    $competence = $this->getCompetenceService()->getCompetenceByIdSource("REFERENS3", $id);
                    $new_competence = ($competence === null);
                    if ($new_competence) {
                        $competence = new Competence();
                    $themes[$themeLibelle] = $theme;
                }
                    $competence->setLibelle($libelle);
                    if ($definition !== 'Définition en attente' and $definition !== 'Définition non nécessaire') $competence->setDescription($definition); else $competence->setDescription(null);
                    $competence->setType($types[$registre]);
                    $competence->setTheme($theme);
                    $competence->setSource(Competence::SOURCE_REFERENS3);
                    $competence->setIdSource($id);
                    if ($new_competence) {
                        $this->getCompetenceService()->create($competence);

                foreach (array_slice($array, 1) as $line) {
                    $competence = $this->getCompetenceService()->getCompetenceByRefentiel($referentiel, $line[4]);
                    if ($competence === null) {
                        $this->getCompetenceService()->createWith($line[2], $line[3], $types[$line[1]], $themes[$line[0]], $referentiel, $line[4]);
                    } else {
                        $this->getCompetenceService()->update($competence);
                        $this->getCompetenceService()->updateWith($competence, $line[2], $line[3], $types[$line[1]], $themes[$line[0]]);
                    }
                }
            }

            if ($mode !== 'import') {
                $title = "Importation d'un référentiel de compétences (Prévisualisation)";
            }
            if ($mode === 'import') {
                $title = "Importation d'un référentiel de compétences (Importation)";
            }
            return new ViewModel([
                'title' => $title,
                'fichier_path' => $fichier_path,
                'form' => $form,
                'mode' => $mode,
                'error' => $error,
                'warning' => $warning,
                'array' => array_slice($array, 1),
            ]);
        }

        return $this->redirect()->toRoute('element/competence',[],[], true);
        $vm = new ViewModel([
            'title' => "Importation d'un référentiel de compétences",
            'form' => $form,
        ]);
        return $vm;
    }
}
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
@@ -2,7 +2,9 @@

namespace Element\Controller;

use Element\Form\CompetenceImportation\CompetenceImportationForm;
use Element\Service\Competence\CompetenceService;
use Element\Service\CompetenceReferentiel\CompetenceReferentielService;
use Element\Service\CompetenceTheme\CompetenceThemeService;
use Element\Service\CompetenceType\CompetenceTypeService;
use Interop\Container\ContainerInterface;
@@ -21,17 +23,24 @@ class CompetenceImporterControllerFactory {
    {
        /**
         * @var CompetenceService $competenceService
         * @var CompetenceReferentielService $competenceReferentielService
         * @var CompetenceThemeService $competenceThemeService
         * @var CompetenceTypeService $competenceTypeService
         */
        $competenceService = $container->get(CompetenceService::class);
        $competenceReferentielService = $container->get(CompetenceReferentielService::class);
        $competenceThemeService = $container->get(CompetenceThemeService::class);
        $competenceTypeService = $container->get(CompetenceTypeService::class);

        /** @var CompetenceImportationForm $competenceImportationForm */
        $competenceImportationForm = $container->get('FormElementManager')->get(CompetenceImportationForm::class);

        $controller = new CompetenceImporterController();
        $controller->setCompetenceService($competenceService);
        $controller->setCompetenceReferentielService($competenceReferentielService);
        $controller->setCompetenceThemeService($competenceThemeService);
        $controller->setCompetenceTypeService($competenceTypeService);
        $controller->setCompetenceImportationForm($competenceImportationForm);
        return $controller;
    }
}
 No newline at end of file
+78 −0
Original line number Diff line number Diff line
<?php

namespace Element\Form\CompetenceImportation;

use Element\Service\CompetenceReferentiel\CompetenceReferentielServiceAwareTrait;
use Laminas\Form\Element\Button;
use Laminas\Form\Element\File;
use Laminas\Form\Element\Select;
use Laminas\Form\Form;
use Laminas\InputFilter\Factory;

class CompetenceImportationForm extends Form {
    use CompetenceReferentielServiceAwareTrait;

    public function init(): void
    {
        //file CSV
        $this->add([
            'type' => File::class,
            'name' => 'fichier',
            'options' => [
                'label' => 'Fichier CSV contenant les compétences <span class="icon icon-asterisque" title="Champ obligatoire"></span> :',
                'label_options' => [ 'disable_html_escape' => true, ],
            ],
        ]);
        //referentiel
        $this->add([
            'type' => Select::class,
            'name' => 'referentiel',
            'options' => [
                'label' => "Référentiel de compétence :",
                'empty_option' => "Sélectionner un référentiel pour la compétence ...",
                'value_options' => $this->getCompetenceReferentielService()->getCompetencesReferentielsAsOptions(),
            ],
            'attributes' => [
                'id' => 'referentiel',
                'class'             => 'bootstrap-selectpicker show-tick',
                'data-live-search'  => 'true',
            ],
        ]);

        //mode
        $this->add([
            'type' => Select::class,
            'name' => 'mode',
            'options' => [
                'label' => 'Mode  <span class="icon icon-asterisque" title="Champ obligatoire"></span> :',
                'label_options' => [ 'disable_html_escape' => true, ],
                'value_options' => [
                    'preview' => "Prévisualisation",
                    'import' => "Importation",
                ]
            ],
            'attributes' => [
                'id' => 'mode',
            ],
        ]);
        //Submit
        $this->add([
            'type' => Button::class,
            'name' => 'creer',
            'options' => [
                'label' => 'Traiter le ficher',
                'label_options' => [ 'disable_html_escape' => true, ],
            ],
            'attributes' => [
                'type' => 'submit',
                'class' => 'btn btn-primary',
            ],
        ]);

        $this->setInputFilter((new Factory())->createInputFilter([
            'fichier' => [ 'required' => true,  ],
            'referentiel'  => ['required' => true, ],
            'mode'  => ['required' => true, ],
        ]));
    }
}
 No newline at end of file
+19 −0
Original line number Diff line number Diff line
<?php

namespace Element\Form\CompetenceImportation;

trait CompetenceImportationFormAwareTrait {

    private CompetenceImportationForm $competenceImportationForm;

    public function getCompetenceImportationForm(): CompetenceImportationForm
    {
        return $this->competenceImportationForm;
    }

    public function setCompetenceImportationForm(CompetenceImportationForm $competenceImportationForm): void
    {
        $this->competenceImportationForm = $competenceImportationForm;
    }

}
 No newline at end of file
Loading