-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathProblemPool.pm
More file actions
448 lines (293 loc) · 10.5 KB
/
ProblemPool.pm
File metadata and controls
448 lines (293 loc) · 10.5 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
package DB::Schema::ResultSet::ProblemPool;
use strict;
use warnings;
use feature 'signatures';
no warnings qw/experimental::signatures/;
use base 'DBIx::Class::ResultSet';
use Try::Tiny;
use Clone qw/clone/;
use DB::Utils qw/getCourseInfo getPoolInfo getPoolProblemInfo/;
use Exception::Class qw/
DB::Exception::PoolNotInCourse
DB::Exception::PoolAlreadyInCourse
DB::Exception::PoolProblemNotInPool
DB::Exception::ParametersNeeded
/;
=head1 DESCRIPTION
This is the functionality of a Course in WeBWorK. This package is based on
C<DBIx::Class::ResultSet>. The basics are a CRUD for anything on the
global courses.
=head2 getAllProblemPools
This gets a list of all problems stored in the database in the C<problem_pool> table.
=head3 input
C<as_result_set>, a boolean if the return is to be a result_set
=head3 output
An array of courses as a C<DBIx::Class::ResultSet::Course> object
if C<$args{as_result_set}> is true. Otherwise an array of hash_ref.
=cut
sub getAllProblemPools ($self, %args) {
my @problem_pools = $self->search({});
return @problem_pools if $args{as_result_set};
return map {
{ $_->get_inflated_columns, course_name => $_->courses->course_name };
} @problem_pools;
}
=head2 getProblemPools
Get all problem pools for a given course
=cut
sub getProblemPools ($self, %args) {
my $course = $self->rs('Course')->getCourse(info => $args{info}, as_result_set => 1);
my @pools = $self->search(
{
'courses.course_id' => $course->course_id
},
{
join => [qw/courses/]
}
);
return @pools if $args{as_result_set};
return map {
{ $_->get_inflated_columns };
} @pools;
}
# CRUD for a single ProblemPool
=head2 getProblemPool
Get a single problem pool for a given course
=cut
sub getProblemPool ($self, %args) {
my $course = $self->rs('Course')->getCourse(info => getCourseInfo($args{info}), as_result_set => 1);
my $search_info = getPoolInfo($args{info});
$search_info->{'courses.course_id'} = $course->course_id;
my $pool = $self->find($search_info, { join => [qw/courses/] });
DB::Exception::PoolNotInCourse->throw(message => 'The pool with '
. ($args{info}{pool_name} ? ' name ' . $args{info}{pool_name} : 'id ' . $args{info}{problem_pool_id})
. 'is not in the course '
. $course->course_name)
unless $pool;
return $pool if $args{as_result_set};
return { $pool->get_columns };
}
=head2 addProblemPool
Add a problem pool for a given course
=head3 arguments
=over
=item * C<params> a hashref specifying information on the added problem pool
One must include either the C<course_id> or C<course_name> and the C<pool_name>.
If there is not enough info to get the course and pool, a C<ParametersNeeded> exception is thrown.
=item * C<as_result_set>: boolean
If C<as_result_set> is true, then the user sets are returned as a C<ResultSet>.
See C<DBIx::Class::ResultSet> for more information
=back
=head3 output
Either a hashref of the added problem pool or a C<DBIx::Class::ResultSet::ProblemPool>
if C<as_result_set> is true.
=cut
sub addProblemPool ($self, %args) {
my $course = $self->rs('Course')->getCourse(info => getCourseInfo($args{params}), as_result_set => 1);
DB::Exception::ParametersNeeded->throw(message => 'The pool_name is missing from the parameters')
unless defined($args{params}->{pool_name});
my $existing_pool = $self->find(
{
'courses.course_id' => $course->course_id,
pool_name => $args{params}->{pool_name}
},
{ prefetch => [qw/courses/] }
);
DB::Exception::PoolAlreadyInCourse->throw(
message => 'The problem pool '
. (
$args{info}->{pool_name} || $args{params}->{pool_name}
? ' with name ' . ($args{info}->{pool_name} // $args{params}->{pool_name})
: ' with id ' . $args{info}->{problem_pool_id}
)
. " is already defined in the course $course->course_name"
) if defined($existing_pool);
my $params = clone $args{params};
# Delete some fields that may be passed in but are not in the database
for my $key (qw/course_name course_id/) {
delete $params->{$key} if defined $params->{$key};
}
# need to check for valid parameters.
my $pool_to_add = $self->new($params);
my $problem_pool = $course->add_to_problem_pools($params);
return $problem_pool if $args{as_result_set};
return { $problem_pool->get_columns };
}
=head2 updateProblemPool
updates the parameters of an existing problem pool
=head3 arguments
=over
=item * C<info> a hashref specifying information on the problem pool
One must include either the C<course_id> or C<course_name> and either the C<pool_name>
or C<problem_pool_id>. If there is not enough info to get the course and pool, a
C<ParametersNeeded> exception is thrown.
=item * C<params>: a hashref containing the information to be updated.
=item * C<as_result_set>: boolean
If C<as_result_set> is true, then the user sets are returned as a C<ResultSet>.
See C<DBIx::Class::ResultSet> for more information
=back
=cut
sub updateProblemPool ($self, %args) {
my $pool = $self->getProblemPool(info => $args{info}, as_result_set => 1);
DB::Exception::PoolNotInCourse->throw(
message => 'The problem pool '
. (
$args{info}->{pool_name}
? " named $args{info}->{pool_name}"
: " with id $args{info}->{problem_pool_id}"
)
. ' is not in the course '
. $args{info}->{course_name} ? " named '$args{info}->{course_name}'" : " with id $args{info}->{course_id}"
) unless defined($pool);
# create a new problem pool to check for valid fields
my $pool_params = $self->new($args{params});
my $updated_pool = $pool->update($args{params});
return $updated_pool if $args{as_result_set};
return { $updated_pool->get_columns };
}
=head2 deleteProblemPool
delete a Problem Pool
=head3 arguments
=over
=item * C<info> a hashref specifying information on the problem pool
One must include either the C<course_id> or C<course_name> and either the C<pool_name>
or C<problem_pool_id>. If there is not enough info to get the course and pool, a
C<ParametersNeeded> exception is thrown.
=item * C<as_result_set>: boolean
If C<as_result_set> is true, then the user sets are returned as a C<ResultSet>.
See C<DBIx::Class::ResultSet> for more information
=back
=head3 output
Nothing (undef) is returned.
=cut
sub deleteProblemPool ($self, %args) {
$self->getProblemPool(info => $args{info}, as_result_set => 1)->delete;
return;
}
#####
#
# CRUD for PoolProblems, that is creating, retrieving, updating and deleting problem to existing ProblemPools
#
####
=head2 getPoolProblems
This gets all problems out of the given Problem Pool
=head3 arguments
=over
=item * hashref containing
=over
=item - course_id or course_name
=item - problem_pool_id or pool_name
=back
=back
=cut
sub getPoolProblems ($self, %args) {
my $problem_pool = $self->getProblemPool(info => $args{info}, as_result_set => 1);
my @pool_problems = $self->rs('PoolProblem')->search({
problem_pool_id => $problem_pool->problem_pool_id
});
return \@pool_problems if $args{as_result_set};
return map {
{ $_->get_inflated_columns };
} @pool_problems;
}
=head2 getPoolProblem
This gets a single problem out of a ProblemPool.
=head3 arguments
=over
=item * hashref containing
=over
=item - course_id or course_name
=item - problem_pool_id or pool_name
=item - pool_problem_id or library_id or empty
=back
=back
=cut
sub getPoolProblem ($self, %args) {
my $problem_pool = $self->getProblemPool(info => $args{info}, as_result_set => 1);
my $pool_problem_info = {};
# If problem_info (pool_problem_id or pool_name) was passed in, then parse it.
try {
$pool_problem_info = getPoolProblemInfo($args{info});
} catch {
$pool_problem_info = {};
};
my @pool_problems = $problem_pool->search_related('pool_problems', $pool_problem_info)->all;
if (scalar(@pool_problems) == 1) {
return $args{as_result_set} ? $pool_problems[0] : { $pool_problems[0]->get_inflated_columns };
} elsif (scalar(@pool_problems) == 0) {
DB::Exception::PoolProblemNotInPool->throw(message => 'The problem with id '
. $pool_problem_info->{pool_problem_id}
. ' is not in the pool named \''
. $problem_pool->pool_name
. "'");
return;
} else {
# Pick a random problem.
my $prob = $pool_problems[ rand @pool_problems ];
return $args{as_result_set} ? $prob : { $prob->get_inflated_columns };
}
}
=head2 addProblemToPool
This adds a problem as a hashref to an existing problem pool.
=cut
sub addProblemToPool ($self, %args) {
my $pool = $self->getProblemPool(info => $args{params}, as_result_set => 1);
my $params = clone $args{params};
DB::Excpetion::PoolNotInCourse->throw(message => 'The problem pool '
. ($params->{pool_name} ? " named $params->{pool_name}" : " with id $params->{problem_pool_id}")
. ' is not in the course '
. ($params->{course_name} ? " named $params->{course_name}" : " with id $params->{course_id}"))
unless defined($pool);
# Remove some parameters that are not in the UserSet database, but may be passed in.
for my $key (qw/pool_name problem_pool_id course_name course_id/) {
delete $params->{$key} if defined $params->{$key};
}
my $course = $self->rs('Course')->find({ course_id => $pool->course_id });
$params->{problem_pool_id} = $pool->problem_pool_id;
my $pool_problem = $self->rs('PoolProblem')->new($params);
my $added_problem = $pool->add_to_pool_problems($params);
return $added_problem if $args{as_result_set};
return { $added_problem->get_inflated_columns };
}
=head2 updatePoolProblem
updated an existing problem to an existing ProblemPool in a course
=head3 arguments
=over
=item * hashref containing
=over
=item - course_id or course_name
=item - pool_name or problem_pool_id
=item - library_id or ???
=back
=item * hashref containing information about the Problem.
=back
=cut
sub updatePoolProblem ($self, %args) {
my $prob = $self->getPoolProblem(info => $args{info}, as_result_set => 1);
DB::Exception::PoolProblemNotInPool->throw(info => $args{info}) unless defined($prob);
my $prob_to_update = $self->rs('PoolProblem')->new($args{params});
my $prob2 = $prob->update({ $prob_to_update->get_columns });
return $prob2 if $args{as_result_set};
return { $prob2->get_inflated_columns };
}
=head2 removePoolProblem
remove a Problem out of a ProblemPool in a course
=head3 arguments
=over
=item * hashref containing
=over
=item - course_id or course_name
=item - pool_problem_id
=back
=item * hashref containing information about the Problem.
=back
=cut
sub removePoolProblem ($self, %args) {
$self->getPoolProblem(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;