unserver-unify
Version:
310 lines (298 loc) • 9.58 kB
JavaScript
;
angular.module('bamboo.survey').controller('showResultCtrl', function($scope, SureyApi, $rootScope, $stateParams, $state, CommonService, $filter, loginService) {
var self = this;
self.name = $state.current.name; // "index.survey.detail"
console.log(self.name);
self.id = $stateParams.id;
//self.forceResult
self.forceResult = $stateParams.force;
console.info(self.forceResult);
//console.log(self.forceResult);
self.answered = $stateParams.answered;
// self.take = $stateParams.take;
self.resultFlag = false;
self.item = {};
console.log("------id-------");
console.log($stateParams);
$scope.options = {
// Sets the chart to be responsive
responsive: true,
//Boolean - Whether we should show a stroke on each segment
segmentShowStroke: true,
//String - The colour of each segment stroke
segmentStrokeColor: '#fff',
//Number - The width of each segment stroke
segmentStrokeWidth: 2,
//Number - The percentage of the chart that we cut out of the middle
percentageInnerCutout: 50, // This is 0 for Pie charts
//Number - Amount of animation steps
animationSteps: 100,
//String - Animation easing effect
animationEasing: 'easeOutBounce',
//Boolean - Whether we animate the rotation of the Doughnut
animateRotate: true,
//Boolean - Whether we animate scaling the Doughnut from the centre
animateScale: false,
//String - A legend template
legendTemplate: '<ul class="tc-chart-js-legend"><% for (var i=0; i<segments.length; i++){%><li><span style="background-color:<%=segments[i].fillColor%>"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>'
};
var _info = {
action: 'getallpublish'
};
SureyApi.api(_info, function(result) {
self.surveys = result;
if (loginService.user) {
self.displaySurveys = result;
} else {
self.displaySurveys = filterPublicSurvey(result);
}
console.log("-------displaySurveys------------");
console.log(self.displaySurveys);
});
function filterPublicSurvey(result) {
var res = [];
angular.forEach(result, function(val, idx) {
if (val.public) res.push(val);
});
return res;
};
var questions = {};
function getText(string) {
return string.replace(/ /g, '').replace(/<[^>]*>/g, "").replace(/\./g, ',');
}
//this.forceResult=false;
this.showresult = function() {
getResult();
self.forceResult = true;
}
getResult();
// function getResult() {
// var info = {
// action: 'getsurveyresult',
// id: self.id
// };
// SureyApi.api(info, function(result) {
// self.summary = result;
// console.log("----result22---------");
// console.log(result);
// angular.forEach(self.summary, function(_record, key) {
// var results = [];
// var existedkeys = [];
// angular.forEach(_record, function(num, _key) {
// if (_key != "_total" && _key != '_type') {
// existedkeys.push(_key);
// results.push(getDoughnutChartData(_record._total, _key, num));
// }
// })
// console.log(key);
// if (questions[key]) {
// console.log(questions[key]);
// angular.forEach(questions[key], function(_key) {
// if (existedkeys.indexOf(_key) < 0) {
// results.push(getDoughnutChartData(_record._total, _key, 0));
// existedkeys.push(_key);
// }
// })
// }
// _record.data = results;
// self.forceResult = $stateParams.force;
// });
// });
// }
function getResult() {
var info = {
action: 'getsurveyresult',
id: self.id
};
SureyApi.api(info, function(result) {
self.summary = result;
console.log("----result22---------");
console.log(result);
angular.forEach(self.summary, function(_record, key) {
var results = [];
var chartData = {};
var existedkeys = [];
var datasets = [];
var data = [];
var labels = [];
var colors = [];
angular.forEach(_record, function(num, _key) {
if (_key != "_total" && _key != '_type') {
existedkeys.push(_key);
data.push(num);
colors.push(CommonService.getHexRandomColorStr());
labels.push(_key);
}
})
console.log(key);
if (questions[key]) {
console.log(questions[key]);
angular.forEach(questions[key], function(_key) {
if (existedkeys.indexOf(_key) < 0) {
data.push(0);
colors.push(CommonService.getHexRandomColorStr());
labels.push(_key);
existedkeys.push(_key);
}
})
}
datasets.push({
data: data,
backgroundColor: colors,
hoverBackgroundColor: colors
});
chartData.labels = labels;
chartData.datasets = datasets;
_record.data = chartData;
});
console.log(self.summary);
});
}
function getInfo() {
var info = {
action: 'get',
id: self.id
};
SureyApi.api(info, function(result) {
self.item = result;
var _qs = result.questions;
var index = 1;
angular.forEach(_qs, function(q,inc) {
var key = q.index||inc;
// q.index = index;
if (q.type != "heading") {
index++;
}
var options = [];
angular.forEach(q.options, function(op) {
if (op) {
var text = getText(op.text);
options.push(text);
}
})
if (options.length > 0) {
questions[key] = options;
}
})
console.log(questions);
$rootScope.currentSurveyTitle = self.item.name;
var infor = {
action: 'checksurveyexist',
id: self.id
};
if (!loginService.user) return;
SureyApi.api(infor, function(data) {
console.log(data);
if (data != 0) {
self.resultFlag = true;
if (self.item.result) {
console.log("-------------");
console.log(self.item.result);
getResult();
}
}
});
});
}
$scope.numToLetters = function(num) {
return String.fromCharCode(65 + num);
};
this.submit = function() {
var allanswered = true;
var lastunanswered = [];
console.log("------------submit----------------");
console.log(self.item);
var result = {};
var _index = 0;
angular.forEach(self.item.questions, function(val, index) {
console.log(val);
if (val.type != 'heading') {
_index++;
// console.log(getText(val.content));
var key=val.index;
/* var key = getText(val.content);
if (key.indexOf('.') > -1) {
key = key.split('.').join('');
}*/
var ans = val.selection;
if (val.type && val.type == 'multiple') {
var ans = [];
angular.forEach(val.options, function(opt, index) {
if (opt.selection) {
ans.push(opt.text);
}
})
result[key] = ans;
} else {
result[key] = ans;
if (typeof val.selection == 'undefined') {
lastunanswered.push(_index);
allanswered = false;
}
}
console.log(val.selection);
}
});
if (!allanswered) {
CommonService.showNoBlockErr('Not all question answered!, Please check No .' + lastunanswered.toString() + "!");
} else {
var info = {
id: self.id,
action: 'submitsurvey',
object: {
v1result: result,
sid: self.id,
}
};
SureyApi.api(info, function() {
var options = {
title: $filter('trans')({
eng: 'Thank you!',
chn: '谢谢!'
}),
message: $filter('trans')({
eng: 'Thank you for submitting the survey!',
chn: '谢谢您参加我们的问卷调查!'
})
}
CommonService.messageBox(options);
console.log("survey submited");
$state.reload();
//$state.go($rootScope.$previousState, $rootScope.$previousStateParams);
})
}
};
function getText(string) {
return string.replace(/ /g, '').replace(/<[^>]*>/g, "");
}
var label = [];
$scope.legend = [];
function getDoughnutChartData(total, label, data, color) {
var _colorstr;
if (color) {
_colorstr = color;
} else {
_colorstr = 'rgba(' + getRandomColorStr() + ',1)';
}
var result = {
value: data,
color: _colorstr,
highlight: '#FFFF66',
label: "[" + label + "] " + (100 * data / total).toString().substring(0, 5) + "%"
};
$scope.legend.push({
fillColor: _colorstr,
label: label
});
return result;
}
function getRandomColorStr() {
var color = Math.floor(Math.random() * 256).toString(10);
color += ',';
color += Math.floor(Math.random() * 256).toString(10);
color += ',';
color += Math.floor(Math.random() * 256).toString(10);
return color;
}
getInfo();
})