Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #20 from brown-ccv/feat-attention-checks
Browse files Browse the repository at this point in the history
Feat: Attention Checks
  • Loading branch information
Rashi1997 authored Feb 15, 2021
2 parents 1da1bda + 6bfe25e commit b7228be
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fs = require('fs');
const trials = './trials';
const _ = require('lodash')

const defaultconfig = { USE_PHOTODIODE: false, USE_EEG: false, USE_ELECTRON: true, USE_MTURK: false}
Expand All @@ -13,5 +11,8 @@ module.exports = {
showMessage: require('./trials/showMessage.js'),
fixation: require('./trials/fixation.js'),
userId: require('./trials/userId.js'),
showImage: require('./trials/showImage.js')
showImage: require('./trials/showImage.js'),
slider: require('./trials/slider.js'),
survey: require('./trials/survey.js'),
multiSurvey: require('./trials/multiSurvey.js')
};
2 changes: 1 addition & 1 deletion tests/fixation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ describe("Fixation trial", () => {
expect(result.on_load()).toEqual(undefined);
expect(result.on_finish(data)).toEqual(10);
});
});
});
41 changes: 41 additions & 0 deletions tests/multiSurvey.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const multiSurvey = require("../trials/multiSurvey.js");

describe("multiSurvey trial", () => {
it(" with same ans choices for all questions", () => {
const preamble = "Answer the below questions";
const options = {
preamble: preamble,
prompts: ["hello?", "1?", "2?"],
ansChoices: { choices: ["no", "yes"] },
};
const questions = [
{ prompt: "hello?", options: ["no", "yes"], required: true },
{ prompt: "1?", options: ["no", "yes"], required: true },
{ prompt: "2?", options: ["no", "yes"], required: true },
];
const result = multiSurvey(options);
expect(result.preamble).toContain(preamble);
expect(result.questions).toEqual(questions);
});

it(" with different ans choices for each questions", () => {
const preamble = "Answer the below questions";
const options = {
preamble: preamble,
prompts: ["hello?", "1?", "2?"],
ansChoices: {
1: ["no", "yes", "maybe"],
2: ["not at all", "yes"],
3: ["nope", "yeah"],
},
};
const questions = [
{ prompt: "hello?", options: ["no", "yes", "maybe"], required: true },
{ prompt: "1?", options: ["not at all", "yes"], required: true },
{ prompt: "2?", options: ["nope", "yeah"], required: true },
];
const result = multiSurvey(options);
expect(result.preamble).toContain(preamble);
expect(result.questions).toEqual(questions);
});
});
19 changes: 19 additions & 0 deletions tests/slider.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const slider = require("../trials/slider.js");

describe("slider trial", () => {
it("slider with require movement", () => {
const message = "Move the slider to the right.";
const result = slider(
message);
const data = {
prompt: null,
answer: null,
response: "response",
};
result.on_finish(data);
expect(result.stimulus).toContain(message);
expect(result.require_movement).toEqual(true);
expect(data.prompt).toEqual([message]);
expect(data.answer).toEqual([data.response]);
});
});
18 changes: 18 additions & 0 deletions tests/survey.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const survey = require("../trials/survey.js");

describe("survey trial", () => {
it("survey with require movement", () => {
const message = "What is your age?";
const result = survey(
{message:message});
const data = {
prompt: null,
answer: null,
responses: JSON.stringify({Q0:"response"}),
};
result.on_finish(data);
expect(result.questions[0].prompt).toContain(message);
expect(data.prompt[0]).toContain(message);
expect(data.answer).toEqual("response");
});
});
47 changes: 47 additions & 0 deletions trials/multiSurvey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @description
* Builds a multi choice/select survey trial.
* @module
* @param {Object} options
* @param {string} options.responseType - This tells jsPsych which plugin file to use to run the trial. (default: 'survey_multi_choice')
* @param {string} options.preamble - HTML formatted string to display at the top of the page above all the questions. (default: empty string)
* @param {string|Array} options.prompts - The question prompts, this can be a string (one question) or an Array of strings (multiple questions) (default: "")
* @param {Object} options.ansChoices - Object consisting of the key as the answer choice name and value as the array of answer choices. (default: {})
*/

module.exports = function (options) {
const defaults = {
responseType: "survey_multi_choice",
preamble: "",
prompts: [],
ansChoices: {},
};
const { responseType, preamble, prompts, ansChoices } = {
...defaults,
...options,
};

// setting questions from prompts and answer choices
let questions = [];
const answers = Object.values(ansChoices);
prompts.forEach(function (prompt, index) {
let options = answers.length === 1 ? answers[0] : answers[index];
questions.push({
prompt: prompt,
options,
required: true,
});
});

return {
type: responseType,
preamble: preamble,
questions: questions,
on_finish: (data) => {
window.scrollTo(0, 0);
data.prompt = prompts;
data.ans_choices = [ansChoices];
data.answer = [{ answer: JSON.parse(data.responses) }];
},
};
};
18 changes: 18 additions & 0 deletions trials/slider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @description
* Builds a trial with a onscreen message and allows the subject to respond by dragging a slider.
* @module
* @param {string} message - The string to be displayed, this can be formatted as an HTML string. (default: empty string)
*/

module.exports = function (message = "") {
return {
type: "html_slider_response",
require_movement: true,
stimulus: message,
on_finish: (data) => {
data.prompt = [message];
data.answer = [data.response];
},
};
};
30 changes: 30 additions & 0 deletions trials/survey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { baseStimulus } = require("../lib/markup/stimuli");

/**
* @description
* Builds a trial with a question and with free response text fields. The subject types in answers.
* @module
* @param {Object} options
* @param {string} options.stimulus - Onscreen stimulus in HTML to be shown in the trial, if not set default text is empty. If the stimulus is not provided, message should be provided as a string. (default: "")
* @param {string} options.message - Onscreen message to be shown in the trial. (default: "")
*/

module.exports = function (options) {
const defaults = {
stimulus: "",
message: "",
};
const { stimulus, message } = { ...defaults, ...options };

const stimulusOrMessage =
message !== "" ? baseStimulus(`<h1>${message}</h1>`, true) : stimulus;

return {
type: "survey_text",
questions: [{ prompt: stimulusOrMessage, required: true }],
on_finish: (data) => {
data.prompt = [stimulusOrMessage];
data.answer = JSON.parse(data.responses)["Q0"];
},
};
};

0 comments on commit b7228be

Please sign in to comment.