const config = require('settings/config'); const cal = require('calendar'); const redirectIfNotLogged = require('auth/views').redirectIfNotLogged; const getUrl = require('create-url').getUrl; function computeUserTable(user, callback) { let result = {}; cal.getTotal(user, (table) => { let courses = []; let total = { name: "Total", type: "", 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; callback(result); }); } 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)); result.total = total; callback(result); }); } module.exports.total = redirectIfNotLogged('total', function(req, res, render) { 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; res.locals.total = result.total; render('totalTable.pug'); }); } 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); } res.locals.courses = result.courses; res.locals.total = result.total; render('totalTableByCourse.pug'); }); }