JavaScript output for variables to print filename working locally but not on Moodle...
Jan 06, 2021
Hello Heroes!
After trials, tribulations, heartbreak and pain I finally worked out the Java code to place variables into file names to save/print the result sides. It worked perfectly and I was extremely proud of myself as I am not a programmer.
All was going well until I uploaded to Moodle and tested it on the server...
Instead of my student names and course name in the file name area, all I could/can get was player.pdf....
Here is my code:
var player = GetPlayer();
var first=player.GetVar("FirstName");
var middle=player.GetVar("MiddleName");
var last=player.GetVar("LastName");
var title=player.GetVar("CourseTitle");
document.title=first + " " + middle + " " + last + " " + "-" + " " + title;
window.print();
All the variables have been checked, like a million times, and it does work brilliantly locally.
Any assistance would be most welcome!
I've added on of the modules below.
79 Replies
I just did a test on TalentLMS to see if it works. Well i nicely get a print at the end. I added the Scorm as popup, so not embedded. Not sure how you added it to Moodle.
Thanks for the reply!
It is in a new window...I don't have a popup choice. Did it provide the filename as intended?
Could it be a Chrome thing or a Moodle thing??? Any ideas.....?
Moodle maybe...as i used Chrome...
So, no ideas???
Could someone else help me out, please?
Thank you
Perhaps I was not clear in my first post. I am trying to output variables as filenames for the pdf print from the results page. I have registered a free account at TalentLMS and still the filename is not the variables that I am trying to pass.
Locally, everything works. But it seems that there are some difficulties when you upload this to a server...PLEASE HELP!
I guess thats the difference in local testing and on a server. On the server document.title might not work. So do run console.log( ) tests to check what happens where.
I don't know how to do this...but thanks.
It works fine inside of Articulate Review. I would suspect it is the way content is displayed in the Player element of most LMSs. The content is within an Iframe most of the time so may not be possible to get the course title and that may be breaking things!
I would use the console to check each variable is set correctly within your LMS, if Moodle is intended environment I would also pull name from there
Thanks for the reply!
It's not my code, just trying to copy & paste to get what I need done... I'm not a programmer...
At present, I'm trying to just do everything in pdfmake, but again, this has proven to be very difficult.
Is there any way that you could help me to get the document.title working? It would save me the migraines I have been living through the past few days...
Optionally, could you send me a good example of javascript to change the colour of a variable based on the text value; true or false? For pdfmake, by the way...
here is my feable attempt:
function colorChange() {
if (Q21.innerHTML = "true") {
Q21.style.color = 'blue';
} else {
Q21.style.color = 'red';
}
}
Thanks again!
Go the simple way for that colorchange if your not into Javascript. Use 2 textfield one with color red and one with color blue and use triggers to either show one or the other.
A console log is just a Javascript call to test at any given point in your code.
// add this wherever you want to test something
console.log("my document title is: "+document.title);
Dont forget copying from the forum can break quotes so make sure they are all fine. Best to check code in Sublime Editor or something like that.
Math,
Thanks for your response, but I am trying to change the colour in the HTML file then pdf file from the pdfmake tool.
I would rather use the document.title which would solve my problem, I just can't get my head around the code.
Any help would be most welcome.
Thank you again!
UPDATE: I am trying to use javascript in the pdfmake HTML to change the Q21 text variable to either blue or red depending on its text value of true or false.
If someone could help me out with the conditional statement, that would be fabulous!
This is my code without the background image data:
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<!-- Include pdfmake JS -->
<script src='pdfmake/pdfmake.min.js'></script>
<script src='pdfmake/vfs_fonts.js'></script>
</head>
<body>
<script>
// Check if GetPlayer function exists, if it does, retrieve values of variables.
if (typeof window.parent.GetPlayer === 'function') {
// Retrieve storyline player object from parent window
var player = window.parent.GetPlayer();
// Retrieve the learners name from the learnerName variable
var name = player.GetVar("learnerName");
// Retrieve the teachers name from the teacher variable
var teacher = player.GetVar("teacherName");
// Retrieve the score from the score variable
var score = player.GetVar("score");
// Retrieve the title of the project from projectTitle variable.
var title = player.GetVar("projectTitle");
// Retrieve the title of the project from projectTitle variable.
var Q21 = player.GetVar("v21");
// If GetPlayer function does not exist we are probably testing the certificate outwith storyline. Set variables with dummy values for testing.
} else {
var name = "Joe Bloggs";
var title = "Test Project";
}
// Get todays date in format dd/mm/yyyy
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
date = dd + '/' + mm + '/' + yyyy;
// Define PDF output - See http://pdfmake.org/#/gettingstarted for information on creating a PDF document with pdfmake.
var docDefinition = {
// Set page size
pageSize: 'A4',
// Set document orientation
pageOrientation: 'landscape',
// Set page margins
pageMargins: [0, 0, 0, 0],
// Define main body of document
content: [
// Include learner name
{
text: name,
fontSize: 25,
bold: true,
color: '#006',
alignment: 'left',
margin: [43, 135, 0, 0]
},
{
text: score,
fontSize: 35,
bold: true,
color: '#006',
alignment: 'left',
margin: [245, 85, 0, 0]
},
{
text: Q21,
fontSize: 20,
bold: true,
alignment: 'left',
margin: [500, -220, 0, 0]
},
],
};
function colorChange() {
if (Q21.innerHTML === "true") {
Q21.style.color = 'blue';
} else {
Q21.style.color = 'red';
}
}
// Function to generate and download the PDF using the document definition object (docDefinition) we defined previously.
function generatePDF() {
pdfMake.createPdf(docDefinition).download(title + '.pdf');
}
// Testing - Uncomment to generate PDF with dummy values outwith storyline. This opens PDF in new window.
// pdfMake.createPdf(docDefinition).open();
</script>
</body>
</html>
Or do a state change, I didn't see any use of PDFMake
PDFMake is the solution I am trying to use to overcome the variables in the filename that document.title is not providing me.
I think you can just do simple if else on the text rather than a function, shouldn't have to define true or false in and if/else statement
Thank you Phil!
I'll try now.
Thank you for your help!!!
Breaks the code....
Is this right:
if (Q21) {
text: true,
color: '#ffffff'
} else {
text: false,
color: '#000000'
}
Just to add. These are actual text variables of either "true" or "false" not 0 or 1. I am trying to show the actual text of "true" or "false" with a colour blue or red.
Is it a boolean in storyline? It should report true or false, if it is a string
If it is text try this (works on theory that can only be true or false) This will only change between black and white you need to add hex codes for Blue and red. Did you get any errors in the colsoe?
It causes the pdf not to generate...
It is an articulate true or false variable from a radio button set. It shows "true" or "false" in articulate and also in the generated pdf as variable Q21.
Thank you again for your help!
I haven't used pdfmake but I believe the text should be in quotes.
if (Q21 == "true") {
text: 'true',
color: '#ffffff'
} else {
text: 'false',
color: '#000000'
},
The code for red: '#ff0000'
The code for blue: '#0000ff'
Thanks Russell!
Where do you think it should be placed in the document?
Before, within or after the Content?
Thanks again!
Oops missed the quotes.
It should be in the content where you want it to display.
Good practice is to look at the console for error normally gives the line number.
Sent from my iPhone
It blocks the pdf generation regardless of where I place it and I don't know how to debug it....
Developer tools, JavaScript console and look for errors. It could list be missing comma or one too many braces.
Sent from my iPhone