Skip to content

Commit b7fd7df

Browse files
authored
Merge pull request #107 from drdrew42/user-set-cleanup
Fixing User Sets
2 parents 2a9c342 + cbc8dd6 commit b7fd7df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1046
-1272
lines changed

bin/import_ww2_db.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ ($=)
446446
say 'Removing problem '
447447
. $problem->problems->problem_number
448448
. ' from '
449-
. $problem->user_sets->problem_sets->set_name
449+
. $problem->user_sets->problem_set->set_name
450450
if $verbose;
451451
$problem->delete;
452452
}

lib/DB/Schema/Result/Attempt.pm

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package DB::Schema::Result::Attempt;
22
use DBIx::Class::Core;
3-
use DB::WithParams;
43

54
use strict;
65
use warnings;
76

8-
use base qw(DBIx::Class::Core DB::WithParams);
7+
use base qw/DBIx::Class::Core DB::Validation/;
98

109
=head1 DESCRIPTION
1110
@@ -56,19 +55,6 @@ Note: a problem should have only one of a library_id, problem_path or problem_po
5655
5756
=cut
5857

59-
sub valid_params {
60-
return {
61-
timestamp => q{[1-9]d+},
62-
scores => q{.*},
63-
answer => q{.*},
64-
comment => q{.*}
65-
};
66-
}
67-
68-
sub required_params {
69-
return {};
70-
}
71-
7258
### this is the table that stores problems for a given Problem Set
7359

