ScoDoc-PE/app/but/bulletin_but.py

272 lines
10 KiB
Python
Raw Normal View History

2021-12-05 20:21:51 +01:00
##############################################################################
# ScoDoc
# Copyright (c) 1999 - 2021 Emmanuel Viennet. All rights reserved.
# See LICENSE
##############################################################################
import datetime
2021-12-06 22:37:49 +01:00
from flask import url_for, g
2021-12-05 20:21:51 +01:00
import numpy as np
import pandas as pd
from app import db
from app.comp import df_cache, moy_ue, moy_mod, inscr_mod
from app.scodoc import sco_utils as scu
from app.scodoc.sco_cache import ResultatsSemestreBUTCache
from app.scodoc.sco_exceptions import ScoFormatError
2021-12-05 21:54:04 +01:00
from app.scodoc import sco_preferences
2021-12-06 10:57:10 +01:00
from app.scodoc.sco_utils import jsnan, fmt_note
2021-12-05 20:21:51 +01:00
class ResultatsSemestreBUT:
"""Structure légère pour stocker les résultats du semestre et
générer les bulletins.
__init__ : charge depuis le cache ou calcule
invalidate(): invalide données cachées
"""
_cached_attrs = (
"sem_cube",
"modimpl_inscr_df",
"modimpl_coefs_df",
"etud_moy_ue",
"modimpls_evals_poids",
"modimpls_evals_notes",
)
def __init__(self, formsemestre):
self.formsemestre = formsemestre
self.ues = formsemestre.query_ues().all()
self.modimpls = formsemestre.modimpls.all()
self.etuds = self.formsemestre.etuds.all()
self.etud_index = {e.id: idx for idx, e in enumerate(self.etuds)}
self.saes = [
m for m in self.modimpls if m.module.module_type == scu.ModuleType.SAE
]
self.ressources = [
m for m in self.modimpls if m.module.module_type == scu.ModuleType.RESSOURCE
]
if not self.load_cached():
self.compute()
self.store()
def load_cached(self) -> bool:
"Load cached dataframes, returns False si pas en cache"
data = ResultatsSemestreBUTCache.get(self.formsemestre.id)
if not data:
return False
for attr in self._cached_attrs:
setattr(self, attr, data[attr])
return True
def store(self):
"Cache our dataframes"
ResultatsSemestreBUTCache.set(
self.formsemestre.id,
{attr: getattr(self, attr) for attr in self._cached_attrs},
)
def compute(self):
"Charge les notes et inscriptions et calcule toutes les moyennes"
(
self.sem_cube,
self.modimpls_evals_poids,
self.modimpls_evals_notes,
_,
) = moy_ue.notes_sem_load_cube(self.formsemestre)
self.modimpl_inscr_df = inscr_mod.df_load_modimpl_inscr(self.formsemestre)
self.modimpl_coefs_df, _, _ = moy_ue.df_load_modimpl_coefs(
self.formsemestre, ues=self.ues, modimpls=self.modimpls
)
# l'idx de la colonne du mod modimpl.id est
# modimpl_coefs_df.columns.get_loc(modimpl.id)
# idx de l'UE: modimpl_coefs_df.index.get_loc(ue.id)
self.etud_moy_ue = moy_ue.compute_ue_moys(
self.sem_cube,
self.etuds,
self.modimpls,
self.ues,
self.modimpl_inscr_df,
self.modimpl_coefs_df,
)
def etud_ue_mod_results(self, etud, ue, modimpls) -> dict:
"dict synthèse résultats dans l'UE pour les modules indiqués"
d = {}
etud_idx = self.etud_index[etud.id]
ue_idx = self.modimpl_coefs_df.index.get_loc(ue.id)
etud_moy_module = self.sem_cube[etud_idx] # module x UE
for mi in modimpls:
2021-12-06 09:21:15 +01:00
coef = self.modimpl_coefs_df[mi.id][ue.id]
if coef > 0:
d[mi.module.code] = {
"id": mi.id,
"coef": coef,
2021-12-06 10:57:10 +01:00
"moyenne": fmt_note(
2021-12-06 09:21:15 +01:00
etud_moy_module[self.modimpl_coefs_df.columns.get_loc(mi.id)][
ue_idx
]
),
}
2021-12-05 20:21:51 +01:00
return d
def etud_ue_results(self, etud, ue):
"dict synthèse résultats UE"
d = {
"id": ue.id,
"ECTS": {
"acquis": 0, # XXX TODO voir jury
"total": ue.ects,
},
"competence": None, # XXX TODO lien avec référentiel
"moyenne": {
"value": fmt_note(self.etud_moy_ue[ue.id][etud.id]),
"min": fmt_note(self.etud_moy_ue[ue.id].min()),
"max": fmt_note(self.etud_moy_ue[ue.id].max()),
"moy": fmt_note(self.etud_moy_ue[ue.id].mean()),
},
2021-12-08 22:33:32 +01:00
"bonus": None, # XXX TODO
2021-12-05 20:21:51 +01:00
"malus": None, # XXX TODO voir ce qui est ici
"capitalise": None, # "AAAA-MM-JJ" TODO
"ressources": self.etud_ue_mod_results(etud, ue, self.ressources),
"saes": self.etud_ue_mod_results(etud, ue, self.saes),
}
return d
def etud_mods_results(self, etud, modimpls) -> dict:
"""dict synthèse résultats des modules indiqués,
avec évaluations de chacun."""
d = {}
etud_idx = self.etud_index[etud.id]
for mi in modimpls:
mod_idx = self.modimpl_coefs_df.columns.get_loc(mi.id)
# moyennes indicatives (moyennes de moyennes d'UE)
moyennes_etuds = np.nan_to_num(
self.sem_cube[:, mod_idx, :].mean(axis=1),
copy=False,
)
d[mi.module.code] = {
"id": mi.id,
"titre": mi.module.titre,
"code_apogee": mi.module.code_apogee,
2021-12-06 22:37:49 +01:00
"url": url_for(
"notes.moduleimpl_status",
scodoc_dept=g.scodoc_dept,
moduleimpl_id=mi.id,
),
2021-12-05 20:21:51 +01:00
"moyenne": {
2021-12-06 10:57:10 +01:00
"value": fmt_note(self.sem_cube[etud_idx, mod_idx].mean()),
"min": fmt_note(moyennes_etuds.min()),
"max": fmt_note(moyennes_etuds.max()),
"moy": fmt_note(moyennes_etuds.mean()),
2021-12-05 20:21:51 +01:00
},
"evaluations": [
self.etud_eval_results(etud, e)
for e in mi.evaluations
if e.visibulletin
],
}
return d
def etud_eval_results(self, etud, e) -> dict:
"dict resultats d'un étudiant à une évaluation"
eval_notes = self.modimpls_evals_notes[e.moduleimpl_id][e.id] # pd.Series
2021-12-05 20:21:51 +01:00
notes_ok = eval_notes.where(eval_notes > -1000).dropna()
d = {
"id": e.id,
"description": e.description,
"date": e.jour.isoformat() if e.jour else None,
2021-12-05 20:21:51 +01:00
"heure_debut": e.heure_debut.strftime("%H:%M") if e.heure_debut else None,
"heure_fin": e.heure_fin.strftime("%H:%M") if e.heure_debut else None,
"coef": e.coefficient,
"poids": {p.ue.acronyme: p.poids for p in e.ue_poids},
"note": {
2021-12-06 10:57:10 +01:00
"value": fmt_note(
self.modimpls_evals_notes[e.moduleimpl_id][e.id][etud.id]
2021-12-05 20:21:51 +01:00
),
2021-12-06 10:57:10 +01:00
"min": fmt_note(notes_ok.min()),
"max": fmt_note(notes_ok.max()),
"moy": fmt_note(notes_ok.mean()),
2021-12-05 20:21:51 +01:00
},
}
return d
def bulletin_etud(self, etud, formsemestre) -> dict:
"""Le bulletin de l'étudiant dans ce semestre"""
d = {
"version": "0",
"type": "BUT",
"date": datetime.datetime.utcnow().isoformat() + "Z",
"etudiant": etud.to_dict_bul(),
"formation": {
"id": formsemestre.formation.id,
"acronyme": formsemestre.formation.acronyme,
"titre_officiel": formsemestre.formation.titre_officiel,
"titre": formsemestre.formation.titre,
},
"formsemestre_id": formsemestre.id,
"ressources": self.etud_mods_results(etud, self.ressources),
"saes": self.etud_mods_results(etud, self.saes),
2021-12-05 20:21:51 +01:00
"ues": {ue.acronyme: self.etud_ue_results(etud, ue) for ue in self.ues},
"semestre": {
"notes": { # moyenne des moyennes générales du semestre
2021-12-06 10:57:10 +01:00
"value": fmt_note("xxx"), # XXX TODO
"min": fmt_note("0."),
"moy": fmt_note("10.0"),
"max": fmt_note("20.00"),
2021-12-05 20:21:51 +01:00
},
"rang": { # classement wrt moyenne général, indicatif
"value": None, # XXX TODO
"total": None,
},
"absences": { # XXX TODO
"injustifie": 1,
"total": 33,
},
2021-12-06 09:21:15 +01:00
"etapes": { # XXX TODO
# à spécifier: liste des étapes Apogée
},
2021-12-05 20:21:51 +01:00
"date_debut": formsemestre.date_debut.isoformat(),
"date_fin": formsemestre.date_fin.isoformat(),
"annee_universitaire": self.formsemestre.annee_scolaire_str(),
"inscription": "TODO-MM-JJ", # XXX TODO
"numero": formsemestre.semestre_id,
"decision": None, # XXX TODO
"situation": "Décision jury: Validé. Diplôme obtenu.", # XXX TODO
"date_jury": "AAAA-MM-JJ", # XXX TODO
"groupes": [], # XXX TODO
},
2021-12-05 21:54:04 +01:00
"options": bulletin_option_affichage(formsemestre),
2021-12-05 20:21:51 +01:00
}
return d
2021-12-05 21:54:04 +01:00
def bulletin_option_affichage(formsemestre):
"dict avec les options d'affichages (préférences) pour ce semestre"
prefs = sco_preferences.SemPreferences(formsemestre.id)
fields = (
"bul_show_abs",
"bul_show_abs_modules",
"bul_show_ects",
"bul_show_codemodules",
"bul_show_matieres",
"bul_show_all_evals",
"bul_show_rangs",
"bul_show_ue_rangs",
"bul_show_mod_rangs",
"bul_show_moypromo",
"bul_show_minmax",
"bul_show_minmax_mod",
"bul_show_minmax_eval",
"bul_show_coef",
"bul_show_ue_cap_details",
"bul_show_ue_cap_current",
"bul_show_temporary",
"bul_temporary_txt",
"bul_show_uevalid",
"bul_show_date_inscr",
)
# on enlève le "bul_" de la clé:
return {field[4:]: prefs[field] for field in fields}