From f3f3274f054241d26e1a2628558dfae415015f38 Mon Sep 17 00:00:00 2001 From: anhefti Date: Thu, 2 Mar 2023 15:44:13 +0100 Subject: [PATCH] fixed moodle old still uses coursemodule, new quiz id --- .../lms/impl/moodle/MoodleUtils.java | 33 ++++++------------- .../moodle/legacy/MoodleCourseAccess.java | 33 ++++++++++++++++--- .../session/impl/ExamUpdateHandler.java | 1 + 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleUtils.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleUtils.java index a8d32da3..eda1f716 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleUtils.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/MoodleUtils.java @@ -13,7 +13,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -178,6 +177,15 @@ public abstract class MoodleUtils { final CourseData courseData, final String uriPrefix, final boolean prependShortCourseName) { + return quizDataOf(lmsSetup, courseData, uriPrefix, prependShortCourseName, true); + } + + public static List quizDataOf( + final LmsSetup lmsSetup, + final CourseData courseData, + final String uriPrefix, + final boolean prependShortCourseName, + final boolean useQuizId) { final Map additionalAttrs = new HashMap<>(); additionalAttrs.put(QuizData.ATTR_ADDITIONAL_CREATION_TIME, String.valueOf(courseData.time_created)); @@ -194,7 +202,7 @@ public abstract class MoodleUtils { additionalAttrs.put(QuizData.ATTR_ADDITIONAL_TIME_LIMIT, String.valueOf(courseQuizData.time_limit)); return new QuizData( MoodleUtils.getInternalQuizId( - courseQuizData.id, + (useQuizId) ? courseQuizData.id : courseQuizData.course_module, courseData.id, courseData.short_name, courseData.idnumber), @@ -219,27 +227,6 @@ public abstract class MoodleUtils { return courseAndQuiz; } - public static final void fillSelectedQuizzes( - final Set quizIds, - final Map finalCourseDataRef, - final CourseQuiz quiz) { - try { - final CourseData course = finalCourseDataRef.get(quiz.course); - if (course != null) { - final String internalQuizId = MoodleUtils.getInternalQuizId( - quiz.id, - course.id, - course.short_name, - course.idnumber); - if (quizIds.contains(internalQuizId)) { - course.quizzes.add(quiz); - } - } - } catch (final Exception e) { - log.error("Failed to verify selected quiz for course: {}", e.getMessage()); - } - } - // ---- Mapping Classes --- /** Maps the Moodle course API course data */ diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/legacy/MoodleCourseAccess.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/legacy/MoodleCourseAccess.java index eca881df..5789e4fa 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/legacy/MoodleCourseAccess.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/moodle/legacy/MoodleCourseAccess.java @@ -55,6 +55,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleRestT import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.CourseData; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.CoursePage; +import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.CourseQuiz; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.CourseQuizData; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.Courses; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.MoodleUserDetails; @@ -262,7 +263,7 @@ public class MoodleCourseAccess implements CourseAccessAPI { courseData.values().stream() .filter(c -> !c.quizzes.isEmpty()) .forEach(c -> asyncQuizFetchBuffer.buffer.addAll( - MoodleUtils.quizDataOf(lmsSetup, c, urlPrefix, this.prependShortCourseName) + MoodleUtils.quizDataOf(lmsSetup, c, urlPrefix, this.prependShortCourseName, false) .stream() .filter(LmsAPIService.quizFilterPredicate(filterMap)) .collect(Collectors.toList()))); @@ -392,7 +393,7 @@ public class MoodleCourseAccess implements CourseAccessAPI { lmsSetup, c, urlPrefix, - this.prependShortCourseName).stream()) + this.prependShortCourseName, false).stream()) .filter(q -> exam.name.contains(q.name)) .findFirst() .get(); @@ -600,7 +601,7 @@ public class MoodleCourseAccess implements CourseAccessAPI { final Map finalCourseDataRef = courseData; courseQuizData.quizzes .stream() - .forEach(quiz -> MoodleUtils.fillSelectedQuizzes(quizIds, finalCourseDataRef, quiz)); + .forEach(quiz -> fillSelectedQuizzes(quizIds, finalCourseDataRef, quiz)); final String urlPrefix = (lmsSetup.lmsApiUrl.endsWith(Constants.URL_PATH_SEPARATOR)) ? lmsSetup.lmsApiUrl + MOODLE_QUIZ_START_URL_PATH @@ -613,7 +614,8 @@ public class MoodleCourseAccess implements CourseAccessAPI { lmsSetup, cd, urlPrefix, - this.prependShortCourseName).stream()) + this.prependShortCourseName, + false).stream()) .collect(Collectors.toList()); } catch (final Exception e) { @@ -698,7 +700,7 @@ public class MoodleCourseAccess implements CourseAccessAPI { final String startURI = uriPrefix + courseQuizData.course_module; return new QuizData( MoodleUtils.getInternalQuizId( - courseQuizData.course_module, + courseQuizData.course_module, // TODO this is wrong should be id. Create recovery task courseData.id, courseData.short_name, courseData.idnumber), @@ -719,6 +721,27 @@ public class MoodleCourseAccess implements CourseAccessAPI { additionalAttrs); } + private static final void fillSelectedQuizzes( + final Set quizIds, + final Map finalCourseDataRef, + final CourseQuiz quiz) { + try { + final CourseData course = finalCourseDataRef.get(quiz.course); + if (course != null) { + final String internalQuizId = MoodleUtils.getInternalQuizId( + quiz.course_module, // TODO this is wrong should be id. Create recovery task + course.id, + course.short_name, + course.idnumber); + if (quizIds.contains(internalQuizId)) { + course.quizzes.add(quiz); + } + } + } catch (final Exception e) { + log.error("Failed to verify selected quiz for course: {}", e.getMessage()); + } + } + private Result getRestTemplate() { if (this.restTemplate == null) { final Result templateRequest = this.restTemplateFactory 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 8726af84..e4e60055 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 @@ -137,6 +137,7 @@ class ExamUpdateHandler { try { final Exam exam = exams.get(quiz.id); + if (hasChanges(exam, quiz)) { final Result updateQuizData = this.examDAO