diff --git a/lib/weblib.php b/lib/weblib.php index b8fd62491d5..c142f8bf949 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -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 = ''; + $output = ''; } 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 .= ''; $output .= ''; $output .= ''; $output .= '
'; - $output .= print_user_picture($user->id, $course->id, $user->picture, true, true); + $output .= print_user_picture($user, $course->id, $user->picture, true, true); $output .= ''; $output .= '
'.fullname($user, has_capability('moodle/site:viewfullnames', $context)).'
';