LLMS_Admin_Reporting::output_widget( array $args = array() )
Outputs the HTML for a reporting widget.
Parameters Parameters
- $args
-
(array) (Optional) Array of widget options and data to be displayed.<br>
- 'id'
(string) Required. A unique identifier for the widget.<br> - 'text'
(string) A short description of the widget's data.<br> - 'data'
(int|string|float) The value of the data to display.<br> - 'data_compare'
(int|string|float) Additional data to compare $data against.<br> - 'data_type'
(string) The type of data. Used to format displayed data. Accepts "numeric", "monetary", "text", "percentage", or "date".<br> - 'icon'
(string) An optional Font Awesome icon used to help visually identify the widget.<br> If supplied, should be supplied without thefa-icon prefix.<br> - 'impact'
(string) The type of impact the data has, either "positive" or "negative". This is used when displaying comparisons to determine if the change was a positive change or negative change. For example: student enrollments has a positive impact while quiz failures has a negative impact. An increase in enrollments will be displayed in green while a decrease will be displayed in red. An increase in quiz failures will be displayed in red while a decrease will be displayed in green.<br> - 'cols'
(string) Grid class widget width ID. See: assets/scss/admin/partials/_grid.scss.<br>
Default value: array()
- 'id'
Return Return
(void)
Source Source
File: includes/admin/reporting/class.llms.admin.reporting.php
public static function output_widget( $args = array() ) {
$args = self::get_output_widget_args( $args );
// Only these data types can make comparisons.
$can_compare = in_array( $args['data_type'], array( 'numeric', 'monetary', 'percentage' ), true );
// Adds a percentage symbol after data.
$data_after = 'percentage' === $args['data_type'] && is_numeric( $args['data'] ) ? '<sup>%</sup>' : '';
$change = false;
$compare_operator = '';
$compare_class = '';
$compare_title = '';
$floating_precision = llms_get_floats_rounding_precision();
if ( $can_compare && $args['data_compare'] && floatval( $args['data'] ) ) {
$change = round( ( $args['data'] - $args['data_compare'] ) / $args['data'] * 100, $floating_precision );
$compare_operator = ( $change <= 0 ) ? '' : '+';
$compare_title = sprintf(
// Translators: %s = The value of the data from the previous data set.
esc_attr__( 'Previously %s', 'lifterlms' ),
round( $args['data_compare'], $floating_precision ) . wp_strip_all_tags( $data_after )
);
$compare_class = ( $change <= 0 ) ? 'negative' : 'positive';
if ( 'negative' === $args['impact'] ) {
$compare_class = ( $change <= 0 ) ? 'positive' : 'negative';
}
}
if ( is_numeric( $args['data'] ?? '' ) ) {
if ( 'percentage' === $args['data_type'] ) {
$args['data'] = round( $args['data'], $floating_precision );
} elseif ( 'monetary' === $args['data_type'] ) {
$args['data'] = llms_price( $args['data'] );
$args['data_compare'] = llms_price_raw( $args['data_compare'] );
}
}
$args['id'] = esc_attr( $args['id'] );
include LLMS_PLUGIN_DIR . 'includes/admin/views/reporting/widget.php';
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 7.3.0 | Better rounding of float values of percentage data types. |
| 6.11.0 | Moved HTML into a view file.<br> Fixed division by zero error encountered during data comparisons when $data is 0.<br> Added a check to ensure only numeric, monetary, or percentage data types will generate comparison data. |
| 3.31.0 | Remove redundant if statement. |
| 3.15.0 | Introduced. |