--- loncom/interface/loncreateuser.pm 2007/07/20 23:52:55 1.157 +++ loncom/interface/loncreateuser.pm 2007/10/03 13:45:58 1.184.2.1 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Create a user # -# $Id: loncreateuser.pm,v 1.157 2007/07/20 23:52:55 albertel Exp $ +# $Id: loncreateuser.pm,v 1.184.2.1 2007/10/03 13:45:58 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -65,7 +65,6 @@ use Apache::lonnet; use Apache::loncommon; use Apache::lonlocal; use Apache::longroup; -use lib '/home/httpd/lib/perl/'; use LONCAPA qw(:DEFAULT :match); my $loginscript; # piece of javascript used in two separate instances @@ -194,46 +193,63 @@ END_SCRIPT # =================================================================== Phase one sub print_username_entry_form { - my ($r) = @_; + my ($r,$response,$srch,$forcenewuser) = @_; my $defdom=$env{'request.role.domain'}; - my $domform = &Apache::loncommon::select_dom_form($defdom,'ccdomain'); - my $selscript=&Apache::loncommon::studentbrowser_javascript(); + my $formtoset = 'crtuser'; + if (exists($env{'form.startrolename'})) { + $formtoset = 'docustom'; + $env{'form.rolename'} = $env{'form.startrolename'}; + } + + my ($jsback,$elements) = &crumb_utilities(); + + my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n". + ''."\n"; + + my %loaditems = ( + 'onload' => "javascript:setFormElements(document.$formtoset)", + ); my $start_page = &Apache::loncommon::start_page('Create Users, Change User Privileges', - $selscript); + $jscript,{'add_entries' => \%loaditems,}); + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.crtuser)", + text=>"User modify/custom role edit", + faq=>282,bug=>'Instructor Interface',}); - my $sellink=&Apache::loncommon::selectstudent_link - ('crtuser','ccuname','ccdomain'); + my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('User Management'); my %existingroles=&my_custom_roles(); my $choice=&Apache::loncommon::select_form('make new role','rolename', ('make new role' => 'Generate new role ...',%existingroles)); my %lt=&Apache::lonlocal::texthash( - 'siur' => "Set Individual User Roles", + 'srch' => "User Search", + or => "or", + 'siur' => "Set Individual User Roles", 'usr' => "Username", 'dom' => "Domain", - 'usrr' => "User Roles", 'ecrp' => "Edit Custom Role Privileges", 'nr' => "Name of Role", - 'cre' => "Custom Role Editor" + 'cre' => "Custom Role Editor", + 'mod' => "to add/modify roles", ); my $help = &Apache::loncommon::help_open_menu(undef,undef,282,'Instructor Interface'); my $helpsiur=&Apache::loncommon::help_open_topic('Course_Change_Privileges'); my $helpecpr=&Apache::loncommon::help_open_topic('Course_Editing_Custom_Roles'); - $r->print(<<"ENDDOCUMENT"); + my $sellink=&Apache::loncommon::selectstudent_link('crtuser','srchterm','srchdomain'); + if ($sellink) { + $sellink = "$lt{'or'} ".$sellink; + } + $r->print(" $start_page -
- +$crumbs

$lt{siur}$helpsiur

