moodle_local_treestudyplan/amd/src/page-coach.js

187 lines
6.8 KiB
JavaScript
Raw Normal View History

2024-03-09 00:11:42 +01:00
/*eslint no-var: "error" */
/*eslint no-unused-vars: "off" */
/*eslint linebreak-style: "off" */
/*eslint no-trailing-spaces: "off" */
/*eslint-env es6*/
// 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';
import {load_strings} from './util/string-helper';
import {ProcessStudyplan} from './studyplan-processor';
import {studyplanTiming} from './util/date-helper';
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
let strings = load_strings({
2024-03-09 22:51:34 +01:00
coach: {
2024-03-09 00:11:42 +01:00
studyplan_select_placeholder: 'studyplan_select_placeholder',
2024-03-09 22:51:34 +01:00
switch_coach_editmode: 'switch_coach_editmode',
2024-03-09 00:11:42 +01:00
},
});
/**
* Initialize the Page
*/
export function init() {
let app = new Vue({
el: '#root',
data: {
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: {}
}])[0].then(function(response){
const timingval = { present: 0, past: 1, future: 2};
response.sort((a,b) => {
const timinga = studyplanTiming(a);
const timingb = studyplanTiming(b);
const t = timingval[timinga] - timingval[timingb];
if(t == 0){
// sort by name if timing is equal
return a.name.localeCompare(b.name);
}
else {
return t;
}
});
app.studyplans = response;
// load studyplan from hash if applicable
const hash = window.location.hash.replace('#','');
const parts = hash.split("-");
if(!!parts && parts.length > 0){
for(let idx in app.studyplans){
if(app.studyplans[idx].id == parts[0]){
app.selectStudyplan(app.studyplans[idx],parts[1]);
break;
}
}
}
}).catch(notification.exception);
},
computed: {
},
methods: {
closeStudyplan() {
app.activestudyplan = null;
app.associatedstudents = [];
app.studentstudyplan = [];
app.displayedstudyplan = null;
window.location.hash = '';
},
selectStudyplan(studyplan,studentid){
// fetch studyplan
2024-03-09 22:51:34 +01:00
const self = this;
self.loadingstudyplan = true;
self.activestudyplan = null;
self.associatedstudents = [];
self.selectedstudent = null;
self.studentstudyplan = null;
2024-03-09 00:11:42 +01:00
call([{
methodname: 'local_treestudyplan_get_studyplan_map',
args: { id: studyplan.id}
}])[0].then(function(response){
2024-03-09 22:51:34 +01:00
self.activestudyplan = ProcessStudyplan(response,true);
self.displayedstudyplan = self.activestudyplan;
self.loadingstudyplan = false;
window.location.hash = self.activestudyplan.id;
2024-03-09 00:11:42 +01:00
call([{
methodname: 'local_treestudyplan_all_associated_grouped',
args: { studyplan_id: studyplan.id}
}])[0].then(function(response){
2024-03-09 22:51:34 +01:00
self.associatedstudents = response;
2024-03-09 00:11:42 +01:00
if(studentid){
2024-03-09 22:51:34 +01:00
for(const group of self.associatedstudents) {
2024-03-09 00:11:42 +01:00
for(const student of group.users){
if(student.id == studentid){
2024-03-09 22:51:34 +01:00
self.showStudentView(student);
2024-03-09 00:11:42 +01:00
break;
}
}
}
2024-03-09 22:51:34 +01:00
} else {
// Select first student available.
for(const group of self.associatedstudents) {
for(const student of group.users){
self.showStudentView(student);
break;
}
}
2024-03-09 00:11:42 +01:00
}
}).catch(notification.exception);
}).catch(function(error){
notification.exception(error);
app.loadingstudyplan = false;
});
},
showStudentView(student){
app.selectedstudent = student;
app.studentstudyplan = null;
if (student) {
app.loadingstudyplan = true;
call([{
methodname: 'local_treestudyplan_get_user_studyplan',
args: { userid: student.id, studyplanid: app.activestudyplan.id}
}])[0].then(function(response){
app.studentstudyplan = ProcessStudyplan(response,false);
app.displayedstudyplan = app.studentstudyplan;
app.loadingstudyplan = false;
window.location.hash = app.activestudyplan.id + "-" + student.id;
}).catch(function(error){
notification.exception(error);
app.loadingstudyplan = false;
});
}
},
showOverview(){
app.selectedstudent = null;
app.studentstudyplan = null;
app.displayedstudyplan = app.activestudyplan;
window.location.hash = app.activestudyplan.id;
}
},
});
}