--- loncom/interface/loncreateuser.pm 2007/08/16 21:16:39 1.173 +++ loncom/interface/loncreateuser.pm 2007/08/26 21:09:43 1.179 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Create a user # -# $Id: loncreateuser.pm,v 1.173 2007/08/16 21:16:39 raeburn Exp $ +# $Id: loncreateuser.pm,v 1.179 2007/08/26 21:09:43 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -264,13 +264,14 @@ ENDCUSTOM sub entry_form { my ($dom,$srch,$forcenewuser) = @_; my $userpicker = - &Apache::loncommon::user_picker($dom,$srch,$forcenewuser); + &Apache::loncommon::user_picker($dom,$srch,$forcenewuser, + 'document.crtuser'); my $srchbutton = &mt('Search'); my $output = <<"ENDDOCUMENT";
$userpicker - +
ENDDOCUMENT return $output; @@ -305,7 +306,7 @@ END # =================================================================== Phase two sub print_user_selection_page { - my ($r,$response,$srch,$srch_results) = @_; + my ($r,$response,$srch,$srch_results,$context,$srcharray) = @_; my @fields = ('username','domain','lastname','firstname','permanentemail'); my $sortby = $env{'form.sortby'}; @@ -329,25 +330,34 @@ $jsback ENDSCRIPT my %lt=&Apache::lonlocal::texthash( - 'srch' => "User Search to add/modify roles", + 'usrch' => "User Search to add/modify roles", + 'stusrch' => "User Search to enroll student", + 'usel' => "Select a user to add/modify roles", + 'stusel' => "Select a user to enroll as a student", 'username' => "username", 'domain' => "domain", 'lastname' => "last name", 'firstname' => "first name", 'permanentemail' => "permanent e-mail", ); - $r->print(&Apache::loncommon::start_page('Create Users, Change User Privileges',$jscript)); - &Apache::lonhtmlcommon::add_breadcrumb - ({href=>"javascript:backPage(document.usersrchform,'','')", - text=>"User modify/custom role edit", - faq=>282,bug=>'Instructor Interface',}, - {href=>"javascript:backPage(document.usersrchform,'get_user_info','select')", - text=>"Select User", - faq=>282,bug=>'Instructor Interface',}); - $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management')); - $r->print("$lt{'srch'}
"); - $r->print(&entry_form($srch->{'srchdomain'},$srch)); - $r->print('

'.&mt('Select a user to add/modify roles').'

