From 913797fd087d1d7fb67c2bb10c839c7df76c8d98 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 3 Oct 2017 10:54:49 +0200 Subject: [PATCH] Added total by course page --- controllers/total/templates/total.pug | 2 + .../total/templates/totalTableByCourse.pug | 27 ++++++++ controllers/total/urls.js | 2 + controllers/total/views.js | 65 +++++++++++++++++++ static/js/totalTable.js | 2 +- templates/base.pug | 2 + utils/calendar.js | 29 +++++++++ 7 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 controllers/total/templates/totalTableByCourse.pug diff --git a/controllers/total/templates/total.pug b/controllers/total/templates/total.pug index 3e8068c..75e0f72 100644 --- a/controllers/total/templates/total.pug +++ b/controllers/total/templates/total.pug @@ -43,4 +43,6 @@ block extracss } block extrajs + script. + window.TABLE_URL = "#{tableUrl}"; script(src="/static/js/totalTable.js") diff --git a/controllers/total/templates/totalTableByCourse.pug b/controllers/total/templates/totalTableByCourse.pug new file mode 100644 index 0000000..303e4fb --- /dev/null +++ b/controllers/total/templates/totalTableByCourse.pug @@ -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 + open an issue + exaplaining what are the problem so we can fix it. Thanks! diff --git a/controllers/total/urls.js b/controllers/total/urls.js index a0be341..62149d8 100644 --- a/controllers/total/urls.js +++ b/controllers/total/urls.js @@ -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'), ] diff --git a/controllers/total/views.js b/controllers/total/views.js index bb7037d..3c9915f 100644 --- a/controllers/total/views.js +++ b/controllers/total/views.js @@ -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'); + }); +} diff --git a/static/js/totalTable.js b/static/js/totalTable.js index 11f2bf3..94f42d3 100644 --- a/static/js/totalTable.js +++ b/static/js/totalTable.js @@ -19,7 +19,7 @@ function getTable() { } } }; - xhr.open('GET', '/total-table', true); + xhr.open('GET', TABLE_URL, true); xhr.send(); } diff --git a/templates/base.pug b/templates/base.pug index 4e723ff..2399536 100644 --- a/templates/base.pug +++ b/templates/base.pug @@ -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 diff --git a/utils/calendar.js b/utils/calendar.js index 8ff7f1a..bea2e15 100644 --- a/utils/calendar.js +++ b/utils/calendar.js @@ -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;