]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Courses.pm
fe32164cd4b53e59eb652d6447b07ae42c7b72c2
[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);
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) 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) = @_;
94     my $acmcm = Fieldmapper::asset::course_module_course_materials->new;
95     $acmcm->course($course);
96     $acmcm->record($record);
97     $acmcm->relationship($relationship);
98     $e->create_asset_course_module_course_materials( $acmcm ) or return $e->die_event;
99     $e->commit;
100 }
101
102 sub detach_material_from_course {
103     my ($self, $conn, $authtoken, $acmcm) = @_;
104
105 }
106
107 __PACKAGE__->register_method(
108     method          => 'fetch_course_materials',
109     autoritative    => 1,
110     stream          => 1,
111     api_name        => 'open-ils.courses.course_materials.retrieve',
112     signature       => q/
113         Returns an array of course materials.
114         @params args     : Supplied object to filter search.
115     /);
116
117 __PACKAGE__->register_method(
118     method          => 'fetch_course_materials',
119     autoritative    => 1,
120     stream          => 1,
121     api_name        => 'open-ils.courses.course_materials.retrieve.fleshed',
122     signature       => q/
123         Returns an array of course materials, each fleshed out with information
124         from the item and the course_material object.
125         @params args     : Supplied object to filter search.
126     /);
127
128 sub fetch_course_materials {
129     my ($self, $conn, $args) = @_;
130     my $e = new_editor();
131     my $materials;
132
133     if ($self->api_name =~ /\.fleshed/) {
134         my $fleshing = {
135             'flesh' => 2, 'flesh_fields' => {
136                 'acmcm' => ['item', 'record'],
137                 'acp' => ['call_number', 'circ_lib', 'location', 'status'],
138                 'bre' => ['wide_display_entry'],
139             }
140         };
141         $materials = $e->search_asset_course_module_course_materials([$args, $fleshing]);
142     } else {
143         $materials = $e->search_asset_course_module_course_materials($args);
144     }
145     $conn->respond($_) for @$materials;
146     return undef;
147 }
148
149 __PACKAGE__->register_method(
150     method          => 'fetch_courses',
151     autoritative    => 1,
152     api_name        => 'open-ils.courses.courses.retrieve',
153     signature       => q/
154         Returns an array of course materials.
155         @params course_id: The id of the course we want to retrieve
156     /);
157
158 sub fetch_courses {
159     my ($self, $conn, @course_ids) = @_;
160     my $e = new_editor();
161
162     return unless @course_ids;
163     my $targets = ();
164     foreach my $course_id (@course_ids) {
165         my $target = $e->retrieve_asset_course_module_course($course_id);
166         push @$targets, $target;
167     }
168
169     return $targets;
170 }
171
172 __PACKAGE__->register_method(
173     method          => 'fetch_course_users',
174     autoritative    => 1,
175     api_name        => 'open-ils.courses.course_users.retrieve',
176     signature       => q/
177         Returns an array of course users.
178         @params course_id: The id of the course we want to retrieve from
179     /);
180 __PACKAGE__->register_method(
181     method          => 'fetch_course_users',
182     autoritative    => 1,
183     api_name        => 'open-ils.courses.course_users.retrieve.staff',
184     signature       => q/
185         Returns an array of course users.
186         @params course_id: The id of the course we want to retrieve from
187     /);
188
189 sub fetch_course_users {
190     my ($self, $conn, $course_id) = @_;
191     my $e = new_editor();
192     my $filter = {};
193     my $users = {};
194     my %patrons;
195
196     $filter->{course} = $course_id;
197     $filter->{is_public} = 't'
198         unless ($self->api_name =~ /\.staff/) and $e->allowed('MANAGE_RESERVES');
199  
200  
201     $users->{list} =  $e->search_asset_course_module_course_users($filter, {order_by => {acmcu => 'id'}});
202     for my $course_user (@{$users->{list}}) {
203         my $patron = {};
204         $patron->{id} = $course_user->id;
205         $patron->{usr_role} = $course_user->usr_role;
206         $patron->{patron_data} = $e->retrieve_actor_user($course_user->usr);
207         $patrons{$course_user->usr} = $patron;
208     }
209
210     my $targets = ();
211     for my $user (values %patrons) {
212         my $final_user = {};
213         $final_user->{id} = $user->{id};
214         $final_user->{usr_role} = $user->{usr_role};
215         $final_user->{patron_id} = $user->{patron_data}->id;
216         $final_user->{first_given_name} = $user->{patron_data}->first_given_name;
217         $final_user->{second_given_name} = $user->{patron_data}->second_given_name;
218         $final_user->{family_name} = $user->{patron_data}->family_name;
219         $final_user->{pref_first_given_name} = $user->{patron_data}->pref_first_given_name;
220         $final_user->{pref_family_name} = $user->{patron_data}->pref_family_name;
221         $final_user->{pref_second_given_name} = $user->{patron_data}->pref_second_given_name;
222         $final_user->{pref_suffix} = $user->{patron_data}->pref_suffix;
223         $final_user->{pref_prefix} = $user->{patron_data}->pref_prefix;
224
225         push @$targets, $final_user;
226     }
227
228     return $targets;
229
230 }
231
232
233
234 1;
235