[LON-CAPA-cvs] cvs: loncom /interface loncommon.pm loncreateuser.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Sat, 28 Jul 2007 21:38:29 -0000


This is a MIME encoded message

--raeburn1185658709
Content-Type: text/plain

raeburn		Sat Jul 28 17:38:29 2007 EDT

  Modified files:              
    /loncom/interface	loncommon.pm loncreateuser.pm 
  Log:
  loncommon.pm
  - added user_picker() routine to generate user interface for adding/mdofiying users.
    - search by username; last name; last name, first name
    - search in course, domain or institutional directory
    - search for 'exact match' or 'contains search string'
  
  loncreateuser.pm
  - use new user search interface
  - search in course searches all course users (not just students)
  - if multiple matches found, can pick the single user to modify (table can be sorted by username, last name etc.) 
  - if no matches found, can indicate new user should be created
  - if institutional data are available, these will appear in new user data input form
  - added breadcrumbs
  - permanent email can be set/modified from CUSR
   
  Future work: 
  - enroll single student (londropadd) should employ loncommon::user_picker() interface.
  - allow search of multiple domains at one time using search catalog-type frames-based interface. (FIXME included).
  
  
--raeburn1185658709
Content-Type: text/plain
Content-Disposition: attachment; filename="raeburn-20070728173829.txt"

Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.554 loncom/interface/loncommon.pm:1.555
--- loncom/interface/loncommon.pm:1.554	Wed Jul 25 19:20:38 2007
+++ loncom/interface/loncommon.pm	Sat Jul 28 17:38:29 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.554 2007/07/25 23:20:38 albertel Exp $
+# $Id: loncommon.pm,v 1.555 2007/07/28 21:38:29 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -5532,6 +5532,120 @@
     return (\@sections,\@groups,$allroles,$rolehash,$accesshash);
 }
 
+sub user_picker {
+    my ($dom,$srch,$forcenewuser) = @_;
+    my $currdom = $dom;
+    my %curr_selected = (
+                        srchin => 'dom',
+                        srchby => 'uname',
+                      );
+    my $srchterm;
+    if (ref($srch) eq 'HASH') {
+        if ($srch->{'srchby'} ne '') {
+            $curr_selected{'srchby'} = $srch->{'srchby'};
+        }
+        if ($srch->{'srchin'} ne '') {
+            $curr_selected{'srchin'} = $srch->{'srchin'};
+        }
+        if ($srch->{'srchtype'} ne '') {
+            $curr_selected{'srchtype'} = $srch->{'srchtype'};
+        }
+        if ($srch->{'srchdomain'} ne '') {
+            $currdom = $srch->{'srchdomain'};
+        }
+        $srchterm = $srch->{'srchterm'};
+    }
+
+    my %lt=&Apache::lonlocal::texthash(
+                    'usr'  => 'Search for',
+                    'or'   => 'or',
+                    'in'   => 'in',
+                    'doma' => 'domain',
+                    'uname' => 'username',
+                    'lastname' => 'last name',
+                    'lastfirst' => 'last name, first name',
+                    'crs' => 'this course',
+                    'dom' => 'this domain', 
+                    'alc' => 'all LON-CAPA',
+                    'instd' => 'institutional directory',
+                    'exact' => 'as exact match to',
+                    'contains' => 'contained in',
+                                       );
+    my $domform = &select_dom_form($currdom,'srchdomain',1);
+
+    my $srchinsel = ' <select name="srchin">';
+
+    my @srchins = ('crs','dom','alc','instd');
+
+    foreach my $option (@srchins) {
+        # FIXME 'alc' option unavailable until 
+        #       loncreateuser::print_user_query_page()
+        #       has been completed.
+        next if ($option eq 'alc');
+        next if ($option eq 'crs' && !$env{'request.course.id'});
+        if ($curr_selected{'srchin'} eq $option) {
+            $srchinsel .= ' 
+   <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
+        } else {
+            $srchinsel .= '
+   <option value="'.$option.'">'.$lt{$option}.'</option>';
+        }
+    }
+    $srchinsel .= "\n  </select>\n";
+
+    my $srchbysel =  ' <select name="srchby">';
+    foreach my $option ('uname','lastname','lastfirst') {
+        if ($curr_selected{'srchby'} eq $option) {
+            $srchbysel .= '
+   <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
+        } else {
+            $srchbysel .= '
+   <option value="'.$option.'">'.$lt{$option}.'</option>';
+         }
+    }
+    $srchbysel .= "\n  </select>\n";
+
+    my $srchtypesel = ' <select name="srchtype">';
+    foreach my $option ('exact','contains') {
+        if ($curr_selected{'srchtype'} eq $option) {
+            $srchtypesel .= '
+   <option value="'.$option.'" selected="selected">'.$lt{$option}.'</option>';
+        } else {
+            $srchtypesel .= '
+   <option value="'.$option.'">'.$lt{$option}.'</option>';
+        }
+    }
+    $srchtypesel .= "\n  </select>\n";
+
+    my $output = <<"END_BLOCK";
+<table>
+<tr>
+ <td align="right">$lt{'usr'}:</td><td><input type="text" size="15" name="srchterm" value="$srchterm" /></td>
+ <td>
+$srchtypesel
+ </td>
+ <td>
+$srchbysel
+ </td>
+</tr>
+<tr>
+ <td align="right">$lt{'in'}:</td>
+ <td colspan="2">
+$srchinsel
+ </td>
+</tr>
+<tr>
+ <td align="right">$lt{'doma'}:</td><td colspan="2">$domform</td>
+</tr>
+</table>
+<br />
+END_BLOCK
+    if ($forcenewuser) {
+        $output .= '<span class="LC_nobreak">'.&mt('Make new user if no match found: ').'<input type="radio" name="forcenew" value="1" onclick="javascript:setSearch()" />'.&mt('Yes').'&nbsp;&nbsp;<input type="radio" name="forcenew" value="0" checked="checked" />'.&mt('No').'</span><br />';
+    }
+    return $output;
+}
+
 =pod
 
 =back
