-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (141 loc) · 3.89 KB
/
index.js
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
/**
* Created by lomtev on 25.03.15.
*/
var http = require('http'), fs = require('fs'), express = require('express'), app = express(), engines = require('consolidate');
var mysql = require('mysql');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
var connection = mysql.createConnection({
host: 'localhost',
user: 'admin',
password: 'd2n|n@r',
database: 'isop'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
connection.query('SELECT * FROM users', function(err, result) {
if (err) throw err;
console.log(result);
});
/*var fu = {
firstname: 'a',
lastname: 'a',
institution: 'a',
login: 'admin',
password: 'admin'
};
connection.query('INSERT INTO users SET ?', fu, function(err, result) {
if (err) throw err;
console.log(result.insertId);
});*/
var question = {
'type' : [],
'number' : [],
'example' : [],
'answer' : []
};
fs.readFile('tests/all.txt', 'utf8', function(err, data) {
if (err) throw err;
var text = data.toString();
text = text.split('\<question\>\n');
text.forEach(function(el) {
el = el.split('\<part\>');
question.number.push(el[0]);
question.type.push(el[1]);
question.example.push(el[2]);
question.answer.push(el[3]);
});
});
app.post('/ajax', function (req, res){
var q = {
'type': [],
'example': []
};
q.type = question.type;
q.example = question.example;
res.send(JSON.stringify(q));
});
var answers = [], correct = [], k, assessment = 0, questionAllocated = "BDEFKI";
app.post('/testB', function (req, res){
k = req.body.ques;
answers = req.body.selectLine;
var userAnswer = req.body.answer;
if (questionAllocated.indexOf(question.type[k]) == -1) {
if (userAnswer == question.answer[k]) {
res.send("right");
} else {
res.send("wrong");
}
} else {
var nums = [];
correct = question.answer[k].split(',');
if (answers != undefined) {
for (var i = 0; i < answers.length; ++i) {
if (answers[i] == 1) nums.push(i);
}
}
res.send(nums.join(',') == correct.join(',') ? "right" : "wrong");
}
});
app.post('/testA', function (req, res){
k = req.body.ques;
var userAnswer = req.body.answer;
answers = req.body.selectLine;
if (questionAllocated.indexOf(question.type[k]) == -1) {
if (userAnswer == question.answer[k]) {
++assessment;
}
} else {
var nums = [];
correct = question.answer[k].split(',');
if (answers != undefined) {
for (var i = 0; i < answers.length; ++i) {
if (answers[i] == 1) nums.push(i);
}
if (nums.join(',') == correct.join(',')) {
++assessment;
}
}
}
res.send();
});
app.post('/assessment', function (req, res){
res.send(JSON.stringify(assessment));
});
app.use(express.static(__dirname + '/public/html/'));
app.use(express.static(__dirname + '/public'));
var indexHtml;
var registrHtml;
var testHtml;
fs.readFile ('public/html/index.html', 'utf8', function(err, data) {
if (err) throw err;
indexHtml = data;
});
fs.readFile ('public/html/registr.html', 'utf8', function(err, data) {
if (err) throw err;
registrHtml = data;
});
fs.readFile ('public/html/test.html', 'utf8', function(err, data) {
if (err) throw err;
testHtml = data;
});
app.get('/', function (req, res) {
res.end(indexHtml);
});
app.get('/registr', function (req, res) {
res.end(registrHtml);
});
app.get('/test', function (req, res) {
res.end(testHtml);
});
function logSuccess() {
};
app.listen(process.env.PORT || 3000);