From f5fa69734e141af2703347fdc949a8216238658f Mon Sep 17 00:00:00 2001 From: Johannes Brandenburger <79154528+johannesbrandenburger@users.noreply.github.com> Date: Sun, 12 May 2024 10:28:22 +0200 Subject: [PATCH 1/7] change course backen --- .../mobilelearning/services/MaintService.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/MaintService.java b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/MaintService.java index eceb53b..07d1213 100644 --- a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/MaintService.java +++ b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/MaintService.java @@ -760,4 +760,40 @@ public Response deleteQuizQuestion(@RestPath String courseId, @RestPath String f return Response.ok().build(); } + static class ChangeCourseOfFormRequest { + public String newCourseId; + } + + @PUT + @Produces(MediaType.APPLICATION_JSON) + @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) + @Path("/course/{courseId}/form/{formId}/change-course") + public Boolean changeCourseOfForm(@RestPath String courseId, @RestPath String formId, ChangeCourseOfFormRequest request) { + Course course = courseRepository.findById(new ObjectId(courseId)); + if (!isOwner(course)) { + throw new NotFoundException(); + } + Course newCourse = courseRepository.findById(new ObjectId(request.newCourseId)); + if (!isOwner(newCourse)) { + throw new NotFoundException(); + } + FeedbackForm feedbackForm = course.getFeedbackFormById(new ObjectId(formId)); + if (feedbackForm == null) { + QuizForm quizForm = course.getQuizFormById(new ObjectId(formId)); + if (quizForm == null) { + throw new NotFoundException(); + } + course.removeQuizForm(quizForm); + newCourse.addQuizForm(quizForm); + } else { + course.removeFeedbackForm(feedbackForm); + newCourse.addFeedbackForm(feedbackForm); + } + course.wasUpdated(); + newCourse.wasUpdated(); + courseRepository.update(course); + courseRepository.update(newCourse); + return true; + } + } From 52581e1b9ea98e4e430aed755bdc434ddbd161c3 Mon Sep 17 00:00:00 2001 From: Johannes Brandenburger <79154528+johannesbrandenburger@users.noreply.github.com> Date: Mon, 13 May 2024 16:12:36 +0200 Subject: [PATCH 2/7] adjust copy of questions --- .../models/feedback/FeedbackQuestion.java | 12 ++ .../models/quiz/QuizQuestion.java | 4 + .../mobilelearning/services/MaintService.java | 204 ++++++++++++------ 3 files changed, 159 insertions(+), 61 deletions(-) diff --git a/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/feedback/FeedbackQuestion.java b/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/feedback/FeedbackQuestion.java index 19592a3..4b37e36 100644 --- a/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/feedback/FeedbackQuestion.java +++ b/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/feedback/FeedbackQuestion.java @@ -38,4 +38,16 @@ public void setRangeLow(String rangeLow) { public void setRangeHigh(String rangeHigh) { this.rangeHigh = rangeHigh; } + + public FeedbackQuestionType getType() { + return this.type; + } + + public String getRangeLow() { + return this.rangeLow; + } + + public String getRangeHigh() { + return this.rangeHigh; + } } diff --git a/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/quiz/QuizQuestion.java b/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/quiz/QuizQuestion.java index 37ac205..e58e99f 100644 --- a/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/quiz/QuizQuestion.java +++ b/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/quiz/QuizQuestion.java @@ -58,4 +58,8 @@ public Integer checkAnswer(List answer) { public void setType(QuizQuestionType type) { this.type = type; } + + public QuizQuestionType getType() { + return this.type; + } } diff --git a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/MaintService.java b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/MaintService.java index 07d1213..a264c46 100644 --- a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/MaintService.java +++ b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/MaintService.java @@ -1,7 +1,7 @@ package de.htwg_konstanz.mobilelearning.services; - import java.util.ArrayList; +import java.util.Date; import java.util.List; import org.bson.types.ObjectId; @@ -14,6 +14,7 @@ import de.htwg_konstanz.mobilelearning.helper.moodle.MoodleCourse; import de.htwg_konstanz.mobilelearning.helper.moodle.MoodleInterface; import de.htwg_konstanz.mobilelearning.models.Course; +import de.htwg_konstanz.mobilelearning.models.Form; import de.htwg_konstanz.mobilelearning.models.Question; import de.htwg_konstanz.mobilelearning.models.QuestionWrapper; import de.htwg_konstanz.mobilelearning.models.auth.User; @@ -42,29 +43,49 @@ * * * // ENDPOINTS: - - // listCourses(); -> Name, Description - - // getCourse(params.courseId); -> Name, Description, MoodleCourseId, FeedbackForms (Name, Description), QuizForms (Name, Description) - // updateCourse(params.courseId, courseName, courseDescription, courseMoodleCourseId); -> Error | Name, Description, MoodleCourseId, FeedbackForms (Name, Description), QuizForms (Name, Description) - // addCourse(courseName, courseDescription); -> Error | Name, Description, MoodleCourseId, FeedbackForms (Name, Description), QuizForms (Name, Description) - // deleteCourse(params.courseId); -> Error | Success - - // getFeedbackForm(params.courseId, params.formId); -> Error | Name, Description, Questions (Name, Description, Type, Options, RangeLow, RangeHigh) - // updateFeedbackForm(params.courseId, params.formId, feedbackformName, feedbackformDescription) -> Error | Name, Description, Questions (Name, Description, Type, Options, RangeLow, RangeHigh) - // addFeedbackForm(params.courseId, feedbackformName, feedbackformDescription) -> Error | Name, Description, Questions (Name, Description, Type, Options, RangeLow, RangeHigh) - // deleteFeedbackForm(params.courseId, params.formId) -> Error | Success - - // getFeedbackQuestion(params.courseId, params.formId, params.questionId); -> Error | Name, Description, Type, Options, RangeLow, RangeHigh - // updateFeedbackQuestion(params.courseId, params.formId, params.questionId, feedbackQuestion?.name, feedbackQuestion?.description, feedbackQuestion?.type, feedbackQuestion?.options, feedbackQuestion?.rangeLow, feedbackQuestion?.rangeHigh) -> Error | Name, Description, Type, Options, RangeLow, RangeHigh - // addFeedbackQuestion(params.courseId, params.formId, feedbackQuestion?.name, feedbackQuestion?.description, feedbackQuestion?.type, feedbackQuestion?.options, feedbackQuestion?.rangeLow, feedbackQuestion?.rangeHigh) -> Error | Name, Description, Type, Options, RangeLow, RangeHigh - // deleteFeedbackQuestion(params.courseId, params.formId, params.questionId) -> Error | Success - + * + * // listCourses(); -> Name, Description + * + * // getCourse(params.courseId); -> Name, Description, MoodleCourseId, + * FeedbackForms (Name, Description), QuizForms (Name, Description) + * // updateCourse(params.courseId, courseName, courseDescription, + * courseMoodleCourseId); -> Error | Name, Description, MoodleCourseId, + * FeedbackForms (Name, Description), QuizForms (Name, Description) + * // addCourse(courseName, courseDescription); -> Error | Name, Description, + * MoodleCourseId, FeedbackForms (Name, Description), QuizForms (Name, + * Description) + * // deleteCourse(params.courseId); -> Error | Success + * + * // getFeedbackForm(params.courseId, params.formId); -> Error | Name, + * Description, Questions (Name, Description, Type, Options, RangeLow, + * RangeHigh) + * // updateFeedbackForm(params.courseId, params.formId, feedbackformName, + * feedbackformDescription) -> Error | Name, Description, Questions (Name, + * Description, Type, Options, RangeLow, RangeHigh) + * // addFeedbackForm(params.courseId, feedbackformName, + * feedbackformDescription) -> Error | Name, Description, Questions (Name, + * Description, Type, Options, RangeLow, RangeHigh) + * // deleteFeedbackForm(params.courseId, params.formId) -> Error | Success + * + * // getFeedbackQuestion(params.courseId, params.formId, params.questionId); -> + * Error | Name, Description, Type, Options, RangeLow, RangeHigh + * // updateFeedbackQuestion(params.courseId, params.formId, params.questionId, + * feedbackQuestion?.name, feedbackQuestion?.description, + * feedbackQuestion?.type, feedbackQuestion?.options, + * feedbackQuestion?.rangeLow, feedbackQuestion?.rangeHigh) -> Error | Name, + * Description, Type, Options, RangeLow, RangeHigh + * // addFeedbackQuestion(params.courseId, params.formId, + * feedbackQuestion?.name, feedbackQuestion?.description, + * feedbackQuestion?.type, feedbackQuestion?.options, + * feedbackQuestion?.rangeLow, feedbackQuestion?.rangeHigh) -> Error | Name, + * Description, Type, Options, RangeLow, RangeHigh + * // deleteFeedbackQuestion(params.courseId, params.formId, params.questionId) + * -> Error | Success + * */ @Path("/maint") public class MaintService { - @Inject private CourseRepository courseRepository; @@ -85,12 +106,12 @@ private Boolean isOwner(Course course) { @Path("/courses") public List listCourses() { List courses = courseRepository - .findAll() - .list() - .stream() - .filter(course -> isOwner(course)) - .sorted((c1, c2) -> -c1.getLastModified().compareTo(c2.getLastModified())) - .toList(); + .findAll() + .list() + .stream() + .filter(course -> isOwner(course)) + .sorted((c1, c2) -> -c1.getLastModified().compareTo(c2.getLastModified())) + .toList(); List coursesWithLessData = courses.stream().map(course -> { Course courseWithLessData = new Course(); courseWithLessData.setId(course.getId()); @@ -142,7 +163,7 @@ public Course getCourse(@RestPath String courseId) { static class UpdateCourseRequest { public String name; public String description; - public String moodleCourseId; + public String moodleCourseId; } @PUT @@ -166,7 +187,7 @@ public Course updateCourse(@RestPath String courseId, UpdateCourseRequest reques static class AddCourseRequest { public String name; public String description; - public String moodleCourseId; + public String moodleCourseId; } @POST @@ -207,7 +228,7 @@ public FeedbackForm getFeedbackForm(@RestPath String courseId, @RestPath String if (feedbackForm == null) { throw new NotFoundException(); } - FeedbackForm feedbackFormWithQuestionContents =feedbackForm.copyWithQuestionContents(course); + FeedbackForm feedbackFormWithQuestionContents = feedbackForm.copyWithQuestionContents(course); return feedbackFormWithQuestionContents; } @@ -220,7 +241,8 @@ static class UpdateFeedbackFormRequest { @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/feedback/form/{formId}") - public FeedbackForm updateFeedbackForm(@RestPath String courseId, @RestPath String formId, UpdateFeedbackFormRequest request) { + public FeedbackForm updateFeedbackForm(@RestPath String courseId, @RestPath String formId, + UpdateFeedbackFormRequest request) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -237,8 +259,11 @@ public FeedbackForm updateFeedbackForm(@RestPath String courseId, @RestPath Stri return getFeedbackForm(courseId, formId); } - // PUT /maint/course/${courseId}/feedback/form/${formId}/reorder ({String[] questionIds}) - // reorderFeedbackFormQuestions(params.courseId, params.formId, questionIds) -> Error | Name, Description, Questions (Name, Description, Type, Options, RangeLow, RangeHigh) + // PUT /maint/course/${courseId}/feedback/form/${formId}/reorder ({String[] + // questionIds}) + // reorderFeedbackFormQuestions(params.courseId, params.formId, questionIds) -> + // Error | Name, Description, Questions (Name, Description, Type, Options, + // RangeLow, RangeHigh) static class ReorderFeedbackFormQuestionsRequest { public List questionIds; } @@ -247,7 +272,8 @@ static class ReorderFeedbackFormQuestionsRequest { @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/feedback/form/{formId}/reorder") - public FeedbackForm reorderFeedbackFormQuestions(@RestPath String courseId, @RestPath String formId, ReorderFeedbackFormQuestionsRequest request) { + public FeedbackForm reorderFeedbackFormQuestions(@RestPath String courseId, @RestPath String formId, + ReorderFeedbackFormQuestionsRequest request) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -285,7 +311,8 @@ public FeedbackForm addFeedbackForm(@RestPath String courseId, AddFeedbackFormRe if (!isOwner(course)) { throw new NotFoundException(); } - FeedbackForm feedbackForm = new FeedbackForm(course.getId(), request.name, request.description, new ArrayList<>(), FormStatus.NOT_STARTED); + FeedbackForm feedbackForm = new FeedbackForm(course.getId(), request.name, request.description, + new ArrayList<>(), FormStatus.NOT_STARTED); course.addFeedbackForm(feedbackForm); course.wasUpdated(); courseRepository.update(course); @@ -305,7 +332,8 @@ public FeedbackForm copyFeedbackForm(@RestPath String courseId, @RestPath String if (feedbackForm == null) { throw new NotFoundException(); } - FeedbackForm copiedFeedbackForm = new FeedbackForm(course.getId(), feedbackForm.getName() + (" (Copy)"), feedbackForm.getDescription(), new ArrayList<>(), FormStatus.NOT_STARTED); + FeedbackForm copiedFeedbackForm = new FeedbackForm(course.getId(), feedbackForm.getName() + (" (Copy)"), + feedbackForm.getDescription(), new ArrayList<>(), FormStatus.NOT_STARTED); // TODO: check if this is the correct way to copy questions (sync or copy) for (QuestionWrapper questionWrapper : feedbackForm.getQuestions()) { @@ -343,7 +371,8 @@ public Response deleteFeedbackForm(@RestPath String courseId, @RestPath String f @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/feedback/form/{formId}/question/{questionId}") - public QuestionWrapper getFeedbackQuestion(@RestPath String courseId, @RestPath String formId, @RestPath String questionId) { + public QuestionWrapper getFeedbackQuestion(@RestPath String courseId, @RestPath String formId, + @RestPath String questionId) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -377,7 +406,8 @@ static class UpdateFeedbackQuestionRequest { @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/feedback/form/{formId}/question/{questionId}") - public QuestionWrapper updateFeedbackQuestion(@RestPath String courseId, @RestPath String formId, @RestPath String questionId, UpdateFeedbackQuestionRequest request) { + public QuestionWrapper updateFeedbackQuestion(@RestPath String courseId, @RestPath String formId, + @RestPath String questionId, UpdateFeedbackQuestionRequest request) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -417,12 +447,13 @@ static class AddFeedbackQuestionRequest { public String rangeLow; public String rangeHigh; } - + @POST @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/feedback/form/{formId}/question") - public QuestionWrapper addFeedbackQuestion(@RestPath String courseId, @RestPath String formId, AddFeedbackQuestionRequest request) { + public QuestionWrapper addFeedbackQuestion(@RestPath String courseId, @RestPath String formId, + AddFeedbackQuestionRequest request) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -431,10 +462,11 @@ public QuestionWrapper addFeedbackQuestion(@RestPath String courseId, @RestPath if (feedbackForm == null) { throw new NotFoundException(); } - + try { FeedbackQuestionType questionType = FeedbackQuestionType.valueOf(request.type); - FeedbackQuestion feedbackQuestion = new FeedbackQuestion(request.name, request.description, questionType, request.options, "", request.rangeLow, request.rangeHigh); + FeedbackQuestion feedbackQuestion = new FeedbackQuestion(request.name, request.description, questionType, + request.options, "", request.rangeLow, request.rangeHigh); course.addFeedbackQuestion(feedbackQuestion); QuestionWrapper questionWrapper = new QuestionWrapper(feedbackQuestion.getId(), new ArrayList<>()); feedbackForm.addQuestion(questionWrapper); @@ -453,7 +485,8 @@ public QuestionWrapper addFeedbackQuestion(@RestPath String courseId, @RestPath @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/feedback/form/{formId}/question/{questionId}") - public Response deleteFeedbackQuestion(@RestPath String courseId, @RestPath String formId, @RestPath String questionId) { + public Response deleteFeedbackQuestion(@RestPath String courseId, @RestPath String formId, + @RestPath String questionId) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -478,7 +511,6 @@ public Response deleteFeedbackQuestion(@RestPath String courseId, @RestPath Stri return Response.ok().build(); } - // NOW THE SAME FOR QUIZ FORMS @GET @Produces(MediaType.APPLICATION_JSON) @@ -493,7 +525,8 @@ public QuizForm getQuizForm(@RestPath String courseId, @RestPath String formId) if (quizForm == null) { throw new NotFoundException(); } - QuizForm quizFormWithQuestionContents = quizForm.copyWithoutResultsAndParticipantsButWithQuestionContents(course); + QuizForm quizFormWithQuestionContents = quizForm + .copyWithoutResultsAndParticipantsButWithQuestionContents(course); return quizFormWithQuestionContents; } @@ -531,7 +564,8 @@ static class ReorderQuizFormQuestionsRequest { @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/quiz/form/{formId}/reorder") - public QuizForm reorderQuizFormQuestions(@RestPath String courseId, @RestPath String formId, ReorderQuizFormQuestionsRequest request) { + public QuizForm reorderQuizFormQuestions(@RestPath String courseId, @RestPath String formId, + ReorderQuizFormQuestionsRequest request) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -569,7 +603,8 @@ public QuizForm addQuizForm(@RestPath String courseId, AddQuizFormRequest reques if (!isOwner(course)) { throw new NotFoundException(); } - QuizForm quizForm = new QuizForm(course.getId(), request.name, request.description, new ArrayList<>(), FormStatus.NOT_STARTED, 0, false); + QuizForm quizForm = new QuizForm(course.getId(), request.name, request.description, new ArrayList<>(), + FormStatus.NOT_STARTED, 0, false); course.addQuizForm(quizForm); course.wasUpdated(); courseRepository.update(course); @@ -589,9 +624,10 @@ public QuizForm copyQuizForm(@RestPath String courseId, @RestPath String formId) if (quizForm == null) { throw new NotFoundException(); } - QuizForm copiedQuizForm = new QuizForm(course.getId(), quizForm.getName() + (" (Copy)"), quizForm.getDescription(), new ArrayList<>(), FormStatus.NOT_STARTED, 0, false); + QuizForm copiedQuizForm = new QuizForm(course.getId(), quizForm.getName() + (" (Copy)"), + quizForm.getDescription(), new ArrayList<>(), FormStatus.NOT_STARTED, 0, false); - // TODO: check if this is the correct way to copy questions (sync or copy) + // TODO: check if this is the correct way to copy questions (sync or copy) for (QuestionWrapper questionWrapper : quizForm.getQuestions()) { QuizQuestion quizQuestion = course.getQuizQuestionById(questionWrapper.getQuestionId()); copiedQuizForm.addQuestion(new QuestionWrapper(quizQuestion.getId(), new ArrayList<>())); @@ -629,7 +665,8 @@ public Response deleteQuizForm(@RestPath String courseId, @RestPath String formI @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/quiz/form/{formId}/question/{questionId}") - public QuestionWrapper getQuizQuestion(@RestPath String courseId, @RestPath String formId, @RestPath String questionId) { + public QuestionWrapper getQuizQuestion(@RestPath String courseId, @RestPath String formId, + @RestPath String questionId) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -663,7 +700,8 @@ static class UpdateQuizQuestionRequest { @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/quiz/form/{formId}/question/{questionId}") - public QuestionWrapper updateQuizQuestion(@RestPath String courseId, @RestPath String formId, @RestPath String questionId, UpdateQuizQuestionRequest request) { + public QuestionWrapper updateQuizQuestion(@RestPath String courseId, @RestPath String formId, + @RestPath String questionId, UpdateQuizQuestionRequest request) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -706,7 +744,8 @@ static class AddQuizQuestionRequest { @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/quiz/form/{formId}/question") - public QuestionWrapper addQuizQuestion(@RestPath String courseId, @RestPath String formId, AddQuizQuestionRequest request) { + public QuestionWrapper addQuizQuestion(@RestPath String courseId, @RestPath String formId, + AddQuizQuestionRequest request) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -717,7 +756,8 @@ public QuestionWrapper addQuizQuestion(@RestPath String courseId, @RestPath Stri } try { QuizQuestionType questionType = QuizQuestionType.valueOf(request.type); - QuizQuestion quizQuestion = new QuizQuestion(request.name, request.description, questionType, request.options, request.hasCorrectAnswers, request.correctAnswers, ""); + QuizQuestion quizQuestion = new QuizQuestion(request.name, request.description, questionType, + request.options, request.hasCorrectAnswers, request.correctAnswers, ""); course.addQuizQuestion(quizQuestion); QuestionWrapper questionWrapper = new QuestionWrapper(quizQuestion.getId(), new ArrayList<>()); quizForm.addQuestion(questionWrapper); @@ -735,7 +775,8 @@ public QuestionWrapper addQuizQuestion(@RestPath String courseId, @RestPath Stri @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/quiz/form/{formId}/question/{questionId}") - public Response deleteQuizQuestion(@RestPath String courseId, @RestPath String formId, @RestPath String questionId) { + public Response deleteQuizQuestion(@RestPath String courseId, @RestPath String formId, + @RestPath String questionId) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -768,7 +809,8 @@ static class ChangeCourseOfFormRequest { @Produces(MediaType.APPLICATION_JSON) @RolesAllowed({ UserRole.PROF, UserRole.STUDENT }) @Path("/course/{courseId}/form/{formId}/change-course") - public Boolean changeCourseOfForm(@RestPath String courseId, @RestPath String formId, ChangeCourseOfFormRequest request) { + public Boolean changeCourseOfForm(@RestPath String courseId, @RestPath String formId, + ChangeCourseOfFormRequest request) { Course course = courseRepository.findById(new ObjectId(courseId)); if (!isOwner(course)) { throw new NotFoundException(); @@ -777,19 +819,59 @@ public Boolean changeCourseOfForm(@RestPath String courseId, @RestPath String fo if (!isOwner(newCourse)) { throw new NotFoundException(); } - FeedbackForm feedbackForm = course.getFeedbackFormById(new ObjectId(formId)); - if (feedbackForm == null) { - QuizForm quizForm = course.getQuizFormById(new ObjectId(formId)); - if (quizForm == null) { - throw new NotFoundException(); - } + + Form form = course.getFormById(new ObjectId(formId)); + if (form == null) { + throw new NotFoundException(); + } + + if (form instanceof QuizForm) { + QuizForm quizForm = (QuizForm) form; course.removeQuizForm(quizForm); + + // copy the question contents + List questions = new ArrayList<>(); + for (QuestionWrapper questionWrapper : quizForm.getQuestions()) { + QuizQuestion quizQuestion = course.getQuizQuestionById(questionWrapper.getQuestionId()); + QuizQuestion copiedQuizQuestion = new QuizQuestion( + quizQuestion.getName(), + quizQuestion.getDescription(), + quizQuestion.getType(), + quizQuestion.getOptions(), + quizQuestion.getHasCorrectAnswers(), + quizQuestion.getCorrectAnswers(), + ""); + newCourse.addQuizQuestion(copiedQuizQuestion); + questions.add(new QuestionWrapper(copiedQuizQuestion.getId(), new ArrayList<>())); + } + + quizForm.setQuestions(questions); newCourse.addQuizForm(quizForm); } else { + + FeedbackForm feedbackForm = (FeedbackForm) form; course.removeFeedbackForm(feedbackForm); + + // copy the question contents + List questions = new ArrayList<>(); + for (QuestionWrapper questionWrapper : feedbackForm.getQuestions()) { + FeedbackQuestion feedbackQuestion = course.getFeedbackQuestionById(questionWrapper.getQuestionId()); + FeedbackQuestion copiedFeedbackQuestion = new FeedbackQuestion( + feedbackQuestion.getName(), + feedbackQuestion.getDescription(), + feedbackQuestion.getType(), + feedbackQuestion.getOptions(), + "", + feedbackQuestion.getRangeLow(), + feedbackQuestion.getRangeHigh()); + newCourse.addFeedbackQuestion(copiedFeedbackQuestion); + questions.add(new QuestionWrapper(copiedFeedbackQuestion.getId(), new ArrayList<>())); + } + feedbackForm.setQuestions(questions); newCourse.addFeedbackForm(feedbackForm); } - course.wasUpdated(); + Date oneSecondAgo = new Date(System.currentTimeMillis() - 1000); + course.setLastModified(oneSecondAgo); newCourse.wasUpdated(); courseRepository.update(course); courseRepository.update(newCourse); From 027f2e15e929b633d39f21007d53a0a76754986f Mon Sep 17 00:00:00 2001 From: Johannes Brandenburger <79154528+johannesbrandenburger@users.noreply.github.com> Date: Mon, 13 May 2024 16:12:57 +0200 Subject: [PATCH 3/7] frontend: copy feedbackform --- .../[formId]/change-course/page.tsx | 113 ++++++++++++++++++ .../[courseId]/feedbackform/[formId]/page.tsx | 7 ++ inputtool/lib/requests.ts | 14 +++ 3 files changed, 134 insertions(+) create mode 100644 inputtool/app/courses/[courseId]/feedbackform/[formId]/change-course/page.tsx diff --git a/inputtool/app/courses/[courseId]/feedbackform/[formId]/change-course/page.tsx b/inputtool/app/courses/[courseId]/feedbackform/[formId]/change-course/page.tsx new file mode 100644 index 0000000..9b704aa --- /dev/null +++ b/inputtool/app/courses/[courseId]/feedbackform/[formId]/change-course/page.tsx @@ -0,0 +1,113 @@ +"use client" + +import { Button } from "@/components/ui/button"; +import { Card, CardContent } from "@/components/ui/card"; +import { Label } from "@radix-ui/react-dropdown-menu"; +import { useRouter } from 'next/navigation' +import { useState, useEffect } from "react"; +import { toast } from "sonner"; +import { CircleArrowLeft, Loader2 } from 'lucide-react'; +import { hasValidJwtToken } from "@/lib/utils"; +import * as React from "react" +import { Course, FeedbackForm, QuizForm } from "@/lib/models"; +import { changeCourseOfForm, fetchFeedbackForm, listCourses } from "@/lib/requests"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select" + + +export default function ChangeCoursePage({ params }: { params: { courseId: string, formId: string } }) { + + const router = useRouter(); + const [loading, setLoading] = useState(true); + const [form, setForm] = useState({ + id: "", + name: "", + key: "", + type: "Feedback", + description: "", + questions: [], + lastModified: new Date(), + }); + const [courses, setCourses] = useState([]); + useEffect(() => { + const loadData = async () => { + setLoading(true); + let courses = await listCourses(); + let form = await fetchFeedbackForm(params.courseId, params.formId); + console.log(form); + setCourses(courses); + form && setForm(form); + setLoading(false); + }; + loadData(); + }, [params.courseId, params.formId]); + + useEffect(() => { + hasValidJwtToken().then((isValid) => { + if (!isValid) router.push("/"); + }); + }, [router]); + + return ( +
+ + {loading && ( + + )} + + {!loading && ( + <> +
+ +
+ +
+ +
+

+ Form: {form.name} +

+ + + + + + + + )} +
+ ); +} \ No newline at end of file diff --git a/inputtool/app/courses/[courseId]/feedbackform/[formId]/page.tsx b/inputtool/app/courses/[courseId]/feedbackform/[formId]/page.tsx index fa28467..111c839 100644 --- a/inputtool/app/courses/[courseId]/feedbackform/[formId]/page.tsx +++ b/inputtool/app/courses/[courseId]/feedbackform/[formId]/page.tsx @@ -196,6 +196,13 @@ export default function FeedbackFormPage({ params }: { params: { courseId: strin }} >
+ +
+ +
+ +
+

+ Form: {form.name} +

+ + + + + + + + )} + + ); +} \ No newline at end of file diff --git a/inputtool/app/courses/[courseId]/quizform/[formId]/page.tsx b/inputtool/app/courses/[courseId]/quizform/[formId]/page.tsx index 6003db8..8ace3e1 100644 --- a/inputtool/app/courses/[courseId]/quizform/[formId]/page.tsx +++ b/inputtool/app/courses/[courseId]/quizform/[formId]/page.tsx @@ -63,7 +63,7 @@ export default function QuizFormPage({ params }: { params: { courseId: string, f } else { toast.error("Failed to save."); } - } + } const result = await updateQuizForm(params.courseId, params.formId, quizform.name, quizform.description); if (result) { @@ -193,6 +193,13 @@ export default function QuizFormPage({ params }: { params: { courseId: string, f }} >
+
- + {/* NOT YET SUPPORTED */} + {/*
Fulltext
-
+
*/}