? p.patch
Index: README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/pcp/README.txt,v
retrieving revision 1.2
diff -u -p -r1.2 README.txt
--- README.txt	9 Nov 2010 09:15:49 -0000	1.2
+++ README.txt	27 Jan 2011 13:21:47 -0000
@@ -6,24 +6,13 @@ has successfully completed, the percenta
 INSTALL
 ==============================================
 1. Download and extract the PCP module into your Drupal site.
-2. Go to admin/build/modules and activate the "Profile" module and "Profile Complete Percentage" module.
-3. Make sure you set up the desired profile fields in the profile module at admin/user/profile.
-4. Go to admin/user/pcp and Check the profile fields you want to use for completion then save.
+2. Go to admin/build/modules and activate "Profile Complete Percentage" module.
+3. Make sure you set up the desired profile fields in the profile module at admin/config/people/accounts/fields.
+4. Go to admin/config/people/pcp and Check the profile fields you want to use for completion then save.
   - Additionally when creating profile fields in step 3 you can tag the field there as well.
 5. Go to admin/build/block and place the "Profile Complete Percentage" block to your desired location.
 
 After steps 1 - 5 of INSTALL are complete, you will see a basic block informing you
 of how much your profile has been complete. All data is determined on the fly so 
-should you opt to activate or deactivate fields to be required in admin/user/pcp the displayed
-data will adjust when the change has been mage.
-
-THEME
-==============================================
-One function in pcp.module is being used to theme the output of the profile percentage complete block.
-
-function theme_pcp_profile_percent_complete($complete_data);
-
-To override the output of the Profile Percent Complete block, copy this function and paste it into 
-your template.php file and rename it to SITENAME_pcp_profile_complete($complete_data);
-
-You now have full control of the output this block will generate.
\ No newline at end of file
+should you opt to activate or deactivate fields to be required in admin/config/people/pcp the displayed
+data will adjust when the change has been mage.
\ No newline at end of file
Index: pcp-block.tpl.php
===================================================================
RCS file: pcp-block.tpl.php
diff -N pcp-block.tpl.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ pcp-block.tpl.php	27 Jan 2011 13:21:47 -0000
@@ -0,0 +1,39 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Default theme implementation for PCP block
+ *
+ * Available variables:
+ *  $uid: Current user ID.
+ *  $current_percent: From 0 to 100% of how much the profile is complete.
+ *  $next_percent: The percent if the user filled the next field.
+ *  $completed: How many fields the user has filled.
+ *  $incomplete: The inverse of $completed, how many empty fields left.
+ *  $total: Total number of fields in profile.
+ *  $nextfield_title: The name of the suggested next field to fill (human readable name).
+ *  $nextfield_name: The name of the suggested next field to fill (machine name).
+ *  $nextfield_id: The id of the $nextfield.
+ *
+ * @see template_preprocess_pcp_profile_percent_complete()
+ */
+?>
+<style type="text/css">
+  #pcp-percent-bar-wrapper {width: 100%; border: 1px solid #000; padding: 1px;}
+  #pcp-percent-bar { width: <?php print $current_percent; ?>%; height: 10px; background-color: #777;}
+</style>
+
+<div id="pcp-wrapper">
+  <?php print t('!complete% Complete', array('!complete' => $current_percent)); ?>
+
+  <div id="pcp-percent-bar-wrapper">
+    <div id="pcp-percent-bar"></div>
+  </div>
+</div>
+
+<?php
+  if (isset($nextfield_id) && isset($next_percent)) {
+    print t('Filling out <em>!empty-field</em> will bring your profile to !complete% Complete', array('!empty-field' => l($nextfield_title, $next_path), '!complete' => $next_percent));
+  }
+?>
\ No newline at end of file
Index: pcp.admin.inc
===================================================================
RCS file: pcp.admin.inc
diff -N pcp.admin.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ pcp.admin.inc	27 Jan 2011 13:21:48 -0000
@@ -0,0 +1,150 @@
+<?php
+// $Id$
+
+/**
+ * Form builder; Configure PCP fields.
+ *
+ * @ingroup forms
+ */
+function pcp_admin_settings($form, &$form_state) {
+  $form['general_settings'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'General Settings',
+  );
+  $form['general_settings']['hide_block_on_complete'] = array(
+    '#type' => 'checkbox',
+    '#title' => 'Hide Block When Complete',
+    '#default_value' => variable_get('pcp_hide_block_on_complete', 0),
+    '#description' => t('When a user reaches 100% complete of their profile, do you want the profile complete percent block to go away? If so, check this box on.'),
+  );
+  if (variable_get('user_pictures', 0)) {
+    $form['general_settings']['pcp_enable_user_picture'] = array(
+      '#type' => 'checkbox',
+      '#title' => 'Enable User Picture Support',
+      '#default_value' => variable_get('pcp_enable_user_picture', 0),
+      '#description' => t('Picture support has been turned on, do you want to make it required as part of Profile Complete Percent? Checking this box will make it required.'),
+    );
+  }
+
+  $form['profile_field_settings'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Profile Field Settings',
+    '#description' => t('Checking a profile field below will add that field to the logic of the complete percentage.'),
+  );
+  $options = pcp_admin_settings_form_data();
+  if ($options['profile_fields_options']) {
+    $form['profile_field_settings']['profile_fields'] = array(
+      '#title' => t('Profile Fields'),
+      '#type' => 'checkboxes',
+      '#options' => $options['profile_fields_options'],
+      '#default_value' => array_flip($options['default_values']),
+    );
+  }
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save'),
+  );
+
+  return $form;
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function pcp_form_user_admin_settings_alter(&$form, &$form_state, $form_id) {
+  if (user_access('administer pcp')) {
+    $form['personalization']['pictures']['pcp_enable_user_picture'] = array(
+      '#title' => 'Make required for PCP module',
+      '#type' => 'checkbox',
+      '#default_value' => variable_get('pcp_enable_user_picture', 0),
+      '#description' => t('Checking this box will tag this field as a required field for completion of the users profile.'),
+      '#weight' => -5,
+    );
+    $form['#submit'][] = 'pcp_admin_settings_submit';
+  }
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function pcp_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
+  // @todo do we need user access check here?
+  $tag = FALSE;
+  $result = pcp_get_tagged_profile_fields($form['#field']['id']);
+  if (!empty($result)) {
+    $tag = TRUE;
+  }
+
+  $form['instance']['pcp_tag'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Make required for PCP module'),
+    '#description' => t('Checking this box will tag this field as a required field for completion of the users profile.'),
+    '#default_value' => $tag,
+    '#weight' => -5,
+  );
+  $form['#submit'][] = 'pcp_admin_settings_submit';
+}
+
+/**
+ * Function that sets up parameters to be used 
+ * when the pcp_admin_settings_form() function
+ * is executed. 
+ *
+ * @return - assoc array
+ *  ['profile_fields_options'] 
+ *    - An associative array of all fields created from the profile module.
+ *  ['default_values']
+ *    - An indexed array of all (if any) default values for the form.
+ */
+function pcp_admin_settings_form_data() {
+  $options = array();
+  $options['profile_fields_options'] = pcp_get_profile_fields();
+  $options['default_values'] = pcp_get_tagged_profile_fields();
+
+  return $options;
+}
+
+/**
+ * Submit callback.
+ */
+function pcp_admin_settings_submit($form, &$form_state) {
+  switch ($form['#form_id']) {
+    // admin/config/people/accounts
+    case 'user_admin_settings':
+      variable_set('pcp_enable_user_picture', $form_state['values']['pcp_enable_user_picture']);
+      break;
+    // admin/config/people/accounts/fields/[field_name]
+    case 'field_ui_field_edit_form':
+      $fid = $form['#field']['id'];
+      db_delete('profile_pcp')
+        ->condition('fid', $fid)
+        ->execute();
+
+      if ($form_state['values']['instance']['pcp_tag']) {
+        db_insert('profile_pcp')
+          ->fields(array('fid' => $fid))
+          ->execute();
+      }
+      break;
+    // admin/config/people/pcp
+    default:
+      // Provides the General Settings data
+      variable_set('pcp_hide_block_on_complete', $form_state['values']['hide_block_on_complete']);
+      variable_set('pcp_enable_user_picture', $form_state['values']['pcp_enable_user_picture']);
+
+      // Process the profile fields that have been modified
+      if ($form_state['values']['profile_fields']) {
+        db_delete('profile_pcp')
+          ->execute();
+        foreach ($form_state['values']['profile_fields'] as $fid) {
+          if ($fid) {
+            db_insert('profile_pcp')
+              ->fields(array('fid' => $fid))
+              ->execute();
+          }
+        }
+        drupal_set_message("Your settings have been saved.");
+      }
+  }
+}
\ No newline at end of file
Index: pcp.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/pcp/pcp.info,v
retrieving revision 1.6
diff -u -p -r1.6 pcp.info
--- pcp.info	8 Feb 2010 18:42:55 -0000	1.6
+++ pcp.info	27 Jan 2011 13:21:48 -0000
@@ -1,8 +1,7 @@
 ; $Id$
 name = Profile Complete Percentage
