From 1362d1fe741174d4327073815f6f02daf572e74b Mon Sep 17 00:00:00 2001
From: Marco Villegas <git@marvil07.net>
Date: Mon, 20 May 2013 22:03:25 -0700
Subject: [PATCH] Issue #1998000: Ported tablesort_sql use to D7.

---
 versioncontrol_project.module |   49 ++++++++++++++++++++++++++++++++---------
 1 file changed, 39 insertions(+), 10 deletions(-)

diff --git a/versioncontrol_project.module b/versioncontrol_project.module
index 36e6863..db4f1db 100644
--- a/versioncontrol_project.module
+++ b/versioncontrol_project.module
@@ -321,9 +321,9 @@ function versioncontrol_project_views_api() {
  *
  * @param $nid
  *   Project node ID to get committers for.
- * @param $sort
- *   Optional ORDER BY clause to use for the list of committers. Defaults to
- *   sorting by the most recent commit.
+ * @param $header_with_order
+ *   The value used by TableSort::orderByHeader() in the query or NULL to use
+ *   the default.
  * @param $only_maintainers
  *   Optional boolean to indicate if the list should only include users that
  *   currently have VCS write access to the project. Defaults to TRUE.
@@ -331,14 +331,43 @@ function versioncontrol_project_views_api() {
  * @return
  *   Array containing objects of committer information, keyed by uid.
  */
-function versioncontrol_project_get_project_committers($nid, $order = '', $only_maintainers = TRUE) {
-  if (empty($order)) {
-    $order = ' ORDER BY last_commit DESC';
+function versioncontrol_project_get_project_committers($nid, $header_with_order = NULL, $only_maintainers = TRUE) {
+  if (is_null($header_with_order)) {
+    $header_with_order = array(
+      array(
+        'data' => t('User'),
+        'field' => 'u.name',
+      ),
+      array(
+        'data' => t('Last commit'),
+        'field' => 'last_commit',
+        'sort' => 'desc',
+      ),
+      array(
+        'data' => t('First commit'),
+        'field' => 'first_commit',
+      ),
+      array(
+        'data' => t('Commits'),
+        'field' => 'commits',
+      ),
+    );
   }
   $repo = versioncontrol_project_repository_load($nid);
-  $query = "SELECT o.author_uid as uid, MIN(o.author_date) AS first_commit, MAX(o.author_date) AS last_commit, COUNT(o.vc_op_id) AS commits, u.name FROM {versioncontrol_operations} o INNER JOIN {users} u ON o.author_uid = u.uid WHERE o.repo_id = :repo_id AND o.author_uid != 0 GROUP BY o.repo_id, o.author_uid $order";
-  // TODO Please convert this statement to the D7 database API syntax.
-  $result = db_query($query, array(':repo_id' => $repo->repo_id));
+  $query = db_select('versioncontrol_operations', 'o')->extend('TableSort');
+  $query->join('users', 'u', 'o.author_uid = u.uid');
+  $query->addExpression('MIN(o.author_date)', 'first_commit');
+  $query->addExpression('MAX(o.author_date)', 'last_commit');
+  $query->addExpression('COUNT(o.vc_op_id)', 'commits');
+  $query->addField('o', 'author_uid', 'uid');
+  $result = $query
+    ->fields('u', array('name'))
+    ->condition('o.repo_id', $repo->repo_id)
+    ->condition('o.author_uid', 0, '!=')
+    ->groupBy('repo_id')
+    ->groupBy('uid')
+    ->orderByHeader($header_with_order)
+    ->execute();
   $committers = array();
   foreach ($result as $committer) {
     $committers[$committer->uid] = $committer;
@@ -377,7 +406,7 @@ function versioncontrol_project_committers_page($project) {
   );
   // Use FALSE for the 3rd argument to get all committers, not just the users
   // that currently have commit access.
-  $committers = versioncontrol_project_get_project_committers($project->nid, tablesort_sql($header), FALSE);
+  $committers = versioncontrol_project_get_project_committers($project->nid, $header, FALSE);
   $rows = array();
   foreach ($committers as $committer) {
     $rows[] = array(
-- 
1.7.10.4

