Code to extract the moodle user icon, given the user id in Moodle 2.x.

Code to extract the moodle user icon, given the user id in Moodle 2.x.

by Frankie Kam -
Number of replies: 3
Picture of Plugin developers

Hi. I'm trying to extract the user's moodle user icon if given the user id. It was fairly easy in Moodle 1.9, but in Moodle 2.x the user pix structure is different. Actually, I can't find the user pix themselves. 

Can anyone help? To be frank (my name's Frankie!), I've created a super-duper wall for Moodle 1.9, and have ported the code over to Moodle 2.2. You can see it here.

http://moodurian.com/m2/mod/page/view.php?id=59
Username: student
Password: student

The Wall works, but the user'sMoodle user icon does not show up. So can someone, anyone, anybody(!?!) tell me how to get the code right?

Here's some attempt by me to extract the user's Moodle icon in Moodle 2.2:

$face="http://www.moodurian.com/user/profile.php/".$USER->id."/f2.jpg";

Nope it doesn't vork, Professor!

//Avatar Image
public function Gravatar($uid)
{
//$query = mysql_query("SELECT email FROM `users` WHERE uid='$uid'") or die(mysql_error());

$query = mysql_query("SELECT email FROM `mdl_m22_user` WHERE id = '$uid'") or die(mysql_error());

$row=mysql_fetch_array($query);
if(!empty($row))
{
...
$data = "../user/profile.php/".$uid."/f2.jpg";
...
return $data;

}
else {
$data="default.jpg";
return $data;
}

Nein. It doesn't vork either, Professor.

Regards 
Frankie Kam

Average of ratings: -
In reply to Frankie Kam

Re: Code to extract the moodle user icon, given the user id in Moodle 2.x.

by Itamar Tzadok -

With $USER, $COURSE (or $PAGE->course) and $OUTPUT try something like

        $params = array(
               'courseid' => $COURSE->id,
               'size' => 100
        );
        $image = $OUTPUT->user_picture($USER, $params);

hth smile

 

In reply to Frankie Kam

Re: Code to extract the moodle user icon, given the user id in Moodle 2.x.

by Rajasekar Selvaraj -

$user = $DB->get_record_sql("select * from mdl_user where id = 2");

echo $OUTPUT->user_picture($user);

 

Use the above code, you will get the image

In reply to Rajasekar Selvaraj

Re: Code to extract the moodle user icon, given the user id in Moodle 2.x.

by Frankie Kam -
Picture of Plugin developers

Hey Itamar and Rajasekar

THANKS TO YOU GUYS, I used:
...
$ustring = "select * from mdl_m22_user where id = ".$uid;
$user = $DB->get_record_sql($ustring);
$face = $OUTPUT->user_picture($user, array('size'=>50));

and here's the result.

Awesomeness!

http://moodurian.com/m2/mod/page/view.php?id=59

Username: student
Password: student

Many thanks once again. Help from guys like you from the Moodle Community helps me to save hours of coding and problem-solving and trouble-shooting. Amazing.