1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
var dbFile = require("../node_simple.js");
var express = require('express');
var router = express.Router();
var csrf = require('csurf'); // Cross-Site Request Forgery prevention
var csrfProtection = csrf();
router.use(csrfProtection); // router is protected
var passport_file = require('../config/passport.js');
var bcrypt = require('bcrypt-nodejs');
var passport = require('passport');
/** Serve the main index.hbs page for a regular user (logged in or not) */
router.get('/', function(req, res, next) {
//addFirstAdmin();
res.render('index', {homePage: true, csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors});
req.session.errors = null;
req.session.success = null;
req.session.messages = null;
});
router.get('/auto-complete-courses', function(req, res, next) {
// console.log('in controller');
dbFile.find_all_course_codes_from_exams( function (success, data) {
var res_obj = {};
res_obj.success = success;
res_obj.data = data;
res.send(JSON.stringify(res_obj));
});
});
/** Serve the about.hbs page */
router.get('/about', function(req, res, next) {
res.render('about');
});
/** Serve the terms_and_conditions.hbs page */
router.get('/terms_and_conditions', function(req, res, next) {
res.render('terms_and_conditions');
});
/** Serve the privacy_poicy.hbs page */
router.get('/privacy_policy', function(req, res, next) {
res.render('privacy_policy');
});
/** Serve the profile page of a user with given username */
router.get('/public_profile/:username', function(req,res,next){
var username = req.params.username;
dbFile.retrieveUser(username, function(success, error, user, statusMsg){
if(success){
res.render('public_profile.hbs', {user: user});
}else{
req.session.messages = {error : statusMsg};
res.redirect('/user/' + username);
}
});
});
/** Serve the user_solutions.hbs page */
router.get('/user_solutions', function(req, res, next) {
res.render('user_solutions');
});
/**
* Serve the exams.hbs page
*
* EXAMPLE EXPECTED DATA GIVEN BELOW:
[ {courseCode: 'CSC240',
year: 2016,
term: 'Fall',
instructors: ['Faith Ellen', 'Tom F.'],
type: 'Midterm Examination',
title: 'Thry of Computation' },
{courseCode: 'CSC240',
year: 2014,
term: 'Fall',
instructors: ['Faith Ellen', 'Tom F.'],
type: 'Midterm Examination',
title: 'Thry of Computation' } ] */
router.get('/exams/', function(req,res,next){
req.checkParams('id','Course code should be between 6').notEmpty().withMessage('Course code required')
.isLength({min: 6, max: 6});
var errors = req.validationErrors();
if (errors){
req.session.errors = errors;
req.session.success = false;
res.redirect('/');
}
});
/** Serve the exams.hbs with a specific exam corresponding to the id */
router.get('/exams/:id', function(req, res, next) {
req.checkParams('id','Course code should be between 6 characters').notEmpty().withMessage('Course code required')
.isLength({min: 6, max: 6});
var errors = req.validationErrors();
if (errors){
req.session.errors = errors;
req.session.success = false;
res.redirect('/');
}else{
var minExamInfoArray = [];
dbFile.get_all_exams(req.params.id, function (exams) {
if (exams.length == 0){
}
else {
//only pass over the information that is necessary for the exams page
for (var i = 0; i<exams.length;i++){
var getInstructors = exams[i].instructors.join(", ");
var minExamInfo = { courseCode:exams[i].course_code,
year:exams[i].year,
term:toProperCase(exams[i].term),
instructors: getInstructors,
type:toProperCase(exams[i].type) + " Examination",
title:exams[i].title,
id:exams[i]._id,
questionCount : exams[i].questions_count
};
minExamInfoArray.push(minExamInfo);
}
}
res.render('exams', {query: req.params.id, result: minExamInfoArray});
});
}
});
/** Route protection. Redirect to the main page if errors. */
router.get('/user/',function (req, res) {
req.checkParams('username','Please enter a username').notEmpty();
var errors = req.validationErrors();
if (errors){
req.session.errors = errors;
req.session.success = false;
res.redirect('/');
}
});
/** Render user search page based on query */
router.get('/user/:query', function(req,res,next){
var query = req.params.query;
// Need to validate query
dbFile.search_users(query, function(success, result){
if(success){
res.render('user_search', {users : result, query : query, resultCount: result.length});
req.session.messages = null;
}else{
res.redirect('/');
}
});
});
/** Render/GET search (exam) page */
router.get('/search/:type', function(req, res, next) {
var searchType = req.params.type;
if(searchType == "courses"){
var courseName = req.query.search;
res.redirect('/exams/' + courseName);
}else{
var userInfo = req.query.search;
res.redirect('/user/' + userInfo);
}
});
/** This is a redirect from the exams page, route is generated in exams.hbs
*
* EXAMPLE DATA GIVEN BELOW:
* questions = [{id,count,comments},{id,count,comments}] --> array of "question" objects
*
* id = questions number
* count= number of solutions
* comments = number of comments*/
router.get('/questions/:exam_id', function (req,res) {
var examID = req.params.exam_id;
dbFile.get_exam_byID(examID, function(success, error, exam){
if(success && exam){
var qList = exam.questions_list;
// Add comments/solutions
dbFile.get_exam_info_by_ID(examID, function (questionsInfo) {
qList.forEach(function(question){
question.count = 0;
question.comments = 0;
// Find q_id in questionsInfo, update comment/solutions count
questionsInfo.forEach(function(q){
if (question.q_id == q._id){
question.count += q.count;
question.comments += q.comments;
}
});
});
var examInfo = {
id : exam._id,
courseCode : exam.course_code,
term : toProperCase(exam.term),
type : toProperCase(exam.type),
year : exam.year,
instructors : exam.instructors.join(),
uploadDate : exam.upload_date,
uploader : exam.uploaded_by,
pageCount : exam.page_count,
questionCount : exam.questions_count
};
res.render('questions', {query: qList, examInfo: examInfo, csrfToken: req.csrfToken()});
req.session.messages = null;
});
}else{
req.session.messages = {error : "Could not find exam."};
res.redirect('/');
}
});
});
/** GET the solutions for a given exam given the question number and the exam_id
* EXPECTED DATA GIVEN BELOW:
* array of solutions:
* solutions = [ {
_id: "354ff71ed078933079d6467e"
exam_id: "578a44ff71ed097fc3079d6e"
q_id: 1 (int)
text: "answer"
votes: 1 (int)
comments: [{}.{}] (just going to be string for now i think)
author : "text"
}
{
_id: ...
exam_id: ..
q_id: ...
text: ..
votes: ..
comments: ..
author : "text"
}
]
"comments": []
A comment:
{
"text": "this is asdfasdf",
"date": {"$date": "2016-07-23T03:55:34.906Z"},
"by": "some_user name"
},
*
**/
router.get('/solutions/:exam_id/:q_num', function (req, res) {
var examID = req.params.exam_id;
var qID = req.params.q_num;
dbFile.get_all_solutions(examID, qID, function (success, failure, message, solutions) {
if(success){
solutions.forEach(function(soln){
soln.commentCount = soln.comments.length;
});
res.render('user_solutions', {query: solutions, examID: examID, qID: qID, csrfToken: req.csrfToken()});
req.session.messages = null;
} else{
res.render('error', {error : message});
}
});
});
/** Redirect the user to Facebook for authentication. When complete,
Facebook will redirect the user back to the application at
/auth/facebook/callback */
router.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email']}));
/** Facebook will redirect the user to this URL after approval. Finish the
authentication process by attempting to obtain an access token. If
access was granted, the user will be logged in. Otherwise,
authentication has failed. */
router.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect: '/user/user_profile',
failureRedirect: '/user/signin',
failureFlash: true}));
/**** Helpers ****/
/**
* Adds an admin manually. There is no way to add an admin unless another admin adds him/her. Hence, there has to be
* at least one admin in the database.
*/
function addFirstAdmin() {
//admin_data = {fname: firstname, lname: lastname, username: username, password: password}
var password = 'lamptable';
var hash_pwd = passport_file.encryptPassword(password);
var admin_data = {
fname: 'Admin',
lname: 'Admin',
username: 'admin',
password: hash_pwd
};
dbFile.addAdmin(admin_data, function (success, error, message) {
});
}
/**
* Returns a proper cased string, given string.
* @param string
* @returns {void|XML}
*/
function toProperCase(string) {
return string.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
module.exports = router;
|