Bug 22844: Make the patron's attribute mandatory at the OPAC
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 28 Apr 2020 11:09:49 +0000 (13:09 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 20 Jul 2020 14:08:22 +0000 (16:08 +0200)
Test plan:
- Set 1+ patron's attribute(s) mandatory
- Use the self-registration feature and confirm that you cannot selfreg
if the attribute has no value (or empty string)
- Same with the modification form (logged in)

Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt
opac/opac-memberentry.pl

index b28d6c9..7acd45a 100644 (file)
                                     [% IF loop.first %]<a name="patron-attr-start-[% pa.type.code | html %]"></a>[% END %]
                                     [% form_id = 'patron-attr-' _ Math.int( Math.rand(1000000) ) %]
                                     <li data-category_code="[% pa.type.category_code | html %]">
-                                        <label for="[% form_id | html %]">[% pa.type.description | html %]: </label>
+                                        [% IF pa.type.mandatory && pa.type.opac_editable %]
+                                            <label for="[% form_id | html %]" class="required">[% pa.type.description | html %]: </label>
+                                        [% ELSE %]
+                                            <label for="[% form_id | html %]">[% pa.type.description | html %]: </label>
+                                        [% END %]
                                         [% IF pa.type.opac_editable %]
                                             <input type="hidden" name="patron_attribute_code" value="[% pa.type.code | html %]" />
                                             [% IF ( pa.type.authorised_value_category ) %]
                                             [% ELSE %]
                                                 <textarea rows="2" cols="30" id="[% form_id | html %]" name="patron_attribute_value">[% pa_value | html %]</textarea>
                                             [% END %]
+                                            [% IF pa.type.mandatory %]
+                                                <span class="required">Required</span>
+                                            [% END %]
                                             <a href="#" class="clear-attribute">Clear</a>
                                             [% IF ( pa.type.repeatable ) %]
                                             <a href="#" class="clone-attribute">New</a>
index 3825403..a076cd3 100755 (executable)
@@ -123,7 +123,7 @@ if ( $action eq 'create' ) {
 
     %borrower = DelEmptyFields(%borrower);
 
-    my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
+    my @empty_mandatory_fields = (CheckMandatoryFields( \%borrower, $action ), CheckMandatoryAttributes( \%borrower, $attributes ) );
     my $invalidformfields = CheckForInvalidFields(\%borrower);
     delete $borrower{'password2'};
     my $cardnumber_error_code;
@@ -259,7 +259,7 @@ elsif ( $action eq 'update' ) {
     $borrower{borrowernumber} = $borrowernumber;
 
     my @empty_mandatory_fields =
-      CheckMandatoryFields( \%borrower, $action );
+      ( CheckMandatoryFields( \%borrower, $action ), CheckMandatoryAttributes( \%borrower, $attributes ) );
     my $invalidformfields = CheckForInvalidFields(\%borrower);
 
     # Send back the data to the template
@@ -412,6 +412,20 @@ sub CheckMandatoryFields {
     return @empty_mandatory_fields;
 }
 
+sub CheckMandatoryAttributes{
+    my ( $borrower, $attributes ) = @_;
+
+    my @empty_mandatory_fields;
+
+    for my $attribute (@$attributes ) {
+        my $attr = Koha::Patron::Attribute::Types->find($attribute->{code});
+        push @empty_mandatory_fields, $attribute->{code}
+            if $attr && $attr->mandatory && $attribute->{attribute} =~ m|^\s*$|;
+    }
+
+    return @empty_mandatory_fields;
+}
+
 sub CheckForInvalidFields {
     my $borrower = shift;
     my @invalidFields;
@@ -691,7 +705,8 @@ sub ParsePatronAttributes {
     }
 
     foreach my $code ( keys %{$delete_candidates} ) {
-        if ( Koha::Patron::Attributes->search({
+        if ( not $borrowernumber # self-registration
+            || Koha::Patron::Attributes->search({
                 borrowernumber => $borrowernumber, code => $code })->count > 0 )
         {
             push @attributes, { code => $code, attribute => '' }