|
|
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 |
| … | |
… | |
| 69 | |
82 | |
| 70 | bless $self, $class; |
83 | bless $self, $class; |
| 71 | return $self; |
84 | return $self; |
| 72 | } |
85 | } |
| 73 | |
86 | |
| 74 | sub hash2string { |
87 | 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 | |
88 | |
| 93 | sub array2string { |
89 | __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 | |
90 | |
| 112 | # ----- utils ----- |
91 | =head1 SYNOPSIS |
| 113 | |
92 | |
| 114 | sub readFile { |
93 | use WeBWorK::CourseEnvironment; |
| 115 | my $fileName = shift; |
94 | $courseEnv = WeBWorK::CourseEnvironment->new($webworkRoot, $courseName); |
| 116 | open INPUTFILE, "<", $fileName |
95 | |
| 117 | or return; #die "Couldn't open environment file $fileName: $!"; |
96 | $timeout = $courseEnv->{sessionKeyTimeout}; |
| 118 | my $result = join "\n", <INPUTFILE>; |
97 | $mode = $courseEnv->{pg}->{options}->{displayMode}; |
| 119 | close INPUTFILE; |
98 | # etc... |
| 120 | return $result; |
|
|
| 121 | } |
|
|
| 122 | |
99 | |
| 123 | 1; |
100 | =head1 DESCRIPTION |
|
|
101 | |
|
|
102 | The WeBWorK::CourseEnvironment module reads the system-wide F<global.conf> and |
|
|
103 | course-specific F<course.conf> files used by WeBWorK to calculate and store |
|
|
104 | settings needed throughout the system. The F<.conf> files are perl source files |
|
|
105 | that can contain any code allowed under the default safe compartment opset. |
|
|
106 | After evaluation of both files, any package variables are copied out of the |
|
|
107 | safe compartment into a hash. This hash becomes the course environment. |
|
|
108 | |
|
|
109 | =head1 CONSTRUCTION |
|
|
110 | |
|
|
111 | =over |
|
|
112 | |
|
|
113 | =item new (ROOT, COURSE) |
|
|
114 | |
|
|
115 | The C<new> method finds the file F<conf/global.conf> relative to the given ROOT |
|
|
116 | directory. After reading this file, it uses the C<$courseFiles{environment}> |
|
|
117 | variable, if present, to locate the course environment file. If found, the file |
|
|
118 | is read and added to the environment. |
|
|
119 | |
|
|
120 | =back |
|
|
121 | |
|
|
122 | =head1 ACCESS |
|
|
123 | |
|
|
124 | There are no formal accessor methods. However, since the course environemnt is |
|
|
125 | a hash of hashes and arrays, is exists as the self hash of an instance |
|
|
126 | variable: |
|
|
127 | |
|
|
128 | $courseEnvironment->{someKey}->{someOtherKey}; |
|
|
129 | |
|
|
130 | =head1 AUTHOR |
|
|
131 | |
|
|
132 | Written by Sam Hathaway, sh002i (at) math.rochester.edu. |
|
|
133 | |
|
|
134 | =cut |