UNPKG

unserver-unify

Version:

427 lines (404 loc) 14 kB
'use strict'; angular.module('bamboo.course').controller('MyProfileCtrl', function($scope,UserService,$location,AppConfig,ApiService,$window,Lightbox, loginService, CommonService, $state,formService, $filter,$uibModal, $translate) { var _self = this; $scope.isVisible = {}; this.defaultpic = ApiService.SHOST + "/public/images/videos.png"; this.coursePhotoUrl = ApiService.SHOST + '/lcourse/'; this.subjectPicUrl = ApiService.SHOST + "/public/" + ApiService.RES; this.defaultPersonPhotoUrl = "assets/images/default-person-icon.png"; this.imagePicUrl = ApiService.PSURL + "/avatar/"; this.userprofile = {}; var ceoAdditionalFields = {head_name:'Immediate superior / Head of Department Name',head_email:'Immediate superior / Head of Department Email',head_no:'Immediate superior / Head of Department No',dean_name:'Dean / Director Name',dean_email:'Dean / Director Email',dean_no:'Dean / Director No'}; this.resumePath=ApiService.SHOST + "/resume/"; //it represent if the filetype is pdf this.detectedFileType = false; var uploadSetting = { image: true, promise: true, s3: true, title: "User Avatar", maxheight: 200, maxwidth: 200, ctrl: _self, id: null, //_self.contentModel.id, resPath: null, imageUrl: null, action: "avatar", usewebcam: true }; $scope.password = {}; function getActivitySummary() { var info = { action: 'getuseractivity', id: $scope.user.id, } UserService.UserApi(info, function(result) { if (!result) { return; } $scope.totalminutes=parseInt(result.total/60); //console.log(result.history); var history=result.history; var label = []; var shortLabel=[]; var data = []; var now=new Date; var len=1000*60*60*24*60; for (var key in history) { var time=new Date(key); if(now-time<len){ label.push(key); // shortLabel.push(key.substring(5)); } } label.sort(); for (var i=0;i<label.length;i++){ var key=label[i]; data.push(parseInt(history[key].total/60)); shortLabel.push(key.substring(5)); } // console.log(shortLabel); var _colorstr = '24,164,107'; var _data = { labels: shortLabel, datasets: [{ label: $translate.instant('Usages'), fillColor: 'rgba(' + _colorstr + ',1)', strokeColor: 'rgba(' + _colorstr + ',1)', pointColor: 'rgba(' + _colorstr + ',1)', pointStrokeColor: '#fff', pointHighlightFill: '#fff', pointHighlightStroke: 'rgba(' + _colorstr + ',1)', data: data, }] }; $scope.activitydata=_data; var label1 = []; var data1 = []; for (var key in result.summary) { label1.push(key); data1.push(parseInt(result.summary[key]/60)); } //console.log(label1); // console.log(data1); var _colorstr = '24,164,107'; var _data1 = { labels: label1, datasets: [{ label: $translate.instant('Usages'), fillColor: 'rgba(' + _colorstr + ',1)', strokeColor: 'rgba(' + _colorstr + ',1)', pointColor: 'rgba(' + _colorstr + ',1)', pointStrokeColor: '#fff', pointHighlightFill: '#fff', pointHighlightStroke: 'rgba(' + _colorstr + ',1)', data: data1, }] }; $scope.categorydata=_data1; }); } $scope.options = AppConfig.CHARTOPTIONS; loginService.getMyProfile(true).then(function(result) { console.log(result); $scope.user = result; $scope.user.birthday= $scope.user.birthday && new Date($scope.user.birthday); $scope.user.enrol= $scope.user.enrol && new Date($scope.user.enrol); getActivitySummary(); // videoresumeSetting.resPath=resumeSetting.resPath = "resume/" + $scope.user.id + "/"; $scope.user_profiles=result.user_profiles; $scope.resume=result.resume; _self.userProfiledoc=loginService.school.userProfiledoc; console.log(_self.userProfiledoc); //if($scope.user && $scope.user.resume){ //_self.detectedFileType = detectFileType(); // } if(_self.userProfiledoc&&(!$scope.user.grole||$scope.user.grole=='user')){ ApiService.get('/schooldoc/'+_self.userProfiledoc).then(function(result) { console.log(result); if(result.data.success){ _self.profileform = result.data.data.content; if (_self.profileform && _self.profileform.form_fields && _self.profileform.form_fields.length > 0 && $scope.user_profiles) { for (var i = 0; i < _self.profileform.form_fields.length; i++) { console.log(_self.profileform.form_fields[i].field_title); console.log($scope.user_profiles); if ($scope.user_profiles[_self.profileform.form_fields[i].field_title]) { _self.profileform.form_fields[i].field_value = $scope.user_profiles[_self.profileform.form_fields[i].field_title]; _self.profileform.form_fields[i].field_number = $scope.user_profiles[_self.profileform.form_fields[i].field_title]; } } } console.log(_self.profileform); } }) } // _self.profileform = loginService.school.userProfile; $scope.tmpUser = angular.copy($scope.user); if($scope.tmpUser.user_profiles){ console.log($scope.tmpUser.user_profiles); angular.forEach(ceoAdditionalFields, function(value, key) { _self.userprofile[key] = $scope.tmpUser.user_profiles[value]; }); } console.log($scope.tmpUser); delete $scope.tmpUser.user_profiles; uploadSetting.id = $scope.user.id; uploadSetting.resPath = "public/" + ApiService.RES + "/avatar/" + uploadSetting.id + "/"; if ($scope.user.avatar) { uploadSetting.imageUrl = ApiService.SHOST + "/" + uploadSetting.resPath + $scope.user.avatar; } if(!$scope.user_profiles){ $scope.user_profiles={}; } console.log("--------log---------"); console.log(self.detectedFileType); }); /*var resumeSetting = { promise: true, accept: ".pdf,.doc,.docx,.xls", s3: true, title: "Resume", randomFlag: true, maxSize: 5000, }; this.uploadResume = function() { CommonService.uploadFile(resumeSetting).then(function(result) { var info={ resume:result.fileName, id:$scope.user.id, } ApiService.post('/profile',info).then(function(result) { if (result.data.success) { CommonService.showNoBlockInfo($filter('trans')({ eng: 'Update Successful!', chn: '更新用户成功!' })); $state.reload(); } else { //$scope.error = "Profile Update failed: " + result.data.error; } }); }); };*/ formService.initFormDate(); this.showTab = "profile"; $scope.editProfile = function() { _self.showTab = "editProfile"; }; $scope.profile = function() { _self.showTab = "profile"; }; this.showTab1 = "basicProfile"; $scope.basicProfile = function() { _self.showTab1 = "basicProfile"; }; $scope.eduProfile = function() { _self.showTab1 = "eduProfile"; }; $scope.advanceProfile = function() { _self.showTab1 = "advanceProfile"; }; $scope.saveUser = function(isValid) { $scope.submitted = true; console.log(isValid); console.log($scope.user); var exKeys=['dids','lastLogin','favourites','subjectIds','summary','courses']; //$scope.user = JSON.parse(JSON.stringify($scope.tmpUser)); var toSave={id:$scope.user.id}; for(var key in $scope.tmpUser){ if(exKeys.indexOf(key)<0){ if($scope.tmpUser[key]!=$scope.user[key]){ toSave[key]=$scope.tmpUser[key]; } } } var user_profiles = {}; console.log(_self.userprofile); angular.forEach(ceoAdditionalFields, function(value, key) { user_profiles[value] = _self.userprofile[key]; }); console.log(user_profiles); toSave['user_profiles'] = user_profiles; // delete $scope.user.email; console.log(toSave); ApiService.post('/profile', toSave).then(function(result) { $scope.submitted = false; console.log(result); if (result.data.success) { CommonService.showNoBlockInfo($filter('trans')({ eng: 'Update Successful!', chn: '更新用户成功!' })); $state.reload(); } else { $scope.error = "Profile Update failed: " + result.data.error; } }); } this.saveEmail=function(){ console.log($scope.user); var info={ id:$scope.user.id, email:$scope.tmpUser.email, action: "changeuseremail", } console.log(info); ApiService.post('/user', info).then(function(result) { if (result.data.success) { CommonService.showNoBlockInfo("Updated!"); $state.reload(); } else { CommonService.showNoBlockErr(result.data.error); } }); } this.showPlan = function(){ $uibModal.open({ templateUrl: "components/dialog/priceplan-dlg.html", controller: "PricingDlgCtrl", controllerAs: 'ctrl', size: 'lg' }).result.then(function(result) { if(result) { var info = { action: 'paypalBuyUserPlan', id: result, url: $location.absUrl(), }; ApiService.post("/payment", info).then(function(result) { console.log(result); if (result.data.success && result.data.data.redirectUrl) { CommonService.showInfo('Redirect to Payment Page , Please wait ...'); $window.location.href = result.data.data.redirectUrl; } else { CommonService.showError("Failed: " + result.data.message); } }); } }); } this.buyMember=function(){ var info={ action:'paypalbuymember', url: $location.absUrl(), } ApiService.post('/payment', info).then(function(result) { console.log(result); if (result.data.success) { console.log(result.data.data); if (result.data.data.redirectUrl) { CommonService.showInfo('Redirect to Payment Page , Please wait ...'); $window.location.href = result.data.data.redirectUrl; } } }); } this.updateProfile = function() { var _updateFields = formService.getFormData(); if (_updateFields) { if (!$scope.user_profiles) { $scope.user_profiles = {}; } } angular.forEach(_updateFields, function(value, key) { $scope.user_profiles[key] = value; }); var info={ id:$scope.user.id, user_profiles:$scope.user_profiles, } console.log(info); ApiService.post('/profile',info).then(function(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; } }); } $scope.cancelSaveUser = function() { //$scope.tmpUser = JSON.parse(JSON.stringify($scope.user)); _self.showTab='profile'; } $scope.savePassword = function(isvalid) { $scope.passworksubmitted = true; if (!isvalid) { return; } if ($scope.password.oldp == $scope.password.newp) { $scope.passworderr = $filter('trans')({ eng: 'new password MUST not be same as your current password!', chn: '新密码不能和老密码相同!' }); return; } if ($scope.password.newp != $scope.password.newp2) { $scope.passworderr = $filter('trans')({ eng: 'Entered two different new password, please re-enter', chn: '两次新密码输入不同,请确认后重新输入' }); return; } $scope.passworksubmitted = false; ApiService.put('/password', $scope.password).then(function(result) { if (result.data.success) { CommonService.showNoBlockInfo('Update Successful!'); $state.reload(); } else { $scope.error = result.data.error; } }); } this.uploadFile = function() { console.log(uploadSetting); CommonService.uploadFile(uploadSetting).then(function(result) { console.log(result); updateAvatar(result.fileName); }); }; function updateAvatar(file) { var info = { action: 'avatar', id: $scope.user.id, file: file, }; console.log(info); ApiService.post('/user', info).then(function(result) { //console.log(result); if (result.data.success) { CommonService.showNoBlockInfo('Update Successful!'); $state.reload(); } else { $scope.error = "Profile Update failed: " + result.data.error; } }); } function getArray(obj, array, level) { if ((typeof obj) === "object") { for (var key in obj) { var _i = { key: key, value: level, title: key, items: [], }; array.push(_i); var info = obj[key]; if (typeof info === "object") { getArray(info, _i.items, level + 1); } } } } $scope.showCalendar = function($event, id) { console.log(id); $event.preventDefault(); $event.stopPropagation(); $scope.isVisible[id] = true; }; });