weblib: print_user()/print_user_picture() optimisations

print_user_picture() was forcing an unneeded dbquery
if you need an imagealt. And who doesn't need one these days.

So - teach print_user_picture() to take either $userid
_or_ $userobj as the first parameter. If that first
parameter has the fields we need, never touch the db.

In other words, only touch the DB as a last resort.

There is a bit of ugliness in testing whether we have
the fields or not, because these fields are inconsistently
with/without NOT NULL in the DB definitions. So we cannot
use isset() because it barfs on nulls. And we cannot use empty()
because it will match both on "missing key" and ''.

And while at it, silence warnings that we are missing string
for the year(s). Also fixes a missing string bug in really boring
courses that noone's visitied in many years ;-)
This commit is contained in:
martinlanghoff
2007-09-19 07:19:35 +00:00
parent 1cf2e21b6e
commit aebfa1546e
+50 -7
View File
@@ -3688,7 +3688,10 @@ function print_file_picture($path, $courseid=0, $height='', $width='', $link='',
/**
* Print the specified user's avatar.
*
* @param int $userid ?
* If you pass a $user object that has id, picture, imagealt, firstname, lastname
* you save a DB query.
*
* @param int $user takes a userid, or a userobj
* @param int $courseid ?
* @param boolean $picture Print the user picture?
* @param int $size Size in pixels. Special values are (true/1 = 100px) and (false/0 = 35px) for backward compatability
@@ -3699,14 +3702,46 @@ function print_file_picture($path, $courseid=0, $height='', $width='', $link='',
* return string
* @todo Finish documenting this function
*/
function print_user_picture($userid, $courseid, $picture, $size=0, $return=false, $link=true, $target='', $alttext=true) {
function print_user_picture($user, $courseid, $picture=NULL, $size=0, $return=false, $link=true, $target='', $alttext=true) {
global $CFG;
$needrec = false;
// only touch the DB if we are missing data...
if (is_object($user)) {
// Note - both picture and imagealt _can_ be empty
// what we are trying to see here is if they have been fetched
// from the DB. We should use isset() _except_ that some installs
// have those fields as nullable, and isset() will return false
// on null. The only safe thing is to ask array_key_exists()
// which works on objects. property_exists() isn't quite
// what we want here...
if (! (array_key_exists('picture', $user)
&& ($alttext && array_key_exists('imagealt', $user)
|| (isset($user->firstname) && isset($user->lastname)))) ) {
$needrec = true;
$user = $user->id;
}
} else {
if ($alttext) {
// we need firstname, lastname, imagealt, can't escape...
$needrec = true;
} else {
$userobj = new StdObj; // fake it to save DB traffic
$userobj->id = $user;
$userobj->picture = $picture;
$user = $userobj;
unset($userobj);
}
}
if ($needrec) {
$user = get_record('user','id',$user);
}
if ($link) {
if ($target) {
$target=' target="_blank"';
}
$output = '<a '.$target.' href="'. $CFG->wwwroot .'/user/view.php?id='. $userid .'&amp;course='. $courseid .'">';
$output = '<a '.$target.' href="'. $CFG->wwwroot .'/user/view.php?id='. $user->id .'&amp;course='. $courseid .'">';
} else {
$output = '';
}
@@ -3722,18 +3757,24 @@ function print_user_picture($userid, $courseid, $picture, $size=0, $return=false
$file = 'f2';
}
$class = "userpicture";
if (is_null($picture)) {
$picture = $user->picture;
}
if ($picture) { // Print custom user picture
if ($CFG->slasharguments) { // Use this method if possible for better caching
$src = $CFG->wwwroot .'/user/pix.php/'. $userid .'/'. $file .'.jpg';
$src = $CFG->wwwroot .'/user/pix.php/'. $user->id .'/'. $file .'.jpg';
} else {
$src = $CFG->wwwroot .'/user/pix.php?file=/'. $userid .'/'. $file .'.jpg';
$src = $CFG->wwwroot .'/user/pix.php?file=/'. $user->id .'/'. $file .'.jpg';
}
} else { // Print default user pictures (use theme version if available)
$class .= " defaultuserpic";
$src = "$CFG->pixpath/u/$file.png";
}
$imagealt = '';
if ($alttext and $user = get_record('user','id',$userid)) {
if ($alttext) {
if (!empty($user->imagealt)) {
$imagealt = $user->imagealt;
} else {
@@ -3794,6 +3835,8 @@ function print_user($user, $course, $messageselect=false, $return=false) {
$datestring->mins = get_string('mins');
$datestring->sec = get_string('sec');
$datestring->secs = get_string('secs');
$datestring->year = get_string('year');
$datestring->years = get_string('years');
$countries = get_list_of_countries();
}
@@ -3808,7 +3851,7 @@ function print_user($user, $course, $messageselect=false, $return=false) {
$output .= '<table class="userinfobox">';
$output .= '<tr>';
$output .= '<td class="left side">';
$output .= print_user_picture($user->id, $course->id, $user->picture, true, true);
$output .= print_user_picture($user, $course->id, $user->picture, true, true);
$output .= '</td>';
$output .= '<td class="content">';
$output .= '<div class="username">'.fullname($user, has_capability('moodle/site:viewfullnames', $context)).'</div>';