-description = Configure CCK profile fields to be tagged as required for a profile to be complete. Display the percent of profile completion to the user via a block.
+description = Configure user fields to be tagged as required for a profile to be complete. Display the percent of profile completion to the user via a block.
 package = Profile
-dependencies[] = profile
-version = 6.x-2.1
-core = 6.x
-
+version = 7.x-1.x
+core = 7.x
+configure = admin/config/people/pcp
\ No newline at end of file
Index: pcp.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/pcp/pcp.install,v
retrieving revision 1.2
diff -u -p -r1.2 pcp.install
--- pcp.install	8 Feb 2010 18:22:46 -0000	1.2
+++ pcp.install	27 Jan 2011 13:21:48 -0000
@@ -3,29 +3,24 @@
 
 /**
  * @file
- * Install file for the profile.module
+ * Install file for the pcp module.
  */
 
-
-/**
-* Implementation of hook_install().
-*/
-function pcp_install() {
-  drupal_install_schema('pcp');
-}
-
 /**
- * Implementation of hook_schema()
+ * Implements of hook_schema()
  */
 function pcp_schema() {
   $schema['profile_pcp'] = array(
+    'description' => 'Stores PCP required profile fields.',
     'fields' => array(
-      'fid' => array('type' => 'int', 'not null' => TRUE),
+      'fid' => array(
+      'type' => 'int',
+      'not null' => TRUE,
+      'description' => 'Field ID',
       ),
-    );
-  return $schema;
-}
+    ),
+    'primary key' => array('fid'),
+  );
 
