Skip to content

Commit

Permalink
Merge pull request freeCodeCamp#82 from FreeCodeCamp/courseware
Browse files Browse the repository at this point in the history
Courseware
  • Loading branch information
Quincy Larson committed Feb 8, 2015
2 parents e98ff50 + 11459d3 commit f136b22
Show file tree
Hide file tree
Showing 38 changed files with 31,802 additions and 146 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ lib-cov
*.pid
*.gz
*.swp
.floo
.flooignore

*.env
pids
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Changelog
Contributing
------------

We welcome pull requests from Free Code Camp "Code Campers" (our students) and seasoned JavaScript developers alike!
We welcome pull requests from Free Code Camp "campers" (our students) and seasoned JavaScript developers alike!
1) Check our [public Trello Board](https://trello.com/b/CW5AFr0v/free-code-camp-development)
2) If your issue or feature isn't on the board, either open an issue on this GitHub repo or message Quincy Larson to request to be added to the Trello board.
3) Once your code is ready, submit the pull request. We'll do a quick code review and give you feedback, and iterate from there.
Expand Down
86 changes: 12 additions & 74 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var express = require('express'),
userController = require('./controllers/user'),
contactController = require('./controllers/contact'),
bonfireController = require('./controllers/bonfire'),
coursewareController = require('./controllers/courseware'),

/**
* User model
Expand Down Expand Up @@ -282,81 +283,18 @@ app.get('/bonfire', function(req, res) {
res.redirect(301, '/playground');
});

app.post('/completed-bonfire/', function (req, res) {
var isCompletedWith = req.body.bonfireInfo.completedWith || undefined;
var isCompletedDate = Math.round(+new Date() / 1000);
var bonfireHash = req.body.bonfireInfo.bonfireHash;
var isSolution = req.body.bonfireInfo.solution;

if (isCompletedWith) {
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
paired.exec(function(err, pairedWith) {
if (err) {
return err;
} else {
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);

if (index > -1) {
req.user.uncompletedBonfires.splice(index,1)
}
pairedWith = pairedWith.pop();

index = pairedWith.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
pairedWith.uncompletedBonfires.splice(index,1)
}

pairedWith.completedBonfires.push({
_id: bonfireHash,
completedWith: req.user._id,
completedDate: isCompletedDate,
solution: isSolution
})

req.user.completedBonfires.push({
_id: bonfireHash,
completedWith: pairedWith._id,
completedDate: isCompletedDate,
solution: isSolution
})

req.user.save(function(err, user) {
pairedWith.save(function(err, paired) {
if (err) {
throw err;
}
if (user && paired) {
res.send(true);
}
})
});
}
})
} else {

req.user.completedBonfires.push({
_id: bonfireHash,
completedWith: null,
completedDate: isCompletedDate,
solution: isSolution
})

var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.uncompletedBonfires.splice(index,1)
}
app.post('/completed-bonfire/', bonfireController.completedBonfire);

req.user.save(function(err, user) {
if (err) {
throw err;
}
if (user) {
debug('Saving user');
res.send(true)
}
});
}
});
/**
* Courseware related routes
*/

app.get('/coursewares/', coursewareController.returnNextCourseware);
app.get(
'/coursewares/:coursewareName',
coursewareController.returnIndividualCourseware
);
app.post('/completed-courseware/', coursewareController.completedCourseware);

// Unique Check API route
app.get('/api/checkUniqueUsername/:username', userController.checkUniqueUsername);
Expand Down
140 changes: 113 additions & 27 deletions controllers/bonfire.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ var _ = require('lodash'),
* Bonfire controller
*/

var highestBonfireNumber = resources.numberOfBonfires();
exports.bonfireNames = function(req, res) {
res.render('bonfires/showList', {
bonfireList: resources.allBonfireNames()
});
};

