Recently I wanted to show a ‘well done, you’re done here – next steps’ alert box once a student had completed a certain activitie/s. I had wanted to do this with the filtercodes {ifactivitycompleted X} filter, however it didn’t work for a quiz so I had to do it manually.
I share my hideous solution as it may help someone else in making their course more user-friendly, which is my jam.
This is using the Boost theme. You may need to adjust your field you check for depending on your platform.
First, you need to have some divs to show/hide depending on if they’ve done the thing or not. Note the IDs on mine for reference:
<div id="certificate-access" class="alert alert-success">You're knowledge checked! <a href="/mod/customcert/view.php?id=552" target="certificate">Access your proof.</a></div>
<div id="certificate-yet-to-get">
<p dir="ltr" style="text-align: left;">To access your certificate you need to have first passed the knowledge check.</p>
</div>
Second, add your javascript. Doesn’t matter where.
<script type="text/javascript">
/// fitlercodes not picking up quiz completion. This checks for the 'To do:' prompt in the completion box of the activity (quiz in this case) and changes the top-prompt depending on if it's found or not.
$(document).ready(function() {
var referenceActivityID = 540; // the activity ID we want to check completed prior to lniking direct. (find via URL ID param).
$(".activity-item").each(function() {
var t = $(this).find(".stretched-link").attr("href");
if (t.indexOf(referenceActivityID) > -1) { // found; it's our activity
// now we search to eliminate 'to do' text. If none found, it's done.
$(this).find(".automatic-completion-conditions").each(function() {
if ($(this).text().indexOf("To do:") == -1) { // done
console.log('completed the activity', +referenceActivityID);
$("#certificate-access").show(); // the div I want to show
$("#certificate-yet-to-get").hide(); // the 'not done yet' div to not show.
} else { // the opposite settings of above. I forget why I needed this.
$("#certificate-access").hide();
$("#certificate-yet-to-get").show();
}
})
}
});
});
</script>
If you needed to check against multiple activities, pretty easy to put the relevant bit of above in a loop of activityIDs.
If this was handy, let me know – not much call to share code like this these days – I feel like a middle-aged fossil posting this on my personal blog like it’s 2004. But it makes sense when you consider that I am now a middle aged fossil.
Leave a Reply