Move new ui to ui directory

This commit is contained in:
nemunaire 2022-03-01 13:43:45 +01:00
commit f85758ef33
56 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,88 @@
export class CorrectionTemplate {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, id_question, label, regexp, score, score_explaination }) {
this.id = id;
this.id_question = id_question;
this.regexp = regexp;
this.label = label;
this.score = score;
this.score_explaination = score_explaination;
}
async getCorrections() {
if (this.id) {
const res = await fetch(`api/questions/${this.id_question}/corrections/${this.id}`, {
method: 'GET',
headers: {'Accept': 'application/json'},
});
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/questions/${this.id_question}/corrections/${this.id}`:`api/questions/${this.id_question}/corrections`, {
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 delete() {
if (this.id) {
const res = await fetch(`api/questions/${this.id_question}/corrections/${this.id}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'},
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
} else {
return true;
}
}
}
export async function getCorrectionTemplates(qid) {
const res = await fetch(`api/questions/${qid}/corrections`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
const data = await res.json();
if (data === null) {
return [];
} else {
return (data).map((c) => new CorrectionTemplate(c))
}
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function autoCorrection(id_user, my_tpls) {
const res = await fetch(`api/users/${id_user}/corrections`, {
method: 'PUT',
headers: {'Accept': 'application/json'},
body: JSON.stringify(my_tpls),
});
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}

147
ui/src/lib/questions.js Normal file
View file

@ -0,0 +1,147 @@
import { Response } from './response';
export class QuestionProposal {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, id_question, label }) {
this.id = id;
this.id_question = id_question;
this.label = label;
if (this.changed !== undefined)
delete this.changed;
}
async save() {
const res = await fetch(this.id?`api/questions/${this.id_question}/proposals/${this.id}`:`api/questions/${this.id_question}/proposals`, {
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 delete() {
if (this.id) {
const res = await fetch(`api/questions/${this.id_question}/proposals/${this.id}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'},
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
}
}
}
export class Question {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, id_survey, title, description, desc_raw, placeholder, kind }) {
this.id = id;
this.id_survey = id_survey;
this.title = title;
this.description = description;
this.desc_raw = desc_raw;
this.placeholder = placeholder;
this.kind = kind;
}
async getProposals() {
const res = await fetch(`api/questions/${this.id}/proposals`, {
method: 'GET',
headers: {'Accept': 'application/json'},
});
if (res.status == 200) {
return (await res.json()).map((p) => new QuestionProposal(p))
} else {
throw new Error((await res.json()).errmsg);
}
}
async getMyResponse() {
const res = await fetch(`api/questions/${this.id}/response`, {
method: 'GET',
headers: {'Accept': 'application/json'},
});
if (res.status == 200) {
return new Response(await res.json());
} else {
throw new Error((await res.json()).errmsg);
}
}
async getResponses() {
const res = await fetch(`api/surveys/${this.id_survey}/questions/${this.id}/responses`, {
method: 'GET',
headers: {'Accept': 'application/json'},
});
if (res.status == 200) {
return (await res.json()).map((r) => new Response(r))
} else {
throw new Error((await res.json()).errmsg);
}
}
async save() {
const res = await fetch(this.id?`api/questions/${this.id}`:`api/surveys/${this.id_survey}/questions`, {
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 delete() {
if (this.id) {
const res = await fetch(`api/questions/${this.id}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'},
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
}
}
}
export async function getQuestion(qid) {
const res = await fetch(`api/questions/${qid}`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return new Question(await res.json());
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getQuestions(sid) {
const res = await fetch(`api/surveys/${sid}/questions`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return (await res.json()).map((e) => new Question(e))
} else {
throw new Error((await res.json()).errmsg);
}
}

34
ui/src/lib/response.js Normal file
View file

@ -0,0 +1,34 @@
export class Response {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, id_question, id_user, value, time_submit, score, score_explaination, id_corrector, time_scored }) {
this.id = id;
this.id_question = id_question;
this.id_user = id_user;
this.value = value;
this.time_submit = time_submit;
this.score = score;
this.score_explaination = score_explaination;
this.id_corrector = id_corrector;
this.time_scored = time_scored;
}
async save() {
const res = await fetch(`api/questions/${this.id_question}/responses/${this.id}`, {
method: 'PUT',
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);
}
}
}

145
ui/src/lib/surveys.js Normal file
View file

@ -0,0 +1,145 @@
import { getQuestions } from './questions';
class Survey {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, title, promo, group, shown, direct, corrected, start_availability, end_availability }) {
this.id = id;
this.title = title;
this.promo = promo;
this.group = group;
this.shown = shown;
this.direct = direct;
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) {
return await res.json();
} 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) {
delete q.id;
q.id_survey = response.id;
q.save();
}
return response;
} else {
throw new Error((await res.json()).errmsg);
}
}
}
async delete() {
if (this.id) {
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);
}
}
}
}
export async function getSurveys() {
const res = await fetch(`api/surveys`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return (await res.json()).map((s) => new Survey(s));
} 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);
}
}

61
ui/src/lib/users.js Normal file
View file

@ -0,0 +1,61 @@
export async function getPromos() {
const res = await fetch('api/promos', {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getUsers() {
const res = await fetch('api/users', {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getUser(uid) {
const res = await fetch(`api/users/${uid}`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getGrades(uid, survey) {
const res = await fetch(`api/grades`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getUserGrade(uid, survey) {
const res = await fetch(`api/users/${uid}/surveys/${survey.id}/grades`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getUserScore(uid, survey) {
const res = await fetch(`api/users/${uid}/surveys/${survey.id}/score`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getScore(survey) {
const res = await fetch(`api/surveys/${survey.id}/score`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}