Skip to content

Commit

Permalink
fix poll import
Browse files Browse the repository at this point in the history
  • Loading branch information
danielausparis committed Dec 19, 2016
1 parent 5434685 commit 502ae6e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
mars-server/dbparams.php
mars-server/mailparams.php
mergecommands.txt
112 changes: 74 additions & 38 deletions mars-manager/js/pollscontroller.js
Expand Up @@ -3,9 +3,11 @@ myApp.controller('PollsController',

function($scope, $state, $http, $cookies, user, statics) {

var getpolls = function() {
var uploadpollid;
var notificationbuttonaction = null;
$scope.notificationstring = '';

// $scope.spinner = "<i class='fa fa-circle-o-notch fa-spin' aria-hidden='true'></i>";
var getpolls = function() {

parms = {
task : 'getpolls'
Expand All @@ -18,7 +20,6 @@ myApp.controller('PollsController',
alert('getpolls error : ' + response.data.text);
}
else {
//$scope.spinner = "";
if (response.data.error == false) {

$scope.polls = response.data.data;
Expand All @@ -36,8 +37,6 @@ myApp.controller('PollsController',

var deletepoll = function(pollid) {

// $scope.spinner = "<i class='fa fa-circle-o-notch fa-spin' aria-hidden='true'></i>";

parms = {
task : 'deletepoll',
pollid : pollid
Expand Down Expand Up @@ -94,7 +93,7 @@ myApp.controller('PollsController',
// statics.polltype = poll.type;
// statics.secret = response.data.secret;
// statics.polltitle = poll.name;

statics.participantnumber = 0;
statics.replay = false;

Expand Down Expand Up @@ -145,11 +144,55 @@ myApp.controller('PollsController',

}

function one_by_one(objects_array, iterator, callback) {
var start_promise = objects_array.reduce(function (prom, object) {
return prom.then(function () {
return iterator(object);
});
}, Promise.resolve()); // initial
if(callback){
start_promise.then(callback);
}else{
return start_promise;
}
}


uploadOneQuestion = function(question) {

return new Promise(function(resolve, reject) {

parms = {
pollid : uploadpollid,
json : question.text
}
//console.log('sending : ' + JSON.stringify(parms, null, 4));

$http.get(statics.rootUrl + 'addquestion.php', {params : parms})
.then(
function(response) {
if (response.data.error) {
alert('Error upload question: ' + response.data.text);
reject();
}
else {
resolve();
}
},
function(response) {
alert('Network error : ' + JSON.stringify(response, null, 4));
reject();
});

});
}



$scope.msds_update = function() {

var f = document.getElementById('msds').files[0],
r = new FileReader();
var f = document.getElementById('msds').files[0];
var r = new FileReader();

r.onloadend = function(e) {

Expand All @@ -174,37 +217,23 @@ myApp.controller('PollsController',
alert('Error upload poll : ' + response.data.text);
}
else {
var pollid = response.data.pollid;
uploadpollid = response.data.pollid;

// ok, now create all questions
for (question of result.questions) {

parms = {
pollid : pollid,
json : question.text
}
//console.log('sending : ' + JSON.stringify(parms, null, 4));

$http.get(statics.rootUrl + 'addquestion.php', {params : parms})
.then(
function(response) {
if (response.data.error) {
alert('Error create question: ' + response.data.text);
}
else {
console.log('success');
document.getElementById('importmodal').style.display='none';
notify('Success : created new poll ' + pollid + '.',
function() {
$state.go('polls', $state.params, {reload: true, inherit: false});
});
}
},
function(response) {
alert('Network error : ' + JSON.stringify(response, null, 4));
}
);
}

one_by_one(result.questions, uploadOneQuestion).then(function(result) {

console.log('success');
document.getElementById('importmodal').style.display='none';

notify('Success : created new poll ' + uploadpollid + '.',
function() {
$state.go('polls', $state.params, {reload: true, inherit: false});
}
);

});

}
},
function(response) {
Expand All @@ -213,12 +242,16 @@ myApp.controller('PollsController',
);
};

r.readAsText(f);
if (f != null) {
r.readAsText(f);
}

}

var notify = function(text, buttonhandler) {
console.log(text);
$scope.notificationstring = text;
$scope.$apply();
notificationbuttonaction = buttonhandler;
document.getElementById('shownotification').style.display='block';
}
Expand Down Expand Up @@ -276,6 +309,9 @@ myApp.controller('PollsController',

console.log('polls');
statics.showheader = true;

$scope.shownotify = false;

$scope.statics = statics;

document.getElementById('shownotification').style.display='none';
Expand Down
6 changes: 3 additions & 3 deletions mars-manager/polls.html
Expand Up @@ -32,9 +32,9 @@ <h5>Import poll</h5>
<div class="w3-padding">
<input type="file" id="msds" ng-model="msds" name="msds" accept=".json"/>
<br><br><br>
<button type="button" class="w3-btn w3-round-small w3-left w3-ripple w3-small w3-grey"
<button type="button" class="w3-btn w3-round-small w3-left w3-ripple w3-small w3-grey"
ng-click="importcancelbuttonhandler()">Cancel</button>
<button class="w3-btn w3-round-small w3-light-green w3-right"
<button class="w3-btn w3-round-small w3-light-green w3-right w3-ripple"
ng-click="msds_update()">
import poll
</button>
Expand All @@ -53,7 +53,7 @@ <h5>Info</h5>
</header>
<div class="w3-padding w3-theme-complement-1">
<br>
{{notificationstring}}
{{notificationstring}}
</div>
<div class="w3-padding w3-theme-complement-2">
<button class="w3-btn w3-round-small w3-light-green w3-ripple w3-right"
Expand Down
3 changes: 3 additions & 0 deletions mars-server/addquestion.php
Expand Up @@ -37,16 +37,19 @@
// find max number
$req = "SELECT number FROM questions WHERE pollid = '$pollid' ORDER BY number DESC LIMIT 1";
$res = DO_REQUEST($req);
//error_log('res : ' . print_r($res, true));
if (count($res) == 0) {
$max = 0;
} else {
$max = $res[0]['number'];
}

$max = $max + 1;
error_log('inserting question ' . $max);

$req = "INSERT INTO questions (text, pollid, correctanswer, nbchoices, number) VALUES ('$text', '$pollid', '$correctanswer', '$nbchoices', '$max')";
DO_REQUEST($req);

}


Expand Down

0 comments on commit 502ae6e

Please sign in to comment.