Check for worker_id (no duplicate allowed)

This commit is contained in:
Thomas FORGIONE 2015-07-23 18:16:44 +02:00
parent eb79b0bc42
commit 0a034471ca
5 changed files with 79 additions and 19 deletions

View File

@ -594,6 +594,40 @@ DBReq.UserIdChecker.prototype.finish = function() {
this.finishAction(this.finalResult);
};
DBReq.UserNameChecker = function(name, finishAction) {
this.name = name;
this.finishAction = finishAction;
var self = this;
pg.connect(pgc.url, function(err, client, release) {
self.client = client;
self.release = release;
self.execute();
});
};
DBReq.UserNameChecker.prototype.execute = function() {
var self = this;
this.client.query(
"SELECT count(id) > 0 AS answer FROM users WHERE worker_id = $1",
[self.name],
function(err, result) {
if (err !== null)
Log.dberror(err + ' in UserNameChecker');
self.finalResult = result.rows[0].answer;
self.finish();
}
);
};
DBReq.UserNameChecker.prototype.finish = function() {
this.release();
this.client = null;
this.release = null;
this.finishAction(this.finalResult);
};
/**
* Class that creates an experiment
* @param id {Number} id of the experiment to check
@ -671,7 +705,7 @@ DBReq.ExpGetter.prototype.execute = function() {
this.client.query(
"SELECT " +
"experiment.id as exp_id, " +
"users.name as username, " +
"users.worker_id as username, " +
"scene.name as scenename, " +
"users.id as user_id " +
"FROM experiment, users, scene " +
@ -757,6 +791,10 @@ DBReq.checkUserId = function(id, callback) {
new DBReq.UserIdChecker(id, callback);
};
DBReq.checkUserName = function(name, callback) {
new DBReq.UserNameChecker(name, callback);
};
/**
* Checks if an experiment id exists
* @memberof DBReq

View File

@ -148,7 +148,7 @@ module.exports.tutorial = function(req, res) {
req.session.save();
res.setHeader('Content-Type', 'text/html');
res.render('tutorial.jade', res.lcals, function(err, result) {
res.render('tutorial.jade', res.locals, function(err, result) {
res.send(result);
});
});
@ -211,7 +211,11 @@ module.exports.viewer = function(req, res, next) {
module.exports.userstudy = function(req, res) {
res.setHeader('Content-Type', 'text/html');
res.render('user_study.jade', res.lcals, function(err, result) {
res.locals.identificationFailed = req.session.identificationFailed;
req.session.identificationFailed = false;
req.session.save();
res.render('user_study.jade', res.locals, function(err, result) {
res.send(result);
});

View File

@ -3,6 +3,14 @@ extends ../../../views/base.jade
block extrahead
link(rel="stylesheet", href="/static/css/signin.css")
block extrabody
if identificationFailed
.container
.alert.alert-danger.alert-dismissible(role="alert", style={'margin-top':'20px'})
button.close(type="button", data-dismiss="alert", aria-label="Close")
span(aria-hidden="true") ×
<strong>Error</strong> : this id is already used !
block content
form.form-signin(method="POST", action='/identification')
h2 Please sign in
@ -13,10 +21,11 @@ block content
div(style={'text-align': 'center', 'margin-top':'10px', 'margin-bottom':'10px'})
label.radio-inline
input(type='radio', name='inputGender', value="male")
input(type='radio', name='inputGender', value="male", checked)
| Male
label.radio-inline
input(type='radio', name='inputGender', value="female")
| Female
button.btn.btn-lg.btn-primary.btn-block(type='submit') Sign in

View File

@ -4,24 +4,31 @@ var db = require('../../controllers/prototype/dbrequests.js');
var Log = require('../../lib/NodeLog.js');
module.exports.index = function(req, res) {
db.checkUserName(req.body.inputId, function(ok) {
if (!ok) {
db.tryUser(req.session.user_id, function(id) {
req.session.user_id = id;
req.session.save();
db.tryUser(req.session.user_id, function(id) {
req.session.user_id = id;
req.session.save();
pg.connect(secret.url, function(err, client, release) {
client.query(
"UPDATE Users SET worker_id = $1, age = $2, male = $3 WHERE id = $4;",
[req.body.inputId, req.body.inputAge, req.body.inputGender === 'male', req.session.user_id],
function(err, result) {
if (err !== null)
Log.dberror(err + ' in identfication');
release();
}
);
});
pg.connect(secret.url, function(err, client, release) {
client.query(
"UPDATE Users SET worker_id = $1, age = $2, male = $3 WHERE id = $4;",
[req.body.inputId, req.body.inputAge, req.body.inputGender === 'male', req.session.user_id],
function(err, result) {
if (err !== null)
Log.dberror(err + ' in identfication');
release();
}
);
});
res.redirect('/');
res.redirect('/');
});
} else {
req.session.identificationFailed = true;
res.redirect('/user-study');
}
});
};

View File

@ -24,6 +24,8 @@ html(lang='fr')
block links
block extrabody
section#main-section.container
if alertCookie
.alert.alert-warning.alert-dismissible(role="alert", style={'margin-top':'20px'})