-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCourse.pm
More file actions
292 lines (182 loc) · 6.83 KB
/
Course.pm
File metadata and controls
292 lines (182 loc) · 6.83 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
package DB::Schema::ResultSet::Course;
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/;
use DB::Exception;
use Exception::Class qw/DB::Exception::CourseNotFound DB::Exception::CourseExists/;
#use TestUtils qw/removeIDs/;
use WeBWorK3::Utils::Settings qw/getDefaultCourseSettings mergeCourseSettings
getDefaultCourseValues validateCourseSettings/;
=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 level.
Unless otherwise indicated, all of the methods here return either one of two types:
=over
=item *
A Course described by a C<DBIx::Class:ResultSet::Course> object if C<$as_result_set> is true.
=item *
A Course as a hashref with the fields described in L<DBIx::Class:Result::Course>.
=back
=cut
=head2 getCourses
This gets a list of Courses stored in the database in the C<courses> 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<$as_result_set> is true. Otherwise an array of hash_ref.
=cut
sub getCourses ($self, %args) {
my @courses = $self->search();
return @courses if $args{as_result_set};
return map {
{ $_->get_inflated_columns };
} @courses;
}
=head2 getCourse
This gets a single course that is stored in the database in the C<courses> table.
=head3 input
=over
=item *
C<info>, a hashref containing either C<course_name> or C<course_id>
=item *
C<as_result_set>, a boolean if the return is to be a result_set
=back
=head3 output
The course either as a C< DBIx::Class::ResultSet::Course> object or a hashref
of the fields. See above.
=cut
sub getCourse ($self, %args) {
my $course = $self->find(getCourseInfo($args{info}));
unless (defined($course)) {
my $info = getCourseInfo($args{info});
DB::Exception::CourseNotFound->throw(message => "The course: $info->{course_name} does not exist.")
if defined($info->{course_name});
DB::Exception::CourseNotFound->throw(
message => "The course with course_id of $info->{course_id} does not exist.")
if defined($info->{course_id});
}
return $course if $args{as_result_set};
return { $course->get_inflated_columns };
}
=head2 addCourse
Adds a single course to the database in the C<courses> table.
=over
=item *
C<params>, a hashref containing either C<course_name> or C<course_id>
=item *
C<as_result_set>, a boolean if the return is to be a result_set
=back
=head3 output
The course either as a C< DBIx::Class::ResultSet::Course> object or a hashref
of the fields. See above.
=cut
sub addCourse ($self, %args) {
my $course_params = clone $args{params};
DB::Exception::ParametersNeeded->throw(message => 'The parameters must include course_name')
unless defined($course_params->{course_name});
# Check if the course exists. If so throw an error.
my $course = $self->find({ course_name => $course_params->{course_name} });
DB::Exception::CourseAlreadyExists->throw(course_name => $course_params->{course_name}) if defined($course);
my $params = {};
for my $field (qw/course_name visible course_dates/) {
# This should be looked up.
$params->{$field} = $course_params->{$field} if defined($course_params->{$field});
}
$params->{course_settings} = {};
# Check the parameters.
my $new_course = $self->create($params);
return $new_course if $args{as_result_set};
return { $new_course->get_inflated_columns };
}
=head2 deleteCourse
This deletes a single course that is stored in the database in the C<courses> table.
=head3 input
=over
=item *
C<course_info>, a hashref containing either C<course_name> or C<course_id>
=item *
C<as_result_set>, a boolean if the return is to be a result_set
=back
=head3 output
Nothing (undef) is returned.
=cut
sub deleteCourse ($self, %args) {
$self->getCourse(info => getCourseInfo($args{info}), as_result_set => 1)->delete;
return;
}
=head1 updateCourse
This updates a single course that is stored in the database in the C<courses> table.
=head3 input
=over
=item * C<info>, either C<course_name> or C<course_id>
=item * C<params>: A hash of the course parameters to be updated.
=back
=head3 output
The updated course as a C<DBIx::Class::ResultSet::Course> object or a hashref.
=cut
sub updateCourse ($self, %args) {
my $course = $self->getCourse(info => getCourseInfo($args{info}), as_result_set => 1);
## TODO: check the validity of the params
my $course_to_return = $course->update($args{params});
return $course_to_return if $args{as_result_set};
return { $course_to_return->get_inflated_columns };
}
=head2 getUserCourses
This gets a list of Courses for a given user
=head3 input
=over
=item * hashref containing info about the user
=item * C<$as_result_set>, a boolean if the return is to be a result_set
=back
=head3 output
An array of courses as a C<DBIx::Class::ResultSet::Course> object
if C<$as_result_set> is true. Otherwise an array of hash_ref.
=cut
sub getUserCourses ($self, %args) {
my $user = $self->result_source->schema->resultset('User')
->getGlobalUser(info => getUserInfo($args{info}), as_result_set => 1);
# my @user_courses = $user->courses->search({});
my @user_courses = $user->course_users->search({}, { prefetch => [qw/role/] });
return @user_courses if $args{as_result_set};
my @user_courses_hashref = ();
for my $user_course (@user_courses) {
my $params = {
$user_course->get_inflated_columns, $user_course->courses->get_inflated_columns,
role => $user_course->role->role_name
};
push(@user_courses_hashref, $params);
}
return @user_courses_hashref;
}
=head2 getCourseSettings
This gets the Course Settings for a course
=head3 input
=over
=item * hashref containing info about the course
=item * C<$as_result_set>, a boolean if the return is to be a result_set
=back
=head3 output
An array of courses as a C<DBIx::Class::ResultSet::Course> object
if C<$as_result_set> is true. Otherwise an array of hash_ref.
=cut
sub getCourseSettings ($self, %args) {
my $course = $self->getCourse(info => $args{info}, as_result_set => 1);
my $course_settings = getDefaultCourseValues();
my $settings_from_db = { $course->course_settings->get_inflated_columns };
return mergeCourseSettings($course_settings, $settings_from_db);
}
sub updateCourseSettings ($self, %args) {
my $course = $self->getCourse(info => $args{info}, as_result_set => 1);
validateCourseSettings($args{settings});
my $current_settings = { $course->course_settings->get_inflated_columns };
my $updated_settings = mergeCourseSettings($current_settings, $args{settings});
my $cs = $course->course_settings->update($updated_settings);
return mergeCourseSettings(getDefaultCourseValues(), { $cs->get_inflated_columns });
}
1;