--- loncom/interface/loncreateuser.pm	2007/12/21 18:16:39	1.220
+++ loncom/interface/loncreateuser.pm	2007/12/21 20:34:26	1.221
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Create a user
 #
-# $Id: loncreateuser.pm,v 1.220 2007/12/21 18:16:39 raeburn Exp $
+# $Id: loncreateuser.pm,v 1.221 2007/12/21 20:34:26 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -2730,10 +2730,11 @@ sub handler {
         if (! exists($env{'form.state'})) {
             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
         } elsif ($env{'form.state'} eq 'got_file') {
-            &Apache::lonuserutils::print_upload_manager_form($r,$context);
+            &Apache::lonuserutils::print_upload_manager_form($r,$context,
+                                                             $permission);
         } elsif ($env{'form.state'} eq 'enrolling') {
             if ($env{'form.datatoken'}) {
-                &Apache::lonuserutils::upfile_drop_add($r,$context);
+                &Apache::lonuserutils::upfile_drop_add($r,$context,$permission);
             }
         } else {
             &Apache::lonuserutils::print_first_users_upload_form($r,$context);
@@ -2755,16 +2756,18 @@ sub handler {
                 my $response;
                 if ($env{'form.srchterm'} !~ /^$match_username$/) {
                     my $response = &mt('You must specify a valid username. Only the following are allowed: letters numbers - . @');
+                    $env{'form.phase'} = '';
                     &print_username_entry_form($r,$context,$response,$srch);
                 } else {
                     my $ccuname =&LONCAPA::clean_username($srch->{'srchterm'});
                     my $ccdomain=&LONCAPA::clean_domain($srch->{'srchdomain'});
                     &print_user_modification_page($r,$ccuname,$ccdomain,
-                                                  $srch,$response,$context);
+                                                  $srch,$response,$context,
+                                                  $permission);
                 }
             } elsif ($env{'form.phase'} eq 'get_user_info') {
                 my ($currstate,$response,$forcenewuser,$results) = 
-                    &user_search_result($srch);
+                    &user_search_result($context,$srch);
                 if ($env{'form.currstate'} eq 'modify') {
                     $currstate = $env{'form.currstate'};
                 }
@@ -2793,7 +2796,8 @@ sub handler {
                         $response = '';
                     }
                     &print_user_modification_page($r,$ccuname,$ccdomain,
-                                                  $srch,$response,$context);
+                                                  $srch,$response,$context,
+                                                  $permission);
                 } elsif ($currstate eq 'query') {
                     &print_user_query_page($r,'createuser');
                 } else {
@@ -2804,7 +2808,7 @@ sub handler {
                 my $ccuname = &LONCAPA::clean_username($env{'form.seluname'});
                 my $ccdomain = &LONCAPA::clean_domain($env{'form.seludom'});
                 &print_user_modification_page($r,$ccuname,$ccdomain,$srch,'',
-                                              $context);
+                                              $context,$permission);
             }
         } elsif ($env{'form.phase'} eq 'update_user_data') {
             &update_user_data($r,$context);
@@ -2821,15 +2825,22 @@ sub handler {
              ($permission->{'view'} || $permission->{'cusr'})) {
         if ($env{'form.phase'} eq 'bulkchange') {
             &Apache::lonhtmlcommon::add_breadcrumb
-                ({href=>'backPage(document.studentform)',
-                  text=>"List Users"});
+                ({href=>'/adm/createuser?action=listusers',
+                  text=>"List Users"},
+                {href=>"/adm/createuser",
+                  text=>"Result"});
             my $setting = $env{'form.roletype'};
             my $choice = $env{'form.bulkaction'};
             $r->print(&header());
-            $r->print(&Apache::lonhtmlcommon::breadcrumbs("List Users",
+            $r->print(&Apache::lonhtmlcommon::breadcrumbs("Update Users",
                                                           'User_Management_List'));
             if ($permission->{'cusr'}) {
                 &Apache::lonuserutils::update_user_list($r,$context,$setting,$choice);
+                $r->print('<p><a href="/adm/createuser?action=listusers">'.&mt('Display User Lists').'</a>');
+                $r->print(&Apache::loncommon::end_page());
+            } else {
+                $r->print(&mt('You are not authorized to make bulk changes to user roles'));
+                $r->print(&Apache::loncommon::end_page());
             }
         } else {
             &Apache::lonhtmlcommon::add_breadcrumb
@@ -2882,7 +2893,8 @@ sub handler {
     } elsif ($env{'form.action'} eq 'dateselect') {
         if ($permission->{'cusr'}) {
             $r->print(&header(undef,undef,{'no_nav_bar' => 1}).
-                      &Apache::lonuserutils::date_section_selector($context).
+                      &Apache::lonuserutils::date_section_selector($context,
+                                                                   $permission).
                       &Apache::loncommon::end_page());
         } else {
             $r->print(&header().
@@ -2936,17 +2948,17 @@ sub print_main_menu {
     my %links = (
                        domain => {
                                    upload => 'Upload a File of Users',
-                                   singleuser => 'Add/Manage a Single User',
+                                   singleuser => 'Add/Modify a Single User',
                                    listusers => 'Manage Multiple Users',
                                  },
                        author => {
                                    upload => 'Upload a File of Co-authors',
-                                   singleuser => 'Add/Manage a Single Co-author',
+                                   singleuser => 'Add/Modify a Single Co-author',
                                    listusers => 'Display Co-authors and Manage Multiple Users',
                                  },
                        course => {
                                    upload => 'Upload a File of Course Users',
-                                   singleuser => 'Add/Manage a Single Course User',
+                                   singleuser => 'Add/Modify a Single Course User',
                                    listusers => 'Display Class Lists and Manage Multiple Users',
                                  },
                      );
@@ -3040,7 +3052,7 @@ sub restore_prev_selections {
 
 #-------------------------------------------------- functions for &phase_two
 sub user_search_result {
-    my ($srch) = @_;
+    my ($context,$srch) = @_;
     my %allhomes;
     my %inst_matches;
     my %srch_results;
@@ -3109,14 +3121,14 @@ sub user_search_result {
                         my $uhome=&Apache::lonnet::homeserver($srch->{'srchterm'},$srch->{'srchdomain'});
                         if ($uhome eq 'no_host') {
                             ($currstate,$response,$forcenewuser) =
-                                &build_search_response($srch,%srch_results);
+                                &build_search_response($context,$srch,%srch_results);
                         } else {
                             $currstate = 'modify';
                         }
                     } else {
                         %srch_results = &Apache::lonnet::usersearch($srch);
                         ($currstate,$response,$forcenewuser) =
-                            &build_search_response($srch,%srch_results);
+                            &build_search_response($context,$srch,%srch_results);
                     }
                 } else {
                     my $courseusers = &get_courseusers();
@@ -3125,7 +3137,7 @@ sub user_search_result {
                             $currstate = 'modify';
                         } else {
                             ($currstate,$response,$forcenewuser) =
-                                &build_search_response($srch,%srch_results);
+                                &build_search_response($context,$srch,%srch_results);
                         }
                     } else {
                         foreach my $user (keys(%$courseusers)) {
@@ -3152,7 +3164,7 @@ sub user_search_result {
                             }
                         }
                         ($currstate,$response,$forcenewuser) =
-                            &build_search_response($srch,%srch_results);
+                            &build_search_response($context,$srch,%srch_results);
                     }
                 }
             }
@@ -3162,7 +3174,7 @@ sub user_search_result {
             ($dirsrchres,%srch_results) = &Apache::lonnet::inst_directory_query($srch);
             if ($dirsrchres eq 'ok') {
                 ($currstate,$response,$forcenewuser) = 
-                    &build_search_response($srch,%srch_results);
+                    &build_search_response($context,$srch,%srch_results);
             } else {
                 my $showdom = &display_domain_info($srch->{'srchdomain'});
                 $response = '<span class="LC_warning">'.
@@ -3176,7 +3188,7 @@ sub user_search_result {
         if ($srch->{'srchin'} eq 'dom') {
             %srch_results = &Apache::lonnet::usersearch($srch);
             ($currstate,$response,$forcenewuser) = 
-                &build_search_response($srch,%srch_results); 
+                &build_search_response($context,$srch,%srch_results); 
         } elsif ($srch->{'srchin'} eq 'crs') {
             my $courseusers = &get_courseusers(); 
             foreach my $user (keys(%$courseusers)) {
@@ -3228,14 +3240,14 @@ sub user_search_result {
                 }
             }
             ($currstate,$response,$forcenewuser) = 
-                &build_search_response($srch,%srch_results); 
+                &build_search_response($context,$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);
+                    &build_search_response($context,$srch,%srch_results);
             } else {
                 my $showdom = &display_domain_info($srch->{'srchdomain'});                $response = '<span class="LC_warning">'.
                     &mt('Institutional directory search is not available in domain: [_1]',$showdom).
@@ -3341,7 +3353,7 @@ sub get_courseusers {
 }
 
 sub build_search_response {
-    my ($srch,%srch_results) = @_;
+    my ($context,$srch,%srch_results) = @_;
     my ($currstate,$response,$forcenewuser);
     my %names = (
           'uname' => 'username',
@@ -3400,8 +3412,15 @@ sub build_search_response {
                 }
             }
             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'}); 
+                my $cancreate =
+                    &Apache::lonuserutils::can_create_user($env{'request.role.domain'},$context);
+                if ($cancreate) {
+                    my $showdom = &display_domain_info($env{'request.role.domain'}); 
                 $response .= '<br /><br />'.&mt("<b>To add a new user</b> (you can only create new users in your current role's domain - <span class=\"LC_cusr_emph\">[_1]</span>):",$env{'request.role.domain'}).'<ul><li>'.&mt("Set 'Domain/institution to search' to: <span class=\"LC_cusr_emph\">[_1]</span>",$showdom).'<li>'.&mt("Set 'Search criteria' to: <span class=\"LC_cusr_emph\">'username is ...... in selected LON-CAPA domain'").'</span></li><li>'.&mt('Provide the proposed username').'</li><li>'.&mt('Search').'</li></ul><br />';
+                } else {
+                    my $helplink = ' href="javascript:helpMenu('."'display'".')"';
+                    $response .= '<br /><br />'.&mt("You are not authorized to create new users in your current role's domain - <span class=\"LC_cusr_emph\">[_1]</span>.",$env{'request.role.domain'}).'<br />'.&mt('Contact the <a[_1]>helpdesk</a> if you need to create a new user.',$helplink).'<br /><br />';
+                }
             }
         }
     }
@@ -3503,83 +3522,24 @@ sub course_level_table {
         }
         my @roles = &Apache::lonuserutils::roles_by_context('course');
 	foreach my $role (@roles) {
+            my $plrole=&Apache::lonnet::plaintext($role);
 	    if (&Apache::lonnet::allowed('c'.$role,$thiscourse)) {
-		my $plrole=&Apache::lonnet::plaintext($role);
-		$table .= &Apache::loncommon::start_data_table_row().
-'<td><input type="checkbox" name="act_'.$protectedcourse.'_'.$role.'" /></td>
-<td>'.$plrole.'</td>
-<td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
-	        if ($role ne 'cc') {
-                    if (%sections_count) {
-                        my $currsec = 
-                            &Apache::lonuserutils::course_sections(\%sections_count,
-                                                        $protectedcourse.'_'.$role);
-                        $table .= 
-                    '<td><table class="LC_createuser">'.
-                     '<tr class="LC_section_row">
-                        <td valign="top">'.$lt{'exs'}.'<br />'.
-                        $currsec.'</td>'.
-                     '<td>&nbsp;&nbsp;</td>'.
-                     '<td valign="top">&nbsp;'.$lt{'new'}.'<br />'.
-                     '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.'" value="" />'.
-                     '<input type="hidden" '.
-                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'.
-                     '</tr></table></td>';
-                    } else {
-                        $table .= '<td><input type="text" size="10" '.
-                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>';
-                    }
-                } else { 
-		    $table .= '<td>&nbsp</td>';
+                $table .= &course_level_row($protectedcourse,$role,$area,$domain,
+                                            $plrole,\%sections_count,\%lt);    
+            } elsif ($env{'request.course.sec'} ne '') {
+                if (&Apache::lonnet::allowed('c'.$role,$thiscourse.'/'.
+                                             $env{'request.course.sec'})) {
+                    $table .= &course_level_row($protectedcourse,$role,$area,$domain,
+                                                $plrole,\%sections_count,\%lt);
                 }
-		$table .= <<ENDTIMEENTRY;
-<td><input type="hidden" name="start_$protectedcourse\_$role" value='' />
-<a href=
-"javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$role.value,'start_$protectedcourse\_$role','cu.pres','dateset')">$lt{'ssd'}</a></td>
-<td><input type="hidden" name="end_$protectedcourse\_$role" value='' />
-<a href=
-"javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt{'sed'}</a></td>
-ENDTIMEENTRY
-                $table.= &Apache::loncommon::end_data_table_row();
             }
         }
-        foreach my $cust (sort keys %customroles) {
-	    if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
-		my $plrole=$cust;
-                my $customrole=$protectedcourse.'_cr_cr_'.$env{'user.domain'}.
-		    '_'.$env{'user.name'}.'_'.$plrole;
-		$table .= &Apache::loncommon::start_data_table_row().
-'<td><input type="checkbox" name="act_'.$customrole.'" /></td>
-<td>'.$plrole.'</td>
-<td>'.$area.'</td>'."\n";
-                if (%sections_count) {
-                    my $currsec = 
-                        &Apache::lonuserutils::course_sections(\%sections_count,
-                                                               $customrole);
-                    $table.=
-                   '<td><table class="LC_createuser">'.
-                   '<tr class="LC_section_row"><td valign="top">'.
-                   $lt{'exs'}.'<br />'.$currsec.'</td>'.
-                   '<td>&nbsp;&nbsp;</td>'.
-                   '<td valign="top">&nbsp;'.$lt{'new'}.'<br />'.
-                   '<input type="text" name="newsec_'.$customrole.'" value="" /></td>'.
-                   '<input type="hidden" '.
-                   'name="sec_'.$customrole.'" /></td>'.
-                   '</tr></table></td>';
-                } else {
-                    $table .= '<td><input type="text" size="10" '.
-                     'name="sec_'.$customrole.'" /></td>';
-                }
-                $table .= <<ENDENTRY;
-<td><input type="hidden" name="start_$customrole" value='' />
-<a href=
-"javascript:pjump('date_start','Start Date $plrole',document.cu.start_$customrole.value,'start_$customrole','cu.pres','dateset')">$lt{'ssd'}</a></td>
-<td><input type="hidden" name="end_$customrole" value='' />
-<a href=
-"javascript:pjump('date_end','End Date $plrole',document.cu.end_$customrole.value,'end_$customrole','cu.pres','dateset')">$lt{'sed'}</a></td>
-ENDENTRY
-               $table .= &Apache::loncommon::end_data_table_row();
-           }
+        if (&Apache::lonnet::allowed('ccr',$thiscourse)) {
+            foreach my $cust (sort keys %customroles) {
+                my $role = 'cr_cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cust;
+                $table .= &course_level_row($protectedcourse,$role,$area,$domain,
+                                            $cust,\%sections_count,\%lt);
+            }
 	}
     }
     return '' if ($table eq ''); # return nothing if there is nothing 
@@ -3599,6 +3559,52 @@ $table.
     return $result;
 }
 
+sub course_level_row {
+    my ($protectedcourse,$role,$area,$domain,$plrole,$sections_count,$lt) = @_;
+    my $table = &Apache::loncommon::start_data_table_row().
+                ' <td><input type="checkbox" name="act_'.
+                $protectedcourse.'_'.$role.'" /></td>'."\n".
+                ' <td>'.$plrole.'</td>'."\n".
+                '<td>'.$area.'<br />Domain: '.$domain.'</td>'."\n";
+    if ($role eq 'cc') {
+        $table .= '<td>&nbsp</td>';
+    } elsif ($env{'request.course.sec'} ne '') {
+        $table .= ' <td><input type="hidden" value="'.
+                  $env{'request.course.sec'}.'" '.
+                  'name="sec_'.$protectedcourse.'_'.$role.'" />'.
+                  $env{'request.course.sec'}.'</td>';
+    } else {
+        if (ref($sections_count) eq 'HASH') {
+            my $currsec = 
+                &Apache::lonuserutils::course_sections($sections_count,
+                                                       $protectedcourse.'_'.$role);
+            $table .= '<td><table class="LC_createuser">'.
+                      '<tr class="LC_section_row">
+                        <td valign="top">'.$lt->{'exs'}.'<br />'.
+                        $currsec.'</td>
+                        <td>&nbsp;&nbsp;</td>
+                        <td valign="top">&nbsp;'.$lt->{'new'}.'<br />'.
+                     '<input type="text" name="newsec_'.$protectedcourse.'_'.$role.
+                     '" value="" />'.
+                     '<input type="hidden" '.
+                     'name="sec_'.$protectedcourse.'_'.$role.'" /></td>'."\n".
+                     '</tr></table></td>';
+        } else {
+            $table .= '<td><input type="text" size="10" '.
+                      'name="sec_'.$protectedcourse.'_'.$role.'" /></td>';
+        }
+    }
+    $table .= <<ENDTIMEENTRY;
+<td><input type="hidden" name="start_$protectedcourse\_$role" value='' />
+<a href=
+"javascript:pjump('date_start','Start Date $plrole',document.cu.start_$protectedcourse\_$role.value,'start_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'ssd'}</a></td>
+<td><input type="hidden" name="end_$protectedcourse\_$role" value='' />
+<a href=
+"javascript:pjump('date_end','End Date $plrole',document.cu.end_$protectedcourse\_$role.value,'end_$protectedcourse\_$role','cu.pres','dateset')">$lt->{'sed'}</a></td>
+ENDTIMEENTRY
+    $table.= &Apache::loncommon::end_data_table_row();
+}
+
 sub course_level_dc {
     my ($dcdom) = @_;
     my %customroles=&Apache::lonuserutils::my_custom_roles();
@@ -3645,6 +3651,7 @@ sub course_level_dc {
                      '<td>&nbsp;&nbsp;</td>'.
                      '<td valign="top">&nbsp;<b>'.$lt{'new'}.'</b><br />'.
                      '<input type="text" name="newsec" value="" />'.
+                     '<input type="hidden" name="sections" value="" />'.
                      '<input type="hidden" name="groups" value="" /></td>'.
                      '</tr></table></td>';
     $otheritems .= <<ENDTIMEENTRY;