1 package OpenILS::Application::Courses;
6 use OpenSRF::AppSession;
7 use OpenILS::Application;
8 use base qw/OpenILS::Application/;
10 use OpenILS::Utils::CStoreEditor qw/:funcs/;
11 use OpenILS::Utils::Fieldmapper;
12 use OpenILS::Application::AppUtils;
13 my $U = "OpenILS::Application::AppUtils";
15 use OpenSRF::Utils::Logger qw/$logger/;
17 __PACKAGE__->register_method(
18 method => 'attach_electronic_resource_to_course',
19 api_name => 'open-ils.courses.attach.electronic_resource',
21 desc => 'Attaches a bib record for an electronic resource to a course',
23 {desc => 'Authentication token', type => 'string'},
24 {desc => 'Record id', type => 'number'},
25 {desc => 'Course id', type => 'number'},
26 {desc => 'Relationship', type => 'string'}
28 return => {desc => '1 on success, event on failure'}
30 sub attach_electronic_resource_to_course {
31 my ($self, $conn, $authtoken, $record, $course, $relationship) = @_;
32 my $e = new_editor(authtoken=>$authtoken, xact=>1);
33 return $e->die_event unless $e->checkauth;
34 return $e->die_event unless
35 $e->allowed('MANAGE_RESERVES');
37 my $located_uris = $e->search_asset_call_number({
40 label => '##URI##' })->[0];
41 my $bib = $e->retrieve_biblio_record_entry([
44 flesh_fields => {'bre' => ['source']}
47 return $e->event unless (($bib->source && $bib->source->transcendant) || $located_uris);
48 _attach_bib($e, $course, $record, $relationship, 0);
53 __PACKAGE__->register_method(
54 method => 'attach_brief_bib_to_course',
55 api_name => 'open-ils.courses.attach.biblio_record',
57 desc => 'Creates a new bib record with the provided XML, and attaches it to a course',
59 {desc => 'Authentication token', type => 'string'},
60 {desc => 'XML', type => 'string'},
61 {desc => 'Course id', type => 'number'},
62 {desc => 'Relationship', type => 'string'}
64 return => {desc => '1 on success, event on failure'}
66 sub attach_brief_bib_to_course {
67 my ($self, $conn, $authtoken, $marcxml, $course, $relationship) = @_;
68 my $e = new_editor(authtoken=>$authtoken, xact=>1);
69 return $e->die_event unless $e->checkauth;
70 return $e->die_event unless $e->allowed('MANAGE_RESERVES');
71 return $e->die_event unless $e->allowed('CREATE_MARC');
73 my $bib_source_id = $U->ou_ancestor_setting_value($self->{ou}, 'circ.course_materials_brief_record_bib_source');
76 $bib_source_name = $e->retrieve_config_bib_source($bib_source_id)->source;
78 # The default value from the seed data
79 $bib_source_name = 'Course materials module';
82 my $bib_create = OpenSRF::AppSession
83 ->create('open-ils.cat')
84 ->request('open-ils.cat.biblio.record.xml.create',
85 $authtoken, $marcxml, $bib_source_name)
87 _attach_bib($e, $course, $bib_create->id, $relationship, 1) if ($bib_create);
91 # Shared logic for both e-resources and brief bibs
93 my ($e, $course, $record, $relationship, $temporary) = @_;
94 my $acmcm = Fieldmapper::asset::course_module_course_materials->new;
95 $acmcm->course($course);
96 $acmcm->record($record);
97 $acmcm->relationship($relationship);
98 $acmcm->temporary_record($temporary);
99 $e->create_asset_course_module_course_materials( $acmcm ) or return $e->die_event;
103 __PACKAGE__->register_method(
104 method => 'fetch_course_materials',
107 api_name => 'open-ils.courses.course_materials.retrieve',
109 Returns an array of course materials.
110 @params args : Supplied object to filter search.
113 __PACKAGE__->register_method(
114 method => 'fetch_course_materials',
117 api_name => 'open-ils.courses.course_materials.retrieve.fleshed',
119 Returns an array of course materials, each fleshed out with information
120 from the item and the course_material object.
121 @params args : Supplied object to filter search.
124 sub fetch_course_materials {
125 my ($self, $conn, $args) = @_;
126 my $e = new_editor();
129 if ($self->api_name =~ /\.fleshed/) {
131 'flesh' => 2, 'flesh_fields' => {
132 'acmcm' => ['item', 'record', 'original_circ_modifier',
133 'original_location', 'original_status'],
134 'acp' => ['call_number', 'circ_lib', 'location', 'status'],
135 'bre' => ['wide_display_entry'],
138 $materials = $e->search_asset_course_module_course_materials([$args, $fleshing]);
140 $materials = $e->search_asset_course_module_course_materials($args);
142 $conn->respond($_) for @$materials;
146 __PACKAGE__->register_method(
147 method => 'fetch_courses',
149 api_name => 'open-ils.courses.courses.retrieve',
151 Returns an array of course materials.
152 @params course_id: The id of the course we want to retrieve
156 my ($self, $conn, @course_ids) = @_;
157 my $e = new_editor();
159 return unless @course_ids;
161 foreach my $course_id (@course_ids) {
162 my $target = $e->retrieve_asset_course_module_course($course_id);
163 push @$targets, $target;
169 __PACKAGE__->register_method(
170 method => 'fetch_course_users',
172 api_name => 'open-ils.courses.course_users.retrieve',
174 Returns an array of course users.
175 @params course_id: The id of the course we want to retrieve from
177 __PACKAGE__->register_method(
178 method => 'fetch_course_users',
180 api_name => 'open-ils.courses.course_users.retrieve.staff',
182 Returns an array of course users.
183 @params course_id: The id of the course we want to retrieve from
186 sub fetch_course_users {
187 my ($self, $conn, $course_id) = @_;
188 my $e = new_editor();
193 $filter->{course} = $course_id;
194 $filter->{is_public} = 't'
195 unless ($self->api_name =~ /\.staff/) and $e->allowed('MANAGE_RESERVES');
198 $users->{list} = $e->search_asset_course_module_course_users($filter, {order_by => {acmcu => 'id'}});
199 for my $course_user (@{$users->{list}}) {
201 $patron->{id} = $course_user->id;
202 $patron->{usr_role} = $course_user->usr_role;
203 $patron->{patron_data} = $e->retrieve_actor_user($course_user->usr);
204 $patrons{$course_user->usr} = $patron;
208 for my $user (values %patrons) {
210 $final_user->{id} = $user->{id};
211 $final_user->{usr_role} = $user->{usr_role};
212 $final_user->{patron_id} = $user->{patron_data}->id;
213 $final_user->{first_given_name} = $user->{patron_data}->first_given_name;
214 $final_user->{second_given_name} = $user->{patron_data}->second_given_name;
215 $final_user->{family_name} = $user->{patron_data}->family_name;
216 $final_user->{pref_first_given_name} = $user->{patron_data}->pref_first_given_name;
217 $final_user->{pref_family_name} = $user->{patron_data}->pref_family_name;
218 $final_user->{pref_second_given_name} = $user->{patron_data}->pref_second_given_name;
219 $final_user->{pref_suffix} = $user->{patron_data}->pref_suffix;
220 $final_user->{pref_prefix} = $user->{patron_data}->pref_prefix;
222 push @$targets, $final_user;
229 __PACKAGE__->register_method(
230 method => 'detach_material',
231 api_name => 'open-ils.courses.detach_material',
233 desc => 'Detaches a material from a course',
235 {desc => 'Authentication token', type => 'string'},
236 {desc => 'Course material id', type => 'number'},
238 return => {desc => '1 on success, event on failure'}
240 sub detach_material {
241 my ($self, $conn, $authtoken, $acmcm_id) = @_;
242 my $e = new_editor(authtoken=>$authtoken, xact=>1);
243 return $e->die_event unless $e->checkauth;
244 return $e->die_event unless
245 $e->allowed('MANAGE_RESERVES');
246 my $acmcm = $e->retrieve_asset_course_module_course_materials($acmcm_id)
247 or return $e->die_event;
248 my $bre_id_to_delete = $acmcm->temporary_record ? $acmcm->record : 0;
249 if ($bre_id_to_delete) {
250 # delete any attached located URIs
251 my $located_uri_cn_ids = $e->search_asset_call_number(
252 {record=>$bre_id_to_delete}, {idlist=>1});
254 for my $cn_id (@$located_uri_cn_ids) {
255 $e->delete_asset_call_number(
256 $e->retrieve_asset_call_number($cn_id))
257 or return $e->die_event;
260 ->create('open-ils.cat')
261 ->request('open-ils.cat.biblio.record_entry.delete',
262 $authtoken, $bre_id_to_delete);
265 _resetItemFields($e, $authtoken, $acmcm);
268 $e->delete_asset_course_module_course_materials($acmcm) or return $e->die_event;
273 sub _resetItemFields {
274 my ($e, $authtoken, $acmcm) = @_;
275 my $cat_sess = OpenSRF::AppSession->connect('open-ils.cat');
276 my $acp = $e->retrieve_asset_copy($acmcm->item);
277 my $course_lib = $e->retrieve_asset_course_module_course($acmcm->course)->owning_lib;
278 if ($acmcm->original_status) {
279 $acp->status($acmcm->orginal_status);
281 if ($acmcm->original_circ_modifier) {
282 $acp->status($acmcm->orginal_circ_modifier);
284 if ($acmcm->original_location) {
285 $acp->status($acmcm->orginal_location);
287 $e->update_asset_copy($acmcm);
288 if ($acmcm->original_callnumber) {
289 my $existing_acn = $e->retrieve_asset_call_number($acp->call_number);
290 # Let's attach to an existing call number, if one exists with the original label
291 # and other appropriate specifications
292 my $acn_id = cat_sess->request('open-ils.cat.call_number.find_or_create',
293 $authtoken, $acmcm->original_callnumber,
294 $existing_acn->record, $course_lib,
295 $existing_acn->prefix, $existing_acn->suffix,
296 $existing_acn->label_class)->acn_id;
297 cat_sess->request('open-ils.cat.transfer_copies_to_volume',
298 $authtoken, $acn_id, [$acp->id]);