From dcfec82dabe75453298f0cfbe4a1cdfb1e0567a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Carvalho?= Date: Tue, 3 Oct 2023 18:37:48 +0100 Subject: [PATCH] feat: Delete student. --- lib/pages/Teacher/Classes/TurmaExemplo.dart | 97 ++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/lib/pages/Teacher/Classes/TurmaExemplo.dart b/lib/pages/Teacher/Classes/TurmaExemplo.dart index 34bf0dc..10ccb69 100644 --- a/lib/pages/Teacher/Classes/TurmaExemplo.dart +++ b/lib/pages/Teacher/Classes/TurmaExemplo.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'dart:js_interop'; import 'package:assessments_app/InovWidgets/ChartData.dart'; import 'package:assessments_app/InovWidgets/LegendWidget.dart'; import 'package:assessments_app/pages/Teacher/Assessments/GenMultipleSelfAssessments.dart'; @@ -750,6 +749,102 @@ class _TurmaExemploState extends State ), ); }, + onLongPress: () { + showDialog( + context: context, + builder: (_) => AlertDialog( + title: Text( + "Delete this student"), + content: Text( + "Are you sure you want to remove this?"), + actions: [ + TextButton( + style: ButtonStyle( + foregroundColor: + MaterialStateProperty + .all( + Colors + .blue), + ), + onPressed: () { + Navigator.of(context, + rootNavigator: + true) + .pop(); + }, + child: Text( + AppLocalizations.of( + context)! + .cancel), + ), + TextButton( + style: ButtonStyle( + foregroundColor: + MaterialStateProperty + .all( + Colors + .blue), + ), + onPressed: () async { + Navigator.of(context, + rootNavigator: + true) + .pop(); + var x; + if (studs[index] != + namedStuds[studs[ + index] + .toString()]) { + x = studs[index] + .toString(); + } else { + x = namedStuds[studs[ + index] + .toString()] + .toString(); + } + print(x); + + // Delete the class from /classes + // This does not removed subcollections + + // Delete assessments in /assessments + + await FirebaseFirestore + .instance + .collection( + 'classes') + .doc(widget + .passedClassName) + .update({ + 'StudentList': + FieldValue + .arrayRemove( + [x]), + }); + await FirebaseFirestore + .instance + .collection( + 'classes') + .doc(widget + .passedClassName) + .update({ + 'NumStudents': + FieldValue + .increment( + -1), + }); + setState(() {}); + }, + child: Text( + AppLocalizations.of( + context)! + .delete, + ), + ), + ], + )); + }, ); }, ),