exports.index = function(req, res) {
res.render('bonfire/show.jade', {
Expand All @@ -32,57 +36,60 @@ exports.index = function(req, res) {
});
};

exports.returnNextBonfire = function(req, res, next) {
exports.returnNextBonfire = function(req, res) {
if (!req.user) {
return res.redirect('../bonfires/meet-bonfire');
}
var currentTime = parseInt(+new Date() / 1000);
if (currentTime - req.user.lastContentSync > 10) {
req.user.lastContentSync = currentTime;
var completed = req.user.completedBonfires.map(function (elem) {
return elem._id;
});

req.user.uncompletedBonfires = resources.allBonfireIds().filter(function (elem) {
if (completed.indexOf(elem) === -1) {
return elem;
}
});
req.user.save();
}
var completed = req.user.completedBonfires.map(function (elem) {
return elem._id;
});

req.user.uncompletedBonfires = resources.allBonfireIds().filter(function (elem) {
if (completed.indexOf(elem) === -1) {
return elem;
}
});
req.user.save();

var uncompletedBonfires = req.user.uncompletedBonfires;


var displayedBonfires = Bonfire.find({'_id': uncompletedBonfires[0]});
displayedBonfires.exec(function(err, bonfire) {
if (err) {
next(err);
}

nameString = bonfire[0].name.toLowerCase().replace(/\s/g, '-');
return res.redirect('/bonfires/' + nameString);
bonfire = bonfire.pop();
if (bonfire === undefined) {
req.flash('errors', {
msg: "It looks like you've completed all the bonfires we have available. Good job!"
});
return res.redirect('../bonfires/meet-bonfire');
}
nameString = bonfire.name.toLowerCase().replace(/\s/g, '-');
return res.redirect('../bonfires/' + nameString);
});
};

exports.returnIndividualBonfire = function(req, res, next) {
var dashedName = req.params.bonfireName;

bonfireName = dashedName.replace(/\-/g, ' ');
var bonfireNumber = 0;

Bonfire.find({"name" : new RegExp(bonfireName, 'i')}, function(err, bonfire) {
if (err) {
next(err);
}


if (bonfire.length < 1) {
req.flash('errors', {
msg: "404: We couldn't find a bonfire with that name. Please double check the name."
});
return res.redirect('/bonfires/meet-bonfire');

return res.redirect('/bonfires');
}
bonfire = bonfire.pop();

bonfire = bonfire.pop()
var dashedNameFull = bonfire.name.toLowerCase().replace(/\s/g, '-');
if (dashedNameFull != dashedName) {
return res.redirect('../bonfires/' + dashedNameFull);
Expand Down Expand Up @@ -142,7 +149,7 @@ function randomString() {
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
};

/**
*
Expand Down Expand Up @@ -184,11 +191,11 @@ function getRidOfEmpties(elem) {
if (elem.length > 0) {
return elem;
}
}
};

exports.publicGenerator = function(req, res) {
res.render('bonfire/public-generator');
}
};

exports.generateChallenge = function(req, res) {
var bonfireName = req.body.name,
Expand All @@ -214,4 +221,83 @@ exports.generateChallenge = function(req, res) {
tests: bonfireTests
};
res.send(response);
}
};

exports.completedBonfire = function (req, res) {
var isCompletedWith = req.body.bonfireInfo.completedWith || undefined;
var isCompletedDate = Math.round(+new Date() / 1000);
var bonfireHash = req.body.bonfireInfo.bonfireHash;
var isSolution = req.body.bonfireInfo.solution;

if (isCompletedWith) {
var paired = User.find({"profile.username": isCompletedWith}).limit(1);
paired.exec(function (err, pairedWith) {
if (err) {
return err;
} else {
var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.points++;
req.user.uncompletedBonfires.splice(index, 1)
}
pairedWith = pairedWith.pop();

index = pairedWith.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
pairedWith.points++;
pairedWith.uncompletedBonfires.splice(index, 1);

}

pairedWith.completedBonfires.push({
_id: bonfireHash,
completedWith: req.user._id,
completedDate: isCompletedDate,
solution: isSolution
});

req.user.completedBonfires.push({
_id: bonfireHash,
completedWith: pairedWith._id,
completedDate: isCompletedDate,
solution: isSolution
})

req.user.save(function (err, user) {
pairedWith.save(function (err, paired) {
if (err) {
throw err;
}
if (user && paired) {
res.send(true);
}
})
});
}
})
} else {

req.user.completedBonfires.push({
_id: bonfireHash,
completedWith: null,
completedDate: isCompletedDate,
solution: isSolution
});

var index = req.user.uncompletedBonfires.indexOf(bonfireHash);
if (index > -1) {
req.user.points++;
req.user.uncompletedBonfires.splice(index, 1)
}

req.user.save(function (err, user) {
if (err) {
throw err;
}
if (user) {
debug('Saving user');
res.send(true)
}
});
}
};
3 changes: 1 addition & 2 deletions controllers/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ module.exports = {
},

getDoneWithFirst100Hours: function(req, res) {
console.log(req.user.points)
if (req.user.points >= 53) {
res.render('contact/done-with-first-100-hours', {
title: 'Congratulations on finishing the first 100 hours of Free Code Camp!'
Expand All @@ -63,7 +62,7 @@ module.exports = {
to: '[email protected]',
name: 'Completionist',
from: req.body.email,
subject: 'Code Camper at ' + req.body.email + ' has completed the first 100 hours',
subject: 'Camper at ' + req.body.email + ' has completed the first 100 hours',
text: ''
};

Expand Down
Loading

0 comments on commit f136b22

Please sign in to comment.