/** * Actions metabox used for primary filtering purposes * * * @uses CRM_User_List_Table class * @since 0.01 * */ function actions($wp_list_table) { ?> <div class="misc-pub-section"> <?php $wp_list_table->search_box('Search', 'wp_crm_text_search'); ?> <?php $wp_list_table->views(); ?> </div> <div class="major-publishing-actions"> <div class="other-action"> <span class="wp_crm_subtle_link wp_crm_toggle" toggle="wp_crm_user_actions"><?php _e('Show Actions', 'wp_crm'); ?> </span> </div> <div class="publishing-action"> <?php submit_button(__('Filter Results'), 'button', false, false, array('id' => 'search-submit')); ?> </div> <br class='clear' /> </div> <div class="wp_crm_user_actions hidden"> <ul class="wp_crm_action_list"> <li class="wp_crm_orange_link wp_crm_export_to_csv"><?php _e('Export to CSV', 'wp_crm'); ?> </li> <?php if (WP_CRM_F::get_quantifiable_attributes()) { ?> <li class="wp_crm_orange_link wp_crm_visualize_results"><?php _e('Visualize User Data', 'wp_crm'); ?> </li> <?php } ?> <?php do_action('wp_crm_user_actions'); ?> </ul> </div> <?php }
/** * Actions metabox used for primary filtering purposes * * * @uses CRM_User_List_Table class * @since 0.01 * */ static function actions($wp_list_table) { ?> <div class="misc-pub-section"> <?php $wp_list_table->search_box('Search', 'wp_crm_text_search'); ?> <?php $wp_list_table->views(); ?> </div> <div class="major-publishing-actions"> <div class="publishing-action"> <?php submit_button(__('Filter Results', 'wp_crm'), 'button', false, false, array('id' => 'search-submit')); ?> </div> <br class="clear" /> </div> <div class="wp_crm_user_actions"> <ul class="wp_crm_action_list"> <li class="button wp_crm_export_to_csv"><?php _e('Export to CSV', 'wp_crm'); ?> </li> <?php if (WP_CRM_F::get_quantifiable_attributes()) { ?> <li class="button wp_crm_visualize_results"><?php _e('Visualize User Data', 'wp_crm'); ?> </li> <?php } ?> <?php do_action('wp_crm_user_actions'); ?> </ul> </div> <div id="performed_actions"> <h3><?php _e('Perfomed actions', 'wp_crm'); ?> </h3> <div class="wp_crm_quick_report_wrapper"></div> </div> <?php }
/** * Visualize quantifiable data * * @todo There may be an issue with overlapping attributes for certain users, as unaccounted_for sometimes results in a negative number * @since 0.19 * */ static function visualize_results($filters) { global $wpdb; parse_str($filters, $filters); $wp_crm_search = $filters['wp_crm_search']; //** Get users from filter query */ $user_ids = WP_CRM_F::user_search($wp_crm_search, array('ids_only' => 'true')); $quantifiable_attributes = WP_CRM_F::get_quantifiable_attributes(); if (!$quantifiable_attributes || !$user_ids) { return; } $user_id_query = ' user_id = ' . implode(' OR user_id = ', $user_ids); foreach ($quantifiable_attributes as $attribute_slug => $attribute) { if (!empty($attribute['option_keys'])) { foreach ($attribute['option_keys'] as $short_slug => $full_meta_key) { $this_count = $wpdb->get_var("SELECT count(DISTINCT(user_id)) FROM {$wpdb->usermeta} WHERE meta_key = '{$full_meta_key}' AND ({$user_id_query})"); if (empty($this_count)) { continue; } $data[$attribute_slug]['counts'][$short_slug] = $this_count; $data[$attribute_slug]['labels'][$short_slug] = $attribute['option_labels'][$short_slug]; } } else { $this_count = $wpdb->get_var("SELECT count(DISTINCT(user_id)) FROM {$wpdb->usermeta} WHERE meta_key = '{$attribute_slug}' AND ({$user_id_query})"); if (empty($this_count)) { continue; } $data[$attribute_slug]['counts'][$short_slug] = $this_count; $data[$attribute_slug]['labels'][$short_slug] = $attribute['title']; } $data[$attribute_slug]['title'] = $attribute['title']; if (empty($data[$attribute_slug]['counts'])) { unset($data[$attribute_slug]); } else { //** Calculate "other" */ $unaccounted_for = count($user_ids) - array_sum($data[$attribute_slug]['counts']); if ($unaccounted_for > 0) { $data[$attribute_slug]['counts']['unaccounted_for'] = $unaccounted_for; $data[$attribute_slug]['labels']['unaccounted_for'] = __('Unaccounted', 'wp_crm'); } } } if (empty($data)) { die('<div class="wp_crm_visualize_results no_data">' . __('There is not enough quantifiable data to generate any graphs.', 'wp_crm') . '</div>'); } ?> <div class="wp_crm_visualize_results"> <script type="text/javascript"> jQuery(document).ready(function() { <?php foreach ($data as $attribute_slug => $attribute_data) { echo "wp_crm_attribute_{$attribute_slug}_chart();\r\n"; } ?> }); <?php foreach ($data as $attribute_slug => $attribute_data) { ?> function wp_crm_attribute_<?php echo $attribute_slug; ?> _chart() { var data = new google.visualization.DataTable({}); data.addColumn('string', 'Attribute'); data.addColumn('number', 'Count'); data.addRows(<?php echo count($attribute_data['counts']); ?> ); <?php $row = 0; foreach ($attribute_data['counts'] as $short_slug => $count) { ?> data.setValue(<?php echo $row; ?> , 0, '<?php echo $attribute_data['labels'][$short_slug]; ?> '); data.setValue(<?php echo $row; ?> , 1, <?php echo $count; ?> ); <?php $row++; } ?> var chart = new google.visualization.PieChart(document.getElementById('wp_crm_attribute_<?php echo $attribute_slug; ?> _chart')); chart.draw(data, { backgroundColor: '#F7F7F7', is3D: true, chartArea: {width: "60%", height: "90%"}, width: 380, height: 310, legend: 'bottom' }); } <?php } ?> </script> <?php foreach ($data as $attribute_slug => $attribute_data) { ?> <div class="wp_crm_chart_wrapper"> <span class="wp_crm_chart_title"><?php echo $attribute_data['title']; ?> </span> <div id="wp_crm_attribute_<?php echo $attribute_slug; ?> _chart" class="wp_crm_visualization_graph"></div> </div> <?php } ?> </div> <?php }
/** * Get a value of a user. * * Ideally an attribute key should be passed, in which case the first value will be returned by default. * If a full meta key is passed, the value will be returned. * If a value of an option is passed, a boolean will be returned depending on if the option is enabled for the user. * * @since 0.1 * */ function wp_crm_get_value($meta_key, $user_id = false, $args = '') { global $current_user, $wp_crm; $args = wp_parse_args($args, array('return' => 'value', 'concat_char' => ', ', 'meta_key' => $meta_key, 'option_key' => '')); if (!$user_id) { $user_id = $current_user->ID; } $quantifiable_attributes = array_keys(WP_CRM_F::get_quantifiable_attributes()); //** Check if meta key exists as key and as label */ $attributes = $wp_crm['data_structure']['attributes']; $full_meta_keys = $wp_crm['data_structure']['full_meta_keys']; $meta_keys = $wp_crm['data_structure']['meta_keys']; //* If passed key is an attribute key (as intended) */ if (is_array($attributes) && in_array($meta_key, array_keys($attributes))) { $args['attribute_key'] = $meta_key; } //* If passed meta_key is actually a label name */ if (is_array($meta_keys) && in_array($meta_key, $meta_keys)) { $meta_value_flip = array_flip($meta_keys); $args['attribute_key'] = $meta_value_flip[$meta_key]; $meta_key = $args['attribute_key']; } //* If a full meta key is passed (for pre-defined values) */ if (is_array($full_meta_keys) && in_array($meta_key, array_keys($full_meta_keys))) { $args['full_meta_key'] = $meta_key; $args['attribute_key'] = $full_meta_keys[$meta_key]; } //** If all else fails, use the passed $meta_key (it may be a main user table key) */ if (empty($args['attribute_key'])) { $args['attribute_key'] = $meta_key; } //** Get the full attribute data once we have the attribute_key */ $attribute = WP_CRM_F::get_attribute($args['attribute_key']); //* Make sure we have a user object */ if (!empty($user_id) && is_numeric($user_id)) { $user_object = (array) wp_crm_get_user($user_id); } //** If we have the full meta key then we can get value easily from get_user_meta() */ if (!empty($args['full_meta_key']) && $args['full_meta_key'] != $args['attribute_key']) { $args['value'] = get_user_meta($user_id, $args['full_meta_key'], true); } else { //** Attribute has options, we return the label of the option */ if (!empty($attribute['has_options'])) { if ($attribute['input_type'] == 'text' || $attribute['input_type'] == 'textarea' || $attribute['input_type'] == 'date') { //** Try to get value by option key. */ $option_key = !empty($args['option_key']) ? $args['option_key'] : (is_array($attribute['option_keys']) ? key($attribute['option_keys']) : false); if (!empty($option_key)) { $args['value'] = get_user_meta($user_id, $args['attribute_key'] . '_option_' . $option_key, true); if (!empty($args['value'])) { $args['label'] = $meta_keys[$args['attribute_key'] . '_option_' . $option_key]; $args['return_option_label'] = true; $args['value'] .= ', ' . $args['label']; } } if (empty($args['value'])) { $args['value'] = WP_CRM_F::get_first_value(!empty($user_object[$args['attribute_key']]) ? $user_object[$args['attribute_key']] : array()); } } else { $options = WP_CRM_F::list_options($user_object, $args['attribute_key']); if (is_array($options)) { $args['value'] = implode($args['concat_char'], $options); } } } else { $args['value'] = WP_CRM_F::get_first_value(!empty($user_object[$args['attribute_key']]) ? $user_object[$args['attribute_key']] : array()); } } //** Check if this should be a boolean response */ if ((empty($args['return_option_label']) || !$args['return_option_label']) && in_array($args['attribute_key'], $quantifiable_attributes)) { if (!empty($args['value']) && $args['value'] == 'on') { $args['value'] = true; } } switch ($args['return']) { case 'value': $result = !empty($args['value']) ? $args['value'] : ''; break; case 'detail': $result = $args; break; default: $result = $args[$args['return']]; break; } return $result; }