2022-07-08 09:53:50 +00:00
|
|
|
export class Work {
|
|
|
|
constructor(res) {
|
|
|
|
this.kind = "w";
|
|
|
|
if (res) {
|
|
|
|
this.update(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 16:03:51 +00:00
|
|
|
update({ id, title, promo, group, shown, tag, description, descr_raw, submission_url, corrected, start_availability, end_availability }) {
|
2022-07-08 09:53:50 +00:00
|
|
|
this.id = id;
|
|
|
|
this.title = title;
|
|
|
|
this.promo = promo;
|
|
|
|
this.group = group;
|
|
|
|
this.shown = shown;
|
2022-09-04 16:03:51 +00:00
|
|
|
this.tag = tag;
|
2022-09-04 15:59:36 +00:00
|
|
|
this.description = description;
|
|
|
|
this.descr_raw = descr_raw;
|
2022-07-08 09:53:50 +00:00
|
|
|
this.submission_url = submission_url;
|
|
|
|
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 save() {
|
|
|
|
const res = await fetch(this.id?`api/works/${this.id}`:'api/works', {
|
|
|
|
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/works`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {'Accept': 'application/json'},
|
|
|
|
body: JSON.stringify(this),
|
|
|
|
});
|
|
|
|
if (res.status == 200) {
|
|
|
|
return await res.json();
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete() {
|
|
|
|
if (this.id) {
|
|
|
|
const res = await fetch(`api/works/${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 11:14:17 +00:00
|
|
|
|
2022-09-05 02:40:49 +00:00
|
|
|
async getSubmission() {
|
|
|
|
const res = await fetch(`api/works/${this.id}/submission`, {
|
|
|
|
headers: {'Accept': 'application/json'}
|
|
|
|
});
|
|
|
|
if (res.status == 200) {
|
|
|
|
return await res.json();
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 11:14:17 +00:00
|
|
|
async getGrades() {
|
|
|
|
const res = await fetch(`api/works/${this.id}/grades`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {'Accept': 'application/json'},
|
|
|
|
});
|
|
|
|
if (res.status == 200) {
|
|
|
|
return await res.json();
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 09:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getWorks() {
|
|
|
|
const res = await fetch(`api/works`, {headers: {'Accept': 'application/json'}})
|
|
|
|
if (res.status == 200) {
|
|
|
|
return (await res.json()).map((s) => new Work(s));
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getWork(wid) {
|
|
|
|
const res = await fetch(`api/works/${wid}`, {headers: {'Accept': 'application/json'}})
|
|
|
|
if (res.status == 200) {
|
|
|
|
return new Work(await res.json());
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 12:02:45 +00:00
|
|
|
|
|
|
|
export async function getUserRendu(baseurl, user) {
|
|
|
|
const res = await fetch(baseurl.replace("%l", user.login).replace("%i", user.id))
|
|
|
|
if (res.status == 200) {
|
|
|
|
return await res.json();
|
|
|
|
} else if (res.status == 404) {
|
|
|
|
return await res.json();
|
|
|
|
} else {
|
|
|
|
throw new Error((await res.json()).errmsg);
|
|
|
|
}
|
|
|
|
}
|