? add_rules_support.patch
Index: pcp.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/pcp/pcp.module,v
retrieving revision 1.2
diff -u -p -r1.2 pcp.module
--- pcp.module	8 Dec 2008 16:55:55 -0000	1.2
+++ pcp.module	6 Feb 2009 21:43:46 -0000
@@ -326,4 +326,133 @@ function theme_pcp_profile_percent_compl
   }
     
   return $output;
+}
+
+/**
+ * Implementation of hook_rules_condition_info().
+ * 
+ * @ingroup rules
+ * @see content_complete_completeness_percentage_form
+ */
+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',
+    ),
+  );
+}
+
+/**
+ * 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'),
+    );
+}
+
+/**
+ * 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 (%s)", implode(', ', $settings['profile_fields']));
+		while ($field = db_fetch_object($result)) {
+			if (!$user->{$field->name}) {
+				return FALSE;		
+			}
+		}
+	} 
+	return TRUE;
+}
+
+/**
+ * Implementation of hook_rules_action_info
+ */
+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 (%s)", 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);
+			}
+		}
+	} 
+}
+
+/**
+ * Helper function to get the setting of a particular condition
+ *
+ * @param Condition name $name
+ * @return Array settings
+ */
+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'];
+				}
+			}
+		}
+	}
 }
\ No newline at end of file
