CSOL-site/controllers/program.js

207 строки
5.0 KiB
JavaScript
Исходник Обычный вид История

2013-05-13 22:53:01 +04:00
var badger = require('../openbadger');
2013-05-08 18:14:31 +04:00
2013-03-25 00:52:09 +04:00
module.exports = function (app) {
2013-03-28 17:14:16 +04:00
function getFilters() {
var filters = [],
requested;
2013-03-25 00:52:09 +04:00
2013-03-28 17:14:16 +04:00
if (arguments.length) {
requested = Array.prototype.splice.call(arguments, 0);
} else {
2013-03-28 20:42:32 +04:00
requested = ['categories', 'grouped_programs', 'ages'];
2013-03-28 17:14:16 +04:00
}
requested.forEach(function(filter) {
switch (filter) {
2013-03-28 20:42:32 +04:00
case 'categories':
case 'category':
2013-03-28 17:14:16 +04:00
filters.push({
2013-03-28 20:42:32 +04:00
name: 'category',
label: 'Category',
2013-03-28 17:14:16 +04:00
options: {
science: 'Science',
technology: 'Technology',
engineering: 'Engineering',
art: 'Art',
math: 'Math'
}
});
break;
case 'orgs':
case 'org':
filters.push({
name: 'org',
label: 'Organisation',
2013-03-28 20:42:32 +04:00
options: {
'org1': 'Org 1',
'org2': 'Org 2',
'org3': 'Org 3',
}
});
break;
case 'programs':
case 'program':
filters.push({
name: 'program',
label: 'Program',
options: {
'p1': 'Program 1',
'p2': 'Program 2',
'p3': 'Program 3',
'p4': 'Program 4',
'p5': 'Program 5',
'p6': 'Program 6'
}
});
break;
case 'grouped_programs':
case 'grouped_program':
filters.push({
name: 'program',
label: 'Program',
options: {
'Org 1': {
'p1': 'Program 1',
'p2': 'Program 2'
},
'Org 2': {
'p3': 'Program 3',
'p4': 'Program 4'
},
'Org 3': {
'p5': 'Program 5',
'p6': 'Program 6'
}
},
is_grouped: true
2013-03-28 17:14:16 +04:00
});
break;
case 'ages':
case 'age':
filters.push({
name: 'age',
label: 'Age Group',
options: {
'lt-13': 'Under 13',
'13-14': '13 to 14',
'15-16': '15 to 16',
'17-18': '17 to 18',
'gt-18': 'Over 18'
}
});
break;
}
});
return filters;
}
app.param('programName', function (req, res, next, programName) {
badger.getProgram(programName, function(err, data) {
if (err)
2013-05-16 17:19:30 +04:00
return next(err);
2013-03-28 20:42:32 +04:00
req.params.program = data.program;
next();
});
});
2013-03-28 20:42:32 +04:00
app.get('/learn', badger.middleware('getPrograms'), function (req, res, next) {
var data = req.remote;
2013-03-28 20:42:32 +04:00
res.render('programs/list.html', {
filters: getFilters('categories', 'orgs', 'ages'),
items: data.programs,
page: data.page,
pages: data.pages
2013-03-28 20:42:32 +04:00
});
2013-03-25 00:52:09 +04:00
});
app.get('/learn/:programName', function (req, res, next) {
res.render('programs/single.html', {
program: req.params.program
});
2013-03-28 20:42:32 +04:00
});
app.get('/learn/:programName/favorite', function (req, res, next) {
2013-03-28 20:42:32 +04:00
return res.redirect('/login', 303);
});
app.get('/learn/:programName/unfavorite', function (req, res, next) {
2013-03-28 20:42:32 +04:00
return res.redirect('/login', 303);
});
2013-03-28 17:14:16 +04:00
app.param('badgeName', function (req, res, next, badgeName) {
badger.getBadge(badgeName, function(err, data) {
2013-05-08 18:14:31 +04:00
if (err)
2013-05-16 17:19:30 +04:00
return next(err);
2013-05-08 18:14:31 +04:00
req.params.badge = data.badge;
next();
});
2013-03-28 17:14:16 +04:00
});
app.get('/earn', badger.middleware('getBadges'), function (req, res, next) {
2013-05-09 21:27:55 +04:00
var data = req.remote;
2013-05-08 18:14:31 +04:00
res.render('badges/list.html', {
filters: getFilters(),
2013-05-09 21:28:41 +04:00
items: data.badges,
page: data.page,
pages: data.pages
2013-05-08 18:14:31 +04:00
});
2013-03-28 17:14:16 +04:00
});
app.get('/earn/:badgeName', function (req, res, next) {
2013-05-08 18:14:31 +04:00
res.render('badges/single.html', {
badge: req.params.badge
});
2013-03-28 17:14:16 +04:00
});
app.get('/earn/:badgeName/claim', function (req, res, next) {
2013-03-28 17:14:16 +04:00
res.render('badges/claim.html');
});
app.get('/earn/:badgeName/favorite', function (req, res, next) {
2013-03-28 17:14:16 +04:00
return res.redirect('/login', 303);
});
app.get('/earn/:badgeName/unfavorite', function (req, res, next) {
2013-03-28 17:14:16 +04:00
return res.redirect('/favorites', 303);
2013-03-25 00:52:09 +04:00
});
app.get('/orgs', function (req, res, next) {
2013-03-28 17:14:16 +04:00
var orgs = [];
for (var i = 0; i < 12; ++i) {
orgs.push({
thumbnail: '/media/images/org.png',
2013-03-28 20:42:32 +04:00
description: 'Organisation blah irure...',
2013-03-28 17:14:16 +04:00
url: '/orgs/some-organisation'
});
}
res.render('orgs/list.html', {
2013-03-28 20:42:32 +04:00
filters: getFilters('categories', 'ages'),
2013-03-28 17:14:16 +04:00
items: orgs
});
2013-03-25 00:52:09 +04:00
});
app.param('orgName', function (req, res, next, orgName) {
// pull some stuff from the database probably
next();
});
2013-03-28 17:14:16 +04:00
app.get('/orgs/:orgName', function (req, res, next) {
res.render('orgs/single.html');
});
app.get('/orgs/:orgName/favorite', function (req, res, next) {
return res.redirect('/login', 303);
});
app.get('/orgs/:orgName/unfavorite', function (req, res, next) {
return res.redirect('/login', 303);
2013-03-25 00:52:09 +04:00
});
};