Index: loncom/interface/loncreateuser.pm
diff -u loncom/interface/loncreateuser.pm:1.159 loncom/interface/loncreateuser.pm:1.160
--- loncom/interface/loncreateuser.pm:1.159	Fri Jul 20 19:57:06 2007
+++ loncom/interface/loncreateuser.pm	Sat Jul 28 17:38:29 2007
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.159 2007/07/20 23:57:06 albertel Exp $
+# $Id: loncreateuser.pm,v 1.160 2007/07/28 21:38:29 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -193,46 +193,160 @@
 # =================================================================== 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 ($newuserscript,$newuservalidate);
+
+    if ($forcenewuser) {
+        $newuserscript = <<"ENDSCRIPT";
+
+function setSearch() {
+    var createnew = 0;
+    for (var i=0; i<document.crtuser.forcenew.length; i++) {
+        if (document.crtuser.forcenew[i].checked) {
+            if (document.crtuser.forcenew[i].value == 1) {
+                createnew = 1;
+            }
+        }
+    } 
+    if (createnew == 1) {
+        for (var i=0; i<document.crtuser.srchby.length; i++) {
+            if (document.crtuser.srchby.options[i].value == 'uname') {
+                document.crtuser.srchby.selectedIndex = i;
+            }
+        }
+        for (var i=0; i<document.crtuser.srchin.length; i++) {
+            if (document.crtuser.srchin.options[i].value == 'dom') {
+                document.crtuser.srchin.selectedIndex = i;
+            }
+        }
+        for (var i=0; i<document.crtuser.srchtype.length; i++) {
+            if (document.crtuser.srchtype.options[i].value == 'exact') {
+                document.crtuser.srchtype.selectedIndex = i;
+            }
+        }
+        for (var i=0; i<document.crtuser.srchdomain.length; i++) {
+            if (document.crtuser.srchdomain.options[i].value == '$env{'request.role.domain'}') {
+                document.crtuser.srchdomain.selectedIndex = i;
+            }
+        }
+    }
+}
+ENDSCRIPT
+        $newuservalidate = <<"ENDBLOCK";
+    setSearch();
+ENDBLOCK
+    }
+    my $validatescript = <<"ENDSCRIPT";
+
+function validateEntry() {
+    var checkok = 1;
+    var srchin = document.crtuser.srchin.options[document.crtuser.srchin.selectedIndex].value;
+    var srchtype = document.crtuser.srchtype.options[document.crtuser.srchtype.selectedIndex].value;
+    var srchby = document.crtuser.srchby.options[document.crtuser.srchby.selectedIndex].value;
+    var srchdomain = document.crtuser.srchdomain.options[document.crtuser.srchdomain.selectedIndex].value;
+    var srchterm =  document.crtuser.srchterm.value;
+    var msg = "";
+
+    if (srchterm == "") {
+        checkok = 0;
+        msg += "You must include some text to search for.\\n";
+    }
+
+    $newuservalidate
+
+    if (srchtype== 'contains') {
+        if (srchterm.length < 3) {
+            checkok = 0;
+            msg += "The text you are searching for must contain at least three characters when using a 'contained in' type search.\\n";   
+        }
+    }
+    if (srchin == 'instd') {
+        if (srchdomain == '') {
+            checkok = 0;
+            msg += "You must choose a domain when using an institutional directory search.\\n"; 
+        }
+    }
+    if (srchin == 'dom') {
+        if (srchdomain == '') {
+            checkok = 0;
+            msg += "You must choose a domain when using a domain search.\\n";
+        }
+    }
+    if (srchby == 'lastfirst') {
+        if (srchterm.indexOf(",") == -1) {
+            checkok = 0;
+            msg += "When using searching by last,first you must include a comma as separator between last name and first name.\\n";
+        }
+        if (srchterm.indexOf(",") == srchterm.length -1) { 
+            checkok = 0;
+            msg += "When searching by last,first you must include at least one character in the first name.\\n";
+        }
+    }
+    if (checkok == 0) {
+        alert("The following need to be corrected before the search can be run:\\n"+msg);
+        return;
+    }
+    if (checkok == 1) {
+        document.crtuser.submit();
+    }
+}
+
+ENDSCRIPT
+
+    my ($jsback,$elements) = &crumb_utilities();
+
+    my $jscript = &Apache::loncommon::studentbrowser_javascript()."\n".
+        '<script type"text/javascript">'."\n".
+        &Apache::lonhtmlcommon::set_form_elements($elements->{$formtoset}).
+        $newuserscript.$validatescript.'</script>'."\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/custom role search",
+       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",
 				       );
     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
