From 0b278682d25cc232d7b0faf6815b8e5283e21b03 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 statuses 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. 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 bf464c3..64a6b86 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -2807,6 +2807,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; @@ -2835,8 +2837,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(), 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(timestamp) >= ? |; + push @params, $age; + } else { + # Calculate item age based on days since item was created + $query .= q| AND TO_DAYS(NOW()) - TO_DAYS(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..44cf3b1 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 wil 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