ScoDoc-Lille/app/scodoc/sco_formsemestre_exterieurs.py

528 lines
19 KiB
Python
Raw Permalink Normal View History

2020-09-26 16:19:37 +02:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
2023-01-02 13:16:27 +01:00
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
2020-09-26 16:19:37 +02: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
#
##############################################################################
"""Saisie et gestion des semestres extérieurs à ScoDoc dans un parcours.
On va créer/gérer des semestres de la même formation que le semestre ScoDoc
est inscrit l'étudiant, leur attribuer la modalité 'EXT'.
Ces semestres n'auront qu'un seul inscrit !
"""
import time
2021-08-01 10:16:16 +02:00
import flask
from flask import url_for, g, request
2021-08-22 13:24:36 +02:00
from flask_login import current_user
from app.comp import res_sem
from app.comp.res_compat import NotesTableCompat
from app.models import (
Formation,
FormSemestre,
FormSemestreUECoef,
Identite,
ScolarFormSemestreValidation,
UniteEns,
)
import app.scodoc.sco_utils as scu
2021-08-29 19:57:32 +02:00
from app import log
from app.scodoc.TrivialFormulator import TrivialFormulator, tf_error_message
from app.scodoc import html_sco_header
from app.scodoc import sco_formations
from app.scodoc import sco_formsemestre
from app.scodoc import sco_formsemestre_inscriptions
from app.scodoc import sco_formsemestre_validation
from app.scodoc.codes_cursus import UE_SPORT
2020-09-26 16:19:37 +02:00
def formsemestre_ext_create(etudid, sem_params):
2020-09-26 16:19:37 +02:00
"""Crée un formsemestre exterieur et y inscrit l'étudiant.
sem_params: dict nécessaire à la création du formsemestre
"""
# Check args
_ = Formation.query.get_or_404(sem_params["formation_id"])
2020-09-26 16:19:37 +02:00
if etudid:
_ = Identite.query.get_or_404(etudid)
2020-09-26 16:19:37 +02:00
# Create formsemestre
sem_params["modalite"] = "EXT"
sem_params["etapes"] = None
2021-08-22 13:24:36 +02:00
sem_params["responsables"] = [current_user.id]
formsemestre_id = sco_formsemestre.do_formsemestre_create(sem_params, silent=True)
2020-09-26 16:19:37 +02:00
# nota: le semestre est créé vide: pas de modules
# Inscription au semestre
sco_formsemestre_inscriptions.do_formsemestre_inscription_with_modules(
formsemestre_id,
etudid,
method="formsemestre_ext_create",
)
return formsemestre_id
def formsemestre_ext_create_form(etudid, formsemestre_id):
2022-09-03 10:07:34 +02:00
"""Formulaire création/inscription à un semestre extérieur"""
etud = Identite.query.get_or_404(etudid)
2020-09-26 16:19:37 +02:00
H = [
html_sco_header.sco_header(),
2022-09-03 10:07:34 +02:00
f"""<h2>Enregistrement d'une inscription antérieure dans un autre
établissement</h2>
2020-09-26 16:19:37 +02:00
<p class="help">
Cette opération crée un semestre extérieur ("ancien") de la même
formation que le semestre courant, et y inscrit juste cet étudiant.
2022-09-03 10:07:34 +02:00
La décision de jury peut ensuite y être saisie.
2020-09-26 16:19:37 +02:00
</p>
<p class="help">
2022-09-03 10:07:34 +02:00
Notez que si un semestre extérieur similaire a déjà été créé pour un autre
étudiant, il est préférable d'utiliser la fonction
"<a href="{ url_for('notes.formsemestre_inscription_with_modules_form',
scodoc_dept=g.scodoc_dept, etudid=etudid, only_ext=1) }">
2020-09-26 16:19:37 +02:00
inscrire à un autre semestre</a>"
</p>
2022-09-03 10:07:34 +02:00
<h3><a href="{ url_for('scolar.ficheEtud',
scodoc_dept=g.scodoc_dept, etudid=etudid)
}" class="stdlink">Étudiant {etud.nomprenom}</a></h3>
2022-09-03 10:07:34 +02:00
""",
2020-09-26 16:19:37 +02:00
]
F = html_sco_header.sco_footer()
2021-08-19 10:28:35 +02:00
orig_sem = sco_formsemestre.get_formsemestre(formsemestre_id)
2022-09-03 10:07:34 +02:00
# Ne propose que des semestres de semestre_id strictement inférieur
# au semestre courant
2020-09-26 16:19:37 +02:00
# et seulement si pas inscrit au même semestre_id d'un semestre ordinaire ScoDoc.
# Les autres situations (eg redoublements en changeant d'établissement)
# doivent être gérées par les validations de semestres "antérieurs"
insem = sco_formsemestre_inscriptions.do_formsemestre_inscription_list(
args={"etudid": etudid, "etat": scu.INSCRIT}
2020-09-26 16:19:37 +02:00
)
2021-08-19 10:28:35 +02:00
semlist = [sco_formsemestre.get_formsemestre(i["formsemestre_id"]) for i in insem]
2022-09-03 10:07:34 +02:00
existing_semestre_ids = {s["semestre_id"] for s in semlist}
2020-09-26 16:19:37 +02:00
min_semestre_id = 1
max_semestre_id = orig_sem["semestre_id"]
semestre_ids = set(range(min_semestre_id, max_semestre_id)) - existing_semestre_ids
H.append(
2022-09-03 10:07:34 +02:00
f"""<p>L'étudiant est déjà inscrit dans des semestres ScoDoc de rangs:
{ sorted(list(existing_semestre_ids)) }
</p>
"""
2020-09-26 16:19:37 +02:00
)
if not semestre_ids:
H.append(
f"""<p class="warning">pas de semestres extérieurs possibles
(indices entre {min_semestre_id} et {max_semestre_id}, semestre courant.)
</p>"""
)
2020-09-26 16:19:37 +02:00
return "\n".join(H) + F
# Formulaire
2021-08-30 23:28:15 +02:00
semestre_ids_list = sorted(semestre_ids)
semestre_ids_labels = [f"S{x}" for x in semestre_ids_list]
2020-09-26 16:19:37 +02:00
descr = [
("formsemestre_id", {"input_type": "hidden"}),
("etudid", {"input_type": "hidden"}),
(
"semestre_id",
{
"input_type": "menu",
"title": "Indice du semestre dans le cursus",
2021-08-30 23:28:15 +02:00
"allowed_values": semestre_ids_list,
"labels": semestre_ids_labels,
2020-09-26 16:19:37 +02:00
},
),
(
"titre",
{
"size": 40,
"title": "Nom de ce semestre extérieur",
"explanation": """par exemple: établissement.
N'indiquez pas les dates, ni le semestre, ni la modalité dans
le titre: ils seront automatiquement ajoutés""",
2020-09-26 16:19:37 +02:00
},
),
(
"date_debut",
{
"title": "Date de début", # j/m/a
"input_type": "datedmy",
2020-09-26 16:19:37 +02:00
"explanation": "j/m/a (peut être approximatif)",
"size": 9,
"allow_null": False,
},
),
(
"date_fin",
{
"title": "Date de fin", # j/m/a
"input_type": "datedmy",
2020-09-26 16:19:37 +02:00
"explanation": "j/m/a (peut être approximatif)",
"size": 9,
"allow_null": False,
},
),
(
"elt_help_ue",
{
2022-09-03 10:07:34 +02:00
"title": """Les notes et coefficients des UE
2020-09-26 16:19:37 +02:00
capitalisées seront saisis ensuite""",
"input_type": "separator",
},
),
]
tf = TrivialFormulator(
request.base_url,
scu.get_request_args(),
2020-09-26 16:19:37 +02:00
descr,
cancelbutton="Annuler",
method="post",
submitlabel="Créer semestre extérieur et y inscrire l'étudiant",
cssclass="inscription",
name="tf",
)
if tf[0] == 0:
H.append(
2022-09-03 10:07:34 +02:00
"""<p>Ce formulaire sert à enregistrer un semestre antérieur dans
la formation effectué dans un autre établissement.
2020-09-26 16:19:37 +02:00
</p>"""
)
return "\n".join(H) + "\n" + tf[1] + F
elif tf[0] == -1:
2021-07-31 18:01:10 +02:00
return flask.redirect(
2022-09-03 10:07:34 +02:00
url_for(
"notes.formsemestre_bulletinetud",
scodoc_dept=g.scodoc_dept,
formsemestre_id=formsemestre_id,
etudid=etudid,
)
2020-09-26 16:19:37 +02:00
)
else:
# Le semestre extérieur est créé dans la même formation que le semestre courant
2020-09-26 16:19:37 +02:00
tf[2]["formation_id"] = orig_sem["formation_id"]
formsemestre_ext_create(etudid, tf[2])
2021-07-31 18:01:10 +02:00
return flask.redirect(
url_for("scolar.ficheEtud", scodoc_dept=g.scodoc_dept, etudid=etudid)
2020-09-26 16:19:37 +02:00
)
def formsemestre_ext_edit_ue_validations(formsemestre_id, etudid):
2020-09-26 16:19:37 +02:00
"""Edition des validations d'UE et de semestre (jury)
pour un semestre extérieur.
On peut saisir pour chaque UE du programme de formation
sa validation, son code jury, sa note, son coefficient
(sauf en BUT le coef. des UE est toujours égal aux ECTS).
2020-09-26 16:19:37 +02:00
La moyenne générale indicative du semestre est calculée et affichée,
2020-09-26 16:19:37 +02:00
mais pas enregistrée.
"""
formsemestre: FormSemestre = FormSemestre.query.get_or_404(formsemestre_id)
etud = Identite.query.get_or_404(etudid)
ues = formsemestre.formation.ues.filter(UniteEns.type != UE_SPORT).order_by(
UniteEns.semestre_idx, UniteEns.numero
)
if formsemestre.formation.is_apc():
ues = ues.filter_by(semestre_idx=formsemestre.semestre_id)
descr = _ue_form_description(formsemestre, etud, ues, scu.get_request_args())
initvalues = {}
if request.method == "GET":
for ue in ues:
validation = ScolarFormSemestreValidation.query.filter_by(
ue_id=ue.id, etudid=etud.id, formsemestre_id=formsemestre.id
).first()
initvalues[f"note_{ue.id}"] = validation.moy_ue if validation else ""
tf = TrivialFormulator(
request.base_url,
scu.get_request_args(),
2020-09-26 16:19:37 +02:00
descr,
submitlabel="Enregistrer ces validations",
cancelbutton="Annuler",
initvalues=initvalues,
cssclass="tf_ext_edit_ue_validations ext_apc"
if formsemestre.formation.is_apc()
else "tf_ext_edit_ue_validations",
# En APC, stocke les coefficients pour l'affichage de la moyenne en direct
form_attrs=f"""data-ue_coefs='[{', '.join(str(ue.ects or 0) for ue in ues)}]'"""
if formsemestre.formation.is_apc()
else "",
2020-09-26 16:19:37 +02:00
)
if tf[0] == -1:
return "<h4>annulation</h4>"
else:
H = _make_page(etud, formsemestre, tf)
2020-09-26 16:19:37 +02:00
if tf[0] == 0: # premier affichage
return "\n".join(H)
else: # soumission
# simule erreur
ok, message = _check_values(formsemestre, ues, tf[2])
2020-09-26 16:19:37 +02:00
if not ok:
H = _make_page(etud, formsemestre, tf, message=message)
2020-09-26 16:19:37 +02:00
return "\n".join(H)
else:
# Submit
_record_ue_validations_and_coefs(formsemestre, etud, ues, tf[2])
2021-07-31 18:01:10 +02:00
return flask.redirect(
url_for(
"notes.formsemestre_bulletinetud",
scodoc_dept=g.scodoc_dept,
formsemestre_id=formsemestre_id,
etudid=etudid,
)
2020-09-26 16:19:37 +02:00
)
def _make_page(etud: Identite, formsemestre: FormSemestre, tf, message="") -> list[str]:
"""html formulaire saisie"""
nt: NotesTableCompat = res_sem.load_formsemestre_results(formsemestre)
moy_gen = nt.get_etud_moy_gen(etud.id)
2020-09-26 16:19:37 +02:00
H = [
2021-06-13 23:37:14 +02:00
html_sco_header.sco_header(
2020-09-26 16:19:37 +02:00
page_title="Validation des UE d'un semestre extérieur",
javascripts=["js/formsemestre_ext_edit_ue_validations.js"],
),
tf_error_message(message),
f"""<p><b>{etud.nomprenom}</b> est inscrit{etud.e} à ce semestre extérieur.</p>
<p>Voici ses UE enregistrées avec leur notes
{ "et coefficients" if not formsemestre.formation.is_apc()
else " (en BUT, les coefficients sont égaux aux ECTS)"}.
2020-09-26 16:19:37 +02:00
</p>
""",
2022-05-10 22:40:00 +02:00
f"""<p>La moyenne de ce semestre serait:
<span class="ext_sem_moy"><span class="ext_sem_moy_val">{moy_gen}</span> / 20</span>
2020-09-26 16:19:37 +02:00
</p>
2022-05-10 22:40:00 +02:00
""",
2020-09-26 16:19:37 +02:00
'<div id="formsemestre_ext_edit_ue_validations">',
tf[1],
"</div>",
2022-05-10 22:40:00 +02:00
f"""<div>
<a class="stdlink"
href="{url_for("notes.formsemestre_bulletinetud", scodoc_dept=g.scodoc_dept,
formsemestre_id=formsemestre.id, etudid=etud.id
2022-05-10 22:40:00 +02:00
)}">retour au bulletin de notes</a>
</div>
""",
html_sco_header.sco_footer(),
2020-09-26 16:19:37 +02:00
]
return H
_UE_VALID_CODES = {
None: "Non inscrit",
"ADM": "Capitalisée (ADM)",
# "CMP": "Acquise (car semestre validé)",
}
def _ue_form_description(
formsemestre: FormSemestre, etud: Identite, ues: list[UniteEns], values
):
2020-09-26 16:19:37 +02:00
"""Description du formulaire de saisie des UE / validations
Pour chaque UE, on peut saisir: son code jury, sa note, son coefficient.
"""
descr = [
(
"head_sep",
{
"input_type": "separator",
"template": """<tr %(item_dom_attr)s><th>UE</th>
<th>Code jury</th><th>Note/20</th>
"""
+ (
"""<th>Coefficient UE</th>"""
if not formsemestre.formation.is_apc()
else ""
)
+ "</tr>",
2020-09-26 16:19:37 +02:00
},
),
("formsemestre_id", {"input_type": "hidden"}),
("etudid", {"input_type": "hidden"}),
]
for ue in ues:
2020-09-26 16:19:37 +02:00
# Menu pour code validation UE:
# Ne propose que ADM, CMP et "Non inscrit"
select_name = f"valid_{ue.id}"
menu_code_ue = f"""<select class="ueext_valid_select" name="{select_name}">"""
cur_code_value = values.get("valid_{ue.id}", False)
for (code, explanation) in _UE_VALID_CODES.items():
if cur_code_value is False: # pas dans le form, cherche en base
validation = ScolarFormSemestreValidation.query.filter_by(
ue_id=ue.id, etudid=etud.id, formsemestre_id=formsemestre.id
).first()
cur_code_value = validation.code if validation else None
if str(cur_code_value) == str(code):
2020-09-26 16:19:37 +02:00
selected = "selected"
else:
selected = ""
# code jury:
menu_code_ue += (
f"""<option value="{code}" {selected}>{explanation}</option>"""
2020-09-26 16:19:37 +02:00
)
if cur_code_value is None:
coef_disabled = 'disabled="1"'
2020-09-26 16:19:37 +02:00
else:
coef_disabled = ""
menu_code_ue += "</select>"
if formsemestre.formation.is_apc():
coef_disabled = 'disabled="1"'
cur_coef_value = ue.ects or 0
coef_input_class = "ext_coef_disabled"
else:
cur_coef_value = values.get(f"coef_{ue.id}", False)
coef_input_class = ""
if cur_coef_value is False: # pas dans le form, cherche en base
ue_coef: FormSemestreUECoef = FormSemestreUECoef.query.filter_by(
formsemestre_id=formsemestre.id, ue_id=ue.id
).first()
cur_coef_value = (ue_coef.coefficient if ue_coef else "") or ""
2020-09-26 16:19:37 +02:00
itemtemplate = (
f"""
<tr>
<td class="tf-fieldlabel">%(label)s</td>
<td>{ menu_code_ue }</td>
<td class="tf-field tf_field_note">%(elem)s</td>
"""
+ (
f"""<td class="tf-field tf_field_coef">
<input type="text" size="4" name="coef_{ue.id}"
class="{coef_input_class}"
value="{cur_coef_value}" {coef_disabled}></input>
</td>"""
if not formsemestre.formation.is_apc()
else ""
)
+ """</tr>"""
2020-09-26 16:19:37 +02:00
)
2020-09-26 16:19:37 +02:00
descr.append(
(
f"note_{ue.id}",
2020-09-26 16:19:37 +02:00
{
"input_type": "text",
"size": 4,
"template": itemtemplate,
"title": "<tt>"
+ (f"S{ue.semestre_idx} " if ue.semestre_idx is not None else "")
+ f"<b>{ue.acronyme}</b></tt> {ue.titre}"
+ f" ({ue.ects} ECTS)"
if ue.ects is not None
else "",
"attributes": [coef_disabled],
2020-09-26 16:19:37 +02:00
},
)
)
return descr
def _check_values(formsemestre: FormSemestre, ue_list, values):
2020-09-26 16:19:37 +02:00
"""Check that form values are ok
for each UE:
code != None => note and coef
note or coef => code != None
note float in [0, 20]
note => coef
coef float >= 0
"""
for ue in ue_list:
pu = f" pour UE {ue.acronyme}"
code = values.get(f"valid_{ue.id}", False)
2020-09-26 16:19:37 +02:00
if code == "None":
code = None
note = values.get(f"note_{ue.id}", False)
2020-09-26 16:19:37 +02:00
try:
note = _convert_field_to_float(note)
except ValueError:
return False, "note invalide" + pu
if code is not False:
2020-09-26 16:19:37 +02:00
if code not in _UE_VALID_CODES:
return False, "code invalide" + pu
if code is not None:
2021-08-17 14:15:15 +02:00
if note is False or note == "":
2020-09-26 16:19:37 +02:00
return False, "note manquante" + pu
coef = values.get(f"coef_{ue.id}", False)
try:
coef = _convert_field_to_float(coef)
except ValueError:
return False, "coefficient invalide" + pu
if note is not False and note != "":
if code is None:
2020-09-26 16:19:37 +02:00
return (
False,
f"""code jury incohérent (code {code}, note {note}) {pu}
(supprimer note)""",
2020-09-26 16:19:37 +02:00
)
if note < 0 or note > 20:
return False, "valeur note invalide" + pu
if not isinstance(coef, float) and not formsemestre.formation.is_apc():
return False, f"coefficient manquant pour note {note} {pu}"
# Vérifie valeur coef seulement pour formations classiques:
if not formsemestre.formation.is_apc():
if coef is not False and coef != "":
if coef < 0:
return False, "valeur coefficient invalide" + pu
2020-09-26 16:19:37 +02:00
return True, "ok"
def _convert_field_to_float(val):
"""val may be empty, False (left unchanged), or a float. Raise exception ValueError"""
if val is not False:
2020-09-26 16:19:37 +02:00
val = val.strip()
if val:
val = float(val)
return val
def _record_ue_validations_and_coefs(
formsemestre: FormSemestre, etud: Identite, ues: list[UniteEns], values
):
"""Enregistre en base les validations
En APC, le coef est toujours NULL
2020-09-26 16:19:37 +02:00
"""
for ue in ues:
code = values.get(f"valid_{ue.id}", False)
2020-09-26 16:19:37 +02:00
if code == "None":
code = None
note = values.get(f"note_{ue.id}", False)
2020-09-26 16:19:37 +02:00
note = _convert_field_to_float(note)
coef = values.get(f"coef_{ue.id}", False)
2020-09-26 16:19:37 +02:00
coef = _convert_field_to_float(coef)
if coef == "" or coef is False:
2020-09-26 16:19:37 +02:00
coef = None
now_dmy = time.strftime("%d/%m/%Y")
log(
f"_record_ue_validations_and_coefs: {formsemestre.id} etudid={etud.id} ue_id={ue.id} moy_ue={note} ue_coef={coef}"
2020-09-26 16:19:37 +02:00
)
assert code is None or (note) # si code validant, il faut une note
2020-09-26 16:19:37 +02:00
sco_formsemestre_validation.do_formsemestre_validate_previous_ue(
formsemestre.id,
etud.id,
ue.id,
2020-09-26 16:19:37 +02:00
note,
now_dmy,
code=code,
ue_coefficient=coef,
)