Skip to content

Commit 2afb35f

Browse files
authored
Merge pull request #343 from ReproNim/fix/nested_variables
Handle nested scoping across activities and sub activities
2 parents 69994bf + 21bf162 commit 2afb35f

File tree

4 files changed

+89
-59
lines changed

4 files changed

+89
-59
lines changed

src/components/Inputs/SaveData/SaveData.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,14 @@ export default {
195195
});
196196
},
197197
sendRetry(url, formData, index, retries = 3, backoff = 10000) {
198+
if (!this.shouldUpload) {
199+
console.log("Not uploading")
200+
return 200;
201+
}
198202
const config1 = {
199203
'Content-Type': 'multipart/form-data'
200204
};
201-
return axios.post(`${config.backendServer}/submit`, formData, config1).then((res) => {
205+
return axios.post(url, formData, config1).then((res) => {
202206
// console.log(322, 'SUCCESS!!', `${fileName}.zip.00${index}`, res.status);
203207
slicedArray.splice(index, 1); // remove successfully POSTed slice
204208
sentPartCount++;

src/components/Section/Section.vue

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747

4848

4949
<div class="text-right mt-3" v-if="showPassOptions !== null ">
50-
<b-button variant="default"
51-
@click="restart">Restart</b-button>
50+
<!--<b-button variant="default"
51+
@click="restart">Restart</b-button> -->
5252
<b-button variant="default" v-if="showPassOptions['dontKnow']"
5353
@click="dontKnow">Don't Know</b-button>
5454
<b-button variant="default" v-if="showPassOptions['skip']"
@@ -122,14 +122,35 @@ export default {
122122
const answered = _.filter(this.context, c =>
123123
Object.keys(this.responses).indexOf(c['@id']) > -1);
124124
if (!answered.length) {
125-
this.listShow = [0];
125+
this.listShow = [this.initializeListShow()];
126126
} else {
127127
this.listShow = _.map(new Array(answered.length + 1), (c, i) => i);
128128
}
129129
this.visibility = this.getVisibility(this.responses);
130130
});
131131
});
132132
},
133+
initializeListShow() {
134+
const responseMapper = this.responseMapper(this.responses);
135+
let i = 0;
136+
for (i = 0; i < this.context.length; i += 1) {
137+
const eachItem = (this.context)[i];
138+
// return _.map(this.context, (o, index) => {
139+
const matchedObject = _.filter(this.activity['http://schema.repronim.org/addProperties'], a => a['http://schema.repronim.org/isAbout'][0]['@id'] === eachItem['@id']);
140+
let val = true; // true by default if not mentioned
141+
if (matchedObject[0]['http://schema.repronim.org/isVis']) {
142+
val = matchedObject[0]['http://schema.repronim.org/isVis'][0]['@value'];
143+
}
144+
if (_.isString(val)) {
145+
val = this.evaluateString(val, responseMapper);
146+
}
147+
if (val === true) { // first visible item
148+
break;
149+
}
150+
}
151+
return i;
152+
// });
153+
},
133154
getVisibility(responses) {
134155
const responseMapper = this.responseMapper(responses);
135156
if (!_.isEmpty(this.activity['http://schema.repronim.org/addProperties'])) {
@@ -152,7 +173,7 @@ export default {
152173
return {};
153174
},
154175
responseMapper(responses) {
155-
let keyArr;
176+
let keyArr = [];
156177
// a variable map is defined! great
157178
if (this.activity['http://schema.repronim.org/addProperties']) {
158179
const vmap = this.activity['http://schema.repronim.org/addProperties'];
@@ -164,6 +185,33 @@ export default {
164185
});
165186
166187
}
188+
const respMapper = {};
189+
_.map(keyArr, (a) => {
190+
respMapper[a.qId] = { val: a.val, ref: a.key };
191+
});
192+
// Store the response variables in the state
193+
this.$store.state.responseMap[this.activity['@id']] = respMapper;
194+
// Create a mapping from uris to variable names
195+
let uri2varmap = {};
196+
Object.entries(this.$store.state.responseMap).forEach(
197+
// eslint-disable-next-line no-unused-vars
198+
([unused, v]) => {
199+
Object.entries(v).forEach(
200+
([key1, value1]) => {
201+
uri2varmap[value1['ref']] = key1;
202+
})
203+
});
204+
Object.entries(this.$store.state.responseMap).forEach(
205+
// eslint-disable-next-line no-unused-vars
206+
([key, v]) => {
207+
Object.entries(v).forEach(
208+
([key1, value1]) => {
209+
if (key in uri2varmap) {
210+
const joined_key = ''.concat(uri2varmap[key],'.',key1);
211+
keyArr.push({ qId: joined_key, val: value1['val'], key: value1['ref'] });
212+
}
213+
})
214+
});
167215
if (this.$store.getters.getQueryParameters) {
168216
const q = this.$store.getters.getQueryParameters;
169217
Object.entries(q).forEach(
@@ -184,25 +232,36 @@ export default {
184232
return outMapper;
185233
},
186234
evaluateString(string, responseMapper) {
187-
// console.log(176, string, responseMapper);
188235
const keys = Object.keys(responseMapper);
189236
let output = string;
237+
let output_modified = false;
190238
_.map(keys, (k) => {
191239
// grab the value of the key from responseMapper
192240
let val = responseMapper[k].val;
193-
if (Array.isArray(responseMapper[k].val)) {
194-
val = responseMapper[k].val[0];
195-
}
196-
if (val !== 'http://schema.repronim.org/Skipped' && val !== 'http://schema.repronim.org/DontKnow') {
197-
if (_.isString(val)) {
198-
val = `'${val}'`; // put the string in quotes
241+
if (val !== undefined) {
242+
if (val !== 'skipped' && val !== 'dontknow') {
243+
if (_.isString(val)) {
244+
val = `'${val}'`; // put the string in quotes
245+
}
246+
if (_.isArray(val)) {
247+
val = `[${val}]`; // put braces for array
248+
}
249+
let output_old = output;
250+
output = output.replaceAll(new RegExp(`\\b${k}\\b` || `\\b${k}\\.`, 'g'), val);
251+
if (output_old !== output) output_modified = true;
252+
} else {
253+
let output_old = output;
254+
output = output.replaceAll(new RegExp(`\\b${k}\\b`, 'g'), 0);
255+
if (output_old !== output) output_modified = true;
199256
}
200-
output = output.replace(new RegExp(`\\b${k}\\b`), val);
201-
} else {
202-
output = output.replace(new RegExp(`\\b${k}\\b`), 0);
203257
}
204258
});
205-
return Function('return ' + output)();
259+
if (output_modified) {
260+
return Function("return " + output)();
261+
}
262+
else {
263+
return false;
264+
}
206265
},
207266
restart() {
208267
this.currentIndex = 0;
@@ -353,9 +412,11 @@ export default {
353412
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
354413
this.checkAlertMessage(idx);
355414
if (skip) {
415+
this.$emit('saveResponse', this.context[idx]['@id'], 'http://schema.repronim.org/Skipped');
356416
this.setResponse('http://schema.repronim.org/Skipped', idx);
357417
}
358418
if (dontKnow) {
419+
this.$emit('saveResponse', this.context[idx]['@id'], 'http://schema.repronim.org/DontKnow');
359420
this.setResponse('http://schema.repronim.org/DontKnow', idx);
360421
}
361422

src/components/Survey/Survey.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,16 @@
490490
});
491491
},
492492
async sendRetry(url, formData, retries = 3, backoff = 10000) {
493+
if (!this.shouldUpload) {
494+
console.log("Not uploading")
495+
return;
496+
}
493497
const config1 = {
494498
'Content-Type': 'multipart/form-data'
495499
};
496500
try {
497501
// eslint-disable-next-line no-unused-vars
498-
const res = await axios.post(`${config.backendServer}/submit`, formData, config1);
502+
const res = await axios.post(url, formData, config1);
499503
// console.log(530, 'SUCCESS!!', formData, res.status);
500504
} catch (e) {
501505
if (retries > 0) {
@@ -582,6 +586,9 @@
582586
return criteria1 && criteria2;
583587
});
584588
},
589+
shouldUpload() {
590+
return !!(config.backendServer && this.$store.getters.getAuthToken);
591+
},
585592
context() {
586593
/* eslint-disable */
587594
if (this.activity['http://schema.repronim.org/order']) {

src/components/SurveyItem/SurveyItem.vue

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,6 @@
170170
width: '100%',
171171
};
172172
},
173-
bodyStyle() {
174-
if (this.ui === 'section' || this.ui === 'multipart') {
175-
return {
176-
padding: 0,
177-
};
178-
}
179-
return {};
180-
},
181173
ui() {
182174
/* eslint-disable */
183175
if (this.data['@type'] && this.data['@type'][0] === "http://schema.repronim.org/Activity") {
@@ -229,42 +221,8 @@
229221
fieldData() {
230222
return this.data;
231223
},
232-
findPassOptions() {
233-
if (this.data['http://schema.repronim.org/responseOptions']) {
234-
// when responseOptions is a remote object
235-
if (Object.keys(this.data['http://schema.repronim.org/responseOptions'][0]).indexOf('@id') > -1) {
236-
this.getRequiredVal();
237-
return this.requireVal;
238-
}
239-
// when responseOptions in embedded in item object itself
240-
if (this.data['http://schema.repronim.org/responseOptions'][0]) {
241-
// make sure the requiredValue key is defined
242-
// todo: requiredValue has moved to activity level, so change the code here
243-
if (this.data['http://schema.repronim.org/responseOptions'][0]['http://schema.org/valueRequired']) {
244-
return this.data['http://schema.repronim.org/responseOptions'][0]['http://schema.org/valueRequired'][0]['@value'];
245-
}
246-
}
247-
}
248-
return false;
249-
},
250224
},
251225
methods: {
252-
getRequiredVal() { // todo: this needs to change. requiredValue is in activity level now
253-
jsonld.expand(this.data['http://schema.repronim.org/responseOptions'][0]['@id'])
254-
.then((rsp) => {
255-
this.requireVal = rsp[0]['http://schema.org/valueRequired'][0]['@value'];
256-
// eslint-disable-next-line no-unused-vars
257-
}).catch((e) => {
258-
// console.log(240, 'constraint error', e);
259-
jsonld.expand(`${this.data['http://schema.repronim.org/responseOptions'][0]['@id']}.jsonld`).then((resp) => {
260-
// console.log(250, resp);
261-
this.requireVal = resp[0]['http://schema.org/valueRequired'][0]['@value'];
262-
// eslint-disable-next-line no-unused-vars
263-
}).catch((e1) => {
264-
// console.log(252, e1);
265-
});
266-
});
267-
},
268226
getValueConstraintsData(url) {
269227
jsonld.expand(url).then((rsp) => {
270228
this.valueC = rsp[0];

0 commit comments

Comments
 (0)