-<form action="/adm/createuser" method="post" name="crtuser">
-<input type="hidden" name="phase" value="get_user_info" />
+$crumbs
 <h2>$lt{siur}$helpsiur</h2>
-<table>
-<tr><td>$lt{usr}:</td><td><input type="text" size="15" name="ccuname" />
-</td><td rowspan="2">$sellink</td></tr><tr><td>
-$lt{'dom'}:</td><td>$domform</td></tr>
-</table>
-<input name="userrole" type="submit" value="$lt{usrr}" />
-</form>
-ENDDOCUMENT
-   if (&Apache::lonnet::allowed('mcr','/')) {
-       $r->print(<<ENDCUSTOM);
+<h3>$lt{'srch'} $sellink</h3>
+$response");
+    $r->print(&entry_form($defdom,$srch,$forcenewuser));
+    if (&Apache::lonnet::allowed('mcr','/')) {
+        $r->print(<<ENDCUSTOM);
 <form action="/adm/createuser" method="post" name="docustom">
 <input type="hidden" name="phase" value="selected_custom_edit" />
 <h2>$lt{'ecrp'}$helpecpr</h2>
@@ -244,6 +358,20 @@
     $r->print(&Apache::loncommon::end_page());
 }
 
+sub entry_form {
+    my ($dom,$srch,$forcenewuser) = @_;
+    my $userpicker = 
+       &Apache::loncommon::user_picker($dom,$srch,$forcenewuser);
+    my $srchbutton = &mt('Search');
+    my $output = <<"ENDDOCUMENT";
+<form action="/adm/createuser" method="post" name="crtuser">
+<input type="hidden" name="phase" value="get_user_info" />
+$userpicker
+<input name="userrole" type="button" value="$srchbutton" onclick="javascript:validateEntry()" />
+</form>
+ENDDOCUMENT
+    return $output;
+}
 
 sub user_modification_js {
     my ($pjump_def,$dc_setcourse_code,$nondc_setsection_code,$groupslist)=@_;
@@ -273,16 +401,109 @@
 }
 
 # =================================================================== 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) = @_;
+    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 = (<<ENDSCRIPT);
+<script type="text/javascript">
+function pickuser(uname,udom) {
+    document.usersrchform.seluname.value=uname;
+    document.usersrchform.seludom.value=udom;
+    document.usersrchform.phase.value="userpicked";
+    document.usersrchform.submit();
+}
+
+$jsback
+</script>
+ENDSCRIPT
+
+    my %lt=&Apache::lonlocal::texthash(
+                                       'srch'           => "User Search",
+                                       '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/custom role search",
+          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("<b>$lt{'srch'}</b><br />");
+    $r->print(&entry_form($srch->{'srchdomain'},$srch));
+    $r->print('<h3>'.&mt('Select a user').'</h3>');
+    $r->print('<form name="usersrchform" method="post">'.
+              &Apache::loncommon::start_data_table()."\n".
+              &Apache::loncommon::start_data_table_header_row()."\n".
+              ' <th> </th>'."\n");
+    foreach my $field (@fields) {
+        $r->print(' <th><a href="javascript:document.usersrchform.sortby.value='.
+                  "'".$field."'".';document.usersrchform.submit();">'.
+                  $lt{$field}.'</a></th>'."\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})
+        } (keys(%$srch_results));
+
+    foreach my $user (@sorted_users) {
+        my ($uname,$udom) = split(/:/,$user);
+        $r->print(&Apache::loncommon::start_data_table_row().
+                  '<td><input type="button" name="seluser" value="'.&mt('Select').'" onclick="javascript:pickuser('."'".$uname."'".','."'".$udom."'".')" /></td>'.
+                  '<td><tt>'.$uname.'</tt></td>'.
+                  '<td><tt>'.$udom.'</tt></td>');
+        foreach my $field ('lastname','firstname','permanentemail') {
+            $r->print('<td>'.$srch_results->{$user}->{$field}.'</td>');
+        }
+        $r->print(&Apache::loncommon::end_data_table_row());
+    }
+    $r->print(&Apache::loncommon::end_data_table().'<br /><br />');
+    $r->print(&Apache::lonhtmlcommon::echo_form_input(['sortby','seluname','seludom','state','phase']));
+    $r->print(' <input type="hidden" name="sortby" value="'.$sortby.'" />'."\n".
+              ' <input type="hidden" name="seluname" value="" />'."\n".
+              ' <input type="hidden" name="seludom" value="" />'."\n".
+              ' <input type="hidden" name="state" value="select" />'."\n".
+              ' <input type="hidden" name="phase" value="get_user_info" />'."\n".
+              '</form>');
+    $r->print($response);
+    $r->print(&Apache::loncommon::end_page());
+}
+
+sub print_user_query_page {
+    my ($r) = @_;
+# 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 = '<br />'.$response
+    }
     my $defdom=$env{'request.role.domain'};
 
     my ($krbdef,$krbdefdom) =
@@ -291,7 +512,7 @@
     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);
 
@@ -498,9 +719,31 @@
     }
     my $js = &user_modification_js($pjump_def,$dc_setcourse_code,
                                    $nondc_setsection_code,$groupslist);
