-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSetProblem.pm
More file actions
368 lines (231 loc) · 8.88 KB
/
SetProblem.pm
File metadata and controls
368 lines (231 loc) · 8.88 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package DB::Schema::ResultSet::SetProblem;
use strict;
use warnings;
use feature 'signatures';
no warnings qw/experimental::signatures/;
use Clone qw/clone/;
use base 'DBIx::Class::ResultSet';
use DB::Utils qw/getCourseInfo getSetInfo getProblemInfo updateAllFields/;
=head1 DESCRIPTION
This is the functionality of a Problem in WeBWorK. This package is based on
C<DBIx::Class::ResultSet>. The basics are a CRUD for problems in Problem sets.
=head2 getGlobalProblems
This gets a list of all problems stored in the database in the C<problems> table.
=head3 input, a hash of options
=over
=item - C<as_result_set>, a boolean. If true this result an array of C<DBIx::Class::ResultSet::ProblemSet>
if false, an array of hashrefs of ProblemSet.
=back
=head3 output
An array of problems as either a hashref or a C<DBIx::Class::ResultSet::Problem> object
=cut
sub getGlobalProblems ($self, %args) {
my @problems = $self->search({});
return @problems if $args{as_result_set};
return map {
{
$_->get_inflated_columns, set_name => $_->problem_set->set_name
};
} @problems;
}
# CRUD for problems in a course
=head1 getProblems
This gets all problems in a given course.
=head3 input, a hash of options
=over
=item * C<info>, either a course name or course_id.
For example, C<{ course_name => 'Precalculus'}> or C<{course_id => 3}>
=item - C<as_result_set>, a boolean. If true this result an array of C<DBIx::Class::ResultSet::ProblemSet>
if false, an array of hashrefs of ProblemSet.
=back
=head3 notes:
if either the course or course_id doesn't exist, an error will be thrown.
=head3 output
An array of Problems (as hashrefs) or an array of C<DBIx::Class::ResultSet::Problem>
=cut
sub getProblems ($self, %args) {
my $course = $self->rs('Course')->getCourse(info => $args{info}, as_result_set => 1);
my @problems =
$self->search({ 'problem_set.course_id' => $course->course_id }, { join => [qw/problem_set/] });
return @problems if $args{as_result_set};
return map {
{ $_->get_inflated_columns };
} @problems;
}
=head1 getSetProblems
This gets all problems in a given set
=head3 input, a hash of options
=over
=item * C<info>, a hash with
=over
=item - C<course_name> or C<course_id>
=item - C<set_name> or C<set_id>
=back
For example, C<{ course_name => 'Precalculus', set_id => 4}>
or C<{course_id => 3, set_name => 'HW #1'}>
=item * C<as_result_set>, a boolean. If true this result an array of C<DBIx::Class::ResultSet::ProblemSet>
if false, an array of hashrefs of ProblemSet.
=back
=head3 notes:
if either the course or set doesn't exist, an exception will be thrown.
=head3 output
An array of Problems (as hashrefs) or an array of C<DBIx::Class::ResultSet::Problem>
=cut
sub getSetProblems ($self, %args) {
my $problem_set = $self->rs('ProblemSet')->getProblemSet(info => $args{info}, as_result_set => 1);
my @problems = $self->search({ 'set_id' => $problem_set->set_id });
return \@problems if $args{as_result_set};
return map {
{ $_->get_inflated_columns };
} @problems;
}
=head2 getSetProblem
This gets a single problem from a given course and problem
=head3 input, a hash of options
=over
=item * C<info>, a hash with
=over
=item - C<course_name> or C<course_id>
=item - C<set_name> or C<set_id>
=item - C<problem_number> or C<set_problem_id>
=back
For example, C<{ course_name => 'Precalculus', set_id => 4}>
or C<{course_id => 3, set_name => 'HW #1'}>
=item * C<as_result_set>, a boolean. If true this result an array of C<DBIx::Class::ResultSet::ProblemSet>
if false, an array of hashrefs of ProblemSet.
=back
=head3 notes:
if either the course or set doesn't exist, an exception will be thrown.
=head3 output
A problem (as a hashref) or an array of C<DBIx::Class::ResultSet::Problem>
=cut
sub getSetProblem ($self, %args) {
my $problem_set = $self->rs('ProblemSet')->getProblemSet(info => $args{info}, as_result_set => 1);
my $problem = $problem_set->problems->find(getProblemInfo($args{info}));
DB::Exception::SetProblemNotFound->throw(
message => 'the problem with '
. (
$args{info}->{problem_number}
? 'problem number ' . $args{info}->{problem_number}
: 'set_problem id: ' . $args{info}->{set_problem_id}
)
. ' is not found for set: '
. $problem_set->set_name
. ' is the course: '
. $problem_set->courses->course_name
) unless $problem;
return $problem if $args{as_result_set};
return { $problem->get_inflated_columns };
}
=head2 addSetProblem
Add a single problem to an existing problem set within a course
=head3 input, a hash of options
=over
=item * C<info>, a hash with
=over
=item - C<course_name> or C<course_id>
=item - C<set_name> or C<set_id>
=back
For example, C<{ course_name => 'Precalculus', set_id => 4}>
or C<{course_id => 3, set_name => 'HW #1'}>
=item * C<as_result_set>, a boolean. If true this result an array of C<DBIx::Class::ResultSet::ProblemSet>
if false, an array of hashrefs of ProblemSet.
=back
=head3 notes:
if either the course or set doesn't exist, an exception will be thrown.
=head3 output
An array of Problems (as hashrefs) or an array of C<DBIx::Class::ResultSet::Problem>
=cut
=head3 Note
=over
=item * If either the problem set or course does not exist an error will be thrown
=item * If the problem parameters are not valid, an error will be thrown.
=back
=cut
sub addSetProblem ($self, %args) {
my $problem_set = $self->rs('ProblemSet')->getProblemSet(info => $args{params}, as_result_set => 1);
my $params = clone $args{params};
# Remove some parameters that are not in the database, but may be passed in,
# including set_problem_id which should not have a value if being added.
for my $key (qw/set_problem_id course_name course_id set_name/) {
delete $params->{$key} if defined $params->{$key};
}
# set the problem number to one more than the set's largest
$params->{problem_number} = 1 + ($problem_set->problems->get_column('problem_number')->max // 0);
$params->{problem_params}{weight} = 1 unless defined($params->{problem_params}{weight});
$self->checkProblemParams($params);
my $added_problem = $problem_set->add_to_problems($params);
return $args{as_result_set} ? $added_problem : { $added_problem->get_inflated_columns };
}
sub checkProblemParams ($self, $params) {
# Check that the problem_number field is a positive number.
DB::Exception::InvalidParameter->throw(message => 'The problem_number must be a positive integer')
unless defined $params->{problem_number} && $params->{problem_number} =~ /^\d+$/;
my $problem_to_add = $self->new($params);
$problem_to_add->validate('problem_params');
return 1;
}
=head2 updateSetProblem
update a single problem in a problem set within a course
=head3 input, a hash of options
=over
=item * C<info>, a hash with
=over
=item - C<course_name> or C<course_id>
=item - C<set_name> or C<set_id>
=back
For example, C<{ course_name => 'Precalculus', set_id => 4}>
or C<{course_id => 3, set_name => 'HW #1'}>
=item * C<params>, a hash with fields/parameters from C<DB::Schema::Result::Problem>
=item * C<as_result_set>, a boolean. If true this result an array of C<DBIx::Class::ResultSet::ProblemSet>
if false, an array of hashrefs of ProblemSet.
=back
=head3 notes:
if either the course or set doesn't exist, an exception will be thrown.
=head3 output
A single Problem (as hashrefs) or an object of class C<DBIx::Class::ResultSet::Problem>
=cut
=head3 Note
=over
=item * If either the problem set or course does not exist an error will be thrown
=item * If the problem parameters are not valid, an error will be thrown.
=back
=cut
sub updateSetProblem ($self, %args) {
my $problem = $self->getSetProblem(info => $args{info}, as_result_set => 1);
my $params = updateAllFields({ $problem->get_inflated_columns }, $args{params});
# Check that the new params are valid.
my $updated_problem = $self->new($params);
$self->checkProblemParams($params);
my $problem_to_return = $problem->update($params);
return $args{as_result_set} ? $problem_to_return : { $problem_to_return->get_inflated_columns };
}
=head2 deleteSetProblem
delete a single problem to an existing problem set within a course
=head3 input, a hash of options
=over
=item * C<info>, a hash with
=over
=item - C<course_name> or C<course_id>
=item - C<set_name> or C<set_id>
=item - C<problem_number> or C<set_problem_id>
=back
For example, C<{ course_name => 'Precalculus', set_id => 4}>
or C<{course_id => 3, set_name => 'HW #1'}>
=item * C<as_result_set>, a boolean. If true this result an array of C<DBIx::Class::ResultSet::ProblemSet>
if false, an array of hashrefs of ProblemSet.
=back
=head3 notes:
if either the course or set doesn't exist, an exception will be thrown.
=head3 output
Nothing (undef) is returned.
=cut
sub deleteSetProblem ($self, %args) {
$self->getSetProblem(info => $args{info}, as_result_set => 1)->delete;
return;
}
# just a small subroutine to shorten access to the db.
sub rs ($self, $table) {
return $self->result_source->schema->resultset($table);
}
1;