'); + if ($context eq 'createuser') { + $r->print(&Apache::loncommon::start_page('Create Users, Change User Privileges',$jscript)); + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.usersrchform,'','')", + text=>"User modify/custom role edit", + faq=>282,bug=>'Instructor Interface',}, + {href=>"javascript:backPage(document.usersrchform,'get_user_info','select')", + text=>"Select User", + faq=>282,bug=>'Instructor Interface',}); + $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management')); + $r->print("$lt{'usrch'}
"); + $r->print(&entry_form($srch->{'srchdomain'},$srch)); + $r->print('

'.$lt{'usel'}.'

'); + } else { + $r->print($jscript."$lt{'stusrch'}
"); + $r->print(&Apache::londropadd::single_user_entry_form($srch->{'srchdomain'},$srch)); + $r->print('

'.$lt{'stusel'}.'

'); + } $r->print('
'. &Apache::loncommon::start_data_table()."\n". &Apache::loncommon::start_data_table_header_row()."\n". @@ -381,19 +391,27 @@ ENDSCRIPT $r->print(&Apache::loncommon::end_data_table_row()); } $r->print(&Apache::loncommon::end_data_table().'

'); - $r->print(&Apache::lonhtmlcommon::echo_form_input(['sortby','seluname','seludom','state','phase'])); + if (ref($srcharray) eq 'ARRAY') { + foreach my $item (@{$srcharray}) { + $r->print(''."\n"); + } + } $r->print(' '."\n". ' '."\n". ' '."\n". - ' '."\n". - ' '."\n". - '
'); + ' '."\n". + ' '."\n"); $r->print($response); - $r->print(&Apache::loncommon::end_page()); + if ($context eq 'createuser') { + $r->print(''.&Apache::loncommon::end_page()); + } else { + $r->print(''."\n". + ''."\n"); + } } sub print_user_query_page { - my ($r) = @_; + my ($r,$caller) = @_; # FIXME - this is for a network-wide name search (similar to catalog search) # To use frames with similar behavior to catalog/portfolio search. # To be implemented. @@ -1204,8 +1222,8 @@ ENDNOPRIV $r->print(&course_level_table(%inccourses)); $r->print('
'."\n"); } - $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','state'])); - $r->print(''); + $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','currstate'])); + $r->print(''); $r->print(''); $r->print("".&Apache::loncommon::end_page()); } @@ -1446,7 +1464,7 @@ sub update_user_data { $changeHash{'middlename'} = $env{'form.cmiddlename'}; $changeHash{'lastname'} = $env{'form.clastname'}; $changeHash{'generation'} = $env{'form.cgeneration'}; - $changeHash{'permanentemail'} = $env{'form.permanentemail'}; + $changeHash{'permanentemail'} = $env{'form.cpermanentemail'}; my $putresult = &Apache::lonnet::put ('environment',\%changeHash, $env{'form.ccdomain'},$env{'form.ccuname'}); @@ -1472,7 +1490,7 @@ sub update_user_data { $lt{'lst'} $lt{'gen'} $lt{'mail'} - $lt{'disk'} + $lt{'disk'} $lt{'prvs'} $userenv{'firstname'} $userenv{'middlename'} @@ -1750,7 +1768,7 @@ END } } $r->print(''."\n". - ''."\n". + ''."\n". ''); $r->print(&Apache::loncommon::end_page()); } @@ -1949,7 +1967,7 @@ ENDCCF } $r->print(&Apache::loncommon::end_data_table(). ''."\n".''."\n". + '" />'."\n".''."\n". ''."\n". ''. &Apache::loncommon::end_page()); @@ -2142,11 +2160,11 @@ sub handler { $srch->{$item} = $env{'form.'.$item}; } if ($env{'form.phase'} eq 'get_user_info') { - my ($state,$response,$forcenewuser,$results) = + my ($currstate,$response,$forcenewuser,$results) = &user_search_result($srch); - if ($state eq 'select') { - &print_user_selection_page($r,$response,$srch,$results); - } elsif ($state eq 'modify') { + if ($currstate eq 'select') { + &print_user_selection_page($r,$response,$srch,$results,'createuser',\@search); + } elsif ($currstate eq 'modify') { my ($ccuname,$ccdomain); if (($srch->{'srchby'} eq 'uname') && ($srch->{'srchtype'} eq 'exact')) { @@ -2160,8 +2178,8 @@ sub handler { $ccdomain=&LONCAPA::clean_domain($ccdomain); &print_user_modification_page($r,$ccuname,$ccdomain,$srch, $response); - } elsif ($state eq 'query') { - &print_user_query_page($r); + } elsif ($currstate eq 'query') { + &print_user_query_page($r,'createuser'); } else { &print_username_entry_form($r,$response,$srch,$forcenewuser); } @@ -2193,15 +2211,17 @@ sub user_search_result { my %allhomes; my %inst_matches; my %srch_results; - my ($response,$state,$forcenewuser); + my ($response,$currstate,$forcenewuser); + $srch->{'srchterm'} =~ s/^\s+//; + $srch->{'srchterm'} =~ s/\s+$//; - if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) { + if ($srch->{'srchby'} !~ /^(uname|lastname|lastfirst)$/) { $response = &mt('Invalid search.'); } if ($srch->{'srchin'} !~ /^(crs|dom|alc|instd)$/) { $response = &mt('Invalid search.'); } - if ($srch->{'srchtype'} !~ /^(exact|contains)$/) { + if ($srch->{'srchtype'} !~ /^(exact|contains|begins)$/) { $response = &mt('Invalid search.'); } if ($srch->{'srchterm'} eq '') { @@ -2215,8 +2235,10 @@ sub user_search_result { } if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') || ($srch->{'srchin'} eq 'alc')) { - if ($srch->{'srchterm'} !~ /^$match_username$/) { - $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @'); + if ($srch->{'srchby'} eq 'uname') { + if ($srch->{'srchterm'} !~ /^$match_username$/) { + $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @'); + } } } if ($srch->{'srchin'} eq 'instd') { @@ -2226,7 +2248,7 @@ sub user_search_result { } } if ($response ne '') { - return ($state,''.$response.''); + return ($currstate,''.$response.''); } if ($srch->{'srchby'} eq 'uname') { if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) { @@ -2237,40 +2259,50 @@ sub user_search_result { my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description'); $response = &mt('New users can only be created in the domain to which you current role belongs - [_1].',$env{'request.role.domain'}.' ('.$domdesc.')'); } else { - $state = 'modify'; + $currstate = 'modify'; } } else { - $state = 'modify'; + $currstate = 'modify'; } } else { if ($srch->{'srchin'} eq 'dom') { if ($srch->{'srchtype'} eq 'exact') { my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'}); if ($uhome eq 'no_host') { - ($state,$response,$forcenewuser) = + ($currstate,$response,$forcenewuser) = &build_search_response($srch,%srch_results); } else { - $state = 'modify'; + $currstate = 'modify'; } } else { %srch_results = &Apache::lonnet::usersearch($srch); - ($state,$response,$forcenewuser) = + ($currstate,$response,$forcenewuser) = &build_search_response($srch,%srch_results); } } else { my $courseusers = &get_courseusers(); if ($srch->{'srchtype'} eq 'exact') { if (exists($courseusers->{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) { - $state = 'modify'; + $currstate = 'modify'; } else { - ($state,$response,$forcenewuser) = + ($currstate,$response,$forcenewuser) = &build_search_response($srch,%srch_results); } } else { foreach my $user (keys(%$courseusers)) { my ($cuname,$cudomain) = split(/:/,$user); if ($cudomain eq $srch->{'srchdomain'}) { - if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) { + my $matched = 0; + if ($srch->{'srchtype'} eq 'begins') { + if ($cuname =~ /^\Q$srch->{'srchterm'}\E/i) { + $matched = 1; + } + } else { + if ($cuname =~ /\Q$srch->{'srchterm'}\E/i) { + $matched = 1; + } + } + if ($matched) { $srch_results{$user} = {&Apache::lonnet::get('environment', ['firstname', @@ -2279,22 +2311,22 @@ sub user_search_result { } } } - ($state,$response,$forcenewuser) = + ($currstate,$response,$forcenewuser) = &build_search_response($srch,%srch_results); } } } } elsif ($srch->{'srchin'} eq 'alc') { - $state = 'query'; + $currstate = 'query'; } elsif ($srch->{'srchin'} eq 'instd') { %srch_results = &Apache::lonnet::inst_directory_query($srch); - ($state,$response,$forcenewuser) = + ($currstate,$response,$forcenewuser) = &build_search_response($srch,%srch_results); } } else { if ($srch->{'srchin'} eq 'dom') { %srch_results = &Apache::lonnet::usersearch($srch); - ($state,$response,$forcenewuser) = + ($currstate,$response,$forcenewuser) = &build_search_response($srch,%srch_results); } elsif ($srch->{'srchin'} eq 'crs') { my $courseusers = &get_courseusers(); @@ -2305,6 +2337,8 @@ sub user_search_result { if ($srch->{'srchby'} eq 'lastname') { if ((($srch->{'srchtype'} eq 'exact') && ($names{'lastname'} eq $srch->{'srchterm'})) || + (($srch->{'srchtype'} eq 'begins') && + ($names{'lastname'} =~ /^\Q$srch->{'srchterm'}\E/i)) || (($srch->{'srchtype'} eq 'contains') && ($names{'lastname'} =~ /\Q$srch->{'srchterm'}\E/i))) { $srch_results{$user} = {firstname => $names{'firstname'}, @@ -2314,6 +2348,8 @@ sub user_search_result { } } elsif ($srch->{'srchby'} eq 'lastfirst') { my ($srchlast,$srchfirst) = split(/,/,$srch->{'srchterm'}); + $srchlast =~ s/\s+$//; + $srchfirst =~ s/^\s+//; if ($srch->{'srchtype'} eq 'exact') { if (($names{'lastname'} eq $srchlast) && ($names{'firstname'} eq $srchfirst)) { @@ -2323,7 +2359,15 @@ sub user_search_result { }; } - } elsif ($srch->{'srchtype'} eq 'contains') { + } elsif ($srch->{'srchtype'} eq 'begins') { + if (($names{'lastname'} =~ /^\Q$srchlast\E/i) && + ($names{'firstname'} =~ /^\Q$srchfirst\E/i)) { + $srch_results{$user} = {firstname => $names{'firstname'}, + lastname => $names{'lastname'}, + permanentemail => $emails{'permanentemail'}, + }; + } + } else { if (($names{'lastname'} =~ /\Q$srchlast\E/i) && ($names{'firstname'} =~ /\Q$srchfirst\E/i)) { $srch_results{$user} = {firstname => $names{'firstname'}, @@ -2334,17 +2378,17 @@ sub user_search_result { } } } - ($state,$response,$forcenewuser) = + ($currstate,$response,$forcenewuser) = &build_search_response($srch,%srch_results); } elsif ($srch->{'srchin'} eq 'alc') { - $state = 'query'; + $currstate = 'query'; } elsif ($srch->{'srchin'} eq 'instd') { %srch_results = &Apache::lonnet::inst_directory_query($srch); - ($state,$response,$forcenewuser) = + ($currstate,$response,$forcenewuser) = &build_search_response($srch,%srch_results); } } - return ($state,$response,$forcenewuser,\%srch_results); + return ($currstate,$response,$forcenewuser,\%srch_results); } sub directorysrch_check { @@ -2393,7 +2437,8 @@ sub directorysrch_check { lastfirst => 'last name, first name', lastname => 'last name', contains => 'contains', - exact => 'as exact match to' + exact => 'as exact match to', + begins => 'begins with', ); if ($can_search) { if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') { @@ -2405,11 +2450,20 @@ sub directorysrch_check { } } if ($can_search) { - if (($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') || - ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) { - return 'ok'; - } else { - return &mt('Directory search in domain [_1] is not available for the requested search type: "[_2]"',$srch->{'srchdomain'},$longtext{$srch->{'srchtype'}}); + if (ref($dom_inst_srch{'directorysrch'}{'searchtypes'}) eq 'ARRAY') { + if (grep(/^\Q$srch->{'srchtype'}\E/,@{$dom_inst_srch{'directorysrch'}{'searchtypes'}})) { + return 'ok'; + } else { + return &mt('Directory search in domain [_1] is not available for the requested search type: "[_2]"',$srch->{'srchdomain'},$longtext{$srch->{'srchtype'}}); + } + } else { + if ((($dom_inst_srch{'directorysrch'}{'searchtypes'} eq 'specify') && + ($srch->{'srchtype'} eq 'exact' || $srch->{'srchtype'} eq 'contains')) || + ($dom_inst_srch{'directorysrch'}{'searchtypes'} eq $srch->{'srchtype'})) { + return 'ok'; + } else { + return &mt('Directory search in domain [_1] is not available for the requested search type: "[_2]"',$srch->{'srchdomain'},$longtext{$srch->{'srchtype'}}); + } } } } @@ -2431,7 +2485,7 @@ sub get_courseusers { sub build_search_response { my ($srch,%srch_results) = @_; - my ($state,$response,$forcenewuser); + my ($currstate,$response,$forcenewuser); my %names = ( 'uname' => 'username', 'lastname' => 'last name', @@ -2450,10 +2504,10 @@ sub build_search_response { exact => 'No exact match', ); if (keys(%srch_results) > 1) { - $state = 'select'; + $currstate = 'select'; } else { if (keys(%srch_results) == 1) { - $state = 'modify'; + $currstate = 'modify'; $response = &mt("$single{$srch->{'srchtype'}} was found for this $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'}); } else { $response = ''.&mt("$nomatch{$srch->{'srchtype'}} found for this $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'}).''; @@ -2481,7 +2535,7 @@ sub build_search_response { } } } - return ($state,$response,$forcenewuser); + return ($currstate,$response,$forcenewuser); } sub crumb_utilities { @@ -2497,12 +2551,19 @@ sub crumb_utilities { rolename => 'selectbox', newrolename => 'textbox', }, + studentform => { + srchterm => 'text', + srchin => 'selectbox', + srchby => 'selectbox', + srchtype => 'selectbox', + srchdomain => 'selectbox', + }, ); my $jsback .= qq| function backPage(formname,prevphase,prevstate) { formname.phase.value = prevphase; - formname.state.value = prevstate; + formname.currstate.value = prevstate; formname.submit(); } |;