- - -
$lt{usr}: -$sellink
-$lt{'dom'}:$domform
- -
-ENDDOCUMENT - if (&Apache::lonnet::allowed('mcr','/')) { - $r->print(<$lt{'srch'} $sellink $lt{'mod'} +$response"); + $r->print(&entry_form($defdom,$srch,$forcenewuser)); + if (&Apache::lonnet::allowed('mcr','/')) { + $r->print(<

$lt{'ecrp'}$helpecpr

@@ -245,6 +261,21 @@ ENDCUSTOM $r->print(&Apache::loncommon::end_page()); } +sub entry_form { + my ($dom,$srch,$forcenewuser) = @_; + my $userpicker = + &Apache::loncommon::user_picker($dom,$srch,$forcenewuser, + 'document.crtuser'); + my $srchbutton = &mt('Search'); + my $output = <<"ENDDOCUMENT"; +
+ +$userpicker + +
+ENDDOCUMENT + return $output; +} sub user_modification_js { my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_; @@ -274,16 +305,128 @@ END } # =================================================================== Phase two -sub print_user_modification_page { - my $r=shift; - my $ccuname =&LONCAPA::clean_username($env{'form.ccuname'}); - my $ccdomain=&LONCAPA::clean_domain($env{'form.ccdomain'}); +sub print_user_selection_page { + my ($r,$response,$srch,$srch_results,$context,$srcharray) = @_; + my @fields = ('username','domain','lastname','firstname','permanentemail'); + my $sortby = $env{'form.sortby'}; + + if (!grep(/^\Q$sortby\E$/,@fields)) { + $sortby = 'lastname'; + } + + my ($jsback,$elements) = &crumb_utilities(); + + my $jscript = (< +function pickuser(uname,udom) { + document.usersrchform.seluname.value=uname; + document.usersrchform.seludom.value=udom; + document.usersrchform.phase.value="userpicked"; + document.usersrchform.submit(); +} + +$jsback + +ENDSCRIPT + + my %lt=&Apache::lonlocal::texthash( + '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", + ); + 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". + ' '."\n"); + foreach my $field (@fields) { + $r->print(' '. + $lt{$field}.''."\n"); + } + $r->print(&Apache::loncommon::end_data_table_header_row()); + + my @sorted_users = sort { + lc($srch_results->{$a}->{$sortby}) cmp lc($srch_results->{$b}->{$sortby}) + || + lc($srch_results->{$a}->{lastname}) cmp lc($srch_results->{$b}->{lastname}) + || + lc($srch_results->{$a}->{firstname}) cmp lc($srch_results->{$b}->{firstname}) + || + lc($a) cmp lc($b) + } (keys(%$srch_results)); + foreach my $user (@sorted_users) { + my ($uname,$udom) = split(/:/,$user); + $r->print(&Apache::loncommon::start_data_table_row(). + ''. + ''.$uname.''. + ''.$udom.''); + foreach my $field ('lastname','firstname','permanentemail') { + $r->print(''.$srch_results->{$user}->{$field}.''); + } + $r->print(&Apache::loncommon::end_data_table_row()); + } + $r->print(&Apache::loncommon::end_data_table().'

'); + if (ref($srcharray) eq 'ARRAY') { + foreach my $item (@{$srcharray}) { + $r->print(''."\n"); + } + } + $r->print(' '."\n". + ' '."\n". + ' '."\n". + ' '."\n". + ' '."\n"); + $r->print($response); + if ($context eq 'createuser') { + $r->print('
'.&Apache::loncommon::end_page()); + } else { + $r->print(''."\n". + ''."\n"); + } +} + +sub print_user_query_page { + 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. + return; +} + +sub print_user_modification_page { + my ($r,$ccuname,$ccdomain,$srch,$response) = @_; unless (($ccuname) && ($ccdomain)) { &print_username_entry_form($r); return; } - + if ($response) { + $response = '
'.$response + } my $defdom=$env{'request.role.domain'}; my ($krbdef,$krbdefdom) = @@ -292,12 +435,10 @@ sub print_user_modification_page { my %param = ( formname => 'document.cu', kerb_def_dom => $krbdefdom, kerb_def_auth => $krbdef - ); + ); $loginscript = &Apache::loncommon::authform_header(%param); $authformkrb = &Apache::loncommon::authform_kerberos(%param); - $ccuname =&LONCAPA::clean_username($ccuname); - $ccdomain=&LONCAPA::clean_domain($ccdomain); my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition(); my $dc_setcourse_code = ''; my $nondc_setsection_code = ''; @@ -429,9 +570,8 @@ ENDSCRIPT var checkcurr = str.match(re1); if (checkcurr != null) { if (document.cu.elements[i-1].checked == true) { - var re2 = /^currsec_[a-zA-Z0-9]+_[a-zA-Z0-9]+_(\\w+)\$/; - match = re2.exec(str); - var role = match[1]; + var match = str.split('_'); + var role = match[3]; if (role == 'cc') { alert("Section designations do not apply to Course Coordinator roles.\\nA course coordinator role will be added with access to all sections."); } @@ -500,9 +640,31 @@ ENDSECCODE } my $js = &user_modification_js($pjump_def,$dc_setcourse_code, $nondc_setsection_code,$groupslist); + + my ($jsback,$elements) = &crumb_utilities(); + + $js .= "\n". + ''; + my $start_page = &Apache::loncommon::start_page('Create Users, Change User Privileges', $js,{'add_entries' => \%loaditem,}); + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.cu)", + text=>"User modify/custom role edit", + faq=>282,bug=>'Instructor Interface',}); + + if ($env{'form.phase'} eq 'userpicked') { + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.cu,'get_user_info','select')", + text=>"Select a user", + faq=>282,bug=>'Instructor Interface',}); + } + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.cu,'$env{'form.phase'}','modify')", + text=>"Set user role", + faq=>282,bug=>'Instructor Interface',}); + my $crumbs = &Apache::lonhtmlcommon::breadcrumbs('User Management'); my $forminfo =<<"ENDFORMINFO";
@@ -521,6 +683,33 @@ ENDFORMINFO } } if ($uhome eq 'no_host') { + my $newuser; + my $instsrch = { + srchin => 'instd', + srchby => 'uname', + srchtype => 'exact', + }; + if ($env{'form.phase'} eq 'userpicked') { + $instsrch->{'srchterm'} = $env{'form.seluname'}; + $instsrch->{'srchdomain'} = $env{'form.seludom'}; + } else { + $instsrch->{'srchterm'} = $ccuname; + $instsrch->{'srchdomain'} = $ccdomain, + } + if (($instsrch->{'srchterm'} ne '') && ($instsrch->{'srchdomain'} ne '')) { + $newuser = $instsrch->{'srchterm'}.':'.$instsrch->{'srchdomain'}; + } + my (%dirsrch_results,%inst_results,$dirsrchres); + if ($newuser) { + if (&directorysrch_check($instsrch) eq 'ok') { + ($dirsrchres,%dirsrch_results) = &Apache::lonnet::inst_directory_query($instsrch); + if ($dirsrchres eq 'ok') { + if (ref($dirsrch_results{$newuser}) eq 'HASH') { + %inst_results = %{$dirsrch_results{$newuser}}; + } + } + } + } my $home_server_list= ''."\n". &Apache::loncommon::home_server_option_list($ccdomain); @@ -534,6 +723,7 @@ ENDFORMINFO 'mn' => "Middle Name", 'ln' => "Last Name", 'gen' => "Generation", + 'mail' => "Permanent e-mail address", 'idsn' => "ID/Student Number", 'hs' => "Home Server", 'lg' => "Login Data" @@ -547,7 +737,9 @@ ENDFORMINFO &initialize_authen_forms(); $r->print(<$lt{'cnu'} +$response $forminfo

$lt{'nu'} "$ccuname" $lt{'id'} $ccdomain

'."\n"; + + $r->print(&Apache::loncommon::start_page($title,$jscript)); + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.userupdate)", + text=>"User modify/custom role edit", + faq=>282,bug=>'Instructor Interface',}); + if ($env{'form.prevphase'} eq 'userpicked') { + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.userupdate,'get_user_info','select')", + text=>"Select a user", + faq=>282,bug=>'Instructor Interface',}); + } + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.userupdate,'$env{'form.prevphase'}','modify')", + text=>"Set user role", + faq=>282,bug=>'Instructor Interface',}, + {href=>"/adm/createuser", + text=>"Result", + faq=>282,bug=>'Instructor Interface',}); + $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management')); + my %disallowed; # Check Inputs if (! $env{'form.ccuname'} ) { @@ -1055,7 +1287,7 @@ sub update_user_data { if ( $env{'form.ccuname'} ne &LONCAPA::clean_username($env{'form.ccuname'}) ) { $r->print($error.&mt('Invalid login name').'. '. - &mt('Only letters, numbers, and underscores are valid').'.'. + &mt('Only letters, numbers, periods, dashes, @, and underscores are valid').'.'. $end); return; } @@ -1106,15 +1338,13 @@ sub update_user_data { $r->print($error.&mt('Invalid login mode or password').$end); return; } + + + $r->print('

