unserver-unify
Version:
96 lines (91 loc) • 2.71 kB
JavaScript
;
angular.module('bamboo.additional').controller('TakeSurveyCtrl', function(ApiService, $scope, loginService, CommonService, $filter, $stateParams, $state, SureyApi) {
var self = this;
this.userprofile = {};
this.wizardstep = 0;
this.takenSurvey = false;
self.id = $stateParams.csvid;
if(!self.id) {
$state.go('^');
} else {
console.log(self.id);
}
$scope.login = {
domain: loginService.subDomain,
userType: 'student'
};
if(loginService.isLogged){
getUserInfo();
}
this.submitForm = function(form,login) {
if (form.$invalid) {
return;
} else {
login.ownername = loginService.school.name;
loginService.login(login).then(function(user) {
if(user&&user._id){
$state.reload();
}
});
}
};
var profileFields = {highest_qualification:'Highest Qualification',major:'Major',university:'University / College',educational_type:'Education Type',passing_year:'Passing Year',skills:'Skills'};
function getUserInfo()
{
loginService.getMyProfile(true).then(function(result) {
console.log(result);
$scope.user = result;
if(result.user_profiles){
self.wizardstep = 2;
checkSurveyIsTaken();
angular.forEach(profileFields, function(value, key) {
self.userprofile[key] = result.user_profiles[value];
});
} else {
self.wizardstep = 1;
}
});
}
function checkSurveyIsTaken() {
var info = {
action: 'checksurveyexist',
id: self.id
};
SureyApi.targetApi(info, function(result) {
console.log(result);
if(result) {
self.takenSurvey = true;
}
});
}
this.profileForm = function(form) {
$scope.submitted = true;
if (form.$invalid) {
return;
} else {
$scope.user_profiles = {};
angular.forEach(profileFields, function(value, key) {
$scope.user_profiles[value] = self.userprofile[key];
});
console.log($scope.user_profiles);
var info={
id:$scope.user.id,
user_profiles:$scope.user_profiles,
}
console.log(info);
ApiService.post('/profile',info).then(function(result) {
console.log(result);
$scope.submitted = false;
if (result.data.success) {
CommonService.showNoBlockInfo($filter('trans')({
eng: 'Update Successful!',
chn: '更新用户成功!'
}));
$state.reload();
} else {
$scope.error = "Profile Update failed: " + result.data.error;
}
});
}
}
});