ScoDoc/app/scodoc/sco_edit_matiere.py

321 lines
11 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
#
##############################################################################
"""Ajout/Modification/Supression matieres
(portage from DTML)
"""
import notesdb as ndb
import sco_utils as scu
from notes_log import log
from TrivialFormulator import TrivialFormulator, TF, tf_error_message
import sco_edit_ue
import sco_formsemestre
import sco_news
from sco_exceptions import ScoValueError
_matiereEditor = ndb.EditableTable(
"notes_matieres",
"matiere_id",
("matiere_id", "ue_id", "numero", "titre"),
sortkey="numero",
output_formators={"numero": ndb.int_null_is_zero},
)
def do_matiere_list(context, *args, **kw):
"list matieres"
cnx = ndb.GetDBConnexion()
return _matiereEditor.list(cnx, *args, **kw)
def do_matiere_edit(context, *args, **kw):
"edit a matiere"
cnx = ndb.GetDBConnexion()
# check
mat = sco_edit_matiere.do_matiere_list(
context, {"matiere_id": args[0]["matiere_id"]}
)[0]
if sco_edit_matiere.matiere_is_locked(context, mat["matiere_id"]):
raise ScoLockedFormError()
# edit
_matiereEditor.edit(cnx, *args, **kw)
sco_core.inval_cache(context) # > modif matiere
def do_matiere_create(context, args, REQUEST):
"create a matiere"
cnx = ndb.GetDBConnexion()
# check
ue = sco_edit_ue.do_ue_list(context, {"ue_id": args["ue_id"]})[0]
# create matiere
r = _matiereEditor.create(cnx, args)
# news
F = sco_formations.formation_list(
context, args={"formation_id": ue["formation_id"]}
)[0]
sco_news.add(
context,
REQUEST,
typ=NEWS_FORM,
object=ue["formation_id"],
text="Modification de la formation %(acronyme)s" % F,
)
return r
def matiere_create(context, ue_id=None, REQUEST=None):
"""Creation d'une matiere"""
UE = sco_edit_ue.do_ue_list(context, args={"ue_id": ue_id})[0]
H = [
html_sco_header.sco_header(
context, REQUEST, page_title="Création d'une matière"
),
"""<h2>Création d'une matière dans l'UE %(titre)s (%(acronyme)s)</h2>""" % UE,
"""<p class="help">Les matières sont des groupes de modules dans une UE
d'une formation donnée. Les matières servent surtout pour la
présentation (bulletins, etc) mais <em>n'ont pas de rôle dans le calcul
des notes.</em>
</p>
<p class="help">Si votre formation n'utilise pas la notion de
"matières", créez une matière par UE, et donnez lui le même nom que l'UE
(en effet, tout module doit appartenir à une matière).
</p>
<p class="help">Comme les UE, les matières n'ont pas de coefficient
associé.
</p>""",
]
tf = TrivialFormulator(
REQUEST.URL0,
REQUEST.form,
(
("ue_id", {"input_type": "hidden", "default": ue_id}),
("titre", {"size": 30, "explanation": "nom de la matière."}),
(
"numero",
{
"size": 2,
"explanation": "numéro (1,2,3,4...) pour affichage",
"type": "int",
},
),
),
submitlabel="Créer cette matière",
)
dest_url = scu.NotesURL() + "/ue_list?formation_id=" + UE["formation_id"]
if tf[0] == 0:
return "\n".join(H) + tf[1] + html_sco_header.sco_footer(context, REQUEST)
elif tf[0] == -1:
return REQUEST.RESPONSE.redirect(dest_url)
else:
# check unicity
mats = do_matiere_list(context, args={"ue_id": ue_id, "titre": tf[2]["titre"]})
if mats:
return (
"\n".join(H)
+ tf_error_message("Titre de matière déjà existant dans cette UE")
+ tf[1]
+ html_sco_header.sco_footer(context, REQUEST)
)
_ = do_matiere_create(context, tf[2], REQUEST)
return REQUEST.RESPONSE.redirect(dest_url)
def do_matiere_delete(context, oid, REQUEST):
"delete matiere and attached modules"
cnx = ndb.GetDBConnexion()
# check
mat = do_matiere_list(context, {"matiere_id": oid})[0]
ue = sco_edit_ue.do_ue_list(context, {"ue_id": mat["ue_id"]})[0]
locked = sco_edit_matiere.matiere_is_locked(context, mat["matiere_id"])
if locked:
log("do_matiere_delete: mat=%s" % mat)
log("do_matiere_delete: ue=%s" % ue)
log("do_matiere_delete: locked sems: %s" % locked)
raise ScoLockedFormError()
log("do_matiere_delete: matiere_id=%s" % oid)
# delete all modules in this matiere
mods = sco_edit_module.do_module_list(context, {"matiere_id": oid})
for mod in mods:
sco_edit_module.do_module_delete(context, mod["module_id"], REQUEST)
_matiereEditor.delete(cnx, oid)
# news
F = sco_formations.formation_list(
context, args={"formation_id": ue["formation_id"]}
)[0]
sco_news.add(
context,
REQUEST,
typ=NEWS_FORM,
object=ue["formation_id"],
text="Modification de la formation %(acronyme)s" % F,
)
def matiere_delete(context, matiere_id=None, REQUEST=None):
"""Delete an UE"""
M = do_matiere_list(context, args={"matiere_id": matiere_id})[0]
UE = sco_edit_ue.do_ue_list(context, args={"ue_id": M["ue_id"]})[0]
H = [
html_sco_header.sco_header(
context, REQUEST, page_title="Suppression d'une matière"
),
"<h2>Suppression de la matière %(titre)s" % M,
" dans l'UE (%(acronyme)s))</h2>" % UE,
]
dest_url = scu.NotesURL() + "/ue_list?formation_id=" + str(UE["formation_id"])
tf = TrivialFormulator(
REQUEST.URL0,
REQUEST.form,
(("matiere_id", {"input_type": "hidden"}),),
initvalues=M,
submitlabel="Confirmer la suppression",
cancelbutton="Annuler",
)
if tf[0] == 0:
return "\n".join(H) + tf[1] + html_sco_header.sco_footer(context, REQUEST)
elif tf[0] == -1:
return REQUEST.RESPONSE.redirect(dest_url)
else:
do_matiere_delete(context, matiere_id, REQUEST)
return REQUEST.RESPONSE.redirect(dest_url)
def matiere_edit(context, matiere_id=None, REQUEST=None):
"""Edit matiere"""
F = do_matiere_list(context, args={"matiere_id": matiere_id})
if not F:
raise ScoValueError("Matière inexistante !")
F = F[0]
U = sco_edit_ue.do_ue_list(context, args={"ue_id": F["ue_id"]})
if not F:
raise ScoValueError("UE inexistante !")
U = U[0]
Fo = sco_formations.formation_list(
context, args={"formation_id": U["formation_id"]}
)[0]
ues = sco_edit_ue.do_ue_list(context, args={"formation_id": U["formation_id"]})
ue_names = ["%(acronyme)s (%(titre)s)" % u for u in ues]
ue_ids = [u["ue_id"] for u in ues]
H = [
html_sco_header.sco_header(
context, REQUEST, page_title="Modification d'une matière"
),
"""<h2>Modification de la matière %(titre)s""" % F,
"""(formation %(acronyme)s, version %(version)s)</h2>""" % Fo,
]
help = """<p class="help">Les matières sont des groupes de modules dans une UE
d'une formation donnée. Les matières servent surtout pour la
présentation (bulletins, etc) mais <em>n'ont pas de rôle dans le calcul
des notes.</em>
</p>
<p class="help">Si votre formation n'utilise pas la notion de
"matières", créez une matière par UE, et donnez lui le même nom que l'UE
(en effet, tout module doit appartenir à une matière).
</p>
<p class="help">Comme les UE, les matières n'ont pas de coefficient
associé.
</p>"""
tf = TrivialFormulator(
REQUEST.URL0,
REQUEST.form,
(
("matiere_id", {"input_type": "hidden"}),
(
"ue_id",
{"input_type": "menu", "allowed_values": ue_ids, "labels": ue_names},
),
("titre", {"size": 30, "explanation": "nom de cette matière"}),
(
"numero",
{
"size": 2,
"explanation": "numéro (1,2,3,4...) pour affichage",
"type": "int",
},
),
),
initvalues=F,
submitlabel="Modifier les valeurs",
)
dest_url = scu.NotesURL() + "/ue_list?formation_id=" + U["formation_id"]
if tf[0] == 0:
return (
"\n".join(H) + tf[1] + help + html_sco_header.sco_footer(context, REQUEST)
)
elif tf[0] == -1:
return REQUEST.RESPONSE.redirect(dest_url)
else:
# check unicity
mats = do_matiere_list(
context, args={"ue_id": tf[2]["ue_id"], "titre": tf[2]["titre"]}
)
if len(mats) > 1 or (len(mats) == 1 and mats[0]["matiere_id"] != matiere_id):
return (
"\n".join(H)
+ tf_error_message("Titre de matière déjà existant dans cette UE")
+ tf[1]
+ html_sco_header.sco_footer(context, REQUEST)
)
# changement d'UE ?
if tf[2]["ue_id"] != F["ue_id"]:
log("attaching mat %s to new UE %s" % (matiere_id, tf[2]["ue_id"]))
ndb.SimpleQuery(
context,
"UPDATE notes_modules SET ue_id = %(ue_id)s WHERE matiere_id=%(matiere_id)s",
{"ue_id": tf[2]["ue_id"], "matiere_id": matiere_id},
)
do_matiere_edit(context, tf[2])
return REQUEST.RESPONSE.redirect(dest_url)
def matiere_is_locked(context, matiere_id):
"""True if matiere should not be modified
(contains modules used in a locked formsemestre)
"""
r = ndb.SimpleDictFetch(
context,
"""SELECT ma.* from notes_matieres ma, notes_modules mod, notes_formsemestre sem, notes_moduleimpl mi
WHERE ma.matiere_id = mod.matiere_id AND mi.module_id = mod.module_id AND mi.formsemestre_id = sem.formsemestre_id
AND ma.matiere_id = %(matiere_id)s AND sem.etat = 0
""",
{"matiere_id": matiere_id},
)
return len(r) > 0