LP#1849212: (follow-up) numerous fixes to open-ils.courses.detach_material
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Courses.pm
index fe32164..304714d 100644 (file)
@@ -45,7 +45,7 @@ sub attach_electronic_resource_to_course {
         }
     ]);
     return $e->event unless (($bib->source && $bib->source->transcendant) || $located_uris);
-    _attach_bib($e, $course, $record, $relationship);
+    _attach_bib($e, $course, $record, $relationship, 0);
 
     return 1;
 }
@@ -84,26 +84,22 @@ sub attach_brief_bib_to_course {
         ->request('open-ils.cat.biblio.record.xml.create',
             $authtoken, $marcxml, $bib_source_name)
         ->gather(1);
-    _attach_bib($e, $course, $bib_create->id, $relationship) if ($bib_create);
+    _attach_bib($e, $course, $bib_create->id, $relationship, 1) if ($bib_create);
     return 1;
 }
 
 # Shared logic for both e-resources and brief bibs
 sub _attach_bib {
-    my ($e, $course, $record, $relationship) = @_;
+    my ($e, $course, $record, $relationship, $temporary) = @_;
     my $acmcm = Fieldmapper::asset::course_module_course_materials->new;
     $acmcm->course($course);
     $acmcm->record($record);
     $acmcm->relationship($relationship);
+    $acmcm->temporary_record($temporary);
     $e->create_asset_course_module_course_materials( $acmcm ) or return $e->die_event;
     $e->commit;
 }
 
