From 7d801f511a99b0a75430976dc22c8c266fb1856d Mon Sep 17 00:00:00 2001 From: Maarten Duijndam Date: Wed, 16 Feb 2022 14:23:55 +0100 Subject: [PATCH 1/2] Add support for the session API --- index.html | 74 +++++++++++++++++++++++++++++++++++++++++++++++++----- stimuli.js | 34 ------------------------- 2 files changed, 68 insertions(+), 40 deletions(-) diff --git a/index.html b/index.html index 7510cd9..44cfd69 100644 --- a/index.html +++ b/index.html @@ -42,11 +42,11 @@ const KEY_CODE_SPACE = 32; // this function will eventually run the jsPsych timeline -function kickOffExperiment() { +function kickOffExperiment(stimuli) { const G_QUESTION_CHOICES = [FALSE_BUTTON_TEXT, TRUE_BUTTON_TEXT]; - let stimuli = pickRandomList(); - let subject_id = jsPsych.randomization.randomID(8); + let subject_id = uil.session.isActive() ? + uil.session.subjectId() : jsPsych.randomization.randomID(8); let practice_items = getPracticeItems().table; let test_items = stimuli.table; let list_name = stimuli.list_name; @@ -188,7 +188,7 @@ on_finish: function (data) { let choice = G_QUESTION_CHOICES[data.button_pressed]; data.answer = choice; - data.correct = choice == data.expected_answer; + data.correct = choice === data.expected_answer; data.integer_correct = data.correct ? 1 : 0; data.rt = Math.round(data.rt); } @@ -254,12 +254,74 @@ let paragraph = document.createElement("p") paragraph.innerHTML = "Please run this experiment on a pc or laptop"; document.body.appendChild(paragraph); - }; + } +} + +/** + * Get the list of practice items + * + * Returns an object with a list and a table, the list will always indicate + * "practice" since it are the practice items + * + * @returns {object} object with list_name and table fields + */ +function getPracticeItems() { + return {list_name : "practice", table : PRACTICE_ITEMS}; +} + +/** + * This function will pick a random list from the TEST_ITEMS array. + * + * Returns an object with a list and a table, the list will always indicate + * which list has been chosen for the participant. + * + * @returns {object} object with list_name and table fields + */ +function pickRandomList() { + let range = function (n) { + let empty_array = []; + let i; + for (i = 0; i < n; i++) { + empty_array.push(i); + } + return empty_array; + } + let num_lists = TEST_ITEMS.length; + var shuffled_range = jsPsych.randomization.repeat(range(num_lists), 1) + var retlist = TEST_ITEMS[shuffled_range[0]]; + return retlist +} + +function findList(name) { + let list = TEST_ITEMS.find((entry) => entry.list_name === name); + if (!list) { + let found = TEST_ITEMS.map((entry) => `"${entry.list_name}"`).join(', '); + console.error( + `List not found "${name}".\n` + + 'This name was configured on the UiL datastore server.\n' + + `The following lists exist in stimuli.js: \n${found}`) + } + return list; +} + +function main() { + + // Option 1: client side randomization: + let stimuli = pickRandomList(); + kickOffExperiment(stimuli); + + // Option 2: server side balancing: + // Make sure you have matched your groups on the dataserver with the + // This experiment uses groups/lists list1, and list2 by default. + // uil.session.start(ACCESS_KEY, (group_name) => { + // let stimuli = findList(group_name); + // kickOffExperiment(stimuli); + // }); } window.addEventListener ( 'load', - kickOffExperiment + main ); diff --git a/stimuli.js b/stimuli.js index 3794109..c948506 100644 --- a/stimuli.js +++ b/stimuli.js @@ -157,38 +157,4 @@ const TEST_ITEMS = [ // {list_name: LISTS[1], table: LIST_GROUP3} ]; -/** - * Get the list of practice items - * - * Returns an object with a list and a table, the list will always indicate - * "practice" since it are the practice items - * - * @returns {object} object with list_name and table fields - */ -function getPracticeItems() { - return {list_name : "practice", table : PRACTICE_ITEMS}; -} - -/** - * This function will pick a random list from the TEST_ITEMS array. - * - * Returns an object with a list and a table, the list will always indicate - * which list has been chosen for the participant. - * - * @returns {object} object with list_name and table fields - */ -function pickRandomList() { - let range = function (n) { - let empty_array = []; - let i; - for (i = 0; i < n; i++) { - empty_array.push(i); - } - return empty_array; - } - let num_lists = TEST_ITEMS.length; - var shuffled_range = jsPsych.randomization.repeat(range(num_lists), 1) - var retlist = TEST_ITEMS[shuffled_range[0]]; - return retlist -} From 556fc9d5eecb9c0c6c751c36791ff07be9705ca6 Mon Sep 17 00:00:00 2001 From: Maarten Date: Fri, 18 Feb 2022 11:22:22 +0100 Subject: [PATCH 2/2] Bug fix of the survey review #fixes 19 --- survey.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/survey.js b/survey.js index 7055d74..a23eecf 100644 --- a/survey.js +++ b/survey.js @@ -117,10 +117,10 @@ let survey_review = { stimulus: function(data){ let survey_1_data= - jsPsych.data.get().last(2).values()[0]; //former + JSON.parse(jsPsych.data.get().last(2).values()[0].responses); let survey_2_data = - jsPsych.data.get().last(1).values()[0]; + JSON.parse(jsPsych.data.get().last(1).values()[0].responses); let b_year = survey_1_data.birth_year; let b_month = survey_1_data.birth_month; @@ -132,7 +132,7 @@ let survey_review = { let hand_pref = survey_2_data.HandPreference; return ` -

Your data

+

Your responses

Birth year: ${b_year}
Birth month: ${b_month}