User profile picture URL in Moodle 2.8

User profile picture URL in Moodle 2.8

by Phineas Head -
Number of replies: 9

What is the URL in Moodle 2.8+ for user X's profile picture, where X is their user #Id number as a GET variable?


https://moodle.org/mod/forum/discuss.php?d=201445 contains useful details but assumes (internal) access to the database (Davo Smith talks about contextid but, sorry Davo, not sure what that is...?). I need this for an external site associated with our Moodle build, so I can display their picture there (obviously my external site db holds their Moodle id).

I need is a solid URL where it is their single userId alone defines the picture to be called. This always used to be yourmoodlesite.com/user/pix.php/X/f1.jpg but this no longer works.

Assuming it is no longer possible to do this, is there an API call I could make, and where would I look to find out how to do this?

Average of ratings: -
In reply to Phineas Head

Re: User profile picture URL in Moodle 2.8

by Conn Warwicker -
Picture of Core developers Picture of Plugin developers

We are using 2.8 and that old format still works fine for me:

moodle.whatever.com/user/pix.php/USERID/f1.jpg

Average of ratings: Useful (1)
In reply to Conn Warwicker

Re: User profile picture URL in Moodle 2.8

by Phineas Head -

OK, but are you 100% certain the userId in the URL is returning the picture of that user? 


For example, my userId is 5. When I go to...

mymoodlesite.com/user/pix.php/5/f1.jpg

...the URL automatically rewrites to...

mymoodlesite.com/pluginfile.php/5/user/icon/f1

...and I get the default grey man image, not me.


Now, if I view source on my profile...

mymoodlesite.com/user/profile.php?id=5

...I notice that my (obviously correct) user profile img src is...

mymoodlesite.com/pluginfile.php/17/user/icon/f1

...i.e. the 5 has been replaced with a 17, and indeed that URL returns the correct image of me when called explicitly. The problem is, 17 is not my userId, 5 is! I take this to be the contextId Davo Smith was talking about, but am still not exactly sure what this is.


Is that the behaviour you observe?

In reply to Phineas Head

Re: User profile picture URL in Moodle 2.8

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Tried testing this:

http://localhost/moodle/user/pix.php/2/f1.jpg redirected to http://localhost/moodle/pluginfile.php/5/user/icon/f1

and displayed the expected image (me). I was using my admin account so the user id =2 was correct.

Another test user also worked where

http://localhost/moodle/user/pix.php/9/f1.jpg redirected to http://localhost/moodle/pluginfile.php/45/user/icon/f1


Average of ratings: Useful (1)
In reply to Richard Oelmann

Re: User profile picture URL in Moodle 2.8

by Phineas Head -

OK, this is weird (and many thanks for testing!)

Running exactly the same experiment with a random (student) userId you're quite right, it works fine for me too (in his case redirecting/rewriting 284 to 731). Even running me, it now seems to work(!) and I am 99% sure I am doing the same thing now, as before.

Either way, when I use the user/pix.php/X/f1.jpg URL as the src of an img in my site (the real purpose of this question), it doesn't work in my site until I open a new browser window, put it into that, have it load and then refresh the old window -- whereupon it works.

That suggests something to do with cacheing I guess. Anyway, thank you very much for answering my question as asked! Now I just need to work out what's happening now!




In reply to Richard Oelmann

Re: User profile picture URL in Moodle 2.8

by Federica Marra -

Hi Richard, how did you get the number 5 for your account id=2

and 45 for account id=9?

I am trying to build a sql report and the table for the user pics is driving me crazy!


Please, take a look at the discussion https://moodle.org/mod/forum/discuss.php?d=318675#p1280180

In reply to Federica Marra

Re: User profile picture URL in Moodle 2.8

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Apologies Frederica, I don't know where they come from other than that's what the automatic redirect generated. I used the url suggested by Conn (the first of each pair in my examples) and the url automatically redirected to the second url in each pair, with that different number as part of the redirected URL.

I presume it is in the database or the file system somewhere, but I'm afraid I don't know where.

In reply to Federica Marra

Re: User profile picture URL in Moodle 2.8

by Conn Warwicker -
Picture of Core developers Picture of Plugin developers

The number in the redirected page is the id of the mdl_context record for that user.

Example:


If I go to my picture on my localhost

http://local/user/pix.php/5/f1.jpg

"5" is the ID of my user.


That looks up the context of the user in the mdl_context table and returns the record:

id: 17

contextlevel: 30

instanceid: 5

path: /1/17

depth: 2


Then it uses that id of the context record "17" to build the URL to the image, using this method:

$url = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', $image);


So it redirects me to:

http://local/pluginfile.php/17/user/icon/f1

Average of ratings: Useful (3)
In reply to Conn Warwicker

Re: User profile picture URL in Moodle 2.8

by Federica Marra -

Hi Conn and Richard,

many thanks for your support and explanation!
In the end I managed to get the pictures in the report with the sql:

SELECT
    -- User.
    u.firstname 'FirstName', u.lastname 'LastName'
   , u.picture, u.id 'UserId'


and in the template:

<img src="http://www.utzacademyonline.com/user/pix.php/userid/f1.jpg">


Special thanks for the sql/template goes to @Matteo Scaramuccia


In reply to Federica Marra

Re: User profile picture URL in Moodle 2.8

by Alex Egea Guirado -

Hello Federica,Conn and Richard

while I'm trying to get a list of students with photography but can not find the way. configurable block proved to report but I missed the SQL statement to remove the photographs since they are encrypted.
You got it? Could you help me to do so.
You need to make lists in Word or Excel with the name and photograph
Thank you,


Alex