unserver-unify
Version:
155 lines (154 loc) • 4.91 kB
JavaScript
;
angular.module('bamboo.comadmin').controller('CompanyEvaluationCtrl', function($rootScope, ApiService, $state, EvaluationApi, $stateParams, CommonService) {
var self = this;
this.eid = $stateParams.eid;
this.uid = $stateParams.uid;
this.jid = $stateParams.jid;
this.record = {};
function getInfo() {
ApiService.get('/useravatar/' + self.uid).then(function(result) {
if (result.data.success) {
// console.log(result.data.data);
self.user = result.data.data;
$rootScope.fullname = self.user.fullname;
self.avatarurl = CommonService.getAvatarSrc(self.user);
}
})
var info = {
action: "get",
id: self.eid,
}
ApiService.post('/evaluation', info).then(function(result) {
if (result.data.success) {
self.evaluation = result.data.data;
var groupQs = [];
var singleQs = [];
angular.forEach(result.data.data.questions, function(q) {
if (q.group) {
groupQs.push(q);
} else {
singleQs.push(q);
}
})
self.questions = singleQs;
var info1 = {
userId: self.uid,
refId: self.jid,
eid: self.eid,
action: 'recallRecord',
}
console.log(info1);
EvaluationApi.api(info1, function(record) {
console.log(record);
if (record) {
self.record = record;
var result = record.result;
angular.forEach(self.questions, function(q) {
if (result[q.index] || result[q.index] == 0) {
switch (q.type) {
case 'rating':
case 'single':
case 'fill':
q.selection = result[q.index];
break;
}
}
})
self.rated();
}
});
// self.questions = result.data.data.questions;
console.log(self.questions);
} else {
var error = result.data.error || '' + result.data.info || '';
CommonService.showError("Update failed: " + error);
}
});
}
getInfo();
this.rated = function() {
console.log(self.questions);
var totalScores = 0;
self.questions.forEach(function(item) {
if (item.type == 'rating' && item.weightage && item.selection) {
totalScores += item.weightage * item.selection / 5;
}
})
self.totalScore = totalScores;
}
this.submit = function() {
var allanswered = true;
var lastunanswered = [];
console.log("------------submit----------------");
var result = {};
var _index = 0;
//result.username = self.item.username;
angular.forEach(self.questions, function(val, index) {
console.log(val);
if (val.type != 'heading') {
_index++;
var key = val.index;
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 {
if (self.record._id) {
var info = {
id: self.record._id,
action: 'updateRecord',
object: {
result: result,
username: self.user.username,
fullname: self.user.fullname,
name: self.user.loginname,
scores: self.totalScore,
}
};
EvaluationApi.api(info, function(result) {
$state.go('^');
})
} else {
var info = {
id: this.eid,
action: 'addRecord',
object: {
result: result,
// eid: self.eid,
userId: self.uid,
refId: self.jid,
username: self.user.username,
fullname: self.user.fullname,
name: self.user.loginname,
scores: self.totalScore,
}
};
ApiService.post('/evaluation', info).then(function(result) {
if (result.data.success) {
$state.go('^');
} else {
var error = result.data.error || '' + result.data.info || '';
CommonService.showError("Update failed: " + error);
}
})
}
}
};
})