WeBWorK::Utils - General utility methods.
Usage: runtime_use($module, @import_list)
This is like use, except it happens at runtime. The module name must be quoted, and a comma after it if an import list is specified. Also, to specify an empty import list (as opposed to no import list) use an empty array reference instead of an empty array.
The following demonstrates equivalent usage of runtime_use
to that of use
.
use Xyzzy; => runtime_use 'Xyzzy';
use Foo qw(pine elm); => runtime_use 'Foo', qw(pine elm);
use Foo::Bar (); => runtime_use 'Foo::Bar', [];
Usage: trim_spaces($string)
Returns a string with whitespace trimmed from the start and end of $string
.
Usage: fix_newlines($string)
Converts carriage returns followed by new lines into just a new line. In other words, converts non-unix like new lines into unix new lines.
Usage: encodeAnswers($hash, $order)
Give a reference to a hash whose keys are answer names and values are student answers in $hash
, and a reference to an array of answer names in the order the answers appear in the problem in $order
, this returns a JSON encoded array of answer name/student answer pairs where the pairs appear in the array in the order provided by $order
.
Usage: decodeAnswers($serialized)
Returns an array of answers decoded from the given $serialized
string.
This method attempts to detect if $serialized
is a JSON encoded array, or is a Storable::nfreeze encoded hash (the old method), and decodes using the appropriate method.
Usage: encode_utf8_base64($in)
UTF-8 encodes, and then base 64 endcodes the input and returns the result.
Usage: decode_utf8_base64($in)
Base 64 decodes, and then UTF-8 decodes the input and returns the result.
Usage: nfreeze_base64($in)
This Storable::nfreeze
encodes and then base 64 encodes $in
and returns the result.
Usage: thaw_base64($in)
This base 64 decodes and then Storable::thaw
decodes $in
and returns result.
Usage: min(@items)
Return the minimum element in @items
.
Usage: max(@items)
Return the maximum element in @items
.
Usage: wwRound($places, $float)
Returns $float
rounded to $places
decimal places.
Usage: cryptPassword($clearPassword)
Returns the crypted form of $clearPassword
using a random 16 character salt.
Usage: undefstr($default, @values)
Returns a copy of @values
whose undefined entries are replaced with $default
.
Usage: sortByName($field, @items)
If $field
is a string naming a single field, then this returns the elements in @items
sorted by that field.
If $field
is a reference to an array of strings each naming a field, then this returns a the entries of @items
sorted first by the first name field, then by second, etc.
A natural sort algorithm is used for sorting, i.e., numeric parts are sorted numerically, and alphabetic parts sorted lexicographically.
Usage: sortAchievements(@achievements)
Returns @achievements
sorted first by achievement id, then by number or category (if the achievement does not have a number).
Usage: not_blank($str)
Returns true if $str
is defined and does not consist entirely of white space.
Usage: role_and_above($userRoles, $role)
Given a reference to a hash $userRoles
whose keys are roles and values are permission levels, returns a reference to an array of roles that are at or above that of the permission level of the role specified in $role
.
Usage: fetchEmailRecipients($c, $permissionType, $sender)
Given a WeBWorK::ContentGenerator
object $c
and permission type $permissionType
, this returns a list of feedback email recipients for the course. If $sender
is provided, then this list is filtered by the section of that sender.
Usage: processEmailMessage($text, $user_record, $STATUS, $merge_data, $for_preview)
Process the email message in $text
and replace macros with values from the $user_record
, the $STATUS
, and $merge_data
. If $for_prevew
is true then the result is formatted to be display in HTML.
The replaceable macros and what they will be replaced with are
$SID => $user_record->student_id
$FN => $user_record->first_name
$LN => $user_record->last_name
$SECTION => $user_record->section
$RECITATION => $user_record->recitation
$EMAIL => $user_record->email_address
$LOGIN => $user_record->user_id
$STATUS => $STATUS
$COL[n] => nth column of $merge_data
$COL[-1] => last column of $merge_data
Usage: createEmailSenderTransportSMTP($ce)
This returns an Email::Sender::Transport::SMTP
object for use in sending emails. A valid WeBWorK::CourseEnvironment
object must be provided in $ce
.
Usage: generateURLs($c, %params)
The parameter $c
must be a WeBWorK::Controller
object.
The following optional parameters may be passed:
A problem set name.
Problem id of a problem in the set.
This should a string with the value 'relative' or 'absolute' to return a single URL, or undefined to return an array containing both URLs this subroutine could be expanded to.
Usage: getAssetURL($ce, $file, $isThemeFile)
Returns the URL for the asset specified in $file
. If $isThemeFile
is true, then the asset will be assumed to be located in a theme directory. The parameter $ce
must be a valid WeBWorK::CourseEnvironment
object.
Usage: x(@args)
This is a dummy function used to mark constant strings for localization. It just returns @args
.