|
|
1 | ################################################################################ |
|
|
2 | # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project |
|
|
3 | # $Id$ |
|
|
4 | ################################################################################ |
|
|
5 | |
| 1 | package WeBWorK::CourseEnvironment; |
6 | package WeBWorK::CourseEnvironment; |
|
|
7 | |
|
|
8 | =head1 NAME |
|
|
9 | |
|
|
10 | WeBWorK::CourseEnvironment - Read configuration information from global.conf |
|
|
11 | and course.conf files. |
|
|
12 | |
|
|
13 | =cut |
| 2 | |
14 | |
| 3 | use strict; |
15 | use strict; |
| 4 | use warnings; |
16 | use warnings; |
| 5 | use Safe; |
17 | use Safe; |
|
|
18 | use WeBWorK::Utils qw(readFile); |
| 6 | |
19 | |
| 7 | # new($invocant, $webworkRoot, $courseName) |
20 | # new($invocant, $webworkRoot, $courseName) |
| 8 | # $invocant implicitly set by caller |
21 | # $invocant implicitly set by caller |
| 9 | # $webworkRoot directory that contains the WeBWorK distribution |
22 | # $webworkRoot directory that contains the WeBWorK distribution |
| 10 | # $courseName name of the course being used |
23 | # $courseName name of the course being used |
| 11 | sub new { |
24 | sub new { |
| 12 | my $invocant = shift; |
25 | my $invocant = shift; |
| 13 | my $class = ref($invocant) || $invocant; |
26 | my $class = ref($invocant) || $invocant; |
| 14 | my $webworkRoot = shift; |
27 | my $webworkRoot = shift; |
|
|
28 | my $webworkURLRoot = shift; |
| 15 | my $courseName = shift; |
29 | my $courseName = shift; |
| 16 | my $safe = Safe->new; |
30 | my $safe = Safe->new; |
| 17 | |
31 | |
| 18 | # set up some defaults that the environment files will need |
32 | # set up some defaults that the environment files will need |
| 19 | $safe->reval("\$webworkRoot = '$webworkRoot'"); |
33 | $safe->reval("\$webworkRoot = '$webworkRoot'"); |
|
|
34 | $safe->reval("\$webworkURLRoot = '$webworkURLRoot'"); |
| 20 | $safe->reval("\$courseName = '$courseName'"); |
35 | $safe->reval("\$courseName = '$courseName'"); |
| 21 | |
36 | |
| 22 | # determine location of globalEnvironmentFile |
37 | # determine location of globalEnvironmentFile |
| 23 | my $globalEnvironmentFile = "$webworkRoot/conf/global.conf"; |
38 | my $globalEnvironmentFile = "$webworkRoot/conf/global.conf"; |
| 24 | |
39 | |
| … | |
… | |
| 69 | |
84 | |
| 70 | bless $self, $class; |
85 | bless $self, $class; |
| 71 | return $self; |
86 | return $self; |
| 72 | } |
87 | } |
| 73 | |
88 | |
| 74 | sub hash2string { |
89 | 1; |
| 75 | my $hr = shift; |
|
|
| 76 | my $indent = shift || 0; |
|
|
| 77 | my $result; |
|
|
| 78 | foreach (keys %$hr) { |
|
|
| 79 | $result .= "\t"x$indent . "{$_} ="; |
|
|
| 80 | if (ref $hr->{$_} eq 'HASH') { |
|
|
| 81 | $result .= "\n"; |
|
|
| 82 | $result .= hash2string($hr->{$_}, $indent+1); |
|
|
| 83 | } elsif (ref $hr->{$_} eq 'ARRAY') { |
|
|
| 84 | $result .= "\n"; |
|
|
| 85 | $result .= array2string($hr->{$_}, $indent+1); |
|
|
| 86 | } else { |
|
|
| 87 | $result .= " " . $hr->{$_} . "\n"; |
|
|
| 88 | } |
|
|
| 89 | } |
|
|
| 90 | return $result; |
|
|
| 91 | } |
|
|
| 92 | |
90 | |
| 93 | sub array2string { |
91 | __END__ |
| 94 | my $ar = shift; |
|
|
| 95 | my $indent = shift || 0; |
|
|
| 96 | my $result; |
|
|
| 97 | foreach (0 .. @$ar-1) { |
|
|
| 98 | $result .= "\t"x$indent . "[$_] ="; |
|
|
| 99 | if (ref $ar->[$_] eq 'HASH') { |
|
|
| 100 | $result .= "\n"; |
|
|
| 101 | $result .= hash2string($ar->[$_], $indent+1); |
|
|
| 102 | } elsif (ref $ar->[$_] eq 'ARRAY') { |
|
|
| 103 | $result .= "\n"; |
|
|
| 104 | $result .= array2string($ar->[$_], $indent+1); |
|
|
| 105 | } else { |
|
|
| 106 | $result .= " " . $ar->[$_] . "\n"; |
|
|
| 107 | } |
|
|
| 108 | } |
|
|
| 109 | return $result; |
|
|
| 110 | } |
|
|
| 111 | |
92 | |
| 112 | # ----- utils ----- |
93 | =head1 SYNOPSIS |
| 113 | |
94 | |
| 114 | sub readFile { |
95 | use WeBWorK::CourseEnvironment; |
| 115 | my $fileName = shift; |
96 | $courseEnv = WeBWorK::CourseEnvironment->new($webworkRoot, $courseName); |
| 116 | open INPUTFILE, "<", $fileName |
97 | |
| 117 | or die "Couldn't open environment file $fileName: $!"; |
98 | $timeout = $courseEnv->{sessionKeyTimeout}; |
| 118 | my $result = join "\n", <INPUTFILE>; |
99 | $mode = $courseEnv->{pg}->{options}->{displayMode}; |
| 119 | close INPUTFILE; |
100 | # etc... |
| 120 | return $result; |
|
|
| 121 | } |
|
|
| 122 | |
101 | |
| 123 | 1; |
102 | =head1 DESCRIPTION |
|
|
103 | |
|
|
104 | The WeBWorK::CourseEnvironment module reads the system-wide F<global.conf> and |
|
|
105 | course-specific F<course.conf> files used by WeBWorK to calculate and store |
|
|
106 | settings needed throughout the system. The F<.conf> files are perl source files |
|
|
107 | that can contain any code allowed under the default safe compartment opset. |
|
|
108 | After evaluation of both files, any package variables are copied out of the |
|
|
109 | safe compartment into a hash. This hash becomes the course environment. |
|
|
110 | |
|
|
111 | =head1 CONSTRUCTION |
|
|
112 | |
|
|
113 | =over |
|
|
114 | |
|
|
115 | =item new (ROOT, COURSE) |
|
|
116 | |
|
|
117 | The C<new> method finds the file F<conf/global.conf> relative to the given ROOT |
|
|
118 | directory. After reading this file, it uses the C<$courseFiles{environment}> |
|
|
119 | variable, if present, to locate the course environment file. If found, the file |
|
|
120 | is read and added to the environment. |
|
|
121 | |
|
|
122 | =back |
|
|
123 | |
|
|
124 | =head1 ACCESS |
|
|
125 | |
|
|
126 | There are no formal accessor methods. However, since the course environemnt is |
|
|
127 | a hash of hashes and arrays, is exists as the self hash of an instance |
|
|
128 | variable: |
|
|
129 | |
|
|
130 | $courseEnvironment->{someKey}->{someOtherKey}; |
|
|
131 | |
|
|
132 | =head1 AUTHOR |
|
|
133 | |
|
|
134 | Written by Sam Hathaway, sh002i (at) math.rochester.edu. |
|
|
135 | |
|
|
136 | =cut |