Added total by course page

This commit is contained in:
Thomas Forgione
2017-10-03 10:54:49 +02:00
parent 5ad9d41eca
commit 913797fd08
7 changed files with 128 additions and 1 deletions

View File

@@ -43,4 +43,6 @@ block extracss
}
block extrajs
script.
window.TABLE_URL = "#{tableUrl}";
script(src="/static/js/totalTable.js")

View File

@@ -0,0 +1,27 @@
table.table.table-bordered.table-striped.table-hover
thead
tr
th Course name
each type in types
th #{type}
th Total TD equivalent
tbody
each course in courses
tr
td #{course.name}
each type in types
td #{course[type]}
td #{course.totalTdEquivalent}
tr.table-active
td
strong #{total.name}
each type in types
td
strong #{total[type]}
td
strong #{total.totalTdEquivalent}
p.
If you think there is a mistake in this table, feel free to
<a href="https://github.com/tforgione/adejs/issues/new">open an issue</a>
exaplaining what are the problem so we can fix it. Thanks!

View File

@@ -2,5 +2,7 @@ const url = require('create-url').url;
module.exports = [
url('/total/', 'total', 'total'),
url('/total-by-course/', 'totalByCourse', 'totalByCourse'),
url('/total-table', 'totalTable', 'totalTable'),
url('/total-table-by-course', 'totalTableByCourse', 'totalTableByCourse'),
]

View File

@@ -1,6 +1,7 @@
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) {
@@ -35,7 +36,46 @@ function computeUserTable(user, callback) {
});
}
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;
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');
});
@@ -59,3 +99,28 @@ module.exports.totalTable = function(req, res, render, next) {
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');
});
}