tellform/public/modules/forms/admin/directives/edit-submissions-form.client.directive.js

175 lines
5.9 KiB
JavaScript
Raw Normal View History

2015-08-18 21:44:36 +00:00
'use strict';
2016-04-28 23:47:50 +00:00
angular.module('forms').directive('editSubmissionsFormDirective', ['$rootScope', '$http',
2016-04-27 20:02:01 +00:00
function ($rootScope, $http) {
2015-08-18 21:44:36 +00:00
return {
2016-05-05 19:12:40 +00:00
templateUrl: 'modules/forms/admin/views/directiveViews/form/edit-submissions-form.client.view.html',
2015-08-18 21:44:36 +00:00
restrict: 'E',
scope: {
myform:'=',
user:'='
},
controller: function($scope){
$scope.table = {
masterChecker: false,
rows: []
2015-08-18 21:44:36 +00:00
};
2016-06-17 21:33:33 +00:00
(function initController(){
var defaultFormFields = _.cloneDeep($scope.myform.form_fields);
//Iterate through form's submissions
var submissions = _.cloneDeep($scope.myform.submissions);
for(var i = 0; i < submissions.length; i++){
for(var x = 0; x < submissions[i].form_fields; x++){
var oldValue = submissions[i].form_fields[x].fieldValue || '';
submissions[i].form_fields[x] = _.merge(defaultFormFields, submissions[i].form_fields);
submissions[i].form_fields[x].fieldValue = oldValue;
}
submissions[i].selected = false;
}
// console.log('after textField2: '+data[0].form_fields[1].fieldValue);
$scope.table.rows = submissions;
// console.log('form submissions successfully fetched');
// console.log( JSON.parse(JSON.stringify($scope.submissions)) ) ;
// console.log( JSON.parse(JSON.stringify($scope.myform.form_fields)) );
})();
/*
** Analytics Functions
*/
$scope.AverageTimeElapsed = (function(){
var totalTime = 0;
var numSubmissions = $scope.table.rows.length;
for(var i=0; i<$scope.table.rows.length; i++){
totalTime += $scope.table.rows[i].timeElapsed;
}
console.log(totalTime/numSubmissions);
return totalTime/numSubmissions;
})();
$scope.DeviceStatistics = (function(){
var newStatItem = function(){
return {
visits: 0,
responses: 0,
completion: 0,
average_time: 0,
total_time: 0
2016-06-20 22:06:41 +00:00
};
2016-06-17 21:33:33 +00:00
};
2016-06-17 22:11:02 +00:00
var stats = {
desktop: newStatItem(),
tablet: newStatItem(),
phone: newStatItem(),
other: newStatItem()
};
2016-06-17 21:33:33 +00:00
var visitors = $scope.myform.analytics.visitors;
for(var i=0; i<visitors.length; i++){
var visitor = visitors[i];
var deviceType = visitor.deviceType;
stats[deviceType].visits++;
stats[deviceType].total_time =+ visitor.timeElapsed;
2016-06-17 21:51:17 +00:00
stats[deviceType].average_time = stats[deviceType].total_time/stats[deviceType].visits || 0;
2016-06-17 21:33:33 +00:00
2016-06-17 21:51:17 +00:00
if(visitor.isSubmitted) stats[deviceType].responses++;
2016-06-17 21:33:33 +00:00
2016-06-17 21:51:17 +00:00
stats[deviceType].completion = stats[deviceType].response/stats[deviceType].visits || 0;
2016-06-17 21:33:33 +00:00
}
console.log(stats);
return stats;
})();
2015-08-18 21:44:36 +00:00
/*
** Table Functions
*/
$scope.isAtLeastOneChecked = function(){
for(var i=0; i<$scope.table.rows.length; i++){
if($scope.table.rows[i].selected) return true;
}
return false;
};
$scope.toggleAllCheckers = function(){
for(var i=0; i<$scope.table.rows.length; i++){
$scope.table.rows[i].selected = $scope.table.masterChecker;
}
};
$scope.toggleObjSelection = function($event, description) {
$event.stopPropagation();
};
$scope.rowClicked = function(row_index) {
$scope.table.rows[row_index].selected = !$scope.table.rows[row_index].selected;
};
/*
* Form Submission Methods
*/
//Fetch and display submissions of Form
2015-09-15 22:21:49 +00:00
2015-08-18 21:44:36 +00:00
//Delete selected submissions of Form
$scope.deleteSelectedSubmissions = function(){
2015-08-18 21:44:36 +00:00
var delete_ids = _.chain($scope.table.rows).filter(function(row){
return !!row.selected;
}).pluck('_id').value();
2016-04-29 06:00:41 +00:00
$http({ url: '/forms/'+$scope.myform._id+'/submissions',
2015-08-18 21:44:36 +00:00
method: 'DELETE',
data: {deleted_submissions: delete_ids},
headers: {'Content-Type': 'application/json;charset=utf-8'}
}).success(function(data, status, headers){
//Remove deleted ids from table
var tmpArray = [];
for(var i=0; i<$scope.table.rows.length; i++){
if(!$scope.table.rows[i].selected){
tmpArray.push($scope.table.rows[i]);
}
}
$scope.table.rows = tmpArray;
})
.error(function(err){
console.log('Could not delete form submissions.\nError: ');
console.log(err);
console.error = err;
2016-04-29 06:00:41 +00:00
});
2015-08-18 21:44:36 +00:00
};
//Export selected submissions of Form
$scope.exportSubmissions = function(type){
var fileMIMETypeMap = {
'xls': 'vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'json': 'json',
2016-04-29 06:00:41 +00:00
'csv': 'csv'
2015-08-18 21:44:36 +00:00
};
2016-05-16 22:26:10 +00:00
console.log($scope.table.rows);
2016-06-17 21:33:33 +00:00
2016-05-17 00:47:34 +00:00
angular.element('#table-submission-data').tableExport({type: type, escape:false});
2016-05-16 22:26:10 +00:00
2016-05-17 00:47:34 +00:00
/*
2016-05-16 22:26:10 +00:00
var blob = new Blob([$scope.table.rows], {
2015-08-18 21:44:36 +00:00
type: 'application/'+fileMIMETypeMap[type]+';charset=utf-8'
});
saveAs(blob, $scope.myform.title+'_sumbissions_export_'+Date.now()+'.'+type);
2016-05-17 00:47:34 +00:00
*/
2015-08-18 21:44:36 +00:00
};
}
};
}
2016-04-17 02:45:17 +00:00
]);