NAME

WeBWorK::Utils::FormatRecords - utilities for formatting database records as strings.

SYNOPSIS

use WeBWorK::Utils::FormatRecords qw/getFormatsForClass/;

    # Get a list of label => format pairs.  The $default_format will be marked
    # as the format that will be selected by default if provided.  Otherwise the
    # first format will be marked as selected by default.
my $formats = getFormatsForClass(ref $Users[0], $default_formats);

use WeBWorK::Utils::FormatRecords qw/formatRecords/;

# Start with an array of database records
my @records = $db->getUsers($db->listUsers);

    # Get a list of label => id pairs.  The labels are formatted according to
    # the provided $preset_format.
my $formattedRecords = formatRecords('lnfn_uid', @records);

DESCRIPTION

This module provides record formatting functions, and a collection of preset formats for the standard WeBWorK record classes. Formats are specified by a list of field names and an sprintf format string.

The return values of these methods are suitable for passing as the second value argument to the Mojolicious select_field tag helper method.

FUNCTIONS

getFormatsForClass($class, $default_format)

Given the name of a record class, returns the preset formats available for that class.

The return value is a reference to a list of two element lists. The first element in each list is a a string description of a format name and the second element is the format name. The return value is suitable for passing as the second value argument to the Mojolicious select_field tag helper method.

If the $default_format is provided then that format will be marked as the default selected format. Otherwise the first format will be marked as the default selected format.

formatRecords($preset_format, @records)

Given the name of a preset format and an array of database records, returns a reference to a list of two element lists. The first element in each list is a formatted string representing the record, and the second element is a string that is obtained by joining the key fields of the database record with exclamation marks.

The $preset_format must be provided. It must either be one of the presets defined above, or the name of a field in the database record class.