Best practice forum (Archived)

Report builder - checkboxes

 
Swtizerland 2004
Report builder - checkboxes
by Russell England - Tuesday, 11 October 2011, 2:19 AM
 

Hi there

I have some custom fields for a user that are checkboxes - but when they are displayed in a report they are appearing as 1's and 0's.

Is there a way to display the nice checkboxes from the user profile or Y / N instead?

Thanks, Russ

Swtizerland 2004
[SOLVED] Re: Report builder - checkboxes
by Russell England - Wednesday, 12 October 2011, 9:43 AM
 

Solved with a couple of modifications to the following functions in 

/local/reportbuilder/classes/rb_base_source.php

    /**
     * Adds any user profile fields to the $columnoptions array
     *
     * @param array &$columnoptions Array of current column options
     *                              Passed by reference and updated if
     *                              any user custom fields exist
     * @return boolean True if user custom fields exist
     */
    protected function add_user_custom_fields_to_columns(&$columnoptions) {
        // auto-generate columns for each user custom field
        if($custom_fields =
            get_records('user_info_field')) {
            foreach($custom_fields as $custom_field) {
                $name = $custom_field->name;
                $key = "user_$custom_field->id";
				$options = array('joins' => $key);
				// Modded by Russell England to check if the custom field is a checkbox
				// If it is then display Y/N
				if ($custom_field->datatype == 'checkbox') {
					$options['displayfunc'] = 'yes_no';
				}
                $columnoptions[] = new rb_column_option(
                    'user_profile',
                    $key,
                    $name,
                    "$key.data",
                    $options
                );
            }
            return true;
        }
        return false;
    }


    /**
     * Adds any user profile fields to the $filteroptions array as text filters
     *
     * @param array &$filteroptions Array of current filter options
     *                              Passed by reference and updated if
     *                              any user custom fields exist
     * @return boolean True if user custom fields exist
     */
    protected function add_user_custom_fields_to_filters(&$filteroptions) {
        if($custom_fields = get_records('user_info_field','','','','id,shortname,name,datatype')) {
            foreach($custom_fields as $custom_field) {
                $name = $custom_field->name;
                $key = "user_$custom_field->id";
				// Modded by Russell England to display a yes/no list if its a checkbox
				if ($custom_field->datatype=='checkbox') {
					$datatype = 'simpleselect';
					$options['selectchoices'] = array(
						'1' => get_string('yes'), '0' => get_string('no'));

				}
				else {
					// These are the defaults
					$datatype='text';
					$options=null;
				}
				$filteroptions[] = new rb_filter_option(
					'user_profile',
					$key,
					$name,
					$datatype,
					$options
				);
            }
            return true;
        }
        return false;
    }

and in /local/reportbuilder/filters/simpleselect.php

    /**
     * Returns the condition to be used with SQL where
     * @param array $data filter settings
     * @return string the filtering condition or null if the filter is disabled
     */
    function get_sql_filter($data) {
        $value    = addslashes($data['value']);
        $query    = $this->_filter->get_field();

        if ($value == '') {
            // return 1=1 instead of TRUE for MSSQL support
            return ' 1=1 ';
        }
		// Modded by Russell England - if its a user_profile field then the data will always be text
		// This code has been modified because of checkbox custom fields - these are stored as '1' or '0'
		// A simpleselect is being used for a yes/no field as a filter in
		// add_user_custom_fields_to_filters() in local/reportbuilder/classes/rb_base_source.php
		// The datatype for a dropdown box is 'simpleselect' rather than 'text'
		// We can't assume that all simpleselect datatypes are strings, they could be integers
		// but we can assume that all user_profile types are strings because the data type is varchar
        if (($this->_attributes['datatype'] == 'text') || ($this->_filter->type=='user_profile')){
            return "$query = '$value'";
        } else {
            return "$query = $value";
        }
    }
Simon Coggins
Re: [SOLVED] Re: Report builder - checkboxes
by Simon Coggins - Thursday, 13 October 2011, 2:09 PM
Group Totara

Hi Russell,

Thanks for that - we have actually fixed this properly in the case of hierarchy custom fields (e.g. competency/position/organisation). See add_custom_fields_for() in rb_base_source.php. Each custom field type is properly handed and the appropriate filter is provided.

I've been meaning to port the fixes back to user and course custom fields but haven't got to it yet.

Simon