Created
June 1, 2021 11:39
-
-
Save fataelislami/96efefb08aeb0cd4c78dade91381541a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
var cookies_id = '<?php echo $lesson_id; ?>#<?php echo $this->session->userdata('user_id'); ?>'; | |
function setCookie(cname, cvalue, exdays) { | |
var d = new Date(); | |
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); | |
var expires = "expires=" + d.toGMTString(); | |
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; | |
// console.log("Saved to Cookie " + cvalue) | |
} | |
function getCookie(cname) { | |
var name = cname + "="; | |
var decodedCookie = decodeURIComponent(document.cookie); | |
var ca = decodedCookie.split(';'); | |
for (var i = 0; i < ca.length; i++) { | |
var c = ca[i]; | |
while (c.charAt(0) == ' ') { | |
c = c.substring(1); | |
} | |
if (c.indexOf(name) == 0) { | |
return c.substring(name.length, c.length); | |
} | |
} | |
return ""; | |
} | |
// function checkCookie() { | |
// var user=getCookie("username"); | |
// if (user != "") { | |
// alert("Welcome again " + user); | |
// } else { | |
// user = prompt("Please enter your name:",""); | |
// if (user != "" && user != null) { | |
// setCookie("username", user, 30); | |
// } | |
// } | |
// } | |
//saving the current progress and starting from the saved progress | |
var newProgress; | |
var savedProgress; | |
var currentProgress = '<?php echo lesson_progress($lesson_id); ?>'; | |
var lessonType = '<?php echo $lesson_details['lesson_type']; ?>'; | |
var videoProvider = '<?php echo isset($provider) ? $provider : null; ?>'; | |
console.log(lessonType); | |
if (lessonType == 'other') { | |
if (currentProgress != 1) { | |
var lesson_id = '<?php echo $lesson_id; ?>'; | |
$('input#' + lesson_id).prop('checked', true); | |
$.ajax({ | |
type: 'POST', | |
url: '<?php echo site_url('user/save_course_progress'); ?>', | |
data: { | |
lesson_id: lesson_id, | |
progress: 1 | |
}, | |
success: function(response) { | |
currentProgress = response; | |
$('#lesson_list_area').show(); | |
$('#lesson_list_loader').hide(); | |
} | |
}); | |
} | |
} | |
function markThisLessonAsCompleted(lesson_id) { | |
$('#lesson_list_area').hide(); | |
$('#lesson_list_loader').show(); | |
var progress; | |
if ($('input#' + lesson_id).is(':checked')) { | |
progress = 1; | |
} else { | |
progress = 0; | |
} | |
$.ajax({ | |
type: 'POST', | |
url: '<?php echo site_url('user/save_course_progress'); ?>', | |
data: { | |
lesson_id: lesson_id, | |
progress: progress | |
}, | |
success: function(response) { | |
currentProgress = response; | |
$('#lesson_list_area').show(); | |
$('#lesson_list_loader').hide(); | |
} | |
}); | |
} | |
var timer = setInterval(function() { | |
if (lessonType == 'video' && (videoProvider == 'html5' || videoProvider == 'system') && currentProgress != 1) { | |
getCurrentTime(); | |
} | |
}, 1000); | |
$(document).ready(function() { | |
if (lessonType == 'video' && videoProvider == 'html5') { | |
var totalDuration = document.querySelector('#player').duration; | |
if (currentProgress == 1 || currentProgress == totalDuration) { | |
document.querySelector('#player').currentTime = 0; | |
} else { | |
document.querySelector('#player').currentTime = currentProgress; | |
} | |
} | |
}); | |
var counter = 0; | |
player.on('canplay', event => { | |
if (counter == 0) { | |
if (currentProgress == 1) { | |
document.querySelector('#player').currentTime = 0; | |
} else { | |
if (currentProgress != "1") { | |
if (getCookie("savedProgress#"+cookies_id) != '') { | |
currentProgress = getCookie("savedProgress#"+cookies_id); | |
} | |
} | |
document.querySelector('#player').currentTime = currentProgress; | |
} | |
} | |
counter++; | |
}); | |
function getCurrentTime() { | |
var lesson_id = '<?php echo $lesson_id; ?>'; | |
newProgress = document.querySelector('#player').currentTime; | |
var totalDuration = document.querySelector('#player').duration; | |
if (newProgress != savedProgress && newProgress > 0 && currentProgress != 1) { | |
// if the user watches the entire video the lesson will be marked as seen automatically. | |
if (totalDuration == newProgress) { | |
console.log("Tamat") | |
newProgress = 1; | |
$('input#' + lesson_id).prop('checked', true); | |
$.ajax({ | |
type: 'POST', | |
url: '<?php echo site_url('user/save_course_progress'); ?>', | |
data: { | |
lesson_id: lesson_id, | |
progress: newProgress | |
}, | |
success: function(response) { | |
savedProgress = response; | |
} | |
}); | |
} | |
// update the video prgress here. | |
setCookie("savedProgress#"+cookies_id, newProgress, 30); | |
} | |
} | |
</script> | |
<script src="//cdn.jsdelivr.net/npm/sweetalert2@10"></script> | |
<script> | |
var video = document.querySelector('#player') | |
var supposedCurrentTime = 0; | |
if (currentProgress != "1") { | |
if (getCookie("savedProgress#"+cookies_id) != '') { | |
supposedCurrentTime = getCookie("savedProgress#"+cookies_id); | |
} | |
} | |
video.addEventListener('seeking', function() { | |
// guard agains infinite recursion: | |
// user seeks, seeking is fired, currentTime is modified, seeking is fired, current time is modified, .... | |
var delta = video.currentTime - supposedCurrentTime; | |
// if (Math.abs(delta) > 0.01) {//uncomment ini jika ingin disable fast and back | |
if (video.currentTime > supposedCurrentTime) { | |
// console.log("Seeking is disabled"+Math.abs(delta)); | |
// alert("Video tidak dapat dipercepat!"); | |
if (currentProgress != 1) { //kalo progress belum selesai | |
Swal.fire({ | |
title: '', | |
text: 'Video tidak dapat dipercepat!', | |
timer: 1000, | |
icon: "error", | |
showCancelButton: false, | |
showConfirmButton: false, | |
buttons: false, | |
}).then( | |
function() {}, | |
// handling the promise rejection | |
function(dismiss) { | |
if (dismiss === 'timer') { | |
//console.log('I was closed by the timer') | |
} | |
} | |
) | |
video.currentTime = supposedCurrentTime; | |
} | |
} | |
}); | |
// delete the following event handler if rewind is not required | |
video.addEventListener('ended', function() { | |
// reset state in order to allow for rewind | |
supposedCurrentTime = 0; | |
}); | |
var timer = setInterval(function() { | |
supposedCurrentTime = video.currentTime | |
}, 1000); | |
// video.addEventListener('timeupdate', function() { | |
// var totalDuration = video.duration; | |
// var currentTime = video.currentTime | |
// // reset state in order to allow for rewind | |
// console.log("Current "+currentTime) | |
// console.log("Total "+totalDuration) | |
// if(currentTime==totalDuration){ | |
// console.log("FINISH") | |
// } | |
// }); | |
</script> | |
<?php if ($this->session->flashdata('flash_message')) : ?> | |
<script type="text/javascript"> | |
Swal.fire({ | |
title: '', | |
text: 'Anda tidak bisa berpindah ke modul ini sebelum modul sebelumnya selesai!', | |
timer: 2400, | |
icon: "error", | |
showCancelButton: false, | |
showConfirmButton: false, | |
buttons: false, | |
}).then( | |
function() {}, | |
// handling the promise rejection | |
function(dismiss) { | |
if (dismiss === 'timer') { | |
//console.log('I was closed by the timer') | |
} | |
} | |
) | |
</script> | |
<?php endif ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment