LP#1849212: (follow-up) numerous fixes to open-ils.courses.detach_material
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Courses.pm
index a68ce6b..304714d 100644 (file)
@@ -140,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;
 }
 
@@ -186,43 +187,23 @@ __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->{usr_role}->{is_public} = 't'
-        unless ($self->api_name =~ /\.staff/) and $e->allowed('MANAGE_RESERVES');
-    $users->{list} =  $e->search_asset_course_module_course_users($filter, {flesh => 1, flesh_fields => {acmcu => ['usr_role']}, 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 $targets;
+    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'}
+        },
+    });
 
 }
 
@@ -276,25 +257,27 @@ sub _resetItemFields {
     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->orginal_status);
+        $acp->status($acmcm->original_status);
     }
     if ($acmcm->original_circ_modifier) {
-        $acp->status($acmcm->orginal_circ_modifier);
+        $acp->circ_modifier($acmcm->original_circ_modifier);
     }
     if ($acmcm->original_location) {
-        $acp->status($acmcm->orginal_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 $acn_id = cat_sess->request('open-ils.cat.call_number.find_or_create',
-            $authtoken, $acmcm->original_callnumber,
+        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)->acn_id;
-        cat_sess->request('open-ils.cat.transfer_copies_to_volume',
+            $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]);
     }
 }