+
+    my ($jsback,$elements) = &crumb_utilities();
+
+    $js .= "\n".
+           '<script type="text/javascript">'."\n".$jsback."\n".'</script>';
+
     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/custom role search",
+       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";
 <form action="/adm/createuser" method="post" name="cu">
@@ -519,6 +762,17 @@
         }
     }
     if ($uhome eq 'no_host') {
+        my $instsrch = { 
+                         srchin => 'instd',
+                         srchterm => $env{'form.seluname'},
+                         srchdomain => $env{'form.seludom'},
+                         srchby => 'uname',
+                         srchtype => 'exact',
+                       };
+        my %inst_results;
+        if (directorysrch_check($instsrch) eq 'ok') {
+            %inst_results = &Apache::lonnet::inst_directory_query($instsrch);
+        }
         my $home_server_list=
             '<option value="default" selected>default</option>'."\n".
                 &Apache::loncommon::home_server_option_list($ccdomain);
@@ -532,6 +786,7 @@
                     'mn'   => "Middle Name",
                     'ln'   => "Last Name",
                     'gen'  => "Generation",
+                    'mail' => "Permanent e-mail address",
                     'idsn' => "ID/Student Number",
                     'hs'   => "Home Server",
                     'lg'   => "Login Data"
@@ -545,7 +800,9 @@
         &initialize_authen_forms();
 	$r->print(<<ENDNEWUSER);
 $start_page
+$crumbs
 <h1>$lt{'cnu'}</h1>
+$response
 $forminfo
 <h2>$lt{'nu'} "$ccuname" $lt{'id'} $ccdomain</h2>
 <script type="text/javascript" language="Javascript">
@@ -556,15 +813,17 @@
 <p>
 <table>
 <tr><td>$lt{'fn'}  </td>
-    <td><input type='text' name='cfirst'  size='15' /></td></tr>
+    <td><input type="text" name="cfirst" size="15" value="$inst_results{'firstname'}" /></td></tr>
 <tr><td>$lt{'mn'} </td> 
-    <td><input type='text' name='cmiddle' size='15' /></td></tr>
+    <td><input type="text" name="cmiddle" size="15" value="$inst_results{'middlename'}" /></td></tr>
 <tr><td>$lt{'ln'}   </td>
-    <td><input type='text' name='clast'   size='15' /></td></tr>
+    <td><input type="text" name="clast" size="15" value="$inst_results{'lastname'}" /></td></tr>
 <tr><td>$lt{'gen'}$genhelp</td>
-    <td><input type='text' name='cgen'    size='5'  /></td></tr>
+    <td><input type="text" name="cgen" size="5" value="$inst_results{'generation'}" /></td></tr>
+<tr><td>$lt{'mail'}</td>
+    <td><input type="text" name="cemail" size="20" value="$inst_results{'permanentemail'}" /></td></tr>
 </table>
-$lt{'idsn'} <input type='text' name='cstid'   size='15' /></p>
+$lt{'idsn'} <input type="text" name="cstid" size="15" value="$inst_results{'id'}" /></p>
 $lt{'hs'}: <select name="hserver" size="1"> $home_server_list </select>
 <hr />
 <h3>$lt{'lg'}</h3>
@@ -584,27 +843,30 @@
                     'fn'   => "first name",
                     'mn'   => "middle name",
                     'ln'   => "last name",
-                    'gen'  => "generation"
+                    'gen'  => "generation",
+                    'email' => "permanent e-mail",
 				       );
 	$r->print(<<ENDCHANGEUSER);
 $start_page
