ScoDoc-Lille/app/scodoc/sco_placement.py

623 lines
23 KiB
Python

# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
# Copyright (c) 1999 - 2021 Emmanuel Viennet. All rights reserved.
#
# 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
#
##############################################################################
"""ScoDoc: génération feuille émargement et placement
Contribution M. Salomon, UFC / IUT DE BELFORT-MONTBÉLIARD, 2016
"""
import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error
import random
import time
from copy import copy
import wtforms.validators
from flask import request, render_template, url_for
from flask_login import current_user
from werkzeug import Response
from flask_wtf import FlaskForm
from wtforms import (
StringField,
PasswordField,
BooleanField,
SubmitField,
SelectField,
RadioField,
HiddenField,
SelectMultipleField,
validators,
)
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo
from app.scodoc.sco_exceptions import ScoValueError
import app.scodoc.sco_utils as scu
import app.scodoc.notesdb as ndb
from app.scodoc import html_sco_header
from app.scodoc import sco_edit_module
from app.scodoc import sco_evaluations
from app.scodoc import sco_excel
from app.scodoc import sco_formsemestre
from app.scodoc import sco_formsemestre_inscriptions
from app.scodoc import sco_groups
from app.scodoc import sco_moduleimpl
from app.scodoc import sco_permissions_check
from app.scodoc import sco_preferences
from app.scodoc import sco_saisie_notes
from app.scodoc import sco_etud
import sco_version
from app.scodoc.gen_tables import GenTable
from app.scodoc.sco_excel import * # XXX à vérifier
from app.scodoc.TrivialFormulator import TrivialFormulator
_ = lambda x: x # sans babel
_l = _
COORD = "Coordonnées"
SEQ = "Continue"
class PlacementForm(FlaskForm):
TOUS = "Tous"
evaluation_id = HiddenField("evaluation_id")
file_format = RadioField(
"Format de fichier",
choices=["pdf", "xls"],
validators=[
wtforms.validators.DataRequired("indiquez le format du fichier attendu"),
],
)
surveillants = StringField("Surveillants", validators=[])
batiment = StringField("Batiment")
salle = StringField("Salle")
nb_rangs = SelectField(
"nb_rangs", coerce=int, choices=[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
)
etiquetage = RadioField(
"Numérotation",
choices=[SEQ, COORD],
validators=[
wtforms.validators.DataRequired("indiquez le style de numérotation"),
],
)
groups = SelectMultipleField(
"Groupe(s)",
validators=[
wtforms.validators.DataRequired("indiquez au moins un groupe"),
],
)
submit = SubmitField("OK")
def set_evaluation_infos(self, evaluation_id):
eval_data = sco_evaluations.do_evaluation_list({"evaluation_id": evaluation_id})
if not eval_data:
raise ScoValueError("invalid evaluation_id")
eval_data = eval_data[0]
# groupes
groups = sco_groups.do_evaluation_listegroupes(
evaluation_id, include_default=True
)
self.groups_tree = {}
self.has_groups = False
for group in groups:
partition = group["partition_name"] or self.TOUS # TODO check required
group_id = group["group_id"]
group_name = group["group_name"] or self.TOUS
if partition not in self.groups_tree:
self.groups_tree[partition] = {}
self.groups_tree[partition][group_name] = group_id
if partition != self.TOUS:
self.has_groups = True
self.groups_tree_length = len(self.groups_tree)
if self.has_groups:
choices = []
for partition in self.groups_tree:
for groupe in self.groups_tree[partition]:
id = str(self.groups_tree[partition][groupe])
choices.append((id, "%s (%s)" % (str(groupe), partition)))
self.groups.choices = choices
class _DistributeurContinu:
"""Distribue les places selon un ordre numérique."""
def __init(self):
self.position = 1
def suivant(self):
retour = self.position
self.position += 1
return retour
class _Distributeur2D:
"""Distribue les places selon des coordonnées sur nb_rangs."""
def __init__(self, nb_rangs):
self.nb_rangs = nb_rangs
self.rang = 1
self.index = 1
def suivant(self):
retour = (self.index, self.rang)
self.rang += 1
if self.rang > self.nb_rangs:
self.rang = 1
self.index += 1
return retour
def placement_eval_selectetuds(evaluation_id):
"""Creation de l'écran de placement"""
form = PlacementForm(
request.form,
data={"evaluation_id": int(evaluation_id), "groups": PlacementForm.TOUS},
)
form.set_evaluation_infos(evaluation_id)
if form.validate_on_submit():
runner = PlacementRunner(form)
if not runner.check_placement():
return (
"""<h2>Génération du placement impossible pour %s</h2>
<p>(vérifiez que le semestre n'est pas verrouillé et que vous
avez l'autorisation d'effectuer cette opération)</p>
<p><a href="moduleimpl_status?moduleimpl_id=%s">Continuer</a></p>
"""
% runner.__dict__
)
return runner._exec_placement() # calcul et generation du fichier
# return flask.redirect(url_for("scodoc.index"))
H = [html_sco_header.sco_header(init_jquery_ui=True)]
H.append(sco_evaluations.evaluation_describe(evaluation_id=evaluation_id))
H.append("<h3>Placement et émargement des étudiants</h3>")
H.append(render_template("forms/placement.html", form=form))
F = html_sco_header.sco_footer()
return "\n".join(H) + "<p>" + F
class PlacementRunner:
def __init__(self, form):
"""Calcul et génération du fichier sur la base des données du formulaire"""
self.evaluation_id = form["evaluation_id"].data
self.etiquetage = form["etiquetage"].data
self.surveillants = form["surveillants"].data
self.batiment = form["batiment"].data
self.salle = form["salle"].data
self.nb_rangs = form["nb_rangs"].data
self.file_format = form["file_format"].data
self.groups_ids = form["groups"].data
self.eval_data = sco_evaluations.do_evaluation_list(
{"evaluation_id": self.evaluation_id}
)[0]
self.cnx = ndb.GetDBConnexion()
self.groups = sco_groups.listgroups(self.groups_ids)
self.gr_title_filename = sco_groups.listgroups_filename(self.groups)
# gr_title = sco_groups.listgroups_abbrev(d['groups'])
self.current_user = current_user
self.moduleimpl_id = self.eval_data["moduleimpl_id"]
self.moduleimpl_data = sco_moduleimpl.do_moduleimpl_list(
moduleimpl_id=self.moduleimpl_id
)[0]
self.Mod = sco_edit_module.do_module_list(
args={"module_id": self.moduleimpl_data["module_id"]}
)[0]
self.sem = sco_formsemestre.get_formsemestre(
self.moduleimpl_data["formsemestre_id"]
)
self.evalname = "%s-%s" % (
self.Mod["code"],
ndb.DateDMYtoISO(self.eval_data["jour"]),
)
if self.eval_data["description"]:
self.evaltitre = self.eval_data["description"]
else:
self.evaltitre = "évaluation du %s" % self.eval_data["jour"]
self.desceval = [
["%s" % self.sem["titreannee"]],
["Module : %s - %s" % (self.Mod["code"], self.Mod["abbrev"])],
["Surveillants : %s" % self.surveillants],
["Batiment : %(batiment)s - Salle : %(salle)s" % self.__dict__],
[
"Controle : %s (coef. %g)"
% (self.evaltitre, self.eval_data["coefficient"])
],
] # une liste de liste de chaines: description de l'evaluation
def check_placement(self):
# Check access (admin, respformation, and responsable_id)
return sco_permissions_check.can_edit_notes(
self.current_user, self.moduleimpl_id
)
def _exec_placement(self):
self._repartition()
if self.file_format == "xls":
return self._production_xls()
else:
return self._production_pdf()
def _repartition(self):
"""
Calcule le placement. retourne une liste de couples ((nom, prenom), position)
"""
# Construit liste des etudiants et les réparti
self.groups = sco_groups.listgroups(self.groups_ids)
self.listetud = self._build_listetud()
self.plan = self._affectation_places()
def _build_listetud(self):
if None in [g["group_name"] for g in self.groups]: # tous les etudiants
getallstudents = True
gr_title_filename = "tous"
else:
getallstudents = False
etudids = sco_groups.do_evaluation_listeetuds_groups(
self.evaluation_id,
self.groups,
getallstudents=getallstudents,
include_dems=True,
)
listetud = [] # liste de couples (nom,prenom)
for etudid in etudids:
# infos identite etudiant (xxx sous-optimal: 1/select par etudiant)
ident = sco_etud.etudident_list(self.cnx, {"etudid": etudid})[0]
# infos inscription
inscr = sco_formsemestre_inscriptions.do_formsemestre_inscription_list(
{
"etudid": etudid,
"formsemestre_id": self.moduleimpl_data["formsemestre_id"],
}
)[0]
if inscr["etat"] != "D":
nom = ident["nom"].upper()
prenom = ident["prenom"].lower().capitalize()
etudid = ident["etudid"]
listetud.append((nom, prenom, etudid))
random.shuffle(listetud)
return listetud
def _affectation_places(self):
plan = []
if self.etiquetage == SEQ:
distributeur = _DistributeurContinu()
else:
distributeur = _Distributeur2D(self.nb_rangs)
for etud in self.listetud:
plan.append((etud, distributeur.suivant()))
return plan
def _production_xls(self):
filename = scu.make_filename(
"placement_%s_%s%s"
% (self.evalname, self.gr_title_filename, scu.XLSX_SUFFIX)
)
xls = self._excel_feuille_placement()
return sco_excel.send_from_flask(xls, filename)
def _production_pdf(self):
pdf_title = self.desceval
pdf_title += (
"Date : %(jour)s - Horaire : %(heure_debut)s à %(heure_fin)s"
% self.eval_data
)
filename = "placement_%(evalname)s_%(gr_title_filename)s.pdf" % self
titles = {
"nom": "Nom",
"prenom": "Prenom",
"colonne": "Colonne",
"ligne": "Ligne",
"place": "Place",
}
nb_rangs = int(self.nb_rangs)
if self.etiquetage == COORD:
columns_ids = ["nom", "prenom", "colonne", "ligne"]
else:
columns_ids = ["nom", "prenom", "place"]
rows = []
for etud in sorted(plan, key=lambda etud: etud[0][0]): # sort by name
if self.etiquetage == COORD:
rows.append(
{
"nom": etud[0][0],
"prenom": etud[0][1],
"colonne": etud[1][0],
"ligne": etud[1][1],
}
)
else:
rows.append({"nom": etud[0][0], "prenom": etud[0][1], "place": etud[1]})
tab = GenTable(
titles=titles,
columns_ids=columns_ids,
rows=rows,
filename=filename,
origin="Généré par %s le " % sco_version.SCONAME
+ scu.timedate_human_repr()
+ "",
pdf_title=pdf_title,
# pdf_shorttitle = '',
preferences=sco_preferences.SemPreferences(M["formsemestre_id"]),
# html_generate_cells=False # la derniere ligne (moyennes) est incomplete
)
t = tab.make_page(format="pdf", with_html_headers=False, REQUEST=REQUEST)
return t
def _one_header(self, ws):
cells = [
ws.make_cell("Nom", self.styles["2bi"]),
ws.make_cell("Prénom", self.styles["2bi"]),
]
if self.etiquetage == COORD:
cells.append(ws.make_cell("Colonne", self.styles["2bi"]))
cells.append(ws.make_cell("Ligne", self.styles["2bi"]))
else:
cells.append(ws.make_cell("Place", self.styles["2bi"]))
return cells
def _headers(self, ws, nb_listes):
cells = []
for _ in range(nb_listes):
cells += self._one_header(ws)
cells.append(ws.make_cell(""))
ws.append_row(cells)
def _make_styles(self, ws0, ws1):
# polices
font0 = Font(name="Calibri", bold=True, size=12)
font1b = copy(font0)
font1b.size = 9
font1i = Font(name="Arial", italic=True, size=10)
font1o = Font(name="Arial", outline=True, size=10)
font2bi = Font(name="Arial", bold=True, italic=True, size=8)
font2 = Font(name="Arial", size=10)
# bordures
side_double = Side(border_style="double", color=COLORS.BLACK.value)
side_thin = Side(border_style="thin", color=COLORS.BLACK.value)
# bordures
border1t = Border(left=side_double, top=side_double, right=side_double)
border1bb = Border(left=side_double, bottom=side_double, right=side_double)
border1bm = Border(left=side_double, right=side_double)
border1m = Border(left=side_double, bottom=side_thin, right=side_double)
border2m = Border(top=side_thin, bottom=side_thin)
border2r = Border(top=side_thin, bottom=side_thin, right=side_thin)
border2l = Border(left=side_thin, top=side_thin, bottom=side_thin)
border2b = Border(
left=side_thin, top=side_thin, bottom=side_thin, right=side_thin
)
# alignements
align_center_center = Alignment(horizontal="center", vertical="center")
align_right_bottom = Alignment(horizontal="right", vertical="bottom")
align_left_center = Alignment(horizontal="left", vertical="center")
align_right_center = Alignment(horizontal="right", vertical="center")
# patterns
pattern = PatternFill(
fill_type="solid", fgColor=sco_excel.COLORS.LIGHT_YELLOW.value
)
# styles
self.styles = {
"titres": sco_excel.excel_make_style(font_name="Arial", bold=True, size=12),
"1t": ws0.excel_make_composite_style(
font=font0, alignment=align_center_center, border=border1t
),
"1m": ws0.excel_make_composite_style(
font=font1b, alignment=align_center_center, border=border1m
),
"1bm": ws0.excel_make_composite_style(
font=font1b, alignment=align_center_center, border=border1bm
),
"1bb": ws0.excel_make_composite_style(
font=font1o, alignment=align_right_bottom, border=border1bb
),
"2b": ws1.excel_make_composite_style(
font=font1i, alignment=align_center_center, border=border2b
),
"2bi": ws1.excel_make_composite_style(
font=font2bi,
alignment=align_center_center,
border=border2b,
fill=pattern,
),
"2l": ws1.excel_make_composite_style(
font=font2, alignment=align_left_center, border=border2l
),
"2m1": ws1.excel_make_composite_style(
font=font2, alignment=align_left_center, border=border2m
),
"2m2": ws1.excel_make_composite_style(
font=font2, alignment=align_right_center, border=border2m
),
"2r": ws1.excel_make_composite_style(
font=font2, alignment=align_right_center, border=border2r
),
}
def _init_lines(self, maxlines):
return [
[] for _ in range(maxlines)
] # lines[no_ligne] -> liste des cellules de la ligne (no_lignes de 1..maxlines
def _write_lines(self, ws, lines):
for line in lines:
ws.append_row(line)
def _titres(self, ws):
dt = time.strftime("%d/%m/%Y a %Hh%M")
ws.append_single_cell_row(
"Feuille placement etudiants éditée le %s" % dt, self.styles["titres"]
)
for line, desceval in enumerate(self.desceval):
if line in [1, 4, 7]:
ws.append_blank_row()
ws.append_single_cell_row(desceval[0], self.styles["titres"])
ws.append_single_cell_row(
"Date : %(jour)s - Horaire : %(heure_debut)s à %(heure_fin)s"
% self.eval_data,
self.styles["titres"],
)
def _feuille0(self, ws0, space):
self._titres(ws0)
# entetes colonnes - feuille0
cells = [ws0.make_cell()]
for col in range(self.nb_rangs):
cells.append(ws0.make_cell("colonne %s" % (col + 1), self.styles["2b"]))
ws0.append_row(cells)
# etudiants - feuille0
place = 1
for rang, linetud in enumerate(self.plan, start=1):
# Chaque rang est affiché sur 3 lignes xlsx (notées A, B, C)
# ligne A: le nom, ligne B: le prénom, ligne C: un espace ou la place
cells_a = [ws0.make_cell(rang, self.styles["2b"])]
cells_b = [ws0.make_cell("", self.styles["2b"])]
cells_c = [ws0.make_cell("", self.styles["2b"])]
row = 14 # premieère ligne de signature
for etudid in linetud:
cells_a.append(ws0.make_cell(etudid[0], self.styles["1t"]))
cells_b.append(ws0.make_cell(etudid[1], self.styles["1m"]))
if self.etiquetage == COORD:
cell_c = ws0.make_cell("", self.styles["1bb"])
else:
cell_c = ws0.make_cell("place %s" % place, self.styles["1bb"])
cells_c.append(cell_c)
ws0.set_row_dimension_height(row, space / 25)
row += 3
place = place + 1
if col == self.nb_rangs:
ws0.append_row(cells_a)
ws0.append_row(cells_b)
ws0.append_row(cells_c)
cells_a = [ws0.make_cell(rang, self.styles["2b"])]
cells_b = [ws0.make_cell("", self.styles["2b"])]
cells_c = [ws0.make_cell("", self.styles["2b"])]
# publication du rang final incomplet
ws0.append_row(cells_a)
ws0.append_row(cells_b)
ws0.append_row(cells_c)
ws0.set_row_dimension_height(row, space / 25)
def _next_page(ws):
pass
def _feuille1(self, ws, maxlines):
# etudiants - feuille1
# structuration:
# 1 page = maxlistes listes
# 1 liste = 3 ou 4 colonnes(excel) (selon numbering) et (maximum maxlines) lignes
maxlistes = 2 # nombre de listes par page
# computes excel columns widths
if self.etiquetage == COORD:
gabarit = [16, 18, 6, 6, 2]
else:
gabarit = [16, 18, 12, 2]
widths = []
for _ in range(maxlistes):
widths += gabarit
ws.set_column_dimension_width(value=widths)
nb_etu_restant = len(self.listetud)
self._titres(ws)
nb_listes = min(
maxlistes, nb_etu_restant // maxlines + 1
) # nombre de colonnes dans la page
self._headers(ws, nb_listes)
# construction liste alphabétique
# Affichage
lines = self._init_lines(maxlines)
line = 0
col = 0
for etud in sorted(self.plan, key=lambda etud: etud[0][0]):
# check for skip of list or page
if col > 0: # add a empty cell between lists
lines[line].append(ws.make_cell())
lines[line].append(ws.make_cell(etud[0][0], self.styles["2l"]))
lines[line].append(ws.make_cell(etud[0][1], self.styles["2m1"]))
if self.etiquetage == COORD:
lines[line].append(ws.make_cell(etud[1][0], self.styles["2m2"]))
lines[line].append(ws.make_cell(etud[1][1], self.styles["2r"]))
else:
lines[line].append(ws.make_cell(etud[1], self.styles["2r"]))
line = line + 1
if line >= maxlines: # fin de liste
col = col + 1
line = 0
if col >= maxlistes: # fin de page
self._write_lines(ws, lines)
lines = self._init_lines(maxlines)
col = 0
ws.append_blank_row()
nb_etu_restant -= maxlistes * maxlines
nb_listes = min(
maxlistes, nb_etu_restant // maxlines + 1
) # nombre de colonnes dans la page
self._headers(ws, nb_listes)
self._write_lines(ws, lines)
def _excel_feuille_placement(self):
"""Genere feuille excel pour placement des etudiants.
E: evaluation (dict)
lines: liste de tuples
(etudid, nom, prenom, etat, groupe, val, explanation)
"""
breakpoint()
sem_preferences = sco_preferences.SemPreferences()
space = sem_preferences.get("feuille_placement_emargement")
maxlines = sem_preferences.get("feuille_placement_positions")
nb_rangs = int(self.nb_rangs)
column_width_ratio = (
1 / 250
) # changement d unités entre pyExcelerator et openpyxl
wb = ScoExcelBook()
SheetName0 = "Emargement"
ws0 = wb.create_sheet(SheetName0)
# ajuste largeurs colonnes (unite inconnue, empirique)
width = 4500 * column_width_ratio
if nb_rangs > 5:
width = 22500 * column_width_ratio // nb_rangs
ws0.set_column_dimension_width("A", 750 * column_width_ratio)
for col in range(nb_rangs):
ws0.set_column_dimension_width(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"[col + 1 : col + 2], width
)
SheetName1 = "Positions"
ws1 = wb.create_sheet(SheetName1)
self._make_styles(ws0, ws1)
self._feuille0(ws0, space)
self._feuille1(ws1, maxlines)
return wb.generate()