MonScoDocEssai/app/templates/assiduites/widgets/moduleimpl_dynamic_selector.j2

133 lines
3.6 KiB
Plaintext
Raw Normal View History

2023-04-17 15:44:55 +02:00
<label for="moduleimpl_select">
Module
<select id="moduleimpl_select" class="dynaSelect">
2023-04-17 15:44:55 +02:00
<option value="" selected> Non spécifié </option>
</select>
</label>
<script>
function getEtudFormSemestres() {
let semestre = {};
sync_get(getUrl() + `/api/etudiant/etudid/${etudid}/formsemestres`, (data) => {
semestre = data;
});
return semestre;
}
function filterFormSemestres(semestres, dateIso) {
2023-04-17 15:44:55 +02:00
const date = new moment.tz(
dateIso,
2023-04-17 15:44:55 +02:00
TIMEZONE
);
semestres = semestres.filter((fm) => {
return date.isBetween(fm.date_debut_iso, fm.date_fin_iso, null, '[]')
2023-04-17 15:44:55 +02:00
})
return semestres;
}
function getFormSemestreProgramme(fm_id) {
let semestre = {};
sync_get(getUrl() + `/api/formsemestre/${fm_id}/programme`, (data) => {
semestre = data;
});
return semestre;
}
function getModulesImplByFormsemestre(semestres) {
const map = new Map();
semestres.forEach((fm) => {
const array = [];
const fm_p = getFormSemestreProgramme(fm.formsemestre_id);
["ressources", "saes", "modules"].forEach((r) => {
if (r in fm_p) {
fm_p[r].forEach((o) => {
array.push(getModuleInfos(o))
})
}
})
map.set(fm.titre_num, array)
})
return map;
}
function getModuleInfos(obj) {
return {
moduleimpl_id: obj.moduleimpl_id,
titre: obj.module.titre,
code: obj.module.code,
}
}
function populateSelect(sems, selected, query) {
const select = document.querySelector(query);
2023-04-17 15:44:55 +02:00
select.innerHTML = `<option value="" selected> Non spécifié </option>`
sems.forEach((mods, label) => {
const optGrp = document.createElement('optgroup');
optGrp.label = label
mods.forEach((obj) => {
const opt = document.createElement('option');
opt.value = obj.moduleimpl_id;
opt.textContent = `${obj.code} ${obj.titre}`
if (obj.moduleimpl_id == selected) {
opt.setAttribute('selected', 'true');
}
optGrp.appendChild(opt);
})
select.appendChild(optGrp);
})
}
function updateSelect(moduleimpl_id, query = "#moduleimpl_select", dateIso = null) {
2023-04-17 15:44:55 +02:00
let sem = getEtudFormSemestres()
if (dateIso == null) {
dateIso = document.querySelector("#tl_date").value
}
sem = filterFormSemestres(sem, dateIso)
2023-04-17 15:44:55 +02:00
const mod = getModulesImplByFormsemestre(sem)
populateSelect(mod, moduleimpl_id, query);
2023-04-17 15:44:55 +02:00
}
function updateSelectedSelect(moduleimpl_id, query = "#moduleimpl_select") {
const mod_id = moduleimpl_id != null ? moduleimpl_id : ""
document.querySelector(query).value = mod_id;
}
window.addEventListener("load", () => {
document.getElementById('moduleimpl_select').addEventListener('change', (el) => {
const assi = getCurrentAssiduite(etudid);
if (assi) {
editAssiduite(assi.assiduite_id, assi.etat);
}
})
const conflicts = getAssiduitesConflict(etudid);
if (conflicts.length > 0) {
updateSelectedSelect(conflicts[0].moduleimpl_id);
}
}, { once: true });
2023-04-17 15:44:55 +02:00
</script>
<style>
#moduleimpl_select {
width: 125px;
text-overflow: ellipsis;
}
</style>