-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSettings.pm
More file actions
279 lines (208 loc) · 8.01 KB
/
Settings.pm
File metadata and controls
279 lines (208 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package WeBWorK3::Utils::Settings;
use strict;
use warnings;
use feature 'signatures';
no warnings qw/experimental::signatures/;
use YAML::XS qw/LoadFile/;
require Exporter;
use base qw/Exporter/;
our @EXPORT_OK = qw/checkSettings getDefaultCourseSettings getDefaultCourseValues
mergeCourseSettings validateSettingsConfFile validateCourseSettings
validateSingleCourseSetting validateSettingConfig
isInteger isTimeString isTimeDuration isDecimal/;
use Exception::Class qw(
DB::Exception::UndefinedCourseField
DB::Exception::InvalidCourseField
DB::Exception::InvalidCourseFieldType
);
use WeBWorK3;
my @allowed_fields = qw/var category subcategory doc doc2 default type options/;
my @required_fields = qw/var doc type default/;
=head1 loadDefaultCourseSettings
load the default settings from the conf/course_settings.yaml file
=cut
sub getDefaultCourseSettings () {
return LoadFile(Mojo::Home->new->detect('WeBWorK3')->child('conf', 'course_defaults.yml'));
}
my @course_setting_categories = qw/email optional general permissions problem problem_set/;
=head1 getDefaultCourseValues
getDefaultCourseValues returns the values of all default course values and returns
it as a hash of categories/variables
=cut
sub getDefaultCourseValues () {
my $course_defaults = getDefaultCourseSettings(); # The full default course settings
my $all_settings = {};
for my $category (@course_setting_categories) {
$all_settings->{$category} = {};
my @settings = grep { $_->{category} eq $category } @$course_defaults;
for my $setting (@settings) {
$all_settings->{$category}->{ $setting->{var} } = $setting->{default};
}
}
return $all_settings;
}
=head1 mergeCourseSettings
mergeCourseSettings takes in two settings and merges them in the following way:
For each course setting in the first argument (typically from the configuration file)
1. If a value in the second argument is present use that else
2. use the value from the first argument
=cut
sub mergeCourseSettings ($settings, $settings_to_update) {
my $updated_settings = {};
# Merge the non-optional categories.
for my $category (@course_setting_categories) {
$updated_settings->{$category} = {};
my @fields = keys %{ $settings->{$category} };
push(@fields, keys %{ $settings_to_update->{$category} });
for my $key (@fields) {
# Use the value in $settings_to_update if it exists, if not use the other.
$updated_settings->{$category}->{$key} =
$settings_to_update->{$category}->{$key} || $settings->{$category}->{$key};
}
}
return $updated_settings;
}
=pod
checkSettingsConfFile loads the course settings configuration file and checks for validity
=cut
sub validateSettingsConfFile () {
#my @all_settings = getDefaultCourseSettings();
for my $setting (@{ getDefaultCourseSettings() }) {
validateSettingConfig($setting);
}
return 1;
}
=pod
isValidCourseSettings checks if the course settings are valid including
=over
=item the key is defined in the course setting configuration file
=item the value is appropriate for the given setting.
=back
=cut
sub flattenCourseSettings ($settings) {
my @flattened_settings = ();
for my $category (keys %$settings) {
for my $var (keys %{ $settings->{$category} }) {
push(
@flattened_settings,
{
var => $var,
category => $category,
value => $settings->{$category}->{$var}
}
);
}
}
return \@flattened_settings;
}
sub validateCourseSettings ($course_settings) {
$course_settings = flattenCourseSettings($course_settings);
my $default_course_settings = getDefaultCourseSettings();
for my $setting (@$course_settings) {
validateSingleCourseSetting($setting, $default_course_settings);
}
return 1;
}
sub validateSingleCourseSetting ($setting, $default_course_settings) {
my @default_setting = grep { $_->{var} eq $setting->{var} } @$default_course_settings;
DB::Exception::UndefinedCourseField->throw(message => qq/The course setting $setting->{var} is not valid/)
unless scalar(@default_setting) == 1;
validateSetting($setting);
return 1;
}
=pod
This checks the variable name (to ensure it is in kebob case)
=cut
sub kebobCase ($in) {
return $in =~ /^[a-z][a-z_\d]*[a-z\d]$/;
}
=head1 validateSettingsConfig
This checks the configuration for a single setting is valid. This includes
=over
=item Check that the variable name is kebob case
=item Ensure that all fields passed in are valid
=item Ensure that all require fields are present
=item Checks that the default value is appropriate for the type
=back
=cut
my @valid_types = qw/text list multilist boolean integer decimal time date_time time_duration timezone/;
sub validateSettingConfig ($setting) {
# Check that the variable name is kebobCase.
DB::Exception::InvalidCourseField->throw(message => "The variable name $setting->{var} must be in kebob case")
unless kebobCase($setting->{var});
# Check that each of the setting fields is allowed.
for my $field (keys %$setting) {
my @fields = grep { $_ eq $field } @allowed_fields;
DB::Exception::InvalidCourseField->throw(
message => "The field: $field is not an allowed field of the setting $setting->{var}")
if scalar(@fields) == 0;
}
# Check that each of the required fields is present in the setting.
for my $field (@required_fields) {
my @fields = grep { $_ eq $field } (keys %$setting);
DB::Exception::InvalidCourseField->throw(
message => "The field: $field is a required field for the setting $setting->{var}")
if scalar(@fields) == 0;
}
my @type = grep { $_ eq $setting->{type} } @valid_types;
DB::Exception::InvalidCourseFieldType->throw(
message => "The setting type: $setting->{type} is not valid for variable: $setting->{var}")
unless scalar(@type) == 1;
return validateSetting($setting);
}
sub validateSetting ($setting) {
my $value = $setting->{default} || $setting->{value};
return 0 if !defined $setting->{type};
if ($setting->{type} eq 'list') {
validateList($setting);
} elsif ($setting->{type} eq 'time') {
DB::Exception::InvalidCourseFieldType->throw(
message => qq/The default for variable $setting->{var} which is $value must be a time value/)
unless isTimeString($setting->{default});
} elsif ($setting->{type} eq 'integer') {
DB::Exception::InvalidCourseFieldType->throw(
message => qq/The default for variable $setting->{var} which is $value must be an integer/)
unless isInteger($setting->{default});
} elsif ($setting->{type} eq 'decimal') {
DB::Exception::InvalidCourseFieldType->throw(
message => qq/The default for variable $setting->{var} which is $value must be a decimal/)
unless isDecimal($setting->{default});
} elsif ($setting->{type} eq 'time_duration') {
DB::Exception::InvalidCourseFieldType->throw(
message => qq/The default for variable $setting->{var} which is $value must be a time duration/)
unless isTimeDuration($setting->{default});
}
return 1;
}
sub validateList ($setting) {
DB::Exception::InvalidCourseFieldType->throw(
message => qq/The options field for the type list in $setting->{var} is missing/)
unless defined($setting->{options});
DB::Exception::InvalidCourseFieldType->throw(
message => qq/The options field for $setting->{var} is not an ARRAYREF/)
unless ref($setting->{options}) eq 'ARRAY';
# See if the $setting->{options} is an arrayref of strings or hashrefs.
my @opt =
(ref($setting->{options}->[0]) eq 'HASH')
? grep { $_ eq $setting->{default} } map { $_->{value} } @{ $setting->{options} }
: grep { $_ eq $setting->{default} } @{ $setting->{options} };
DB::Exception::InvalidCourseFieldType->throw(
message => qq/The default for variable $setting->{var} needs to be one of the given options/)
unless scalar(@opt) == 1;
return 1;
}
sub isInteger ($in) {
return $in =~ /^-?\d+$/;
}
# Test for a 24-hour time string
sub isTimeString ($in) {
return $in =~ /(^0?\d:[0-5]\d$)|(^1\d:[0-5]\d$)|(^2[0-3]:[0-5]\d$)/;
}
# Test for a time duration which can have the unit: sec, min, day, week, hr, hour
sub isTimeDuration ($in) {
return $in =~ /^(\d+)\s(sec|second|min|minute|day|week|hr|hour)s?$/i;
}
sub isDecimal ($in) {
return $in =~ /(^-?\d+(\.\d+)?$)|(^-?\.\d+$)/;
}
1;