From c1f84f06e3b6f6452b4f754649f362e6d4168226 Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Mon, 6 Aug 2018 14:08:57 +0000 Subject: [PATCH] Bug 21173 - Customize age unit and starting point for automatic item modification by age tool and cronjob. This allows librarians to define either the item creation date or items last altered timestamp as the starting point for the age of the item so the autmatic_item_modification_by_age.pl can alter item fields for example for items which have been in the catalogue for a while. A use case for this functionality is: * When an items shelving location changes from 'In processing' to 'Shelving trolley' after 1 hour the status 'Shelving trolley' is removed for the item. The unit 'Hours' can now be set (extending the functionality of this cronjob from just calculating item age in days) and the age starting point of 'Last altered' can be defined so existing items in the catalogue like this example item will be altered by the cronjob. Test plan: 1. Go to Tools->Automatic item modifications by age and create a new rule by selecting the 'Edit rules' button 2. Notice you can only set the age in the unit of days not hours. Also alsthough not visible in the interface the rule will calculate the age of the item based on when it was created (i.e. added to Koha). 3. Apply patch and run ./updatedatabase.pl in the koha-shell 4. Now revisit Tools->Automatic item modifications by age. Notice the table of existing rules now contains a new column 'Age starting point' this is the date from which the item age is calculated. This enhancement offers you two options as the age starting point: Created (displayed in this table as 'itemcreated') or Last altered (displayed in this table as 'itemaltered'). 5. Click 'Edit rules' to create a new rule. Notice in the form for creating the new rule you can set the unit to either 'Hours' or 'Days'. When you select 'Days' then the Age starting point selection will appear where you can specify if you want the rule to work on the number of days since the item was last altered or number of days since the item was created. NOTE: If you select 'Hours' as the unit type when creating a new rule then the Age starting point selection area is hidden. This is because the item creation value is saved in the database as a date not a datetime. Therefore we cannot calculate the number of hours since the item was created in the Koha catalog. Therefore if the unit is set to 'Hours' then by default the item will use the item last altered value (stored as a date time) as the age starting point. For existing rules if they have the unit set to 'Hours' then the Age starting point area is hidden by default when the page loads. 6. In the new rule set the following values: Age: 1, Unit: Hours, condition items.location = 'CART', items.location = '' 7. Now save the rule and confirm the correct values are dispayed in the rules table which is loaded 8. Click 'Edit rules' button again. 9. Modify the rule you just created changing Age to 2 and create another new rule with the following values: Age='1', Unit='Days', 'Age starting point'='Created', Condition 'items.homebranch'='', substituions 'items.homebranch'='' 10. Now click 'Save' and confirm the data displayed for both rules is correct. 11. In the database manually set the location field of two items to 'CART' and set their timestamp value to 2 hours before the current time 12. Exit out of the database terminal and manually run the automatic_item_modification_by_age.pl cronjob from the koha-shell with this command: ./automatic_item_modification_by_age.pl --confirm 13. Now return to the database and notice the two items you set have a location of 'CART' now have a blank location field because the manually run cronjob altered them as they meet the conditions of the rule. 14. Repeat step 11 and enter this text into your /etc/cron.d/koha-common file: */2 * * * * root koha-foreach --enabled /usr/share/koha/bin/cronjobs/automatic_item_modification_by_age.pl --confirm This will make the cronjob run every 2 minutes. 15. Now restart koha-common with this command: sudo /etc/init.d/koha-common restart 16. Now wait for more than 2 minutes and check the database again and notice that the two items have blank location fields as the automatically running cronjob altered the items. Sponsored-By: Toi Ohomai Institute of Technology, New Zealand --- C4/Items.pm | 17 ++++- .../tools/automatic_item_modification_by_age.tt | 76 +++++++++++++++++++--- .../prog/js/automatic_item_modification_by_age.js | 12 ++++ tools/automatic_item_modification_by_age.pl | 18 ++++- 4 files changed, 111 insertions(+), 12 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index 837c195..088ba3f 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -2827,6 +2827,8 @@ sub ToggleNewStatus { my $report; for my $rule ( @rules ) { my $age = $rule->{age}; + my $unit = $rule->{unit}; + my $age_starting_point = $rule->{age_starting_point}; my $conditions = $rule->{conditions}; my $substitutions = $rule->{substitutions}; my @params; @@ -2855,8 +2857,19 @@ sub ToggleNewStatus { } } if ( defined $age ) { - $query .= q| AND TO_DAYS(NOW()) - TO_DAYS(dateaccessioned) >= ? |; - push @params, $age; + # Calculate item age based on hours since item last altered + if ( defined $rule && defined $age_starting_point && $unit eq "hours" && $age_starting_point eq "itemaltered" ) { + $query .= q| AND HOUR(TIMEDIFF(NOW(), items.timestamp)) >= ? |; + push @params, $age; + } elsif ( defined $rule && defined $age_starting_point && $unit eq "days" && $age_starting_point eq "itemaltered" ) { + # Calculate item age based on days since item last altered + $query .= q| AND TO_DAYS(NOW()) - TO_DAYS(items.timestamp) >= ? |; + push @params, $age; + } else { + # Calculate item age based on days since item was created + $query .= q| AND TO_DAYS(NOW()) - TO_DAYS(items.dateaccessioned) >= ? |; + push @params, $age; + } } my $sth = $dbh->prepare($query); $sth->execute( @params ); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/automatic_item_modification_by_age.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/automatic_item_modification_by_age.tt index 7e1d5a3..cb4a5f7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/automatic_item_modification_by_age.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/automatic_item_modification_by_age.tt @@ -70,10 +70,39 @@ Rule [% loop.count %] Remove this rule
-
Age in days
+
Age
-
+
+
Unit
+

Note: If the unit of 'Hours' is set then you cannot choose an age starting point, the item age starting point wil default to the last item alteration event. The Age starting point selection will be hidden if you have set the unit to 'Hours'.

+ +
+ [% IF rule.unit == "days" %] +
+
Age starting point (starting point from which the item age is calculated)
+ +
+ [% END %] +
Conditions
[% FOR condition IN rule.conditions %]
@@ -87,7 +116,6 @@ [% END %] [% END %] - = Add a condition Remove condition @@ -108,7 +136,6 @@ [% END %] [% END %] - = Add a substitution Remove substitution @@ -131,9 +158,26 @@ Rule Remove this rule
-
Age in days
+
Age
+
+
Unit
+

Note: If the unit of 'Hours' is set then you cannot choose an age starting point, the item age starting point will default to the last item alteration event. The Age starting point selection will be hidden if you have set the unit to 'Hours'.

+ +
+
+
Age starting point (starting point from which the item age is calculated)
+ +
Conditions
@@ -143,7 +187,6 @@ [% END %] - = Add a condition Remove condition @@ -158,7 +201,6 @@ [% END %] - = Add a substitution Remove substitution @@ -173,6 +215,7 @@ Age + Age starting point Conditions Substitutions @@ -181,13 +224,28 @@ [% FOR rule IN rules %] - [% IF rule.age.defined and rule.age.length > 0 %] - [% rule.age %] days + [% IF rule.age.defined and rule.unit.defined and rule.age.length > 0 %] + [% IF rule.age == 1 %] + [% IF rule.unit == "days" %] + [% rule.age %] day + [% ELSE %] + [% rule.age %] hour + [% END %] + [% ELSE %] + [% rule.age %] [% rule.unit %] + [% END %] [% ELSE %] There is no age for this rule. [% END %] + [% IF rule.age_starting_point.defined %] + [% rule.age_starting_point %] + [% ELSE %] + There is no age starting point for this rule. + [% END %] + + [% FOR condition IN rule.conditions %] [% IF condition.field %]
diff --git a/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js b/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js index 6c37d9b..ea5e686 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js +++ b/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js @@ -52,6 +52,8 @@ function update_rule_count(){ $(document).ready(function() { $("#new_rule .remove_rule").hide(); $("#new_rule a.remove_block").hide(); + $("#new_rule_age_starting_point_div").hide(); + $("#rules a.remove_block").click(function(e){ e.preventDefault(); remove_block_action($(this)); @@ -61,6 +63,16 @@ $(document).ready(function() { remove_rule_action($(this)); }); + $("#new_rule_unit").change(function(){ + var unitselected = $(this).find("option:selected").attr("id"); + var newruleagestart = document.getElementById("new_rule_age_starting_point_div"); + if (unitselected == "days") { + newruleagestart.style.display = "block" + } else { + newruleagestart.style.display = "none"; + } + }); + var unique_id = $(".rule").length + 1; $(".add_rule").click(function(e){ e.preventDefault(); diff --git a/tools/automatic_item_modification_by_age.pl b/tools/automatic_item_modification_by_age.pl index 12a1c00..807d79b 100755 --- a/tools/automatic_item_modification_by_age.pl +++ b/tools/automatic_item_modification_by_age.pl @@ -39,7 +39,7 @@ use C4::Context; use C4::Items; use C4::Output; use C4::Koha; - +use Data::Dumper; use Koha::Items; use Koha::Biblioitems; @@ -86,9 +86,25 @@ if ( $op eq 'update' ) { push @{ $rule->{conditions} }, {} unless @{ $rule->{conditions} }; $rule->{age} = $cgi->param("age_$unique_id"); + + if ($cgi->param("unit_$unique_id")) { + $rule->{unit} = $cgi->param("unit_$unique_id"); + } else { + $rule->{unit} = $cgi->param("unit"); + } + + if ($cgi->param("age_starting_point_$unique_id")) { + $rule->{age_starting_point} = $cgi->param("age_starting_point_$unique_id"); + } elsif ($cgi->param("age_starting_point")) { + $rule->{age_starting_point} = $cgi->param("age_starting_point"); + } else { + $rule->{age_starting_point} = "itemaltered" + } + push @rules, $rule; } my $syspref_content = to_json( \@rules ); + C4::Context->set_preference($syspref_name, $syspref_content); $op = 'show'; -- 2.1.4