+$crumbs
 <h1>$lt{'cup'}</h1>
 $forminfo
 <h2>$lt{'usr'} "$ccuname" $lt{'id'} "$ccdomain"</h2>
 ENDCHANGEUSER
         # Get the users information
-        my %userenv = &Apache::lonnet::get('environment',
-                          ['firstname','middlename','lastname','generation',
-                           'portfolioquota'],$ccdomain,$ccuname);
+        my %userenv = 
+            &Apache::lonnet::get('environment',
+                ['firstname','middlename','lastname','generation',
+                 'permanentemail','portfolioquota'],$ccdomain,$ccuname);
         my %rolesdump=&Apache::lonnet::dump('roles',$ccdomain,$ccuname);
         $r->print('
 <hr />'.
                   &Apache::loncommon::start_data_table().
                   &Apache::loncommon::start_data_table_header_row().
-'<th>'.$lt{'fn'}.'</th><th>'.$lt{'mn'}.'</th><th>'.$lt{'ln'}.'</th><th>'.$lt{'gen'}.'</th>'.
+'<th>'.$lt{'fn'}.'</th><th>'.$lt{'mn'}.'</th><th>'.$lt{'ln'}.'</th><th>'.$lt{'gen'}.'</th><th>'.$lt{'email'}.'</th>'.
                   &Apache::loncommon::end_data_table_header_row().
                   &Apache::loncommon::start_data_table_row());
-        foreach my $item ('firstname','middlename','lastname','generation') {
+        foreach my $item ('firstname','middlename','lastname','generation','permenanentemail') {
            if (&Apache::lonnet::allowed('mau',$ccdomain)) {
               $r->print(<<"END");
 <td><input type="text" name="c$item" value="$userenv{$item}" size="15" /></td>
@@ -1025,12 +1287,15 @@
         $r->print(&course_level_table(%inccourses));
         $r->print('<hr /><input type="button" value="'.&mt('Modify User').'" onClick="setSections()" />'."\n");
     }
+    $r->print(&Apache::lonhtmlcommon::echo_form_input(['phase','userrole','ccdomain','prevphase','state']));
+    $r->print('<input type="hidden" name="state" value="" />');
+    $r->print('<input type="hidden" name="prevphase" value="'.$env{'form.phase'}.'" />');
     $r->print("</form>".&Apache::loncommon::end_page());
 }
 
 # ================================================================= Phase Three
 sub update_user_data {
-    my $r=shift;
+    my ($r) = @_; 
     my $uhome=&Apache::lonnet::homeserver($env{'form.ccuname'},
                                           $env{'form.ccdomain'});
     # Error messages
@@ -1043,7 +1308,31 @@
     } else {
         $title='Modify User Privileges';
     }
-    $r->print(&Apache::loncommon::start_page($title));
+
+    my ($jsback,$elements) = &crumb_utilities();
+    my $jscript = '<script type="text/javascript">'."\n".
+                  $jsback."\n".'</script>'."\n";
+
+    $r->print(&Apache::loncommon::start_page($title,$jscript));
+    &Apache::lonhtmlcommon::add_breadcrumb
+       ({href=>"javascript:backPage(document.userupdate)",
+         text=>"User/custom role search",
+         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'} ) {
@@ -1053,7 +1342,7 @@
     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;
     }
@@ -1174,14 +1463,14 @@
         # 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; 
         }
@@ -1239,7 +1528,8 @@
             ($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) {
@@ -1248,6 +1538,7 @@
             $changeHash{'middlename'} = $env{'form.cmiddlename'};
             $changeHash{'lastname'}   = $env{'form.clastname'};
             $changeHash{'generation'} = $env{'form.cgeneration'};
+            $changeHash{'permanentemail'} = $env{'form.permanentemail'};
             my $putresult = &Apache::lonnet::put
                 ('environment',\%changeHash,
                  $env{'form.ccdomain'},$env{'form.ccuname'});
@@ -1259,6 +1550,7 @@
                              'mddl' => "middle",
                              'lst'  => "last",
 			     'gen'  => "generation",
+                             'mail' => "permanent e-mail",
                              'disk' => "disk space allocated to portfolio files",
                              'prvs' => "Previous",
                              'chto' => "Changed To"
@@ -1277,6 +1569,7 @@
     <td>$userenv{'middlename'} </td>
     <td>$userenv{'lastname'}   </td>
     <td>$userenv{'generation'} </td>
+    <td>$userenv{'permanentemail'} </td>
     <td>$oldportfolioquota Mb</td>
 </tr>
 <tr><td>$lt{'chto'}</td>
@@ -1284,6 +1577,7 @@
     <td>$env{'form.cmiddlename'} </td>
     <td>$env{'form.clastname'}   </td>
     <td>$env{'form.cgeneration'} </td>
+    <td>$env{'form.cpermanentemail'} </td>
     <td>$newportfolioquota Mb $defquotatext </td></tr>
 </table>
 END
@@ -1313,12 +1607,14 @@
                            'usr'  => "User",                    
                            'id'   => "in domain",
                            'gen'  => "Generation",
+                           'mail' => "Permanent e-mail",
                            'disk' => "Disk space allocated to user's portfolio files",
 					       );
             $r->print(<<"END");
 <h2>$lt{'usr'} "$env{'form.ccuname'}" $lt{'id'} "$env{'form.ccdomain'}"</h2>
 <h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} </h4>
 <h4>$lt{'gen'}: $userenv{'generation'}</h4>
