use Login Name and Email variables in script on pageload

use Login Name and Email variables in script on pageload

by Stefan Schoch -
Number of replies: 5
Picture of Particularly helpful Moodlers

Since nobody answered to this question in the 'General Forum', I give it another try here:


Hi there,

I am using a Chat-Tool (tawk.to) to be able to chat with my visitors / students.

Currently they have to enter their name manually so I can identify them.

There is an option to pass the Username and Email to the tawk.to script by adding the following script to the 'additional HTML' section:

<script type="text/javascript">
var Tawk_API=Tawk_API||{};
Tawk_API.visitor = {
name : 'visitor name',
email : 'visitor@email.com'
};

var Tawk_LoadStart=new Date();
</script>
This works fine, but now I need to know how I can access the variable 'username' and 'email' that are used by Moodle within that script (instead of visitor name above, because few of my students are called 'visitor name' zwinkernd)

How can I pass these variables to the script?

Stefan

Moodle 3.8.3


Average of ratings: -
In reply to Stefan Schoch

Re: use Login Name and Email variables in script on pageload

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Where is this bit of Javascript going?

It may be possible (in theory) to make an Ajax call to a Moodle webservice but I've not done it - it won't be easy.
In reply to Howard Miller

Re: use Login Name and Email variables in script on pageload

by Stefan Schoch -
Picture of Particularly helpful Moodlers
Hey Howard,

thx for your reply. I don't really understand what you ask by 'where is this javasript going'.

I placed in into the 'additional html' section where it is triggered and does it's job so far: the 'username' used by the tawk.to chatbox is replaced with 'visitor name'.
What I need to do now is call the corresponding Moodle API that gives me back the username and emailadress of the current logged in user so I can put it into the variables.

Ususally (i.e. on my Joomla site) this is no problem at all, I only need to know the script I have to call.

Stefan
In reply to Stefan Schoch

Re: use Login Name and Email variables in script on pageload

by Luis de Vasconcelos -
Perhaps you could get it from the $USER object?
In reply to Luis de Vasconcelos

Re: use Login Name and Email variables in script on pageload

by Stefan Schoch -
Picture of Particularly helpful Moodlers

I assume that is true.

But - how do I get the user object?

I have no idea how to call the API.

Any tips?

In reply to Stefan Schoch

Re: use Login Name and Email variables in script on pageload

by Michael Milette -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Hi Stefan,

If your theme supports filtering in the Additional HTML fields, an easy solution would be to use the FilterCodes plugin. It includes two plain text tags which will be of interest to you: {fullname} and {email}. The easiest way to find out if it will work is to simply install and activate the plugin, and add the tags.

You will find it at https://moodle.org/plugins/filter_filtercodes

If it doesn't work, you can always contact the theme developer and ask them to add support for this. Additional information on what they would need to do is included in the readme file.

Hope you find this useful,

Michael Milette
Average of ratings: Useful (1)