Commit d47c41ff authored by Daniel Gerhardt's avatar Daniel Gerhardt
Browse files

Add CouchDB design docs as JavaScript

Functions need to be stringified to create valid JSON which is accepted
by CouchDB.
parent 6b4830a4
var designDoc = {
"_id": "_design/answer",
"language": "javascript",
"views": {
"doc_by_questionid_user_piround": {
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit([doc.questionId, doc.user, doc.piRound], doc);
}
}
},
"doc_by_questionid_timestamp": {
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit([doc.questionId, doc.timestamp], doc);
}
}
},
"doc_by_user_sessionid": {
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit([doc.user, doc.sessionId], doc);
}
}
},
"by_questionid": {
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit(doc.questionId, null);
}
}
},
"by_questionid_piround_text_subject": {
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit([doc.questionId, doc.piRound, doc.abstention, doc.answerText, doc.answerSubject, doc.successfulFreeTextAnswer], null);
}
},
"reduce": "_count"
},
"by_sessionid": {
/* Redundant view but kept for now to allow simpler queries. */
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit(doc.sessionId, null);
}
},
"reduce": "_count"
},
"by_sessionid_variant": {
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit([doc.sessionId, doc.questionVariant], null);
}
},
"reduce": "_count"
},
"questionid_by_user_sessionid_variant": {
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit([doc.user, doc.sessionId, doc.questionVariant], doc.questionId);
}
}
},
"questionid_piround_by_user_sessionid_variant": {
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit([doc.user, doc.sessionId, doc.questionVariant], [doc.questionId, doc.piRound]);
}
}
}
}
};
var designDoc = {
"_id": "_design/comment",
"language": "javascript",
"views": {
"doc_by_sessionid_creator_timestamp": {
"map": function (doc) {
if (doc.type === "interposed_question") {
emit([doc.sessionId, doc.creator, doc.timestamp], doc);
}
}
},
"doc_by_sessionid_timestamp": {
"map": function (doc) {
if (doc.type === "interposed_question") {
emit([doc.sessionId, doc.timestamp], doc);
}
}
},
"by_sessionid": {
/* Redundant view but kept for now to allow simpler queries. */
"map": function (doc) {
if (doc.type === "interposed_question") {
emit(doc.sessionId, null);
}
},
"reduce": "_count"
},
"by_sessionid_read": {
"map": function (doc) {
if (doc.type === "interposed_question") {
emit([doc.sessionId, doc.read], null);
}
},
"reduce": "_count"
},
"by_sessionid_creator_read": {
"map": function (doc) {
if (doc.type === "interposed_question") {
emit([doc.sessionId, doc.creator, doc.read], null);
}
},
"reduce": "_count"
}
}
};
var designDoc = {
"_id": "_design/content",
"language": "javascript",
"views": {
"doc_by_sessionid_variant_active": {
"map": function (doc) {
if (doc.type === "skill_question") {
emit([doc.sessionId, doc.questionVariant, doc.active, doc.subject, doc.text.substr(0, 16)], doc);
}
},
"reduce": "_count"
},
"by_sessionid": {
/* Redundant view but kept for now to allow simpler queries. */
"map": function (doc) {
if (doc.type === "skill_question") {
emit(doc.sessionId, null);
}
},
"reduce": "_count"
},
"by_sessionid_variant_active": {
"map": function (doc) {
if (doc.type === "skill_question") {
emit([doc.sessionId, doc.questionVariant, doc.active, doc.subject, doc.text.substr(0, 16)], null);
}
},
"reduce": "_count"
}
}
};
var designDoc = {
"_id": "_design/learning_progress",
"language": "javascript",
"views": {
"question_value_achieved_for_user": {
"comment": "This view returns the points users scored for answered questions.",
"map": function (doc) {
if (doc.type === "skill_question_answer" && !doc.abstention) {
/* The 'questionValue' contains the points scored with this answer,
* and this could be negative if a wrong answer was given.
* However, we do not want negative values, so we set the lower bound to 0.*/
score = Math.max(doc.questionValue || 0, 0);
emit([doc.sessionId, doc.user], {
questionId: doc.questionId,
score: score, piRound: doc.piRound
});
}
}
},
"maximum_value_of_question": {
"comment": "This view returns the maximum number that can be achieved when answering this question.",
"map": function (doc) {
/* The question's value is determined by the maximum of all possibleAnswer values.
* We assume that a correct answer is assigned a positive value,
* while a negative value suggests a wrong answer.
* The goal then is to get the highest possible value.
* This leaves us with two cases:
* 1) On any single choice question, the value is the maximum of all possibleAnswer values.
* 2) On a multiple choice question, we add up all positive values. */
var value = 0, answers = [], positiveAnswers = [], score = 0;
if (doc.type === "skill_question" && ["school", "flashcard"].indexOf(doc.questionType) === -1) {
if ("freetext" === doc.questionType && !doc.fixedAnswer) { return; }
answers = doc.possibleAnswers.map(function(answer) { return answer.value || 0; });
/* find the maximum value */
if (doc.fixedAnswer) { value = doc.rating; }
else { value = Math.max.apply(null, [0].concat(answers)); }
/* ignore likert ('vote') questions without any points */
if (doc.questionType === "vote" && value === 0) { return; }
/* special case for mc and grid questions: add up all positive answers. */
if (["grid", "mc"].indexOf(doc.questionType) !== -1) {
positiveAnswers = answers.filter(function(val) { return val >= 0; });
if (positiveAnswers.length > 0) {
value = positiveAnswers.reduce(function(prev, cur) { return prev + cur; }, 0);
}
}
emit([doc.sessionId, doc._id], {
value: value,
questionVariant: doc.questionVariant,
piRound: doc.piRound
});
}
}
}
}
};
var designDoc = {
"_id": "_design/logged_in",
"language": "javascript",
"views": {
"visited_sessions_by_user": {
"map": function (doc) {
if (doc.type === "logged_in") {
emit(doc.user, doc.visitedSessions);
}
}
},
"all": {
"map": function (doc) {
if (doc.type === "logged_in"){
emit(doc.user, doc);
}
}
},
"by_last_activity_for_guests": {
"map": function (doc) {
if (doc.type === "logged_in" && doc.user.indexOf("Guest") === 0) {
emit(doc.timestamp || 0, {_rev: doc._rev});
}
}
}
}
};
var designDoc = {
"_id": "_design/motd",
"language": "javascript",
"views": {
"doc_by_sessionkey": {
"map": function (doc) {
if (doc.type === "motd" && doc.audience === "session") {
emit(doc.sessionkey, doc);
}
}
},
"doc_by_audience_for_global": {
"map": function (doc) {
if (doc.type === "motd" && doc.audience !== "session") {
emit(doc.audience, doc);
}
}
},
"by_motdkey": {
"map": function (doc) {
if (doc.type === "motd") {
emit(doc.motdkey, doc);
}
}
}
}
};
var designDoc = {
"_id": "_design/motdlist",
"language": "javascript",
"views": {
"doc_by_username": {
"map": function (doc) {
if (doc.type === "motdlist") {
emit(doc.username, doc);
}
}
}
}
};
var designDoc = {
"_id": "_design/session",
"language": "javascript",
"views": {
"by_courseid": {
"map": function (doc) {
if (doc.type === "session" && doc.courseId && doc.sessionType !== "public_pool") {
emit(doc.courseId, null);
}
}
},
"by_keyword": {
"map": function (doc) {
if (doc.type === "session") {
emit(doc.keyword, null);
}
}
},
"partial_by_sessiontype_creator_name": {
"map": function (doc) {
if (doc.type === "session") {
emit([doc.sessionType, doc.creator, doc.name], {
shortName: doc.shortName,
keyword: doc.keyword,
active: doc.active,
courseType: doc.courseType,
creationTime: doc.creationTime
});
}
}
},
"partial_by_ppsubject_name_for_publicpool": {
"map": function (doc) {
if (doc.type === "session" && doc.sessionType === "public_pool") {
emit([doc.ppSubject, doc.name], {
ppSubject: doc.ppSubject,
name: doc.name,
keyword: doc.keyword,
ppLevel: doc.ppLevel
});
}
}
},
"by_lastactivity_for_guests": {
"map": function (doc) {
if (doc.type === "session" && doc.sessionType !== "public_pool" && doc.creator.indexOf("Guest") === 0) {
emit(doc.lastOwnerActivity || doc.creationTime, {_rev: doc._rev});
}
}
}
}
};
var designDoc = {
"_id": "_design/statistics",
"language": "javascript",
"views": {
"active_student_users": {
"map": function (doc) {
if (doc.type === "skill_question_answer") {
emit(doc.user, 1);
}
},
"reduce": "_count"
},
"statistics": {
"map": function (doc) {
switch (doc.type) {
case "session":
if (doc.active) {
emit("openSessions", 1);
} else {
emit("closedSessions", 1);
}
break;
case "skill_question":
if (doc.questionType === "flashcard") {
emit("flashcards", 1);
} else {
if (doc.questionVariant === "lecture") {
emit("lectureQuestions", 1);
} else if (doc.questionVariant === "preparation") {
emit("preparationQuestions", 1);
}
if (doc.piRound === 2) {
emit("conceptQuestions", 1);
}
}
break;
case "skill_question_answer":
emit("answers", 1);
break;
case "interposed_question":
emit ("interposedQuestions", 1);
break;
case "log":
if (doc.event === "delete") {
switch (doc.payload.type) {
case "session":
emit("deletedSessions", doc.payload.sessionCount || 1);
break;
case "question":
emit("deletedQuestions", doc.payload.questionCount || 1);
break;
case "answer":
emit("deletedAnswers", doc.payload.answerCount || 1);
break;
case "comment":
emit("deletedComments", doc.payload.commentCount || 1);
break;
case "user":
emit("deletedUsers", 1);
break;
}
}
break;
}
},
"reduce": "_sum"
},
"unique_session_creators": {
"map": function (doc) {
if (doc.type === "session") {
emit(doc.creator, 1);
}
},
"reduce": "_count"
}
}
};
var designDoc = {
"_id": "_design/user",
"language": "javascript",
"views": {
"doc_by_username": {
"map": function (doc) {
if (doc.type === "userdetails") {
emit(doc.username, doc);
}
}
},
"by_creation_for_inactive": {
"map": function (doc) {
if (doc.type === "userdetails" && doc.activationKey) {
emit(doc.creation, {_rev: doc._rev});
}
}
}
}
};
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