Merge branch 'dev'

This commit is contained in:
Thomas Forgione 2017-10-03 10:55:42 +02:00
commit 9860afd498
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
7 changed files with 129 additions and 2 deletions

View File

@ -43,4 +43,6 @@ block extracss
} }
block extrajs block extrajs
script(src=getStatic('js/totalTable.js')) 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 = [ module.exports = [
url('/total/', 'total', 'total'), url('/total/', 'total', 'total'),
url('/total-by-course/', 'totalByCourse', 'totalByCourse'),
url('/total-table', 'totalTable', 'totalTable'), url('/total-table', 'totalTable', 'totalTable'),
url('/total-table-by-course', 'totalTableByCourse', 'totalTableByCourse'),
] ]

View File

@ -1,6 +1,7 @@
const config = require('settings/config'); const config = require('settings/config');
const cal = require('calendar'); const cal = require('calendar');
const redirectIfNotLogged = require('auth/views').redirectIfNotLogged; const redirectIfNotLogged = require('auth/views').redirectIfNotLogged;
const getUrl = require('create-url').getUrl;
function computeUserTable(user, callback) { 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) { 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'); render('total.pug');
}); });
@ -59,3 +99,28 @@ module.exports.totalTable = function(req, res, render, next) {
render('totalTable.pug'); 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(); xhr.send();
} }

View File

@ -29,6 +29,8 @@ html
if session.user !== undefined if session.user !== undefined
li.nav-item li.nav-item
a.nav-link(href=getUrl("total")) Total a.nav-link(href=getUrl("total")) Total
li.nav-item
a.nav-link(href=getUrl("totalByCourse")) Total by course
ul.navbar-nav.ml-auto ul.navbar-nav.ml-auto
if session.user === undefined if session.user === undefined
li.nav-item 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; module.exports = cal;