adejs/controllers/total/views.js

251 lines
6.7 KiB
JavaScript
Raw Normal View History

const config = require('settings/config');
const cal = require('calendar');
const redirectIfNotLogged = require('auth/views').redirectIfNotLogged;
2017-10-03 10:54:49 +02:00
const getUrl = require('create-url').getUrl;
2017-09-24 00:38:12 +02:00
2018-12-04 14:53:17 +01:00
function stdTimezoneOffset(date) {
var jan = new Date(date.getFullYear(), 0, 1);
var jul = new Date(date.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
function isDstObserved(date) {
return date.getTimezoneOffset() < stdTimezoneOffset(date);
}
2018-11-15 17:19:13 +01:00
function compareStrings(a, b) {
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
function computeUserTable(user, callback) {
let result = {};
cal.getTotal(user, (table) => {
2017-09-24 00:38:12 +02:00
let courses = [];
let total = {
name: "Total",
2017-09-24 00:39:13 +02:00
type: "",
2017-09-24 00:38:12 +02:00
time: 0,
tdEquivalent: 0,
}
for (let key in table) {
courses.push({
name: key,
type: table[key].type,
time: table[key].time,
tdEquivalent: table[key].tdEquivalent,
});
total.time += table[key].time;
total.tdEquivalent += table[key].tdEquivalent;
}
result.courses = courses;
result.total = total;
2017-09-24 00:38:12 +02:00
callback(result);
2017-09-24 00:38:12 +02:00
});
}
2017-10-03 10:54:49 +02:00
function computeUserTableByCourse(user, callback) {
let result = {};
cal.getTotalByCourse(user, (table) => {
let courses = [];
let total = { name: "Total" };
for (let type in cal.Type) {
total[type] = 0;
}
total.totalTdEquivalent = 0;
for (let key in table) {
let item = {};
item.name = key;
for (let type in cal.Type) {
item[type] = table[key][type];
total[type] += table[key][type];
}
item.totalTdEquivalent = table[key].totalTdEquivalent;
total.totalTdEquivalent += table[key].totalTdEquivalent;
courses.push(item);
}
result.courses = courses.sort((a,b) => -(a.totalTdEquivalent - b.totalTdEquivalent));
2017-10-03 10:54:49 +02:00
result.total = total;
callback(result);
});
}
module.exports.total = redirectIfNotLogged('total', function(req, res, render) {
2017-10-03 10:54:49 +02:00
res.locals.tableUrl = getUrl('totalTable');
render('total.pug');
});
module.exports.totalByCourse = redirectIfNotLogged('totalByCourse', function(req, res, render) {
res.locals.tableUrl = getUrl('totalTableByCourse');
render('total.pug');
});
module.exports.totalTable = function(req, res, render, next) {
if (req.session.user === undefined) {
res.status(404);
res.setHeader('Content-Type', 'text/html');
res.render(config.BASE_DIR + '/templates/404.pug', res.locals, function(err, result) {
if (err)
console.log(err);
res.send(result);
});
return;
}
computeUserTable(req.session.user, (result) => {
res.locals.courses = result.courses;
2017-09-25 17:17:24 +02:00
res.locals.total = result.total;
render('totalTable.pug');
2017-09-24 00:38:12 +02:00
});
}
2017-10-03 10:54:49 +02:00
module.exports.totalTableByCourse = function(req, res, render, next) {
if (req.session.user === undefined) {
res.status(404);
res.setHeader('Content-Type', 'text/html');
res.render(config.BASE_DIR + '/templates/404.pug', res.locals, function(err, result) {
if (err)
console.log(err);
res.send(result);
});
return;
}
computeUserTableByCourse(req.session.user, (result) => {
res.locals.types = [];
for (let type in cal.Type) {
res.locals.types.push(type);
}
2017-10-04 11:25:59 +02:00
res.locals.courses = [];
res.locals.noCourses = [];
for (let course of result.courses) {
if (course.totalTdEquivalent > 0) {
res.locals.courses.push(course);
} else {
res.locals.noCourses.push(course);
}
}
2017-10-03 10:54:49 +02:00
res.locals.total = result.total;
render('totalTableByCourse.pug');
});
}
2018-11-15 17:19:13 +01:00
module.exports.histogram = redirectIfNotLogged('histogram', function(req, res, render) {
render('histogram.pug');
});
module.exports.histogramData = function(req, res, render, next) {
if (req.session.user === undefined) {
res.status(404);
res.setHeader('Content-Type', 'text/html');
res.render(config.BASE_DIR + '/templates/404.pug', res.locals, function(err, result) {
if (err)
console.log(err);
res.send(result);
});
return;
}
cal.getCalendar(req.session.user, new Date(2000, 1, 1), new Date(3000, 1, 1), (calendar) => {
let histogram = {};
for (let event of calendar.events) {
let hours = event.startTime.getHours();
if (hours % 2 === 0) {
hours += 2;
} else {
hours += 1;
}
hours = hours.toString().padStart(2, '0');
let minutes = event.startTime.getMinutes().toString().padStart(2, '0');
let key = hours + ':' + minutes;
histogram[key] = histogram[key] || 0;
histogram[key]++;
}
let histogramArray = [];
for (let key in histogram) {
histogramArray.push({
time: key,
count: histogram[key],
});
}
histogramArray.sort((a, b) => compareStrings(a.time, b.time));
res.send(JSON.stringify(histogramArray));
});
}
2018-12-04 14:53:17 +01:00
2018-12-04 15:00:34 +01:00
module.exports.calendar = redirectIfNotLogged('calendar', function(req, res, render) {
2018-12-04 14:53:17 +01:00
res.locals.calendarUrl = getUrl('calendarData');
render('calendar.pug');
});
module.exports.calendarData = function(req, res, render) {
if (req.session.user === undefined) {
res.status(404);
res.setHeader('Content-Type', 'text/html');
res.render(config.BASE_DIR + '/templates/404.pug', res.locals, function(err, result) {
if (err)
console.log(err);
res.send(result);
});
return;
}
cal.getCalendar(req.session.user, new Date(2000, 1, 1), new Date(3000, 1, 1), (calendar) => {
let events = [];
for (let event of calendar.events) {
if (isDstObserved(event.startTime)) {
event.startTime.setTime(event.startTime.getTime() + (60 * 60 * 1000));
}
if (isDstObserved(event.finishTime)) {
event.finishTime.setTime(event.finishTime.getTime() + (60 * 60 * 1000));
}
events.push({
2018-12-11 14:18:12 +01:00
title: event.name + '\n' + event.location,
2018-12-04 14:53:17 +01:00
start: event.startTime,
end: event.finishTime,
});
}
res.send(JSON.stringify(events));
});
}