2022-09-16 08:55:51 +00:00
|
|
|
import { getCorrectionTemplates } from './correctionTemplates';
|
2021-11-18 11:12:28 +00:00
|
|
|
import { getQuestions } from './questions';
|
2022-03-28 08:53:45 +00:00
|
|
|
import { Response } from './response';
|
2022-07-08 09:53:50 +00:00
|
|
|
import { Work } from './works';
|
2021-11-18 11:12:28 +00:00
|
|
|
|
2022-03-01 19:21:49 +00:00
|
|
|
export class Survey {
|
2021-11-18 11:12:28 +00:00
|
|
|
constructor(res) {
|
2022-07-08 09:53:50 +00:00
|
|
|
this.kind = "s";
|
2021-11-18 11:12:28 +00:00
|
|
|
if (res) {
|
|
|
|
this.update(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 14:28:27 +00:00
|
|
|
update({ id, id_category, title, promo, group, shown, direct, corrected, start_availability, end_availability }) {
|
2021-11-18 11:12:28 +00:00
|
|
|
this.id = id;
|
2022-11-20 14:28:27 +00:00
|
|
|
this.id_category = id_category;
|
2021-11-18 11:12:28 +00:00
|
|
|
this.title = title;
|
|
|
|
this.promo = promo;
|
2022-02-21 07:53:27 +00:00
|
|
|
this.group = group;
|
2021-11-18 11:12:28 +00:00
|
|
|
this.shown = shown;
|
2022-02-28 18:00:30 +00:00
|
|
|
this.direct = direct;
|
2021-11-18 11:12:28 +00:00
|
|
|
this.corrected = corrected;
|
|
|
|
if (this.start_availability != start_availability) {
|
|
|
|
this.start_availability = start_availability;
|
|
|
|
delete this.__start_availability;
|
|
|
|
}
|
|
|
|
if (this.end_availability != end_availability) {
|
|
|
|
this.end_availability = end_availability;
|
|
|
|
delete this.__end_availability;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startAvailability() {
|
|
|
|
if (!this.__start_availability) {
|
|
|
|
this.__start_availability = new Date(this.start_availability)
|
|
|
|
}
|
|
|
|
return this.__start_availability
|
|
|
|
}
|
|
|
|
|
|
|
|
endAvailability() {
|
|
|
|
if (!this.__end_availability) {
|
|
|
|
this.__end_availability = new Date(this.end_availability)
|
|
|
|
}
|
|
|
|
return this.__end_availability
|
|
|
|
}
|
|
|
|
|
|
|
|
isFinished() {
|
|
|
|
return this.endAvailability() < new Date();
|
|
|
|
}
|
|
|
|
|
|
|
|
async retrieveAnswers(id_user=null) {
|
|
|
|
const res = await fetch(id_user?`api/users/${id_user}/surveys/${this.id}/responses`:`api/surveys/${this.id}/responses`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {'Accept': 'application/json'},
|
|
|
|
});
|
|
|
|
if (res.status == 200) {
|
2022-03-28 08:53:45 +00:00
|
|
|
return (await res.json()).map((r) => new Response(r));
|
2021-11-18 11:12:28 +00:00
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async submitAnswers(answer, id_user=null) {
|
|
|
|
const res = await fetch(id_user?`api/users/${id_user}/surveys/${this.id}`:`api/surveys/${this.id}`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {'Accept': 'application/json'},
|
|
|
|
body: JSON.stringify(answer),
|
|
|
|
});
|
|
|
|
if (res.status == 200) {
|
|
|
|
return await res.json();
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async save() {
|
|
|
|
const res = await fetch(this.id?`api/surveys/${this.id}`:'api/surveys', {
|
|
|
|
method: this.id?'PUT':'POST',
|
|
|
|
headers: {'Accept': 'application/json'},
|
|
|
|
body: JSON.stringify(this),
|
|
|
|
});
|
|
|
|
if (res.status == 200) {
|
|
|
|
const data = await res.json()
|
|
|
|
this.update(data);
|
|
|
|
return data;
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async duplicate() {
|
|
|
|
if (this.id) {
|
|
|
|
const oldSurveyId = this.id;
|
|
|
|
delete this.id;
|
|
|
|
const res = await fetch(`api/surveys`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {'Accept': 'application/json'},
|
|
|
|
body: JSON.stringify(this),
|
|
|
|
});
|
|
|
|
if (res.status == 200) {
|
|
|
|
const response = await res.json();
|
|
|
|
|
|
|
|
// Now recopy questions
|
|
|
|
const questions = await getQuestions(oldSurveyId);
|
|
|
|
for (const q of questions) {
|
2022-09-16 08:55:51 +00:00
|
|
|
const oldQuestionId = q.id;
|
|
|
|
|
2022-11-27 23:25:26 +00:00
|
|
|
// This will create a new question with the same parameters
|
2021-11-18 11:12:28 +00:00
|
|
|
delete q.id;
|
2022-11-27 23:25:26 +00:00
|
|
|
|
|
|
|
// Also alter id_survey
|
2021-11-18 11:12:28 +00:00
|
|
|
q.id_survey = response.id;
|
2022-09-16 08:55:51 +00:00
|
|
|
|
2022-11-27 23:25:26 +00:00
|
|
|
// This save will create
|
|
|
|
const question = await q.save();
|
|
|
|
|
|
|
|
// Revert to the old question ID to perform the next retrievals
|
|
|
|
q.id = oldQuestionId;
|
|
|
|
|
|
|
|
// Now recopy proposals
|
|
|
|
if (q.kind == "mcq" || q.kind == "ucq") {
|
|
|
|
const proposals = await q.getProposals();
|
|
|
|
for (const p of proposals) {
|
|
|
|
delete p.id;
|
|
|
|
p.id_question = question.id;
|
|
|
|
await p.save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now recopy correction templates
|
|
|
|
const cts = await getCorrectionTemplates(oldQuestionId);
|
|
|
|
for (const ct of cts) {
|
|
|
|
delete ct.id;
|
|
|
|
ct.id_question = question.id;
|
|
|
|
ct.save();
|
|
|
|
}
|
2021-11-18 11:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete() {
|
|
|
|
if (this.id) {
|
2022-09-16 08:55:51 +00:00
|
|
|
// Start by deleting questions
|
|
|
|
const questions = await getQuestions(this.id);
|
|
|
|
for (const q of questions) {
|
|
|
|
await q.delete();
|
|
|
|
}
|
|
|
|
|
2021-11-18 11:12:28 +00:00
|
|
|
const res = await fetch(`api/surveys/${this.id}`, {
|
|
|
|
method: 'DELETE',
|
|
|
|
headers: {'Accept': 'application/json'},
|
|
|
|
});
|
|
|
|
if (res.status == 200) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 09:53:50 +00:00
|
|
|
export async function getSurveys(allworks) {
|
|
|
|
const res = await fetch(allworks?`api/all_works`:`api/surveys`, {headers: {'Accept': 'application/json'}})
|
2021-11-18 11:12:28 +00:00
|
|
|
if (res.status == 200) {
|
2022-07-08 09:53:50 +00:00
|
|
|
if (allworks) {
|
|
|
|
return (await res.json()).map((s) => {
|
|
|
|
if (s.kind == "survey")
|
|
|
|
return new Survey(s);
|
|
|
|
else
|
|
|
|
return new Work(s);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return (await res.json()).map((s) => new Survey(s));
|
|
|
|
}
|
2021-11-18 11:12:28 +00:00
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSurvey(sid) {
|
|
|
|
const res = await fetch(`api/surveys/${sid}`, {headers: {'Accept': 'application/json'}})
|
|
|
|
if (res.status == 200) {
|
|
|
|
return new Survey(await res.json());
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|