From 274ea09001fdf276dcdcc8754d2bb9ebdb03560d Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Mon, 17 Jan 2011 17:06:56 -0500
Subject: [PATCH] task #1022308 follow-up: Use {users}.name as the shown value on operation handlers for author, committer and attribution.

This also reverts "#1022308 by mikey_p: Temporary fix for global and specific commit logs should show username"
---
 ...control_handler_field_operation_attribution.inc |   41 ++++++++++++-------
 ...rsioncontrol_handler_field_operation_person.inc |   16 ++++---
 includes/views/versioncontrol.views.inc            |    2 +
 3 files changed, 37 insertions(+), 22 deletions(-)

diff --git includes/views/handlers/versioncontrol_handler_field_operation_attribution.inc includes/views/handlers/versioncontrol_handler_field_operation_attribution.inc
index 31ff671..e1aa3d4 100644
--- includes/views/handlers/versioncontrol_handler_field_operation_attribution.inc
+++ includes/views/handlers/versioncontrol_handler_field_operation_attribution.inc
@@ -6,41 +6,52 @@
  * possible. It also shows the committer if it is different.
  */
 class versioncontrol_handler_field_operation_attribution extends versioncontrol_handler_field_operation_person {
-  function construct() {
-    parent::construct();
-    // @TODO After we get joins against both the author_uid and commiter_uid we need to require both here
-    $this->additional_fields['username'] = array('table' => 'users', 'field' => 'name');
-  }
 
-  function query() {
-    $this->ensure_my_table();
-    $this->add_additional_fields();
-  }
+  /**
+   * An array to store {users}.name's keyed by uid for all the results.
+   * This is needed in order to get names for author_uid and committer_uid at the same time.
+   */
+  public $usernames;
 
   function pre_render($values) {
     parent::pre_render($values);
     // Ensure the author alias is set.
     $this->aliases['author'] = $this->field_alias;
+
+    // Get all uids taking care of repeated one.
+    foreach ($values as $result) {
+      $uids[$result->{$this->aliases['author_uid']}] = TRUE;
+      $uids[$result->{$this->aliases['committer_uid']}] = TRUE;
+    }
+    $uids = array_keys($uids);
+
+    $result = db_query('SELECT u.uid, u.name FROM {users} u WHERE u.uid IN ( %s )', implode(', ', $uids));
+    while ($row = db_fetch_object($result)) {
+      $this->usernames[$row->uid] = $row->name;
+    }
   }
 
   function render($values) {
     $author_uid = $values->{$this->aliases['author_uid']};
     $committer_uid = $values->{$this->aliases['committer_uid']};
 
+    // Render the author.
+    $this->aliases['person_uid'] = $this->aliases['author_uid'];
+    $this->field_alias = $this->aliases['author'];
+    $this->aliases['person_username'] = 'author_username';
+    $values->{$this->aliases['person_username']} = $this->usernames[$author_uid];
+    $author = parent::render($values);
+
     // Show only author if they are equal.
     if ($author_uid == $committer_uid) {
-      $this->aliases['person_uid'] = $this->aliases['author_uid'];
-      $this->field_alias = $this->aliases['author'];
-      $author = parent::render($values);
       return t('by !author', array('!author' => $author));
     }
 
     // Show both if they are different.
-    $this->aliases['person_uid'] = $this->aliases['author_uid'];
-    $this->field_alias = $this->aliases['author'];
-    $author = parent::render($values);
     $this->aliases['person_uid'] = $this->aliases['committer_uid'];
     $this->field_alias = $this->aliases['committer'];
+    $this->aliases['person_username'] = 'committer_username';
+    $values->{$this->aliases['person_username']} = $this->usernames[$committer_uid];
     $committer = parent::render($values);
     return t('<span class="authored-by">authored by !author</span>, <span class="committed-by">committed by !committer</span>', array(
       '!author' => $author, '!committer' => $committer));
diff --git includes/views/handlers/versioncontrol_handler_field_operation_person.inc includes/views/handlers/versioncontrol_handler_field_operation_person.inc
index 6c890d0..cf97a46 100644
--- includes/views/handlers/versioncontrol_handler_field_operation_person.inc
+++ includes/views/handlers/versioncontrol_handler_field_operation_person.inc
@@ -14,15 +14,17 @@ class versioncontrol_handler_field_operation_person extends views_handler_field
     else {
       $account = new stdClass();
       $account->uid = $values->{$this->aliases['person_uid']};
-      // @TODO The following is a hack until we can join both author_uid and committer_uid to users.
-      // Only fix the username for author which is the only column joined to {users}.
-      if ($this->field_alias == $this->aliases['author']) {
-        $account->name = $values->{$this->aliases['username']};
+      // Show the value on the field if not mapped.
+      if ($account->uid == 0) {
+        $account->name = $values->{$this->field_alias};
       }
       else {
-        // @TODO Remove this query
-        // This is a temporary hack that we should very rarely need.
-        $account->name = db_result(db_query("SELECT name FROM {users} WHERE uid = %d", $account->uid));
+        if (empty($values->{$this->aliases['person_username']})) {
+          return $values->{$this->field_alias};
+        }
+        else {
+          $account->name = $values->{$this->aliases['person_username']};
+        }
       }
       return theme('username', $account);
     }
diff --git includes/views/versioncontrol.views.inc includes/views/versioncontrol.views.inc
index dda6a0d..9624421 100755
--- includes/views/versioncontrol.views.inc
+++ includes/views/versioncontrol.views.inc
@@ -206,6 +206,7 @@ function versioncontrol_views_data() {
       'click sortable' => TRUE,
       'additional fields' => array(
         'person_uid' => array('table' => 'versioncontrol_operations', 'field' => 'author_uid'),
+        'person_username' => array('table' => 'users', 'field' => 'name'),
       ),
     ),
     'sort' => array(
@@ -259,6 +260,7 @@ function versioncontrol_views_data() {
       'click sortable' => TRUE,
       'additional fields' => array(
         'person_uid' => array('table' => 'versioncontrol_operations', 'field' => 'committer_uid'),
+        'person_username' => array('table' => 'users', 'field' => 'name'),
       ),
     ),
     'sort' => array(
-- 
1.7.2.3

