[system] / trunk / webwork2 / lib / WeBWorK / Utils.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork2/lib/WeBWorK/Utils.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 818 - (view) (download) (as text)

1 : sh002i 440 ################################################################################
2 : sh002i 494 # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project
3 : sh002i 440 # $Id$
4 :     ################################################################################
5 :    
6 : sh002i 410 package WeBWorK::Utils;
7 : sh002i 818 use base qw(Exporter);
8 : sh002i 410
9 : sh002i 455 =head1 NAME
10 :    
11 :     WeBWorK::Utils - useful utilities used by other WeBWorK modules.
12 :    
13 :     =cut
14 :    
15 : sh002i 412 use strict;
16 :     use warnings;
17 :     use Date::Format;
18 :     use Date::Parse;
19 :    
20 : sh002i 410 our @EXPORT = ();
21 : sh002i 424 our @EXPORT_OK = qw(
22 :     runtime_use
23 :     readFile
24 :     formatDateTime
25 :     parseDateTime
26 : sh002i 562 writeLog
27 :     writeTimingLogEntry
28 : sh002i 427 dbDecode
29 :     dbEncode
30 : sh002i 429 decodeAnswers
31 :     encodeAnswers
32 : sh002i 424 ref2string
33 : sh002i 646 dequoteHere
34 : sh002i 667 wrapText
35 : sh002i 424 );
36 : sh002i 410
37 :     sub runtime_use($) {
38 :     return unless @_;
39 : sh002i 424 eval "package Main; require $_[0]; import $_[0]";
40 : sh002i 410 die $@ if $@;
41 :     }
42 :    
43 :     sub readFile($) {
44 :     my $fileName = shift;
45 : sh002i 558 local *INPUTFILE;
46 : sh002i 410 open INPUTFILE, "<", $fileName
47 :     or die "Failed to read $fileName: $!";
48 :     local $/ = undef;
49 :     my $result = <INPUTFILE>;
50 :     close INPUTFILE;
51 :     return $result;
52 :     }
53 : sh002i 412
54 :     sub formatDateTime($) {
55 :     my $dateTime = shift;
56 : sh002i 558 # "standard" WeBWorK date/time format (for set definition files):
57 : sh002i 412 # %m month number, starting with 01
58 :     # %d numeric day of the month, with leading zeros (eg 01..31)
59 :     # %y year (2 digits)
60 :     # %I hour, 12 hour clock, leading 0's)
61 :     # %M minute, leading 0's
62 :     # %P am or pm (Yes %p and %P are backwards :)
63 : sh002i 562 return time2str("%m/%d/%y %I:%M%P", $dateTime);
64 : sh002i 412 }
65 :    
66 :     sub parseDateTime($) {
67 : sh002i 424 my $string = shift;
68 : sh002i 737 return str2time($string);
69 : sh002i 412 }
70 : sh002i 422
71 : sh002i 562 sub writeLog($$@) {
72 :     my ($ce, $facility, @message) = @_;
73 :     unless ($ce->{webworkFiles}->{logs}->{$facility}) {
74 :     warn "There is no log file for the $facility facility defined.\n";
75 :     return;
76 :     }
77 :     my $logFile = $ce->{webworkFiles}->{logs}->{$facility};
78 :     local *LOG;
79 :     if (open LOG, ">>", $logFile) {
80 :     print LOG "[", time2str("%a %b %d %H:%M:%S %Y", time), "] @message\n";
81 :     close LOG;
82 :     } else {
83 :     warn "failed to open $logFile for writing: $!";
84 :     }
85 :     }
86 : sh002i 558
87 : sh002i 631 # $ce - a WeBWork::CourseEnvironment object
88 :     # $function - fully qualified function name
89 :     # $details - any information, do not use the characters '[' or ']'
90 : sh002i 692 # $beginEnd - the string "begin", "intermediate", or "end"
91 :     # use the intermediate step begun or completed for INTERMEDIATE
92 : sh002i 631 # use an empty string for $details when calling for END
93 : sh002i 562 sub writeTimingLogEntry($$$$) {
94 :     my ($ce, $function, $details, $beginEnd) = @_;
95 :     return unless defined $ce->{webworkFiles}->{logs}->{timing};
96 : sh002i 692 $beginEnd = ($beginEnd eq "begin") ? ">" : ($beginEnd eq "end") ? "<" : "-";
97 : sh002i 562 writeLog($ce, "timing", "$$ ".time." $beginEnd $function [$details]");
98 :     }
99 :    
100 : sh002i 429 # -----
101 :    
102 : sh002i 427 sub dbDecode($) {
103 :     my $string = shift;
104 :     return unless defined $string and $string;
105 :     my %hash = $string =~ /(.*?)(?<!\\)=(.*?)(?:(?<!\\)&|$)/g;
106 : sh002i 429 $hash{$_} =~ s/\\(&|=)/$1/g foreach keys %hash; # unescape & and =
107 : sh002i 427 return %hash;
108 :     }
109 :    
110 :     sub dbEncode(@) {
111 :     my %hash = @_;
112 :     my $string;
113 :     foreach (keys %hash) {
114 :     $hash{$_} = "" unless defined $hash{$_}; # promote undef to ""
115 :     $hash{$_} =~ s/(=|&)/\\$1/g; # escape & and =
116 :     $string .= "$_=$hash{$_}&";
117 :     }
118 :     chop $string; # remove final '&' from string for old code :p
119 :     return $string;
120 :     }
121 :    
122 : sh002i 429 sub decodeAnswers($) {
123 :     my $string = shift;
124 :     return unless defined $string and $string;
125 :     my @array = split m/##/, $string;
126 :     $array[$_] =~ s/\\#\\/#/g foreach 0 .. $#array;
127 : sh002i 445 push @array, "" if @array%2;
128 : sh002i 429 return @array; # it's actually a hash ;)
129 :     }
130 :    
131 :     sub encodeAnswers(\%\@) {
132 :     my %hash = %{ shift() };
133 :     my @order = @{ shift() };
134 :     my $string;
135 :     foreach my $name (@order) {
136 :     my $value = defined $hash{$name} ? $hash{$name} : "";
137 :     $name =~ s/#/\\#\\/g; # this is a WEIRD way to escape things
138 :     $value =~ s/#/\\#\\/g; # and it's not my fault!
139 :     $string .= "$name##$value##"; # this is also not my fault
140 :     }
141 :     $string =~ s/##$//; # remove last pair of hashs
142 :     return $string;
143 :     }
144 :    
145 : sh002i 424 # -----
146 : sh002i 422
147 : sh002i 424 sub ref2string($;$);
148 :     sub ref2string($;$) {
149 :     my $ref = shift;
150 :     my $dontExpand = shift || {};
151 :     my $refType = ref $ref;
152 : sh002i 422 my $result;
153 : sh002i 424 if ($refType and not $dontExpand->{$refType}) {
154 :     my $baseType = refBaseType($ref);
155 :     $result .= '<font size="1" color="grey">' . $refType;
156 : sh002i 425 $result .= " ($baseType)" if $baseType and $refType ne $baseType;
157 : sh002i 424 $result .= ":</font><br>";
158 :     $result .= '<table border="1" cellpadding="2">';
159 :     if ($baseType eq "HASH") {
160 :     my %hash = %$ref;
161 :     foreach (sort keys %hash) {
162 :     $result .= '<tr valign="top">';
163 :     $result .= "<td>$_</td>";
164 :     $result .= "<td>" . ref2string($hash{$_}, $dontExpand) . "</td>";
165 :     $result .= "</tr>";
166 :     }
167 :     } elsif ($baseType eq "ARRAY") {
168 :     my @array = @$ref;
169 : sh002i 429 # special case for Problem, Set, and User objects, which are defined
170 :     # using lists and contain a @FIELDS package variable:
171 :     no strict 'refs';
172 :     my @FIELDS = eval { @{$refType."::FIELDS"} };
173 :     use strict 'refs';
174 :     undef @FIELDS unless scalar @FIELDS == scalar @array and not $@;
175 : sh002i 424 foreach (0 .. $#array) {
176 :     $result .= '<tr valign="top">';
177 :     $result .= "<td>$_</td>";
178 : sh002i 429 $result .= "<td>".$FIELDS[$_]."</td>" if @FIELDS;
179 : sh002i 424 $result .= "<td>" . ref2string($array[$_], $dontExpand) . "</td>";
180 :     $result .= "</tr>";
181 :     }
182 :     } elsif ($baseType eq "SCALAR") {
183 :     my $scalar = $$ref;
184 :     $result .= '<tr valign="top">';
185 :     $result .= "<td>$scalar</td>";
186 :     $result .= "</tr>";
187 : sh002i 422 } else {
188 : sh002i 424 # perhaps a coderef? in any case, i don't feel like dealing with it!
189 :     $result .= '<tr valign="top">';
190 :     $result .= "<td>$ref</td>";
191 :     $result .= "</tr>";
192 : sh002i 422 }
193 : sh002i 424 $result .= "</table>"
194 : sh002i 422 } else {
195 : sh002i 424 $result .= defined $ref ? $ref : '<font color="red">undef</font>';
196 :     }
197 : sh002i 422 }
198 :    
199 : sh002i 424 sub refBaseType($) {
200 :     my $ref = shift;
201 :     local $SIG{__DIE__} = 'IGNORE';
202 :     return "HASH" if eval { $_ = %$ref; 1 };
203 :     return "ARRAY" if eval { $_ = @$ref; 1 };
204 :     return "SCALAR" if eval { $_ = $$ref; 1 };
205 :     return 0;
206 : sh002i 422 }
207 :    
208 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9