-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathProblemSet.pm
More file actions
539 lines (364 loc) · 13.1 KB
/
ProblemSet.pm
File metadata and controls
539 lines (364 loc) · 13.1 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package DB::Schema::ResultSet::ProblemSet;
use strict;
use warnings;
use feature 'signatures';
no warnings qw/experimental::signatures/;
use base 'DBIx::Class::ResultSet';
use Clone qw/clone/;
use DB::Utils qw/getCourseInfo getUserInfo getSetInfo updateAllFields/;
our $SET_TYPES = {
'HW' => 1,
'QUIZ' => 2,
'JITAR' => 3,
'REVIEW' => 4,
};
use DB::Exception;
=head1 DESCRIPTION
This is the functionality of a ProblemSet in WeBWorK. This package is based on
C<DBIx::Class::ResultSet>. The basics are a CRUD for ProblemSets.
Note: a ProblemSet is an abstract class for HWSet, Quiz, ReviewSet, which differ
in parameter and dates types.
=head2 getProblemSets
This gets a list of all ProblemSet (and set-like objects) stored in the database
in the C<problem_set> table.
=head3 input
=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 courses as a C<DBIx::Class::ResultSet::ProblemSet> object.
=cut
sub getAllProblemSets ($self, %args) {
my @problem_sets = $self->search();
return @problem_sets if $args{as_result_set};
my @all_sets = ();
for my $set (@problem_sets) {
my $expanded_set =
{ $set->get_inflated_columns, $set->courses->get_inflated_columns, set_type => $set->set_type };
delete $expanded_set->{type};
push(@all_sets, $expanded_set);
}
return @all_sets;
}
# The following is CRUD for problem sets in a given course
=head2 getProblemSets
Get all problem sets for a given course
=head3 input
A hash of input values.
=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 output
An array of Users (as hashrefs) or an array of C<DBIx::Class::ResultSet::ProblemSet>
=cut
sub getProblemSets ($self, %args) {
my $course = $self->rs('Course')->getCourse(info => $args{info}, as_result_set => 1);
my @problem_sets = $self->search({ 'course_id' => $course->course_id });
return @problem_sets if $args{as_result_set};
my $sets = _formatSets(\@problem_sets);
return @$sets;
}
=head2 getHWSets
Get all homework sets (C<ProblemSet> of type C<HWSet>) for a given course
=head3 input
A hash of input values.
=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::HWSet>
if false, an array of hashrefs of ProblemSet.
=back
=head3 output
An array of homework sets (as hashrefs) or an arrayref of C<DBIx::Class::ResultSet::HWSet>
=cut
sub getHWSets ($self, %args) {
my $p = getCourseInfo($args{info}); # pull out the course_info that is passed
my $search_params = {};
for my $key (keys %$p) {
$search_params->{"courses.$key"} = $p->{$key};
}
$search_params->{'type'} = 1;
my @problem_sets = $self->search($search_params, { join => 'courses' });
my $sets = _formatSets(\@problem_sets);
return $args{as_result_set} ? @problem_sets : @$sets;
}
=head2 getQuizzes
Get all quizzes (C<ProblemSet> of type C<Quiz>) for a given course
=head3 input
A hash of input values.
=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::HWSet>
if false, an array of hashrefs of Quiz.
=back
=head3 output
An array of quizzes (as hashrefs) or an arrayref of C<DBIx::Class::ResultSet::Quiz>
=cut
sub getQuizzes ($self, %args) {
my $p = getCourseInfo($args{info}); # pull out the course_info that is passed
my $search_params = {};
for my $key (keys %$p) {
$search_params->{"courses.$key"} = $p->{$key};
}
$search_params->{'type'} = 2;
my @problem_sets = $self->search($search_params, { join => 'courses' });
my $sets = _formatSets(\@problem_sets);
return $args{as_result_set} ? @problem_sets : @$sets;
}
=head2 getReviewSets
Get all quizzes (C<ProblemSet> of type C<ReviewSet>) for a given course
=head3 input
A hash of input values.
=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::ReviewSet>
if false, an array of hashrefs of HWSet.
=back
=head3 output
An array of review sets (as hashrefs) or an arrayref of C<DBIx::Class::ResultSet::ReviewSet>
=cut
sub getReviewSets ($self, %args) {
my $p = getCourseInfo($args{info}); # pull out the course_info that is passed
my $search_params = {};
for my $key (keys %$p) {
$search_params->{"courses.$key"} = $p->{$key};
}
$search_params->{'type'} = 3;
my @problem_sets = $self->search($search_params, { join => 'courses' });
my $sets = _formatSets(\@problem_sets);
return $args{as_result_set} ? @problem_sets : @$sets;
}
=head2 getProblemSet
Get a single ProblemSet for a given course
=head3 input
A hash of input values.
=over
=item * C<info>, a hash including
=over
=item * either a course name or course_id.
=item * either a set name or set_id
=back
For example, C<{ course_name => 'Precalculus', set_id => 3}> or
C<{course_id => 3, set_name ='HW #1'}>
=item * C<as_result_set>, a boolean. If true this an object of type
C<DBIx::Class::ResultSet::ProblemSet>
if false, a hashrefs of ProblemSet.
=back
=head3 output
An hashref of a problem set or an object of type C<DBIx::Class::ResultSet::ProblemSet>
=cut
sub getProblemSet ($self, %args) {
my $course_info = getCourseInfo($args{info});
my $set_info = getSetInfo($args{info});
my $course = $self->rs('Course')->getCourse(info => $course_info, as_result_set => 1);
my $problem_set = $course->problem_sets->find($set_info);
DB::Exception::SetNotInCourse->throw(
set_name => $set_info,
course_name => $course->course_name
) unless defined($problem_set) || $args{skip_throw};
return $problem_set if $args{as_result_set};
my $set = { $problem_set->get_inflated_columns, set_type => $problem_set->set_type };
delete $set->{type};
return $set;
}
=head2 addProblemSet
Add one HW set for a given course
=head3 input
A hash of input values.
=over
=item * C<info>, a hash including
=over
=item - either a course name or course_id.
=item - either a set name or set_id
=back
For example, C<{ course_name => 'Precalculus', set_id => 3}> or
C<{course_id => 3, set_name ='HW #1'}>
=item *C<params>, a hash including all parameters for the set.
=over
=item - C<set_name>: name of the set
=item - C<type>: the type of problem set
=item - C<set_visible>: (boolean) visiblility of the set to a student
=item - C<set_dates>: a hash of dates related to the problem set. Note: different types have
different date fields.
=item - C<set_params>: a hash of additional parameters of the problem set. Note: different problem set types
have different params fields.
=back
Note: depending
on the type of problem set, the C<set_params> and C<set_dates> may be different.
=item * C<as_result_set>, a boolean. If true this an object of type
C<DBIx::Class::ResultSet::ProblemSet>
if false, a hashrefs of ProblemSet.
=back
=head3 output
An hashref of a problem set or an object of type C<DBIx::Class::ResultSet::ProblemSet>
=cut
sub addProblemSet {
my ($self, %args) = @_;
my $course = $self->rs('Course')->getCourse(info => getCourseInfo($args{params}), as_result_set => 1);
my $set_params = clone $args{params};
$set_params->{type} = $SET_TYPES->{ $set_params->{set_type} || 'HW' };
# Delete a few fields that may be passed in but are not in the database
# Note: on client-side set_id=0 means that the set is new, so delete this
# and it will be determined.
for my $key (qw/course_id course_name set_type set_id/) {
delete $set_params->{$key} if defined $set_params->{$key};
}
DB::Exception::ParametersNeeded->throw(message => 'You must defined the field set_name in the params argument')
unless defined($set_params->{set_name});
# Check if the set exists.
my $problem_set = $self->getProblemSet(
info => {
course_id => $course->course_id,
set_name => $set_params->{set_name}
},
as_result_set => 1,
skip_throw => 1
);
DB::Exception::SetAlreadyExists->throw(message =>
"The problem set with name $set_params->{set_name} already exists in the course $course->course_name")
if defined($problem_set);
# Check that fields/dates/parameters are valid
my $set_obj = $self->new($set_params);
$set_obj->validate('set_dates');
$set_obj->validate('set_params');
my $new_set = $course->add_to_problem_sets($set_params);
return $new_set if $args{as_result_set};
my $set = { $new_set->get_inflated_columns, set_type => $new_set->set_type };
delete $set->{type};
return $set;
}
=head2 updateProblemSet
Update a problem set (HW, Quiz, etc.) for a given course
=head3 input
A hash of input values.
=over
=item * C<info>, a hash including
=over
=item - either a course name or course_id.
=item - either a set name or set_id
=back
For example, C<{ course_name => 'Precalculus', set_id => 3}> or
C<{course_id => 3, set_name ='HW #1'}>
=item *C<params>, a hash including all parameters for the set.
=over
=item - C<set_name>: name of the set
=item - C<type>: the type of problem set
=item - C<set_visible>: (boolean) visiblility of the set to a student
=item - C<set_dates>: a hash of dates related to the problem set. Note: different types have
different date fields.
=item - C<set_params>: a hash of additional parameters of the problem set. Note: different problem set types
have different params fields.
=back
Note: depending
on the type of problem set, the C<set_params> and C<set_dates> may be different.
=item * C<as_result_set>, a boolean. If true this an object of type
C<DBIx::Class::ResultSet::ProblemSet>
if false, a hashrefs of ProblemSet.
=back
=head3 output
An hashref of a problem set or an object of type C<DBIx::Class::ResultSet::ProblemSet>
=cut
sub updateProblemSet ($self, %args) {
my $problem_set = $self->getProblemSet(info => $args{info}, as_result_set => 1);
my $set_params = { $problem_set->get_inflated_columns };
my $params = clone($args{params});
if (defined $params->{set_type}) {
$params->{type} = $SET_TYPES->{ $params->{set_type} };
delete $params->{set_type};
}
my $params2;
# If the problem set type changed, don't update the params, just used the ones passed in.
if (!defined($params->{type}) || $problem_set->type == $params->{type}) {
$params2 = updateAllFields($set_params, $params);
} else {
# The set type is changing, so assume that the set_dates and set_params are deleted
# unless passed in.
$params2 = $params;
unless (defined $params2->{set_dates}) {
if ($params2->{type} == 1) { # HomeworkSet
$params2->{set_dates} = {
open => 0,
reduced_scoring => 0,
due => 0,
answer => 0,
};
} elsif ($params2->{type} == 2) { # Quiz
$params2->{set_dates} = {
open => 0,
due => 0,
answer => 0,
};
} elsif ($params2->{type} == 3) { # JITAR
$params2->{set_dates} = {
open => 0,
reduced_scoring => 0,
due => 0,
answer => 0,
};
} elsif ($params2->{type} == 4) { # ReviewSet
$params2->{set_dates} = {
open => 0,
closed => 0
};
}
}
}
$params2->{set_params} = {} unless defined($params2->{set_params});
my $set_obj = $self->new($params2);
# Check the parameters are valid.
$set_obj->validate('set_dates') if $set_obj->set_dates;
$set_obj->validate('set_params') if $set_obj->set_params;
my $updated_set = $problem_set->update({ $set_obj->get_inflated_columns });
return $updated_set if $args{as_result_set};
my $set = { $updated_set->get_inflated_columns, set_type => $updated_set->set_type };
delete $set->{type};
return $set;
}
=head2 deleteProblemSet
Delete a problem set (HW, Quiz, etc.) for a given course
=head3 input
A hash of input values.
=over
=item * C<info>, a hash including
=over
=item - either a course name or course_id.
=item - either a set name or set_id
=back
For example, C<{ course_name => 'Precalculus', set_id => 3}> or
C<{course_id => 3, set_name ='HW #1'}>
=item * C<as_result_set>, a boolean. If true this an object of type
C<DBIx::Class::ResultSet::ProblemSet>
if false, a hashrefs of ProblemSet.
=back
=head3 output
Nothing (undef) is returned.
=cut
sub deleteProblemSet ($self, %args) {
$self->getProblemSet(info => $args{info}, as_result_set => 1)->delete;
return;
}
# The following are private methods used in this module.
sub _formatSets ($problem_sets) {
my @sets = ();
for my $set (@$problem_sets) {
my $expanded_set = { $set->get_inflated_columns, set_type => $set->set_type };
delete $expanded_set->{type};
push(@sets, $expanded_set);
}
return \@sets;
}
# just a small subroutine to shorten access to the db.
sub rs ($self, $table) {
return $self->result_source->schema->resultset($table);
}
1;