'.&mt('User [_1] in domain [_2]', + $env{'form.ccuname'}, $env{'form.ccdomain'}).'

'); + if ($env{'form.makeuser'}) { - # Create a new user - my %lt=&Apache::lonlocal::texthash( - 'cru' => "Creating user", - 'id' => "in domain" - ); - $r->print(<$lt{'cru'} "$env{'form.ccuname'}" $lt{'id'} "$env{'form.ccdomain'}" -ENDNEWUSERHEAD + $r->print('

'.&mt('Creating new account.').'

'); # Check for the authentication mode and password if (! $amode || ! $genpwd) { $r->print($error.&mt('Invalid login mode or password').$end); @@ -1137,7 +1367,7 @@ ENDNEWUSERHEAD ($env{'form.ccdomain'},$env{'form.ccuname'},$env{'form.cstid'}, $amode,$genpwd,$env{'form.cfirst'}, $env{'form.cmiddle'},$env{'form.clast'},$env{'form.cgen'}, - undef,$desiredhost + undef,$desiredhost,$env{'form.cemail'} ); $r->print(&mt('Generating user').': '.$result); my $home = &Apache::lonnet::homeserver($env{'form.ccuname'}, @@ -1147,13 +1377,6 @@ ENDNEWUSERHEAD } elsif (($env{'form.login'} ne 'nochange') && ($env{'form.login'} ne '' )) { # Modify user privileges - my %lt=&Apache::lonlocal::texthash( - 'usr' => "User", - 'id' => "in domain" - ); - $r->print(<$lt{'usr'} "$env{'form.ccuname'}" $lt{'id'} "$env{'form.ccdomain'}" -ENDMODIFYUSERHEAD if (! $amode || ! $genpwd) { $r->print($error.'Invalid login mode or password'.$end); return; @@ -1176,14 +1399,14 @@ ENDMODIFYUSERHEAD # Check for need to change my %userenv = &Apache::lonnet::get ('environment',['firstname','middlename','lastname','generation', - 'portfolioquota','inststatus'],$env{'form.ccdomain'}, - $env{'form.ccuname'}); + 'permanentemail','portfolioquota','inststatus'], + $env{'form.ccdomain'},$env{'form.ccuname'}); my ($tmp) = keys(%userenv); if ($tmp =~ /^(con_lost|error)/i) { %userenv = (); } # Check to see if we need to change user information - foreach my $item ('firstname','middlename','lastname','generation') { + foreach my $item ('firstname','middlename','lastname','generation','permanentemail') { # Strip leading and trailing whitespace $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g; } @@ -1241,7 +1464,8 @@ ENDMODIFYUSERHEAD ($env{'form.cfirstname'} ne $userenv{'firstname'} || $env{'form.cmiddlename'} ne $userenv{'middlename'} || $env{'form.clastname'} ne $userenv{'lastname'} || - $env{'form.cgeneration'} ne $userenv{'generation'} )) { + $env{'form.cgeneration'} ne $userenv{'generation'} || + $env{'form.cpermanentemail'} ne $userenv{'permanentemail'} )) { $namechanged = 1; } if ($namechanged) { @@ -1250,6 +1474,7 @@ ENDMODIFYUSERHEAD $changeHash{'middlename'} = $env{'form.cmiddlename'}; $changeHash{'lastname'} = $env{'form.clastname'}; $changeHash{'generation'} = $env{'form.cgeneration'}; + $changeHash{'permanentemail'} = $env{'form.cpermanentemail'}; my $putresult = &Apache::lonnet::put ('environment',\%changeHash, $env{'form.ccdomain'},$env{'form.ccuname'}); @@ -1261,6 +1486,7 @@ ENDMODIFYUSERHEAD 'mddl' => "middle", 'lst' => "last", 'gen' => "generation", + 'mail' => "permanent e-mail", 'disk' => "disk space allocated to portfolio files", 'prvs' => "Previous", 'chto' => "Changed To" @@ -1273,12 +1499,14 @@ ENDMODIFYUSERHEAD $lt{'mddl'} $lt{'lst'} $lt{'gen'} - $lt{'disk'} + $lt{'mail'} + $lt{'disk'} $lt{'prvs'} $userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} $userenv{'generation'} + $userenv{'permanentemail'} $oldportfolioquota Mb $lt{'chto'} @@ -1286,6 +1514,7 @@ ENDMODIFYUSERHEAD $env{'form.cmiddlename'} $env{'form.clastname'} $env{'form.cgeneration'} + $env{'form.cpermanentemail'} $newportfolioquota Mb $defquotatext END @@ -1312,15 +1541,12 @@ END # They did not want to change the users name but we can # still tell them what the name is my %lt=&Apache::lonlocal::texthash( - 'usr' => "User", - 'id' => "in domain", - 'gen' => "Generation", + 'mail' => "Permanent e-mail", 'disk' => "Disk space allocated to user's portfolio files", ); $r->print(<<"END"); -

$lt{'usr'} "$env{'form.ccuname'}" $lt{'id'} "$env{'form.ccdomain'}"

-

$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'}

-

$lt{'gen'}: $userenv{'generation'}

+

$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} $userenv{'generation'}

+

$lt{'mail'}: $userenv{'permanentemail'}

END if ($putresult eq 'ok') { if ($oldportfolioquota != $newportfolioquota) { @@ -1340,23 +1566,20 @@ END if ($key=~/^form\.rev/) { if ($key=~/^form\.rev\:([^\_]+)\_([^\_\.]+)$/) { # Revoke standard role - $r->print(&mt('Revoking').' '.$2.' in '.$1.': '. - &Apache::lonnet::revokerole($env{'form.ccdomain'}, - $env{'form.ccuname'},$1,$2).'
'); - if ($2 eq 'st') { - $1=~m{^/($match_domain)/($match_courseid)}; - my $cid=$1.'_'.$2; - $r->print(&mt('Drop from classlist').': '. - &Apache::lonnet::critical('put:'. - $env{'course.'.$cid.'.domain'}.':'. - $env{'course.'.$cid.'.num'}.':classlist:'. - &escape($env{'form.ccuname'}.':'. - $env{'form.ccdomain'}).'='. - &escape($now.':'), - $env{'course.'.$cid.'.home'}).'
'); + my ($scope,$role) = ($1,$2); + my $result = + &Apache::lonnet::revokerole($env{'form.ccdomain'}, + $env{'form.ccuname'}, + $scope,$role); + $r->print(&mt('Revoking [_1] in [_2]: [_3]', + $role,$scope,''.$result.'').'
'); + if ($role eq 'st') { + my $result = &classlist_drop($scope,$env{'form.ccuname'}, + $env{'form.ccdomain'},$now); + $r->print($result); } } - if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) { + if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$ }s) { # Revoke custom role $r->print(&mt('Revoking custom role:'). ' '.$4.' by '.$3.':'.$2.' in '.$1.': '. @@ -1367,26 +1590,23 @@ END } elsif ($key=~/^form\.del/) { if ($key=~/^form\.del\:([^\_]+)\_([^\_\.]+)$/) { # Delete standard role - $r->print(&mt('Deleting').' '.$2.' in '.$1.': '. - &Apache::lonnet::assignrole($env{'form.ccdomain'}, - $env{'form.ccuname'},$1,$2,$now,0,1).'
'); - if ($2 eq 'st') { - $1=~m{^/($match_domain)/($match_courseid)}; - my $cid=$1.'_'.$2; - $r->print(&mt('Drop from classlist').': '. - &Apache::lonnet::critical('put:'. - $env{'course.'.$cid.'.domain'}.':'. - $env{'course.'.$cid.'.num'}.':classlist:'. - &escape($env{'form.ccuname'}.':'. - $env{'form.ccdomain'}).'='. - &escape($now.':'), - $env{'course.'.$cid.'.home'}).'
'); + my ($scope,$role) = ($1,$2); + my $result = + &Apache::lonnet::assignrole($env{'form.ccdomain'}, + $env{'form.ccuname'}, + $scope,$role,$now,0,1); + $r->print(&mt('Deleting [_1] in [_2]: [_3]',$role,$scope, + ''.$result.'').'
'); + if ($role eq 'st') { + my $result = &classlist_drop($scope,$env{'form.ccuname'}, + $env{'form.ccdomain'},$now); + $r->print($result); } } if ($key=~m{^form\.del\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$}) { my ($url,$rdom,$rnam,$rolename) = ($1,$2,$3,$4); # Delete custom role - $r->print(&mt('Deleting custom role [_1] by [_2]@[_3] in [_4]', + $r->print(&mt('Deleting custom role [_1] by [_2]:[_3] in [_4]', $rolename,$rnam,$rdom,$url).': '. &Apache::lonnet::assigncustomrole($env{'form.ccdomain'}, $env{'form.ccuname'},$url,$rdom,$rnam,$rolename,$now, @@ -1547,10 +1767,46 @@ END } # End of foreach (keys(%env)) # Flush the course logs so reverse user roles immediately updated &Apache::lonnet::flushcourselogs(); - $r->print('

Create/Modify Another User

'); + $r->print('

'.&mt('Create/Modify Another User').'

'); + $r->print(''."\n"); + foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') { + $r->print(''."\n"); + } + foreach my $item ('sortby','seluname','seludom') { + if (exists($env{'form.'.$item})) { + $r->print(''."\n"); + } + } + $r->print(''."\n". + ''."\n". + ''); $r->print(&Apache::loncommon::end_page()); } +sub classlist_drop { + my ($scope,$uname,$udom,$now) = @_; + my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)}); + my $cid=$cdom.'_'.$cnum; + my $user = $uname.':'.$udom; + if (!&active_student_roles($cnum,$cdom,$uname,$udom)) { + my $result = + &Apache::lonnet::cput('classlist', + { $user => $now }, + $env{'course.'.$cid.'.domain'}, + $env{'course.'.$cid.'.num'}); + return &mt('Drop from classlist: [_1]', + ''.$result.'').'
'; + } +} + +sub active_student_roles { + my ($cnum,$cdom,$uname,$udom) = @_; + my %roles = + &Apache::lonnet::get_my_roles($uname,$udom,'userroles', + ['future','active'],['st']); + return exists($roles{"$cnum:$cdom:st"}); +} + sub quota_admin { my ($setquota,$changeHash) = @_; my $quotachanged; @@ -1598,7 +1854,7 @@ sub build_roles { # ========================================================== Custom Role Editor sub custom_role_editor { - my $r=shift; + my ($r) = @_; my $rolename=$env{'form.rolename'}; if ($rolename eq 'make new role') { @@ -1664,6 +1920,7 @@ sub custom_role_editor { } $full{$priv}=1; } + my ($jsback,$elements) = &crumb_utilities(); my $button_code = "\n"; my $head_script = "\n"; $head_script .= ''."\n"; + $head_script .= "\n".$jsback."\n".''."\n"; $r->print(&Apache::loncommon::start_page('Custom Role Editor',$head_script)); + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.form1,'','')", + text=>"User modify/custom role edit", + faq=>282,bug=>'Instructor Interface',}, + {href=>"javascript:backPage(document.form1,'','')", + text=>"Edit custom role", + faq=>282,bug=>'Instructor Interface',}); + $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management')); + $r->print($body_top); my %lt=&Apache::lonlocal::texthash( 'prv' => "Privilege", @@ -1685,7 +1951,7 @@ sub custom_role_editor { $r->print($button_code); $r->print(''); $r->print(< +
ENDCCF @@ -1710,7 +1976,10 @@ ENDCCF &Apache::loncommon::end_data_table_row()); } $r->print(&Apache::loncommon::end_data_table(). - '
'. + ''."\n".''."\n". + ''."\n". + ''. &Apache::loncommon::end_page()); } # -------------------------------------------------------- @@ -1798,11 +2067,27 @@ sub set_custom_role { return; } - $r->print(&Apache::loncommon::start_page('Save Custom Role').'

'); + my ($jsback,$elements) = &crumb_utilities(); + my $jscript = ''; + + $r->print(&Apache::loncommon::start_page('Save Custom Role'),$jscript); + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>"javascript:backPage(document.customresult,'','')", + text=>"User modify/custom role edit", + faq=>282,bug=>'Instructor Interface',}, + {href=>"javascript:backPage(document.customresult,'selected_custom_edit','')", + text=>"Edit custom role", + faq=>282,bug=>'Instructor Interface',}, + {href=>"javascript:backPage(document.customresult,'set_custom_roles','')", + text=>"Result", + faq=>282,bug=>'Instructor Interface',}); + $r->print(&Apache::lonhtmlcommon::breadcrumbs('User Management')); + my ($rdummy,$roledef)= &Apache::lonnet::get('roles',["rolesdef_$rolename"]); # ------------------------------------------------------- Does this role exist? + $r->print('

'); if (($rdummy ne 'con_lost') && ($roledef ne '')) { $r->print(&mt('Existing Role').' "'); } else { @@ -1851,7 +2136,8 @@ sub set_custom_role { $env{'user.name'}, $rolename)); } - $r->print('

Create another role, or Create/Modify a user.

'); + $r->print('

Create another role, or Create/Modify a user.

'); + $r->print(&Apache::lonhtmlcommon::echo_form_input([]).'
'); $r->print(&Apache::loncommon::end_page()); } @@ -1873,17 +2159,53 @@ sub handler { (&Apache::lonnet::allowed('mau',$env{'request.role.domain'}))) { &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; - if (!$env{'form.phase'}) { - &print_username_entry_form($r); - } - if ($env{'form.phase'} eq 'get_user_info') { - &print_user_modification_page($r); + &Apache::lonhtmlcommon::clear_breadcrumbs(); + + my $phase = $env{'form.phase'}; + my @search = ('srchterm','srchby','srchin','srchtype','srchdomain'); + + if (($phase eq 'get_user_info') || ($phase eq 'userpicked')) { + my $srch; + foreach my $item (@search) { + $srch->{$item} = $env{'form.'.$item}; + } + if ($env{'form.phase'} eq 'get_user_info') { + my ($currstate,$response,$forcenewuser,$results) = + &user_search_result($srch); + 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')) { + $ccuname = $srch->{'srchterm'}; + $ccdomain= $srch->{'srchdomain'}; + } else { + my @matchedunames = keys(%{$results}); + ($ccuname,$ccdomain) = split(/:/,$matchedunames[0]); + } + $ccuname =&LONCAPA::clean_username($ccuname); + $ccdomain=&LONCAPA::clean_domain($ccdomain); + &print_user_modification_page($r,$ccuname,$ccdomain,$srch, + $response); + } elsif ($currstate eq 'query') { + &print_user_query_page($r,'createuser'); + } else { + &print_username_entry_form($r,$response,$srch,$forcenewuser); + } + } elsif ($env{'form.phase'} eq 'userpicked') { + my $ccuname = &LONCAPA::clean_username($env{'form.seluname'}); + my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'}); + &print_user_modification_page($r,$ccuname,$ccdomain,$srch); + } } elsif ($env{'form.phase'} eq 'update_user_data') { &update_user_data($r); } elsif ($env{'form.phase'} eq 'selected_custom_edit') { &custom_role_editor($r); } elsif ($env{'form.phase'} eq 'set_custom_roles') { &set_custom_role($r); + } else { + &print_username_entry_form($r); } } else { $env{'user.error.msg'}= @@ -1891,9 +2213,422 @@ sub handler { return HTTP_NOT_ACCEPTABLE; } return OK; -} +} #-------------------------------------------------- functions for &phase_two +sub user_search_result { + my ($srch) = @_; + my %allhomes; + my %inst_matches; + my %srch_results; + my ($response,$currstate,$forcenewuser,$dirsrchres); + $srch->{'srchterm'} =~ s/\s+/ /g; + 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|begins)$/) { + $response = &mt('Invalid search.'); + } + if ($srch->{'srchterm'} eq '') { + $response = &mt('You must enter a search term.'); + } + if ($srch->{'srchterm'} =~ /^\s+$/) { + $response = &mt('Your search term must contain more than just spaces.'); + } + if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) { + if (($srch->{'srchdomain'} eq '') || + ! (&Apache::lonnet::domain($srch->{'srchdomain'}))) { + $response = &mt('You must specify a valid domain when searching in a domain or institutional directory.') + } + } + if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs') || + ($srch->{'srchin'} eq 'alc')) { + 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 ($response ne '') { + $response = ''.$response.''; + } + if ($srch->{'srchin'} eq 'instd') { + my $instd_chk = &directorysrch_check($srch); + if ($instd_chk ne 'ok') { + $response = ''.$instd_chk.''. + '
'.&mt('You may want to search in the LON-CAPA domain instead of the institutional directory.').'

'; + } + } + if ($response ne '') { + return ($currstate,$response); + } + if ($srch->{'srchby'} eq 'uname') { + if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'crs')) { + if ($env{'form.forcenew'}) { + if ($srch->{'srchdomain'} ne $env{'request.role.domain'}) { + my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'}); + if ($uhome eq 'no_host') { + my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description'); + my $showdom = &display_domain_info($env{'request.role.domain'}); + $response = &mt('New users can only be created in the domain to which your current role belongs - [_1].',$showdom); + } else { + $currstate = 'modify'; + } + } else { + $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') { + ($currstate,$response,$forcenewuser) = + &build_search_response($srch,%srch_results); + } else { + $currstate = 'modify'; + } + } else { + %srch_results = &Apache::lonnet::usersearch($srch); + ($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'}})) { + $currstate = 'modify'; + } else { + ($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'}) { + 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', + 'lastname', + 'permanentemail'])}; + } + } + } + ($currstate,$response,$forcenewuser) = + &build_search_response($srch,%srch_results); + } + } + } + } elsif ($srch->{'srchin'} eq 'alc') { + $currstate = 'query'; + } elsif ($srch->{'srchin'} eq 'instd') { + ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); + if ($dirsrchres eq 'ok') { + ($currstate,$response,$forcenewuser) = + &build_search_response($srch,%srch_results); + } else { + my $showdom = &display_domain_info($srch->{'srchdomain'}); + $response = ''. + &mt('Institutional directory search is not available in domain: [_1]',$showdom). + '
'. + &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.'). + '

'; + } + } + } else { + if ($srch->{'srchin'} eq 'dom') { + %srch_results = &Apache::lonnet::usersearch($srch); + ($currstate,$response,$forcenewuser) = + &build_search_response($srch,%srch_results); + } elsif ($srch->{'srchin'} eq 'crs') { + my $courseusers = &get_courseusers(); + foreach my $user (keys(%$courseusers)) { + my ($uname,$udom) = split(/:/,$user); + my %names = &Apache::loncommon::getnames($uname,$udom); + my %emails = &Apache::loncommon::getemails($uname,$udom); + 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'}, + lastname => $names{'lastname'}, + permanentemail => $emails{'permanentemail'}, + }; + } + } 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)) { + $srch_results{$user} = {firstname => $names{'firstname'}, + lastname => $names{'lastname'}, + permanentemail => $emails{'permanentemail'}, + + }; + } + } 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'}, + lastname => $names{'lastname'}, + permanentemail => $emails{'permanentemail'}, + }; + } + } + } + } + ($currstate,$response,$forcenewuser) = + &build_search_response($srch,%srch_results); + } elsif ($srch->{'srchin'} eq 'alc') { + $currstate = 'query'; + } elsif ($srch->{'srchin'} eq 'instd') { + ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch); + if ($dirsrchres eq 'ok') { + ($currstate,$response,$forcenewuser) = + &build_search_response($srch,%srch_results); + } else { + my $showdom = &display_domain_info($srch->{'srchdomain'}); $response = ''. + &mt('Institutional directory search is not available in domain: [_1]',$showdom). + '
'. + &mt('You may want to search in the LON-CAPA domain instead of the institutional directory.'). + '

'; + } + } + } + return ($currstate,$response,$forcenewuser,\%srch_results); +} + +sub directorysrch_check { + my ($srch) = @_; + my $can_search = 0; + my $response; + my %dom_inst_srch = &Apache::lonnet::get_dom('configuration', + ['directorysrch'],$srch->{'srchdomain'}); + my $showdom = &display_domain_info($srch->{'srchdomain'}); + if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') { + if (!$dom_inst_srch{'directorysrch'}{'available'}) { + return &mt('Institutional directory search is not available in domain: [_1]',$showdom); + } + if ($dom_inst_srch{'directorysrch'}{'localonly'}) { + if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) { + return &mt('Institutional directory search in domain: [_1] is only allowed for users with a current role in the domain.',$showdom); + } + my @usertypes = split(/:/,$env{'environment.inststatus'}); + if (!@usertypes) { + push(@usertypes,'default'); + } + if (ref($dom_inst_srch{'directorysrch'}{'cansearch'}) eq 'ARRAY') { + foreach my $type (@usertypes) { + if (grep(/^\Q$type\E$/,@{$dom_inst_srch{'directorysrch'}{'cansearch'}})) { + $can_search = 1; + last; + } + } + } + if (!$can_search) { + my ($insttypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($srch->{'srchdomain'}); + my @longtypes; + foreach my $item (@usertypes) { + push (@longtypes,$insttypes->{$item}); + } + my $insttype_str = join(', ',@longtypes); + return &mt('Institutional directory search in domain: [_1] is not available to your user type: ',$showdom).$insttype_str; + } + } else { + $can_search = 1; + } + } else { + return &mt('Institutional directory search has not been configured for domain: [_1]',$showdom); + } + my %longtext = &Apache::lonlocal::texthash ( + uname => 'username', + lastfirst => 'last name, first name', + lastname => 'last name', + contains => 'contains', + exact => 'as exact match to', + begins => 'begins with', + ); + if ($can_search) { + if (ref($dom_inst_srch{'directorysrch'}{'searchby'}) eq 'ARRAY') { + if (!grep(/^\Q$srch->{'srchby'}\E$/,@{$dom_inst_srch{'directorysrch'}{'searchby'}})) { + return &mt('Institutional directory search in domain: [_1] is not available for searching by "[_2]"',$showdom,$longtext{$srch->{'srchby'}}); + } + } else { + return &mt('Institutional directory search in domain: [_1] is not available.', $showdom); + } + } + if ($can_search) { + 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('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$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('Institutional directory search in domain [_1] is not available for the requested search type: "[_2]"',$showdom,$longtext{$srch->{'srchtype'}}); + } + } + } +} + + +sub get_courseusers { + my %advhash; + my $classlist = &Apache::loncoursedata::get_classlist(); + my %coursepersonnel=&Apache::lonnet::get_course_adv_roles(); + foreach my $role (sort(keys(%coursepersonnel))) { + foreach my $user (split(/\,/,$coursepersonnel{$role})) { + if (!exists($classlist->{$user})) { + $classlist->{$user} = []; + } + } + } + return $classlist; +} + +sub build_search_response { + my ($srch,%srch_results) = @_; + my ($currstate,$response,$forcenewuser); + my %names = ( + 'uname' => 'username', + 'lastname' => 'last name', + 'lastfirst' => 'last name, first name', + 'crs' => 'this course', + 'dom' => 'LON-CAPA domain: ', + 'instd' => 'the institutional directory for domain: ', + ); + + my %single = ( + begins => 'A match', + contains => 'A match', + exact => 'An exact match', + ); + my %nomatch = ( + begins => 'No match', + contains => 'No match', + exact => 'No exact match', + ); + if (keys(%srch_results) > 1) { + $currstate = 'select'; + } else { + if (keys(%srch_results) == 1) { + $currstate = 'modify'; + $response = &mt("$single{$srch->{'srchtype'}} was found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}.",$srch->{'srchterm'}); + if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') { + $response .= &display_domain_info($srch->{'srchdomain'}); + } + } else { + $response = ''.&mt("$nomatch{$srch->{'srchtype'}} found for the $names{$srch->{'srchby'}} ([_1]) in $names{$srch->{'srchin'}}",$srch->{'srchterm'}); + if ($srch->{'srchin'} eq 'dom' || $srch->{'srchin'} eq 'instd') { + $response .= &display_domain_info($srch->{'srchdomain'}); + } + $response .= ''; + if ($srch->{'srchin'} ne 'alc') { + $forcenewuser = 1; + my $cansrchinst = 0; + if ($srch->{'srchdomain'}) { + my %domconfig = &Apache::lonnet::get_dom('configuration',['directorysrch'],$srch->{'srchdomain'}); + if (ref($domconfig{'directorysrch'}) eq 'HASH') { + if ($domconfig{'directorysrch'}{'available'}) { + $cansrchinst = 1; + } + } + } + if ((($srch->{'srchby'} eq 'lastfirst') || + ($srch->{'srchby'} eq 'lastname')) && + ($srch->{'srchin'} eq 'dom')) { + if ($cansrchinst) { + $response .= '
'.&mt('You may want to broaden your search to a search of the institutional directory for the domain.'); + } + } + if ($srch->{'srchin'} eq 'crs') { + $response .= '
'.&mt('You may want to broaden your search to the selected LON-CAPA domain.'); + } + } + if (!($srch->{'srchby'} eq 'uname' && $srch->{'srchin'} eq 'dom' && $srch->{'srchtype'} eq 'exact' && $srch->{'srchdomain'} eq $env{'request.role.domain'})) { + my $showdom = &display_domain_info($env{'request.role.domain'}); + $response .= '

'.&mt("To add a new user (you can only create new users in your current role's domain - [_1]):",$env{'request.role.domain'}).'
  • '.&mt("Set 'Domain/institution to search' to: [_1]",$showdom).'
  • '.&mt("Set 'Search criteria' to: 'username is ...... in selected LON-CAPA domain'").'
  • '.&mt('Provide the proposed username').'
  • '.&mt('Search').'

'; + } + } + } + return ($currstate,$response,$forcenewuser); +} + +sub display_domain_info { + my ($dom) = @_; + my $output = $dom; + if ($dom ne '') { + my $domdesc = &Apache::lonnet::domain($dom,'description'); + if ($domdesc ne '') { + $output .= ' ('.$domdesc.')'; + } + } + return $output; +} + +sub crumb_utilities { + my %elements = ( + crtuser => { + srchterm => 'text', + srchin => 'selectbox', + srchby => 'selectbox', + srchtype => 'selectbox', + srchdomain => 'selectbox', + }, + docustom => { + 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.currstate.value = prevstate; + formname.submit(); +} +|; + return ($jsback,\%elements); +} + sub course_level_table { my (%inccourses) = @_; my $table = ''; @@ -1958,10 +2693,10 @@ sub course_level_table { $table .= ' '; } $table .= < + $lt{'ssd'} - + $lt{'sed'} ENDTIMEENTRY @@ -1994,10 +2729,10 @@ ENDTIMEENTRY 'name="sec_'.$customrole.'" />'; } $table .= < + $lt{'ssd'} - + $lt{'sed'} ENDENTRY @@ -2089,10 +2824,10 @@ sub course_level_dc { ''. ''; $otheritems .= < + $lt{'ssd'} - + $lt{'sed'} ENDTIMEENTRY