-sub detach_material_from_course {
-    my ($self, $conn, $authtoken, $acmcm) = @_;
-
-}
-
 __PACKAGE__->register_method(
     method          => 'fetch_course_materials',
     autoritative    => 1,
@@ -133,7 +129,8 @@ sub fetch_course_materials {
     if ($self->api_name =~ /\.fleshed/) {
         my $fleshing = {
             'flesh' => 2, 'flesh_fields' => {
-                'acmcm' => ['item', 'record'],
+                'acmcm' => ['item', 'record', 'original_circ_modifier',
+                    'original_location', 'original_status'],
                 'acp' => ['call_number', 'circ_lib', 'location', 'status'],
                 'bre' => ['wide_display_entry'],
             }
@@ -143,6 +140,7 @@ sub fetch_course_materials {
         $materials = $e->search_asset_course_module_course_materials($args);
     }
     $conn->respond($_) for @$materials;
+    $conn->respond_complete();
     return undef;
 }
 
@@ -189,44 +187,99 @@ __PACKAGE__->register_method(
 sub fetch_course_users {
     my ($self, $conn, $course_id) = @_;
     my $e = new_editor();
-    my $filter = {};
-    my $users = {};
-    my %patrons;
-
-    $filter->{course} = $course_id;
-    $filter->{is_public} = 't'
-        unless ($self->api_name =~ /\.staff/) and $e->allowed('MANAGE_RESERVES');
-    $users->{list} =  $e->search_asset_course_module_course_users($filter, {order_by => {acmcu => 'id'}});
-    for my $course_user (@{$users->{list}}) {
-        my $patron = {};
-        $patron->{id} = $course_user->id;
-        $patron->{usr_role} = $course_user->usr_role;
-        $patron->{patron_data} = $e->retrieve_actor_user($course_user->usr);
-        $patrons{$course_user->usr} = $patron;
-    }
 
-    my $targets = ();
-    for my $user (values %patrons) {
-        my $final_user = {};
-        $final_user->{id} = $user->{id};
-        $final_user->{usr_role} = $user->{usr_role};
-        $final_user->{patron_id} = $user->{patron_data}->id;
-        $final_user->{first_given_name} = $user->{patron_data}->first_given_name;
-        $final_user->{second_given_name} = $user->{patron_data}->second_given_name;
-        $final_user->{family_name} = $user->{patron_data}->family_name;
-        $final_user->{pref_first_given_name} = $user->{patron_data}->pref_first_given_name;
-        $final_user->{pref_family_name} = $user->{patron_data}->pref_family_name;
-        $final_user->{pref_second_given_name} = $user->{patron_data}->pref_second_given_name;
-        $final_user->{pref_suffix} = $user->{patron_data}->pref_suffix;
-        $final_user->{pref_prefix} = $user->{patron_data}->pref_prefix;
-
-        push @$targets, $final_user;
+    return $e->json_query({
+        select => {
+            acmcu => ['id'],
+            acmr => [{column => 'name', alias => 'usr_role'}],
+            au => [{column => 'id', alias => 'patron_id'},
+                'first_given_name', 'second_given_name',
+                'family_name', 'pref_first_given_name',
+                'pref_second_given_name', 'pref_family_name',
+                'pref_suffix', 'pref_prefix'],
+        },
+        from => { acmcu => {'acmr' => {}, 'au' => {}} },
+        where => {
+            '+acmcu' => { course => $course_id },
+            '+acmr' => {'is_public' => 't'}
+        },
+    });
+
+}
+
+__PACKAGE__->register_method(
+    method          => 'detach_material',
+    api_name        => 'open-ils.courses.detach_material',
+    signature => {
+        desc => 'Detaches a material from a course',
+        params => [
+            {desc => 'Authentication token', type => 'string'},
+            {desc => 'Course material id', type => 'number'},
+        ],
+        return => {desc => '1 on success, event on failure'}
+    });
+sub detach_material {
+    my ($self, $conn, $authtoken, $acmcm_id) = @_;
+    my $e = new_editor(authtoken=>$authtoken, xact=>1);
+    return $e->die_event unless $e->checkauth;
+    return $e->die_event unless
+        $e->allowed('MANAGE_RESERVES');
+    my $acmcm = $e->retrieve_asset_course_module_course_materials($acmcm_id)
+        or return $e->die_event;
+    my $bre_id_to_delete = $acmcm->temporary_record ? $acmcm->record : 0;
+    if ($bre_id_to_delete) {
+        # delete any attached located URIs
+        my $located_uri_cn_ids = $e->search_asset_call_number(
+            {record=>$bre_id_to_delete}, {idlist=>1});
+
+        for my $cn_id (@$located_uri_cn_ids) {
+            $e->delete_asset_call_number(
+                $e->retrieve_asset_call_number($cn_id))
+                or return $e->die_event;
+        }
+        OpenSRF::AppSession
+            ->create('open-ils.cat')
+            ->request('open-ils.cat.biblio.record_entry.delete',
+                $authtoken, $bre_id_to_delete);
     }
+    if ($acmcm->item) {
+        _resetItemFields($e, $authtoken, $acmcm);
+    } 
 
-    return $targets;
+    $e->delete_asset_course_module_course_materials($acmcm) or return $e->die_event;
+    $e->commit;
+    return 1;
+}
 
+sub _resetItemFields {
+    my ($e, $authtoken, $acmcm) = @_;
+    my $cat_sess = OpenSRF::AppSession->connect('open-ils.cat');
+    my $acp = $e->retrieve_asset_copy($acmcm->item);
+    my $course_lib = $e->retrieve_asset_course_module_course($acmcm->course)->owning_lib;
+    if ($acmcm->original_status) {
+        $acp->status($acmcm->original_status);
+    }
+    if ($acmcm->original_circ_modifier) {
+        $acp->circ_modifier($acmcm->original_circ_modifier);
+    }
+    if ($acmcm->original_location) {
+        $acp->location($acmcm->original_location);
+    }
+    $e->update_asset_copy($acmcm);
+    if ($acmcm->original_callnumber) {
+        my $existing_acn = $e->retrieve_asset_call_number($acp->call_number);
+        my $orig_acn = $e->retrieve_asset_call_number($acmcm->original_callnumber);
+        # Let's attach to an existing call number, if one exists with the original label
+        # and other appropriate specifications
+        my $dest_acn = $cat_sess->request('open-ils.cat.call_number.find_or_create',
+            $authtoken, $orig_acn->label,
+            $existing_acn->record, $course_lib,
+            $existing_acn->prefix, $existing_acn->suffix,
+            $existing_acn->label_class)->gather(1);
+        my $acn_id = $dest_acn->{acn_id};
+        $cat_sess->request('open-ils.cat.transfer_copies_to_volume',
+            $authtoken, $acn_id, [$acp->id]);
+    }
 }