Bug 10601: Add a counter on batch items modifications
authorJonathan Druart <jonathan.druart@biblibre.com>
Tue, 16 Jul 2013 15:28:43 +0000 (17:28 +0200)
committerGalen Charlton <gmc@esilibrary.com>
Wed, 18 Sep 2013 17:10:00 +0000 (17:10 +0000)
After executing a batch items modifications, a dialog is displayed above
the result table. It contains the number of items (and total fields)
which has been modified.

Note that items that are selected for modification but which do not
end up actually be changed are not reported in the final counts.

This patch adds two methiods to C4::BackgroundJob, ->set() and ->get(),
that allow background jobs to pass arbitrary data back to the client.

Test plan:
1/ Go to tools/batchMod.pl
2/ Enter a barcodes list
3/ Check/uncheck items and fill some values to apply
4/ Save
5/ The table summary will be displayed with a dialog box on top:
 XX item(s) modified (with YY fields modified)
Check that XX and YY correspond with what you expected.

Signed-off-by: Liz Rea <liz@catalyst.net.nz>
batch modification still seems to work correctly, with the helpful addition of the counter. Thanks!

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
C4/BackgroundJob.pm
koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt
tools/batchMod.pl

index 6830565..05327f0 100644 (file)
@@ -260,6 +260,44 @@ sub fetch {
     return $self;
 }
 
+=head2 set
+
+=over 4
+
+$job->set($hashref);
+
+=back
+
+Set some variables into the hashref.
+These variables can be retrieved using the get method.
+
+=cut
+
+sub set {
+    my ($self, $hashref) = @_;
+    while ( my ($k, $v) = each %$hashref ) {
+        $self->{$k} = $v;
+    }
+    $self->_serialize();
+}
+
+=head2 get
+
+=over 4
+
+$job->get($hashref);
+
+=back
+
+Get a variable which has been previously stored with the set method.
+
+=cut
+
+sub get {
+    my ($self, $key) = @_;
+    return $self->{$key};
+}
+
 1;
 __END__
 
index 8ee771c..29f2eab 100644 (file)
@@ -56,7 +56,14 @@ $(document).ready(function(){
 <div id="doc3" class="yui-t7">
     <div id="bd">
         <div id="yui-main">
-            [% IF ( show ) %]<h1>Batch item modification</h1>[% ELSE %]<h1>Batch item modification results</h1>[% END %]
+            [% IF ( show ) %]
+              <h1>Batch item modification</h1>
+            [% ELSE %]
+              <h1>Batch item modification results</h1>
+              <div class="dialog message">
+              [% modified_items %] item(s) modified (with [% modified_fields %] fields modified)
+              </div>
+            [% END %]
 [% IF ( barcode_not_unique ) %]<div class="dialog alert"><strong>Error saving item</strong>: Barcode must be unique.</div>[% END %]
 [% IF ( no_next_barcode ) %]<div class="dialog alert"><strong>Error saving items</strong>: Unable to automatically determine values for barcodes. No item has been inserted.</div>[% END %]
 [% IF ( book_on_loan ) %]<div class="dialog alert"><strong>Cannot Delete</strong>: item is checked out.</div>[% END %]
index 63d6417..69762b1 100755 (executable)
@@ -84,6 +84,8 @@ my $deleted_items = 0;     # Number of deleted items
 my $deleted_records = 0;   # Number of deleted records ( with no items attached )
 my $not_deleted_items = 0; # Number of items that could not be deleted
 my @not_deleted;           # List of the itemnumbers that could not be deleted
+my $modified_items = 0;    # Numbers of modified items
+my $modified_fields = 0;   # Numbers of modified fields
 
 my %cookies = parse CGI::Cookie($cookie);
 my $sessionID = $cookies{'CGISESSID'}->value;
@@ -160,7 +162,7 @@ if ($op eq "action") {
 
                $job->progress($i) if $runinbackground;
                my $itemdata = GetItem($itemnumber);
-               if ($input->param("del")){
+        if ( $del ){
                        my $return = DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
                        if ($return == 1) {
                            $deleted_items++;
@@ -184,14 +186,25 @@ if ($op eq "action") {
                             }
                         }
                } else {
-                   if ($values_to_modify || $values_to_blank) {
-                       my $localmarcitem = Item2Marc($itemdata);
-                       UpdateMarcWith( $marcitem, $localmarcitem );
-                       eval{
-                            if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
-                                LostItem($itemnumber, 'MARK RETURNED') if $item->{itemlost};
-                            }
-                        };
+            if ($values_to_modify || $values_to_blank) {
+                my $localmarcitem = Item2Marc($itemdata);
+
+                my $modified = UpdateMarcWith( $marcitem, $localmarcitem );
+                if ( $modified ) {
+                    eval {
+                        if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
+                            LostItem($itemnumber, 'MARK RETURNED') if $item->{itemlost};
+                        }
+                    };
+                }
+                if ( $runinbackground ) {
+                    $modified_items++ if $modified;
+                    $modified_fields += $modified;
+                    $job->set({
+                        modified_items  => $modified_items,
+                        modified_fields => $modified_fields,
+                    });
+                }
                    }
                }
                $i++;
@@ -561,21 +574,25 @@ sub BuildItemsData{
 # And $tag>10
 sub UpdateMarcWith {
   my ($marcfrom,$marcto)=@_;
-  #warn "FROM :",$marcfrom->as_formatted;
-       my (  $itemtag,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
-       my $fieldfrom=$marcfrom->field($itemtag);
-       my @fields_to=$marcto->field($itemtag);
-    foreach my $subfield ($fieldfrom->subfields()){
-               foreach my $field_to_update (@fields_to){
-                   if ($subfield->[1]){
-                       $field_to_update->update($subfield->[0]=>$subfield->[1]);
-                   }
-                   else {
-                       $field_to_update->delete_subfield(code=> $subfield->[0]);
-                   }
-               }
+    my (  $itemtag,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
+    my $fieldfrom=$marcfrom->field($itemtag);
+    my @fields_to=$marcto->field($itemtag);
+    my $modified = 0;
+    foreach my $subfield ( $fieldfrom->subfields() ) {
+        foreach my $field_to_update ( @fields_to ) {
+            if ( $subfield->[1] ) {
+                unless ( $field_to_update->subfield($subfield->[0]) ~~ $subfield->[1] ) {
+                    $modified++;
+                    $field_to_update->update( $subfield->[0] => $subfield->[1] );
+                }
+            }
+            else {
+                $modified++;
+                $field_to_update->delete_subfield( code => $subfield->[0] );
+            }
+        }
     }
-  #warn "TO edited:",$marcto->as_formatted;
+    return $modified;
 }
 
 sub find_value {
@@ -610,6 +627,13 @@ sub add_saved_job_results_to_template {
     my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
     my $results = $job->results();
     add_results_to_template($template, $results);
+
+    my $fields = $job->get("modified_fields");
+    my $items = $job->get("modified_items");
+    $template->param(
+        modified_items => $items,
+        modified_fields => $fields,
+    );
 }
 
 sub put_in_background {