--- loncom/interface/loncreateuser.pm	2007/11/10 03:51:46	1.193
+++ loncom/interface/loncreateuser.pm	2007/12/01 03:49:08	1.197
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.193 2007/11/10 03:51:46 raeburn Exp $
+# $Id: loncreateuser.pm,v 1.197 2007/12/01 03:49:08 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -440,7 +440,7 @@ sub print_user_query_page {
 }
 
 sub print_user_modification_page {
-    my ($r,$ccuname,$ccdomain,$srch,$response) = @_;
+    my ($r,$ccuname,$ccdomain,$srch,$response,$context) = @_;
     if (($ccuname eq '') || ($ccdomain eq '')) {
         my $usermsg = &mt('No username and/or domain provided.'); 
 	&print_username_entry_form($r,$usermsg);
@@ -448,30 +448,32 @@ sub print_user_modification_page {
     }
     my %abv_auth = &auth_abbrev();
     my ($curr_authtype,%rulematch,%inst_results,$curr_kerb_ver,$newuser,
-        %alerts,%curr_rules);
+        %alerts,%curr_rules,%got_rules);
     my $uhome=&Apache::lonnet::homeserver($ccuname,$ccdomain);
     if ($uhome eq 'no_host') {
         $newuser = 1;
         my $checkhash;
         my $checks = { 'username' => 1 };
-        $checkhash->{$ccuname.':'.$ccdomain} = { 'status' => 'new' };
+        $checkhash->{$ccuname.':'.$ccdomain} = { 'newuser' => $newuser };
         &Apache::loncommon::user_rule_check($checkhash,$checks,
-                   \%alerts,\%rulematch,\%inst_results,\%curr_rules);
-        if (ref($alerts{$ccuname.':'.$ccdomain}) eq 'HASH') {
-            if ($alerts{$ccuname.':'.$ccdomain}{'username'}) {
-                my $domdesc = 
+            \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
+        if (ref($alerts{'username'}) eq 'HASH') {
+            if (ref($alerts{'username'}{$ccdomain}) eq 'HASH') {
+                my $domdesc =
                     &Apache::lonnet::domain($ccdomain,'description');
-                my $userchkmsg;
-                if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
-                    $userchkmsg = 
-                        &Apache::loncommon::instrule_disallow_msg('username',
+                if ($alerts{'username'}{$ccdomain}{$ccuname}) {
+                    my $userchkmsg;
+                    if (ref($curr_rules{$ccdomain}) eq 'HASH') {  
+                        $userchkmsg = 
+                            &Apache::loncommon::instrule_disallow_msg('username',
                                                                  $domdesc,1).
                         &Apache::loncommon::user_rule_formats($ccdomain,
                             $domdesc,$curr_rules{$ccdomain}{'username'},
                             'username');
-                }
-                &print_username_entry_form($r,$userchkmsg);
-                return;
+                    }
+                    &print_username_entry_form($r,$userchkmsg);
+                    return;
+                } 
             }
         }
     } else {
@@ -790,6 +792,9 @@ $lt{'hs'}: $home_server_pick
         } else {
             $r->print($home_server_pick);
         }
+        if ($context eq 'domain') {
+            $r->print(&Apache::lonuserutils::forceid_change());
+        }
         $r->print('</div>'."\n".'<div class="LC_left_float"><h3>'.
                   $lt{'lg'}.'</h3>');
         my ($fixedauth,$varauth,$authmsg); 
@@ -1266,8 +1271,6 @@ $lt{'uuas'} ($currentauth). $lt{'adcs'}.
 ENDBADAUTH
         }
     } else { # Authentication type is valid
-        my $authformcurrent='';
-        my $authform_other='';
         &initialize_authen_forms($ccdomain,$currentauth);
         my ($authformcurrent,$authform_other,$can_modify) =
             &modify_login_block($ccdomain,$currentauth);
@@ -1585,26 +1588,30 @@ sub update_user_data {
         my %checkhash;
         my %checks = ('id' => 1);
         %{$checkhash{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}} = (
-            'status' => 'new', 
-            'id' => $env{'form.cid'}
+            'newuser' => 1, 
+            'id' => $env{'form.cid'},
         );
-        &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
-                                      \%rulematch,\%inst_results,\%curr_rules);
-        if (ref($alerts{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}) eq 'HASH') {
-            if ($alerts{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}}{'id'}) {
-                my $domdesc =
-                    &Apache::lonnet::domain($env{'form.ccdomain'},'description');
-                my $userchkmsg;
-                if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
-                    $userchkmsg  = 
-                        &Apache::loncommon::instrule_disallow_msg('id',
-                                                                  $domdesc,1).
-                        &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
-                            $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
-                } 
-                $r->print($error.&mt('Invalid ID format').$end.
-                          $userchkmsg.$rtnlink);
-                return;
+        if ($env{'form.cid'} ne '') {
+            &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
+                                          \%rulematch,\%inst_results,\%curr_rules);
+            if (ref($alerts{'id'}) eq 'HASH') {
+                if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
+                    my $domdesc =
+                        &Apache::lonnet::domain($env{'form.ccdomain'},'description');
+                    if ($alerts{'id'}{$env{'form.ccdomain'}}{$env{'form.cid'}}) {
+                        my $userchkmsg;
+                        if (ref($curr_rules{$env{'form.ccdomain'}}) eq 'HASH') {
+                            $userchkmsg  = 
+                                &Apache::loncommon::instrule_disallow_msg('id',
+                                                                    $domdesc,1).
+                                &Apache::loncommon::user_rule_formats($env{'form.ccdomain'},
+                                    $domdesc,$curr_rules{$env{'form.ccdomain'}}{'id'},'id');
+                        }
+                        $r->print($error.&mt('Invalid ID format').$end.
+                                  $userchkmsg.$rtnlink);
+                        return;
+                    }
+                }
             }
         }
 	# Call modifyuser
