--- loncom/interface/loncommon.pm 2007/11/09 23:05:47 1.611 +++ loncom/interface/loncommon.pm 2007/11/10 03:51:46 1.612 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # a pile of common routines # -# $Id: loncommon.pm,v 1.611 2007/11/09 23:05:47 albertel Exp $ +# $Id: loncommon.pm,v 1.612 2007/11/10 03:51:46 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -6430,57 +6430,127 @@ END_BLOCK return $output; } -sub username_rule_check { - my ($srch,$caller) = @_; - my ($response,@curr_rules,%inst_results,$rulematch); - my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($srch->{'srchdomain'}); - if (ref($srch) eq 'HASH') { - (my $inst_response,%inst_results) = - &Apache::lonnet::get_instuser($srch->{'srchdomain'}, - $srch->{'srchterm'}); - my %domconfig = &Apache::lonnet::get_dom('configuration', - ['usercreation'],$srch->{'srchdomain'}); - if (ref($domconfig{'usercreation'}) eq 'HASH') { - if (ref($domconfig{'usercreation'}{'username_rule'}) eq 'ARRAY') { - @curr_rules = @{$domconfig{'usercreation'}{'username_rule'}}; - } - } - if (@curr_rules > 0) { - my $domdesc = &Apache::lonnet::domain($srch->{'srchdomain'},'description'); - my $instuser_reqd; - my %rule_check = &Apache::lonnet::inst_rulecheck($srch->{'srchdomain'},$srch->{'srchterm'},\@curr_rules); - foreach my $rule (@curr_rules) { - if ($rule_check{$rule}) { - $rulematch = $rule; - if ($inst_response eq 'ok') { - if (keys(%inst_results) == 0) { - if ($caller eq 'new') { - $response = &mt('The username you chose matches the format of usernames defined for [_1], but the user does not exist in the institutional directory.',$domdesc).'
'.&mt("You must choose a username with a different format -- one that will not conflict with 'official' institutional usernames."); - } +sub user_rule_check { + my ($usershash,$checks,$alerts,$rulematch,$inst_results,$curr_rules) = @_; + my $response; + if (ref($usershash) eq 'HASH') { + my %got_rules; + foreach my $user (keys(%{$usershash})) { + my ($uname,$udom) = split(/:/,$user); + next if ($udom eq '' || $uname eq ''); + my ($userstatus,$id); + if (ref($usershash->{$user}) eq 'HASH') { + $userstatus = $usershash->{$user}->{'status'}; + $id = $usershash->{$user}->{'id'}; + } + my $inst_response; + if (ref($checks) eq 'HASH') { + if (defined($checks->{'username'})) { + ($inst_response,%{$inst_results}) = + &Apache::lonnet::get_instuser($udom,$uname); + } elsif (defined($checks->{'id'})) { + ($inst_response,%{$inst_results}) = + &Apache::lonnet::get_instuser($udom,undef,$id); + } + } + if (!$got_rules{$udom}) { + my %domconfig = &Apache::lonnet::get_dom('configuration', + ['usercreation'],$udom); + if (ref($domconfig{'usercreation'}) eq 'HASH') { + foreach my $item (keys(%{$checks})) { + if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') { + $$curr_rules{$udom}{$item} = + $domconfig{'usercreation'}{$item.'_rule'}; } } - last; } + $got_rules{$udom} = 1; } - if ($response) { - if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) { - if (@{$ruleorder} > 0) { - $response .= '
'.&mt('Usernames with the following format(s) may only be used for verified users at [_1]:',$domdesc).' '; } } } } - return ($response,$rulematch,$rules,%inst_results); + return; +} + +sub user_rule_formats { + my ($domain,$domdesc,$curr_rules,$check) = @_; + my %text = ( + 'username' => 'Usernames', + 'id' => 'IDs', + ); + my $output; + my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($domain,$check); + if ((ref($rules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) { + if (@{$ruleorder} > 0) { + $output = '
'.&mt("$text{$check} with the following format(s) may only be used for verified users at [_1]:",$domdesc).' '; + } + } + return $output; +} + +sub instrule_disallow_msg { + my ($checkitem,$domdesc,$count) = @_; + my $response; + my %text = ( + item => 'username', + items => 'usernames', + match => 'matches', + do => 'does', + action => 'a username', + one => 'one', + ); + if ($count > 1) { + $text{'item'} = 'usernames'; + $text{'match'} ='match'; + $text{'do'} = 'do'; + $text{'action'} = 'usernames', + $text{'one'} = 'ones'; + } + if ($checkitem eq 'id') { + $text{'items'} = 'IDs'; + $text{'item'} = 'ID'; + $text{'action'} = 'an ID'; + } + $response = &mt("The $text{'item'} you chose $text{'match'} the format of $text{'items'} defined for [_1], but the $text{'item'} $text{'do'} not exist in the institutional directory.",$domdesc).'
'; + if ($checkitem eq 'username') { + $response .= &mt("You must choose $text{'action'} with a different format -- $text{'one'} that will not conflict with 'official' institutional $text{'items'}."); + } elsif ($checkitem eq 'id') { + $response .= &mt("You must either choose $text{'action'} with a different format -- $text{'one'} that will not conflict with 'official' institutional $text{'items'}, or leave this field blank."); + } + return $response; } =pod