From 92d9bd6925850bbeee214a7fd933c449bfb558f3 Mon Sep 17 00:00:00 2001 From: Mats Eikeland Mollestad Date: Wed, 22 Sep 2021 17:24:51 +0200 Subject: [PATCH 1/6] feat: some resource stuff --- Package.swift | 8 ++- Public/assets/js/flash-card/json-data.js | 3 +- Public/assets/js/multiple-choise/json-data.js | 3 +- Public/assets/js/resources/create.js | 50 ++++++++++++++----- 4 files changed, 48 insertions(+), 16 deletions(-) diff --git a/Package.swift b/Package.swift index 80d1d05..ff34510 100644 --- a/Package.swift +++ b/Package.swift @@ -4,15 +4,21 @@ import Foundation var dependencies: [Package.Dependency] = [ // 💧 A server-side Swift web framework. - .package(name: "vapor", url: "https://github.com/vapor/vapor.git", from: "4.29.0"), + .package(name: "vapor", url: "https://github.com/vapor/vapor.git", from: "4.48.0"), .package(name: "HTMLKitVaporProvider", url: "https://github.com/MatsMoll/htmlkit-vapor-provider.git", from: "1.0.1"), .package(url: "https://github.com/vapor-community/HTMLKit.git", from: "2.1.0"), + + .package(name: "RediStack", url: "https://github.com/Mordil/RediStack", from: "1.2.0"), + + .package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.15.0"), ] // Kognita Core + + switch ProcessInfo.processInfo.environment["BUILD_TYPE"] { case "LOCAL": dependencies.append(contentsOf: [ diff --git a/Public/assets/js/flash-card/json-data.js b/Public/assets/js/flash-card/json-data.js index f1a4fef..d8586f1 100644 --- a/Public/assets/js/flash-card/json-data.js +++ b/Public/assets/js/flash-card/json-data.js @@ -27,6 +27,7 @@ function jsonData() { "subtopicId" : subtopicId, "description" : descriptionValue, "question" : question, - "solution" : solutionValue + "solution" : solutionValue, + "resources": [] }); } \ No newline at end of file diff --git a/Public/assets/js/multiple-choise/json-data.js b/Public/assets/js/multiple-choise/json-data.js index 0ab7ebb..e446544 100644 --- a/Public/assets/js/multiple-choise/json-data.js +++ b/Public/assets/js/multiple-choise/json-data.js @@ -49,6 +49,7 @@ function jsonData() { "question" : question, "isMultipleSelect" : isMultipleSelect, "choises" : choises, - "solution" : solutionValue + "solution" : solutionValue, + "resources": [] }); } \ No newline at end of file diff --git a/Public/assets/js/resources/create.js b/Public/assets/js/resources/create.js index 79dcd3f..d97d01e 100644 --- a/Public/assets/js/resources/create.js +++ b/Public/assets/js/resources/create.js @@ -6,21 +6,31 @@ function createResource() { let connectedID = document.getElementById("resource-connect-id").value; let connectionType = document.getElementById("resource-connect-type").value; + let inputName = document.getElementById("resource-input-name").value; + let formName = document.getElementById("resource-form-name").value; var url = "/api/resources/" var jsonData; - if (resourceTabID == "article-rec") { - url += "article" - jsonData = articleData(title) - } else if (resourceTabID == "video-rec") { - url += "video" - jsonData = videoData(title) - } else if (resourceTabID == "book-rec") { - url += "book" - jsonData = bookData(title) - } else { - return + try { + if (title.length == 0) { + throw new Error("Ups! Mangler tittel"); + } + if (resourceTabID == "article-rec") { + url += "article" + jsonData = articleData(title) + } else if (resourceTabID == "video-rec") { + url += "video" + jsonData = videoData(title) + } else if (resourceTabID == "book-rec") { + url += "book" + jsonData = bookData(title) + } else { + return + } + } catch (error) { + console.log(error); + $("#resource-form").addClass("was-validated"); } fetch(url, { @@ -33,6 +43,7 @@ function createResource() { }) .then(function (response) { if (response.ok) { + $("#create-resource-modal").modal('hide'); return response.json(); } else if (response.status == 400) { throw new Error("Sjekk at all nødvendig info er fylt ut"); @@ -46,7 +57,14 @@ function createResource() { console.log("Not a number"); return } - if (connectionType == "term") { + if (inputName.length != 0 && formName.length != 0) { + let form = document.getElementById(formName); + var input = document.createElement("input"); + input.type = "hidden"; + input.name = inputName + "[]"; + input.value = json; + form.appendChild(input); + } else if (connectionType == "term") { connectTerm(connectedID, json); } else if (connectionType == "subtopic") { connectSubtopic(connectedID, json); @@ -62,7 +80,9 @@ function videoData(title) { if (isNaN(duration)) { duration = null; } - + if (url.length == 0 || creator.length == 0) { + throw Error("Ups! Mangeler noe data"); + } return JSON.stringify({ "url": url, "creator": creator, @@ -79,6 +99,10 @@ function articleData(title) { let url = document.getElementById("article-url").value; let author = document.getElementById("article-author").value; + if (url.length == 0 || author.length == 0) { + throw new Error("Ups! Noe data mangler"); + } + return JSON.stringify({ "url": url, "author": author, From ed5a5e2d9347b5c5239585eef285734407c63e82 Mon Sep 17 00:00:00 2001 From: Mats Eikeland Mollestad Date: Wed, 22 Sep 2021 17:47:54 +0200 Subject: [PATCH 2/6] fix: Package --- Package.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Package.swift b/Package.swift index ff34510..80fe8ea 100644 --- a/Package.swift +++ b/Package.swift @@ -29,9 +29,9 @@ case "LOCAL": ) case "DEV": dependencies.append(contentsOf: [ - .package(name: "KognitaCore", url: "https://Kognita:dyjdov-bupgev-goffY8@github.com/MatsMoll/KognitaCore", .branch("develop")), - .package(name: "KognitaViews", url: "https://Kognita:dyjdov-bupgev-goffY8@github.com/MatsMoll/KognitaPages", .branch("develop")), - .package(name: "KognitaAPI", url: "https://Kognita:dyjdov-bupgev-goffY8@github.com/MatsMoll/kognita-rest-api", .branch("develop")) + .package(name: "KognitaCore", url: "https://github.com/MatsMoll/KognitaCore", .branch("develop")), + .package(name: "KognitaViews", url: "https://github.com/MatsMoll/KognitaPages", .branch("develop")), + .package(name: "KognitaAPI", url: "https://github.com/MatsMoll/kognita-rest-api", .branch("develop")) ] ) default: @@ -51,9 +51,9 @@ default: let pagesVersion = ProcessInfo.processInfo.environment["KOGNITA_PAGES"] ?? "2.0.0" let apiVersion = ProcessInfo.processInfo.environment["KOGNITA_API"] ?? "2.0.0" dependencies.append(contentsOf: [ - .package(name: "KognitaCore", url: "https://Kognita:dyjdov-bupgev-goffY8@github.com/MatsMoll/KognitaCore", from: Version(stringLiteral: coreVersion)), - .package(name: "KognitaViews", url: "https://Kognita:dyjdov-bupgev-goffY8@github.com/MatsMoll/KognitaPages", from: Version(stringLiteral: pagesVersion)), - .package(name: "KognitaAPI", url: "https://Kognita:dyjdov-bupgev-goffY8@github.com/MatsMoll/kognita-rest-api", from: Version(stringLiteral: apiVersion)) + .package(name: "KognitaCore", url: "https://github.com/MatsMoll/KognitaCore", from: Version(stringLiteral: coreVersion)), + .package(name: "KognitaViews", url: "https://github.com/MatsMoll/KognitaPages", from: Version(stringLiteral: pagesVersion)), + .package(name: "KognitaAPI", url: "https://github.com/MatsMoll/kognita-rest-api", from: Version(stringLiteral: apiVersion)) ] ) } From 1c11d2c79a7c5331916c3d94f53cc71c155eca38 Mon Sep 17 00:00:00 2001 From: Mats Eikeland Mollestad Date: Thu, 23 Sep 2021 13:49:57 +0200 Subject: [PATCH 3/6] [json-data] Updated missing json data when not admin --- Public/assets/js/multiple-choise/json-data.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Public/assets/js/multiple-choise/json-data.js b/Public/assets/js/multiple-choise/json-data.js index e446544..2f98d6a 100644 --- a/Public/assets/js/multiple-choise/json-data.js +++ b/Public/assets/js/multiple-choise/json-data.js @@ -38,8 +38,9 @@ function jsonData() { if (choises.filter(choise => choise.isCorrect).length < 1) { throw Error("Minst et alternativ må være markert som riktig. Dette kan gjøres ved å trykke på det riktige alternativet") } - - console.log(choises); + if (isTestable == null) { + isTestable = false + } return JSON.stringify({ "isTestable" : isTestable, @@ -52,4 +53,4 @@ function jsonData() { "solution" : solutionValue, "resources": [] }); -} \ No newline at end of file +} From c25e64987035a8c6bff59d9c119840e4bfeab901 Mon Sep 17 00:00:00 2001 From: "Mats E. Mollestad" Date: Thu, 17 Nov 2022 23:50:51 +0100 Subject: [PATCH 4/6] feat: added landing metrics --- Makefile | 21 --------- Package.swift | 7 ++- .../App/Creator/CreatorWebController.swift | 25 +++++------ Sources/App/routes.swift | 17 +++++-- develop.sh | 5 --- install.sh | 4 -- set-xcodeproj-env.py | 45 ------------------- 7 files changed, 28 insertions(+), 96 deletions(-) delete mode 100644 Makefile delete mode 100644 develop.sh delete mode 100644 install.sh delete mode 100644 set-xcodeproj-env.py diff --git a/Makefile b/Makefile deleted file mode 100644 index 388f9b5..0000000 --- a/Makefile +++ /dev/null @@ -1,21 +0,0 @@ -commit: - xcodebuild -scheme Run test | xcpretty - git add . - git commit -m"$(msg)" - git push - -test: - xcodebuild \ - -scheme Run \ - -derivedDataPath DerivedData \ - -destination "platform=OS X" \ - -enableCodeCoverage YES \ - test | xcpretty - - sleep 1 - - xcrun llvm-cov show \ - -format=html \ - -instr-profile DerivedData/Build/ProfileData/$(shell ls DerivedData/Build/ProfileData)/Coverage.profdata \ - DerivedData/Build/Products/Debug/App.framework/Versions/A/App Sources/App > coverage.html - open coverage.html \ No newline at end of file diff --git a/Package.swift b/Package.swift index 80fe8ea..1771196 100644 --- a/Package.swift +++ b/Package.swift @@ -4,13 +4,13 @@ import Foundation var dependencies: [Package.Dependency] = [ // 💧 A server-side Swift web framework. - .package(name: "vapor", url: "https://github.com/vapor/vapor.git", from: "4.48.0"), + .package(name: "vapor", url: "https://github.com/vapor/vapor.git", from: "4.67.3"), .package(name: "HTMLKitVaporProvider", url: "https://github.com/MatsMoll/htmlkit-vapor-provider.git", from: "1.0.1"), - .package(url: "https://github.com/vapor-community/HTMLKit.git", from: "2.1.0"), + .package(url: "https://github.com/vapor-community/HTMLKit.git", .exact("2.1.1")), - .package(name: "RediStack", url: "https://github.com/Mordil/RediStack", from: "1.2.0"), + .package(name: "RediStack", url: "https://github.com/Mordil/RediStack", from: "1.3.0"), .package(url: "https://github.com/apple/swift-nio-ssl.git", from: "2.15.0"), ] @@ -18,7 +18,6 @@ var dependencies: [Package.Dependency] = [ // Kognita Core - switch ProcessInfo.processInfo.environment["BUILD_TYPE"] { case "LOCAL": dependencies.append(contentsOf: [ diff --git a/Sources/App/Creator/CreatorWebController.swift b/Sources/App/Creator/CreatorWebController.swift index a0868e6..ca68898 100644 --- a/Sources/App/Creator/CreatorWebController.swift +++ b/Sources/App/Creator/CreatorWebController.swift @@ -8,7 +8,6 @@ import Vapor import KognitaCore import KognitaViews -import QTIKit final class CreatorWebController: RouteCollection { @@ -91,18 +90,18 @@ final class CreatorWebController: RouteCollection { let files: [File] } - private func decodeAssessmentItems(from files: [File]) -> [AssessmentItem] { - var assessmentItems = [AssessmentItem]() - for file in files { - guard - let data = file.data.getData(at: 0, length: file.data.readableBytes), - let xml = String(data: data, encoding: .utf8), - let item = try? QTIKit.assessmentItem(withXML: xml) - else { continue } - assessmentItems.append(item) - } - return assessmentItems - } +// private func decodeAssessmentItems(from files: [File]) -> [AssessmentItem] { +// var assessmentItems = [AssessmentItem]() +// for file in files { +// guard +// let data = file.data.getData(at: 0, length: file.data.readableBytes), +// let xml = String(data: data, encoding: .utf8), +// let item = try? QTIKit.assessmentItem(withXML: xml) +// else { continue } +// assessmentItems.append(item) +// } +// return assessmentItems +// } // func importQTIContent(on req: Request) throws -> EventLoopFuture { // diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 50f7582..7eb1cfa 100644 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -40,10 +40,19 @@ private func setupUserWeb(for app: Application) throws { if req.auth.get(User.self) != nil { return req.eventLoop.future(req.redirect(to: "/subjects")) } - let context = Pages.Landing.Context(showCookieMessage: req.cookies.isAccepted == false) - - return try req.htmlkit.render(view: Pages.Landing.self, with: context) - .encodeResponse(for: req) + + return req.repositories(transaction: { repo in + repo.userRepository.numberOfUsers().and(repo.taskResultRepository.numberOfCompletedTasks()) + }) + .flatMapThrowing { (numberOfUsers, numberOfCompletedTasks) -> Response in + let context = Pages.Landing.Context( + showCookieMessage: req.cookies.isAccepted == false, + numberOfCompletedTasks: numberOfCompletedTasks, + numberOfUsers: numberOfUsers + ) + + return try req.htmlkit.render(Pages.Landing.self, with: context) + } } app.get("privacy-policy") { req -> View in diff --git a/develop.sh b/develop.sh deleted file mode 100644 index 3ca0397..0000000 --- a/develop.sh +++ /dev/null @@ -1,5 +0,0 @@ -export BUILD_TYPE=LOCAL -swift package generate-xcodeproj -python set-xcodeproj-env.py -sleep 1 -open Kognita.xcodeproj/ diff --git a/install.sh b/install.sh deleted file mode 100644 index 335a425..0000000 --- a/install.sh +++ /dev/null @@ -1,4 +0,0 @@ -sudo pip install pbxproj -brew install swiftlint -brew install postgresql - diff --git a/set-xcodeproj-env.py b/set-xcodeproj-env.py deleted file mode 100644 index 9e6c878..0000000 --- a/set-xcodeproj-env.py +++ /dev/null @@ -1,45 +0,0 @@ -import xml.etree.ElementTree as ETree -import getpass -from pbxproj import XcodeProject -import sys -import os - -file = "Kognita.xcodeproj/xcshareddata/xcschemes/Run.xcscheme" - -tree = ETree.parse(file) - -def environmentVariable(name, value, etree, is_enabled=True): - key = ETree.SubElement(etree, "EnvironmentVariable") - key.attrib["key"] = name - key.attrib["value"] = value - if is_enabled: - key.attrib["isEnabled"] = "YES" - -# Adding Env variables -launchAction = tree.getroot().find("LaunchAction") -launchAction.set("useCustomWorkingDirectory", "YES") -launchAction.set("customWorkingDirectory", os.getcwd()) - -env_var_section = ETree.SubElement(launchAction, "EnvironmentVariables") - -environmentVariable("MAILGUN_KEY", "dd", env_var_section) -environmentVariable("MAILGUN_DOMAIN", "dd", env_var_section) -environmentVariable("DATABASE_USER", getpass.getuser(), env_var_section) -environmentVariable("TEXT_CLIENT_BASE_URL", "127.0.0.1", env_var_section) -environmentVariable("TEXT_CLIENT_PORT", "5000", env_var_section) -environmentVariable("TEXT_CLIENT_SCHEME", "http", env_var_section) -environmentVariable("ROOT_URL", "http://localhost:8080", env_var_section) - -tree.write(file) - -# Adding swiftlint script -project = XcodeProject.load("Kognita.xcodeproj/project.pbxproj") -project.add_run_script(""" -if which swiftlint >/dev/null; then - swiftlint - swiftlint autocorrect -else - echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint" -fi -""", target_name=["Run"]) -project.save() From 03a198147fd2e1083f48a72dd99be0a1263be749 Mon Sep 17 00:00:00 2001 From: "Mats E. Mollestad" Date: Fri, 18 Nov 2022 00:05:32 +0100 Subject: [PATCH 5/6] fix: package --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 1771196..cc8b55c 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,7 @@ var dependencies: [Package.Dependency] = [ // 💧 A server-side Swift web framework. .package(name: "vapor", url: "https://github.com/vapor/vapor.git", from: "4.67.3"), - .package(name: "HTMLKitVaporProvider", url: "https://github.com/MatsMoll/htmlkit-vapor-provider.git", from: "1.0.1"), + .package(name: "HTMLKitVaporProvider", url: "https://github.com/MatsMoll/htmlkit-vapor-provider.git", .exact("1.0.1")), .package(url: "https://github.com/vapor-community/HTMLKit.git", .exact("2.1.1")), From e1a44b4930858524119a97087671894a3dbef7a0 Mon Sep 17 00:00:00 2001 From: "Mats E. Mollestad" Date: Fri, 18 Nov 2022 00:12:11 +0100 Subject: [PATCH 6/6] removed redirect to subjects page --- Sources/App/routes.swift | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 7eb1cfa..6857ecc 100644 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -37,11 +37,8 @@ private func setupUserWeb(for app: Application) throws { let redirectMiddle = sessionMiddle.grouped(RedirectMiddleware(path: "/login")) sessionMiddle.get { req -> EventLoopFuture in - if req.auth.get(User.self) != nil { - return req.eventLoop.future(req.redirect(to: "/subjects")) - } - return req.repositories(transaction: { repo in + req.repositories(transaction: { repo in repo.userRepository.numberOfUsers().and(repo.taskResultRepository.numberOfCompletedTasks()) }) .flatMapThrowing { (numberOfUsers, numberOfCompletedTasks) -> Response in