/* global _, angular, i18n, Ladda, Odometer */
'use strict';
angular.module('Client.programs', [])
/* course program */
.directive('courseContent', [function() {
return {
link: function(scope, element, attrs) {
var $btState = $('').addClass('waves-effect waves-light btn z-depth-1 white-text red normal medium');
$btState.text('รายละเอียดเพิ่มเติม');
// element.append($btState);
//
attrs.$observe('courseContent', function(code)
{
// console.log(code+':'+!code.startsWith('2', 5))
if(code) {
// if(!code.startsWith('2', 5)) {
element.append($btState);
element.removeAttr('course-content');
//console.log(element)
if(element.hasClass('disabled')) {
$btState.addClass('disabled').text('ปิดการเรียนชั่วคราว');
}
$btState.addClass('more-detail').on('click', function() {
scope.gotoUrl('/course/'+code);
});
}
});
}
}
}])
.directive('courseState', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var $btState = $('').addClass('waves-effect waves-light btn z-depth-1 white-text red normal medium');
$btState.text('ลงทะเบียน');
// element.append($btState);
attrs.$observe('courseState', function(code)
{
if(code) {
// if(!code.startsWith('2', 5)) {
element.removeAttr('course-state')
// console.log(course)
scope.service.course.state(code).then(function(state) {
// console.log(state)
//console.log(scope.service.user.token())
if(scope.service.user.token()=='') {
state.text = 'ลงทะเบียน';
state.code = 'register';
}
$btState.addClass(state.code).text(state.text).on('click', function() {
element.find('a').addClass('normal regular disabled');
if(state.code=='register') {
scope.gotoUrl('/signin')
//scope.service.program.info().then(function(program) {
//if(program.registration) {
//scope.scrollToTop();
//$location.path('/register/'+program.registration);
//}
//});
} else {
if(state.code=='enrol') {
element.on('click', function() {
element.find('a').addClass('normal regular disabled').text('กำลังโหลด...').unbind("click");
});
}
//scope.service.program.info().then(function(program) {
//console.log(program)
//if(program.registration) {
// scope.gotoPage('/register/'+program.registration);
//} else {
scope.service.course.apply(code, state)
//}
//});
//scope.service.course.apply(code, state).then(function(result) {
//console.log(result)
//});
}
});
// setTimeout(function() {
element.append($btState); //hide enrol button
// }, 300);
//
});
}
});
}
}
}])
.directive('courseLearn', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var $btState = $('').addClass('waves-effect waves-light btn z-depth-1 white-text red normal medium');
$btState.text('เรียน');
element.append($btState);
attrs.$observe('courseLearn', function(code)
{
if(code) {
element.removeAttr('course-learn')
$btState.addClass('continue').on('click', function() {
scope.service.course.learn(code)
});
// scope.service.course.learn(code).then(function(state) {
// //console.log(state)
// //console.log(scope.service.user.token())
// if(scope.service.user.token()=='') {
// state.text = 'ลงทะเบียน';
// state.code = 'register';
// }
// $btState.addClass(state.code).text(state.text).on('click', function() {
// if(state.code=='register') {
// scope.gotoUrl(state.code)
// //scope.service.program.info().then(function(program) {
// //if(program.registration) {
// //scope.scrollToTop();
// //$location.path('/register/'+program.registration);
// //}
// //});
// } else {
// //scope.service.program.info().then(function(program) {
// //console.log(program)
// //if(program.registration) {
// // scope.gotoPage('/register/'+program.registration);
// //} else {
// scope.service.course.apply(code, state)
// //}
// //});
// //scope.service.course.apply(code, state).then(function(result) {
// //console.log(result)
// //});
// }
// })
// });
}
});
}
}
}])
.directive('courseIntro', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
attrs.$observe('courseIntro', function(code)
{
if(code) {
var background = scope.course.poster;
var videoUrl = scope.course.video_url;
var $introduction = $('
').addClass('bold underline-text short').text('แนะนำหลักสูตร');
var $backgroundBlur = $('').addClass('background-blur valign-wrapper');
var $container = $('').addClass('container');
$container.append($introduction);
if(background != '' && videoUrl == '') {
var $bannerIntro = $('
').attr('src', background).attr('width', '100%');
$container.append($bannerIntro);
element.append($backgroundBlur.append($container));
}
else if(videoUrl != '') {
var $video = $('').addClass('responsive-video').attr('controls', 'controls').attr('muted', 'muted').attr('width', '100%');
var $source = $('').attr('type', 'video/mp4');
// poster
if(background) {
$video.attr('poster', background);
}
$source.attr('src', videoUrl);
$video.append($source);
$container.append($video);
element.append($backgroundBlur.append($container));
}
else element.remove();
}
//
});
}
}
}])
.directive('courseInstructor', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
attrs.$observe('courseInstructor', function()
{
if(scope.course.instructors) {
let count_show = 0;
var $container = $('').addClass('container');
var $row = $('').addClass('row grid');
var $title = $('').addClass('bold underline-text short');
if(scope.course.title_text.instruction) {
$container.append($title.text(scope.course.title_text.instruction))
}
if(scope.course.instructors[1]) {
angular.forEach(scope.course.instructors, function(instructor, key) {
if(instructor.name.photo) {
var $col = $('
').addClass('col s3');
var $gradient = $('').css({position:'absolute',top:0,width:'100%',height:'100%'}).addClass('Black');
var $photo = $('').addClass('shadow gradient photo');
$photo.css('background-color', '#666666');
$photo.css('background-image', "url('"+instructor.image+"')");
$photo.css('padding-top', 670 / 400 * 100+'%');
$photo.css('position','relative');
var $instructor = $('').addClass('bottom wrapper center white-text');
var $fullname = $('').addClass('name');
$fullname.html(instructor.name.name + '
' + instructor.name.lastname);
$instructor.append($fullname);
if(instructor.title) {
var $divPosition = $('');
// var $hr = $('
').addClass('thick short').css('background-color', '#EE7115');
var $hr = $('
').addClass('thick short').css('background-color', 'red');
var $position = $('').addClass('brief').html(instructor.title);
$divPosition.append($hr).append($position);
$instructor.append($divPosition);
}
$photo.append($gradient).append($instructor);
$col.append($photo);
$container.append($row.append($col));
count_show++;
}
});
if(count_show==0) {
element.remove();
}
}else {
element.remove();
}
element.append($container);
}
});
}
}
}])
.directive('courseOutline', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
attrs.$observe('courseOutline', function(total)
{
console.log(total)
console.log(scope.course)
if(total) {
var $container = $('').addClass('container');
var $row = $('').addClass('row');
var $title = $('').addClass('head-text underline-text')
if(scope.course.title_text.outline) {
$container.append($title.text(scope.course.title_text.outline))
}
//
if(scope.course.outlines.length) {
angular.forEach(scope.course.outlines, function(outline, key) {
var $outline = $('
').addClass('outline col s12 m4 l4');
var $title = $('').addClass('title dbadman bold relative');
var $chapter = $('').addClass('outdent right-align').text(outline.item);
var $brief = $('').addClass('brief normal light').text(outline.brief);
//
$title.append($chapter).append($('').text(outline.title));
$outline.append($title).append($brief);
$container.append($row.append($outline));
if(scope.course.type=='live') {
$outline.append($('').addClass('date-calendar normal medium').text(outline.type.textdate))
$outline.append($('').addClass('time normal medium').text(outline.type.time))
$outline.append($('
').attr('src', outline.type.icon).css('margin-top', '1em'))
}
});
}else {
element.remove();
}
//
element.css('background-image', 'url('+scope.course.bg_outline+')').addClass('cover');
element.append($container);
}
});
}
}
}])
.directive('courseType', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs)
{
attrs.$observe('courseType', function(type)
{
if(type) {
var image = '//services.promptskill.com/app/public/img/icon/icon-'+type+'.png';
var $img = $('
').css('width', '8.5em');
$img.css('margin-top', '2em');
var img = new Image();
img.onload = function() {
$img.attr('src', image);
element.append($img);
}
img.src = 'https:' + image;
}
});
}
}
}])
.directive('courseActivity', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs)
{
attrs.$observe('courseActivity', function (code) {
if(code) {
scope.service.learner.lrsConnect(code).then(function(result) {
// console.log(result)
scope.$parent.lrs = result.lrs;
scope.$parent.lrs['id'] = result.lrsid;
//console.log(scope.lrs)
result.sess.then(function(sess) {
// console.log(sess.data)
scope.$parent.lrs.token = sess.data.token;
scope.activity = scope.course.activity;
})
scope.outline = scope.course.outline;
});
}
});
}
}
}])
.directive('courseNavigator', ['$location','$timeout', function($location, $timeout) {
return {
restrict: 'A',
templateUrl: '/templates/blocks/course-navigator.dbdacademy',
link: function(scope, element, attrs) {
//
var navigator = '';
//
var nav = 0;
var navi = [];
var path = $location.path();
let next_activity_state = true;
//
attrs.$observe('courseNavigator', function(data) {
if(data !='') {
element.removeAttr('course-navigator');
scope.service.course.navigator(angular.fromJson(data)).then(function(course) {
scope.navigator = course;
console.log(scope.navigator);
navigator = course.control;
})
}
});
// call function
scope.prevActivity = function() {
$location.hash(null);
//
scope.scrollToTop();
if(navigator.prev.activity =='') $location.path('/course/'+scope.navigator.code);
$location.path('/course/'+scope.navigator.code+'/'+navigator.prev.activity);
}
scope.nextActivity = function() {
//
$location.hash(null);
console.log(navigator)
if(navigator.next.activity !='') {
//scope.scrollToTop();
//if(navigator.next.activity=='next') {
//console.log($location.path().split('/')[4])
if(next_activity_state) {
next_activity_state = false;
scope.service.learner.completion(scope.navigator.code, $location.path().split('/')[4]).then(function(activity) {
console.log(navigator.next.activity);
console.log(activity)
if(activity !='alert') {
if(navigator.next.activity !='next') {
if(navigator.next.activity =='certificate') {
//scope.service.learner.certificate(scope.navigator.code);
scope.service.course.completion(scope.navigator.code).then(function(status) {
console.log(status)
//console.log(scope.service.user.certificate());
//scope.service.learner.certificate(status.course);
//scope.scrollToTop();
if(navigator.next.activity =='certificate') {
scope.scrollToTop();
$location.path('course/'+scope.navigator.code+'/certificate');
} else {
$location.path(activity);
}
//scope.$parent.alert = {"msg":'ระบบออกใบประกาศ'};
});
} else {
$location.path(navigator.next.activity);
console.log('errorrrrrrrrr')
}
} else {
scope.scrollToTop();
$location.path('/course/'+scope.navigator.code+'/'+activity);
}
} else {
scope.$parent.alert = {"msg":'กรุณาเรียนให้จบหรือตอบคำถามให้ครบ
ก่อนเรียนบทต่อไป'};
next_activity_state = true;
}
});
}
//} else {
// if(navigator.next.activity=='certificate') {
// scope.scrollToTop();
// scope.certificate = {"course":scope.navigator.code};
//$location.path('/certificate');
//else $location.path('/course/'+scope.navigator.code+'/'+navigator.next.activity);
// }
//}
} else {
scope.$parent.alert = {"msg":'กรุณาเรียนให้จบหรือตอบคำถามให้ครบ
ก่อนเรียนบทต่อไป'};
next_activity_state = true;
//console.log(scope.alert)
}
// console.log(next_activity_state);
}
scope.showPanel = function() {
//console.log('show panel');
var $panel = angular.element('#course-navigator .panel')
var $coursePanel = angular.element('#course-navigator .course-panel');
//
if ($coursePanel.hasClass('show')) {
$coursePanel.removeClass('show')
//$coursePanel.attr('style', null);
//$coursePanel.children('.message-wrapper').remove();
//$coursePanel.children('.timeline-wrapper').show();
} else {
scope.$parent.alert = {"msg":""};
//var fontSize = parseFloat($coursePanel.css('font-size'));
$coursePanel.addClass('show');
$timeout(function() {
$coursePanel.removeClass('show')
}, 7000);
}
}
scope.gotoActivity = function(outline, activity) {
if(activity!=undefined) {
$location.hash(null);
scope.scrollToTop().then(function() {
$location.path('/course/'+scope.navigator.code+'/'+outline+'/'+activity);
});
}
}
}
}
}])
.directive('toggle', [function() {
return {
restrict: 'C',
link: function(scope, element, attr)
{
element.on('click', function() {
//console.log(element)
if (element.hasClass('current')) {
element.removeClass('current')
//element.addClass('done')
} else {
$(element).parent().find('.toggle.current').removeClass('current');
element.addClass('current')
}
});
}
}
}])
/* learner */
.directive('courseProgress', [function() {
return {
restrict: 'A',
templateUrl: 'templates/blocks/course-progress.dbdacademy',
link: function(scope, element, attrs)
{
//console.log(attrs.scale)
attrs.$observe('courseProgress', function(progress) {
//scope.service.user.courseProgress(code).then(function(data) {
scope.scale = attrs.scale;
scope.progress = progress;
//scope.scored = data.scored;
//});
});
}
}
}])
.directive('courseCertificate', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs)
{
//var code = scope.service.user.certificate();
//var course = {code: code};
//scope.service.program.info().then(function(program) {
//course['course_id'] = program.program_id;
//scope.service.course.content(course).then(function(content) {
//console.log(content)
// scope.$parent.course = content;
//console.log(scope.course)
//});
//});
//console.log(scope.course.code) //
scope.service.course.certificate(scope.course.code).then(function(cert) {
//console.log(cert)
scope.code = cert.code;
scope.learner = cert.learner;
//scope.learner['reference'] = false;
if(cert.reference) {
//scope.learner['reference'] = true;
scope.cardShow = true;
scope.cert = cert.reference;
} else {
scope.cert = cert.token;
}
//console.log(scope.learner)
if(cert.learner) {
$('.card-background').attr('src', cert.learner.template);
}
})
}
}
}])
.directive('embedCert', ['$timeout', function($timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs)
{
var $iframe = $('').css({width:'100%',height:'100%',position:'absolute',border:'none',top:0, left:0});
attrs.$observe('embedCert', function(certcode)
{
if(certcode) {
element.empty();
element.removeAttr('embed-cert');
var width = 60;
var height = (11.25 / 16) * width;
element.css('padding-bottom', height + '%');
element.css('width', width + '%');
element.css('margin', '4em auto');
element.css('position', 'relative');
element.addClass('onload');
//
var course = scope.course.code;
var referenceCode = '';
//var download = scope.service.member.center('certificate')+'/download/';
scope.service.course.certificated(course).then(function(reference) {
//console.log(reference)
if(reference) {
$iframe.attr('src', scope.service.member.center('certificate')+'/viewed/'+reference);
//download = download + reference;
} else {
//console.log(certcode);
scope.service.user.certificated(certcode).then(function(certificated) {
//console.log(certificated);
scope.service.course.certificated(course, certificated);
reference = certificated.reference;
//download = download + reference;
$iframe.attr('src', scope.service.member.center('certificate')+'/viewed/'+reference);
});
}
$timeout(function(){
referenceCode = reference;
},900);
//console.log(download)
/* $('').addClass('btn red').attr('href',download)
.attr('target','_blank').text('Download').appendTo(element.parent()); */
});
/*
scope.service.user.certificated(certcode).then(function(certificated) {
//console.log(certificated)
var course = scope.service.user.certificate();
scope.service.course.certificated(course, certificated);
});
*/
element.append($iframe);
//
scope.printCert = function() {
var iframe = document.getElementById('certificate').contentWindow;
iframe.focus();
iframe.print();
}
scope.downloadCert = function() {
// console.log('cert')
// console.log(referenceCode)
window.location = scope.service.member.center('certificate')+'/download/'+referenceCode
}
}
});
}
}
}])
/* LRS */
.directive('activityEmbed', ['$window','$location', function($window, $location) {
return {
restrict: 'A',
link: function(scope, element, attrs)
{
attrs.$observe('activityEmbed', function(module)
{
if(module !='') {
var $script = $('').attr('src', scope.lrs.url+'/mod/hvp/library/js/h5p-resizer.js');
var $iframe = $('').css({width:'100%',height:'100%',border:'none',top:0});
var src = scope.lrs.url+'/mod/'+scope.activity.module+'/embed.php?id='+module;
var height = scope.activity.height;
// $iframe.attr('scrolling', 'yes');
//console.log(element.width()+':'+height)
// console.log(scope.navigator)
// console.log($location.path()+':'+$location.path().split('/')[4])
scope.service.learner.completed($location.path().split('/')[2], $location.path().split('/')[4])
.then(function(activity) {
// console.log(activity)
// var navigator = activity.data;
console.log(scope.navigator)
//
if(scope.activity.module_type =='final_test') {
console.log(activity)
if(activity.activity && $location.hash()!='retry' || activity.remain ==0) {
scope.retry = true && activity.remain >0 && activity.result==false;
src = '/resource/test-result/embed.php#learnerid='+activity.navigator.learnerid+'&course='+activity.navigator.code+'&token='+btoa(angular.toJson(activity));
} else {
src = scope.lrs.url+'/mod/'+scope.activity.module+'/embed.php?id='+activity.cmid;
}
//$location.hash(null)
//console.log(src)
//$iframe.attr('src', src);
}
if(scope.activity.module=='questionnaire') {
src = '/resource/survey-form/embed.php#learnerid='+activity.learnerid+'&course='+activity.course+'&token='+scope.service.user.token();
if(scope.mobile) {
$iframe.css('min-height', parseInt(element.width() * height * 4 / 100)+'px');
}
}
// element.css('padding-bottom', height+'%');
element.css('position', 'relative');
if(!scope.mobile) {
$iframe.css('min-height', parseInt(element.width() * height / 100)+'px');
}
//
if(scope.activity.module_type =='quiz') {
$iframe.css('padding', '1em 2rem');
}
if(scope.activity.module_type =='final_test') {
$iframe.css('padding', '1em 2rem');
}
//
element.append($iframe);
$iframe.attr('src', src);
element.css('background-color', '#fff');
// retry button
if(scope.retry) {
var $retry = $('').addClass('waves-effect btn btn-requiz z-depth-0 retry');
if(activity.activity.length==0) {
$retry.text('เริ่มทำแบบทดสอบ');
} else {
$retry.text('ทำแบบทดสอบใหม่');
}
$retry.on('click', function() {
//$location.hash('retry')
//console.log($location.path())
location.href = $location.path()+'#retry';
//scope.retry = false;
})
element.append($retry);
}
//$location.hash(null)
element.ready(function() {
element.append($script);
});
});
/*
if(scope.activity.module=='questionnaire') {
src = '/resource/survey-form/embed.php#learnerid='+scope.navigator.learnerid+'&course='+scope.navigator.code+'&token='+scope.service.user.token();
}
element.css('padding-bottom', height+'%');
//element.css('min-height', "16em");
//console.log(height);
//element.css('padding-top', "3em");
//element.css("background-color", "#ffffff");
element.css('position', 'relative');
//
//console.log(scope.lrs.token)
//console.log(src + '?id='+module + '&secret='+scope.lrs.token + '&userid='+scope.lrs.id)
if(scope.activity.module_type =='quiz') {
$iframe.css('padding', '1em 2rem');
}
//
element.append($iframe);
$iframe.attr('src', src);
element.css('background-color', '#fff');
//console.log($('#moobed'))
//$iframe.onload = function() {
//console.log(script)
//element.append($script);
//}
//element.append($script); */
}
});
}
}
}])
/* member */
.directive('member', ['$window','$location', function($window, $location) {
return {
restrict: 'A',
link: function(scope, element, attrs)
{
scope.$parent.headbar = false;
scope.$parent.fullpage = true;
scope.$parent.member = false;
//
attrs.$observe('member', function(action) {
scope.service.program.page('member').then(function(data) {
scope.page = data;
});
scope.service.member.authorized().then(function(authorized) {
console.log(authorized)
if(action=='reset-password') {
if(!authorized) $location.path('/signin');
//scope.permission = authorized.user;
$('input[name="email"]').val(authorized.email);
}
if(action=='change-info') {
if(!authorized) $location.path('/signin');
//console.log(authorized)
scope['user'] = authorized.user;
}
});
});
// member call function
scope.formInput = function() {
var valid = true;
//var $forminput = angular.element('.form-input').find('input');
angular.forEach(angular.element('.form-input').find('input'), function(input) {
//console.log(input)
if(angular.element(input).val() =='') {
angular.element(input).addClass('invalid');
valid = false;
}
});
return valid;
}
scope.getEmail = function() {
var id_card = $('input#verify_id_card').val();
var passwd = $('input#verify_password').val();
if(id_card && passwd) {
scope.service.member.getemail(id_card, passwd).then(function(email) {
//console.log(email)
if(email) {
$('input#verify').val(email)
} else {
scope.helper = {'msg':'ไม่พบบัญชีสมาชิกของท่าน'};
}
});
}
}
scope.copy = function(e) {
// copy element
var copyElement = document.getElementById("verify");
//console.log($(copyElement).val())
//copyElement.setSelectionRange(0, copyElement.selectionEnd);
if($(copyElement).val().length) {
// select the text
copyElement.select();
copyElement.setSelectionRange(0, copyElement.selectionEnd);
// copy
document.execCommand('copy');
//console.log($(e.currentTarget))
$(e.currentTarget).text('COPIED');
}
}
scope.signin = function() {
if(scope.formInput()) {
scope.service.member.signin(angular.element('.form-input').find('input')).then(function(data) {
// console.log(data)
if(data.err==undefined) {
$window.location = 'https://'+scope.service.program.domain()+'#'+data.token;
} else {
scope.helper = data;
//console.log(scope.helper)
}
});
}
}
scope.forgotPassword = function() {
if(scope.formInput()) {
var email = angular.element('.form-input').find('input[name="email"]').val();
//console.log(email)
scope.service.member.password(email).then(function(data) {
console.log(data)
if(data.type=='success') $location.path('/');
scope.helper = data;
});
}
}
scope.resetPassword = function() {
if(scope.formInput()) {
var passwd = angular.element('.form-input').find('input[name="new_password"]').val();
if(angular.element('.form-input').find('input[name="confirm_password"]').val()==passwd) {
var email = angular.element('.form-input').find('input[name="email"]').val();
scope.service.member.resetpassword(email, passwd).then(function(data) {
console.log(data)
if(data.type=='success') {
scope.helper = data;
scope.service.member.clear();
}
//scope.helper['redirect'] = 'signin';
});
} else {
scope.helper = {'msg':'รหัสผ่านยืนยันไม่ถูกต้อง'}
}
}
}
}
};
}])
.directive('uniqueValid', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs)
{
var $helper = element.next().next();
var text = $helper.attr('data-error');
//console.log($helper.attr('data-error'))
element.on('blur', function() {
//console.log($helper);
if($(this).val() && element.hasClass('valid')) {
scope.service.member.valid($(this).val()).then(function(data) {
console.log(data)
if(data) {
console.log(data.msg)
$helper.attr('data-error', data.msg);
element.removeClass('valid').addClass('invalid');
}
});
}
});
//
element.on('keydown', function() {
$helper.attr('data-error', text);
element.removeClass('invalid').addClass('valid');
});
}
}
}])