Koha/tools/automatic_item_modification_by_age.pl
Jonathan Druart c631fa0924 Bug 11023: Automatic item modification by age (Was Toggle "new" status")
This patch adds:
- a new DB field items.new.
- a new page to configure this new status
  (tools/toggle_new_status.pl).
- a new cronjob script (misc/cronjobs/automatic_item_modification_by_age.pl
was misc/cronjob/toggle_new_status.pl)

Why this status is useful for some libraries ?
The use cases are:
- to know easily what are the new items (with a simple sql query).
- to display an icon in the search results.
- issuing rules can be adapt for new items. Automatically (using the
  cronjob script), the status change (depending the configuration) and
  the item can be issued, for example.
- a RSS/Atom feeds can be created on these new items.

Test plan:
- log in with a librarian having the tools > items_batchmod permission.
- navigate to Home > Tools > Automatic item modifications by age (was: Toggle new status)
- click on the edit button
- there are 3 "blocks":
  * duration: the duration during an item is considered as new.
  * conditions: the status will change only if the conditions are meet.
  * substitutions: if there is no substitution, no action will be done.
    You can add some change to apply to the matching items.
    E.g. ccode=3
         new=''
    If the value is an empty string (in other words, the input does not
    contain anything), the field will be deleted.
  You can create as many rules as you want.
- test the interface : add/remove rule, conditions, substitutions,
  submit the form, edit, etc.
  (There is a looot of JS everywhere, so certainly a looot of bugs...).
- when you have your rules defined, you can now launch the cronjob
  script without any parameter.
  A report will be displayed with the matching itemnumber and the
  substitutions to apply. Verify results are consistent.
- launch the script with the -c argument and verify values have been
  modified depending the substitution rules.

Signed-off-by: juliette et remy <juliette.levast@iepg.fr>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023: Add the ability to specify fields from biblioitems table.

Test plan:
Same as before but try with fields from the biblioitems table.

Signed-off-by: juliette et remy <juliette.levast@iepg.fr>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023: Add UT for C4::Items::ToggleNewStatus

Test plan:
prove t/db_dependent/Items/ToggleNewStatus.t

Signed-off-by: juliette et remy <juliette.levast@iepg.fr>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023: FIX - condition on biblioitems table does not work

If a rule contains a condition on the biblioitems table, the match won't
work. This patch fixes this issue.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023: Use DBIx-Class to retrieve column names

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023: Don't use the biblioitems fields for the subtitution

It's dangerous to allow a change on the biblioitems fields with this
feature.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023: Rename the duration parameter with 'age'

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023: The age parameter should be a number

The template should check if the age parameter is correctly filled
(should be a number).

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023: Change the name of the feature

Originaly this feature only permits to update the "new" field.
Now all item fields can be updated.
The name of the feature is now "Automatic item modifications by age".

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023 [QA Followup]

* Update DB version
* Fix capitalization error
* Rename misc/cronjobs/toggle_new_status.pl to misc/cronjobs/automatic_item_modification_by_age.pl

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Bug 11023 [QA Followup] - Complete the renaming of "toggle new status" to "automatic item modification by age"

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Brendan A Gallagher <brendan@bywatersolutions.com>
2016-03-02 22:56:31 +00:00

119 lines
3.6 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Copyright 2013 BibLibre
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
=head1 NAME
automatic_item_modification_by_age.pl: Update new status for items.
=cut
=head1 DESCRIPTION
This script allows a user to update the new status for items.
=cut
use Modern::Perl;
use CGI;
use JSON qw( to_json from_json );
use C4::Auth;
use C4::Context;
use C4::Items;
use C4::Output;
use C4::Koha;
my $cgi = new CGI;
# open template
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => "tools/automatic_item_modification_by_age.tt",
query => $cgi,
type => "intranet",
authnotrequired => 0,
flagsrequired => { tools => 'items_batchmod' },
}
);
my $op = $cgi->param('op') // 'show';
my $syspref_name = q|automatic_item_modification_by_age_configuration|;
if ( $op eq 'update' ) {
my @rules;
my @unique_ids = $cgi->param('unique_id');
for my $unique_id ( @unique_ids ) {
my @substitution_fields = $cgi->param("substitution_field_$unique_id");
my @substitution_values = $cgi->param("substitution_value_$unique_id");
my @condition_fields = $cgi->param("condition_field_$unique_id");
my @condition_values = $cgi->param("condition_value_$unique_id");
my $rule = {
substitutions => [],
conditions => [],
};
for my $value ( @substitution_values ) {
my $field = shift @substitution_fields;
last unless $field;
push @{ $rule->{substitutions} }, { field => $field, value => $value };
}
push @{ $rule->{substitutions} }, {}
unless @{ $rule->{substitutions} };
for my $value ( @condition_values ) {
my $field = shift @condition_fields;
last unless $field;
push @{ $rule->{conditions} }, { field => $field, value => $value };
}
push @{ $rule->{conditions} }, {}
unless @{ $rule->{conditions} };
$rule->{age} = $cgi->param("age_$unique_id");
push @rules, $rule;
}
my $syspref_content = to_json( \@rules );
C4::Context->set_preference($syspref_name, $syspref_content);
$op = 'show';
}
my @messages;
my $syspref_content = C4::Context->preference($syspref_name);
my $rules;
$rules = eval { JSON::from_json( $syspref_content ) }
if $syspref_content;
if ( $@ ) {
push @messages, {
type => 'error',
code => 'unable_to_load_configuration'
};
$template->param( messages => \@messages );
output_html_with_http_headers $cgi, $cookie, $template->output;
exit;
}
my @item_fields = map { "items.$_" } C4::Items::columns;
my @biblioitem_fields = map { "biblioitems.$_" } C4::Items::biblioitems_columns;
$template->param(
op => $op,
messages => \@messages,
condition_fields => [ @item_fields, @biblioitem_fields ],
substitution_fields => \@item_fields,
rules => $rules,
);
output_html_with_http_headers $cgi, $cookie, $template->output;