fixed moodle old still uses coursemodule, new quiz id

This commit is contained in:
anhefti 2023-03-02 15:44:13 +01:00
parent 67d0052acc
commit f3f3274f05
3 changed files with 39 additions and 28 deletions

View file

@ -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<QuizData> quizDataOf(
final LmsSetup lmsSetup,
final CourseData courseData,
final String uriPrefix,
final boolean prependShortCourseName,
final boolean useQuizId) {
final Map<String, String> 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<String> quizIds,
final Map<String, CourseData> 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 */

View file

@ -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<String, CourseData> 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<String> quizIds,
final Map<String, CourseData> 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<MoodleAPIRestTemplate> getRestTemplate() {
if (this.restTemplate == null) {
final Result<MoodleAPIRestTemplate> templateRequest = this.restTemplateFactory

View file

@ -137,6 +137,7 @@ class ExamUpdateHandler {
try {
final Exam exam = exams.get(quiz.id);
if (hasChanges(exam, quiz)) {
final Result<QuizData> updateQuizData = this.examDAO