# -*- mode: python -*- # -*- coding: utf-8 -*- ############################################################################## # # Gestion scolarite IUT # # Copyright (c) 1999 - 2024 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 # ############################################################################## """Affichage étudiants d'un ou plusieurs groupes sous forme: de liste html (table exportable), de trombinoscope (exportable en pdf) """ # Re-ecriture en 2014 (re-organisation de l'interface, modernisation du code) # Modif en 2024 (9.7/revamp, abandon des tabs bootstrap) import datetime from urllib.parse import parse_qs from flask import url_for, g, render_template, request from flask_login import current_user from app import db, log from app.models import FormSemestre, Identite, ModuleImpl, ScolarEvent import app.scodoc.sco_utils as scu from app.scodoc import sco_assiduites as scass from app.scodoc import sco_excel from app.scodoc import sco_formsemestre from app.scodoc import sco_groups from app.scodoc import sco_cursus from app.scodoc import sco_portal_apogee from app.scodoc import sco_preferences from app.scodoc import sco_etud from app.scodoc.sco_etud import etud_sort_key from app.scodoc.gen_tables import GenTable from app.scodoc.sco_exceptions import ScoValueError, ScoPermissionDenied from app.scodoc.sco_permissions import Permission # view def groups_lists( group_ids=(), fmt="html", with_codes=0, with_date_inscription=0, etat=None, with_paiement=0, with_archives=0, with_annotations=0, with_bourse=0, formsemestre_id=None, ): """Affichage des étudiants des groupes indiqués group_ids: liste de group_id fmt: csv, json, xml, xls, allxls, xlsappel, moodlecsv, pdf Options pour listes: with_paiement: si vrai, ajoute colonnes infos paiement droits et finalisation inscription (lent car interrogation portail) with_archives: ajoute colonne avec noms fichiers archivés formsemestre_id est utilisé si aucun groupe selectionné pour construire la liste des groupes. """ # version sans tabs: juste la liste des étudiants # Informations sur les groupes à afficher: groups_infos = DisplayedGroupsInfos( group_ids, formsemestre_id=formsemestre_id, etat=etat, select_all_when_unspecified=True, ) # Formats spéciaux: download direct if fmt != "html": return groups_table( groups_infos=groups_infos, fmt=fmt, with_codes=with_codes, with_date_inscription=with_date_inscription, etat=etat, with_paiement=with_paiement, with_archives=with_archives, with_annotations=with_annotations, with_bourse=with_bourse, ) # Note: le formulaire est soumis a chaque modif des groupes # on pourrait faire comme pour le form de saisie des notes. Il faudrait pour cela: # - charger tous les etudiants au debut, quels que soient les groupes selectionnés # - ajouter du JS pour modifier les liens (arguments group_ids) quand le menu change return render_template( "formsemestre/groups_lists.j2", form_groups_choice=form_groups_choice(groups_infos, submit_on_change=True), groups_table=groups_table( groups_infos=groups_infos, fmt=fmt, with_codes=with_codes, with_date_inscription=with_date_inscription, etat=etat, with_paiement=with_paiement, with_archives=with_archives, with_annotations=with_annotations, with_bourse=with_bourse, ), groups_titles=groups_infos.groups_titles, ) # view def groups_photos(group_ids=(), etat=None, formsemestre_id=None): """Affichage des photos des étudiants (trombi) des groupes indiqués group_ids: liste de group_id formsemestre_id est utilisé si aucun groupe selectionné pour construire la liste des groupes. """ groups_infos = DisplayedGroupsInfos( group_ids, formsemestre_id=formsemestre_id, select_all_when_unspecified=True, ) return render_template( "formsemestre/groups_photos.j2", form_groups_choice=form_groups_choice(groups_infos, submit_on_change=True), tab_photos_html=tab_photos_html(groups_infos, etat=etat), groups_titles=groups_infos.groups_titles, ) def groups_feuilles(group_ids=(), etat=None, formsemestre_id=None): """Affichage des feuilles d'appel des groupes indiqués group_ids: liste de group_id formsemestre_id est utilisé si aucun groupe selectionné pour construire la liste des groupes. """ groups_infos = DisplayedGroupsInfos( group_ids, formsemestre_id=formsemestre_id, select_all_when_unspecified=True, ) return render_template( "formsemestre/groups_feuilles.j2", form_groups_choice=form_groups_choice(groups_infos, submit_on_change=True), tab_absences_html=tab_absences_html(groups_infos, etat=etat), groups_titles=groups_infos.groups_titles, ) def form_groups_choice( groups_infos, with_selectall_butt=False, with_deselect_butt=False, submit_on_change=False, default_deselect_others=True, args: dict | None = None, title="Groupes :", ): """Formulaire complet pour choix de groupe. Si submit_on_change, soumet (recharge la page) à chaque modif. Si default_deselect_others, désélectionne le groupe "Tous" quand on sélectionne un autre groupe. Ces deux options ajoutent des classes utilisées en JS pour la gestion du formulaire. """ default_group_id = sco_groups.get_default_group(groups_infos.formsemestre_id) args = args or {} args_wdg = "\n".join( [ f"""""" for k, v in args.items() if k not in ("formsemestre_id", "group_ids") ] ) H = [ f"""
") return "\n".join(H) def menu_groups_choice( groups_infos, submit_on_change=False, default_deselect_others=True, ): """Menu pour selection groupes group_ids est la liste des groupes actuellement sélectionnés et doit comporter au moins un élément, sauf si formsemestre_id est spécifié. (utilisé pour retrouver le semestre et proposer la liste des autres groupes) Si url_export : selecteur.value = &group_ids=xxx&group_ids=yyy... sinon : selecteur.value = [xxx, yyy, ...] """ default_group_id = sco_groups.get_default_group(groups_infos.formsemestre_id) n_members = len(sco_groups.get_group_members(default_group_id)) values: dict = { # Choix : Tous (tous les groupes) "": [ { "value": default_group_id, "label": f"Tous ({n_members})", "selected": default_group_id in groups_infos.group_ids, "single": default_deselect_others, } ] } for partition in groups_infos.partitions: p_name: str = partition["partition_name"] vals: list[tuple[str, str, bool]] = [] # Les groupes dans cette partition: for grp in sco_groups.get_partition_groups(partition): selected: bool = grp["group_id"] in groups_infos.group_ids if grp["group_name"]: vals.append( { "value": grp["group_id"], "label": f"{grp['group_name']} ({len(sco_groups.get_group_members(grp['group_id']))})", "selected": selected, } ) values[p_name] = vals multi_select: scu.MultiSelect = scu.MultiSelect( values=values, name="group_ids", html_id="group_ids_sel" ) if submit_on_change: multi_select.change_event("submit_group_selector();") return multi_select.html() def menu_group_choice(group_id=None, formsemestre_id=None): """Un bête menu pour choisir un seul groupe group_id est le groupe actuellement sélectionné. Si aucun groupe selectionné, utilise formsemestre_id pour lister les groupes. """ if group_id: group = sco_groups.get_group(group_id) formsemestre_id = group["formsemestre_id"] elif not formsemestre_id: raise ValueError("missing formsemestre_id") H = [ """ """ ) return "\n".join(H) class DisplayedGroupsInfos: """Container with attributes describing groups to display in the page .groups_query_args : 'group_ids=xxx&group_ids=yyy' .base_url : url de la requete, avec les groupes, sans les autres paramètres .formsemestre_id : semestre "principal" (en fait celui du 1er groupe de la liste) .members : .groups_titles etat: filtrage selon l'état de l'inscription select_all_when_unspecified : sélectionne le groupe "tous" si aucun groupe n'est indiqué. empty_list_select_all: si vrai (défaut) on sélectionne le groupe tous si aucun groupe indiqué. """ def __init__( self, group_ids=(), # groupes specifies dans l'URL, ou un seul int formsemestre_id: int | None = None, etat: str | None = None, select_all_when_unspecified=False, empty_list_select_all=True, moduleimpl_id=None, # used to find formsemestre when unspecified ): group_ids = [] if group_ids is None else group_ids if isinstance(group_ids, int): if group_ids: group_ids = [group_ids] # cas ou un seul parametre, pas de liste else: try: group_ids = [int(g) for g in group_ids] except ValueError as exc: log(f"DisplayedGroupsInfos: invalid group_id '{group_ids}'") raise ScoValueError( "identifiant de groupe invalide (mettre à jour vos bookmarks ?)" ) from exc if not formsemestre_id and moduleimpl_id: modimpl = db.session.get(ModuleImpl, moduleimpl_id) if modimpl is None: raise ValueError("invalid moduleimpl_id") formsemestre_id = modimpl.formsemestre_id if not group_ids: # appel sans groupe (eg page accueil) if not formsemestre_id: raise ScoValueError("missing parameter formsemestre_id or group_ids") if empty_list_select_all: if select_all_when_unspecified: group_ids = [ sco_groups.get_default_group( formsemestre_id, fix_if_missing=True ) ] else: # selectionne le premier groupe trouvé, s'il y en a un partition = sco_groups.get_partitions_list( formsemestre_id, with_default=True )[0] groups = sco_groups.get_partition_groups(partition) if groups: group_ids = [groups[0]["group_id"]] else: group_ids = [sco_groups.get_default_group(formsemestre_id)] else: group_ids = [] gq = [] for group_id in group_ids: gq.append("group_ids=" + str(group_id)) self.groups_query_args = "&".join(gq) self.base_url = request.base_url + "?" + self.groups_query_args self.group_ids = group_ids self.groups = [] groups_titles = [] self.members = [] self.tous_les_etuds_du_sem = ( False # affiche tous les etuds du semestre ? (si un seul semestre) ) self.sems = {} # formsemestre_id : sem self.formsemestre = None self.formsemestre_id = formsemestre_id self.nbdem = 0 # nombre d'étudiants démissionnaires en tout sem = None selected_partitions = set() for group_id in group_ids: group_members, group, group_tit, sem, nbdem = sco_groups.get_group_infos( group_id, etat=etat ) self.groups.append(group) self.nbdem += nbdem self.sems[sem["formsemestre_id"]] = sem if not self.formsemestre_id: self.formsemestre_id = sem["formsemestre_id"] self.formsemestre = sem self.members.extend(group_members) groups_titles.append(group_tit) if group["partition_name"] is None: self.tous_les_etuds_du_sem = True else: # liste les partitions explicitement sélectionnés (= des groupes de group_ids) selected_partitions.add((group["numero"] or 0, group["partition_id"])) self.selected_partitions = [ x[1] for x in sorted(list(selected_partitions)) ] # -> [ partition_id ] if not self.formsemestre: # aucun groupe selectionne self.formsemestre = sco_formsemestre.get_formsemestre(formsemestre_id) if formsemestre_id not in self.sems: self.sems[formsemestre_id] = self.formsemestre self.sortuniq() if len(self.sems) > 1: self.tous_les_etuds_du_sem = False # plusieurs semestres if self.tous_les_etuds_du_sem: if sem and sem["semestre_id"] >= 0: self.groups_titles = "S%d" % sem["semestre_id"] else: self.groups_titles = "tous" self.groups_filename = self.groups_titles else: self.groups_titles = ", ".join(groups_titles) self.groups_filename = "_".join(groups_titles).replace(" ", "_") # Sanitize filename: self.groups_filename = scu.make_filename(self.groups_filename) # colonnes pour affichages nom des groupes: # gère le cas où les étudiants appartiennent à des semestres différents self.partitions = [] # les partitions, sans celle par defaut for formsemestre_id in self.sems: for partition in sco_groups.get_partitions_list(formsemestre_id): if partition["partition_name"]: self.partitions.append(partition) def sortuniq(self): "Trie les étudiants (de plusieurs groupes) et élimine les doublons" if (len(self.group_ids) <= 1) or len(self.members) <= 1: return # on suppose que les etudiants d'un groupe sont deja triés # tri selon nom_usuel ou nom, sans accents self.members.sort(key=etud_sort_key) to_remove = [] T = self.members for i in range(len(T) - 1, 0, -1): if T[i - 1]["etudid"] == T[i]["etudid"]: to_remove.append(i) for i in to_remove: del T[i] def get_etudids(self) -> set[int]: "Les etudids des groupes choisis" return {member["etudid"] for member in self.members} def get_form_elem(self): """html hidden input with groups""" H = [] for group_id in self.group_ids: H.append(f'') return "\n".join(H) def get_formsemestre(self) -> FormSemestre: "le formsemestre" return ( db.session.get(FormSemestre, self.formsemestre_id) if self.formsemestre_id else None ) def get_groups_key(self) -> str: "clé identifiant les groupes sélectionnés, utile pour cache" return "-".join(str(x) for x in sorted(self.group_ids)) # Ancien ZScolar.group_list renommé ici en group_table def groups_table( groups_infos: DisplayedGroupsInfos = None, with_codes=0, with_date_inscription=0, etat=None, fmt="html", with_paiement=0, # si vrai, ajoute colonnes infos paiement droits et finalisation inscription (lent car interrogation portail) with_archives=0, # ajoute colonne avec noms fichiers archivés with_annotations=0, with_bourse=0, ): """liste etudiants inscrits dans ce semestre fmt: csv, json, xml, xls, allxls, xlsappel, moodlecsv, pdf Si with_codes, ajoute 4 colonnes avec les codes etudid, NIP, INE et etape """ from app.scodoc import sco_report can_view_etud_data = int(current_user.has_permission(Permission.ViewEtudData)) with_codes = int(with_codes) with_date_inscription = int(with_date_inscription) with_paiement = int(with_paiement) and can_view_etud_data with_archives = int(with_archives) and can_view_etud_data with_annotations = int(with_annotations) and can_view_etud_data with_bourse = int(with_bourse) and can_view_etud_data base_url = ( groups_infos.base_url + f"""&with_codes={with_codes}&with_date_inscription={ with_date_inscription}&with_paiement={with_paiement}&with_archives={ with_archives}&with_annotations={with_annotations }&with_bourse={with_bourse}""" ) # columns_ids = ["civilite_str", "nom_disp", "prenom"] # colonnes a inclure titles = { "civilite_str": "Civ.", "nom_disp": "Nom", "prenom": "Prénom", "email": "Mail", "emailperso": "Personnel", "etat": "Etat", "etudid": "etudid", "code_nip": "code_nip", "code_ine": "code_ine", "date_inscription": "Date inscription", "datefinalisationinscription_str": "Finalisation inscr.", "paiementinscription_str": "Paiement", "etudarchive": "Fichiers", "annotations_str": "Annotations", "bourse_str": "Boursier", # requière ViewEtudData "etape": "Etape", "semestre_groupe": "Semestre-Groupe", # pour Moodle "annee": "annee_admission", "nationalite": "nationalite", # requière ViewEtudData } # ajoute colonnes pour groupes columns_ids.extend([p["partition_id"] for p in groups_infos.partitions]) titles.update( dict( [(p["partition_id"], p["partition_name"]) for p in groups_infos.partitions] ) ) partitions_name = { p["partition_id"]: p["partition_name"] for p in groups_infos.partitions } if fmt != "html": # ne mentionne l'état que en Excel (style en html) columns_ids.append("etat") columns_ids.append("email") if can_view_etud_data: columns_ids.append("emailperso") if fmt == "moodlecsv": columns_ids = ["email", "semestre_groupe"] if with_codes: columns_ids += ["etape", "etudid", "code_nip", "code_ine"] if with_date_inscription: columns_ids += ["date_inscription"] if with_paiement: columns_ids += ["datefinalisationinscription_str", "paiementinscription_str"] if with_paiement: sco_portal_apogee.check_paiement_etuds(groups_infos.members) if with_archives: from app.scodoc import sco_archives_etud sco_archives_etud.add_archives_info_to_etud_list(groups_infos.members) columns_ids += ["etudarchive"] if with_annotations: sco_etud.add_annotations_to_etud_list(groups_infos.members) columns_ids += ["annotations_str"] if with_bourse: columns_ids += ["bourse_str"] moodle_sem_name = groups_infos.formsemestre["session_id"] moodle_groupenames = set() # ajoute liens for etud_info in groups_infos.members: if with_date_inscription: event = ScolarEvent.query.filter_by( etudid=etud_info["etudid"], event_type="INSCRIPTION", formsemestre_id=groups_infos.formsemestre_id, ).first() if event: etud_info["date_inscription"] = event.event_date.strftime(scu.DATE_FMT) etud_info["_date_inscription_xls"] = event.event_date etud_info["_date_inscription_order"] = event.event_date.isoformat if etud_info["email"]: etud_info["_email_target"] = "mailto:" + etud_info["email"] else: etud_info["_email_target"] = "" if etud_info["emailperso"]: etud_info["_emailperso_target"] = "mailto:" + etud_info["emailperso"] else: etud_info["_emailperso_target"] = "" fiche_url = url_for( "scolar.fiche_etud", scodoc_dept=g.scodoc_dept, etudid=etud_info["etudid"] ) etud_info["_nom_disp_target"] = fiche_url etud_info["_nom_disp_order"] = etud_sort_key(etud_info) etud_info["_prenom_target"] = fiche_url etud_info["_nom_disp_td_attrs"] = ( f"""id="{etud_info['etudid']}" class="etudinfo" """ ) etud_info["bourse_str"] = "oui" if etud_info["boursier"] else "non" if etud_info["etat"] == "D": etud_info["_css_row_class"] = "etuddem" # et groupes: for partition_id in etud_info["partitions"]: etud_info[partition_id] = etud_info["partitions"][partition_id][ "group_name" ] # Ajoute colonne pour moodle: semestre_groupe, de la forme # RT-DUT-FI-S3-2021-PARTITION-GROUPE moodle_groupename = [] if groups_infos.selected_partitions: # il y a des groupes selectionnes, utilise leurs partitions for partition_id in groups_infos.selected_partitions: if partition_id in etud_info["partitions"]: moodle_groupename.append( partitions_name[partition_id] + "-" + etud_info["partitions"][partition_id]["group_name"] ) else: # pas de groupes sélectionnés: prend le premier s'il y en a un moodle_groupename = ["tous"] if etud_info["partitions"]: for p in etud_info[ "partitions" ].items(): # partitions is an OrderedDict moodle_groupename = [ partitions_name[p[0]] + "-" + p[1]["group_name"] ] break moodle_groupenames |= set(moodle_groupename) etud_info["semestre_groupe"] = ( moodle_sem_name + "-" + "+".join(moodle_groupename) ) if groups_infos.nbdem > 1: s = "s" else: s = "" if fmt == "moodlecsv": # de la forme S1-[FI][FA]-groupe.csv if not moodle_groupenames: moodle_groupenames = {"tous"} filename = ( moodle_sem_name + "-" + groups_infos.formsemestre["modalite"] + "-" + "+".join(sorted(moodle_groupenames)) ) else: filename = f"etudiants_{groups_infos.groups_filename}" prefs = sco_preferences.SemPreferences(groups_infos.formsemestre_id) tab = GenTable( rows=groups_infos.members, columns_ids=columns_ids, titles=titles, caption="soit %d étudiants inscrits et %d démissionaire%s." % (len(groups_infos.members) - groups_infos.nbdem, groups_infos.nbdem, s), base_url=base_url, filename=filename, pdf_link=False, # pas d'export pdf html_sortable=True, html_class="table_leftalign table_listegroupe", xml_outer_tag="group_list", xml_row_tag="etud", text_fields_separator=prefs["moodle_csv_separator"], text_with_titles=prefs["moodle_csv_with_headerline"], preferences=prefs, table_id="groups_table", ) # if fmt == "html": amail_inst = [ x["email"] for x in groups_infos.members if x["email"] and x["etat"] != "D" ] amail_perso = [ x["emailperso"] for x in groups_infos.members if x["emailperso"] and x["etat"] != "D" ] if len(groups_infos.members): if groups_infos.tous_les_etuds_du_sem: htitle = "Les %d étudiants inscrits" % len(groups_infos.members) else: htitle = "Groupe %s (%d étudiants)" % ( groups_infos.groups_titles, len(groups_infos.members), ) else: htitle = "Aucun étudiant !" H = [ f"""