\nI'd like to have the pictures in the HTML block changed automatically on a daily or weekly basis. Can this even be done? If so, would someone please put me on the right track?
\n","author":{"@type":"Person","name":"Tonechas"},"upvoteCount":0,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"I think the key to this question is that you can add javascript to the html block. Thats also why these capabilities are marked with an XSS warning. This is my approach:
\n<img>
tags of the pictures you previously added. Add the following javascript to rotate through these pictures on a weekly basis:<script>\n // Get the week number.\n Date.prototype.getWeek = function() {\n var onejan = new Date(this.getFullYear(), 0, 1);\n return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);\n }\n var numberofweek = (new Date()).getWeek();\n\n var parent = document.currentScript.parentNode;\n var pictures = parent.querySelectorAll('img');\n for (var picturenumber = 0; picturenumber < pictures.length; picturenumber++) {\n if (!(numberofweek % pictures.length == picturenumber)) {\n // Hide all pictures that are not mapped to this week.\n pictures[picturenumber].style.display = 'none';\n }\n }\n</script>\n
\nCons of this approach:
\nPros of this approach:
\nblock_html
comes with moodleTested on Moodle 3.9.x
\nThe pictures will start from the beginning if there are more weeks than available pictures.
Reputation: 13743
I need to display multiple pictures one by one at different time periods in a Moodle course. To that end I'm using an HTML block with one picture at a time (in my first attempt I used a random glossary entry block, which turned out to be an inadequate solution). The HTML block approach is a bit cumbersome as whenever I want to display a different picture I have to manually update the HTML block.
I'd like to have the pictures in the HTML block changed automatically on a daily or weekly basis. Can this even be done? If so, would someone please put me on the right track?
Upvotes: 0
Views: 360
Reputation: 53
I think the key to this question is that you can add javascript to the html block. Thats also why these capabilities are marked with an XSS warning. This is my approach:
<img>
tags of the pictures you previously added. Add the following javascript to rotate through these pictures on a weekly basis:<script>
// Get the week number.
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);
}
var numberofweek = (new Date()).getWeek();
var parent = document.currentScript.parentNode;
var pictures = parent.querySelectorAll('img');
for (var picturenumber = 0; picturenumber < pictures.length; picturenumber++) {
if (!(numberofweek % pictures.length == picturenumber)) {
// Hide all pictures that are not mapped to this week.
pictures[picturenumber].style.display = 'none';
}
}
</script>
Cons of this approach:
Pros of this approach:
block_html
comes with moodleTested on Moodle 3.9.x
The pictures will start from the beginning if there are more weeks than available pictures.
Upvotes: 1