commit bd669b71f60154a93faf82c3d9d366ea29a0e49c Author: Matthew Radcliffe Date: Wed Mar 20 13:49:30 2013 -0400 Issue #1258032 by mradcliffe: Try out saving points within the transaction controller class instead of through entity CRUD API. diff --git a/userpoints.module b/userpoints.module index 7b9e5c7..8284f94 100644 --- a/userpoints.module +++ b/userpoints.module @@ -608,70 +608,6 @@ function userpoints_token_info() { } /** - * Implements hook_userpoints_transaction_update(). - * - * @todo Support writing to all entities with userpoints field. - */ -function userpoints_userpoints_transaction_update($entity) { - $wrapper = entity_metadata_wrapper('userpoints_transaction', $entity); - $account = $wrapper->user->value(); - - $delta = 0; - - if (isset($account->field_userpoints['und'])) { - foreach ($account->field_userpoints['und'] as $num => $values) { - if ($values['bundle'] == $entity->type) { - // Set the cardinality of the field to the matching bundle. - $delta = $num; - break; - } - else { - // Increment cardinality of field. - $delta++; - } - } - } - - user_save($account); - $account->field_userpoints['und'][$delta]['points'] = userpoints_transaction_get_points($account->uid, $entity->type); - $account->field_userpoints['und'][$delta]['bundle'] = $entity->type; - $account = user_save($account); -} - -/** - * Implements hook_userpoints_transaction_insert(). - * - * This supports retroactive points if there are transactions for a user and no - * points field value yet. - * - * @todo Support writing to all entities with userpoints field. - */ -function userpoints_userpoints_transaction_insert($entity) { - $wrapper = entity_metadata_wrapper('userpoints_transaction', $entity); - $account = $wrapper->user->value(); - - $delta = 0; - - if (isset($account->field_userpoints['und'])) { - foreach ($account->field_userpoints['und'] as $num => $values) { - if ($values['bundle'] == $entity->type) { - // Set the cardinality of the field to the matching bundle. - $delta = $num; - break; - } - else { - // Increment cardinality of field. - $delta++; - } - } - } - - $account->field_userpoints['und'][$delta]['points'] = userpoints_transaction_get_points($account->uid, $entity->type); - $account->field_userpoints['und'][$delta]['bundle'] = $entity->type; - $account = user_save($account); -} - -/** * Get sum of points from transactions. This is an aggregate function. * * @param $uid diff --git a/userpoints.transaction.inc b/userpoints.transaction.inc index 48a7192..cf9c607 100644 --- a/userpoints.transaction.inc +++ b/userpoints.transaction.inc @@ -1225,6 +1225,8 @@ class UserpointsTransactionController extends EntityAPIController { // Update totals if the transaction is approved and not expired. if ($entity->isApproved() && !$entity->isExpired()) { + $account = user_load($entity->uid); + $this->updateFieldValues($account, $entity->points); $this->updateTotals($entity->tid, $entity->uid, $entity->points); } @@ -1238,6 +1240,42 @@ class UserpointsTransactionController extends EntityAPIController { return $return; } + /** + * Update point field(s) values. + * + * @todo Currently hardcoded for user accounts only. + */ + protected function updateFieldValues($entity, $points) { + $entity_type = $entity->getEntityType(); + $delta = 0; + + if (isset($entity->field_userpoints['und'])) { + foreach ($entity->field_userpoints['und'] as $num => $values) { + if ($this->type == $values['bundle']) { + // Find the corresponding cardinality for a bundle. + $delta = $num; + break; + } + else { + $delta++; + } + } + } + + if (isset($entity->field_userpoints['und'][$delta])) { + // Add points sum to total for bundle. + $entity->field_userpoints['und'][$delta]['points'] += $points; + } + else { + // Increment cardinality for untracked bundle on user account. + $entity->field_userpoints['und'][$delta] = array( + 'points' => userpoints_transaction_get_points($entity->uid, $this->type), + 'bundle' => $this->type, + ); + } + + entity_save($entity_type, $entity); + } /** * Update the total aggregations of the corresponding user. commit 8994e150114b7a36763210a370b08d34b8479c2e Author: Matthew Radcliffe Date: Wed Mar 20 14:18:37 2013 -0400 Issue #1258032 by mradcliffe: Move things around per berdir's comments. diff --git a/userpoints.transaction.inc b/userpoints.transaction.inc index 8ac82a3..2630b8f 100644 --- a/userpoints.transaction.inc +++ b/userpoints.transaction.inc @@ -1265,12 +1265,16 @@ class UserpointsTransactionController extends EntityAPIController { // Set the field values for points and bundle for a given index. Max points // are calculated in field CRUD. - $entity->field_userpoints[LANGUAGE_NONE][$delta] = array( - 'points' => userpoints_transaction_get_points($entity->uid, $transaction->type), - 'bundle' => $transaction->type, - ); + $item = &$entity->field_userpoints[LANGUAGE_NONE][$delta]; + $item['bundle'] = $transaction->type; + $item['points'] = userpoints_transaction_get_points($entity->uid, $transaction->type); + + if (!isset($item['max_points']) || $item['points'] > $item['max_points']) { + // Set maximum points only if current points is greater. + $item['max_points'] = $item['points']; + } - $ret = entity_save($entity_type, $entity); + entity_save($entity_type, $entity); } /** diff --git a/userpoints_field/userpoints_field.install b/userpoints_field/userpoints_field.install index 5383b31..39eebc2 100644 --- a/userpoints_field/userpoints_field.install +++ b/userpoints_field/userpoints_field.install @@ -1,7 +1,7 @@ $item) { - // There should always be cardinality 1 for this field, but don't assume. - - if (!isset($item['max_points']) || $item['points'] > $item['max_points']) { - // Set maximum points only if current points is greater. - $items[$delta]['max_points'] = $item['points']; - } - } - } -} - -/** * Implements hook_field_widget_form(). * * Do not display any widget. All points will be saved via the userpoints transaction system. @@ -170,7 +154,7 @@ function userpoints_field_metadata_property_callback(&$info, $entity_type, $fiel 'raw getter callback' => 'entity_property_verbatim_get', ), 'bundle' => array( - 'type' => 'token', + 'type' => 'userpoints_transaction_type', 'label' => t('Userpoints transaction type'), 'sanitized' => FALSE, 'getter callback' => 'entity_property_verbatim_get', commit dd8b551ddf245b9f9816dbffc91667bfb2cf14d4 Author: Matthew Radcliffe Date: Wed Mar 27 14:34:34 2013 -0400 Issue #1258032 by mradcliffe: Change test databases to have userpoints_field enabled as that is required to process the update anyway. diff --git a/tests/userpoints.filled.database.all.php.gz b/tests/userpoints.filled.database.all.php.gz index dc54f63..17ab4dc 100644 Binary files a/tests/userpoints.filled.database.all.php.gz and b/tests/userpoints.filled.database.all.php.gz differ diff --git a/tests/userpoints.filled.database.php b/tests/userpoints.filled.database.php index 6a0c027..5dd4a96 100644 --- a/tests/userpoints.filled.database.php +++ b/tests/userpoints.filled.database.php @@ -433,6 +433,16 @@ db_insert('system')->fields(array( 'status' => 1, )) ->values(array( + 'name' => 'userpoints_field', + 'filename' => drupal_get_path('module', 'userpoints') . '/userpoints_field/userpoints_field.module', + 'type' => 'module', + 'owner' => '', + 'bootstrap' => 0, + 'weight' => 0, + 'schema_version' => '7000', + 'status' => 1, +)) +->values(array( 'name' => 'entity', 'filename' => drupal_get_path('module', 'entity') . '/entity.module', 'type' => 'module',