From ac8c7b034d0b772b260a5f4f8e2f323db8762115 Mon Sep 17 00:00:00 2001 From: anhefti Date: Mon, 2 Oct 2023 16:17:49 +0200 Subject: [PATCH] SEBSERV-464 fix moodle course short name change recover --- .../session/impl/ExamUpdateHandler.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamUpdateHandler.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamUpdateHandler.java index 55b1d1c5..4cb062fe 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamUpdateHandler.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/session/impl/ExamUpdateHandler.java @@ -12,6 +12,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import org.joda.time.DateTime; @@ -28,6 +29,7 @@ import ch.ethz.seb.sebserver.gbl.model.exam.Exam; import ch.ethz.seb.sebserver.gbl.model.exam.Exam.ExamStatus; import ch.ethz.seb.sebserver.gbl.model.exam.QuizData; import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup.Features; +import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup.LmsType; import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile; import ch.ethz.seb.sebserver.gbl.util.Result; import ch.ethz.seb.sebserver.gbl.util.Utils; @@ -38,6 +40,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ExamDAO; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.LmsAPIService; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.LmsAPITemplate; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.SEBRestrictionService; +import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils; import ch.ethz.seb.sebserver.webservice.servicelayer.session.ExamFinishedEvent; import ch.ethz.seb.sebserver.webservice.servicelayer.session.ExamResetEvent; import ch.ethz.seb.sebserver.webservice.servicelayer.session.ExamStartedEvent; @@ -138,7 +141,12 @@ class ExamUpdateHandler { .forEach(quiz -> { try { - final Exam exam = exams.get(quiz.id); + final Exam exam = getExamForQuizWithMoodleSpecialCase(exams, quiz); + + if (exam == null) { + log.warn("Failed to find map exam to fetched quiz-data: {}", quiz); + return; + } if (hasChanges(exam, quiz)) { @@ -485,4 +493,29 @@ class ExamUpdateHandler { } } + private Exam getExamForQuizWithMoodleSpecialCase(final Map exams, final QuizData quiz) { + Exam exam = exams.get(quiz.id); + + if (exam == null) { + try { + final LmsAPITemplate lms = this.lmsAPIService + .getLmsAPITemplate(quiz.lmsSetupId) + .getOrThrow(); + + if (lms.getType() == LmsType.MOODLE || lms.getType() == LmsType.MOODLE_PLUGIN) { + final String quizId = MoodleUtils.getQuizId(quiz.id); + final Optional find = + exams.keySet().stream().filter(key -> key.startsWith(quizId)).findFirst(); + if (find.isPresent()) { + exam = exams.get(find.get()); + } + } + } catch (final Exception e) { + log.error("Failed to verify changed external Exam id from moodle course: {}", e.getMessage()); + } + } + + return exam; + } + }