Skip to content

Commit

Permalink
MDL-21676 user_picture refactoring, reenabling email requirement - to…
Browse files Browse the repository at this point in the history
…wards Gravatar support
  • Loading branch information
skodak committed Jul 4, 2010
1 parent 9092c96 commit 3a11c09
Show file tree
Hide file tree
Showing 24 changed files with 185 additions and 156 deletions.
3 changes: 2 additions & 1 deletion admin/report/configlog/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
$orderby = "cl.$sort $dir";
}

$sql = "SELECT u.id, u.firstname, u.lastname, u.picture, u.imagealt,
$ufields = user_picture::fields('u');
$sql = "SELECT $ufields,
cl.timemodified, cl.plugin, cl.name, cl.value, cl.oldvalue
FROM {config_log} cl
JOIN {user} u ON u.id = cl.userid
Expand Down
6 changes: 3 additions & 3 deletions blocks/messages/block_messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function get_content() {
$action = null; //this was using popup_action() but popping up a fullsize window seems wrong
$this->content->footer = $OUTPUT->action_link($link, get_string('messages', 'message'), $action);

$users = $DB->get_records_sql("SELECT m.useridfrom AS id, COUNT(m.useridfrom) AS count,
u.firstname, u.lastname, u.picture, u.imagealt, u.lastaccess
$ufields = user_picture::fields('u', array('lastaccess'));
$users = $DB->get_records_sql("SELECT $ufields, COUNT(m.useridfrom) AS count
FROM {user} u, {message} m
WHERE m.useridto = ? AND u.id = m.useridfrom
GROUP BY m.useridfrom, u.firstname,u.lastname,u.picture,u.lastaccess,u.imagealt", array($USER->id));
GROUP BY $ufields", array($USER->id));


//Now, we have in users, the list of users to show
Expand Down
2 changes: 1 addition & 1 deletion blocks/online_users/block_online_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function get_content() {
$params['currentgroup'] = $currentgroup;
}

$userfields = user_picture::fields('u').', username';
$userfields = user_picture::fields('u', array('username'));

if ($this->page->course->id == SITEID) { // Site-level
$sql = "SELECT $userfields, MAX(u.lastaccess) AS lastaccess
Expand Down
56 changes: 30 additions & 26 deletions comment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Comment is helper class to add/delete comments anywhere in moodle
*
* @package comment
* @copyright 2010 Dongsheng Cai <[email protected]>
* @copyright 2010 Dongsheng Cai <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand Down Expand Up @@ -107,7 +107,7 @@ public function __construct($options) {
} else {
$this->cid = uniqid();
}

// setup context
if (!empty($options->context)) {
$this->context = $options->context;
Expand All @@ -132,7 +132,7 @@ public function __construct($options) {
if (!empty($options->pluginname)) {
$this->pluginname = $options->pluginname;
}

// setup coursemodule
if (!empty($options->cm)) {
$this->cm = $options->cm;
Expand Down Expand Up @@ -403,34 +403,38 @@ public function get_comments($page = '') {
$this->page = $page;
$params = array();
$start = $page * $CFG->commentsperpage;
$sql = "SELECT c.id, c.userid, c.content, c.format, c.timecreated, u.picture, u.imagealt, u.username, u.firstname, u.lastname
FROM {comments} c, {user} u WHERE u.id=c.userid AND c.contextid=? AND c.commentarea=? AND c.itemid=?
ORDER BY c.timecreated DESC";
$params[] = $this->contextid;
$params[] = $this->commentarea;
$params[] = $this->itemid;
$ufields = user_picture::fields('u');
$sql = "SELECT $ufields, c.id AS cid, c.content AS c.content, c.format AS cformat, c.timecreated AS ctimecreated
FROM {comments} c
JOIN {user} u ON u.id = c.userid
WHERE c.contextid = :contextid AND c.commentarea = :commentarea AND c.itemid = :itemid
ORDER BY c.timecreated DESC";
$params['contextid'] = $this->contextid;
$params['commentarea'] = $this->commentarea;
$params['itemid'] = $this->itemid;

$comments = array();
$candelete = has_capability('moodle/comment:delete', $this->context);
if ($records = $DB->get_records_sql($sql, $params, $start, $CFG->commentsperpage)) {
foreach ($records as &$c) {
$url = $CFG->httpswwwroot.'/user/view.php?id='.$c->userid.'&amp;course='.$this->courseid;
$c->username = '<a href="'.$url.'">'.fullname($c).'</a>';
$c->time = userdate($c->timecreated, get_string('strftimerecent', 'langconfig'));
$user = new stdclass;
$user->id = $c->userid;
$user->picture = $c->picture;
$user->firstname = $c->firstname;
$user->lastname = $c->lastname;
$user->imagealt = $c->imagealt;
$c->content = format_text($c->content, $c->format);
$c->avatar = $OUTPUT->user_picture($user, array('size'=>18));
if (($USER->id == $c->userid) || !empty($candelete)) {
$c->delete = true;
}
$comments[] = $c;
$rs = $DB->get_recordset_sql($sql, $params, $start, $CFG->commentsperpage);
foreach ($records as $u) {
$c = new object();
$c->id = $u->cid;
$c->content = $u->ccontent;
$c->format = $u->cformat;
$c->timecreated = $u->ctimecreated;
$url = $CFG->httpswwwroot.'/user/view.php?id='.$c->id.'&amp;course='.$this->courseid;
$c->username = '<a href="'.$url.'">'.fullname($u).'</a>';
$c->time = userdate($c->timecreated, get_string('strftimerecent', 'langconfig'));
$c->content = format_text($c->content, $c->format);

$c->avatar = $OUTPUT->user_picture($u, array('size'=>18));
if (($USER->id == $u->id) || !empty($candelete)) {
$c->delete = true;
}
$comments[] = $c;
}
$rs->close();

if (!empty($this->plugintype)) {
// moodle module will filter comments
$comments = plugin_callback($this->plugintype, $this->pluginname, FEATURE_COMMENT, 'display', array($comments, $this->args), $comments);
Expand Down
2 changes: 1 addition & 1 deletion enrol/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@
$picture = $OUTPUT->user_picture($user, array('courseid'=>$course->id));

if ($user->lastseen) {
$strlastaccess = format_time(time() - $user->lastaccess);
$strlastaccess = userdate($user->lastseen);
} else {
$strlastaccess = get_string('never');
}
Expand Down
9 changes: 5 additions & 4 deletions grade/report/grader/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,16 @@ public function load_users() {
// the MAX() magic is required in order to please PG
$sort = "MAX(g.finalgrade) $this->sortorder";

$sql = "SELECT u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
$ufields = user_picture::fields('u', array('idnumber'));
$sql = "SELECT $ufields
FROM {user} u
JOIN {role_assignments} ra ON ra.userid = u.id
$this->groupsql
LEFT JOIN {grade_grades} g ON (g.userid = u.id AND g.itemid = :gitemid)
WHERE ra.roleid $usql AND u.deleted = 0
$this->groupwheresql
AND ra.contextid ".get_related_contexts_string($this->context)."
GROUP BY u.id, u.firstname, u.lastname, u.imagealt, u.picture, u.idnumber
GROUP BY $ufields
ORDER BY $sort";

} else {
Expand All @@ -364,8 +365,8 @@ public function load_users() {

$params = array_merge($gbrparams, $this->groupwheresql_params);

$userfields = user_picture::fields('u');
$sql = "SELECT DISTINCT $userfields, u.idnumber
$userfields = user_picture::fields('u', array('idnumber'));
$sql = "SELECT DISTINCT $userfields
FROM {user} u
JOIN {role_assignments} ra ON u.id = ra.userid
$this->groupsql
Expand Down
6 changes: 3 additions & 3 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,7 @@ function print_header($title='', $heading='', $navigation='', $focus='',

$PAGE->set_title($title);
$PAGE->set_heading($heading);
$PAGE->set_cacheable($cache);
$PAGE->set_cacheable($cache);
if ($button == '') {
$button = '&nbsp;';
}
Expand Down Expand Up @@ -2197,7 +2197,7 @@ function print_header_simple($title='', $heading='', $navigation='', $focus='',
}

$PAGE->set_title($title);
$PAGE->set_heading($heading);
$PAGE->set_heading($heading);
$PAGE->set_cacheable(true);
$PAGE->set_button($button);

Expand Down Expand Up @@ -2663,7 +2663,7 @@ function print_file_picture($path, $courseid=0, $height='', $width='', $link='',
*
* @global object
* @global object
* @param mixed $user Should be a $user object with at least fields id, picture, imagealt, firstname, lastname
* @param mixed $user Should be a $user object with at least fields id, picture, imagealt, firstname, lastname, email
* If any of these are missing, or if a userid is passed, the the database is queried. Avoid this
* if at all possible, particularly for reports. It is very bad for performance.
* @param int $courseid The course id. Used when constructing the link to the user's profile.
Expand Down
46 changes: 27 additions & 19 deletions lib/outputcomponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ public function __construct(stdClass $options) {
*/
class user_picture implements renderable {
/**
* List of mandatory fields in user record here.
* @var string
* @var array List of mandatory fields in user record here. (do not include TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE)
*/
const FIELDS = 'id,picture,firstname,lastname,imagealt'; //TODO: add email, but first update all places that call user_picture(), this is necessary for gravatar support
protected static $fields = array('id', 'picture', 'firstname', 'lastname', 'imagealt', 'email');

/**
* @var object $user A user object with at least fields all columns specified in FIELDS constant set.
* @var object $user A user object with at least fields all columns specified in $fields array constant set.
*/
public $user;
/**
Expand Down Expand Up @@ -143,18 +142,13 @@ class user_picture implements renderable {
public function __construct(stdClass $user) {
global $DB;

static $fields = null;
if (is_null($fields)) {
$fields = explode(',', self::FIELDS);
}

if (empty($user->id)) {
throw new coding_exception('User id is required when printing user avatar image.');
}

// only touch the DB if we are missing data and complain loudly...
$needrec = false;
foreach ($fields as $field) {
foreach (self::$fields as $field) {
if (!array_key_exists($field, $user)) {
$needrec = true;
debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. '
Expand All @@ -164,7 +158,7 @@ public function __construct(stdClass $user) {
}

if ($needrec) {
$this->user = $DB->get_record('user', array('id'=>$user->id), self::FIELDS, MUST_EXIST);
$this->user = $DB->get_record('user', array('id'=>$user->id), self::fields(), MUST_EXIST);
} else {
$this->user = clone($user);
}
Expand All @@ -178,19 +172,33 @@ public function __construct(stdClass $user) {
* id of the result record. Please note it has to be converted back to id before rendering.
*
* @param string $tableprefix name of database table prefix in query
* @param array $extrafields extra fields to be included in result (do not include TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE)
* @param string $idalias alias of id field
* @return string
*/
public static function fields($tableprefix = '', $idalias = '') {
if ($tableprefix === '' and $idalias === '') {
return self::FIELDS;
public static function fields($tableprefix = '', array $extrafields = NULL, $idalias = 'id') {
if (!$tableprefix and !$extrafields and !$idalias) {
return implode(',', self::$fields);
}
if ($tableprefix) {
$tableprefix .= '.';
}
$fields = explode(',', self::FIELDS);
foreach ($fields as $key=>$field) {
if ($field === 'id' and $idalias !== '') {
$field = "$field AS $idalias";
$fields = array();
foreach (self::$fields as $field) {
if ($field === 'id' and $idalias and $idalias !== 'id') {
$fields[$field] = "$tableprefix.$field AS $idalias";
} else {
$fields[$field] = $tableprefix.$field;
}
}
// add extra fields if not already there
if ($extrafields) {
foreach ($extrafields as $e) {
if ($e === 'id' or isset($fields[$e])) {
continue;
}
$fields[$e] = $tableprefix.$e;
}
$fields[$key] = "$tableprefix.$field";
}
return implode(',', $fields);
}
Expand Down
Loading

0 comments on commit 3a11c09

Please sign in to comment.