Added total by course page

This commit is contained in:
Thomas Forgione 2017-10-03 10:54:49 +02:00
parent 5ad9d41eca
commit 913797fd08
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
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');
});
}

View File

@ -19,7 +19,7 @@ function getTable() {
}
}
};
xhr.open('GET', '/total-table', true);
xhr.open('GET', TABLE_URL, true);
xhr.send();
}

View File

@ -29,6 +29,8 @@ html
if session.user !== undefined
li.nav-item
a.nav-link(href=getUrl("total")) Total
li.nav-item
a.nav-link(href=getUrl("totalByCourse")) Total by course
ul.navbar-nav.ml-auto
if session.user === undefined
li.nav-item

View File

@ -247,4 +247,33 @@ cal.getTotal = function(user, callback) {
}
cal.getTotalByCourse = function(user, callback) {
let firstDate = new Date(2017, 8, 1);
let lastDate = new Date(2018, 8, 1);
cal.getCalendar(user, firstDate, lastDate, (calendar) => {
let res = {};
for (let event of calendar.events) {
// Guess name of event, last element of split('-');
let split = event.name.split('-');
let name = split[split.length - 1];
if (res[name] === undefined) {
res[name] = {}
for (let type in cal.Type) {
res[name][type] = 0;
}
res[name].totalTdEquivalent = 0;
}
res[name][cal.getTypeName(event.type)]++;
res[name].totalTdEquivalent += event.getTdEquivalent();
}
callback(res);
});
}
module.exports = cal;