7460
__PACKAGE__->table('attempt');
@@ -79,38 +65,37 @@ __PACKAGE__->add_columns(
7965
attempt_id => {
8066
data_type => 'integer',
8167
size => 16,
82-
is_nullable => 0,
8368
is_auto_increment => 1,
8469
},
8570
user_problem_id => {
86-
data_type => 'integer',
87-
size => 16,
88-
is_nullable => 0,
71+
data_type => 'integer',
72+
size => 16,
73+
8974
},
9075
# store scores as a JSON object
9176
scores => {
9277
data_type => 'text',
9378
size => 256,
94-
is_nullable => 0,
9579
default_value => '{}',
80+
retrieve_on_insert => 1,
9681
serializer_class => 'JSON',
9782
serializer_options => { utf8 => 1 }
9883
},
9984
# store answers as a JSON object
10085
answers => {
10186
data_type => 'text',
10287
size => 256,
103-
is_nullable => 0,
10488
default_value => '{}',
89+
retrieve_on_insert => 1,
10590
serializer_class => 'JSON',
10691
serializer_options => { utf8 => 1 }
10792
},
10893
# store comments as a JSON object
10994
comments => {
11095
data_type => 'text',
11196
size => 256,
112-
is_nullable => 0,
11397
default_value => '{}',
98+
retrieve_on_insert => 1,
11499
serializer_class => 'JSON',
115100
serializer_options => { utf8 => 1 }
116101
}
@@ -120,4 +105,13 @@ __PACKAGE__->set_primary_key('attempt_id');
120105

121106
__PACKAGE__->belongs_to(user_problem => 'DB::Schema::Result::UserProblem', 'user_problem_id');
122107

108+
sub valid_params {
109+
return {
110+
timestamp => q{[1-9]d+},
111+
scores => q{.*},
112+
answer => q{.*},
113+
comment => q{.*}
114+
};
115+
}
116+
123117
1;

lib/DB/Schema/Result/Course.pm

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package DB::Schema::Result::Course;
2-
use base qw/DBIx::Class::Core/;
32
use strict;
43
use warnings;
4+
use feature 'signatures';
5+
no warnings qw/experimental::signatures/;
56

7+
use base qw/DBIx::Class::Core DB::Validation/;
68
use Mojo::JSON qw/true false/;
79

810
=head1 DESCRIPTION
@@ -33,11 +35,6 @@ C<visible>: a boolean on whether the course is visible or not.
3335
3436
=cut
3537

36-
our @VALID_DATES = qw/open end/;
37-
our @REQUIRED_DATES = qw//;
38-
our $VALID_PARAMS = { visible => q{[01]} };
39-
our $REQUIRED_PARAMS = { _ALL_ => ['visible'] };
40-
4138
__PACKAGE__->table('course');
4239

4340
__PACKAGE__->load_components(qw/InflateColumn::Serializer InflateColumn::Boolean Core/);
@@ -46,23 +43,20 @@ __PACKAGE__->add_columns(
4643
course_id => {
4744
data_type => 'integer',
4845
size => 16,
49-
is_nullable => 0,
5046
is_auto_increment => 1,
5147
},
5248
course_name => {
53-
data_type => 'text',
54-
is_nullable => 0,
49+
data_type => 'text',
5550
},
5651
course_dates => {
5752
data_type => 'text',
58-
is_nullable => 0,
5953
default_value => '{}',
54+
retrieve_on_insert => 1,
6055
serializer_class => 'JSON',
6156
serializer_options => { utf8 => 1 }
6257
},
6358
visible => {
6459
data_type => 'boolean',
65-
is_nullable => 0,
6660
default_value => 1,
6761
retrieve_on_insert => 1
6862
}
@@ -83,4 +77,53 @@ __PACKAGE__->has_many(problem_pools => 'DB::Schema::Result::ProblemPool', 'cours
8377
# set up the one-to-one relationship to course settings;
8478
__PACKAGE__->has_one(course_settings => 'DB::Schema::Result::CourseSettings', 'course_id');
8579

80+
=head2 C<valid_fields>
81+
82+
subroutine that returns a hash of the valid fields for json columns
83+
84+
=cut
85+
86+
sub valid_fields ($, $field_name) {
87+
if ($field_name eq 'course_dates') {
88+
return {
89+
open => q{\d+},
90+
end => q{\d+}
91+
};
92+
} elsif ($field_name eq 'course_params') {
93+
return { visible => 'bool' };
94+
} else {
95+
return {};
96+
}
97+
}
98+
99+
=head2 C<additional_validation>
100+
101+
subroutine that checks json columns for consistency
102+
103+
=cut
104+
105+
sub additional_validation ($course, $field_name) {
106+
return 1 if ($field_name ne 'course_dates');
107+
108+
my $dates = $course->get_inflated_column('course_dates');
109+
DB::Exception::ImproperDateOrder->throw(message => 'The course dates are not in order')
110+
unless $dates->{end} > $dates->{open};
111+
112+
return 1;
113+
}
114+
115+
=head2 C<required>
116+
117+
subroutine that returns a hashref describing the required fields in JSON columns
118+
119+
=cut
120+
121+
sub required ($, $field_name) {
122+
if ($field_name eq 'set_dates') {
123+
return { '_ALL_' => [ 'open', 'end' ] };
124+
} else {
125+
return {};
126+
}
127+
}
128+
86129
1;

lib/DB/Schema/Result/CourseSettings.pm

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ C<email>: a JSON object that stores email settings
4747
4848
=cut
4949

50-
our @VALID_DATES = qw/open end/;
51-
our @REQUIRED_DATES = qw//;
52-
our $VALID = {
53-
institution => q{.*},
54-
visible => q{[01]}
55-
};
56-
our $REQUIRED = { _ALL_ => ['visible'] };
57-
5850
__PACKAGE__->table('course_settings');
5951

6052
__PACKAGE__->load_components('InflateColumn::Serializer', 'Core');
@@ -63,59 +55,57 @@ __PACKAGE__->add_columns(
6355
course_settings_id => {
6456
data_type => 'integer',
6557
size => 16,
66-
is_nullable => 0,
6758
is_auto_increment => 1,
6859
},
6960
course_id => {
70-
data_type => 'integer',
71-
size => 16,
72-
is_nullable => 0,
61+
data_type => 'integer',
62+
size => 16,
7363
},
7464
general => {
7565
data_type => 'text',
7666
size => 256,
77-
is_nullable => 0,
7867
default_value => '{}',
68+
retrieve_on_insert => 1,
7969
serializer_class => 'JSON',
8070
serializer_options => { utf8 => 1 }
8171
},
8272
optional => {
8373
data_type => 'text',
8474
size => 256,
85-
is_nullable => 0,
8675
default_value => '{}',
76+
retrieve_on_insert => 1,
8777
serializer_class => 'JSON',
8878
serializer_options => { utf8 => 1 }
8979
},
9080
problem_set => {
9181
data_type => 'text',
9282
size => 256,
93-
is_nullable => 0,
9483
default_value => '{}',
84+
retrieve_on_insert => 1,
9585
serializer_class => 'JSON',
9686
serializer_options => { utf8 => 1 }
9787
},
9888
problem => {
9989
data_type => 'text',
10090
size => 256,
101-
is_nullable => 0,
10291
default_value => '{}',
92+
retrieve_on_insert => 1,
10393
serializer_class => 'JSON',
10494
serializer_options => { utf8 => 1 }
10595
},
10696
permissions => {
10797
data_type => 'text',
10898
size => 256,
109-
is_nullable => 0,
11099
default_value => '{}',
100+
retrieve_on_insert => 1,
111101
serializer_class => 'JSON',
112102
serializer_options => { utf8 => 1 }
113103
},
114104
email => {
115105
data_type => 'text',
116106
size => 256,
117-
is_nullable => 0,
118107
default_value => '{}',
108+
retrieve_on_insert => 1,
119109
serializer_class => 'JSON',
120110
serializer_options => { utf8 => 1 }
121111
}

lib/DB/Schema/Result/CourseUser.pm

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package DB::Schema::Result::CourseUser;
22
use base qw/DBIx::Class::Core/;
33
use strict;
44
use warnings;
5+
use feature 'signatures';
6+
no warnings qw/experimental::signatures/;
57

6-
use base qw(DBIx::Class::Core DB::WithParams);
8+
use base qw/DBIx::Class::Core DB::Validation/;
79

810
=head1 DESCRIPTION
911
@@ -92,23 +94,19 @@ __PACKAGE__->add_columns(
9294
course_user_id => {
9395
data_type => 'integer',
9496
size => 16,
95-
is_nullable => 0,
9697
is_auto_increment => 1,
9798
},
9899
course_id => {
99-
data_type => 'integer',
100-
size => 16,
101-
is_nullable => 0,
100+
data_type => 'integer',
101+
size => 16,
102102
},
103103
user_id => {
104-
data_type => 'integer',
105-
size => 16,
106-
is_nullable => 0,
104+
data_type => 'integer',
105+
size => 16,
107106
},
108107
role_id => {
109108
data_type => 'integer',
110109
size => 16,
111-
is_nullable => 0,
112110
default_value => 0
113111
},
114112
section => {
@@ -125,28 +123,13 @@ __PACKAGE__->add_columns(
125123
course_user_params => {
126124
data_type => 'text',
127125
size => 256,
128-
is_nullable => 0,
129126
default_value => '{}',
127+
retrieve_on_insert => 1,
130128
serializer_class => 'JSON',
131129
serializer_options => { utf8 => 1 }
132130
}
133131
);
134132

135-
sub valid_params {
136-
return {
137-
comment => q{.*},
138-
useMathQuill => 'bool',
139-
displayMode => q{.*},
140-
status => q{[A-Z]},
141-
lis_source_did => q{.*},
142-
showOldAnswers => 'bool'
143-
};
144-
}
145-
146-
sub required_params {
147-
return {};
148-
}
149-
150133
__PACKAGE__->set_primary_key('course_user_id');
151134
__PACKAGE__->add_unique_constraint([qw/course_id user_id/]);
152135

@@ -162,4 +145,23 @@ __PACKAGE__->has_one(
162145
{ cascade_delete => 0 }
163146
);
164147

148+
=head2 C<valid_fields>
149+
150+
subroutine that returns a hash of the valid fields for json columns
151+
152+
=cut
153+
154+
sub valid_fields ($, $column_name) {
155+
if ($column_name eq 'course_user_params') {
156+
return {
157+
comment => q{.*},
158+
useMathQuill => 'bool',
159+
displayMode => q{.*},
160+
status => q{[A-Z]},
161+
lis_source_did => q{.*},
162+
showOldAnswers => 'bool'
163+
};
164+
}
165+
}
166+
165167
1;

0 commit comments

Comments
 (0)