UNPKG

unserver-unify

Version:

157 lines (153 loc) 5.23 kB
'use strict'; angular.module('bamboo.course').controller("CFCourseCtrl", function($scope, $window, $state, loginService, $location, $stateParams, ApiService, UserService, CommonService, VideoService, $document, $interval) { this.id = $stateParams.cid; console.log($stateParams); var self = this; this.item = {}; this.teacherPicUrl = ApiService.SHOST + "/photo/" + ApiService.RES + "/teachers/"; $scope.timeUp = false; $scope.fundingResult = false; self.currency = (loginService.school.currency&&loginService.school.currency.sign)?loginService.school.currency.sign:'$'; this.defaultPhotoUrl = "assets/images/course_no-image.jpg"; this.newsPicUrl = ApiService.SHOST + "/cfcourses/"; this.promise = {}; ApiService.get("/cfcourse/" + self.id).then(function(result) { // console.log( '-Starting auth session result: '); if (result.data.success) { console.log(result.data.data); self.item = result.data.data || {}; if (self.item.photo) { self.imageUrl = self.newsPicUrl + self.item.photo; } if (self.item.end) { var end = new Date(self.item.end); var now = new Date(); var countdown = end.getTime() - now.getTime(); console.log(countdown); self.item.countdown = countdown > 0 ? countdown / 1000 : 0; $scope.countdown = self.item.countdown; console.log($scope.countdown); $scope.end = end; } if (self.item.editors) { getEditorInfo(self.item.editors); } //self.promise = $interval(isTimeUp, 1000); } }); this.editors = []; function getEditorInfo(names) { // get teacher info for all editors UserService.getTeachersInfoByLoginnames(names, function(data) { var teacherinfos = {}; self.editors = []; angular.forEach(data, function(teacher, index) { if (teacher.photo && !teacher.avatarflag) { teacher.photourl = self.teacherPicUrl + teacher._id + "/" + teacher.photo; } else if (teacher.avatar) { var user = { id: teacher.userid, avatar: teacher.avatar, } teacher.photourl = CommonService.getAvatarSrc(user); } else { teacher.photourl = self.defaultPersonPhotoUrl; } self.editors.push(teacher); }) }); } this.diableEnrol; if (loginService.isLogged) { var info = { action: 'checkmyapplication', cid: self.id, } ApiService.post('/cfcourse', info).then(function(result) { console.log(result); if (result.data.success) { self.diableEnrol = result.data.data; } }); } this.enroll = function() { if (self.item.finialprice || self.item.price) { var info = { action: "paypalbookprecourse", lid: self.id, 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; } else if (result.data.data.enrolled) { /* $state.go("index.courses.course", { cid: _self.id, cs:ApiService.getCheckSum(_self.id) }); */ } } }); } else { var params = { cid: self.id, action: 'addapplication' }; ApiService.post('/cfcourse', params).then(function(result) { //console.log(result); if (result.data.success) { CommonService.showNoBlockInfo('Enroll Successfully!'); $state.reload(); } }); } } $scope.scrollTo = function(id) { var duration = 1000; //milliseconds var offset = 100; //pixels; adjust for floating menu, context etc //Scroll to #some-id with 30 px "padding" //Note: Use this in a directive, not with document.getElementById var someElement = angular.element(document.getElementById(id)); $document.scrollToElement(someElement, offset, duration); }; function isTimeUp() { $scope.now = new Date(); var diff = $scope.now.getTime() - $scope.end.getTime(); if (diff >= 0) { $scope.timeUp = true; getFundingResult(); $interval.cancel(self.promise); } //console.log(diff); } function getFundingResult() { if (self.item && (self.item.applications >= self.item.mincounter)) { $scope.fundingResult = true; } else { unpublish(); } } function unpublish() { var object = { enable: false, } var info = { object: object, } info.action = 'update'; info.id = self.id; console.log(info); ApiService.post('/cfcourse', info).then(function(result) { //console.log(result.data); if (result.data.success) { console.log(result.data.data); } else { console.log(result.data.error); } }); } });