--- /dev/null
+/*
+ * Copyright © FYLAB and the Conseil Régional d'Île-de-France, 2009
+ * This file is part of L'Interface Libre et Interactive de l'Enseignement (Lilie).
+ *
+ * Lilie is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE.txt in the directory of this software
+ * distribution.
+ *
+ * Lilie 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
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * and the CeCILL-C along with Lilie. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ */
+
+
+
+package org.lilie.services.eliot.notes.domaine
+
+import org.lilie.services.eliot.notes.AbstractNotesController
+import grails.converters.JSON
+import org.lilie.services.eliot.EliotConstantes
+import org.lilie.services.eliot.notes.scolarite.ModifResult
+import org.lilie.services.eliot.notes.scolarite.NotesModaliteMatiereParams
+import org.lilie.services.eliot.scolarite.TypePeriode
+import org.lilie.services.eliot.scolarite.NaturePeriodeEnum
+
+import org.lilie.services.eliot.scolarite.ModaliteMatiere
+import org.lilie.services.eliot.scolarite.Service
+import org.lilie.services.eliot.notes.ActionEnum
+import org.lilie.services.eliot.notes.NotesDroitService
+import org.lilie.services.eliot.notes.NotesFonction
+import org.lilie.services.eliot.notes.scolarite.NotesSousServiceService
+
+class SousServiceController extends AbstractNotesController {
+
+ NotesDroitService notesDroitService
+ NotesSousServiceService notesSousServiceService
+
+ def creeSousService = { ServiceCommand command ->
+
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ etablissementCourant(),
+ ActionEnum.CREATION,
+ NotesFonction.DIRECTION_ASSIMILE
+ )
+
+ if (command.hasErrors()) {
+ log.error(command.errors)
+ render(
+ wrapResultat {
+ [(EliotConstantes.JSON_SUCCESS): false]
+ } as JSON
+ )
+ } else {
+ params.creation = true
+ Map result = internalModifieSousService(command)
+ render result as JSON
+ }
+ }
+
+ def modifieSousService = { ServiceCommand command ->
+
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ etablissementCourant(),
+ ActionEnum.MODIFICATION,
+ NotesFonction.DIRECTION_ASSIMILE
+ )
+
+ Map result = internalModifieSousService(command)
+ render result as JSON
+
+ }
+
+ private def internalModifieSousService(ServiceCommand command) {
+ wrapResultat {
+ Map valeurs = [:]
+ if (command.hasErrors()) {
+ valeurs[(EliotConstantes.JSON_SUCCESS)] = false
+ log.error("Paramètres de modifieRelation sont incorrectes: " +
+ command.errors)
+ } else {
+
+ Boolean creation = params.creation ?: false
+ command.parse()
+ NotesModaliteMatiereParams dto = creeModaliteMatieresDto(command)
+
+ if (!creation && dto.serviceParent == null) {
+ throw new IllegalArgumentException("Service ${command.idService} n'existe pas")
+ }
+
+ ModifResult modifResult = null
+ if (creation) {
+ modifResult = notesSousServiceService.creeSousService(
+ securiteSession, dto)
+ } else {
+ modifResult = notesSousServiceService.modifieSousService(
+ securiteSession, dto)
+ }
+
+ if (modifResult.mmExisteDeja) {
+ valeurs['modified'] = false
+ valeurs[EliotConstantes.JSON_MESSAGE] =
+ message(code: 'eliot.notes.parametrageServices.lienServiceSousMatiere.existeDeja',
+ args: [dto.serviceParent.matiere.codeGestion,
+ creation ? dto.modaliteMatiere.code : dto.nouveauModaliteMatiere.code])
+ valeurs[EliotConstantes.JSON_SUCCESS] = false
+ }
+ valeurs['recharger'] = true
+ valeurs['id'] = modifResult.id // id du service
+ valeurs['idCompose'] = modifResult.idCompose // id composé du service
+ }
+ return valeurs
+ }
+ }
+
+ /**
+ * Crée un DTO de params pour la modif/création des liens
+ * @param command
+ * @return
+ */
+ private NotesModaliteMatiereParams creeModaliteMatieresDto(ServiceCommand command) {
+ NotesModaliteMatiereParams paramsDto = new NotesModaliteMatiereParams()
+ paramsDto.serviceParent = command.getServiceParent()
+
+ paramsDto.modaliteMatiere = command.getModaliteMatiere()
+ paramsDto.nouveauModaliteMatiere = command.getNouveauModaliteMatiere()
+ paramsDto.classe = command.getClasse()
+
+ Map<TypePeriode, BigDecimal> coeffs = [:]
+ command.coeffs.each { Long typePeriodeId, BigDecimal coeff ->
+ coeffs.put(TypePeriode.get(typePeriodeId), coeff)
+ }
+ paramsDto.coeffs = coeffs
+ paramsDto.typePeriode = command.idTypePeriode ? TypePeriode.get(command.idTypePeriode) : null
+ paramsDto.nature = NaturePeriodeEnum.valueOf(NaturePeriodeEnum.class, command.nature)
+ paramsDto.evaluable = command.evaluable
+
+ return paramsDto
+ }
+
+ def actionSupprimeSousService = { ServiceSuppressionCommand command ->
+
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ etablissementCourant(),
+ ActionEnum.SUPPRESSION,
+ NotesFonction.DIRECTION_ASSIMILE
+ )
+
+ Map result = wrapResultat {
+
+ if (command.hasErrors() || command.modaliteMatiereId == null) {
+ if (command.modaliteMatiereId == null) {
+ log.error("Param modaliteMatiereId manquant")
+ } else {
+ log.error(command.errors)
+ }
+ return [(EliotConstantes.JSON_SUCCESS): false]
+ } else {
+
+ ModaliteMatiere mm = command.getModaliteMatiere()
+ if (mm == null) {
+ throw new IllegalArgumentException("ModaliteMatiere ${command.modaliteMatiereId} n'existe pas")
+ }
+ Service service = command.getService()
+ if (service == null) {
+ throw new IllegalArgumentException("Service ${command.serviceId} n'existe pas")
+ }
+
+ NaturePeriodeEnum nature = command.getNatureEnum()
+ TypePeriode typePeriode = command.getTypePriode()
+
+ // vérifie que le service n'est pas évaluable dans les autres périodes
+ if (!command.phase1Passee() && notesSousServiceService.
+ sousServiceEvaluableDansAutrePeriodes(securiteSession,
+ service, mm, nature, typePeriode)) {
+ return [
+ message: message(code: 'eliot.notes.parametrageServices.sousService.evaluableDansAutresPeriodes'),
+ 'deleted': false,
+ 'valParam': 'val1confirmee'
+ ]
+ } else {
+ // vérifie données pas attachées au service
+ if (!command.phase2passee() && notesSousServiceService.
+ sousServiceHasDonneesLiees(service, mm)) {
+ return [
+ message: message(code: 'eliot.notes.parametrageServices.sousService.aDesDonneesLiees'),
+ 'deleted': false,
+ 'valParam': 'val2confirmee'
+ ]
+ } else {
+ supprimeSousService(command)
+ }
+ }
+ }
+
+ }
+
+ render result as JSON
+ }
+
+ private def supprimeSousService(def command) {
+ ModaliteMatiere mm = command.getModaliteMatiere()
+ Service service = command.getService()
+ TypePeriode typePeriodeChoisi = command.getTypePriode()
+ NaturePeriodeEnum nature = command.getNatureEnum()
+
+ notesSousServiceService.supprimeSousServicesCompletement(securiteSession,
+ service, mm, typePeriodeChoisi, nature)
+
+
+ return ['deleted': true]
+ }
+
+ def executeSupprimeSousService = { ServiceSuppressionCommand command ->
+
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ etablissementCourant(),
+ ActionEnum.SUPPRESSION,
+ NotesFonction.DIRECTION_ASSIMILE
+ )
+
+ Map result = wrapResultat {
+ supprimeSousService(command)
+ }
+
+ render result as JSON
+ }
+}