? count.patch
Index: audio.install
===================================================================
RCS file: /cvs/drupal/contributions/modules/audio/audio.install,v
retrieving revision 1.13
diff -u -p -r1.13 audio.install
--- audio.install	15 Feb 2007 16:37:47 -0000	1.13
+++ audio.install	15 May 2007 17:32:40 -0000
@@ -13,8 +13,6 @@ function audio_install() {
           `vid` int(10) unsigned NOT NULL default '0',
           `nid` int(10) unsigned NOT NULL default '0',
           `title_format` varchar(128) default '',
-          `play_count` int(10) unsigned NOT NULL default '0',
-          `download_count` int(10) unsigned NOT NULL default '0',
           `downloadable` tinyint(1) NOT NULL default '1',
           `fileformat` varchar(10) NOT NULL default '',
           `sample_rate` int(10) unsigned NOT NULL default '0',
@@ -26,6 +24,14 @@ function audio_install() {
         ) /*!40100 DEFAULT CHARACTER SET utf8 */;
       ");
       db_query("
+        CREATE TABLE  {audio_count} (
+          `nid` int(10) unsigned NOT NULL default '0',
+          `play_count` int(10) unsigned NOT NULL default '0',
+          `download_count` int(10) unsigned NOT NULL default '0',
+          PRIMARY KEY  (`nid`)
+        ) /*!40100 DEFAULT CHARACTER SET utf8 */;
+      ");
+      db_query("
         CREATE TABLE  {audio_file} (
           `vid` int(10) unsigned NOT NULL default '0',
           `origname` varchar(255) NOT NULL default '',
@@ -56,10 +62,6 @@ function audio_install() {
             nid integer NOT NULL default '0'
                 CHECK (nid >= 0),
             title_format varchar(128) default '',
-            play_count integer NOT NULL default '0'
-                CHECK (play_count >= 0),
-            download_count integer NOT NULL default '0'
-                CHECK (download_count >= 0),
             downloadable smallint NOT NULL default '1',
             fileformat varchar(10) NOT NULL default '',
             sample_rate integer NOT NULL default '0'
@@ -73,6 +75,17 @@ function audio_install() {
         );
       ");
       db_query("
+        CREATE TABLE {audio_count} (
+            nid integer NOT NULL default '0'
+                CHECK (nid >= 0),
+            play_count integer NOT NULL default '0'
+                CHECK (play_count >= 0),
+            download_count integer NOT NULL default '0'
+                CHECK (download_count >= 0),
+            PRIMARY KEY  (nid)
+        );
+      ");
+      db_query("
         CREATE TABLE {audio_file} (
             vid integer NOT NULL default '0'
                 CHECK (vid >= 0),
@@ -106,6 +119,7 @@ function audio_install() {
  */
 function audio_uninstall() {
   db_query('DROP TABLE {audio}');
+  db_query('DROP TABLE {audio_count}');
   db_query('DROP TABLE {audio_file}');
   db_query('DROP TABLE {audio_metadata}');
 
@@ -276,3 +290,46 @@ function audio_update_111() {
   }
   return array();
 }
+
+/**
+ * Movie download and play counts to their own table, so they are not
+ * reset when a new revision is created.
+ */
+function audio_update_112() {
+  $ret = array();
+
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql("
+        CREATE TABLE  {audio_count} (
+          `nid` int(10) unsigned NOT NULL default '0',
+          `play_count` int(10) unsigned NOT NULL default '0',
+          `download_count` int(10) unsigned NOT NULL default '0',
+          PRIMARY KEY  (`nid`)
+        ) /*!40100 DEFAULT CHARACTER SET utf8 */;
+      ");
+      break;
+    case 'pgsql':
+      $ret[] = update_sql("
+        CREATE TABLE {audio_count} (
+            nid integer NOT NULL default '0'
+                CHECK (nid >= 0),
+            play_count integer NOT NULL default '0'
+                CHECK (play_count >= 0),
+            download_count integer NOT NULL default '0'
+                CHECK (download_count >= 0),
+            PRIMARY KEY  (nid)
+        );
+      ");
+  }
+
+  // Migrate old information.
+  $ret[] = update_sql('INSERT INTO {audio_count} (nid, play_count, download_count) SELECT nid, SUM(play_count), SUM(download_count) FROM {audio} GROUP BY nid');
+
+  // Drop the old columns.
+  $ret[] = update_sql('ALTER TABLE {audio} DROP play_count');
+  $ret[] = update_sql('ALTER TABLE {audio} DROP download_count');
+
+  return $ret;
+}
Index: audio.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/audio/audio.module,v
retrieving revision 1.106
diff -u -p -r1.106 audio.module
--- audio.module	17 Apr 2007 16:55:25 -0000	1.106
+++ audio.module	15 May 2007 17:32:41 -0000
@@ -405,6 +405,7 @@ function audio_load($node) {
   if ($node->vid) {
     // load the audio records...
     $ret['audio_fileinfo'] = db_fetch_array(db_query("SELECT * FROM {audio} WHERE vid=%d", $node->vid));
+    $ret['audio_fileinfo'] = array_merge($ret['audio_fileinfo'], db_fetch_array(db_query('SELECT play_count, download_count FROM {audio_count} WHERE nid = %d', $node->nid)));
     // ... then move some non-file info stuff out of the array
     $ret['title_format'] = $ret['audio_fileinfo']['title_format'];
     unset($ret['audio_fileinfo']['title_format']);
@@ -469,6 +470,7 @@ function audio_insert(&$node) {
   $i = $node->audio_fileinfo;
   db_query("INSERT INTO {audio} (vid, nid, title_format, downloadable, fileformat, bitrate, bitrate_mode, sample_rate, channel_mode, playtime) VALUES (%d, %d, '%s', %d, '%s', %f, '%s', %d, '%s', '%s')",
     $node->vid, $node->nid, $node->title_format, $i['downloadable'], $i['fileformat'], $i['bitrate'], $i['bitrate_mode'], $i['sample_rate'], $i['channel_mode'], $i['playtime']);
+  db_query('INSERT INTO {audio_count} (nid, play_count, download_count) VALUES (%d, %d, %d)', $node->nid, $i['play_count'], $i['download_count']);
 
   // add the tags records
   _audio_save_tags_to_db($node);
@@ -548,9 +550,10 @@ function audio_update(&$node) {
   db_query("DELETE FROM {audio} WHERE vid=%d", $node->vid);
   db_query("DELETE FROM {audio_file} WHERE vid=%d", $node->vid);
   $i = $node->audio_fileinfo;
-  db_query("INSERT INTO {audio} (vid, nid, title_format, play_count, download_count, downloadable, fileformat, bitrate, bitrate_mode, sample_rate, channel_mode, playtime) VALUES (%d, %d, '%s', %d, %d, %d, '%s', %f, '%s', %d, '%s', '%s')",
-    $node->vid, $node->nid, $node->title_format, $i['play_count'], $i['download_count'], $i['downloadable'], $i['fileformat'], $i['bitrate'], $i['bitrate_mode'], $i['sample_rate'], $i['channel_mode'], $i['playtime']);
+  db_query("INSERT INTO {audio} (vid, nid, title_format, downloadable, fileformat, bitrate, bitrate_mode, sample_rate, channel_mode, playtime) VALUES (%d, %d, '%s', %d, %d, %d, '%s', %f, '%s', %d, '%s', '%s')",
+    $node->vid, $node->nid, $node->title_format, $i['downloadable'], $i['fileformat'], $i['bitrate'], $i['bitrate_mode'], $i['sample_rate'], $i['channel_mode'], $i['playtime']);
   $f = $node->audio_file;
+  db_query('INSERT INTO {audio_count} (nid, play_count, download_count) VALUES (%d, %d, %d)', $node->nid, $i['play_count'], $i['download_count']);
   db_query("INSERT INTO {audio_file} (vid, origname, filename, filepath, filemime, filesize) VALUES (%d, '%s', '%s', '%s', '%s', %d)",
     $node->vid, $f->origname, basename($f->filepath), $f->filepath, $f->filemime, filesize($f->filepath));
 
@@ -1409,7 +1412,7 @@ function audio_download($nid = false) {
   if ($nid && $node = node_load($nid)) {
     if (_audio_allow_download($node)) {
       // increment the play count
-      db_query('UPDATE {audio} SET download_count = download_count + 1 WHERE vid = %d', $node->vid);
+      db_query('UPDATE {audio_count} SET download_count = download_count + 1 WHERE nid = %d', $node->nid);
 
       // downloading counts as "viewing" the node
       node_tag_new($node->nid);
@@ -1452,7 +1455,7 @@ function audio_play($nid = false) {
   if ($nid && $node = node_load($nid)) {
     if (_audio_allow_play($node)) {
       // increment the play count
-      db_query('UPDATE {audio} SET play_count = play_count + 1 WHERE vid = %d', $node->vid);
+      db_query('UPDATE {audio_count} SET play_count = play_count + 1 WHERE nid = %d', $node->nid);
 
       // playing counts as "viewing" the node
       node_tag_new($node->nid);
Index: views_audio.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/audio/views_audio.inc,v
retrieving revision 1.6
diff -u -p -r1.6 views_audio.inc
--- views_audio.inc	2 Jan 2007 16:39:07 -0000	1.6
+++ views_audio.inc	15 May 2007 17:32:41 -0000
@@ -14,16 +14,6 @@ function audio_views_tables() {
       )
     ),
     'fields' => array(
-      'play_count' => array(
-        'name' => t('Audio: Play count'),
-        'sortable' => TRUE,
-        'help' => t('This will display the number of times this has been played.'),
-      ),
-      'download_count' => array(
-        'name' => t('Audio: Download count'),
-        'sortable' => TRUE,
-        'help' => t('This will display the number of times this has been downloaded.'),
-      ),
       'playtime' => array(
         'name' => t('Audio: Length'),
         'sortable' => TRUE,
@@ -55,6 +45,31 @@ function audio_views_tables() {
     ),
   );
 
+  $tables['audio_count'] = array(
+    'name' => 'audio_count',
+    'join' => array(
+      'left' => array(
+        'table' => 'audio',
+        'field' => 'nid'
+      ),
+      'right' => array(
+        'field' => 'nid'
+      )
+    ),
+    'fields' => array(
+      'play_count' => array(
+        'name' => t('Audio: Play count'),
+        'sortable' => TRUE,
+        'help' => t('This will display the number of times this has been played.'),
+      ),
+      'download_count' => array(
+        'name' => t('Audio: Download count'),
+        'sortable' => TRUE,
+        'help' => t('This will display the number of times this has been downloaded.'),
+      ),
+    ),
+  );
+
   $tables['audio_file'] = array(
     'name' => 'audio_file',
     'join' => array(
