Compare content of a field before and after editing a node

dtabach - October 16, 2008 - 21:10

Hello,

This is my very first attempt to write a simple module. I am not a coder, but after reading the Userpoints API, I thought I should give it a try.

My new module is dependent on User Points 5.3.x , and should give points to users who keep their Profile fields filled.

The profile node, a CCK type with several fields, is automatically created when a new account is created (by Bio module). Initially, the node has only the title field filled (Auto Node Title module). The user should afterwards edit the node and add info to the other fields.

The easy part was to know if a field has some information and reward points to the user accordingly. The problem is to remove points when the user deletes a field content upon editing his profile node, because I need to know if the removed field had info before the edition.

After a lot of reading, it seems this would be possible by performing operations based on '$op'.

Here is the result of my attempt so far (not working yet):

<?php
function userpoints_rewardprofilefields_nodeapi(&$node, $op) {
  if (
$node->type =='profile') {
    switch (
$op) {
    case
'prepare' :
        if (
$node->field_aboutme[0]['value']) {
           
$fieldbefore = 10;
        }
    break;
    case
'update' :
        if (
$node->field_aboutme[0]['value']) {
           
$fieldafter = 10;
        }
       
$points = $fieldafter - $fieldbefore;
       
userpoints_userpointsapi($points);
       }
// switch
 
} // if node type...
} // function
?>

The final version of the module will act on several fields, but for testing purposes I did it as if there was only one field.

The intention here is to give zero points if 'field_aboutme' was filled and remained filled (10-10=0) or was empty and remained empty (0-0=0). If the field changed from empty to filled, user earns 10 points (10-0=0). If it was filled before then it was emptied, user looses 10 points (0-10=-10).

The problem is that the variable '$fieldbefore' does not retrieve its value when the node is updated. I tried different $ops in the first and second case, to no avail.

Is there any way to do this?

Possible solution

dtabach - October 16, 2008 - 23:05

Use constants FIELDBEFORE and FIELDAFTER instead of variables.

Would this bring trouble?

 
 

Drupal is a registered trademark of Dries Buytaert.