cleaned up submissions table

This commit is contained in:
David Baldwynn 2015-07-01 21:54:46 -07:00
parent 0c8c320a71
commit 8204980626
8 changed files with 333 additions and 313 deletions

View file

@ -15,7 +15,7 @@ var mongoose = require('mongoose'),
_ = require('lodash'); _ = require('lodash');
/** /**
* Create a new form manually * Create a new form
*/ */
exports.create = function(req, res) { exports.create = function(req, res) {
var form = new Form(req.body); var form = new Form(req.body);
@ -107,9 +107,9 @@ exports.createSubmission = function(req, res) {
submission.form_fields = req.body.form_fields; submission.form_fields = req.body.form_fields;
submission.title = req.body.title; submission.title = req.body.title;
submission.timeElapsed = req.body.timeElapsed; submission.timeElapsed = req.body.timeElapsed;
console.log(req.body);
// submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress; // submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if (form.isGenerated){ if (form.isGenerated){
fdfTemplate = form.convertToFDF(); fdfTemplate = form.convertToFDF();
} else { } else {
@ -120,9 +120,10 @@ exports.createSubmission = function(req, res) {
} }
} }
fdfData = pdfFiller.fillFdfTemplate(fdfTemplate, submission.form_fields, null); if(form.autofillPDFs){
fdfData = pdfFiller.fillFdfTemplate(fdfTemplate, submission.form_fields, null);
submission.fdfData = fdfData; submission.fdfData = fdfData;
}
submission.save(function(err){ submission.save(function(err){
if (err) { if (err) {
@ -146,10 +147,12 @@ exports.listSubmissions = function(req, res) {
FormSubmission.find({ form: req.form }).populate('admin', 'form').exec(function(err, submissions) { FormSubmission.find({ form: req.form }).populate('admin', 'form').exec(function(err, submissions) {
if (err) { if (err) {
res.status(400).send({ console.log(err);
res.status(500).send({
message: errorHandler.getErrorMessage(err) message: errorHandler.getErrorMessage(err)
}); });
} else { } else {
console.log('hello');
res.json(submissions); res.json(submissions);
} }
}); });
@ -186,7 +189,7 @@ exports.delete = function(req, res) {
console.log('deleting form'); console.log('deleting form');
Form.remove({_id: form._id}, function(err) { Form.remove({_id: form._id}, function(err) {
if (err) { if (err) {
res.status(400).send({ res.status(500).send({
message: err.message message: err.message
}); });
} else { } else {
@ -228,39 +231,42 @@ exports.formByID = function(req, res, next, id) {
} }
Form.findById(id).populate('admin').exec(function(err, form) { Form.findById(id).populate('admin').exec(function(err, form) {
if (err) return next(err); if (err) {
if (!form) { return next(err);
} else if (!form || form === null) {
res.status(404).send({ res.status(404).send({
message: 'Form not found' message: 'Form not found'
}); });
} }
if(!form.admin){ else {
form.admin = req.user; if(!form.admin){
form.save(function(err) { form.admin = req.user;
if (err) { form.save(function(err) {
console.log(err); if (err) {
res.status(400).send({ console.log(err);
message: errorHandler.getErrorMessage(err) res.status(400).send({
}); message: errorHandler.getErrorMessage(err)
} else { });
//Remove sensitive information from User object } else {
form.admin.password = null; //Remove sensitive information from User object
form.admin.created = null; form.admin.password = null;
form.admin.salt = null; form.admin.created = null;
form.admin.salt = null;
req.form = form; req.form = form;
next(); next();
} }
}); });
}
//Remove sensitive information from User object
form.admin.password = null;
form.admin.created = null;
form.admin.salt = null;
req.form = form;
next();
} }
//Remove sensitive information from User object
form.admin.password = null;
form.admin.created = null;
form.admin.salt = null;
req.form = form;
next();
}); });
}; };

View file

@ -85,10 +85,10 @@ FormSubmissionSchema.pre('save', function (next) {
Form.findById(that.form, function(err, _form){ Form.findById(that.form, function(err, _form){
if(err) next( new Error(err.mesasge) ); if(err) next( new Error(err.mesasge) );
// that.title = _form.title; that.title = _form.title;
// console.log(_form); // console.log(_form);
if(true){ //_form.autofillPDFs){ if(_form.autofillPDFs){
dest_filename = _form.title.trim()+'_submission_'+Date.now()+'.pdf'; dest_filename = _form.title.trim()+'_submission_'+Date.now()+'.pdf';
dest_path = path.join(config.pdfUploadPath, dest_filename); dest_path = path.join(config.pdfUploadPath, dest_filename);
@ -97,7 +97,6 @@ FormSubmissionSchema.pre('save', function (next) {
// console.log('autofillPDFs check'); // console.log('autofillPDFs check');
pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){ pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){
console.log('fdfData: \n'); console.log('fdfData: \n');
console.log(that.fdfData); console.log(that.fdfData);
@ -113,13 +112,11 @@ FormSubmissionSchema.pre('save', function (next) {
next(); next();
}); });
} else { } else {
next(); next();
} }
}); });
}); });
mongoose.model('FormSubmission', FormSubmissionSchema); mongoose.model('FormSubmission', FormSubmissionSchema);

View file

@ -2,239 +2,232 @@
angular.module('forms').controller('EditFormController', ['$scope', '$state', '$rootScope', 'Upload', '$stateParams', 'FormFields', 'Forms', 'CurrentForm', '$modal', '$location', angular.module('forms').controller('EditFormController', ['$scope', '$state', '$rootScope', 'Upload', '$stateParams', 'FormFields', 'Forms', 'CurrentForm', '$modal', '$location',
function ($scope, $state, $rootScope, Upload, $stateParams, FormFields, Forms, CurrentForm, $modal, $location) { function ($scope, $state, $rootScope, Upload, $stateParams, FormFields, Forms, CurrentForm, $modal, $location) {
// Principal.identity().then(function(user){
// $scope.authentication.user = user; $scope.isNewForm = false;
// }).then(function(){ $scope.pdfLoading = false;
// console.log('aeouaoeuaoeuaou'); var _current_upload = null;
// console.log('isAuthenticated(): '+Principal.isAuthenticated());\ $scope.log = '';
$scope.isNewForm = false; // Get current form if it exists, or create new one
if($stateParams.formId){
$scope.form = {};
var _form = Forms.get({ formId: $stateParams.formId}, function(form){
_form.pdf = form.pdf;
_form.$save();
$scope.form = angular.fromJson(angular.toJson(_form));
console.log(JSON.stringify($scope.form.pdf));
});
} else {
$scope.form = {};
$scope.form.form_fields = [];
$scope.isNewForm = true;
}
//PDF Functions
$scope.cancelUpload = function(){
_current_upload.abort();
$scope.pdfLoading = false; $scope.pdfLoading = false;
var _current_upload = null; };
$scope.log = '';
// Get current form if it exists, or create new one $scope.removePDF = function(){
if($stateParams.formId){ $scope.form.pdf = null;
$scope.form = {}; $scope.isGenerated = false;
var _form = Forms.get({ formId: $stateParams.formId}, function(form){ $scope.autofillPDFs = false;
_form.pdf = form.pdf;
_form.$save();
$scope.form = angular.fromJson(angular.toJson(_form)); console.log('form.pdf: '+$scope.form.pdf+' REMOVED');
console.log(JSON.stringify($scope.form.pdf)); };
$scope.uploadPDF = function(files) {
if (files && files.length) {
// for (var i = 0; i < files.length; i++) {
var file = files[0];
_current_upload = Upload.upload({
url: '/upload/pdf',
fields: {
'user': $scope.user,
'form': $scope.form
},
file: file
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
$scope.log = 'progress: ' + progressPercentage + '% ' +
evt.config.file.name + '\n' + $scope.log;
$scope.pdfLoading = true;
}).success(function (data, status, headers, config) {
$scope.log = 'file ' + data.originalname + 'uploaded as '+ data.name +'. JSON: ' + JSON.stringify(data) + '\n' + $scope.log;
$scope.form.pdf = data;
$scope.pdfLoading = false;
console.log($scope.log);
console.log('$scope.pdf: '+$scope.form.pdf.name);
if(!$scope.$$phase) {
$scope.$apply();
}
}).error(function(err){
$scope.pdfLoading = false;
console.log('Error occured during upload.\n');
console.log(err);
}); });
} else { // }
$scope.form = {};
$scope.form.form_fields = [];
$scope.isNewForm = true;
} }
};
//PDF Functions $scope.goToWithId = function(route, id) {
$scope.cancelUpload = function(){ $state.go(route, {'formId': id}, {reload: true});
_current_upload.abort(); };
$scope.pdfLoading = false;
};
$scope.removePDF = function(){ // Create new Form
$scope.form.pdf = null; $scope.createOrUpdate = function() {
$scope.isGenerated = false;
$scope.autofillPDFs = false;
console.log('form.pdf: '+$scope.form.pdf+' REMOVED'); if($scope.isNewForm){
}; // Create new Form object
$scope.uploadPDF = function(files) {
if (files && files.length) {
// for (var i = 0; i < files.length; i++) {
var file = files[0];
_current_upload = Upload.upload({
url: '/upload/pdf',
fields: {
'user': $scope.user,
'form': $scope.form
},
file: file
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
$scope.log = 'progress: ' + progressPercentage + '% ' +
evt.config.file.name + '\n' + $scope.log;
$scope.pdfLoading = true;
}).success(function (data, status, headers, config) {
$scope.log = 'file ' + data.originalname + 'uploaded as '+ data.name +'. JSON: ' + JSON.stringify(data) + '\n' + $scope.log;
$scope.form.pdf = data;
$scope.pdfLoading = false;
console.log($scope.log);
console.log('$scope.pdf: '+$scope.form.pdf.name);
if(!$scope.$$phase) {
$scope.$apply();
}
}).error(function(err){
$scope.pdfLoading = false;
console.log('Error occured during upload.\n');
console.log(err);
});
// }
}
};
$scope.goToWithId = function(route, id) {
$state.go(route, {'formId': id}, {reload: true});
};
// Create new Form
$scope.createOrUpdate = function() {
if($scope.isNewForm){
// Create new Form object
var form = new Forms($scope.form);
form.$save(function(response) {
console.log('form created');
// console.log(response.pdf);
// Clear form fields
$scope.form = {};
// Redirect after save
$scope.goToWithId('viewForm', response._id);
}, function(errorResponse) {
console.log(errorResponse.data.message);
$scope.error = errorResponse.data.message;
});
} else{
console.log('update form');
$scope.update();
}
};
// Update existing Form
$scope.update = function() {
var form = new Forms($scope.form); var form = new Forms($scope.form);
form.$update(function(response) {
console.log('form updated'); form.$save(function(response) {
console.log('form created');
// console.log(response.pdf);
// Clear form fields
$scope.form = {};
// Redirect after save
$scope.goToWithId('viewForm', response._id); $scope.goToWithId('viewForm', response._id);
// $location.path('forms/' + response._id + '/admin');
}, function(errorResponse) { }, function(errorResponse) {
console.log(errorResponse.data.message); console.log(errorResponse.data.message);
$scope.error = errorResponse.data.message; $scope.error = errorResponse.data.message;
}); });
} else{
console.log('update form');
$scope.update();
}
};
// Update existing Form
$scope.update = function() {
var form = new Forms($scope.form);
form.$update(function(response) {
console.log('form updated');
$scope.goToWithId('viewForm', response._id);
// $location.path('forms/' + response._id + '/admin');
}, function(errorResponse) {
console.log(errorResponse.data.message);
$scope.error = errorResponse.data.message;
});
};
//Populate AddField with all available form field types
$scope.addField = {};
$scope.addField.types = FormFields.fields;
$scope.addField.new = $scope.addField.types[0].name;
$scope.addField.lastAddedID = 0;
// preview form mode
$scope.previewMode = false;
// previewForm - for preview purposes, form will be copied into this
// otherwise, actual form might get manipulated in preview mode
$scope.previewForm = {};
// accordion settings
$scope.accordion = {};
$scope.accordion.oneAtATime = true;
// create new field button click
$scope.addNewField = function(){
// incr field_id counter
$scope.addField.lastAddedID++;
var newField = {
'title' : 'New field - ' + ($scope.addField.lastAddedID),
'fieldType' : $scope.addField.new,
'fieldValue' : '',
'required' : true,
'disabled' : false
}; };
//Populate AddField with all available form field types // put newField into fields array
$scope.addField = {}; $scope.form.form_fields.push(newField);
$scope.addField.types = FormFields.fields; // console.log($scope.form.form_fields);
$scope.addField.new = $scope.addField.types[0].name; };
$scope.addField.lastAddedID = 0;
// preview form mode // deletes particular field on button click
$scope.previewMode = false; $scope.deleteField = function (field_id){
for(var i = 0; i < $scope.form.form_fields.length; i++){
// previewForm - for preview purposes, form will be copied into this if($scope.form.form_fields[i].field_id === field_id){
// otherwise, actual form might get manipulated in preview mode $scope.form.form_fields.splice(i, 1);
$scope.previewForm = {}; break;
// accordion settings
$scope.accordion = {};
$scope.accordion.oneAtATime = true;
// create new field button click
$scope.addNewField = function(){
// incr field_id counter
$scope.addField.lastAddedID++;
var newField = {
'title' : 'New field - ' + ($scope.addField.lastAddedID),
'fieldType' : $scope.addField.new,
'fieldValue' : '',
'required' : true,
'disabled' : false
};
// put newField into fields array
$scope.form.form_fields.push(newField);
// console.log($scope.form.form_fields);
};
// deletes particular field on button click
$scope.deleteField = function (field_id){
for(var i = 0; i < $scope.form.form_fields.length; i++){
if($scope.form.form_fields[i].field_id === field_id){
$scope.form.form_fields.splice(i, 1);
break;
}
} }
}
};
// add new option to the field
$scope.addOption = function (field){
if(!field.field_options)
field.field_options = [];
var lastOptionID = 0;
if(field.field_options[field.field_options.length-1])
lastOptionID = field.field_options[field.field_options.length-1].option_id;
// new option's id
var option_id = lastOptionID + 1;
var newOption = {
'option_id' : option_id,
'option_title' : 'Option ' + option_id,
'option_value' : option_id
}; };
// add new option to the field // put new option into field_options array
$scope.addOption = function (field){ field.field_options.push(newOption);
if(!field.field_options) };
field.field_options = [];
var lastOptionID = 0; // delete particular option
$scope.deleteOption = function (field, option){
if(field.field_options[field.field_options.length-1]) for(var i = 0; i < field.field_options.length; i++){
lastOptionID = field.field_options[field.field_options.length-1].option_id; if(field.field_options[i].option_id === option.option_id){
field.field_options.splice(i, 1);
// new option's id break;
var option_id = lastOptionID + 1;
var newOption = {
'option_id' : option_id,
'option_title' : 'Option ' + option_id,
'option_value' : option_id
};
// put new option into field_options array
field.field_options.push(newOption);
};
// delete particular option
$scope.deleteOption = function (field, option){
for(var i = 0; i < field.field_options.length; i++){
if(field.field_options[i].option_id === option.option_id){
field.field_options.splice(i, 1);
break;
}
} }
}; }
};
// preview form // preview form
$scope.previewOn = function(){ $scope.previewOn = function(){
if($scope.form.form_fields === null || $scope.form.form_fields.length === 0) { if($scope.form.form_fields === null || $scope.form.form_fields.length === 0) {
var title = 'Error'; var title = 'Error';
var msg = 'No fields added yet, please add fields to the form before preview.'; var msg = 'No fields added yet, please add fields to the form before preview.';
var btns = [{result:'ok', label: 'OK', cssClass: 'btn-primary'}]; var btns = [{result:'ok', label: 'OK', cssClass: 'btn-primary'}];
// $dialog.messageBox(title, msg, btns).open(); // $dialog.messageBox(title, msg, btns).open();
} }
else { else {
$scope.previewMode = !$scope.previewMode;
$scope.form.submitted = false;
angular.copy($scope.form, $scope.previewForm);
}
};
// hide preview form, go back to create mode
$scope.previewOff = function(){
$scope.previewMode = !$scope.previewMode; $scope.previewMode = !$scope.previewMode;
$scope.form.submitted = false; $scope.form.submitted = false;
}; angular.copy($scope.form, $scope.previewForm);
}
};
// decides whether field options block will be shown (true for dropdown and radio fields) // hide preview form, go back to create mode
$scope.showAddOptions = function (field){ $scope.previewOff = function(){
if(field.field_type === 'radio' || field.field_type === 'dropdown') $scope.previewMode = !$scope.previewMode;
return true; $scope.form.submitted = false;
else };
return false;
};
// }); // decides whether field options block will be shown (true for dropdown and radio fields)
$scope.showAddOptions = function (field){
if(field.field_type === 'radio' || field.field_type === 'dropdown')
return true;
else
return false;
};
} }
]); ]);

View file

@ -3,20 +3,10 @@
// Forms controller // Forms controller
angular.module('forms').controller('SubmitFormController', ['$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', angular.module('forms').controller('SubmitFormController', ['$scope', '$stateParams', '$state', 'Forms', 'CurrentForm',
function($scope, $stateParams, $state, Forms, CurrentForm) { function($scope, $stateParams, $state, Forms, CurrentForm) {
// Principal.identity().then(function(user){
// $scope.authentication.user = user;
// }).then(function(){
$scope.form = Forms.get({ $scope.form = Forms.get({
formId: $stateParams.formId formId: $stateParams.formId
}); });
CurrentForm.setForm($scope.form); CurrentForm.setForm($scope.form);
// console.log($scope.form);
// });
} }
]); ]);

View file

@ -33,9 +33,9 @@ angular.module('forms').controller('ViewSubmissionController', ['$scope', '$stat
$http.delete('/forms/'+$stateParams.formId+'submissions/'+$scope.submission._id). $http.delete('/forms/'+$stateParams.formId+'submissions/'+$scope.submission._id).
success(function(data, status, headers){ success(function(data, status, headers){
console.log('submission deleted successfully'); console.log('submission deleted successfully');
alert('submission deleted..'); alert('submission deleted..');
}); });
} else { } else {
$scope.submission.$remove(function() { $scope.submission.$remove(function() {
@ -43,9 +43,9 @@ angular.module('forms').controller('ViewSubmissionController', ['$scope', '$stat
$state.path('submissions'); $state.path('submissions');
$http.delete('/forms/'+$stateParams.formId+'/submissions/'+$scope.submission._id). $http.delete('/forms/'+$stateParams.formId+'/submissions/'+$scope.submission._id).
success(function(data, status, headers){ success(function(data, status, headers){
console.log('submission deleted successfully'); console.log('submission deleted successfully');
alert('submission deleted..'); alert('submission deleted..');
}); });
}); });
} }
}; };

View file

@ -4,51 +4,73 @@
angular.module('forms').controller('ViewFormController', ['$scope', '$stateParams', '$state', 'Forms', 'CurrentForm','$http', angular.module('forms').controller('ViewFormController', ['$scope', '$stateParams', '$state', 'Forms', 'CurrentForm','$http',
function($scope, $stateParams, $state, Forms, CurrentForm, $http) { function($scope, $stateParams, $state, Forms, CurrentForm, $http) {
// view form submissions // view form submissions
$scope.viewSubmissions = false; $scope.form = CurrentForm.getForm();
$scope.submissions = undefined;
$scope.viewSubmissions = false;
//show submissions of Form
$scope.showSubmissions = function(){
$scope.viewSubmissions = true;
}
//hide submissions of Form
$scope.hideSubmissions = function(){
$scope.viewSubmissions = false;
}
// Return all user's Forms //show submissions of Form
$scope.findAll = function() { $scope.showSubmissions = function(){
$scope.forms = Forms.query(); $scope.viewSubmissions = true;
}; if(!$scope.submissions){
$http.get('/forms/'+$scope.form._id+'/submissions')
// Find a specific Form
$scope.findOne = function() {
$scope.form = Forms.get({
formId: $stateParams.formId
});
CurrentForm.setForm($scope.form);
};
// Remove existing Form
$scope.remove = function() {
console.log('hello');
var form = CurrentForm.getForm()
if(!form){
form = $scope.form
}
$http.delete('/forms/'+$scope.form._id)
.success(function(data, status, headers){ .success(function(data, status, headers){
console.log('form deleted successfully'); console.log(data);
alert('Form deleted..'); $scope.submissions = data;
$state.go('listForms'); console.log('form submissions successfully fetched');
}).error(function(error){ })
console.log('ERROR: Form could not be deleted.'); .error(function(err){
console.error(error); console.log('Could not fetch form submissions.\nError: '+err);
});
} else if(!$scope.submissions.length){
$http.get('/forms/'+$scope.form._id+'/submissions')
.success(function(data, status, headers){
$scope.submissions = data;
console.log('form submissions successfully fetched');
})
.error(function(err){
console.log('Could not fetch form submissions.\nError: '+err);
}); });
}
console.log($scope.submissions);
}
}; //hide submissions of Form
$scope.hideSubmissions = function(){
$scope.viewSubmissions = false;
}
// Return all user's Forms
// }); $scope.findAll = function() {
$scope.forms = Forms.query();
};
// Find a specific Form
$scope.findOne = function() {
$scope.form = Forms.get({
formId: $stateParams.formId
});
CurrentForm.setForm($scope.form);
};
// Remove existing Form
$scope.remove = function() {
console.log('hello');
var form = CurrentForm.getForm()
if(!form){
form = $scope.form
}
$http.delete('/forms/'+$scope.form._id)
.success(function(data, status, headers){
console.log('form deleted successfully');
alert('Form deleted..');
$state.go('listForms');
}).error(function(error){
console.log('ERROR: Form could not be deleted.');
console.error(error);
});
};
} }
]); ]);

View file

@ -41,14 +41,14 @@
</div> </div>
<br><br><br><br><br> <br><br><br><br><br>
<div class="row form-actions"> <div class="row form-actions">
<p class="text-center col-xs-3 col-xs-offset-2"> <p class="text-center col-xs-3 col-xs-offset-4">
<button class="btn btn-success left" type="button"> <button class="btn btn-success left" type="button">
<a href="/#!/form/{{form._id}}" style="color:white;"> Submit again?</a> <a href="/#!/forms/{{form._id}}" style="color:white;"> Submit again?</a>
</button> </button>
</p> </p>
<p class="text-center col-xs-2" ng-if="authentication.isAuthenticated()"> <p class="text-center col-xs-2" ng-if="authentication.isAuthenticated()">
<button class="btn btn-caution left" type="button"> <button class="btn btn-caution left" type="button">
<a href="/form/{{form.id}}/admin" style="color:white;">Edit Form</a> <a href="/#!/forms/{{form._id}}/admin" style="color:white;">Edit Form</a>
</button> </button>
</p> </p>
</div> </div>

View file

@ -25,20 +25,32 @@
</p> </p>
</div> </div>
<div class="submissions-table col-xs-8" ng-if="viewSubmissions"> <div class="submissions-table col-xs-8" ng-if="viewSubmissions">
<table class="table table-striped"> <table class="table table-striped table-hover table-condensed">
<thead> <thead>
<tr> <tr>
<th>#</th> <th>#</th>
<th data-ng-repeat="(key, val) in submissions[0].form_fields"> <th data-ng-repeat="(key, value) in submissions[0].form_fields">
{{key}} {{value.title}}
</th>
<th>
Time Elapsed
</th>
<th>
Date Submitted (UTC)
</th> </th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr data-ng-repeat="submission in submissions"> <tr data-ng-repeat="submission in submissions">
<td>{{$index+1}}</td> <th class="scope">{{$index+1}}</th>
<td data-ng-repeat="(key, val) in submission.form_fields"> <td data-ng-repeat="(key, value) in submission.form_fields">
{{value}} {{value.fieldValue}}
</td>
<td>
{{submission.timeElapsed}}
</th>
<td>
{{submission.created | date:'yyyy-MM-dd HH:mm:ss'}}
</td> </td>
</tr> </tr>
</tbody> </tbody>