2023-03-21 21:14:38 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# Gestion scolarite IUT
|
|
|
|
#
|
2023-12-31 23:04:06 +01:00
|
|
|
# Copyright (c) 1999 - 2024 Emmanuel Viennet. All rights reserved.
|
2023-03-21 21:14:38 +01:00
|
|
|
#
|
|
|
|
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
# Emmanuel Viennet emmanuel.viennet@viennet.net
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
"""Association de nouvelles versions de formation à des formsemestre
|
|
|
|
"""
|
|
|
|
import flask
|
|
|
|
from flask import url_for, flash
|
|
|
|
from flask import g, request
|
|
|
|
|
|
|
|
from app import db
|
|
|
|
from app.models import (
|
|
|
|
Module,
|
|
|
|
ModuleImpl,
|
|
|
|
Evaluation,
|
|
|
|
EvaluationUEPoids,
|
2023-03-22 21:39:55 +01:00
|
|
|
ScolarEvent,
|
|
|
|
ScolarFormSemestreValidation,
|
2023-03-21 21:14:38 +01:00
|
|
|
UniteEns,
|
|
|
|
)
|
|
|
|
|
|
|
|
from app.models.formations import Formation
|
|
|
|
from app.models.formsemestre import FormSemestre
|
|
|
|
import app.scodoc.sco_utils as scu
|
|
|
|
|
|
|
|
from app import log
|
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
2024-10-10 00:41:20 +02:00
|
|
|
from app.formations import formation_io
|
2023-03-21 21:14:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def formsemestre_associate_new_version(
|
|
|
|
formation_id: int,
|
|
|
|
formsemestre_id: int = None,
|
|
|
|
other_formsemestre_ids: list[int] = None,
|
|
|
|
):
|
|
|
|
"""Formulaire nouvelle version formation et association d'un ou plusieurs formsemestre.
|
|
|
|
formation_id: la formation à dupliquer
|
2023-03-22 21:39:55 +01:00
|
|
|
formsemestre_id: optionnel, formsemestre de départ, qui sera associé à la nouvelle version
|
2023-03-21 21:14:38 +01:00
|
|
|
"""
|
2023-03-22 21:39:55 +01:00
|
|
|
formsemestre_id = int(formsemestre_id) if formsemestre_id else None
|
2024-10-29 19:18:36 +01:00
|
|
|
formation: Formation = Formation.get_or_404(formation_id)
|
2023-03-22 21:39:55 +01:00
|
|
|
other_formsemestre_ids = {int(x) for x in (other_formsemestre_ids or [])}
|
2023-03-21 21:14:38 +01:00
|
|
|
if request.method == "GET":
|
|
|
|
# dresse la liste des semestres non verrouillés de la même formation
|
|
|
|
other_formsemestres: list[FormSemestre] = formation.formsemestres.filter_by(
|
|
|
|
etat=True
|
|
|
|
)
|
|
|
|
|
|
|
|
H = []
|
|
|
|
for other_formsemestre in other_formsemestres:
|
|
|
|
checked = (
|
|
|
|
'checked="checked"'
|
|
|
|
if (
|
|
|
|
other_formsemestre.id == formsemestre_id
|
|
|
|
or other_formsemestre.id in other_formsemestre_ids
|
|
|
|
)
|
|
|
|
else ""
|
|
|
|
)
|
|
|
|
disabled = (
|
|
|
|
'disabled="1"' if other_formsemestre.id == formsemestre_id else ""
|
|
|
|
)
|
|
|
|
|
|
|
|
H.append(
|
|
|
|
f"""<div><input type="checkbox" name="other_formsemestre_ids:list"
|
|
|
|
value="{other_formsemestre.id}" {checked} {disabled}
|
|
|
|
><a class="stdlink" href="{
|
2023-03-22 21:39:55 +01:00
|
|
|
url_for("notes.formsemestre_status",
|
2023-03-21 21:14:38 +01:00
|
|
|
scodoc_dept=g.scodoc_dept, formsemestre_id=other_formsemestre.id)
|
|
|
|
}">{other_formsemestre.titre_mois()}</a></input></div>"""
|
|
|
|
)
|
|
|
|
if formsemestre_id is None:
|
|
|
|
cancel_url = url_for(
|
|
|
|
"notes.ue_table", scodoc_dept=g.scodoc_dept, formation_id=formation.id
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
cancel_url = url_for(
|
|
|
|
"notes.formsemestre_status",
|
|
|
|
scodoc_dept=g.scodoc_dept,
|
|
|
|
formsemestre_id=formsemestre_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
return scu.confirm_dialog(
|
|
|
|
(
|
|
|
|
"""<h2>Associer à une nouvelle version de formation non verrouillée ?</h2>"""
|
|
|
|
if formsemestre_id
|
|
|
|
else """<h2>Créer une nouvelle version de la formation ?</h2>"""
|
|
|
|
)
|
|
|
|
+ f"""<p><b>Formation: </b><a class="stdlink" href="{
|
|
|
|
url_for("notes.ue_table", scodoc_dept=g.scodoc_dept, formation_id=formation.id)
|
|
|
|
}">{formation.titre} version {formation.version}</a></p>
|
2023-12-31 23:04:06 +01:00
|
|
|
|
2023-03-21 21:14:38 +01:00
|
|
|
<p class="help">Le programme pédagogique ("formation") va être dupliqué
|
2023-12-31 23:04:06 +01:00
|
|
|
pour que vous puissiez le modifier sans affecter les semestres déjà terminés.
|
2023-03-21 21:14:38 +01:00
|
|
|
</p>
|
|
|
|
<p class="help">Veillez à ne pas abuser de cette possibilité, car créer
|
|
|
|
trop de versions de formations va vous compliquer la gestion
|
|
|
|
(à vous de garder trace des différences et à ne pas vous
|
|
|
|
tromper par la suite...).
|
|
|
|
</p>
|
|
|
|
<div class="othersemlist">
|
|
|
|
<p>Si vous voulez associer des semestres à la nouvelle
|
|
|
|
version, cochez-les maintenant <br>
|
2023-12-31 23:04:06 +01:00
|
|
|
(<b>attention : vous ne pourrez pas le faire plus tard car on ne peut pas
|
2023-03-21 21:14:38 +01:00
|
|
|
changer la formation d'un semestre !</b>):
|
|
|
|
</p>
|
|
|
|
{ "".join(H) }
|
|
|
|
<p>Les données (étudiants, notes...) de ces semestres seront inchangées.</p>
|
|
|
|
</div>
|
2023-03-22 21:39:55 +01:00
|
|
|
"""
|
|
|
|
+ (
|
|
|
|
f"""
|
2023-03-21 21:14:38 +01:00
|
|
|
<div class="othersemlist">
|
|
|
|
Vous pouvez aussi essayer d'<a class="stdlink" href="{url_for(
|
|
|
|
"notes.formsemestre_change_formation",
|
|
|
|
scodoc_dept=g.scodoc_dept, formsemestre_id=formsemestre_id
|
|
|
|
)}
|
|
|
|
">associer ce semestre à une autre formation identique</a>.
|
|
|
|
</div>
|
2023-03-22 21:39:55 +01:00
|
|
|
"""
|
|
|
|
if formsemestre_id is not None
|
|
|
|
else ""
|
|
|
|
),
|
2023-03-21 21:14:38 +01:00
|
|
|
OK="Créer une nouvelle version et y associer ces semestres",
|
|
|
|
dest_url="",
|
|
|
|
cancel_url=cancel_url,
|
|
|
|
parameters={
|
|
|
|
"formation_id": formation_id,
|
|
|
|
"formsemestre_id": formsemestre_id,
|
|
|
|
},
|
2024-08-24 08:06:46 +02:00
|
|
|
template="sco_page_dept.j2",
|
2023-03-21 21:14:38 +01:00
|
|
|
)
|
|
|
|
elif request.method == "POST":
|
|
|
|
if formsemestre_id is not None: # pas dans le form car checkbox disabled
|
|
|
|
other_formsemestre_ids |= {formsemestre_id}
|
|
|
|
new_formation_id = do_formsemestres_associate_new_version(
|
|
|
|
formation_id, other_formsemestre_ids
|
|
|
|
)
|
|
|
|
flash(
|
|
|
|
"Nouvelle version de la formation créée"
|
|
|
|
+ (" et semestres associés." if other_formsemestre_ids else ".")
|
|
|
|
)
|
|
|
|
if formsemestre_id is None:
|
|
|
|
return flask.redirect(
|
|
|
|
url_for(
|
|
|
|
"notes.ue_table",
|
|
|
|
scodoc_dept=g.scodoc_dept,
|
|
|
|
formation_id=new_formation_id,
|
|
|
|
)
|
|
|
|
)
|
2024-01-26 14:57:50 +01:00
|
|
|
return flask.redirect(
|
|
|
|
url_for(
|
|
|
|
"notes.formsemestre_status",
|
|
|
|
scodoc_dept=g.scodoc_dept,
|
|
|
|
formsemestre_id=formsemestre_id,
|
2023-03-21 21:14:38 +01:00
|
|
|
)
|
2024-01-26 14:57:50 +01:00
|
|
|
)
|
|
|
|
raise ScoValueError("Méthode invalide")
|
2023-03-21 21:14:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def do_formsemestres_associate_new_version(
|
|
|
|
formation_id: int, formsemestre_ids: list[int]
|
|
|
|
) -> int:
|
|
|
|
"""Crée une nouvelle version de la formation du semestre, et y rattache les semestres.
|
|
|
|
Tous les moduleimpl sont ré-associés à la nouvelle formation, ainsi que les decisions de jury
|
|
|
|
si elles existent (codes d'UE validées).
|
|
|
|
Les semestre doivent tous appartenir à la meme version de la formation.
|
|
|
|
renvoie l'id de la nouvelle formation.
|
|
|
|
"""
|
|
|
|
log(f"do_formsemestres_associate_new_version {formation_id} {formsemestre_ids}")
|
|
|
|
|
2023-03-22 21:39:55 +01:00
|
|
|
# Check: tous les semestres de la formation
|
2024-10-29 19:18:36 +01:00
|
|
|
formsemestres = [FormSemestre.get_or_404(i) for i in formsemestre_ids]
|
2023-03-21 21:14:38 +01:00
|
|
|
if not all(
|
|
|
|
[formsemestre.formation_id == formation_id for formsemestre in formsemestres]
|
|
|
|
):
|
|
|
|
raise ScoValueError("les semestres ne sont pas tous de la même formation !")
|
|
|
|
|
|
|
|
# New formation:
|
|
|
|
(
|
2023-07-12 22:11:33 +02:00
|
|
|
new_formation_id,
|
2023-03-21 21:14:38 +01:00
|
|
|
modules_old2new,
|
|
|
|
ues_old2new,
|
2024-10-10 00:41:20 +02:00
|
|
|
) = formation_io.formation_create_new_version(formation_id, redirect=False)
|
2023-03-21 21:14:38 +01:00
|
|
|
# Log new ues:
|
|
|
|
for ue_id in ues_old2new:
|
2023-07-11 06:57:38 +02:00
|
|
|
ue = db.session.get(UniteEns, ue_id)
|
|
|
|
new_ue = db.session.get(UniteEns, ues_old2new[ue_id])
|
2023-03-21 21:14:38 +01:00
|
|
|
assert ue.semestre_idx == new_ue.semestre_idx
|
|
|
|
log(f"{ue} -> {new_ue}")
|
|
|
|
# Log new modules
|
|
|
|
for module_id in modules_old2new:
|
2023-07-11 06:57:38 +02:00
|
|
|
mod = db.session.get(Module, module_id)
|
|
|
|
new_mod = db.session.get(Module, modules_old2new[module_id])
|
2023-03-21 21:14:38 +01:00
|
|
|
assert mod.semestre_id == new_mod.semestre_id
|
|
|
|
log(f"{mod} -> {new_mod}")
|
|
|
|
# re-associate
|
|
|
|
for formsemestre_id in formsemestre_ids:
|
2023-03-22 21:39:55 +01:00
|
|
|
formsemestre = FormSemestre.get_formsemestre(formsemestre_id)
|
2023-07-12 22:11:33 +02:00
|
|
|
formsemestre.formation_id = new_formation_id
|
2023-03-22 21:39:55 +01:00
|
|
|
db.session.add(formsemestre)
|
|
|
|
_reassociate_moduleimpls(formsemestre, ues_old2new, modules_old2new)
|
2023-03-21 21:14:38 +01:00
|
|
|
|
2023-03-22 21:39:55 +01:00
|
|
|
db.session.commit()
|
2023-07-17 15:09:17 +02:00
|
|
|
return new_formation_id
|
2023-03-21 21:14:38 +01:00
|
|
|
|
|
|
|
|
2023-03-22 21:39:55 +01:00
|
|
|
def _reassociate_moduleimpls(
|
|
|
|
formsemestre: FormSemestre,
|
|
|
|
ues_old2new: dict[int, int],
|
|
|
|
modules_old2new: dict[int, int],
|
|
|
|
):
|
2023-03-21 21:14:38 +01:00
|
|
|
"""Associe les moduleimpls d'un semestre existant à un autre programme
|
|
|
|
et met à jour les décisions de jury (validations d'UE).
|
|
|
|
"""
|
|
|
|
# re-associate moduleimpls to new modules:
|
2023-03-22 21:39:55 +01:00
|
|
|
for modimpl in formsemestre.modimpls:
|
|
|
|
modimpl.module_id = modules_old2new[modimpl.module_id]
|
|
|
|
db.session.add(modimpl)
|
2023-03-21 21:14:38 +01:00
|
|
|
# Update poids des évaluations
|
|
|
|
# les poids associent les évaluations aux UE (qui ont changé d'id)
|
|
|
|
for poids in EvaluationUEPoids.query.filter(
|
|
|
|
EvaluationUEPoids.evaluation_id == Evaluation.id,
|
|
|
|
Evaluation.moduleimpl_id == ModuleImpl.id,
|
2023-03-22 21:39:55 +01:00
|
|
|
ModuleImpl.formsemestre_id == formsemestre.id,
|
2023-03-21 21:14:38 +01:00
|
|
|
):
|
2023-07-12 22:11:33 +02:00
|
|
|
if poids.ue_id in ues_old2new:
|
|
|
|
poids.ue_id = ues_old2new[poids.ue_id]
|
|
|
|
db.session.add(poids)
|
|
|
|
else:
|
|
|
|
# poids vers une UE qui n'est pas ou plus dans notre formation
|
|
|
|
db.session.delete(poids)
|
2023-03-21 21:14:38 +01:00
|
|
|
|
|
|
|
# update decisions:
|
2023-03-22 21:39:55 +01:00
|
|
|
for event in ScolarEvent.query.filter_by(formsemestre_id=formsemestre.id):
|
|
|
|
if event.ue_id is not None:
|
|
|
|
event.ue_id = ues_old2new[event.ue_id]
|
|
|
|
db.session.add(event)
|
|
|
|
|
|
|
|
for validation in ScolarFormSemestreValidation.query.filter_by(
|
|
|
|
formsemestre_id=formsemestre.id
|
|
|
|
):
|
2023-07-12 22:11:33 +02:00
|
|
|
if (validation.ue_id is not None) and validation.ue_id in ues_old2new:
|
2023-03-22 21:39:55 +01:00
|
|
|
validation.ue_id = ues_old2new[validation.ue_id]
|
2023-07-12 22:11:33 +02:00
|
|
|
# si l'UE n'est pas ou plus dans notre formation, laisse.
|
2023-03-22 21:39:55 +01:00
|
|
|
db.session.add(validation)
|
|
|
|
|
|
|
|
db.session.commit()
|
2023-03-21 21:14:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
def formations_are_equals(
|
|
|
|
formation1: Formation, formation2: Formation = None, formation2_dict: dict = None
|
|
|
|
) -> bool:
|
|
|
|
"""True if the two formations are exactly the same, except for their versions.
|
|
|
|
Can specify either formation2 or its dict repr.
|
|
|
|
"""
|
2024-10-10 00:41:20 +02:00
|
|
|
fd1 = formation_io.formation_export_dict(
|
2023-03-21 21:14:38 +01:00
|
|
|
formation1, export_external_ues=True, ue_reference_style="acronyme"
|
|
|
|
)
|
|
|
|
if formation2_dict is None:
|
|
|
|
if formation2 is None:
|
|
|
|
raise ValueError("must specify formation2 or formation2_dict")
|
2024-10-10 00:41:20 +02:00
|
|
|
formation2_dict = formation_io.formation_export_dict(
|
2023-03-21 21:14:38 +01:00
|
|
|
formation2, export_external_ues=True, ue_reference_style="acronyme"
|
|
|
|
)
|
|
|
|
del fd1["version"]
|
|
|
|
if "version" in formation2_dict:
|
|
|
|
del formation2_dict["version"]
|
|
|
|
return fd1 == formation2_dict
|
|
|
|
|
|
|
|
|
|
|
|
def formsemestre_change_formation(formsemestre: FormSemestre, new_formation: Formation):
|
|
|
|
"""Change la formation d'un semestre. La nouvelle formation doit avoir exactement
|
|
|
|
le même contenu que l'actuelle, à l'exception du numéro de version.
|
|
|
|
"""
|
|
|
|
if not formations_are_equals(formsemestre.formation, new_formation):
|
|
|
|
raise ScoValueError(
|
|
|
|
"formsemestre_change_formation: les deux formations diffèrent"
|
|
|
|
)
|
|
|
|
log(
|
|
|
|
f"formsemestre_change_formation: formsemestre {formsemestre} to formation {new_formation}"
|
|
|
|
)
|
2023-03-22 21:39:55 +01:00
|
|
|
# Il faut ré-associer tous les modimpls et les UEs
|
|
|
|
modules_old2new = {}
|
2023-03-21 21:14:38 +01:00
|
|
|
for modimpl in formsemestre.modimpls:
|
|
|
|
old_module: Module = modimpl.module
|
|
|
|
new_module: Module = (
|
|
|
|
Module.query.filter_by(
|
|
|
|
formation_id=new_formation.id,
|
|
|
|
code=old_module.code,
|
|
|
|
titre=old_module.titre,
|
|
|
|
)
|
|
|
|
.join(UniteEns)
|
|
|
|
.filter_by(acronyme=old_module.ue.acronyme)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if new_module is None:
|
|
|
|
raise ValueError(
|
|
|
|
f"formsemestre_change_formation: erreur sur module {old_module}"
|
|
|
|
)
|
2023-03-22 21:39:55 +01:00
|
|
|
modules_old2new[old_module.id] = new_module.id
|
|
|
|
|
|
|
|
ues_old2new = {}
|
|
|
|
for old_ue in formsemestre.formation.ues:
|
|
|
|
new_ue: UniteEns = UniteEns.query.filter_by(
|
|
|
|
formation_id=new_formation.id, acronyme=old_ue.acronyme, titre=old_ue.titre
|
|
|
|
).first()
|
|
|
|
if new_ue is None:
|
|
|
|
raise ValueError(f"formsemestre_change_formation: erreur sur UE {old_ue}")
|
|
|
|
ues_old2new[old_ue.id] = new_ue.id
|
|
|
|
|
2023-03-21 21:14:38 +01:00
|
|
|
formsemestre.formation = new_formation
|
2023-03-22 21:39:55 +01:00
|
|
|
db.session.add(formsemestre)
|
|
|
|
_reassociate_moduleimpls(formsemestre, ues_old2new, modules_old2new)
|
|
|
|
|
2023-03-21 21:14:38 +01:00
|
|
|
db.session.commit()
|