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.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -178,6 +177,15 @@ public abstract class MoodleUtils {
final CourseData courseData, final CourseData courseData,
final String uriPrefix, final String uriPrefix,
final boolean prependShortCourseName) { 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<>(); final Map<String, String> additionalAttrs = new HashMap<>();
additionalAttrs.put(QuizData.ATTR_ADDITIONAL_CREATION_TIME, String.valueOf(courseData.time_created)); 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)); additionalAttrs.put(QuizData.ATTR_ADDITIONAL_TIME_LIMIT, String.valueOf(courseQuizData.time_limit));
return new QuizData( return new QuizData(
MoodleUtils.getInternalQuizId( MoodleUtils.getInternalQuizId(
courseQuizData.id, (useQuizId) ? courseQuizData.id : courseQuizData.course_module,
courseData.id, courseData.id,
courseData.short_name, courseData.short_name,
courseData.idnumber), courseData.idnumber),
@ -219,27 +227,6 @@ public abstract class MoodleUtils {
return courseAndQuiz; 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 --- // ---- Mapping Classes ---
/** Maps the Moodle course API course data */ /** 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;
import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.CourseData; 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.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.CourseQuizData;
import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.Courses; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.Courses;
import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.MoodleUserDetails; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.moodle.MoodleUtils.MoodleUserDetails;
@ -262,7 +263,7 @@ public class MoodleCourseAccess implements CourseAccessAPI {
courseData.values().stream() courseData.values().stream()
.filter(c -> !c.quizzes.isEmpty()) .filter(c -> !c.quizzes.isEmpty())
.forEach(c -> asyncQuizFetchBuffer.buffer.addAll( .forEach(c -> asyncQuizFetchBuffer.buffer.addAll(
MoodleUtils.quizDataOf(lmsSetup, c, urlPrefix, this.prependShortCourseName) MoodleUtils.quizDataOf(lmsSetup, c, urlPrefix, this.prependShortCourseName, false)
.stream() .stream()
.filter(LmsAPIService.quizFilterPredicate(filterMap)) .filter(LmsAPIService.quizFilterPredicate(filterMap))
.collect(Collectors.toList()))); .collect(Collectors.toList())));
@ -392,7 +393,7 @@ public class MoodleCourseAccess implements CourseAccessAPI {
lmsSetup, lmsSetup,
c, c,
urlPrefix, urlPrefix,
this.prependShortCourseName).stream()) this.prependShortCourseName, false).stream())
.filter(q -> exam.name.contains(q.name)) .filter(q -> exam.name.contains(q.name))
.findFirst() .findFirst()
.get(); .get();
@ -600,7 +601,7 @@ public class MoodleCourseAccess implements CourseAccessAPI {
final Map<String, CourseData> finalCourseDataRef = courseData; final Map<String, CourseData> finalCourseDataRef = courseData;
courseQuizData.quizzes courseQuizData.quizzes
.stream() .stream()
.forEach(quiz -> MoodleUtils.fillSelectedQuizzes(quizIds, finalCourseDataRef, quiz)); .forEach(quiz -> fillSelectedQuizzes(quizIds, finalCourseDataRef, quiz));
final String urlPrefix = (lmsSetup.lmsApiUrl.endsWith(Constants.URL_PATH_SEPARATOR)) final String urlPrefix = (lmsSetup.lmsApiUrl.endsWith(Constants.URL_PATH_SEPARATOR))
? lmsSetup.lmsApiUrl + MOODLE_QUIZ_START_URL_PATH ? lmsSetup.lmsApiUrl + MOODLE_QUIZ_START_URL_PATH
@ -613,7 +614,8 @@ public class MoodleCourseAccess implements CourseAccessAPI {
lmsSetup, lmsSetup,
cd, cd,
urlPrefix, urlPrefix,
this.prependShortCourseName).stream()) this.prependShortCourseName,
false).stream())
.collect(Collectors.toList()); .collect(Collectors.toList());
} catch (final Exception e) { } catch (final Exception e) {
@ -698,7 +700,7 @@ public class MoodleCourseAccess implements CourseAccessAPI {
final String startURI = uriPrefix + courseQuizData.course_module; final String startURI = uriPrefix + courseQuizData.course_module;
return new QuizData( return new QuizData(
MoodleUtils.getInternalQuizId( MoodleUtils.getInternalQuizId(
courseQuizData.course_module, courseQuizData.course_module, // TODO this is wrong should be id. Create recovery task
courseData.id, courseData.id,
courseData.short_name, courseData.short_name,
courseData.idnumber), courseData.idnumber),
@ -719,6 +721,27 @@ public class MoodleCourseAccess implements CourseAccessAPI {
additionalAttrs); 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() { private Result<MoodleAPIRestTemplate> getRestTemplate() {
if (this.restTemplate == null) { if (this.restTemplate == null) {
final Result<MoodleAPIRestTemplate> templateRequest = this.restTemplateFactory final Result<MoodleAPIRestTemplate> templateRequest = this.restTemplateFactory

View file

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