2021-11-13 08:25:51 +01:00
|
|
|
# -*- coding: UTF-8 -*
|
|
|
|
|
|
|
|
"""ScoDoc models: evaluations
|
|
|
|
"""
|
2022-02-10 21:55:06 +01:00
|
|
|
import datetime
|
2021-11-13 08:25:51 +01:00
|
|
|
|
|
|
|
from app import db
|
2022-02-10 21:55:06 +01:00
|
|
|
from app.models.moduleimpls import ModuleImpl
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
numero = db.Column(db.Integer)
|
|
|
|
ues = db.relationship("UniteEns", secondary="evaluation_ue_poids", viewonly=True)
|
|
|
|
|
2021-11-20 16:35:09 +01:00
|
|
|
def __repr__(self):
|
2021-12-05 20:21:51 +01: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
|
|
|
|
2021-11-13 08:25:51 +01:00
|
|
|
def to_dict(self):
|
|
|
|
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
|
2021-11-13 08:25:51 +01:00
|
|
|
e["jour"] = ndb.DateISOtoDMY(e["jour"])
|
2022-05-16 16:00:24 +02:00
|
|
|
e["date_debut"] = datetime.datetime.combine(
|
|
|
|
self.jour, self.heure_debut
|
|
|
|
).isoformat()
|
|
|
|
e["date_fin"] = datetime.datetime.combine(self.jour, self.heure_fin).isoformat()
|
2021-11-13 08:25:51 +01:00
|
|
|
e["numero"] = ndb.int_null_is_zero(e["numero"])
|
2022-05-16 16:00:24 +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
|
|
|
|
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])
|
|
|
|
|
|
|
|
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
|
|
|
|
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():
|
|
|
|
ue = UniteEns.query.get(ue_id)
|
|
|
|
L.append(EvaluationUEPoids(evaluation=self, ue=ue, poids=poids))
|
|
|
|
self.ue_poids = L
|
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)
|
|
|
|
|
2021-12-19 11:08:03 +01:00
|
|
|
def get_ue_poids_dict(self) -> dict:
|
2021-11-13 08:25:51 +01:00
|
|
|
"""returns { ue_id : poids }"""
|
|
|
|
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}"
|
|
|
|
for p in self.ue_poids
|
|
|
|
if evaluation_semestre_idx == p.ue.semestre_idx
|
|
|
|
]
|
|
|
|
)
|
2021-12-19 11:08:03 +01:00
|
|
|
|
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
|
|
|
|
def evaluation_enrich_dict(e):
|
2022-05-16 16:00:24 +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"])
|
|
|
|
e["jouriso"] = ndb.DateDMYtoISO(e["jour"])
|
|
|
|
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-09 16:26:23 +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-09 16:26:23 +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:
|
|
|
|
modimpl = ModuleImpl.query.get(moduleimpl_id)
|
|
|
|
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 !")
|