moodle_local_treestudyplan/amd/src/page-coach.js

262 lines
10 KiB
JavaScript
Raw Normal View History

2024-06-03 23:24:16 +02:00
/* eslint no-var: "error" */
/* eslint no-unused-vars: "off" */
/* eslint linebreak-style: "off" */
/* eslint no-trailing-spaces: "off" */
2024-07-19 17:48:21 +02:00
/* eslint promise/no-nesting: "off" */
2024-06-03 23:24:16 +02:00
/* eslint max-depth: ["error", 6]*/
/* eslint-env es6*/
2024-03-09 00:11:42 +01:00
// Put this file in path/to/plugin/amd/src
// You can call it anything you like
import {call} from 'core/ajax';
import notification from 'core/notification';
import Vue from './vue/vue';
import Debugger from './util/debugger';
2024-06-03 23:24:16 +02:00
import {loadStrings} from './util/string-helper';
import {processStudyplan} from './studyplan-processor';
2024-03-09 00:11:42 +01:00
import {studyplanTiming} from './util/date-helper';
2024-06-02 17:21:30 +02:00
import {addBrowserButtonEvent} from './util/browserbuttonevents';
2024-03-09 00:11:42 +01:00
2024-03-09 22:51:34 +01:00
import EditorComponents from './studyplan-editor-components';
Vue.use(EditorComponents);
import TSComponents from './treestudyplan-components';
Vue.use(TSComponents);
2024-03-09 00:11:42 +01:00
import RVComponents from './report-viewer-components';
Vue.use(RVComponents);
2024-03-09 22:51:34 +01:00
2024-03-09 00:11:42 +01:00
import ModalComponents from './modedit-modal';
Vue.use(ModalComponents);
import PortalVue from './portal-vue/portal-vue.esm';
Vue.use(PortalVue);
import BootstrapVue from './bootstrap-vue/bootstrap-vue';
Vue.use(BootstrapVue);
2024-03-09 22:51:34 +01:00
let debug = new Debugger("treestudyplancoach");
2024-03-09 00:11:42 +01:00
2024-06-03 23:24:16 +02:00
let strings = loadStrings({
2024-03-09 22:51:34 +01:00
coach: {
2024-03-09 00:11:42 +01:00
},
});
/**
* Initialize the Page
*/
export function init() {
let app = new Vue({
el: '#root',
data: {
2024-06-02 17:21:30 +02:00
selected: {
planid: 0,
studentid: 0,
},
2024-03-09 00:11:42 +01:00
displayedstudyplan: null,
activestudyplan: null,
associatedstudents: [],
selectedstudent: null,
studentstudyplan: null,
loadingstudyplan: false,
studyplans: [],
2024-03-09 22:51:34 +01:00
text: strings.coach,
2024-03-09 00:11:42 +01:00
toolbox: {
right: true,
},
usedcontexts: [],
2024-03-09 22:51:34 +01:00
editmode: false,
2024-03-09 00:11:42 +01:00
},
async mounted() {
call([{
2024-03-09 22:51:34 +01:00
methodname: 'local_treestudyplan_list_coaching_studyplans',
2024-03-09 00:11:42 +01:00
args: {}
2024-06-03 23:24:16 +02:00
}])[0].then(function(response) {
const timingval = {present: 0, past: 1, future: 2};
response.sort((a, b) => {
2024-03-09 00:11:42 +01:00
const timinga = studyplanTiming(a);
const timingb = studyplanTiming(b);
const t = timingval[timinga] - timingval[timingb];
2024-06-03 23:24:16 +02:00
if (t == 0) {
// Sort by name if timing is equal
2024-03-09 00:11:42 +01:00
return a.name.localeCompare(b.name);
2024-06-03 23:24:16 +02:00
} else {
2024-03-09 00:11:42 +01:00
return t;
}
});
app.studyplans = response;
2024-06-03 23:24:16 +02:00
// Load studyplan from hash if applicable
const hash = window.location.hash.replace('#', '');
2024-03-09 00:11:42 +01:00
const parts = hash.split("-");
2024-06-03 23:24:16 +02:00
if (!!parts && parts.length > 0 && parts[0] != '') {
for (let idx in app.studyplans) {
if (app.studyplans[idx].id == parts[0]) {
app.selectStudyplan(app.studyplans[idx], parts[1], false);
2024-03-09 00:11:42 +01:00
break;
}
}
}
2024-06-03 23:24:16 +02:00
return;
2024-03-09 00:11:42 +01:00
}).catch(notification.exception);
2024-06-02 17:21:30 +02:00
addBrowserButtonEvent(this.navChanged, this.navChanged);
2024-03-09 00:11:42 +01:00
},
computed: {
studentcount() {
let count = 0;
for (const group of app.associatedstudents) {
count += group.users.length;
}
return count;
}
2024-03-09 00:11:42 +01:00
},
methods: {
2024-06-02 17:21:30 +02:00
navChanged() {
2024-06-03 23:24:16 +02:00
const hash = window.location.hash.replace('#', '');
2024-06-02 17:21:30 +02:00
const parts = hash.split("-");
2024-06-03 23:24:16 +02:00
debug.log("Navigation changed", hash, parts);
2024-06-02 17:21:30 +02:00
2024-06-03 23:24:16 +02:00
if (!!parts && parts.length > 0) {
2024-06-02 17:21:30 +02:00
const planid = Number(parts[0]);
2024-06-03 23:24:16 +02:00
const studentid = (parts.length > 1) ? Number(parts[1]) : 0;
2024-06-02 17:21:30 +02:00
2024-06-03 23:24:16 +02:00
debug.log("Selected ids", planid, studentid, this.selected.planid, this.selected.studentid);
if (planid == 0) {
2024-06-02 17:21:30 +02:00
if (planid != this.selected.planid) {
this.closeStudyplan(false);
}
2024-06-03 23:24:16 +02:00
} else if (this.selected.planid != planid || (studentid == 0 && this.selected.studentid != 0)) {
debug.info("Requested plan changed - loading studyplan");
for (let idx in app.studyplans) {
2024-06-02 17:21:30 +02:00
const plan = this.studyplans[idx];
2024-06-03 23:24:16 +02:00
if (Number(plan.id) == planid) {
this.selectStudyplan(plan, studentid, false);
2024-06-02 17:21:30 +02:00
break;
}
}
} else if (this.selected.studentid != studentid) {
2024-06-03 23:24:16 +02:00
for (const group of app.associatedstudents) {
for (const student of group.users) {
if (Number(student.id) == studentid) {
app.showStudentView(student, false);
2024-06-02 17:21:30 +02:00
break;
}
}
}
}
}
},
2024-06-03 23:24:16 +02:00
closeStudyplan(updatehash = true) {
2024-06-02 17:21:30 +02:00
app.selected.planid = 0;
app.selected.studentid = 0;
2024-03-09 00:11:42 +01:00
app.activestudyplan = null;
app.associatedstudents = [];
app.studentstudyplan = [];
app.displayedstudyplan = null;
2024-06-02 17:21:30 +02:00
if (updatehash) {
window.location.hash = '';
}
2024-03-09 00:11:42 +01:00
},
2024-06-03 23:24:16 +02:00
selectStudyplan(studyplan, studentid, updatehash = true) {
2024-06-02 17:21:30 +02:00
app.selected.planid = Number(studyplan.id);
2024-06-03 23:24:16 +02:00
app.selected.studentid = studentid ? Number(studentid) : 0;
// Fetch studyplan
2024-03-09 22:51:34 +01:00
const self = this;
self.loadingstudyplan = true;
self.associatedstudents = [];
self.selectedstudent = null;
self.studentstudyplan = null;
2024-03-09 00:11:42 +01:00
call([{
methodname: 'local_treestudyplan_get_studyplan_map',
2024-06-03 23:24:16 +02:00
args: {id: studyplan.id}
}])[0].then((response) => {
self.activestudyplan = processStudyplan(response, true);
2024-06-02 17:21:30 +02:00
2024-03-09 00:11:42 +01:00
call([{
methodname: 'local_treestudyplan_all_associated_grouped',
2024-06-03 23:24:16 +02:00
args: {'studyplan_id': studyplan.id}
}])[0].then(function(response) {
2024-03-09 22:51:34 +01:00
self.associatedstudents = response;
2024-06-02 17:21:30 +02:00
let foundstudent = false;
2024-06-03 23:24:16 +02:00
if (studentid) {
for (const group of self.associatedstudents) {
for (const student of group.users) {
if (student.id == studentid) {
2024-06-02 17:21:30 +02:00
foundstudent = true;
2024-03-09 22:51:34 +01:00
self.showStudentView(student);
2024-03-09 00:11:42 +01:00
break;
}
}
}
2024-06-02 17:21:30 +02:00
}
if (!foundstudent) {
2024-03-09 22:51:34 +01:00
// Select first student available.
2024-06-03 23:24:16 +02:00
for (const group of self.associatedstudents) {
for (const student of group.users) {
2024-06-02 17:21:30 +02:00
foundstudent = true;
2024-03-09 22:51:34 +01:00
self.showStudentView(student);
break;
}
}
2024-03-09 00:11:42 +01:00
}
2024-06-03 23:24:16 +02:00
if (!foundstudent) {
// Update hash with just the studyplan if no student was available for display
2024-06-02 17:21:30 +02:00
app.selected.studentid = 0;
if (updatehash) {
window.location.hash = app.activestudyplan.id;
}
self.displayedstudyplan = self.activestudyplan;
self.loadingstudyplan = false;
}
2024-06-03 23:24:16 +02:00
return;
2024-03-09 00:11:42 +01:00
}).catch(notification.exception);
2024-06-03 23:24:16 +02:00
return;
}).catch(function(error) {
2024-03-09 00:11:42 +01:00
notification.exception(error);
app.loadingstudyplan = false;
});
},
2024-06-03 23:24:16 +02:00
showStudentView(student, updatehash = true) {
app.selected.studentid = student ? Number(student.id) : 0;
2024-03-09 00:11:42 +01:00
if (student) {
2024-06-02 17:21:30 +02:00
app.selectedstudent = student;
app.studentstudyplan = null;
2024-03-09 00:11:42 +01:00
app.loadingstudyplan = true;
call([{
methodname: 'local_treestudyplan_get_user_studyplan',
2024-06-03 23:24:16 +02:00
args: {userid: student.id, studyplanid: app.activestudyplan.id}
}])[0].then((response) => {
app.studentstudyplan = processStudyplan(response, false);
2024-03-09 00:11:42 +01:00
app.displayedstudyplan = app.studentstudyplan;
app.loadingstudyplan = false;
2024-06-02 17:21:30 +02:00
if (updatehash) {
window.location.hash = app.activestudyplan.id + "-" + student.id;
}
2024-06-03 23:24:16 +02:00
return;
}).catch(function(error) {
2024-03-09 00:11:42 +01:00
notification.exception(error);
app.loadingstudyplan = false;
});
2024-06-02 17:21:30 +02:00
} else {
this.showOverview(updatehash);
2024-03-09 00:11:42 +01:00
}
},
2024-06-03 23:24:16 +02:00
showOverview(updatehash = true) {
debug.info("Switch to overview", updatehash);
2024-06-02 17:21:30 +02:00
app.selected.studentid = 0;
2024-03-09 00:11:42 +01:00
app.selectedstudent = null;
app.studentstudyplan = null;
app.displayedstudyplan = app.activestudyplan;
2024-06-02 17:21:30 +02:00
if (updatehash) {
window.location.hash = app.activestudyplan.id;
}
2024-03-09 00:11:42 +01:00
}
},
});
}