From 6894f6d74fd5df3ba32127d26b66c9672e7bdd8b Mon Sep 17 00:00:00 2001 From: Mats Eikeland Mollestad Date: Tue, 11 Feb 2020 12:30:30 +0100 Subject: [PATCH 1/5] [MultipleChoiseTask] Returned more info when rendering a task --- .../MultipleChoiseTask+RenderPracticing.swift | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/Sources/App/Task/Multiple Choise Task/MultipleChoiseTask+RenderPracticing.swift b/Sources/App/Task/Multiple Choise Task/MultipleChoiseTask+RenderPracticing.swift index f714528..6905cd2 100644 --- a/Sources/App/Task/Multiple Choise Task/MultipleChoiseTask+RenderPracticing.swift +++ b/Sources/App/Task/Multiple Choise Task/MultipleChoiseTask+RenderPracticing.swift @@ -23,21 +23,27 @@ extension MultipleChoiseTask: RenderTaskPracticing { try TaskResult.DatabaseRepository .getLastResult(for: preview.task.requireID(), by: user.userId, on: req) - .map { lastResult in + .flatMap { lastResult in - try req.renderer() - .render( - MultipleChoiseTask.Templates.Execute.self, - with: .init( - multiple: content, - taskContent: preview, - user: user, - currentTaskIndex: index, - session: session, - lastResult: lastResult?.content, - practiceProgress: progress + if lastResult != nil { + TaskResult. + } else { + return req.future().map { + try req.renderer() + .render( + MultipleChoiseTask.Templates.Execute.self, + with: .init( + multiple: content, + taskContent: preview, + user: user, + currentTaskIndex: index, + session: session, + lastResult: lastResult?.content, + practiceProgress: progress + ) ) - ) + } + } } } } From 8db4f9c3af8a31b671a093f495ee0e25f2b79426 Mon Sep 17 00:00:00 2001 From: Mats Eikeland Mollestad Date: Tue, 11 Feb 2020 15:15:27 +0100 Subject: [PATCH 2/5] [MultipleChoise] Fetching more data that should be presented --- .../MultipleChoiseTask+RenderPracticing.swift | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Sources/App/Task/Multiple Choise Task/MultipleChoiseTask+RenderPracticing.swift b/Sources/App/Task/Multiple Choise Task/MultipleChoiseTask+RenderPracticing.swift index 6905cd2..c39abdd 100644 --- a/Sources/App/Task/Multiple Choise Task/MultipleChoiseTask+RenderPracticing.swift +++ b/Sources/App/Task/Multiple Choise Task/MultipleChoiseTask+RenderPracticing.swift @@ -26,7 +26,25 @@ extension MultipleChoiseTask: RenderTaskPracticing { .flatMap { lastResult in if lastResult != nil { - TaskResult. + return try TaskSessionAnswer.DatabaseRepository + .multipleChoiseAnswers(in: session.requireID(), taskID: content.task.requireID(), on: req) + .map { selectedChoises in + + try req.renderer() + .render( + MultipleChoiseTask.Templates.Execute.self, + with: .init( + multiple: content, + taskContent: preview, + user: user, + currentTaskIndex: index, + session: session, + lastResult: lastResult?.content, + practiceProgress: progress, + selectedChoises: selectedChoises.map { $0.choiseID } + ) + ) + } } else { return req.future().map { try req.renderer() From df3b171d2352e3389b43cabbcdcdb1a5ba6d8883 Mon Sep 17 00:00:00 2001 From: Mats Eikeland Mollestad Date: Tue, 11 Feb 2020 15:30:50 +0100 Subject: [PATCH 3/5] [FlashCard] Presenting given answers when going back to a task --- .../FlashCardTask+RenderPracticing.swift | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/Sources/App/Task/Flash Card/FlashCardTask+RenderPracticing.swift b/Sources/App/Task/Flash Card/FlashCardTask+RenderPracticing.swift index d646f01..47b1a67 100644 --- a/Sources/App/Task/Flash Card/FlashCardTask+RenderPracticing.swift +++ b/Sources/App/Task/Flash Card/FlashCardTask+RenderPracticing.swift @@ -17,27 +17,32 @@ extension FlashCardTask: RenderTaskPracticing { .content(for: self, on: req) .flatMap { preview in - try PracticeSession.DatabaseRepository - .goalProgress(in: session, on: req) - .flatMap { progress in + try TaskSessionAnswer.DatabaseRepository + .flashCardAnswers(in: session.requireID(), taskID: preview.task.requireID(), on: req) + .flatMap { answer in - try TaskResult.DatabaseRepository - .getLastResult(for: preview.task.requireID(), by: user.userId, on: req) - .map { lastResult in + try PracticeSession.DatabaseRepository + .goalProgress(in: session, on: req) + .flatMap { progress in - try req.renderer() - .render( - FlashCardTask.Templates.Execute.self, - with: .init( - taskPreview: preview, - user: user, - currentTaskIndex: index, - practiceProgress: progress, - session: session, - lastResult: lastResult?.content, - numberOfTasks: 0 + try TaskResult.DatabaseRepository + .getLastResult(for: preview.task.requireID(), by: user.userId, on: req) + .map { lastResult in + + try req.renderer() + .render( + FlashCardTask.Templates.Execute.self, + with: .init( + taskPreview: preview, + user: user, + currentTaskIndex: index, + practiceProgress: progress, + session: session, + lastResult: lastResult?.content, + prevAnswer: answer + ) ) - ) + } } } } From 8cd62b1b5048126e4e7c71d290e95768dc2629c5 Mon Sep 17 00:00:00 2001 From: eskildbr Date: Wed, 12 Feb 2020 14:19:36 +0100 Subject: [PATCH 4/5] [feature] Added web controller for user profile --- Sources/App/User/UserWebController.swift | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Sources/App/User/UserWebController.swift b/Sources/App/User/UserWebController.swift index 6a9180f..15ee779 100644 --- a/Sources/App/User/UserWebController.swift +++ b/Sources/App/User/UserWebController.swift @@ -12,11 +12,16 @@ import KognitaViews import Mailgun import FluentPostgreSQL import KognitaAPI +import Authentication final class UserWebController: RouteCollection { func boot(router: Router) { + let redirectMiddle = router.grouped(RedirectMiddleware(path: "/login")) + + redirectMiddle.get("profile", use: profilePage) + router.get("signup", use: signupForm) router.get("login", use: loginForm) router.get("start-reset-password", use: startResetPasswordForm) @@ -29,6 +34,7 @@ final class UserWebController: RouteCollection { router.post("signup", use: create) router.post("start-reset-password", use: startResetPassword) router.post("reset-password", use: resetPassword) + } func signupForm(_ req: Request) throws -> EventLoopFuture { @@ -36,6 +42,24 @@ final class UserWebController: RouteCollection { .render(with: .init(), for: req) } + func profilePage(_ req: Request) throws -> EventLoopFuture { + let user = try req.requireAuthenticated(User.self) + + return try Subject.DatabaseRepository + .allActive(for: user, on: req) + .map { subjects in + + try req.renderer() + .render( + view: User.Templates.Profile.self, + with: .init( + user: user, + subjects: subjects + ) + ) + } + } + func loginForm(_ req: Request) throws -> EventLoopFuture { if try req.authenticated(User.self) != nil { From c1a3ce7fb568b3d226806906b2a49f56c23dbff4 Mon Sep 17 00:00:00 2001 From: Mats Eikeland Mollestad Date: Thu, 13 Feb 2020 15:18:43 +0100 Subject: [PATCH 5/5] [fix] Minor improvments --- Public/assets/js/flash-card/edit.js | 2 +- Sources/App/Subject Test/SubjectTestWebController.swift | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Public/assets/js/flash-card/edit.js b/Public/assets/js/flash-card/edit.js index 82bf89d..510714d 100644 --- a/Public/assets/js/flash-card/edit.js +++ b/Public/assets/js/flash-card/edit.js @@ -28,7 +28,7 @@ function editFlashCard() { } }) .then(function (json) { - window.location.href = "/tasks/flash-card/" + json.id; + window.location.href = "/creator/tasks/flash-card/" + json.id + "/edit?wasUpdated=true"; }) .catch(function (error) { presentErrorMessage(error.message); diff --git a/Sources/App/Subject Test/SubjectTestWebController.swift b/Sources/App/Subject Test/SubjectTestWebController.swift index 2c2de54..2708d70 100644 --- a/Sources/App/Subject Test/SubjectTestWebController.swift +++ b/Sources/App/Subject Test/SubjectTestWebController.swift @@ -175,12 +175,18 @@ class SubjectTestWebController: SubjectTestWebCo .map { _ in req.redirect(to: "results") } + .catchMap { error in + switch error { + case SubjectTest.DatabaseRepository.Errors.alreadyEnded: return req.redirect(to: "results") + default: throw error + } + } } static func results(on req: Request) throws -> EventLoopFuture { let user = try req.requireAuthenticated(User.self) - + return try API.results(on: req) .map { results in