+<h4>$lt{'mail'}: $userenv{'permanentemail'}</h4>
 END
             if ($putresult eq 'ok') {
                 if ($oldportfolioquota != $newportfolioquota) {
@@ -1546,6 +1842,18 @@
 # Flush the course logs so reverse user roles immediately updated
     &Apache::lonnet::flushcourselogs();
     $r->print('<p><a href="/adm/createuser">Create/Modify Another User</a></p>');
+    $r->print('<form name="userupdate" method="post" />'."\n");
+    foreach my $item ('srchby','srchin','srchtype','srchterm','srchdomain','ccuname','ccdomain') {
+        $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
+    }
+    foreach my $item ('sortby','seluname','seludom') {
+        if (exists($env{'form.'.$item})) {
+            $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.'" />'."\n");
+        }
+    }
+    $r->print('<input type="hidden" name="phase" value="" />'."\n".
+              '<input type ="hidden" name="state" value="" />'."\n".
+              '</form>');
     $r->print(&Apache::loncommon::end_page());
 }
 
@@ -1596,7 +1904,7 @@
 # ========================================================== Custom Role Editor
 
 sub custom_role_editor {
-    my $r=shift;
+    my ($r) = @_;
     my $rolename=$env{'form.rolename'};
 
     if ($rolename eq 'make new role') {
@@ -1662,6 +1970,7 @@
 	}
 	$full{$priv}=1;
     }
+    my ($jsback,$elements) = &crumb_utilities();
     my $button_code = "\n";
     my $head_script = "\n";
     $head_script .= '<script type="text/javascript">'."\n";
@@ -1670,8 +1979,17 @@
         $head_script .= &make_script_template($role);
         $button_code .= &make_button_code($role);
     }
-    $head_script .= '</script>'."\n";
+    $head_script .= "\n".$jsback."\n".'</script>'."\n";
     $r->print(&Apache::loncommon::start_page('Custom Role Editor',$head_script));
+   &Apache::lonhtmlcommon::add_breadcrumb
+     ({href=>"javascript:backPage(document.form1,'','')",
+       text=>"User/custom role search",
+       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",
@@ -1683,7 +2001,7 @@
     $r->print($button_code);
     $r->print('</form>');
     $r->print(<<ENDCCF);
-<form name=form1 method="post">
+<form name="form1" method="post">
 <input type="hidden" name="phase" value="set_custom_roles" />
 <input type="hidden" name="rolename" value="$rolename" />
 ENDCCF
@@ -1708,7 +2026,10 @@
              &Apache::loncommon::end_data_table_row());
     }
     $r->print(&Apache::loncommon::end_data_table().
-   '<input type="reset" value="'.&mt("Reset").'" /><input type="submit" value="'.&mt('Define Role').'" /></form>'.
+   '<input type="hidden" name="startrolename" value="'.$env{'form.rolename'}.
+   '" />'."\n".'<input type="hidden" name="state" value="" />'."\n".   
+   '<input type="reset" value="'.&mt("Reset").'" />'."\n".
+   '<input type="submit" value="'.&mt('Define Role').'" /></form>'.
 	      &Apache::loncommon::end_page());
 }
 # --------------------------------------------------------
