2

My purpose is simply being able to retrieve basic user info (id, username, lastname, firstname) in javascript on ANY moodle page WITHOUT having to create a plugin.

I've seen that there is a javascript global variable in Moodle : M but no user info in there.

Is there a way to access user info (id at least) from a logged in user on any page only in javascript another way ?

I've tried the generico filter to create a block i would be able to retrieve information from

<span data-firstname="@@USER:firstname@@" data-lastname="@@USER:lastname@@" data-userid="@@USER:id@@" id="useriddata"></span>

But since i want it on any page, i've tried on the Moodle > Extra HTML textarea. Sadly it doesn't work since the HTML seems not to be filtered there.

Creating a plugin for such a small information seems wrong. What can i do ?

Overdose
  • 585
  • 7
  • 30

1 Answers1

1

You may request user info via Web Services using ajax, e.g. core_user_get_users_by_field. You should, however, determine userid first, which can probably be retrieved from top menu links after signin (profile link etc.). But I would recommend to develop the plugin anyway

  • Is there no default Moodle cookie, no localStorage data having user id ? – Overdose Oct 18 '18 at 14:37
  • There is, but cookies are not 100% reliable – Evgeniy Voevodin Oct 20 '18 at 20:25
  • If a user voluntarily deletes or block the cookies, that's his right, but moodle relies a bit on cookies to provide a better user experience, so in that case, i'm not concerned. If you can point me to an alternate solution, without php ( = without plugin), i'm all for it. For instance, if there was a tag with a data-userid attribute, accessible on ALL pages, it would be ok for me. – Overdose Oct 23 '18 at 08:13