2021-11-13 08:25:51 +01:00
|
|
|
# -*- coding: UTF-8 -*
|
|
|
|
|
|
|
|
"""ScoDoc models: evaluations
|
|
|
|
"""
|
2022-02-10 21:55:06 +01:00
|
|
|
import datetime
|
2023-04-03 17:46:31 +02:00
|
|
|
from operator import attrgetter
|
2021-11-13 08:25:51 +01:00
|
|
|
|
|
|
|
from app import db
|
2022-10-05 23:48:54 +02:00
|
|
|
from app.models.etudiants import Identite
|
2022-02-10 21:55:06 +01:00
|
|
|
from app.models.moduleimpls import ModuleImpl
|
2022-10-05 23:48:54 +02:00
|
|
|
from app.models.notes import NotesNotes
|
2022-02-10 21:55:06 +01:00
|
|
|
from app.models.ues import UniteEns
|
2021-11-13 08:25:51 +01:00
|
|
|
|
2022-02-10 21:55:06 +01:00
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
2021-11-13 08:25:51 +01:00
|
|
|
import app.scodoc.notesdb as ndb
|
|
|
|
|
2022-12-15 17:09:35 +01:00
|
|
|
DEFAULT_EVALUATION_TIME = datetime.time(8, 0)
|
|
|
|
|
2021-11-13 08:25:51 +01:00
|
|
|
|
|
|
|
class Evaluation(db.Model):
|
|
|
|
"""Evaluation (contrôle, examen, ...)"""
|
|
|
|
|
|
|
|
__tablename__ = "notes_evaluation"
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
evaluation_id = db.synonym("id")
|
|
|
|
moduleimpl_id = db.Column(
|
|
|
|
db.Integer, db.ForeignKey("notes_moduleimpl.id"), index=True
|
|
|
|
)
|
|
|
|
jour = db.Column(db.Date)
|
|
|
|
heure_debut = db.Column(db.Time)
|
|
|
|
heure_fin = db.Column(db.Time)
|
|
|
|
description = db.Column(db.Text)
|
|
|
|
note_max = db.Column(db.Float)
|
|
|
|
coefficient = db.Column(db.Float)
|
|
|
|
visibulletin = db.Column(
|
|
|
|
db.Boolean, nullable=False, default=True, server_default="true"
|
|
|
|
)
|
|
|
|
publish_incomplete = db.Column(
|
|
|
|
db.Boolean, nullable=False, default=False, server_default="false"
|
|
|
|
)
|
|
|
|
# type d'evaluation: 0 normale, 1 rattrapage, 2 "2eme session"
|
|
|
|
evaluation_type = db.Column(
|
|
|
|
db.Integer, nullable=False, default=0, server_default="0"
|
|
|
|
)
|
|
|
|
# ordre de presentation (par défaut, le plus petit numero
|
|
|
|
# est la plus ancienne eval):
|
2023-04-03 17:46:31 +02:00
|
|
|
numero = db.Column(db.Integer, nullable=False, default=0)
|
2021-11-13 08:25:51 +01:00
|
|
|
ues = db.relationship("UniteEns", secondary="evaluation_ue_poids", viewonly=True)
|
|
|
|
|
2021-11-20 16:35:09 +01:00
|
|
|
def __repr__(self):
|
2022-10-01 18:55:32 +02:00
|
|
|
return f"""<Evaluation {self.id} {
|
|
|
|
self.jour.isoformat() if self.jour else ''} "{
|
|
|
|
self.description[:16] if self.description else ''}">"""
|
2021-11-20 16:35:09 +01:00
|
|
|
|
2022-05-19 04:15:26 +02:00
|
|
|
def to_dict(self) -> dict:
|
2022-11-01 11:19:28 +01:00
|
|
|
"Représentation dict (riche, compat ScoDoc 7)"
|
2021-11-13 08:25:51 +01:00
|
|
|
e = dict(self.__dict__)
|
|
|
|
e.pop("_sa_instance_state", None)
|
|
|
|
# ScoDoc7 output_formators
|
2021-11-29 22:18:37 +01:00
|
|
|
e["evaluation_id"] = self.id
|
2022-05-19 04:15:26 +02:00
|
|
|
e["jour"] = e["jour"].strftime("%d/%m/%Y") if e["jour"] else ""
|
|
|
|
if self.jour is None:
|
|
|
|
e["date_debut"] = None
|
|
|
|
e["date_fin"] = None
|
|
|
|
else:
|
|
|
|
e["date_debut"] = datetime.datetime.combine(
|
|
|
|
self.jour, self.heure_debut or datetime.time(0, 0)
|
|
|
|
).isoformat()
|
|
|
|
e["date_fin"] = datetime.datetime.combine(
|
|
|
|
self.jour, self.heure_fin or datetime.time(0, 0)
|
|
|
|
).isoformat()
|
2021-11-13 08:25:51 +01:00
|
|
|
e["numero"] = ndb.int_null_is_zero(e["numero"])
|
2022-05-11 04:14:42 +02:00
|
|
|
e["poids"] = self.get_ue_poids_dict() # { ue_id : poids }
|
2022-02-10 21:55:06 +01:00
|
|
|
return evaluation_enrich_dict(e)
|
2021-11-13 08:25:51 +01:00
|
|
|
|
2022-11-01 11:19:28 +01:00
|
|
|
def to_dict_api(self) -> dict:
|
|
|
|
"Représentation dict pour API JSON"
|
|
|
|
if self.jour is None:
|
|
|
|
date_debut = None
|
|
|
|
date_fin = None
|
|
|
|
else:
|
|
|
|
date_debut = datetime.datetime.combine(
|
|
|
|
self.jour, self.heure_debut or datetime.time(0, 0)
|
|
|
|
).isoformat()
|
|
|
|
date_fin = datetime.datetime.combine(
|
|
|
|
self.jour, self.heure_fin or datetime.time(0, 0)
|
|
|
|
).isoformat()
|
|
|
|
|
|
|
|
return {
|
|
|
|
"coefficient": self.coefficient,
|
|
|
|
"date_debut": date_debut,
|
|
|
|
"date_fin": date_fin,
|
|
|
|
"description": self.description,
|
|
|
|
"evaluation_type": self.evaluation_type,
|
|
|
|
"id": self.id,
|
|
|
|
"moduleimpl_id": self.moduleimpl_id,
|
|
|
|
"note_max": self.note_max,
|
|
|
|
"numero": self.numero,
|
|
|
|
"poids": self.get_ue_poids_dict(),
|
|
|
|
"publish_incomplete": self.publish_incomplete,
|
|
|
|
"visi_bulletin": self.visibulletin,
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:54:24 +01:00
|
|
|
def from_dict(self, data):
|
|
|
|
"""Set evaluation attributes from given dict values."""
|
2022-02-10 21:55:06 +01:00
|
|
|
check_evaluation_args(data)
|
2021-12-16 22:54:24 +01:00
|
|
|
for k in self.__dict__.keys():
|
|
|
|
if k != "_sa_instance_state" and k != "id" and k in data:
|
|
|
|
setattr(self, k, data[k])
|
|
|
|
|
2022-10-01 18:55:32 +02:00
|
|
|
def descr_heure(self) -> str:
|
|
|
|
"Description de la plage horaire pour affichages"
|
|
|
|
if self.heure_debut and (
|
|
|
|
not self.heure_fin or self.heure_fin == self.heure_debut
|
|
|
|
):
|
2022-12-15 17:09:35 +01:00
|
|
|
return f"""à {self.heure_debut.strftime("%Hh%M")}"""
|
2022-10-01 18:55:32 +02:00
|
|
|
elif self.heure_debut and self.heure_fin:
|
2022-12-15 17:09:35 +01:00
|
|
|
return f"""de {self.heure_debut.strftime("%Hh%M")} à {self.heure_fin.strftime("%Hh%M")}"""
|
2022-10-01 18:55:32 +02:00
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2022-12-15 17:09:35 +01:00
|
|
|
def descr_duree(self) -> str:
|
|
|
|
"Description de la durée pour affichages"
|
|
|
|
if self.heure_debut is None and self.heure_fin is None:
|
|
|
|
return ""
|
|
|
|
debut = self.heure_debut or DEFAULT_EVALUATION_TIME
|
|
|
|
fin = self.heure_fin or DEFAULT_EVALUATION_TIME
|
|
|
|
d = (fin.hour * 60 + fin.minute) - (debut.hour * 60 + debut.minute)
|
|
|
|
duree = f"{d//60}h"
|
|
|
|
if d % 60:
|
|
|
|
duree += f"{d%60:02d}"
|
|
|
|
return duree
|
|
|
|
|
2021-12-16 22:54:24 +01:00
|
|
|
def clone(self, not_copying=()):
|
|
|
|
"""Clone, not copying the given attrs
|
|
|
|
Attention: la copie n'a pas d'id avant le prochain commit
|
|
|
|
"""
|
|
|
|
d = dict(self.__dict__)
|
|
|
|
d.pop("id") # get rid of id
|
|
|
|
d.pop("_sa_instance_state") # get rid of SQLAlchemy special attr
|
|
|
|
for k in not_copying:
|
|
|
|
d.pop(k)
|
|
|
|
copy = self.__class__(**d)
|
|
|
|
db.session.add(copy)
|
|
|
|
return copy
|
2021-11-13 08:25:51 +01:00
|
|
|
|
2023-05-29 16:04:41 +02:00
|
|
|
def is_matin(self) -> bool:
|
|
|
|
"Evaluation ayant lieu le matin (faux si pas de date)"
|
|
|
|
heure_debut_dt = self.heure_debut or datetime.time(8, 00)
|
|
|
|
# 8:00 au cas ou pas d'heure (note externe?)
|
|
|
|
return bool(self.jour) and heure_debut_dt < datetime.time(12, 00)
|
|
|
|
|
|
|
|
def is_apresmidi(self) -> bool:
|
|
|
|
"Evaluation ayant lieu l'après midi (faux si pas de date)"
|
|
|
|
heure_debut_dt = self.heure_debut or datetime.time(8, 00)
|
|
|
|
# 8:00 au cas ou pas d'heure (note externe?)
|
|
|
|
return bool(self.jour) and heure_debut_dt >= datetime.time(12, 00)
|
|
|
|
|
2022-10-05 10:31:25 +02:00
|
|
|
def set_default_poids(self) -> bool:
|
|
|
|
"""Initialize les poids bvers les UE à leurs valeurs par défaut
|
|
|
|
C'est à dire à 1 si le coef. module/UE est non nul, 0 sinon.
|
|
|
|
Les poids existants ne sont pas modifiés.
|
|
|
|
Return True if (uncommited) modification, False otherwise.
|
|
|
|
"""
|
|
|
|
ue_coef_dict = self.moduleimpl.module.get_ue_coef_dict()
|
2023-04-03 17:46:31 +02:00
|
|
|
sem_ues = self.moduleimpl.formsemestre.get_ues(with_sport=False)
|
2022-10-05 10:31:25 +02:00
|
|
|
modified = False
|
|
|
|
for ue in sem_ues:
|
|
|
|
existing_poids = EvaluationUEPoids.query.filter_by(
|
|
|
|
ue=ue, evaluation=self
|
|
|
|
).first()
|
|
|
|
if existing_poids is None:
|
|
|
|
coef_ue = ue_coef_dict.get(ue.id, 0.0) or 0.0
|
|
|
|
if coef_ue > 0:
|
|
|
|
poids = 1.0 # par défaut au départ
|
|
|
|
else:
|
|
|
|
poids = 0.0
|
|
|
|
self.set_ue_poids(ue, poids)
|
|
|
|
modified = True
|
|
|
|
return modified
|
|
|
|
|
2021-12-19 11:08:03 +01:00
|
|
|
def set_ue_poids(self, ue, poids: float) -> None:
|
2021-11-13 08:25:51 +01:00
|
|
|
"""Set poids évaluation vers cette UE"""
|
|
|
|
self.update_ue_poids_dict({ue.id: poids})
|
|
|
|
|
2021-12-19 11:08:03 +01:00
|
|
|
def set_ue_poids_dict(self, ue_poids_dict: dict) -> None:
|
2021-11-13 08:25:51 +01:00
|
|
|
"""set poids vers les UE (remplace existants)
|
|
|
|
ue_poids_dict = { ue_id : poids }
|
|
|
|
"""
|
|
|
|
L = []
|
|
|
|
for ue_id, poids in ue_poids_dict.items():
|
2023-07-11 06:57:38 +02:00
|
|
|
ue = db.session.get(UniteEns, ue_id)
|
|
|
|
ue_poids = EvaluationUEPoids(evaluation=self, ue=ue, poids=poids)
|
|
|
|
L.append(ue_poids)
|
|
|
|
db.session.add(ue_poids)
|
2022-10-03 09:04:04 +02:00
|
|
|
self.ue_poids = L # backref # pylint:disable=attribute-defined-outside-init
|
2021-11-29 22:18:37 +01:00
|
|
|
self.moduleimpl.invalidate_evaluations_poids() # inval cache
|
2021-11-13 08:25:51 +01:00
|
|
|
|
2021-12-19 11:08:03 +01:00
|
|
|
def update_ue_poids_dict(self, ue_poids_dict: dict) -> None:
|
2021-11-13 08:25:51 +01:00
|
|
|
"""update poids vers UE (ajoute aux existants)"""
|
|
|
|
current = self.get_ue_poids_dict()
|
|
|
|
current.update(ue_poids_dict)
|
|
|
|
self.set_ue_poids_dict(current)
|
|
|
|
|
2022-10-03 09:04:04 +02:00
|
|
|
def get_ue_poids_dict(self, sort=False) -> dict:
|
|
|
|
"""returns { ue_id : poids }
|
|
|
|
Si sort, trie par UE
|
|
|
|
"""
|
|
|
|
if sort:
|
|
|
|
return {
|
|
|
|
p.ue.id: p.poids
|
|
|
|
for p in sorted(
|
2023-04-12 12:47:43 +02:00
|
|
|
self.ue_poids, key=attrgetter("ue.numero", "ue.acronyme")
|
2022-10-03 09:04:04 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-13 08:25:51 +01:00
|
|
|
return {p.ue.id: p.poids for p in self.ue_poids}
|
|
|
|
|
2021-12-19 11:08:03 +01:00
|
|
|
def get_ue_poids_str(self) -> str:
|
|
|
|
"""string describing poids, for excel cells and pdfs
|
|
|
|
Note: si les poids ne sont pas initialisés (poids par défaut),
|
|
|
|
ils ne sont pas affichés.
|
|
|
|
"""
|
2022-01-20 13:00:25 +01:00
|
|
|
# restreint aux UE du semestre dans lequel est cette évaluation
|
|
|
|
# au cas où le module ait changé de semestre et qu'il reste des poids
|
|
|
|
evaluation_semestre_idx = self.moduleimpl.module.semestre_id
|
|
|
|
return ", ".join(
|
|
|
|
[
|
|
|
|
f"{p.ue.acronyme}: {p.poids}"
|
2022-09-06 23:50:56 +02:00
|
|
|
for p in sorted(
|
|
|
|
self.ue_poids, key=lambda p: (p.ue.numero or 0, p.ue.acronyme)
|
|
|
|
)
|
2022-01-20 13:00:25 +01:00
|
|
|
if evaluation_semestre_idx == p.ue.semestre_idx
|
|
|
|
]
|
|
|
|
)
|
2021-12-19 11:08:03 +01:00
|
|
|
|
2022-10-05 23:48:54 +02:00
|
|
|
def get_etud_note(self, etud: Identite) -> NotesNotes:
|
|
|
|
"""La note de l'étudiant, ou None si pas noté.
|
|
|
|
(nb: pas de cache, lent, ne pas utiliser pour des calculs)
|
|
|
|
"""
|
|
|
|
return NotesNotes.query.filter_by(etudid=etud.id, evaluation_id=self.id).first()
|
|
|
|
|
2021-11-13 08:25:51 +01:00
|
|
|
|
|
|
|
class EvaluationUEPoids(db.Model):
|
|
|
|
"""Poids des évaluations (BUT)
|
|
|
|
association many to many
|
|
|
|
"""
|
|
|
|
|
|
|
|
evaluation_id = db.Column(
|
2021-12-13 23:44:13 +01:00
|
|
|
db.Integer,
|
|
|
|
db.ForeignKey("notes_evaluation.id", ondelete="CASCADE"),
|
|
|
|
primary_key=True,
|
|
|
|
)
|
|
|
|
ue_id = db.Column(
|
|
|
|
db.Integer,
|
|
|
|
db.ForeignKey("notes_ue.id", ondelete="CASCADE"),
|
|
|
|
primary_key=True,
|
2021-11-13 08:25:51 +01:00
|
|
|
)
|
|
|
|
poids = db.Column(
|
|
|
|
db.Float,
|
|
|
|
nullable=False,
|
|
|
|
)
|
|
|
|
evaluation = db.relationship(
|
|
|
|
Evaluation,
|
|
|
|
backref=db.backref("ue_poids", cascade="all, delete-orphan"),
|
|
|
|
)
|
|
|
|
ue = db.relationship(
|
|
|
|
UniteEns,
|
|
|
|
backref=db.backref("evaluation_ue_poids", cascade="all, delete-orphan"),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"<EvaluationUEPoids {self.evaluation} {self.ue} poids={self.poids}>"
|
2022-02-10 21:55:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Fonction héritée de ScoDoc7 à refactorer
|
2022-10-01 18:55:32 +02:00
|
|
|
def evaluation_enrich_dict(e: dict):
|
2022-05-11 04:14:42 +02:00
|
|
|
"""add or convert some fields in an evaluation dict"""
|
2022-02-10 21:55:06 +01:00
|
|
|
# For ScoDoc7 compat
|
|
|
|
heure_debut_dt = e["heure_debut"] or datetime.time(
|
|
|
|
8, 00
|
|
|
|
) # au cas ou pas d'heure (note externe?)
|
|
|
|
heure_fin_dt = e["heure_fin"] or datetime.time(8, 00)
|
|
|
|
e["heure_debut"] = ndb.TimefromISO8601(e["heure_debut"])
|
|
|
|
e["heure_fin"] = ndb.TimefromISO8601(e["heure_fin"])
|
2022-11-01 11:19:28 +01:00
|
|
|
e["jour_iso"] = ndb.DateDMYtoISO(e["jour"])
|
2022-02-10 21:55:06 +01:00
|
|
|
heure_debut, heure_fin = e["heure_debut"], e["heure_fin"]
|
|
|
|
d = ndb.TimeDuration(heure_debut, heure_fin)
|
|
|
|
if d is not None:
|
|
|
|
m = d % 60
|
|
|
|
e["duree"] = "%dh" % (d / 60)
|
|
|
|
if m != 0:
|
|
|
|
e["duree"] += "%02d" % m
|
|
|
|
else:
|
|
|
|
e["duree"] = ""
|
|
|
|
if heure_debut and (not heure_fin or heure_fin == heure_debut):
|
|
|
|
e["descrheure"] = " à " + heure_debut
|
|
|
|
elif heure_debut and heure_fin:
|
|
|
|
e["descrheure"] = " de %s à %s" % (heure_debut, heure_fin)
|
|
|
|
else:
|
|
|
|
e["descrheure"] = ""
|
|
|
|
# matin, apresmidi: utile pour se referer aux absences:
|
2022-05-06 01:15:37 +02:00
|
|
|
|
|
|
|
if e["jour"] and heure_debut_dt < datetime.time(12, 00):
|
2022-02-10 21:55:06 +01:00
|
|
|
e["matin"] = 1
|
|
|
|
else:
|
|
|
|
e["matin"] = 0
|
2022-05-06 01:15:37 +02:00
|
|
|
if e["jour"] and heure_fin_dt > datetime.time(12, 00):
|
2022-02-10 21:55:06 +01:00
|
|
|
e["apresmidi"] = 1
|
|
|
|
else:
|
|
|
|
e["apresmidi"] = 0
|
|
|
|
return e
|
|
|
|
|
|
|
|
|
|
|
|
def check_evaluation_args(args):
|
|
|
|
"Check coefficient, dates and duration, raises exception if invalid"
|
|
|
|
moduleimpl_id = args["moduleimpl_id"]
|
|
|
|
# check bareme
|
|
|
|
note_max = args.get("note_max", None)
|
|
|
|
if note_max is None:
|
|
|
|
raise ScoValueError("missing note_max")
|
|
|
|
try:
|
|
|
|
note_max = float(note_max)
|
|
|
|
except ValueError:
|
|
|
|
raise ScoValueError("Invalid note_max value")
|
|
|
|
if note_max < 0:
|
|
|
|
raise ScoValueError("Invalid note_max value (must be positive or null)")
|
|
|
|
# check coefficient
|
|
|
|
coef = args.get("coefficient", None)
|
|
|
|
if coef is None:
|
|
|
|
raise ScoValueError("missing coefficient")
|
|
|
|
try:
|
|
|
|
coef = float(coef)
|
|
|
|
except ValueError:
|
|
|
|
raise ScoValueError("Invalid coefficient value")
|
|
|
|
if coef < 0:
|
|
|
|
raise ScoValueError("Invalid coefficient value (must be positive or null)")
|
|
|
|
# check date
|
|
|
|
jour = args.get("jour", None)
|
|
|
|
args["jour"] = jour
|
|
|
|
if jour:
|
2023-07-11 06:57:38 +02:00
|
|
|
modimpl = db.session.get(ModuleImpl, moduleimpl_id)
|
2022-02-10 21:55:06 +01:00
|
|
|
formsemestre = modimpl.formsemestre
|
|
|
|
y, m, d = [int(x) for x in ndb.DateDMYtoISO(jour).split("-")]
|
|
|
|
jour = datetime.date(y, m, d)
|
|
|
|
if (jour > formsemestre.date_fin) or (jour < formsemestre.date_debut):
|
|
|
|
raise ScoValueError(
|
|
|
|
"La date de l'évaluation (%s/%s/%s) n'est pas dans le semestre !"
|
|
|
|
% (d, m, y),
|
|
|
|
dest_url="javascript:history.back();",
|
|
|
|
)
|
|
|
|
heure_debut = args.get("heure_debut", None)
|
|
|
|
args["heure_debut"] = heure_debut
|
|
|
|
heure_fin = args.get("heure_fin", None)
|
|
|
|
args["heure_fin"] = heure_fin
|
|
|
|
if jour and ((not heure_debut) or (not heure_fin)):
|
|
|
|
raise ScoValueError("Les heures doivent être précisées")
|
|
|
|
d = ndb.TimeDuration(heure_debut, heure_fin)
|
|
|
|
if d and ((d < 0) or (d > 60 * 12)):
|
|
|
|
raise ScoValueError("Heures de l'évaluation incohérentes !")
|