Commit 10c57313 authored by Tom Käsler's avatar Tom Käsler

add neccessary attributes to question

parent d565528d
Pipeline #7879 passed with stages
in 2 minutes and 44 seconds
......@@ -27,6 +27,13 @@ CREATE TABLE questions (
content TEXT NOT NULL,
variant VARCHAR(255) NOT NULL,
format VARCHAR(255) NOT NULL,
hint TEXT,
solution TEXT,
active TINYINT(1) NOT NULL DEFAULT 0,
voting_disabled TINYINT(1) NOT NULL DEFAULT 1,
show_statistic TINYINT(1) NOT NULL DEFAULT 0,
show_answer TINYINT(1) NOT NULL DEFAULT 0,
abstention_allowed TINYINT(1) NOT NULL DEFAULT 1,
format_attributes TEXT,
PRIMARY KEY(id),
CONSTRAINT question_session_fk FOREIGN KEY (session_id) REFERENCES sessions(id) ON UPDATE CASCADE ON DELETE CASCADE
......
......@@ -32,5 +32,5 @@ object QuestionJsonProtocol extends DefaultJsonProtocol {
}
// questin JSON protocol utilizes the above format. No need to manually do things here
implicit val questionFormat: RootJsonFormat[Question] = jsonFormat8(Question)
implicit val questionFormat: RootJsonFormat[Question] = jsonFormat15(Question)
}
......@@ -9,6 +9,13 @@ case class Question(
content: String,
variant: String,
format: String,
hint: Option[String],
solution: Option[String],
active: Boolean,
votingDisabled: Boolean,
showStatistic: Boolean,
showAnswer: Boolean,
abstentionAllowed: Boolean,
formatAttributes: Option[FormatAttributes],
answerOptions: Option[Seq[AnswerOption]]
)
......@@ -13,30 +13,43 @@ class QuestionsTable(tag: Tag) extends Table[Question](tag, "questions"){
def content: Rep[String] = column[String]("content")
def variant: Rep[String] = column[String]("variant")
def format: Rep[String] = column[String]("format")
def hint: Rep[String] = column[String]("hint")
def solution: Rep[String] = column[String]("solution")
def active: Rep[Boolean] = column[Boolean]("active")
def votingDisabled: Rep[Boolean] = column[Boolean]("voting_disabled")
def showStatistic: Rep[Boolean] = column[Boolean]("show_statistic")
def showAnswer: Rep[Boolean] = column[Boolean]("show_answer")
def abstentionAllowed: Rep[Boolean] = column[Boolean]("abstention_allowed")
def formatAttributes: Rep[String] = column[String]("format_attributes")
def * = (id.?, sessionId, subject, content, variant, format, formatAttributes) <> (
// TODO: please clean this up.
def * = (id.?, sessionId, subject, content, variant, format, hint.?, solution.?, active, votingDisabled, showStatistic,
showAnswer, abstentionAllowed, formatAttributes) <> (
// first comes the part for fetching
{ q: (Option[QuestionId], SessionId, String, String, String, String, String) => q match {
case (id, sessionId, subject, content, variant, format, formatAttributes) => {
formatAttributes match {
// some question formats don't have formatAttributes
case "null" => new Question(id, sessionId, subject, content, variant, format, None, None)
// formatAttributes are stored as JSON strings. This parsese them into the map
// using the formatAttributes JSON protocol doesn't work since it returns a JsObject
case _ => val fA = formatAttributes.substring(1, formatAttributes.length - 1)
.split(",")
.map(_.split(":"))
.map { case Array(k, v) => (k.substring(1, k.length-1), v.substring(1, v.length-1))}
.toMap
new Question(id, sessionId, subject, content, variant, format, Some(FormatAttributes(fA)), None)
{ q: (Option[QuestionId], SessionId, String, String, String, String, Option[String], Option[String],
Boolean, Boolean, Boolean, Boolean, Boolean, String) =>
q match {
case (id, sessionId, subject, content, variant, format, hint, solution, active, vD, sS, sA, aA, formatAttributes) => {
formatAttributes match {
// some question formats don't have formatAttributes
case "null" => new Question(id, sessionId, subject, content, variant, format, hint, solution, active, vD, sS, sA, aA, None, None)
// formatAttributes are stored as JSON strings. This parsese them into the map
// using the formatAttributes JSON protocol doesn't work since it returns a JsObject
case _ => val fA = formatAttributes.substring(1, formatAttributes.length - 1)
.split(",")
.map(_.split(":"))
.map { case Array(k, v) => (k.substring(1, k.length-1), v.substring(1, v.length-1))}
.toMap
new Question(id, sessionId, subject, content, variant, format, hint, solution, active, vD, sS, sA, aA, Some(FormatAttributes(fA)), None)
}
}
}
}}, {
// part for storing questions in the table
q: Question =>
Some((q.id, q.sessionId, q.subject, q.content, q.variant, q.format, q.formatAttributes.toJson.toString)):
Option[(Option[QuestionId], SessionId, String, String, String, String, String)]
Some((q.id, q.sessionId, q.subject, q.content, q.variant, q.format, q.hint, q.solution,
q.active, q.votingDisabled, q.showStatistic, q.showAnswer, q.abstentionAllowed, q.formatAttributes.toJson.toString)):
Option[(Option[QuestionId], SessionId, String, String, String, String, Option[String], Option[String],
Boolean, Boolean, Boolean, Boolean, Boolean, String)]
})
def session: ForeignKeyQuery[SessionsTable, de.thm.arsnova.models.Session] = foreignKey("question_session_fk", sessionId, TableQuery[SessionsTable])(_.id)
......
......@@ -56,7 +56,7 @@ trait QuestionApiSpec extends FunSpec with Matchers with ScalaFutures with BaseS
val content = "postContent"
val variant = "preparation"
val format = "freetext"
val newFreetext = Question(None, sessionId, subject, content, variant, format, None, None)
val newFreetext = Question(None, sessionId, subject, content, variant, format, None, None, true, true, false, true, true, None, None)
val requestEntity = HttpEntity(MediaTypes.`application/json`,newFreetext.toJson.toString)
Post("/question", requestEntity) ~> questionApi ~> check {
response.status should be(OK)
......@@ -76,7 +76,7 @@ trait QuestionApiSpec extends FunSpec with Matchers with ScalaFutures with BaseS
val variant = "preparation"
val format = "flashcard"
val backside = "backside"
val newFlashcard = Question(None, sessionId, subject, content, variant, format,
val newFlashcard = Question(None, sessionId, subject, content, variant, format, None, None, true, true, false, true, true,
Some(FormatAttributes(Map("backside" -> "backside"))), None)
val requestEntity = HttpEntity(MediaTypes.`application/json`, newFlashcard.toJson.toString)
Post("/question", requestEntity) ~> questionApi ~> check {
......@@ -97,7 +97,7 @@ trait QuestionApiSpec extends FunSpec with Matchers with ScalaFutures with BaseS
val variant = "preparation"
val format = "mc"
val answerOptions = Seq(AnswerOption(None, None, false, "false", -1), AnswerOption(None, None, true, "true", 1))
val newMC = Question(None, sessionId, subject, content, variant, format,
val newMC = Question(None, sessionId, subject, content, variant, format, None, None, true, true, false, true, true,
None, Some(answerOptions))
val requestEntity = HttpEntity(MediaTypes.`application/json`, newMC.toJson.toString)
Post("/question", requestEntity) ~> questionApi ~> check {
......
......@@ -45,19 +45,19 @@ trait TestData {
)
val preparationQuestions = Seq(
Question(Some(1), 1, "subject1", "content1", "preparation", "freetext", None, None),
Question(Some(2), 1, "subject2", "content2", "preparation", "freetext", None, None),
Question(Some(3), 1, "subject3", "content3", "preparation", "flashcard",
Question(Some(1), 1, "subject1", "content1", "preparation", "freetext", None, None, true, true, false, true, true, None, None),
Question(Some(2), 1, "subject2", "content2", "preparation", "freetext", None, None, true, true, false, true, true, None, None),
Question(Some(3), 1, "subject3", "content3", "preparation", "flashcard", None, None, true, true, false, true, true,
Some(FormatAttributes(Map("backside" -> "backside3"))), None),
Question(Some(FOUR), 1, "subject4", "content4", "preparation", "flashcard",
Question(Some(FOUR), 1, "subject4", "content4", "preparation", "flashcard", None, None, true, true, false, true, true,
Some(FormatAttributes(Map("backside" -> "backside4"))), None),
Question(Some(FIVE), 1, "subject5", "content5", "preparation", "mc",
Question(Some(FIVE), 1, "subject5", "content5", "preparation", "mc", None, None, true, true, false, true, true,
None, Some(testAnswerOptions))
)
val liveQuestions = Seq(
Question(Some(SIX), 1, "subject6", "content6", "live", "freetext", None, None),
Question(Some(SEVEN), 1, "subject7", "content7", "live", "flashcard",
Question(Some(SIX), 1, "subject6", "content6", "live", "freetext", None, None, true, true, false, true, true, None, None),
Question(Some(SEVEN), 1, "subject7", "content7", "live", "flashcard", None, None, true, true, false, true, true,
Some(FormatAttributes(Map("backside" -> "backside7"))), None)
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment