unserver-unify
Version:
474 lines (467 loc) • 15.7 kB
JavaScript
;
angular.module('bamboo.course').controller('CourseInfoCtrl', function($scope, loginService, $window, youtubeEmbedUtils, pwdService, $rootScope, $state, $sce, ApiService, CourseApi, CommonService, $stateParams, CourseService, AppConfig, UserService, $location, $filter, deviceDetector, $timeout) {
this.id = $stateParams.cid;
this.course = {};
$scope._user = loginService.user;
var self = this;
console.log($stateParams);
self.shareFlag = $stateParams.share;
this.courseURL = ApiService.SHOST + '/lcourse/';
this.teacherdefault = 'assets/images/default-person-icon.png';
this.defaultPersonPhotoUrl = "assets/images/default-person-icon.png";
this.PicUrl = ApiService.SHOST + '/public/' + ApiService.RES;
this.teacherPicUrl = ApiService.SHOST + "/photo/" + ApiService.RES + "/teachers/";
this.coursePhotoUrl = ApiService.SHOST + '/lcourse/' + self.id + "/";
this.mvPhotoUrl = ApiService.SHOST + '/lcourse/' + self.id + '/download/';
$scope.averageRating = 0;
var colorlist = ['color-one', 'color-two', 'color-three', 'color-four', 'color-five', 'color-six', 'color-seven'];
var servicecolor = ['service-block-yellow', 'service-block-blue', 'service-block-dark', 'service-block-aqua', 'service-block-red', 'service-block-green', 'service-block-sea', 'service-block-purple', 'service-block-dark-blue'];
this.relatedcourses = [];
this.getlistcolor = function(index, level) {
var i = 5;
if (!level || level == 0) {
i = index % 2;
}
return colorlist[i];
}
this.mycourseFlag;
$scope.showSec = null;
loginService.getMyProfile().then(function(result) {
var mycourses = result.courses;
console.log(mycourses);
if (mycourses.indexOf(self.id) > -1) {
self.mycourseFlag = true;
console.log("my course");
}
});
this.handleContentClick = function() {
if (self.mycourseFlag) {
$state.go("index.courses.course", {
cid: self.id,
cs: ApiService.getCheckSum(self.id)
},{reload: true, inherit: false});
} else if (loginService.isLogged && (self.course.privacy != 'fee') && !loginService.school.disableAdminStudy) {
self.enroll();
} else if (!loginService.isLogged) {
$rootScope.showCourseLoginDialog();
}
}
var commentstart = 0;
this.sendComment = function() {
var params = {
r_content: self.commentscontent,
lid: self.id,
action: "addcoursecomments",
};
ApiService.post('/mvtopics', params).then(function(result) {
if (result.data.success) {
commentstart = 0;
self.commentscontent = "";
getLatestTopics();
} else {
CommonService.showError('Update Fail!');
}
});
};
function getLatestTopics() {
var info = {
action: 'getcoursecomments',
cid: self.id,
}
console.log(info);
ApiService.post('/mvtopics', info).then(function(result) {
console.log(result);
if (result.data.success && result.data.data) {
console.log(result.data.data);
self.coursetopics = result.data.data.topics;
self.commentscount = result.data.data.count;
angular.forEach(self.coursetopics, function(val, index) {
val.avatar_url = CommonService.getAvatarSrc(val.author);
})
// console.log(self.coursetopics);
//$state.reload();
} else {
// CommonService.showError('Update Fail!');
console.log(result.data);
}
});
};
this.enroll = function() {
console.log(loginService.user);
console.log(self.course);
if (self.course.privacy == 'member') {
if (!loginService.user.memberExpire) {
//CommonService.showError("Only Member can access the course, please enroll the membership");
pwdService.renewMemberDlg();
return;
} else {
var expire = new Date(loginService.user.memberExpire);
if (expire < new Date()) {
// CommonService.showError("Your member expired, please enroll again");
pwdService.renewMemberDlg();
return;
}
}
}
var info = {
action: "enroll",
lid: self.id,
}
console.log(info);
CourseApi.lcApi(info, function(result) {
console.log(result);
if (result.approved) {
loginService.getMyProfile(true);
if (!self.futureFlag) {
$state.go("index.courses.course", {
cid: self.id,
cs: ApiService.getCheckSum(self.id)
});
} else {
$timeout(function() {
$state.reload();
}, 100);
}
} else if (result.apply) {
CommonService.showInfo("Course Application Submitted");
$state.reload();
}
});
}
this.applybyCoupon = function() {
CommonService.changeText("Enter your coupon code :").then(function(result) {
// console.log(result);
if (!result || result.length < 6) {
return CommonService.showError("Invald code!");
}
var info = {
action: "couponEnrollCourse",
lid: self.id,
code: result,
}
// console.log(info);
CourseApi.lcApi(info, function(result) {
// console.log(result);
if (result.approved) {
loginService.getMyProfile(true);
if (!self.futureFlag) {
$state.go("index.courses.course", {
cid: self.id,
cs: ApiService.getCheckSum(self.id)
});
}
}
});
});
}
this.paypalPurchase = function() {
if (!loginService.school.paymentFlag) {
CommonService.showInfo("The payment function is not enabled. Please contact your system administrator: nicholas_tai@tsf.com.sg");
return;
}
var info = {
action: "paypalbuycourse",
lid: self.id,
url: $location.absUrl(),
}
if (loginService.school.paymentGateWay == 'wdpay') {
info.action = "weidongBuyCourse";
}
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) {
if (!self.futureFlag) {
$state.go("index.courses.course", {
cid: self.id,
cs: ApiService.getCheckSum(self.id)
});
}
}
}
});
}
function getErollExist() {
var info = {
action: "checkenrollexist",
lid: self.id,
}
CourseApi.lcApi(info, function(result) {
// console.log(result);
if (result) {
self.enrollinprocess = true;
}
});
}
function getEnrolledUsers() {
var info = {
start: 0,
limit: 50,
action: 'getenrolledavatar',
lid: self.id,
}
CourseApi.lcApi(info, function(result) {
angular.forEach(result.items, function(val, index) {
val.avatar_url = CommonService.getAvatarSrc(val);
})
$scope.displayedStudents = result.items;
$scope.totalenroll = result.counter;
console.log(result.items);
});
}
this.futureFlag=false;
function getInfo() {
$scope.id = self.id;
ApiService.get('/fcourse/' + self.id).then(function(result) {
if (result.data.success) {
console.log("-------getInfo-----------");
console.log(result.data);
if (!self.course.video&&!self.course.youtube&&!self.course.photo) {
self.imgdefault = 'assets/images/videos.png';
if (loginService.school.coursephoto) {
self.imgdefault = ApiService.SHOST + "/public/" + ApiService.RES + "/school/" + ApiService.gid + "/" + loginService.school.coursephoto;
}
}
self.course = result.data.data;
$scope.advinfo = result.data.data.advInfo;
var youtubeID = self.course.youtube && youtubeEmbedUtils.getIdFromURL(self.course.youtube);
$scope.youtubeUrl = $sce.trustAsResourceUrl('//www.youtube.com/embed/' + youtubeID + '?&controls=1&showinfo=0');
// $scope.youtubeUrl =$sce.trustAsResourceUrl( self.course.youtube);
console.log(self.course);
if (self.course.timecontrol && self.course.start) {
var start = new Date(self.course.start);
if (start > new Date) {
console.log('future start')
self.futureFlag = true;
}
}
// if (self.course.assessment) {
if (self.course.rating) {
$scope.averageRating = self.course.rating.toString().substring(0, 3);
}
// }
// config for outline video if available
if (self.course.video) {
//config video player
self.outlinemp4config = {
sources: [{}],
tracks: [{}],
theme: AppConfig.videogular_theme,
cuePoints: {}
};
self.outlinemp4config.sources = [{}];
self.outlinemp4config.sources[0].src = self.getPDFUrl(self.course.video);
self.outlinemp4config.sources[0].type = 'video/mp4';
}
self.course.contents = result.data.sections;
if (self.course.singlesection) {
// if (loginService.user) {
console.log(result.data);
var mvid = result.data.sections[0] && result.data.sections[0].id;
if (mvid) {
console.log(mvid);
CourseApi.getInfo(mvid, function(result) {
console.log(result);
var _contents = result.contents;
console.log(_contents);
if (_contents) {
getMvSubjects(_contents);
} else {
getMvSubjects(self.course.contents);
}
});
}
// }
} else {
getMvSubjects(self.course.contents);
}
$rootScope.lcoursename = self.course.name;
if (deviceDetector.isMobile() && !deviceDetector.isTablet()) {
$rootScope.lcoursename = null;
}
var editors = self.course.editors;
var names = [];
names.push(self.course.creater);
angular.forEach(editors, function(val, idx) {
names.push(val);
});
getEditorInfo(names);
if (loginService.user) {
var username = loginService.user.name;
console.log(username);
console.log(names);
if (names.indexOf(username) > -1) {
self.mycourseFlag = true;
}
}
self.course.contents = result.data.sections;
if (loginService.user && self.course.privacy != 'public') {
getErollExist();
}
if (loginService.user) {
getLatestTopics();
}
// getRelevantCourses();
getEnrolledUsers();
} else {
$state.go('index.404');
}
});
}
function getMvSubjects(contents) {
var _pages = contents;
var _wk = [];
var _row = {};
var mindex = 0;
var sindex = 0;
var tablelist = [];
for (var i = 0; _pages && i < _pages.length; i++) {
var page = _pages[i];
if (!page.level || page.level == 0) {
if (_row.main) {
_wk.push(_row);
}
_row = {};
mindex++;
sindex = 0;
} else {
sindex++;
}
var idx = '' + mindex;
if (sindex > 0) {
idx += '-' + sindex
}
var id = page.id;
var info = {
id: id,
mindex: mindex,
title: page.name,
level: page.level,
idx: idx,
description: page.description,
photo: page.photo,
defaultpage: page.defaultpage,
};
tablelist.push(info);
if (!page.level || page.level == 0) {
var _row = {
main: info,
};
} else {
if (mindex < 1) {
continue;
}
if (!_row.sub) {
_row.sub = [];
}
_row.sub.push(info);
}
}
if (tablelist.length > 0) {
var chapters = [];
var cidx = cidx = tablelist[0].mindex;
var cinfo = new Array();
for (var p = 0; p < tablelist.length; p++) {
if (tablelist[p].mindex != cidx) {
var sinf = {
cidx: cidx,
cinfo: cinfo,
};
chapters.push(sinf);
cidx = tablelist[p].mindex;
cinfo = new Array();
}
cinfo.push(tablelist[p]);
}
var sinf = {
cidx: cidx,
cinfo: cinfo,
};
chapters.push(sinf);
}
if (_row.main) {
_wk.push(_row);
}
$scope.tablelist = tablelist;
$scope.charpters = chapters;
$scope.coursesrow = _wk;
}
function getEditorInfo(names) {
// get teacher info for all editors
UserService.getTeachersInfoByLoginnames(names, function(data) {
var teacherinfos = {};
self.editors = [];
console.log(data);
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);
CourseService.setEditorInfo(self.editors);
})
});
}
getInfo();
this.getPDFUrl = function(file) {
return $sce.trustAsResourceUrl(self.coursePhotoUrl + encodeURI(file));
};
this.getVideoUrl = function() {
return CommonService.getYoutubeLinkfromUrl(self.course.youtube);
// var newId = self.youtube_id_from_url(self.course.youtube);
// return $sce.trustAsResourceUrl("//www.youtube.com/embed/" + newId + '?&controls=1&showinfo=0');
};
/* this.youtube_id_from_url = function(url) {
var id = '';
url = url.replace(/(>|<)/gi, '').split(/(vi\/|v=|\/v\/|youtu.be\/|\/embed\/)/);
if (url[2] !== undefined) {
id = url[2].split(/[^0-9a-z_]/i);
id = id[0];
} else {
id = url;
}
return id;
}*/
this.popupMsg = function() {
if (loginService.user) {
CommonService.showInfo($filter('trans')({
eng: 'If you want to start this course, Please enroll first!',
chn: '如果需要开始课程学习,请首先报名!'
}));
} else {
CommonService.showInfo($filter('trans')({
eng: 'Please login first!',
chn: '请先登录!'
}));
}
};
this.selectItem = function(item) {
$scope.showSec = item;
};
this.delete = function(id) {
CommonService.confirm({
message: 'Are you sure you want to delete it?'
}).then(function() {
var params = {
reply_id: id,
};
CourseApi.post('/reply/' + id + '/delete', params, function() {
getLatestTopics();
CommonService.showNoBlockInfo('Delete Successful!');
// getMvsubjectInfo();
$state.reload();
});
});
};
});