@@ -1796,11 +2117,27 @@
         return;
     }
 
-    $r->print(&Apache::loncommon::start_page('Save Custom Role').'<h2>');
+    my ($jsback,$elements) = &crumb_utilities();
+    my $jscript = '<script type="text/javascript">'.$jsback."\n".'</script>';
+
+    $r->print(&Apache::loncommon::start_page('Save Custom Role'),$jscript);
+    &Apache::lonhtmlcommon::add_breadcrumb
+        ({href=>"javascript:backPage(document.customresult,'','')",
+          text=>"User/custom role search",
+          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('<h2>');
     if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 	$r->print(&mt('Existing Role').' "');
     } else {
@@ -1849,7 +2186,8 @@
 						$env{'user.name'},
 						$rolename));
     }
-    $r->print('<p><a href="/adm/createuser">Create another role, or Create/Modify a user.</a></p>');
+    $r->print('<p><a href="/adm/createuser">Create another role, or Create/Modify a user.</a></p><form name="customresult" method="post">');
+    $r->print(&Apache::lonhtmlcommon::echo_form_input([]).'</form>');
     $r->print(&Apache::loncommon::end_page());
 }
 
@@ -1871,17 +2209,52 @@
         (&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 ($state,$response,$forcenewuser,$results) = 
+                   &user_search_result($srch);
+               if ($state eq 'select') {
+                   &print_user_selection_page($r,$response,$srch,$results);
+               } elsif ($state eq 'modify') {
+                   my ($ccuname,$ccdomain);
+                   if ($srch->{'srchby'} eq 'uname') {
+                       $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 ($state eq 'query') {
+                   &print_user_query_page($r);
+               } 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'}=
@@ -1889,9 +2262,308 @@
       return HTTP_NOT_ACCEPTABLE; 
    }
    return OK;
-} 
+}
 
 #-------------------------------------------------- functions for &phase_two
+sub user_search_result {
+    my ($srch) = @_;
+    my @alldomains = sort(&Apache::lonnet::all_domains());
+    my %allhomes;
+    my %inst_matches;
+    my %srch_results;
+    my ($response,$state,$forcenewuser);
+
+    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)$/) {
+        $response = &mt('Invalid search.');
+    }
+    if ($srch->{'srchterm'} eq '') {
+        $response = &mt('You must enter a search term.');
+    }
+    if (($srch->{'srchin'} eq 'dom') || ($srch->{'srchin'} eq 'instd')) {
+        if (($srch->{'srchdomain'} eq '') || 
+            (!(grep/^\Q$srch->{'srchdomain'}\E$/,@alldomains))) {
+            $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->{'srchterm'} !~ /^$match_username$/) {
+            $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
+        }
+    }
+    if ($srch->{'srchin'} eq 'instd') {
+        my $instd_chk = &directorysrch_check($srch);
+        if ($instd_chk ne 'ok') {
+            $response = $instd_chk;
+        }
+    }
+    if ($response ne '') {
+        return ($state,$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');
+                        $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';
+                    }
+                } else {
+                    $state = 'modify';
+                }
+            } else {
+                if ($srch->{'srchin'} eq 'dom') {
+                    my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
+                    if ($uhome eq 'no_host') {
+                        ($state,$response,$forcenewuser) =
+                            &build_search_response($srch,%srch_results);
+                    } else {
+                        $state = 'modify';
+                    }
+                } else {
+                    my %courseusers = &get_courseusers();
+                    if (exists($courseusers{$srch->{'srchterm'}.':'.$srch->{'srchdomain'}})) {
+                        $state = 'modify';
+                    } else {
+                        ($state,$response,$forcenewuser) =
+                            &build_search_response($srch,%srch_results);
+                    }
+                }
+            }
+        } elsif ($srch->{'srchin'} eq 'alc') {
+            $state = 'query';
+        } elsif ($srch->{'srchin'} eq 'instd') {
+            %srch_results = &Apache::lonnet::inst_directory_query($srch);
+            ($state,$response,$forcenewuser) = 
+                &build_search_response($srch,%srch_results); 
+        }
+    } else {
+        if ($srch->{'srchin'} eq 'dom') {
+            %srch_results = &Apache::lonnet::usersearch($srch);
+            ($state,$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 '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'});
+                    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 'contains') {
+                        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'},
+                                               };
+                        }
+                    }
+                }
+            }
+            ($state,$response,$forcenewuser) = 
+                &build_search_response($srch,%srch_results); 
+        } elsif ($srch->{'srchin'} eq 'alc') {
+            $state = 'query';
+        } elsif ($srch->{'srchin'} eq 'instd') {
+            %srch_results = &Apache::lonnet::inst_directory_query($srch); 
+            ($state,$response,$forcenewuser) = 
+                &build_search_response($srch,%srch_results);
+        }
+    }
+    return ($state,$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'});
+    if (ref($dom_inst_srch{'directorysrch'}) eq 'HASH') {
+        if (!$dom_inst_srch{'directorysrch'}{'available'}) {
+            return &mt('Directory search unavailable in domain: [_1]',$srch->{'srchdomain'}); 
+        }
+        if ($dom_inst_srch{'directorysrch'}{'localonly'}) {
+            if ($env{'request.role.domain'} ne $srch->{'srchdomain'}) {
+                return &mt('Directory search in domain: [_1] is only allowed for users with a current role in the domain.',$srch->{'srchdomain'}); 
+            }
+            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('Directory search in domain: [_1] is unavailable to your user type: ',$srch->{'srchdomain'}).$insttype_str;
+            } 
+        } else {
+            $can_search = 1;
+        }
+    } else {
+        return &mt('Directory search has not been configured for domain: [_1]',$srch->{'srchdomain'});
+    }
+    my %longtext = &Apache::lonlocal::texthash (
+                       uname => 'username',
+                       lastfirst => 'last name, first name',
+                       lastname => 'last name',
+                       contains => 'is contained in',
+                       exact => 'as exact match to'
+                   );
+    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('Directory search in domain: [_1] is not available for searching by [_2]',$srch->{'srchdomain'},$longtext{$srch->{'srchby'}});
+            }
+        } else {
+            return &mt('Directory search in domain: [_1] is not available.', $srch->{'srchdomain'});
+        }
+    }
+    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'}});
+        }
+    }
+}
+
+
+sub get_courseusers {
+    my %advhash;
+    my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
+    foreach my $role (sort(keys(%coursepersonnel))) {
+        foreach my $user (split(/\,/,$coursepersonnel{$role})) {
+            $advhash{$user} = '';
+        }
+    }
+    my $classlist = &Apache::loncoursedata::get_classlist();
+    my %combined = (%advhash, %{$classlist});
+    return %combined;
+}
+
+sub build_search_response {
+    my ($srch,%srch_results) = @_;
+    my ($state,$response,$forcenewuser);
+    my %names = (
+          'uname' => 'username',
+          'lastname' => 'last name',
+          'lastfirst' => 'last name, first name',
+          'crs' => 'this course',
+          'dom' => 'this domain',
+          'instd' => "your institution's directory",
+    );
+
+    my %single = (
+                   contains => 'A match',
+                   exact => 'An exact match',
+                 );
+    my %nomatch = (
+                   contains => 'No match',
+                   exact => 'No exact match',
+                  );
+    if (keys(%srch_results) > 1) {
+        $state = 'select';
+    } else {
+        if (keys(%srch_results) == 1) {
+            $state = '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'});
+            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')) {
+                    if ($srch->{'srchin'} eq 'crs') {
+                        $response .= '<br />'.&mt('You may want to broaden your search to the whole domain.'); 
+                    } elsif ($srch->{'srchin'} eq 'dom') {
+                        if ($cansrchinst) {
+                            $response .= '<br />'.&mt('You may want to broaden your search to a search of the institutional directory for this domain.');
+                        }
+                    }
+                }
+                $response .= '<br />'.&mt("To add as a new user:").'<ul><li>'.&mt("Enter the proposed username in the <i>'Search for'</i> box").'</li><li>'.&mt("Set <i>'Make new user if no match found</i>' to <b>Yes</b>").'</li><li>'.&mt("Click <i>'Search'</i>").'</li></ul>'.&mt("Note: you can only create new users in the domain of your current role -".$env{'request.role.domain'}).'<br /><br />';
+            }
+        }
+    }
+    return ($state,$response,$forcenewuser);
+}
+
+sub crumb_utilities {
+    my %elements = (
+       crtuser => {
+           srchterm => 'text',
+           srchin => 'selectbox',
+           srchby => 'selectbox',
+           srchtype => 'selectbox',
+           srchdomain => 'selectbox',
+       },
+       docustom => {
+           rolename => 'selectbox',
+           newrolename => 'textbox',
+       },
+    );
+
+    my $jsback .= qq|
+function backPage(formname,prevphase,prevstate) {
+    formname.phase.value = prevphase;
+    formname.state.value = prevstate;
+    formname.submit();
+}
+|;
+    return ($jsback,\%elements);
+}
+
 sub course_level_table {
     my (%inccourses) = @_;
     my $table = '';

--raeburn1185658709--