? .cvsignore
? userpoints_convert.patch
? userpoints_convert2.patch
Index: userpoints.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints/Attic/userpoints.admin.inc,v
retrieving revision 1.1.2.12
diff -u -p -r1.1.2.12 userpoints.admin.inc
--- userpoints.admin.inc	30 Jan 2011 17:44:26 -0000	1.1.2.12
+++ userpoints.admin.inc	31 Jan 2011 20:52:03 -0000
@@ -882,3 +882,85 @@ function userpoints_admin_settings($form
 
   return system_settings_form($form);
 }
+
+/**
+ * Form builder function; display form to convert existing transactions.
+ */
+function userpoints_admin_convert($form, &$form_state) {
+  $form['convert'] = array(
+    '#type' => 'submit',
+    '#value' => t('Convert transactions'),
+  );
+
+  return $form;
+}
+
+/**
+ * Start batch processing of existing transactions.
+ */
+function userpoints_admin_convert_submit($form, &$form_state) {
+  $batch = array(
+    'title' => t('Convert transactions'),
+    'operations' => array(
+      array('userpoints_admin_convert_process', array()),
+    ),
+    'finished' => 'userpoints_admin_convert_finished',
+    'file' => drupal_get_path('module', 'userpoints') . '/userpoints.admin.inc',
+  );
+  batch_set($batch);
+}
+
+/**
+ * Batch processing function for converting old points transactions.
+ */
+function userpoints_admin_convert_process(&$context) {
+
+  $defined_operations = array_keys(userpoints_get_info());
+
+  // First run, initialize sandbox.
+  if (!isset($context['sandbox']['max'])) {
+    // Assume that the thread ids are distributed more or less equally over the
+    // whole data set. This allows us to calculate the approximate progress.
+    $context['sandbox']['max'] = db_query('SELECT COUNT(txn_id) FROM {userpoints_txn} WHERE operation NOT IN (:defined)', array(':defined' => $defined_operations))->fetchField();
+    $context['results']['count'] = 0;
+    $context['results']['txn_id'] = 0;
+    $context['results']['converted'] = 0;
+  }
+
+  // Fetch the 10 next thread_ids.
+  $result = db_query_range('SELECT * FROM userpoints_txn WHERE operation NOT IN (:defined) AND txn_id > :txn_id ORDER BY txn_id ASC', 0, 20, array(':defined' => $defined_operations, ':txn_id' => $context['results']['txn_id']));
+  $modules = module_implements('userpoints_convert');
+  $current_count = 0;
+  foreach ($result as $transaction) {
+    foreach ($modules as $module) {
+      if (module_invoke($module, 'userpoints_convert', $transaction)) {
+        $context['results']['converted']++;
+        drupal_write_record('userpoints_txn', $transaction, array('txn_id'));
+        break;
+      }
+    }
+    $context['results']['count']++;
+    $context['results']['txn_id'] = $transaction->txn_id;
+    $current_count++;
+  }
+
+  // It is possible that the process finishes early.
+  if ($current_count < 20) {
+    $context['results']['count'] = $context['sandbox']['max'];
+  }
+
+  // Set #finished based on sandbox.
+  $context['finished'] = empty($context['sandbox']['max']) ? 1 : ($context['results']['count'] / $context['sandbox']['max']);
+}
+
+/**
+ * Finished batch callback for converting transactions.
+ */
+function userpoints_admin_convert_finished($success, $results, $operations) {
+  if ($success) {
+    drupal_set_message(t('Converted %converted transactions out of %count processed transactions.', array('%converted' => $results['converted'], '%count' => $results['count'])));
+  }
+  else {
+    drupal_set_message(t('Finished with an error.'), 'error');
+  };
+}
Index: userpoints.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/userpoints/userpoints.module,v
retrieving revision 1.68.2.24
diff -u -p -r1.68.2.24 userpoints.module
--- userpoints.module	30 Jan 2011 17:44:26 -0000	1.68.2.24
+++ userpoints.module	31 Jan 2011 20:52:07 -0000
@@ -239,6 +239,19 @@ function userpoints_menu() {
       'weight' => 10,
   );
 
+  $items['admin/config/people/userpoints/convert'] = array(
+      'title' => 'Convert',
+      'description' => strtr('Allows to convert old !points transactions to use the new descriptions.', userpoints_translation()),
+      'title arguments' => userpoints_translation(),
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('userpoints_admin_convert'),
+      'access callback' => 'userpoints_admin_access',
+      'access arguments' => array('administer'),
+      'file' => 'userpoints.admin.inc',
+      'type' => MENU_LOCAL_TASK,
+      'weight' => 10,
+  );
+
   $items['userpoints'] = array(
       'title' => 'Users by !points',
       'title arguments' => userpoints_translation(),