@@ -1644,17 +1651,40 @@ sub update_user_data {
         # Check for need to change
         my %userenv = &Apache::lonnet::get
             ('environment',['firstname','middlename','lastname','generation',
-             'permanentemail','portfolioquota','inststatus'],
+             'id','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','permanentemail') {
+        foreach my $item ('firstname','middlename','lastname','generation','permanentemail','id') {
             # Strip leading and trailing whitespace
             $env{'form.c'.$item} =~ s/(\s+$|^\s+)//g; 
         }
+        # Check to see if we can change the ID/student number
+        my $forceid = $env{'form.forceid'};
+        my $recurseid = $env{'form.recurseid'};
+        my $newuser = 0;
+        my $disallowed_id = 0;
+        my (%alerts,%rulematch,%idinst_results,%curr_rules,%got_rules);
+        if (!$forceid) {
+            $env{'form.cid'} = $userenv{'id'};
+        } elsif ($env{'form.cid'} ne $userenv{'id'}) {
+            my $checkhash;
+            my $checks = { 'id' => 1 };
+            $checkhash->{$env{'form.ccuname'}.':'.$env{'form.ccdomain'}} = 
+                   { 'newuser' => $newuser,
+                     'id'  => $env{'form.cid'}, 
+                   };
+            &Apache::loncommon::user_rule_check($checkhash,$checks,
+                \%alerts,\%rulematch,\%idinst_results,\%curr_rules,\%got_rules);
+            if (ref($alerts{'id'}) eq 'HASH') {
+                if (ref($alerts{'id'}{$env{'form.ccdomain'}}) eq 'HASH') {
+                   $disallowed_id = 1;
+                }
+            }
+        }
         my ($quotachanged,$namechanged,$oldportfolioquota,$newportfolioquota,
             $inststatus,$isdefault,$defquotatext);
         my ($defquota,$settingstatus) = 
@@ -1710,6 +1740,7 @@ sub update_user_data {
              $env{'form.cmiddlename'} ne $userenv{'middlename'} ||
              $env{'form.clastname'}   ne $userenv{'lastname'}   ||
              $env{'form.cgeneration'} ne $userenv{'generation'} ||
+             $env{'form.cid'} ne $userenv{'id'}                 ||
              $env{'form.cpermanentemail'} ne $userenv{'permanentemail'} )) {
             $namechanged = 1;
         }
@@ -1719,6 +1750,7 @@ sub update_user_data {
             $changeHash{'middlename'} = $env{'form.cmiddlename'};
             $changeHash{'lastname'}   = $env{'form.clastname'};
             $changeHash{'generation'} = $env{'form.cgeneration'};
+            $changeHash{'id'}         = $env{'form.cid'};
             $changeHash{'permanentemail'} = $env{'form.cpermanentemail'};
             my $putresult = &Apache::lonnet::put
                 ('environment',\%changeHash,
@@ -1731,6 +1763,7 @@ sub update_user_data {
                              'mddl' => "middle",
                              'lst'  => "last",
 			     'gen'  => "generation",
+                             'id'   => "ID/Student number",
                              'mail' => "permanent e-mail",
                              'disk' => "disk space allocated to portfolio files",
                              'prvs' => "Previous",
@@ -1744,6 +1777,7 @@ sub update_user_data {
     <th>$lt{'mddl'}</th>
     <th>$lt{'lst'}</th>
     <th>$lt{'gen'}</th>
+    <th>$lt{'id'}</th>
     <th>$lt{'mail'}</th>
     <th>$lt{'disk'}</th></tr>
 <tr><td>$lt{'prvs'}</td>
@@ -1751,6 +1785,7 @@ sub update_user_data {
     <td>$userenv{'middlename'} </td>
     <td>$userenv{'lastname'}   </td>
     <td>$userenv{'generation'} </td>
+    <td>$userenv{'id'}</td>
     <td>$userenv{'permanentemail'} </td>
     <td>$oldportfolioquota Mb</td>
 </tr>
@@ -1759,10 +1794,24 @@ sub update_user_data {
     <td>$env{'form.cmiddlename'} </td>
     <td>$env{'form.clastname'}   </td>
     <td>$env{'form.cgeneration'} </td>
+    <td>$env{'form.cid'} </td>
     <td>$env{'form.cpermanentemail'} </td>
     <td>$newportfolioquota Mb $defquotatext </td></tr>
 </table>
 END
+                if (($forceid) && ($recurseid) && (!$disallowed_id) &&
+                    (&Apache::lonnet::allowed('mau',$env{'form.ccdomain'}))) {
+                    my %userupdate = (
+                                  lastname   => $env{'form.clasaname'},
+                                  middlename => $env{'form.cmiddlename'},
+                                  firstname  => $env{'form.cfirstname'},
+                                  generation => $env{'fora.cgeneration'},
+                                  id         => $env{'form.cid'},
+                             );
+                    my $idresult = &propagate_id_change($env{'form.ccname'},
+                                    $env{'form.ccdomain'},\%userupdate);
+                    $r->print('<br />'.$idresult.'<br />');
+                }
                 if (($env{'form.ccdomain'} eq $env{'user.domain'}) && 
                     ($env{'form.ccuname'} eq $env{'user.name'})) {
                     my %newenvhash;
@@ -1786,12 +1835,19 @@ 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(
+                           'id'   => "ID/Student number",
                            'mail' => "Permanent e-mail",
                            'disk' => "Disk space allocated to user's portfolio files",
 					       );
             $r->print(<<"END");
-<h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} $userenv{'generation'}&nbsp;&nbsp;($lt{'mail'}: $userenv{'permanentemail'})</h4>
+<h4>$userenv{'firstname'} $userenv{'middlename'} $userenv{'lastname'} $userenv{'generation'}
 END
+            if ($userenv{'permanentemail'} eq '') {
+                $r->print('</h4>');
+            } else {
+                $r->print('&nbsp;&nbsp;('.$lt{'mail'}.': '.
+                          $userenv{'permanentemail'}.')</h4>');
+            }
             if ($putresult eq 'ok') {
                 if ($oldportfolioquota != $newportfolioquota) {
                     $r->print('<h4>'.$lt{'disk'}.': '.$newportfolioquota.' Mb '. 
@@ -1824,8 +1880,8 @@ END
 						 $env{'form.ccdomain'},$now);
 		    $r->print($result);
 		}
-	    } 
-	    if ($key=~m{^form\.rev\:([^_]+)_cr\.cr/($match_domain)/($match_username)/(\w+)$ }s) {
+	    }
+	    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.': <b>'.
@@ -2511,7 +2567,7 @@ sub handler {
                         $response = '';
                     }
                     &print_user_modification_page($r,$ccuname,$ccdomain,
-                                                  $srch,$response);
+                                                  $srch,$response,$context);
                 } elsif ($currstate eq 'query') {
                     &print_user_query_page($r,'createuser');
                 } else {
@@ -2521,7 +2577,8 @@ sub handler {
             } 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);
+                &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
+                                              $context);
             }
         } elsif ($env{'form.phase'} eq 'update_user_data') {
             &update_user_data($r);
@@ -2823,7 +2880,8 @@ sub user_search_result {
 					{&Apache::lonnet::get('environment',
 							     ['firstname',
 							      'lastname',
-							      'permanentemail'])};
+							      'permanentemail'],
+							      $cudomain,$cuname)};
                                 }
                             }
                         }