Skip to content

Commit

Permalink
added strict mode to multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhunt committed Aug 9, 2022
1 parent 22543ba commit f7d4ccb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Change List
=========
Version 3.1.59 (Build 202208XXXX)
- Added strict multiplayer

Version 3.1.58 (Build 2022060500)
- Added better free trial link
- Rebuilt JS for < M.8 versions
Expand Down
2 changes: 1 addition & 1 deletion presets/pw-multiplayeraudio.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"name":"MultiPlayerAudio-Widget","key":"pw-multiplayeraudio","version":"1.0.1","instructions":"This is an HTML5 audio player that will only play the audio a preconfigured number of times. Insert an audio player or audio link between the tags.","showatto":"1","showplayers":"0","requirecss":"","requirejs":"","shim":"","defaults":"canplaycount=5","amd":"1","body":"<div id='@@AUTOID@@_container'></div>\n<div id=\"@@AUTOID@@_original\" style=\"display: none\">\n@@canplaycount@@","bodyend":"</div>","script":"@@canplaycount@@\ntemplates.render('filter_poodll/pw-multiplayeraudio',opts).then(function(html, js) {\n\n // The templates object has append, prepend and replace functions.\n templates.appendNodeContents('#' + @@AUTOID@@ + '_container', html, js);\n \n }).fail(function(ex) {\n // Deal with this exception (I recommend core/notify exception function for this).\n\n });","style":"","dataset":"","datasetvars":"","alternate":"","alternateend":""}
{"name":"MultiPlayerAudio-Widget","key":"pw-multiplayeraudio","version":"1.0.1","instructions":"This is an HTML5 audio player that will only play the audio a preconfigured number of times. Insert an audio player or audio link between the tags.","showatto":"1","showplayers":"0","requirecss":"","requirejs":"","shim":"","defaults":"canplaycount=2,quizstrictmode=false|true","amd":"1","body":"<div id='@@AUTOID@@_container'></div>\n<div id=\"@@AUTOID@@_original\" style=\"display: none\">\n@@canplaycount@@ \n@@quizstrictmode@@ \n@@URLPARAM:attempt@@\n@@URLPARAM:cmid@@","bodyend":"</div>","script":"templates.render('filter_poodll/pw-multiplayeraudio',opts).then(function(html, js) {\n\n // The templates object has append, prepend and replace functions.\n templates.appendNodeContents('#' + @@AUTOID@@ + '_container', html, js);\n \n }).fail(function(ex) {\n // Deal with this exception (I recommend core/notify exception function for this).\n\n });","style":"","dataset":"","datasetvars":"","alternate":"","alternateend":""}
62 changes: 50 additions & 12 deletions templates/pw-multiplayeraudio.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,44 @@ $('#{{AUTOID}}').data('playtime', {{canplaycount}});
var aplayer = $('#' + "{{AUTOID}}").children().first();
var fa = $('#' + "{{AUTOID}}" + ' .fa');
var thestate =$('#' + "{{AUTOID}}" + '_state');
var thetime = $('#' + "{{AUTOID}}" + '_time');
var thetime = $('#' + "{{AUTOID}}" + '_time');
var playtimer = $('#' + "{{AUTOID}}");


var details = parser.parse('{{AUTOID}}_original', 'audio');
aplayer.attr('src',details.mediaurl); // mediaurl, subtitlesurl, sources

$(fa).removeClass('fa-minus');
$(fa).removeClass('fa-minus');

//set status
thestate.text('- ready -');


// setting base volume for audio player
aplayer[0].volume = 0.2;

//set status
thestate.text('- ready -');
//cookies if in strict mode
var cookiename = details.mediaurl + '_' + "{{URLPARAM:attempt}}"+ '_' + "{{URLPARAM:cmid}}";
console.log(cookiename);
console.log(getCookie(cookiename));
if("{{quizstrictmode}}"=="true" && "{{URLPARAM:attempt}}" !=""){
var prevplaycount = getCookie(cookiename);
if(prevplaycount!==null){
playtimer.data('playtime',prevplaycount);
if(prevplaycount==="0"){
disableSubsequentPlays();
}
}
}


// setting base volume for audio player
aplayer[0].volume = 0.2;



// Append Playtime text
$('#' + "{{AUTOID}}" + '_playtime').empty();
var playtimer = $('#' + "{{AUTOID}}").data('playtime');
$('#' + "{{AUTOID}}" + '_playtime').append('Remaining plays: '+playtimer +'x');
var remainingplays = playtimer.data('playtime');
$('#' + "{{AUTOID}}" + '_playtime').append('Remaining plays: '+remainingplays +'x');



Expand All @@ -67,10 +85,14 @@ $('#' + "{{AUTOID}}" + '_vol-down').click(function(){
});

aplayer[0].addEventListener('ended', function(){
var playtimer = $('#' + "{{AUTOID}}");
$('#' + "{{AUTOID}}" + '_playtime').empty();
var currentplaycount = playtimer.data('playtime');
currentplaycount--;
//if cookies are enabled set the play count in the cookie
if("{{quizstrictmode}}"=="true" && "{{URLPARAM:attempt}}" !="" ){
setCookie(cookiename,currentplaycount,1);
}

playtimer.data('playtime',currentplaycount );

$(fa).removeClass('fa-circle-o-notch');
Expand All @@ -83,9 +105,7 @@ $('#' + "{{AUTOID}}" + '_vol-down').click(function(){
$('#' + "{{AUTOID}}" + '_playtime').append('Remaining plays: '+currentplaycount+'x');
$(fa).addClass('fa-play');
}else{
$('#' + "{{AUTOID}}" + '_playtime').append('Remaining plays: 0');
$(fa).addClass('fa-minus');
$('#' + "{{AUTOID}}").data("disabled", true);
disableSubsequentPlays();
}//end of if currentplaycount > 0

});//end of ended listener
Expand All @@ -98,6 +118,23 @@ $('#' + "{{AUTOID}}" + '_vol-down').click(function(){
play_audio();
});//end of click event

function disableSubsequentPlays(){
$('#' + "{{AUTOID}}" + '_playtime').append('Remaining plays: 0');
$(fa).addClass('fa-minus');
$('#' + "{{AUTOID}}").data("disabled", true);
}

function setCookie(key, value, expiry) {
var expires = new Date();
expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}

function getCookie(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
}


function play_audio(){
aplayer[0].play();
Expand All @@ -113,6 +150,7 @@ aplayer.bind('durationchange',function(){thetime.text(poodll_mp_fetchtime(aplaye

//time display function
function poodll_mp_sec2time(seconds){
if(seconds=='Infinity'){return "00:00:00";}
var date = new Date(null);
date.setSeconds(seconds);
return date.toISOString().substr(11, 8);
Expand Down

0 comments on commit f7d4ccb

Please sign in to comment.