-function pcp_uninstall() {
-  drupal_uninstall_schema('pcp');
+  return $schema;
 }
\ No newline at end of file
Index: pcp.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/pcp/pcp.module,v
retrieving revision 1.5
diff -u -p -r1.5 pcp.module
--- pcp.module	9 Nov 2010 09:31:30 -0000	1.5
+++ pcp.module	27 Jan 2011 13:21:49 -0000
@@ -4,404 +4,231 @@
 /**
  * @file
  * Allows users with valid permissions to tag profile fields created
- * from the profile module as required fields for a 
+ * from the profile module as required fields for a
  * users profile to be considered complete.
  */
 
 /**
- * Implementation of hook_perm()
+ * Implements hook_init().
  */
-function pcp_perm() {
-  return array(
-    'administer pcp',
-    );
+function pcp_init() {
+  // needed to execute pcp_form_user_admin_settings_alter()
+  include_once('pcp.admin.inc');
 }
 
 /**
- * Implementation of hook_menu()
+ * Implements hook_theme().
  */
-function pcp_menu() {
-  $items = array();
-  
-  $items['admin/user/pcp'] = array(
-    'title' => 'Profile Complete Percentages',
-    'description' => 'Tag profile fields as required for percent complete handling.',
-    'page callback' => 'pcp_admin_settings',
-    'access arguments' => array('administer pcp'),
-    );
-
-  return $items;
+function pcp_theme($existing, $type, $theme, $path) {
+  return array(
+    'pcp_profile_percent_complete' => array(
+      'template' => 'pcp-block',
+      'variables' => array('complete_data' => NULL),
+    ),
+  );
 }
 
 /**
- * Implementation of hook_block()
+ * Implements hook_permission().
  */
-function pcp_block($op = 'list', $delta = 0, $edit = array()) {
-  switch ($op) {
-    case 'list':
-      $blocks = array();
-      $blocks[0]['info'] = t('Profile Complete Percentage');
-      return $blocks;
-      break;
-    case 'view':
-      $block = array();
-      switch ($delta) {
-        case 0:
-          global $user;
-          $complete_data = pcp_get_complete_percentage_data($user);
-          $doHide = variable_get('pcp_hide_block_on_complete', 0);
-          if ($doHide && $complete_data['percent'] == 100) {
-            $subject = '';
-            $content = '';
-          }
-          else {
-            $subject = t('Profile Complete');
-            $content = theme('pcp_profile_percent_complete', $complete_data);
-          }
-          $block['subject'] = $subject;
-          $block['content'] = $content;
-          break;
-      }
-      return $block;
-      break;
-  }
+function pcp_permission() {
+  return array(
+    'administer pcp' => array(
+      'title' => t('Administer Profile Complete Percentages'),
+      'description' => t('Allows a user to configure which fields to be considered complete.'),
+    ),
+  );
 }
 
 /**
- * Implementation of hook_form_alter()
+ * Implements hook_menu().
  */
-function pcp_form_alter(&$form, $form_state, $form_id) {
-  if ($form_id == 'user_admin_settings' && user_access('administer pcp')) {
-    $form['pictures']['settings']['pcp_enable_user_picture'] = array(
-      '#title' => 'Make required for PCP module',
-      '#type' => 'checkbox',
-      '#default_value' => variable_get('pcp_enable_user_picture', 0),
-      '#description' => t('Checking this box will tag this field as a required field for completion of the users profile.'),
-      );
-    $form['#submit'][] = 'pcp_user_admin_settings_submit';
-  }
-  
-  if ($form_id == 'profile_field_form' && user_access('administer pcp')) {
-    $fid = $form['fid']['#value'];
-    $tag = TRUE;
-    if ($fid) {
-      $field_data = pcp_get_tagged_profile_fields($fid);
-      if (!$field_data[0]['fid']) {
-        $tag = FALSE;
-      }
-    }
-    $form['pcp_settings'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('PCP Settings'),
-      '#weight' => 0,
-      );
-    $form['pcp_settings']['tag'] = array(
-      '#type' => 'checkbox',
-      '#title' => t('Make required for PCP module'),
-      '#description' => t('Checking this box will tag this field as a required field for completion of the users profile.'),
-      '#default_value' => $tag,
-      );
-    $form['#submit'][] = 'pcp_profile_field_form_submit';
-  }
+function pcp_menu() {
+  $items['admin/config/people/pcp'] = array(
+    'title' => 'Profile Complete Percentages',
+    'description' => 'Tag profile fields as required for percent complete handling.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('pcp_admin_settings'),
+    'access arguments' => array('administer pcp'),
+    'file' => 'pcp.admin.inc',
+    'weight' => 10,
+  );
 
-  if ($form_id == 'user_profile_form' && $_GET['fieldname'] != '') {
-    $fieldname = 'edit-'. preg_replace("/_/", "-", $_GET['fieldname']);
-    drupal_add_js("
-      $('#". $fieldname ."').css({
-        'border' : '2px solid red' 
-        });
-    ", 'inline', 'footer');
-  }
+  return $items;
 }
 
 /**
- * Called when a user submits a profile field form from the 
- * profile module (when adding or editing a profile field).
+ * Implements hook_block_info().
  */
-function pcp_profile_field_form_submit($form, &$form_state) {
-  $fid = $form_state['values']['fid'] ? $form_state['values']['fid'] : db_result(db_query("SELECT MAX(fid) FROM {profile_fields}"));
-  db_query("DELETE FROM {profile_pcp} WHERE fid = %d", $form_state['values']['fid']);
-  if ($form_state['values']['tag']) {
-    db_query("INSERT INTO {profile_pcp} (`fid`) VALUES (%d)", $fid);
-  }
-}
+function pcp_block_info() {
+  $blocks['pcp_profile_percent_complete'] = array(
+    'info' => t('Profile Complete Percentage'),
+  );
 
-/**
- * Called when a user submits the admin user settings form.
- */
-function pcp_user_admin_settings_submit($form, &$form_state) {
-  variable_set('pcp_enable_user_picture', $form_state['values']['pcp_enable_user_picture']);
+  return $blocks;
 }
 
 /**
- * Menu Callback Function
- *  Build output of the pcp module settings which allows the
- *  administrator to tag specific profile fields which will be 
- *  used to determine the completion of a users profile.
+ * Implements hook_block_view().
  */
-function pcp_admin_settings() {
-  $form = drupal_get_form('pcp_admin_settings_form');
-  return $form;
-}
+function pcp_block_view($delta = '') {
+  //The $delta parameter tells us which block is being requested.
+  switch ($delta) {
+    case 'pcp_profile_percent_complete':
+      global $user;
 
-/**
- * Admin settings form
- */
-function pcp_admin_settings_form() {
-  $form = array();
-  $form['general_settings'] = array(
-    '#type' => 'fieldset',
-    '#title' => 'General Settings',
-    );
-  $form['general_settings']['hide_block_on_complete'] = array(
-    '#type' => 'checkbox',
-    '#title' => 'Hide Block When Complete',
-    '#default_value' => variable_get('pcp_hide_block_on_complete', 0),
-    '#description' => t('When a user reaches 100% complete of their profile, do you want the profile complete percent block to go away? If so, check this box on.'),
-    );
-  if (variable_get('user_pictures', 0)) {
-    $form['general_settings']['pcp_enable_user_picture'] = array(
-      '#type' => 'checkbox',
-      '#title' => 'Enable User Picture Support',
-      '#default_value' => variable_get('pcp_enable_user_picture', 0),
-      '#description' => t('Picture support has been turned on, do you want to make it required as part of Profile Complete Percent? Checking this box will make it required.'),
-      );
-  }
-  
-  $form['profile_field_settings'] = array(
-    '#type' => 'fieldset',
-    '#title' => 'Profile Field Settings',
-    '#description' => t('Checking a profile field below will add that field to the logic of the complete percentage.'),
-    );
-  $options = pcp_admin_settings_form_data();
-  if ($options['profile_fields_options']) {
-    $form['profile_field_settings']['profile_fields'] = array(
-      '#title' => t('Profile Fields'),
-      '#type' => 'checkboxes',
-      '#options' => $options['profile_fields_options'],
-      '#default_value' => $options['default_values'],
-      );
-  }
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Save'),
-    );
-  return $form;
-}
+      $out = pcp_get_complete_percentage_data($user);
+      $hide = variable_get('pcp_hide_block_on_complete', 0);
 
-/**
- * Admin settings form submit
- */
-function pcp_admin_settings_form_submit($form, &$form_state) {
-  // Provess the General Settings data
-  variable_set('pcp_hide_block_on_complete', $form_state['values']['hide_block_on_complete']);
-  variable_set('pcp_enable_user_picture', $form_state['values']['pcp_enable_user_picture']);
-  
-  // Process the profile fields that have been modified if available.
-  if (is_array($form_state['values']['profile_fields']) && !empty($form_state['values']['profile_fields'])) {
-    db_query("DELETE FROM {profile_pcp}");
-    foreach ($form_state['values']['profile_fields'] as $fid) {
-      if ($fid) {
-        db_query("INSERT INTO {profile_pcp} VALUES (%d)", $fid);
+      if (($hide && empty($out)) || empty($out)) {
+        $subject = '';
+        $content = '';
+      }
+      else {
+        $subject = t('Profile Complete');
+        $content = theme('pcp_profile_percent_complete', $out);
       }
-    }
-    drupal_set_message("Your settings have been saved.");
-  }
-}
-
-/**
- * Function that sets up parameters to be used 
- * when the pcp_admin_settings_form() function
- * is executed. 
- *
- * @return - assoc array
- *  ['profile_fields_options'] 
- *    - An associative array of all fields created from the profile module.
- *  ['default_values']
- *    - An indexed array of all (if any) default values for the form.
- */
-function pcp_admin_settings_form_data() {
-  $profile_fields_options = array();
-  $default_values = array();
-  $profile_fields = pcp_get_profile_fields();
-  foreach ($profile_fields as $key => $value) {
-    $field_options[$value['fid']] = $value['title'];
-  }
-  $tagged_profile_fields = pcp_get_tagged_profile_fields();
-  foreach ($tagged_profile_fields as $key => $value) {
-    $default_values[] = $value['fid'];
-  }
-  
-  $options = array();
-  $options['profile_fields_options'] = $field_options;
-  $options['default_values'] = $default_values;
-  return $options;
-}
-
-/**
- * Return a users profile field values that have been saved
- * for a given user.
- *
- * @param int $uid - The uid of the user we are returning data for.
- *
- * @return assoc array of all profile fields for the user.
- */
-function pcp_get_user_profile_values($uid) {
-  $values = array();
-  if ($uid) {
-    $query = db_query("SELECT * FROM {profile_values} WHERE uid = %d", $uid);
-    while ($result = db_fetch_array($query)) {
-      $values[$result['fid']] = $result['value'];
-    }
-  }
-  return $values;
-}
 
-/**
- * Return a users additional field values that have been saved
- * for a given user.
- *
- * @param int $uid - The uid of the user we are returning data for.
- *
- * @return assoc array of all profile fields for the user.
- */
-function pcp_get_user_additional_values($uid) {
-  $values = array();
-  if ($uid) {
-    $account = user_load($uid);
-    $values['user_picture'] = $account->picture;
+      $block['subject'] = $subject;
+      $block['content'] = $content;
+      break;
   }
-  
-  return $values;
+  return $block;
 }
 
 /**
  * Get the profile complete percentage data for a given user.
  *
- * @param obj $user 
-*  - The user object to get data for.
- *  
- * @return assoc array of all values needed at the theme layer.
- *  - Refer to comments in theme_pcp_profile_percent_complete() for specific values.
+ * @param $user
+ *   User object
+ *
+ * @return
+ *   PCP data array.
  */
 function pcp_get_complete_percentage_data($user) {
+  $entity_fields = field_info_instances('user', 'user');
+
   $profile_fields = pcp_get_tagged_profile_fields();
   $additional_fields = pcp_get_additional_tagged_fields();
   $fields = $profile_fields + $additional_fields;
+
   $user_profile_values = pcp_get_user_profile_values($user->uid);
   $user_additional_values = pcp_get_user_additional_values($user->uid);
   $user_values = $user_profile_values + $user_additional_values;
 
-  $percent = 0;
-  $complete = 0;
-  $nextfield_set = FALSE;
-
-  if (is_array($fields) && !empty($fields)) {
-    
-    //Shuffle the fields to display a random field to fill in (issue #708382)
-    shuffle($fields);
-    
-    foreach ($fields as $key => $value) {
-      if (($user_values[$value['fid']] == '') || ($user_values[$value['fid']] == '0' && $value['type'] == 'selection')) {
-        if ($nextfield_set === FALSE) {
-          $nextfield_set = TRUE;
-          $fid = $value['fid'];
-          $nextfield_fid = $fid;
-          
-          if (is_numeric($fid)) {
-            $nextdata = db_fetch_array(db_query("SELECT title, name, category FROM {profile_fields} WHERE fid = %d", $fid));
-            $nextfield = $nextdata['title'];
-            $nextcategory = $nextdata['category'];
-            $nextname = $nextdata['name'];
-          }
-          elseif ($fid == 'user_picture') {
-            $nextfield = $value['title'];
-            $nextcategory = 'Default';
-            $nextname = $value['name'];
-          }
-        }
-        continue;
-      }
-      $complete++;
+  $data = array();
+
+  // Enumerate empty fields
+  $empty_fields = array();
+  foreach ($fields as $key => $field) {
+    if (empty($user_values[$key])) {
+      $empty_fields[$key] = $field;
     }
-    $dec = number_format(($complete / count($fields)), 2);
-    $percent = $dec * 100;
-    if ($nextfield_set) {
-      $next = number_format((($complete + 1) / count($fields)), 2);
-      $nextpercent = $next * 100;
+  }
+
+  // If there is one empty field or more
+  if ($empty_fields) {
+    $data['uid'] = $user->uid;
+
+    // Pick up a random field, we won't use suffle because it reindex the array
+    // keys
+    $fid = array_rand($empty_fields);
+
+    if (is_numeric($fid)) {
+      $field_title = $entity_fields[$fields[$fid]]['label'];
+      $field_name = $fields[$fid];
+    }
+    elseif ($fid == 'user_picture') {
+      $field_title = $fields[$fid]['title'];
+      $field_name = $fields[$fid]['name'];
     }
+
+    $data['nextfield_id'] = $fid;
+    $data['nextfield_title'] = $field_title;
+    $data['nextfield_name'] = $field_name;
   }
-  
-  $complete_data = array();
-  $complete_data['uid'] = $user->uid;
-  $complete_data['percent'] = $percent;
-  $complete_data['completed']  = $complete;
-  $complete_data['incomplete'] = count($fields) - $complete;
-  $complete_data['total'] = count($fields);
-  $complete_data['nextfield'] = $nextfield;
-  $complete_data['nextfield_fid'] = $nextfield_fid;
-  $complete_data['nextpercent'] = $nextpercent;
-  $complete_data['nextcategory'] = $nextcategory;
-  $complete_data['nextname'] = $nextname;
-  
-  return $complete_data;
+
+  $fields_count = count($fields);
+  $empty_fields_count = count($empty_fields);
+
+  $completed_fields = $fields_count - $empty_fields_count;
+  $current_percent = number_format(($completed_fields / $fields_count), 2) * 100;
+  $next_percent = number_format((($completed_fields + 1) / $fields_count), 2) * 100;
+
+  $data['completed']  = $completed_fields;
+  $data['incomplete'] = $empty_fields_count;
+  $data['total'] = $fields_count;
+
+  $data['current_percent'] = $current_percent;
+  $data['next_percent'] = $next_percent;
+
+  return $data;
 }
 
 /**
- * Get all the profile fields that have been tagged.
- * If an $fid is passed in, only the data for that field will be returned.
- *
- * @param int $fid - The fid of the field data should be returned for. If 0, all fields are returned.
- *
- * @return indexed array - All fields and their information returned from the query.
+ * Get all the profile fields stored in the system, tagged or not tagged.
  */
-function pcp_get_tagged_profile_fields($fid = 0) {  
-  if ($fid > 0) {
-    $query = db_query("SELECT * FROM {profile_pcp} LEFT JOIN {profile_fields} USING(fid) WHERE fid = %d", array($fid));
-  }
-  else{
-    $query = db_query("SELECT * FROM {profile_pcp} LEFT JOIN {profile_fields} USING(fid)");
+function pcp_get_profile_fields($options = NULL) {
+  $params = array('entity_type' => 'user');
+
+  if ($options) {
+    $params = array_merge($params, $options);
   }
-  
+
+  $instances = field_read_instances($params);
   $fields = array();
-  while ($result = db_fetch_array($query)) {
-    $fields[] = $result;
+  foreach ($instances as $instance) {
+    $fields[$instance['field_id']] = $instance['field_name'];
   }
-  
+
   return $fields;
 }
 
 /**
- * Get all the profile fields stored in the system, tagged or not tagged.
+ * Get all the profile fields that have been tagged.
+ * If an $fid is passed in, only the data for that field will be returned.
+ *
+ * @param $fid
+ *   The fid of the field data should be returned for. If null, all fields
+ *   are returned.
+ *
+ * @return
+ *   field(s) ID and name.
  */
-function pcp_get_profile_fields() {
-  $query = db_query("SELECT * FROM {profile_fields}");
+function pcp_get_tagged_profile_fields($fid = null) {
+  $query = db_select('field_config_instance', 'fci');
+  $query->addField('fci', 'field_id', 'fid');
+  $query->addField('fci', 'field_name', 'field_name');
+  $query->join('profile_pcp', 'pcp', 'pcp.fid = fci.field_id');
+
+  if (isset($fid)) {
+    $query->condition('fid', $fid);
+  }
+
+  $result = $query->execute();
+
   $fields = array();
-  while ($result = db_fetch_array($query)) {
-    $fields[] = $result;
+  foreach ($result as $row) {
+    $fields[$row->fid] = $row->field_name;
   }
-  
+
   return $fields;
 }
 
 /**
- * Get all other fields stored in the system that where not
- * created by the profile module. This allows additional PCP
+ * Get all other fields stored in the system that where not created by
+ * the profile module. This allows additional PCP
  * support for other drupal features.
  */
 function pcp_get_additional_fields() {
   $fields = array();
-  
+
   // Enable user picture support.
   $fields['user_picture'] = array(
     'title' => 'User Picture',
     'name' => 'picture_upload',
-    );
-  
-  // TODO:
-  // Module invoke all on a specific hook that
-  // will allow third party module to inject their own
-  // pcp functionality. Coming in future feature release.
+  );
+
   return $fields;
 }
 
@@ -412,196 +239,67 @@ function pcp_get_additional_fields() {
  */
 function pcp_get_additional_tagged_fields() {
   $fields = array();
-  
+
   // Enable user picture support.
   if (variable_get('pcp_enable_user_picture', 0)) {
     $fields['user_picture'] = array(
       'fid' => 'user_picture',
       'title' => 'User Picture',
       'name' => 'picture_upload',
-      );
+    );
   }
-  
-  return $fields;
-}
-
 
-function pcp_theme() {
-  return array(
-    'pcp_profile_percent_complete' => array(
-      'arguments' => array('complete_data' => NULL),
-    ),
-  );
+  return $fields;
 }
 
 /**
- * Block Theme function that displays the default output of a users
- * profile complete percent. Use this theme function to override
- * the output / display of this block.
+ * Return a users profile field values that have been saved
+ * for a given user.
  *
- * @param - assoc array $complete_data
- *  ['uid'] - int - The user ID of the user viewing the page.
- *  ['percent'] - int - A number that represents the total percent complete (ie 50).
- *  ['completed'] - int - How many fields total that have been completed (filled out) by $user.
- *  ['incomplete'] - int - How many fields still need to be filled out.
- *  ['total'] - int - The count of all tagged profile fields.
- *  ['nextfield'] - str - The next field to fill out that is currently empty.
- *  ['nextfield_fid'] - int or str - The fid identifier of the field being passed in. 
- *  ['nextpercent'] - int - What the users total percent complete value will be when ['nextfield'] is complete.
- *  ['nextcategory] - str - The category the next field falls under for targeting with a link.
- *  ['nextname'] - str - The field name of the next field for field focus after linked to the profile field.
- */
-function theme_pcp_profile_percent_complete($complete_data) {
-  $output = '<style type="text/css">';
-  $output .= '#pcp-percent-bar-wrapper {width: 100%; border: 1px solid #000; padding: 1px;}';
-  $output .= '#pcp-percent-bar { width: '. $complete_data['percent'] .'%; height: 10px; background-color: #777777;}';
-  $output .= '</style>';
-  
-  $output .= '<div id="pcp-wrapper">';
-  $output .= t('!complete% Complete', array('!complete' => $complete_data['percent']));
-  $output .= '<div id="pcp-percent-bar-wrapper">';
-  $output .= '<div id="pcp-percent-bar"></div>';
-  $output .= '</div>';
-  $output .= '</div>';
-  
-  if ($complete_data['nextfield'] && $complete_data['nextpercent']) {
-    if (is_numeric($complete_data['nextfield_fid'])) {
-      $nextpath = 'user/'. $complete_data['uid'] .'/edit/'. $complete_data['nextcategory'];
-    }
-    
-    if ($complete_data['nextfield_fid'] == 'user_picture') {
-      $nextpath = 'user/'. $complete_data['uid'] .'/edit';
-    }
-    $output .= t('Filling out <em>!empty-field</em> will bring your profile to !complete% Complete', array('!empty-field' => l($complete_data['nextfield'], $nextpath, array('query' => 'fieldname='. $complete_data['nextname'])), '!complete' => $complete_data['nextpercent']));
-  }
-    
-  return $output;
-}
-
-/**
- * Implementation of hook_rules_condition_info().
- * 
- * @ingroup rules
- * @see content_complete_completeness_percentage_form
+ * @param int $uid - The uid of the user we are returning data for.
+ *
+ * @return assoc array of all profile fields for the user.
  */
-function pcp_rules_condition_info() {
-  return array(
-    'pcp_condition_check_profile_fields_completeness' => array(
-      'label' => t('Check profile fields completeness'),
-      'arguments' => array(
-        'user' => array('type' => 'user', 'label' => t('User')),
-      ),
-      'help' => 'Evaluates to TRUE, if all the the selected user profile fields are filled in.',
-      'module' => 'Profile Complete Percent',
-    ),
-  );
-}
+function pcp_get_user_profile_values($uid) {
+  $fields = field_info_instances('user', 'user');
+  $user = user_load($uid);
+  $user_fields = array();
 
-/**
- * Rules Condition form configuration - select the profile fields to check
- * 
- * @see pcp_rules_condition_info
- */
-function pcp_condition_check_profile_fields_completeness_form($settings = array(), &$form) {
-
-  $options = pcp_admin_settings_form_data();
-  $form['settings']['profile_fields'] = array(
-    '#title' => t('Profile Fields'),
-    '#type' => 'checkboxes',
-    '#options' => $options['profile_fields_options'],
-    '#default_value' => isset($settings['profile_fields']) ? $settings['profile_fields'] : array(),
-    '#description' => t('Check which fields the user has to fill in'),
-    );
-}
+  foreach ($fields as $field) {
+    $fid = $field['field_id'];
+    $user_fields[$fid] = $user->$field['field_name'];
+  }
 
-/**
- * Rules Condition - Are the selected user profile fields filed in?
- * 
- * @param $user 
- *   The user for which the condition is checked.
- * @param $settings
- *   The configured settings of the rule condition
- * 
- * @see pcp_condition_check_profile_fields_completeness_form
- * @see pcp_rules_condition_info
- * 
- * @return TRUE or FALSE
- */
-function pcp_condition_check_profile_fields_completeness($user, $settings) {
-  $edit_categories = array();
-  profile_load_profile($user);
-  if ($settings['profile_fields']) {
-    $result = db_query("SELECT fid, title, name, category FROM {profile_fields} WHERE fid IN (%d)", implode(', ', $settings['profile_fields']));
-    while ($field = db_fetch_object($result)) {
-      if (!$user->{$field->name}) {
-        return FALSE;    
-      }
-    }
-  } 
-  return TRUE;
+  return $user_fields;
 }
 
 /**
- * Implementation of hook_rules_action_info
+ * Return a users additional field values that have been saved
+ * for a given user.
+ *
+ * @param int $uid - The uid of the user we are returning data for.
+ *
+ * @return assoc array of all profile fields for the user.
  */
-function pcp_rules_action_info() {
-  return array(
-    'pcp_action_redirect_user_to_editform' => array(
-      'label' => t('Redirect the user to his own profile edit page'),
-      'arguments' => array(
-        'user' => array('type' => 'user', 'label' => t('User whos profile fields should be filled in')),
-      ),
-      'module' => 'Profile Complete Percent',
-    ),
-  );
-}
-
-/**
- * Rules Action - Redirect the user to his profile edit page
- * 
- * @param $user 
- *   The user for which the action is performed.
- * @param $settings
- *   The configured settings of the rule action
- * 
- * @see pcp_rules_action_info
- * 
- * @return Perform the redirection
- */
-function pcp_action_redirect_user_to_editform($user, $settings) {
-  $settings = pcp_get_condition_settings('pcp_condition_check_profile_fields_completeness');
-  $edit_categories = array();
-  profile_load_profile($user);
-  if ($settings['profile_fields']) {
-    $result = db_query("SELECT fid, title, name, category FROM {profile_fields} WHERE fid IN (%d)", implode(', ', $settings['profile_fields']));
-    while ($field = db_fetch_object($result)) {
-      if (!$user->{$field->name}) {
-        drupal_set_message(t('Please fill in "<i>' . $field->category . '</i>".'), 'warning');
-
-        // Necessary to avoid redirect loop or malfunctioning
-        unset($_REQUEST['destination']);
-        unset($_REQUEST['edit']['destination']);
-        drupal_goto('user/' . $user->uid . '/edit/' . $field->category);
-      }
-    }
-  } 
+function pcp_get_user_additional_values($uid) {
+  $values = array();
+  if ($uid) {
+    $account = user_load($uid);
+    $values['user_picture'] = $account->picture;
+  }
+  return $values;
 }
 
 /**
- * Helper function to get the setting of a particular condition
+ * Process variables for pcp-block.tpl.php.
  *
- * @param Condition name $name
- * @return Array settings
+ * @see pcp-block.tpl.php
  */
-function pcp_get_condition_settings($name) {
-  $rules = rules_get_configured_items('rules');
-  foreach ($rules as $rule) {
-    if ($rule['#conditions']) {
-      foreach ($rule['#conditions'] as $condition) {
-        if ($condition['#name'] == $name) {
-          return $condition['#settings'];
-        }
-      }
-    }
+function template_preprocess_pcp_profile_percent_complete(&$variables) {
+  if (isset($variables['nextfield_name'])) {
+    $input_name = 'edit-' . str_replace('_', '-', $variables['nextfield_name']);
+    $user_edit_path = 'user/'. $variables['uid'] .'/edit';
+
+    $variables['next_path'] = url($user_edit_path, array('absolute' => TRUE, 'fragment' => $input_name));
   }
-}
\ No newline at end of file
+}
