antville/code/Poll/objectFunctions.js

82 lines
2.4 KiB
JavaScript
Raw Normal View History

/**
* check if poll is ok. if true, save modified poll
* @param Object the req.data object coming in from the action
* @param Object the user as creator of the poll modifications
* @return Object containing the properties
* - error (boolean): true if error occured, false otherwise
* - message (String): an error or a confirmation message
* - url (String): the URL string of the poll
* - id (Number): the internal Hop ID of the poll
*/
2002-01-25 17:57:44 +00:00
function evalPoll(param, creator) {
var result;
2002-01-25 17:57:44 +00:00
var choiceInput = param.choice;
if (param.choice_array) {
var choiceCnt = 0;
for (var i in param.choice_array) {
if (param.choice_array[i])
choiceCnt++;
}
}
if (param.question && choiceCnt > 1) {
2002-01-25 17:57:44 +00:00
this.question = param.question;
this.modifytime = new Date();
2002-12-01 19:26:40 +00:00
for (var i=this.size(); i>0; i--) {
var ch = this.get(i-1);
this.remove(ch);
}
for (var i=0; i<param.choice_array.length; i++) {
var title = param.choice_array[i];
if (!title)
continue;
var newChoice = new choice();
newChoice.poll = this;
newChoice.title = title;
newChoice.createtime = new Date();
newChoice.modifytime = new Date();
this.add(newChoice);
}
result = getConfirm("pollCreate");
} else
result = getError("pollMissingValues");
2002-01-25 17:57:44 +00:00
return(result);
}
/**
* check if a vote is ok. if true, save modified vote
* @param Object the req.data object coming in from the action
* @param Object the user as creator of the poll modifications
* @return Object containing the properties
* - error (boolean): true if error occured, false otherwise
* - message (String): an error or a confirmation message
* - url (String): the URL string of the poll
*/
2002-01-25 17:57:44 +00:00
function evalVote(param, usr) {
var result;
2002-01-25 17:57:44 +00:00
if (param.choice) {
var c = this.get(param.choice);
var v = session.user ? this.votes.get(session.user.name) : null;
2002-01-25 17:57:44 +00:00
if (v) {
v.choice = c;
v.modifytime = new Date();
}
else {
var v = new vote();
v.poll = this;
v.choice = c;
v.user = session.user;
v.username = session.user.name;
2002-01-25 17:57:44 +00:00
v.createtime = new Date();
v.modifytime = new Date();
this.votes.add(v);
}
result = getConfirm("vote");
} else
result = getConfirm("noVote");
result.url = this.href();
2002-01-25 17:57:44 +00:00
return(result);
}