Commit c59a6aae authored by Côme Chilliet's avatar Côme Chilliet

Merge branch '6032-supann-diploma-sise-select-has-too-many-options' into '1.4-dev'

Resolve "SupAnn diploma SISE select has too many options"

See merge request fusiondirectory/fd-plugins!680
parents 353b60a6 bf5dc380
......@@ -22,7 +22,7 @@ class supann
{
static $choices_for = [];
static function get_prefixed_choices_for ($shortname)
static function get_prefixed_choices_for (string $shortname, $processLabel = 'supann::truncate_label')
{
$dir = @opendir(SUPANN_DIR);
$m = [];
......@@ -31,7 +31,7 @@ class supann
while ($file = readdir($dir)) {
if (preg_match('/^'.$shortname.'_(.+)$/', $file, $m)) {
$prefix = $m[1];
$res[$prefix] =& static::get_choices_for($file);
$res[$prefix] =& static::get_choices_for($file, '', $processLabel);
}
}
closedir($dir);
......@@ -39,7 +39,7 @@ class supann
return $res;
}
static function &get_choices_for ($type, $prefix = '')
static function &get_choices_for (string $type, string $prefix = '', $processLabel = 'supann::truncate_label')
{
if (isset(static::$choices_for[$prefix.$type])) {
return static::$choices_for[$prefix.$type];
......@@ -59,10 +59,10 @@ class supann
while (($line = fgets($entiteList)) !== FALSE) {
$line = trim($line);
if (!preg_match('/^#/', $line) && !empty($line)) {
$entite_line = preg_split('/;/', $line);
$entite_line = explode(';', $line, 2);
$entity_codes[] = $prefix.$entite_line[0];
$entity_labels[] = static::truncate_label($entite_line[1]);
$entity_labels[] = $processLabel($entite_line[1]);
}
}
fclose($entiteList);
......@@ -72,7 +72,7 @@ class supann
return static::$choices_for[$prefix.$type];
}
/* return the 64 first chars and "…" after if text is longer */
/*! \brief Returns the $len first chars and "…" after if text is longer */
static function truncate_label ($str, $len = 50)
{
if (mb_strlen($str) > $len) {
......
This diff is collapsed.
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org)
Copyright (C) 2010-2012 Antoine Gallavardin
Copyright (C) 2013-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
class SupannCompositeAttribute extends CompositeAttribute
{
function __construct ($description, $ldapName, array $attributes, $acl = "", $label = "Composite attribute")
{
parent::__construct($description, $ldapName, $attributes, '', '', $acl, $label);
}
function readValues (string $value): array
{
$values = [];
$m = [];
foreach ($this->attributes as &$attribute) {
$shortname = preg_replace('/^[^_]+_/', '', $attribute->getLdapName());
if (preg_match("/\\[$shortname=([^\\]]+)\\]/", $value, $m)) {
$values[] = $m[1];
} else {
$values[] = "";
}
}
unset($attribute);
return $values;
}
function writeValues (array $values)
{
$value = '';
$i = 0;
foreach ($this->attributes as &$attribute) {
if ($values[$i] != '') {
$shortname = preg_replace('/^[^_]+_/', '', $attribute->getLdapName());
$value .= "[$shortname=".$values[$i]."]";
}
$i++;
}
unset($attribute);
return $value;
}
function supannGetValues (&$values)
{
foreach ($this->attributes as &$attribute) {
$shortname = preg_replace('/^([^_]+)_.*$/', '\\1', $attribute->getLdapName());
$value = $attribute->getValue();
if (!isset($values[$shortname])) {
$values[$shortname] = [];
}
if ($value == "") {
continue;
}
$values[$shortname][$value] = $value;
}
unset($attribute);
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org)
Copyright (C) 2010-2012 Antoine Gallavardin
Copyright (C) 2013-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
class SupannCursusAnneeAttribute extends SupannPrefixedSelectAttribute
{
function __construct ($label, $description, $ldapName, $required, $acl = "")
{
$attributes = [
new SelectAttribute('', '', $ldapName.'_prefix', $required),
new SelectAttribute('', '', $ldapName.'_content', FALSE)
];
CompositeAttribute::__construct($description, $ldapName, $attributes, '/^{SUPANN}(.)(\\d+)$/', '{SUPANN}%s%d', $acl, $label);
$this->setLinearRendering(TRUE);
$this->attributes[0]->setChoices(
['L','M','D','X','B'],
[_('Licence'),_('Master'),_('Ph.D.'),_('Another class of degree'),_('Post-graduate year')]
);
$yearLabels = [
_('1st year'),_('2nd year'),_('3rd year'),
_('4th year'),_('5th year'),_('6th year'),
_('7th year'),_('8th year'),_('9th year'),
];
$this->prefixedChoices = [
'L' => [range(1, 3),array_slice($yearLabels, 0, 3)],
'M' => [range(1, 2),array_slice($yearLabels, 0, 2)],
'D' => [range(1, 9),array_slice($yearLabels, 0, 9)],
'X' => [range(1, 9),array_slice($yearLabels, 0, 9)],
'B' => [range(0, 20),range(0, 20)],
];
$this->attributes[0]->setSubmitForm(TRUE);
$this->supannUpdateSelect();
$this->setRequired($required);
}
protected function supannUpdateSelect ()
{
$prefix = $this->attributes[0]->getValue();
$this->attributes[1]->setChoices($this->prefixedChoices[$prefix][0], $this->prefixedChoices[$prefix][1]);
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org)
Copyright (C) 2010-2012 Antoine Gallavardin
Copyright (C) 2013-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
class SupannEtuInscriptionAttribute extends SupannCompositeAttribute
{
protected $diplomas;
function __construct ($description, $ldapName, $acl = '', $label = 'Composite attribute')
{
$year = intval(date('Y'));
$attributes = [
new SelectAttribute(
_('Establishment'), _('supannEtablissement - Etablissement in which this registration was done'),
'supannEtablissement_etab', TRUE
),
new IntAttribute(
_('Year'), _('supannEtuAnneeInscription - The year this registration will begin'),
'supannEtuAnneeInscription_anneeinsc', TRUE,
$year - 100, $year + 100, $year
),
new SelectAttribute(
_('Registration type'), _('supannEtuRegimeInscription - The type of this registration'),
'supannEtuRegimeInscription_regimeinsc', TRUE
),
new SelectAttribute(
_('Disciplinary Sector'), _('supannEtuSecteurDisciplinaire - Disciplinary sector education diploma'),
'supannEtuSecteurDisciplinaire_sectdisc', TRUE
),
new SelectAttribute(
_('Diploma type'), _('supannEtuTypeDiplome - Type of diploma'),
'supannEtuTypeDiplome_typedip', TRUE
),
new SupannCursusAnneeAttribute(
_('Curriculum year '), _('supannEtuCursusAnnee - Type of curriculum (L, M, D or X, ...) and the year in the diploma.'),
'supannEtuCursusAnnee_cursusann', TRUE
),
new SelectAttribute(
_('Entity assignment'), _('supannEntiteAffectation - To wich entities does this user belong to'),
'supannEntiteAffectation_affect', FALSE
),
new SupannPrefixedSelectAttribute(
_('Diploma'), _('supannEtuDiplome - Diploma prepared by the student'),
'supannEtuDiplome_diplome', FALSE, ''
),
new SupannPrefixedSelectAttribute(
_('Step'), _('supannEtuEtape - Step can be considered a split (semester, year, etc.) in time of education leading to a diploma'),
'supannEtuEtape_etape', FALSE, 'etuetape'
),
new SupannPrefixedSelectAttribute(
_('educational element'), _('supannEtuElementPedagogique - Generic description of the content of education with a high level of granularity'),
'supannEtuElementPedagogique_eltpedago', FALSE, 'etuelementpedagogique'
),
];
parent::__construct($description, $ldapName, $attributes, $acl, $label);
list ($codes, $labels) = supann::get_choices_for('eturegimeinscription_SISE', '{SISE}');
$this->attributes[2]->setChoices($codes, $labels); // supannEtuRegimeInscription
list ($codes, $labels) = supann::get_choices_for('discipline_SISE', '{SISE}');
$this->attributes[3]->setChoices($codes, $labels); // supannEtuSecteurDisciplinaire
$this->attributes[3]->setSubmitForm(TRUE);
list ($codes, $labels) = supann::get_choices_for('typediplome_SISE', '{SISE}');
$this->attributes[4]->setChoices($codes, $labels); // supannEtuTypeDiplome
$this->attributes[4]->setSubmitForm(TRUE);
$prefixedChoices = supann::get_prefixed_choices_for(
'diplome',
function ($line)
{
return explode(';', $line);
}
);
$this->diplomas = [];
foreach ($prefixedChoices as $prefix => $choices) {
[$choicesId, $choicesLines] = $choices;
foreach ($choicesId as $i => $id) {
[$type,$sectdisc,$label1,$label2] = $choicesLines[$i];
if (!empty($label2)) {
$label1 .= ' - '.$label2;
}
if ($type[0] != '{') {
$type = '{'.$prefix.'}'.$type;
}
if ($sectdisc[0] != '{') {
$sectdisc = '{'.$prefix.'}'.$sectdisc;
}
$this->diplomas[$type][$sectdisc][$prefix][0][] = $id;
$this->diplomas[$type][$sectdisc][$prefix][1][] = $label1;
}
}
$this->supannUpdateSelect();
}
protected function supannUpdateSelect ()
{
$sectdisc = $this->attributes[3]->getValue();
$type = $this->attributes[4]->getValue();
$this->attributes[7]->setPrefixedChoices($this->diplomas[$type][$sectdisc] ?? []);
}
function applyPostValue ()
{
parent::applyPostValue();
$this->supannUpdateSelect();
}
function setValue ($values)
{
if (!is_array($values)) {
$values = $this->inputValue($values);
}
reset($values);
foreach ($this->attributes as &$attribute) {
if ($attribute->getLdapName() == 'supannEtuDiplome_diplome') {
$this->supannUpdateSelect();
}
$attribute->setValue(current($values));
next($values);
}
unset($attribute);
reset($values);
}
function resetToDefault ()
{
foreach ($this->attributes as &$attribute) {
if ($attribute->getLdapName() == 'supannEtuDiplome_diplome') {
$this->supannUpdateSelect();
}
$attribute->resetToDefault();
}
unset($attribute);
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org)
Copyright (C) 2010-2012 Antoine Gallavardin
Copyright (C) 2013-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
class SupannOrderedArrayAttribute extends OrderedArrayAttribute
{
function supannPrepareSave ()
{
$values = [];
foreach ($this->value as $value) {
$this->attribute->setValue($value);
$this->attribute->supannGetValues($values);
}
foreach ($values as $ldapName => $array) {
if ($this->plugin->attributesAccess[$ldapName]->isVisible()) {
continue;
}
$this->plugin->attributesAccess[$ldapName]->setValue(array_values($array));
}
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org)
Copyright (C) 2010-2012 Antoine Gallavardin
Copyright (C) 2013-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
class SupannPrefixedSelectAttribute extends CompositeAttribute
{
protected $prefixedChoices;
function __construct (string $label, string $description, string $ldapName, bool $required, string $filename, string $acl = '')
{
$attributes = [
new SelectAttribute('', '', $ldapName.'_prefix', $required),
new SelectAttribute('', '', $ldapName.'_content', $required)
];
parent::__construct($description, $ldapName, $attributes, '/^{(.*)}(.*)$/', '{%s}%s', $acl, $label);
$this->setLinearRendering(TRUE);
$this->attributes[0]->setSubmitForm(TRUE);
if (!empty($filename)) {
$this->setPrefixedChoices(supann::get_prefixed_choices_for($filename));
}
$this->setRequired($required);
}
protected function supannUpdateSelect ()
{
$prefix = $this->attributes[0]->getValue();
if (!isset($this->prefixedChoices[$prefix])) {
$this->prefixedChoices[$prefix] = [[], []];
}
$this->attributes[1]->setChoices($this->prefixedChoices[$prefix][0], $this->prefixedChoices[$prefix][1]);
$this->attributes[1]->setRequired($prefix != '');
}
public function setPrefixedChoices (array $prefixedChoices)
{
$this->prefixedChoices = $prefixedChoices;
if (!$this->isRequired() && !isset($this->prefixedChoices[''])) {
$this->prefixedChoices[''] = [[''], [_('None')]];
}
$this->attributes[0]->setChoices(array_keys($this->prefixedChoices));
$this->supannUpdateSelect();
}
function applyPostValue ()
{
parent::applyPostValue();
$this->supannUpdateSelect();
}
function setValue ($values)
{
if (!is_array($values)) {
$values = $this->inputValue($values);
}
$this->attributes[0]->setValue($values[0]);
$this->supannUpdateSelect();
$this->attributes[1]->setValue($values[1]);
}
function resetToDefault ()
{
$this->attributes[0]->resetToDefault();
$this->supannUpdateSelect();
$this->attributes[1]->resetToDefault();
}
function writeValues (array $values)
{
if ($values[0] == '') {
return '';
} else {
return parent::writeValues($values);
}
}
function displayValue ($values): string
{
if (!is_array($values)) {
$values = $this->inputValue($values);
}
$this->setValue($values);
$v1 = $this->attributes[0]->displayValue($values[0]);
$choices2 = $this->attributes[1]->getDisplayChoices();
if (isset($choices2[$values[1]])) {
$v2 = $choices2[$values[1]];
} else {
$v2 = $values[1];
}
return ($v1 == '' ? $v2 : $v1.': '.$v2);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment