unserver-unify
Version:
213 lines (207 loc) • 6.38 kB
JavaScript
;
angular.module('bamboo.mystudy').controller('MyCoursesCtrl', function($scope, UserService, ApiService, loginService, CourseApi, CommonService, $state, $anchorScroll, $timeout, $translate) {
var _self = this;
var page = 1;
this.items = [];
var mycourses = [];
this.myTeachingCourses;
this.progress = {};
this.coursePhotoUrl = ApiService.SHOST + '/lcourse/';
this.subjectPicUrl = ApiService.SHOST + "/public/" + ApiService.RES;
this.defaultEventPhoto = this.defaultNewsPhoto = ApiService.SHOST + "/public/images/details.png";
this.defaultPersonPhotoUrl = ApiService.SHOST + "/public/images/user_icon&48.png";
this.defaultpic = ApiService.SHOST + '/public/images/videos.png';
if (loginService.school.coursephoto) {
this.defaultpic = ApiService.SHOST + "/public/" + ApiService.RES + "/school/" + ApiService.gid + "/" + loginService.school.coursephoto;
}
this.pageSize = 6;
this.finished = 0;
this.currentfinished = 0;
this.totalrecords = 0;
this.currentPage = 1;
this.currentCompletedPage = 1;
this.totalcompletedrecords = 0;
this.deleteMode = false;
$scope.setting = {};
var weekago = new Date();
weekago.setDate(weekago.getDate() - 7);
function compareDate(courseDate) {
var cd = new Date(courseDate);
if (cd > weekago) return true;
else return false;
}
var mycourses = [];
var myid;
loginService.getMyProfile(true).then(function(result) {
console.log(result);
myid = result.id;
mycourses = result.courses;
});
this.deleteCourse = function(id, name) {
CommonService.showConfirm($translate.instant("Remove the course from your courses ?"), function() {
var info = {
lid: id,
uid: myid,
action: "deleteenrollcourse",
};
console.log(info);
UserService.UserApi(info, function(result) {
CommonService.showNoBlockInfo($translate.instant('Course was removed successfully!'));
$state.reload();
});
console.log("proceed");
})
}
this.enableDeleteMode = function() {
$timeout(function() {
_self.deleteMode = true;
}, 0);
console.log($scope);
}
_self.toEnrolls=[];
function getMyCourses() {
var info = {
action: "getMyRelaventCourses",
page: _self.currentPage - 1,
limit: _self.pageSize,
};
CourseApi.lcApi(info, function(result) {
console.log(result);
_self.items = result.courses;
angular.forEach(_self.items, function(item, index) {
if (compareDate(item.update)) {
item.newflag = true;
}
var id = item._id;
if (mycourses.indexOf(id) > -1) {
item.myCourseFlag = true;
}else{
_self.toEnrolls.push(item);
item.newflag=true
}
if (_self.progress && _self.progress[id] && _self.progress[id].chapters && _self.progress[id].resources) {
//console.log(_self.progress[id]);
item.progress = parseInt(_self.progress[id].chapters * 100 / _self.progress[id].resources);
if (item.progress == 100) {
_self.currentfinished++;
}
}
})
_self.totalCount = result.count;
});
}
this.upComings=[];
function getUpcomingCourses(){
var info = {
action: "getMyComingCourses",
};
CourseApi.lcApi(info, function(result) {
console.log(result);
_self.upComings=result;
});
}
getUpcomingCourses();
/* function getUpcomingCourses() {
var info = {
action: "getcomingcourses",
};
CourseApi.lcApi(info, function(result) {
console.log(result);
_self.toEnrolls = result;
angular.forEach(_self.items, function(item, index) {
item.newflag = true;
})
});
}
getUpcomingCourses();*/
this.gotocourse = function(id) {
if (mycourses.indexOf(id) < 0) {
var info = {
action: "enroll",
lid: id,
}
console.log(info);
CourseApi.lcApi(info, function(result) {
console.log(result);
console.log("course should be enrolled");
});
}
$state.go('index.courses.course', {
cid: id,
cs: ApiService.getCheckSum(id)
});
}
this.gotoCourseResult=function(id){
if (mycourses.indexOf(id) < 0) {
var info = {
action: "enroll",
lid: id,
}
console.log(info);
CourseApi.lcApi(info, function(result) {
console.log(result);
console.log("course should be enrolled");
});
}
// $state.go('index.courses.course.result', {
$state.go('index.myspace.mycourses.result', {
cid: id,
// cs: ApiService.getCheckSum(id)
});
}
function getMyProgress() {
var info = {
action: "getmyprogress",
}
CourseApi.lcApi(info, function(result) {
console.log(result);
_self.progress = result;
angular.forEach(_self.progress, function(course, key) {
// console.log(course);
if (course.chapters == course.resources) {
_self.finished++;
}
_self.totalrecords++;
});
getMyCourses();
});
}
loginService.getMyProfile().then(function(result) {
mycourses = result.courses;
getMyProgress();
getCompletedCourses();
});
this.pageChanged = function(page) {
console.log(page);
_self.currentPage = page;
getMyProgress();
}
this.pageCompletedChanged = function(page) {
_self.currentCompletedPage = page;
getCompletedCourses();
}
function getCompletedCourses () {
var info = {
start: _self.pageSize * (_self.currentCompletedPage - 1),
limit: _self.pageSize,
action: 'getmycerts'
};
console.log(info);
CertifApi(info, function(data) {
console.log(data);
_self.completedCourses = data.items;
_self.totalcompletedrecords = data.counter;
});
}
function CertifApi(info, callback) {
ApiService.post('/user', info).then(function(result) {
if (result.data.success) {
if (!callback) {
CommonService.showNoBlockSuccess('Update Successful!');
} else {
callback(result.data.data);
}
}
});
}
});