--- modules/coppa/coppa.1.module	Thu Feb 24 18:09:03 2011
+++ modules/coppa/coppa.module	Sat Jan 14 10:55:17 2012
@@ -89,8 +89,39 @@
 function coppa_user($op, &$edit, &$account, $category = NULL) {
   global $user;
   switch ($op) {
+    case 'insert':
+      // Add coppa role to user and store parent email in database.
+      if (isset($edit['parent_email'])) {
+        db_query("INSERT INTO {coppa_email} (cid, mail, mail_sent) VALUES (%d, '%s', %d)", $account->uid, $edit['parent_email'], time());
+        // Check if parent_email is already user.
+        $pid = db_result(db_query("SELECT uid FROM {users} WHERE mail='%s'", $edit['parent_email']));
+        if (!empty($pid)) {
+          db_query("INSERT INTO {coppa} (pid, cid, status) VALUES (%d, %d, %d)", $pid, $account->uid, 0);
+        }
+
+        $coppa_roles = variable_get('coppa_roles', 0);
+
+        if (!is_array($coppa_roles)) {
+          $coppa_roles = array();
+        }
+
+        foreach ($coppa_roles as $rid) {
+          if (!isset($account->roles[$rid]) && !isset($edit['roles'][$rid])) {
+            $role_name = db_result(db_query('SELECT name FROM {role} WHERE rid = %d', $rid));
+            $edit['roles'] = $edit['roles'] + array($rid => $role_name);
+          }
+        }
+      }
+      else {
+        $cid = coppa_email_check_parent($edit['mail']);
+        if (!empty($cid)) {
+          db_query("INSERT INTO {coppa} (pid, cid, status) VALUES (%d, %d, %d)", $account->uid, $cid, 0);
+        }
+      }
+    break;
     case 'login':
       if (coppa_needed($user->uid)) {
+        // TODO: Maybe move this to form validate on user_login & user_login_block, see example: http://drupal.org/node/771020.
         // following code taken from user.module: user_logout()
         // unfortunately we need a slightly modified version of user_logout otherwise we would use that
         watchdog('user', t('Session closed for %name COPPA required.', array('%name' => $user->name)));
@@ -110,7 +141,13 @@
         drupal_goto('coppa/required');
 
       }
-      break;
+    break;
+    case 'delete':
+      db_query("DELETE FROM {coppa} WHERE pid=%d OR cid=%d", $account->uid, $account->uid);
+      if (module_exists('coppa_email')) {
+        db_query("DELETE FROM {coppa_email} WHERE cid=%d", $account->uid);
+      }
+    break;
   }
 }
 
@@ -203,13 +240,30 @@
  * if a profile_dob entry is not found for this user then an empty array is returned
  */
 function coppa_get_dob($uid) {
-  $result = db_query("SELECT pv.value FROM {profile_values} pv, {profile_fields} pf, {users} u WHERE pf.name = 'profile_dob' AND pf.fid = pv.fid AND pv.uid = u.uid AND u.uid = %d", $uid);
-  $r = db_fetch_array($result);
-  if ($r) {
-    return unserialize($r['value']);
+  $field = variable_get('coppa_dob_field', 'profile_dob');
+  $type = variable_get('coppa_dob_field_type', 'profile');
+  if ($type == 'profile') {
+    $result = db_result(db_query("SELECT pv.value FROM {profile_values} pv, {profile_fields} pf, {users} u WHERE pf.name = 'profile_dob' AND pf.fid = pv.fid AND pv.uid = u.uid AND u.uid = %d", $uid));
+    if ($result) {
+      return unserialize($result);
+    }
+    else {
+      return array();
+    }
   }
   else {
-    return array();
+    $db_info[$field] = content_database_info(content_fields($field));
+    $value = $db_info[$field]['columns']['value']['column'];
+    $table = $db_info[$field]['table'];
+    $result = db_result(db_query("SELECT d.$value FROM {node} n, {" . $table . "} d, {users} u WHERE n.uid = u.uid AND n.nid = d.nid AND u.uid = %d", $uid));
+    if ($result) {
+      $dob = format_date(strtotime($result), 'custom', 'Y-m-d');
+      $dob = explode('-', $dob);
+      return array('year' => $dob[0], 'month' => $dob[1], 'day' => $dob[2]);
+    }
+    else {
+      return array();
+    }
   }
 }
 
@@ -241,29 +295,34 @@
  */
 function _coppa_get_dob_form($form, $form_state) {
   $field = variable_get('coppa_dob_field', 'profile_dob');
-  $field  = 'field_dob';
   $type = variable_get('coppa_dob_field_type', 'profile');
-  $type = 'content_profile';
 
   switch ($type) {
     case 'profile':
-      // @todo
+      $dob = $form_state['values'][$field]['year'] . "-" . $form_state['values'][$field]['month'] . "-" . $form_state['values'][$field]['day'] . 'T00:00:00';
       break;
 
     case 'content_profile':
       $dob = $form_state['values'][$field][0]['value'];
-      return strtotime($dob);
+      break;
   }
+
+  return strtotime($dob);
 }
 
 /**
  * Determine age based on DOB.
  */
-function coppa_age($dob, $now = NULL) {
-  if (!$now) {
-    $now = time();
+function coppa_age($dob) {
+  $now = time();
+
+  $dob = format_date($dob, 'custom', 'Y-m-d');
+  list($Y, $m, $d) = explode("-", $dob);
+  $age = format_date($now, 'custom', 'Y') - $Y;
+
+  if(format_date($now, 'custom', 'md') < $m.$d) {
+    $age--;
   }
 
-
-  return 12;
+  return $age;
 }
\ No newline at end of file
