]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Courses.pm
a12831c74de0fa319aa934a8d629e75f8f9545e4
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Courses.pm
1 package OpenILS::Application::Courses;
2
3 use strict;
4 use warnings;
5
6 use OpenSRF::AppSession;
7 use OpenILS::Application;
8 use base qw/OpenILS::Application/;
9
10 use OpenILS::Utils::CStoreEditor qw/:funcs/;
11 use OpenILS::Utils::Fieldmapper;
12 use OpenILS::Application::AppUtils;
13 my $U = "OpenILS::Application::AppUtils";
14
15 use OpenSRF::Utils::Logger qw/$logger/;
16
17 __PACKAGE__->register_method(
18     method          => 'attach_electronic_resource_to_course',
19     api_name        => 'open-ils.courses.attach.electronic_resource',
20     signature => {
21         desc => 'Attaches a bib record for an electronic resource to a course',
22         params => [
23             {desc => 'Authentication token', type => 'string'},
24             {desc => 'Record id', type => 'number'},
25             {desc => 'Course id', type => 'number'},
26             {desc => 'Relationship', type => 'string'}
27         ],
28         return => {desc => '1 on success, event on failure'}
29     });
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');
36
37     my $located_uris = $e->search_asset_call_number({
38         record => $record,
39         deleted => 'f',
40         label => '##URI##' })->[0];
41     my $bib = $e->retrieve_biblio_record_entry([
42         $record, {
43             flesh => 1,
44             flesh_fields => {'bre' => ['source']}
45         }
46     ]);
47     return $e->event unless (($bib->source && $bib->source->transcendant) || $located_uris);
48     _attach_bib($e, $course, $record, $relationship, 0);
49
50     return 1;
51 }
52
53 __PACKAGE__->register_method(
54     method          => 'attach_brief_bib_to_course',
55     api_name        => 'open-ils.courses.attach.biblio_record',
56     signature => {
57         desc => 'Creates a new bib record with the provided XML, and attaches it to a course',
58         params => [
59             {desc => 'Authentication token', type => 'string'},
60             {desc => 'XML', type => 'string'},
61             {desc => 'Course id', type => 'number'},
62             {desc => 'Relationship', type => 'string'}
63         ],
64         return => {desc => '1 on success, event on failure'}
65     });
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');
72
73     my $bib_source_id = $U->ou_ancestor_setting_value($self->{ou}, 'circ.course_materials_brief_record_bib_source');
74     my $bib_source_name;
75     if ($bib_source_id) {
76         $bib_source_name = $e->retrieve_config_bib_source($bib_source_id)->source;
77     } else {
78         # The default value from the seed data
79         $bib_source_name = 'Course materials module';
80     }
81
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)
86         ->gather(1);
87     _attach_bib($e, $course, $bib_create->id, $relationship, 1) if ($bib_create);
88     return 1;
89 }
90
91 # Shared logic for both e-resources and brief bibs
92 sub _attach_bib {
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;
100     $e->commit;
101 }
102
103 __PACKAGE__->register_method(
104     method          => 'fetch_course_materials',
105     autoritative    => 1,
106     stream          => 1,
107     api_name        => 'open-ils.courses.course_materials.retrieve',
108     signature       => q/
109         Returns an array of course materials.
110         @params args     : Supplied object to filter search.
111     /);
112
113 __PACKAGE__->register_method(
114     method          => 'fetch_course_materials',
115     autoritative    => 1,
116     stream          => 1,
117     api_name        => 'open-ils.courses.course_materials.retrieve.fleshed',
118     signature       => q/
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.
122     /);
123
124 sub fetch_course_materials {
125     my ($self, $conn, $args) = @_;
126     my $e = new_editor();
127     my $materials;
128
129     if ($self->api_name =~ /\.fleshed/) {
130         my $fleshing = {
131             'flesh' => 2, 'flesh_fields' => {
132                 'acmcm' => ['item', 'record'],
133                 'acp' => ['call_number', 'circ_lib', 'location', 'status'],
134                 'bre' => ['wide_display_entry'],
135             }
136         };
137         $materials = $e->search_asset_course_module_course_materials([$args, $fleshing]);
138     } else {
139         $materials = $e->search_asset_course_module_course_materials($args);
140     }
141     $conn->respond($_) for @$materials;
142     return undef;
143 }
144
145 __PACKAGE__->register_method(
146     method          => 'fetch_courses',
147     autoritative    => 1,
148     api_name        => 'open-ils.courses.courses.retrieve',
149     signature       => q/
150         Returns an array of course materials.
151         @params course_id: The id of the course we want to retrieve
152     /);
153
154 sub fetch_courses {
155     my ($self, $conn, @course_ids) = @_;
156     my $e = new_editor();
157
158     return unless @course_ids;
159     my $targets = ();
160     foreach my $course_id (@course_ids) {
161         my $target = $e->retrieve_asset_course_module_course($course_id);
162         push @$targets, $target;
163     }
164
165     return $targets;
166 }
167
168 __PACKAGE__->register_method(
169     method          => 'fetch_course_users',
170     autoritative    => 1,
171     api_name        => 'open-ils.courses.course_users.retrieve',
172     signature       => q/
173         Returns an array of course users.
174         @params course_id: The id of the course we want to retrieve from
175     /);
176 __PACKAGE__->register_method(
177     method          => 'fetch_course_users',
178     autoritative    => 1,
179     api_name        => 'open-ils.courses.course_users.retrieve.staff',
180     signature       => q/
181         Returns an array of course users.
182         @params course_id: The id of the course we want to retrieve from
183     /);
184
185 sub fetch_course_users {
186     my ($self, $conn, $course_id) = @_;
187     my $e = new_editor();
188     my $filter = {};
189     my $users = {};
190     my %patrons;
191
192     $filter->{course} = $course_id;
193     $filter->{is_public} = 't'
194         unless ($self->api_name =~ /\.staff/) and $e->allowed('MANAGE_RESERVES');
195  
196  
197     $users->{list} =  $e->search_asset_course_module_course_users($filter, {order_by => {acmcu => 'id'}});
198     for my $course_user (@{$users->{list}}) {
199         my $patron = {};
200         $patron->{id} = $course_user->id;
201         $patron->{usr_role} = $course_user->usr_role;
202         $patron->{patron_data} = $e->retrieve_actor_user($course_user->usr);
203         $patrons{$course_user->usr} = $patron;
204     }
205
206     my $targets = ();
207     for my $user (values %patrons) {
208         my $final_user = {};
209         $final_user->{id} = $user->{id};
210         $final_user->{usr_role} = $user->{usr_role};
211         $final_user->{patron_id} = $user->{patron_data}->id;
212         $final_user->{first_given_name} = $user->{patron_data}->first_given_name;
213         $final_user->{second_given_name} = $user->{patron_data}->second_given_name;
214         $final_user->{family_name} = $user->{patron_data}->family_name;
215         $final_user->{pref_first_given_name} = $user->{patron_data}->pref_first_given_name;
216         $final_user->{pref_family_name} = $user->{patron_data}->pref_family_name;
217         $final_user->{pref_second_given_name} = $user->{patron_data}->pref_second_given_name;
218         $final_user->{pref_suffix} = $user->{patron_data}->pref_suffix;
219         $final_user->{pref_prefix} = $user->{patron_data}->pref_prefix;
220
221         push @$targets, $final_user;
222     }
223
224     return $targets;
225
226 }
227
228 __PACKAGE__->register_method(
229     method          => 'detach_material',
230     api_name        => 'open-ils.courses.detach_material',
231     signature => {
232         desc => 'Detaches a material from a course',
233         params => [
234             {desc => 'Authentication token', type => 'string'},
235             {desc => 'Course material id', type => 'number'},
236         ],
237         return => {desc => '1 on success, event on failure'}
238     });
239 sub detach_material {
240     my ($self, $conn, $authtoken, $acmcm_id) = @_;
241     my $e = new_editor(authtoken=>$authtoken, xact=>1);
242     return $e->die_event unless $e->checkauth;
243     return $e->die_event unless
244         $e->allowed('MANAGE_RESERVES');
245     my $acmcm = $e->retrieve_asset_course_module_course_materials($acmcm_id)
246         or return $e->die_event;
247     my $bre_id_to_delete = $acmcm->temporary_record ? $acmcm->record : 0;
248     if ($bre_id_to_delete) {
249         # delete any attached located URIs
250         my $located_uri_cn_ids = $e->search_asset_call_number(
251             {record=>$bre_id_to_delete}, {idlist=>1});
252
253         for my $cn_id (@$located_uri_cn_ids) {
254             $e->delete_asset_call_number(
255                 $e->retrieve_asset_call_number($cn_id))
256                 or return $e->die_event;
257         }
258         OpenSRF::AppSession
259             ->create('open-ils.cat')
260             ->request('open-ils.cat.biblio.record_entry.delete',
261                 $authtoken, $bre_id_to_delete);
262     }
263     if ($acmcm->item) {
264         _resetItemFields($e, $authtoken, $acmcm);
265     } 
266
267     $e->delete_asset_course_module_course_materials($acmcm) or return $e->die_event;
268     $e->commit;
269     return 1;
270 }
271
272 sub _resetItemFields {
273     my ($e, $authtoken, $acmcm) = @_;
274     my $cat_sess = OpenSRF::AppSession->connect('open-ils.cat');
275     my $acp = $e->retrieve_asset_copy($acmcm->item);
276     my $course_lib = $e->retrieve_asset_course_module_course($acmcm->course)->owning_lib;
277     if ($acmcm->original_status) {
278         $acp->status($acmcm->orginal_status);
279     }
280     if ($acmcm->original_circ_modifier) {
281         $acp->status($acmcm->orginal_circ_modifier);
282     }
283     if ($acmcm->original_location) {
284         $acp->status($acmcm->orginal_location);
285     }
286     $e->update_asset_copy($acmcm);
287     if ($acmcm->original_callnumber) {
288         my $existing_acn = $e->retrieve_asset_call_number($acp->call_number);
289         # Let's attach to an existing call number, if one exists with the original label
290         # and other appropriate specifications
291         my $acn_id = cat_sess->request('open-ils.cat.call_number.find_or_create',
292             $authtoken, $acmcm->original_callnumber,
293             $existing_acn->record, $course_lib,
294             $existing_acn->prefix, $existing_acn->suffix,
295             $existing_acn->label_class)->acn_id;
296         cat_sess->request('open-ils.cat.transfer_copies_to_volume',
297             $authtoken, $acn_id, [$acp->id]);
298     }
299 }
300
301
302
303 1;
304