--- loncom/interface/lonuserutils.pm	2009/10/30 03:57:35	1.101
+++ loncom/interface/lonuserutils.pm	2010/01/19 18:02:20	1.113
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Utility functions for managing LON-CAPA user accounts
 #
-# $Id: lonuserutils.pm,v 1.101 2009/10/30 03:57:35 raeburn Exp $
+# $Id: lonuserutils.pm,v 1.113 2010/01/19 18:02:20 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -91,7 +91,7 @@ sub modifyuserrole {
     if ($setting eq 'course' || $context eq 'course') {
         $scope = '/'.$cid;
         $scope =~ s/\_/\//g;
-        if ($role ne 'cc' && $sec ne '') {
+        if (($role ne 'cc') && ($role ne 'co') && ($sec ne '')) {
             $scope .='/'.$sec;
         }
     } elsif ($context eq 'domain') {
@@ -247,6 +247,10 @@ sub domain_roles_select {
     foreach my $roletype (@roletypes) {
         # set up the text for this domain
         $select_menus{$roletype}->{'text'}= $lt{$roletype};
+        my $crstype;
+        if ($roletype eq 'community') {
+            $crstype = 'Community';
+        }
         # we want a choice of 'default' as the default in the second menu
         if ($env{'form.roletype'} ne '') {
             $select_menus{$roletype}->{'default'} = $env{'form.showrole'};
@@ -271,7 +275,7 @@ sub domain_roles_select {
                               &mt('Custom role');
             } else {
                 $select_menus{$roletype}->{'select2'}->{$role} = 
-                              &Apache::lonnet::plaintext($role);
+                              &Apache::lonnet::plaintext($role,$crstype);
             }
         }
         $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
@@ -291,7 +295,7 @@ sub hidden_input {
 }
 
 sub print_upload_manager_header {
-    my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission)=@_;
+    my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission,$crstype)=@_;
     my $javascript;
     #
     if (! exists($env{'form.upfile_associate'})) {
@@ -328,7 +332,7 @@ sub print_upload_manager_header {
     my $javascript_validations =
         &javascript_validations('upload',$krbdefdom,$password_choice,undef,
                                 $env{'request.role.domain'},$context,
-                                $groupslist);
+                                $groupslist,$crstype);
     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
     $r->print('<p>'
              .&mt('Total number of records found in file: [_1]'
@@ -361,7 +365,7 @@ sub print_upload_manager_header {
 ###############################################################
 sub javascript_validations {
     my ($mode,$krbdefdom,$curr_authtype,$curr_authfield,$domain,
-        $context,$groupslist)=@_;
+        $context,$groupslist,$crstype)=@_;
     my %param = (
                   kerb_def_dom => $krbdefdom,
                   curr_authtype => $curr_authtype,
@@ -382,10 +386,10 @@ sub javascript_validations {
         if (($context eq 'course') || ($context eq 'domain')) {
             if ($context eq 'course') {
                 if ($env{'request.course.sec'} eq '') {
-                    $setsection_call = 'setSections(document.'.$param{'formname'}.');';
+                    $setsection_call = 'setSections(document.'.$param{'formname'}.",'$crstype'".');';
                     $setsections_js =
                         &setsections_javascript($param{'formname'},$groupslist,
-                                                $mode);
+                                                $mode,'',$crstype);
                 } else {
                     $setsection_call = "'ok'";
                 }
@@ -826,7 +830,7 @@ sub print_upload_manager_footer {
                .'&nbsp;'.&mt('Add a domain role').'</label>'
                .'&nbsp;&nbsp;&nbsp;<label>'
                .'<input type="radio" name="roleaction" value="course" />'
-               .'&nbsp;'.&mt('Add a course role').'</label>'
+               .'&nbsp;'.&mt('Add a course/community role').'</label>'
                .'</span>';
     } elsif ($context eq 'author') {
         $Str .= &Apache::lonhtmlcommon::row_title(
@@ -895,7 +899,7 @@ sub print_upload_manager_footer {
            .'<hr />';
     if ($context eq 'course') {
         $Str .= '<p class="LC_info">'
-               .&mt('Note: For large courses, this operation may be time consuming.')
+               .&mt('Note: This operation may be time consuming when adding several users.')
                .'</p>';
     }
     $Str .= '<p><input type="button"'
@@ -975,7 +979,7 @@ sub print_upload_manager_form {
         &Apache::loncommon::get_kerberos_defaults($defdom);
     #
     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
-                                 $permission);
+                                 $permission,$crstype);
     my $i;
     my $keyfields;
     if ($total>=0) {
@@ -1172,7 +1176,7 @@ sub default_role_selector {
     my %customroles;
     my ($options,$coursepick,$cb_jscript);
     if ($context ne 'author') {
-        %customroles = &my_custom_roles();
+        %customroles = &my_custom_roles($crstype);
     }
 
     my %lt=&Apache::lonlocal::texthash(
@@ -1198,16 +1202,16 @@ sub default_role_selector {
            $options .= '  <option value="'.$role.'">'.$plrole.'</option>';
         }
         my $courseform = &Apache::loncommon::selectcourse_link
-            ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course');
+            ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course/Community');
         $cb_jscript = 
-            &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform');
+            &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform','courserole','Course/Community');
         $coursepick = &Apache::loncommon::start_data_table().
                       &Apache::loncommon::start_data_table_header_row().
                       '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th>'.
                       '<th>'.$lt{'grs'}.'</th>'.
                       &Apache::loncommon::end_data_table_header_row().
                       &Apache::loncommon::start_data_table_row()."\n".
-                      '<td><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc',''".')" /></td>'."\n".
+                      '<td><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc','','','','crstype'".')" /></td>'."\n".
                       '<td><select name="courserole">'."\n".
                       &default_course_roles($context,$checkpriv,'Course',%customroles)."\n".
                       '</select></td><td>'.
@@ -1225,6 +1229,7 @@ sub default_role_selector {
                       $env{'request.role.domain'}.'" />'.
                       '<input type="hidden" name="dccourse" value="" />'.
                       '<input type="hidden" name="dcdomain" value="" />'.
+                      '<input type="hidden" name="crstype" value="" />'.
                       '</td></tr></table></td>'.
                       &Apache::loncommon::end_data_table_row().
                       &Apache::loncommon::end_data_table()."\n";
@@ -1290,7 +1295,13 @@ sub domain_roles {
 
 sub course_roles {
     my ($context,$checkpriv,$custom,$roletype) = @_;
-    my @allroles = &roles_by_context('course',$custom,$roletype);
+    my $crstype;
+    if ($roletype eq 'community') {
+        $crstype = 'Community' ;
+    } else {
+        $crstype = 'Course';
+    }
+    my @allroles = &roles_by_context('course',$custom,$crstype);
     my @roles;
     if ($context eq 'domain') {
         @roles = @allroles;
@@ -1339,10 +1350,14 @@ sub curr_role_permissions {
 # ======================================================= Existing Custom Roles
 
 sub my_custom_roles {
+    my ($crstype) = @_;
     my %returnhash=();
     my %rolehash=&Apache::lonnet::dump('roles');
-    foreach my $key (keys %rolehash) {
+    foreach my $key (keys(%rolehash)) {
         if ($key=~/^rolesdef\_(\w+)$/) {
+            if ($crstype eq 'Community') {
+                next if ($rolehash{$key} =~ /bre\&S/); 
+            }
             $returnhash{$1}=$1;
         }
     }
@@ -1431,26 +1446,50 @@ sub print_userlist {
                   '<input type="hidden" name="phase" value="" /></form>');
         return;
     }
-    if (!(($context eq 'domain') && ($env{'form.roletype'} eq 'course'))) {
-        $r->print('&nbsp;'.&list_submit_button(&mt('Update Display')).
-                  "\n</p>\n");
+    if (!(($context eq 'domain') && 
+          (($env{'form.roletype'} eq 'course') || ($env{'form.roletype'} eq 'community')))) {
+        $r->print(
+            "\n</p>\n"
+           .'<p>'
+           .&list_submit_button(&mt('Update Display'))
+           ."</p>\n"
+        );
     }
     my ($indexhash,$keylist) = &make_keylist_array();
-    my (%userlist,%userinfo);
-    if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
+    my (%userlist,%userinfo,$clearcoursepick);
+    if (($context eq 'domain') && 
+        ($env{'form.roletype'} eq 'course') || 
+        ($env{'form.roletype'} eq 'community')) {
+        my ($crstype,$numcodes,$title,$warning);
+        if ($env{'form.roletype'} eq 'course') {
+            $crstype = 'Course';
+            $numcodes = $totcodes;
+            $title = &mt('Select Courses');
+            $warning = &mt('Warning: data retrieval for multiple courses can take considerable time, as this operation is not currently optimized.');
+        } elsif ($env{'form.roletype'} eq 'community') {
+            $crstype = 'Community';
+            $numcodes = 0;
+            $title = &mt('Select Communities');
+            $warning = &mt('Warning: data retrieval for multiple communities can take considerable time, as this operation is not currently optimized.');
+        }
         my $courseform =
-            &Apache::lonhtmlcommon::course_selection($formname,$totcodes,
-                                         $codetitles,$idlist,$idlist_titles);
+            &Apache::lonhtmlcommon::course_selection($formname,$numcodes,
+                            $codetitles,$idlist,$idlist_titles,$crstype);
         $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
                   &Apache::lonhtmlcommon::start_pick_box()."\n".
-                  &Apache::lonhtmlcommon::row_title(&mt('Select Course(s)'),
-                                                    'LC_oddrow_value')."\n".
+                  &Apache::lonhtmlcommon::row_title($title,'LC_oddrow_value')."\n".
                   $courseform."\n".
                   &Apache::lonhtmlcommon::row_closure(1).
                   &Apache::lonhtmlcommon::end_pick_box().'</p>'.
-                  '<p>'.&list_submit_button(&mt('Update Display')).
-                  "\n".'</p><span class="LC_warning">'.&mt('Warning: data retrieval for multiple courses can take considerable time, as this operation is not currently optimized.').'</span>'."\n");
-        if ($env{'form.coursepick'}) {
+                  '<p><input type="hidden" name="origroletype" value="'.$env{'form.roletype'}.'" />'.
+                  &list_submit_button(&mt('Update Display')).
+                  "\n".'</p><span class="LC_warning">'.$warning.'</span>'."\n");
+        $clearcoursepick = 0;
+        if (($env{'form.origroletype'} ne '') &&
+            ($env{'form.origroletype'} ne $env{'form.roletype'})) {
+            $clearcoursepick = 1;
+        }
+        if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
             $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
         }
     } else {
@@ -1525,7 +1564,7 @@ sub print_userlist {
                 }
             } elsif (($env{'form.roletype'} eq 'course') ||
                      ($env{'form.roletype'} eq 'community')) {
-                if ($env{'form.coursepick'}) {
+                if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
                     my %courses = &process_coursepick();
                     my %allusers;
                     my $hidepriv = 1;
@@ -1653,7 +1692,7 @@ sub role_filter {
         $role_select .= '</select>';
         $output = '<label><span class="LC_nobreak">'
                  .&mt('Role: [_1]',$role_select)
-                 .'</span></label>';
+                 .'</span></label> ';
     }
     return $output;
 }
@@ -1682,7 +1721,7 @@ sub section_group_filter {
             $env{'form.'.$name{$item}} = 'all';
         }
         if ($item eq 'sec') {
-            if ($env{'form.showrole'} eq 'cc') {
+            if (($env{'form.showrole'} eq 'cc') || ($env{'form.showrole'} eq 'co')) {
                 $env{'form.'.$name{$item}} = 'none';
             }
             my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
@@ -1693,7 +1732,7 @@ sub section_group_filter {
         }
         if (@options > 0) {
             my $currsel;
-            $markup = '<select name="'.$name{$item}.'" />'."\n";
+            $markup = '<select name="'.$name{$item}.'">'."\n";
             foreach my $option ('all','none',@options) { 
                 $currsel = '';
                 if ($env{'form.'.$name{$item}} eq $option) {
@@ -1708,7 +1747,9 @@ sub section_group_filter {
                 $markup .= '</option>'."\n";
             }
             $markup .= '</select>'."\n";
-            $output .= ('&nbsp;'x3).'<label>'.$title{$item}.': '.$markup.'</label>';
+            $output .= ('&nbsp;'x3).'<span class="LC_nobreak">'
+                      .'<label>'.$title{$item}.': '.$markup.'</label>'
+                      .'</span> ';
         }
     }
     return $output;
@@ -1761,7 +1802,8 @@ sub gather_userinfo {
                                            $indexhash,$uniqid,$userlist);
                     }
                 }
-            } elsif ($env{'form.roletype'} eq 'course') {
+            } elsif (($env{'form.roletype'} eq 'course') || 
+                     ($env{'form.roletype'} eq 'community')) {
                 ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                     split(/:/,$item);
                 if (ref($rolehash->{$item}) eq 'HASH') {
@@ -1811,7 +1853,8 @@ sub gather_userinfo {
 sub build_user_record {
     my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
     next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
-    if (!(($context eq 'domain') && ($env{'form.roletype'} eq 'course'))) {
+    if (!(($context eq 'domain') && (($env{'form.roletype'} eq 'course')
+                             && ($env{'form.roletype'} eq 'community')))) {
         &process_date_info($userdata);
     }
     my $username = $userdata->{'username'};
@@ -1926,13 +1969,17 @@ sub process_coursepick {
     my $coursefilter = $env{'form.coursepick'};
     my $cdom = $env{'request.role.domain'};
     my %courses;
+    my $crssrch = 'Course';
+    if ($env{'form.roletype'} eq 'community') {
+        $crssrch = 'Community';
+    }
     if ($coursefilter eq 'all') {
         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
-                                                 undef,undef,'Course');
+                                                 undef,undef,$crssrch);
     } elsif ($coursefilter eq 'category') {
         my $instcode = &instcode_from_coursefilter();
         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
-                                                 undef,undef,'Course');
+                                                 undef,undef,$crssrch);
     } elsif ($coursefilter eq 'specific') {
         if ($env{'form.coursetotal'} > 1) {
             my @course_ids = split(/&&/,$env{'form.courselist'});
@@ -2114,6 +2161,12 @@ function photowindow(photolink) {
 <input type="hidden" name="displayclickers" value="$displayclickers" />
 END
         }
+    } elsif ($context eq 'domain') {
+        if ($setting eq 'community') {
+            $crstype = 'Community';
+        } elsif ($setting eq 'course') {
+            $crstype = 'Course';
+        }
     }
     if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
         my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
@@ -2210,6 +2263,8 @@ END
                       );
     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
         $lt{'extent'} = &mt('Course(s): description, section(s), status');
+    } elsif ($context eq 'domain' && $env{'form.roletype'} eq 'community') {
+        $lt{'extent'} = &mt('Communities: description, section(s), status');
     } elsif ($context eq 'author') {
         $lt{'extent'} = &mt('Author'); 
     }
@@ -2221,18 +2276,21 @@ END
         if ($context eq 'course') {
             push(@cols,'section');
         }
-        if (!($context eq 'domain' && $env{'form.roletype'} eq 'course')) { 
+        if (!($context eq 'domain' && ($env{'form.roletype'} eq 'course')
+                              && ($env{'form.roletype'} eq 'community'))) { 
             push(@cols,('start','end'));
         }
         if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
             push(@cols,'role');
         }
         if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
-                                    $env{'form.roletype'} eq 'course')) {
+                                    $env{'form.roletype'} eq 'course' ||
+                                    $env{'form.roletype'} eq 'community')) {
             push (@cols,'extent');
         }
         if (($statusmode eq 'Any') && 
-            (!($context eq 'domain' && $env{'form.roletype'} eq 'course'))) {
+            (!($context eq 'domain' && (($env{'form.roletype'} eq 'course')
+             || ($env{'form.roletype'} eq 'community'))))) {
             push(@cols,'status');
         }
         if ($context eq 'course') {
@@ -2250,14 +2308,18 @@ END
     my $results_description;
     if ($mode ne 'autoenroll') {
         $results_description = &results_header_row($rolefilter,$statusmode,
-                                                   $context,$permission,$mode);
+                                                   $context,$permission,$mode,$crstype);
         $r->print('<b>'.$results_description.'</b><br /><br />');
     }
     my ($output,$actionselect,%canchange,%canchangesec);
     if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
         if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
             if ($permission->{'cusr'}) {
-                $actionselect = &select_actions($context,$setting,$statusmode,$formname);
+                unless (($context eq 'domain') && 
+                        (($setting eq 'course') || ($setting eq 'community'))) {
+                    $actionselect = 
+                        &select_actions($context,$setting,$statusmode,$formname);
+                }
             }
             $r->print(<<END);
 <input type="hidden" name="srchby"  value="uname" />
@@ -2341,7 +2403,7 @@ END
             if ($env{'form.userwin'}) {
                 $checkwin = ' checked="checked"';
             }
-            $output .= '</td><td valign="top"><span class="LC_nobreak"><input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}.'</span></td></tr></table></fieldset></div>';
+            $output .= '</td><td valign="top"  style="border-left: 1px solid;"><span class="LC_nobreak"><input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}.'</span></td></tr></table></fieldset></div>';
         }
         $output .= "\n".'<div class="LC_clear_float_footer">&nbsp;</div>'."\n".
                   &Apache::loncommon::start_data_table().
@@ -2351,11 +2413,7 @@ END
  <th><a href=\"javascript:document.$formname.sortby.value='type';document.$formname.submit();\">$lt{'type'}</a></th>
             ";
         } else {
-            if ($mode eq 'pickauthor') {
-                $output .= "\n".'<th>&nbsp;</th>'."\n";
-            } else { 
-                $output .= "\n".'<th>'.&mt('Count').'</th>'."\n";
-            }
+            $output .= "\n".'<th>&nbsp;</th>'."\n";
             if ($actionselect) {
                 $output .= '<th>'.&mt('Select').'</th>'."\n";
             }
@@ -2410,9 +2468,13 @@ END
                        time.'_'.rand(1000000000).'.csv';
         unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
             $r->log_error("Couldn't open $CSVfilename for output $!");
-            $r->print(&mt('Problems occurred in writing the CSV file. '
-                         .'This error has been logged. '
-                         .'Please alert your LON-CAPA administrator.'));
+            $r->print(
+                '<p class="LC_error">'
+               .&mt('Problems occurred in writing the CSV file.')
+               .' '.&mt('This error has been logged.')
+               .' '.&mt('Please alert your LON-CAPA administrator.')
+               .'</p>'
+            );
             $CSVfile = undef;
         }
         #
@@ -2486,7 +2548,8 @@ END
                 }
             } elsif ($env{'form.roletype'} eq 'author') {
                 ($uname,$udom,$role) = split(/:/,$user,-1);
-            } elsif ($env{'form.roletype'} eq 'course') {
+            } elsif (($env{'form.roletype'} eq 'course') || 
+                     ($env{'form.roletype'} eq 'community')) {
                 ($uname,$udom,$role) = split(/:/,$user);
             }
         } else {
@@ -2593,7 +2656,7 @@ END
         if ($clickers!~/\w/) { $clickers='-'; }
         $in{'clicker'} = $clickers; 
 	my $role = $in{'role'};
-        $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype); 
+        $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype);
         if (! defined($in{'start'}) || $in{'start'} == 0) {
             $in{'start'} = &mt('none');
         } else {
@@ -2880,7 +2943,7 @@ sub print_username_link {
         $output = $in->{'username'};
     } else {
         $output = '<a href="javascript:username_display_launch('.
-                  "'$in->{'username'}','$in->{'domain'}'".')" />'.
+                  "'$in->{'username'}','$in->{'domain'}'".')">'.
                   $in->{'username'}.'</a>';
     }
     return $output;
@@ -3053,7 +3116,7 @@ END
         } else {
             opener.document.$callingform.retainsec.value = formname.retainsec.value;
         }
-        setSections(formname);
+        setSections(formname,'$crstype');
         if (seccheck == 'ok') {
             opener.document.$callingform.newsecs.value = formname.sections.value;
         }
@@ -3113,7 +3176,7 @@ ENDJS
                  fors => 'For student roles changing the section, will result in a section switch as students may only be in one section of a course at a time.',
                  forn => 'For a role in a course that is not a student role, a user may have roles in more than one section of a course at a time.',
                  reta => "Retain each user's current section affiliations?", 
-                 dnap => '(Does not apply to student roles).', 
+                 dnap => '(Does not apply to student roles).',
             );
     my ($date_items,$headertext);
     if ($env{'form.bulkaction'} eq 'chgsec') {
@@ -3135,6 +3198,11 @@ ENDJS
     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
         my ($cnum,$cdom) = &get_course_identity();
         my $crstype = &Apache::loncommon::course_type();
+        if ($crstype eq 'Community') {
+            $lt{'fors'} = &mt('For member roles changing the section, will result in a section switch as members may only be in one section of a community at a time.');
+            $lt{'forn'} = &mt('For a role in a community that is not a member role, a user may have roles in more than one section of a community at a time.');
+            $lt{'dnap'} = &mt('(Does not apply to member roles).'); 
+        }
         my $info;
         if ($env{'form.showrole'} eq 'st') {
             $output .= '<p>'.$lt{'fors'}.'</p>'; 
@@ -3200,26 +3268,50 @@ sub section_picker {
 }
 
 sub results_header_row {
-    my ($rolefilter,$statusmode,$context,$permission,$mode) = @_;
+    my ($rolefilter,$statusmode,$context,$permission,$mode,$crstype) = @_;
     my ($description,$showfilter);
     if ($rolefilter ne 'Any') {
         $showfilter = $rolefilter;
     }
     if ($context eq 'course') {
         if ($mode eq 'csv' || $mode eq 'excel') {
-            $description = &mt('Course - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
+            if ($crstype eq 'Community') {
+                $description = &mt('Community - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
+            } else {
+                $description = &mt('Course - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
+            }
         }
         if ($statusmode eq 'Expired') {
-            $description .= &mt('Users in course with expired [_1] roles',$showfilter);
+            if ($crstype eq 'Community') {
+                $description .= &mt('Users in community with expired [_1] roles',$showfilter);
+            } else {
+                $description .= &mt('Users in course with expired [_1] roles',$showfilter);
+            }
         } elsif ($statusmode eq 'Future') {
-            $description .= &mt('Users in course with future [_1] roles',$showfilter);
+            if ($crstype eq 'Community') {
+                $description .= &mt('Users in community with future [_1] roles',$showfilter);
+            } else {
+                $description .= &mt('Users in course with future [_1] roles',$showfilter);
+            }
         } elsif ($statusmode eq 'Active') {
-            $description .= &mt('Users in course with active [_1] roles',$showfilter);
+            if ($crstype eq 'Community') {
+                $description .= &mt('Users in community with active [_1] roles',$showfilter);
+            } else {
+                $description .= &mt('Users in course with active [_1] roles',$showfilter);
+            }
         } else {
             if ($rolefilter eq 'Any') {
-                $description .= &mt('All users in course');
+                if ($crstype eq 'Community') {
+                    $description .= &mt('All users in community');
+                } else {
+                    $description .= &mt('All users in course');
+                }
             } else {
-                $description .= &mt('All users in course with [_1] roles',$rolefilter);
+                if ($crstype eq 'Community') {
+                    $description .= &mt('All users in community with [_1] roles',$rolefilter);
+                } else {
+                    $description .= &mt('All users in course with [_1] roles',$rolefilter);
+                }
             }
         }
         my $constraint;
@@ -3227,7 +3319,7 @@ sub results_header_row {
         if ($viewablesec ne '') {
             if ($env{'form.showrole'} eq 'st') {
                 $constraint = &mt('only users in section "[_1]"',$viewablesec);
-            } elsif ($env{'form.showrole'} ne 'cc') {
+            } elsif (($env{'form.showrole'} ne 'cc') && ($env{'form.showrole'} ne 'co')) {
                 $constraint = &mt('only users affiliated with no section or section "[_1]"',$viewablesec);
             }
             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
@@ -3315,19 +3407,28 @@ sub results_header_row {
                     $description .= &mt('All co-authors in domain  with [_1] roles',$rolefilter);
                 }
             }
-        } elsif ($env{'form.roletype'} eq 'course') {
+        } elsif (($env{'form.roletype'} eq 'course') || 
+                 ($env{'form.roletype'} eq 'community')) {
             my $coursefilter = $env{'form.coursepick'};
-            if ($coursefilter eq 'category') {
-                my $instcode = &instcode_from_coursefilter();
-                if ($instcode eq '.') {
+            if ($env{'form.roletype'} eq 'course') {
+                if ($coursefilter eq 'category') {
+                    my $instcode = &instcode_from_coursefilter();
+                    if ($instcode eq '.') {
+                        $description .= &mt('All courses in domain').' - ';
+                    } else {
+                        $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
+                    }
+                } elsif ($coursefilter eq 'selected') {
+                    $description .= &mt('Selected courses in domain').' - ';
+                } elsif ($coursefilter eq 'all') {
                     $description .= &mt('All courses in domain').' - ';
-                } else {
-                    $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
                 }
-            } elsif ($coursefilter eq 'selected') {
-                $description .= &mt('Selected courses in domain').' - ';
-            } elsif ($coursefilter eq 'all') {
-                $description .= &mt('All courses in domain').' - ';
+            } elsif ($env{'form.roletype'} eq 'community') {
+                if ($coursefilter eq 'selected') {
+                    $description .= &mt('Selected communities in domain').' - ';
+                } elsif ($coursefilter eq 'all') {
+                    $description .= &mt('All communities in domain').' - ';
+                }
             }
             if ($statusmode eq 'Expired') {
                 $description .= &mt('users with expired [_1] roles',$showfilter);
@@ -3563,20 +3664,23 @@ sub print_first_users_upload_form {
     $str .= '<h2>'.&mt('Upload a file containing information about users').'</h2>'."\n";
 
     # Excel and CSV Help
-    $str .= '<p>'
+    $str .= '<div class="LC_left_float">'
            .&Apache::loncommon::help_open_topic("Course_Create_Class_List",
                 &mt("How do I create a users list from a spreadsheet"))
-           ."<br />\n"
+           .'</div><div class="LC_left_float">'."\n"
            .&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
                 &mt("How do I create a CSV file from a spreadsheet"))
-           ."</p>\n";
-
+           .'</div><br clear="all" />'."\n";
     $str .= &Apache::lonhtmlcommon::start_pick_box()
-           .&Apache::lonhtmlcommon::row_title(&mt('File'))
-           .'<p class="LC_info">'."\n"
-           .&mt('Please upload an UTF8 encoded file to ensure a correct character encoding in your classlist.')."\n"
-           .'</p>'."\n"
-           .&Apache::loncommon::upfile_select_html()
+           .&Apache::lonhtmlcommon::row_title(&mt('File'));
+    if (&Apache::lonlocal::current_language() ne 'en') {
+        if ($context eq 'course') { 
+            $str .= '<p class="LC_info">'."\n"
+                   .&mt('Please upload an UTF8 encoded file to ensure a correct character encoding in your classlist.')."\n"
+                   .'</p>'."\n";
+        }
+    }
+    $str .= &Apache::loncommon::upfile_select_html()
            .&Apache::lonhtmlcommon::row_closure()
            .&Apache::lonhtmlcommon::row_title(
                 '<label for="noFirstLine">'
@@ -3590,8 +3694,6 @@ sub print_first_users_upload_form {
            .'<input type="submit" name="fileupload" value="'.&mt('Next').'" />'
            .'</p>';
 
-    $str .= &Apache::loncommon::end_page();
-
     $r->print($str);
     return;
 }
@@ -3731,7 +3833,7 @@ sub upfile_drop_add {
     }
     my (%userlist,%modifiable_fields,@poss_roles);
     my $secidx = &Apache::loncoursedata::CL_SECTION();
-    my @courseroles = &roles_by_context('course',1,'',$crstype);
+    my @courseroles = &roles_by_context('course',1,$crstype);
     if (!&Apache::lonnet::allowed('mau',$domain)) {
         if ($context eq 'course' || $context eq 'author') {
             @poss_roles =  &curr_role_permissions($context,'','',$crstype);
@@ -3820,7 +3922,7 @@ sub upfile_drop_add {
             }
         }
         my (%curr_rules,%got_rules,%alerts);
-        my %customroles = &my_custom_roles();
+        my %customroles = &my_custom_roles($crstype);
         my @permitted_roles = 
             &roles_on_upload($context,$setting,$crstype,%customroles); 
         # Get new users list
@@ -4064,7 +4166,7 @@ sub upfile_drop_add {
                                         $role = 'cr_'.$env{'user.domain'}.'_'.
                                                 $env{'user.name'}.'_'.$role;
                                     }
-                                    if ($role ne 'cc') { 
+                                    if (($role ne 'cc') && ($role ne 'co')) { 
                                         if (@secs > 1) {
                                             $multiple = 1;
                                             foreach my $sec (@secs) {
@@ -4425,7 +4527,7 @@ sub update_user_list {
                 }
             } elsif ($choice eq 'chgsec') {
                 my (@newsecs,$revresult,$nochg,@retained);
-                if ($role ne 'cc') {
+                if (($role ne 'cc') && ($role ne 'co')) {
                     @newsecs = split(/,/,$env{'form.newsecs'});
                 }
                 # remove existing section if not to be retained.   
@@ -4680,7 +4782,7 @@ sub get_groupslist {
 }
 
 sub setsections_javascript {
-    my ($formname,$groupslist,$mode,$checkauth) = @_;
+    my ($formname,$groupslist,$mode,$checkauth,$crstype) = @_;
     my ($checkincluded,$finish,$rolecode,$setsection_js);
     if ($mode eq 'upload') {
         $checkincluded = 'formname.name == "'.$formname.'"';
@@ -4719,8 +4821,11 @@ sub setsections_javascript {
     }
     my %alerts = &Apache::lonlocal::texthash(
                     secd => 'Section designations do not apply to Course Coordinator roles.',
+                    sedn => 'Section designations do not apply to Coordinator roles.',
                     accr => 'A course coordinator role will be added with access to all sections.',
+                    acor => 'A coordinator role will be added with access to all sections',
                     inea => 'In each course, each user may only have one student role at a time.',
+                    inec => 'In each community, each user may only have on member role at a time.',
                     youh => 'You had selected ',
                     secs => 'sections.',
                     plmo => 'Please modify your selections so they include no more than one section.',
@@ -4728,11 +4833,13 @@ sub setsections_javascript {
                     plch => 'Please choose a different section name.',
                     mnot => 'may not be used as a section name, as it is the name of a course group.',
                     secn => 'Section names and group names must be distinct. Please choose a different section name.',
+                    nonw => 'Section names may only contain letters or numbers.',
                  );                
     $setsection_js .= <<"ENDSECCODE";
 
-function setSections(formname) {
+function setSections(formname,crstype) {
     var re1 = /^currsec_/;
+    var re2 =/\\W/;
     var groups = new Array($groupslist);
     for (var i=0;i<formname.elements.length;i++) {
         var str = formname.elements[i].name;
@@ -4740,10 +4847,13 @@ function setSections(formname) {
         if (checkcurr != null) {
             if ($checkincluded) {
                 $rolecode
-                if (role == 'cc') {
-                    alert("$alerts{'secd'}\\n$alerts{'accr'}");
-                }
-                else {
+                if (role == 'cc' || role == 'co') {
+                    if (role == 'cc') {
+                        alert("$alerts{'secd'}\\n$alerts{'accr'}");
+                    } else {
+                        alert("$alerts{'sedn'}\\n$alerts{'acor'}");
+                    }
+                } else {
                     var sections = '';
                     var numsec = 0;
                     var sections;
@@ -4773,13 +4883,30 @@ function setSections(formname) {
                     }
                     var newsecs = formname.elements[i+1].value;
                     var numsplit;
+                    var validsecs = new Array();
+                    var badsecs = new Array();
                     if (newsecs != null && newsecs != "") {
                         numsplit = newsecs.split(/,/g);
-                        numsec = numsec + numsplit.length;
+                        for (var i=0; i<numsplit.length; i++) {
+                            if (re2.test(numsplit[i]) == true) {
+                                badsecs.push(numsplit[i]);
+                            } else {
+                                validsecs.push(numsplit[i]);
+                            }
+                        }
+                        if (badsecs.length > 0) {
+                            alert("$alerts{'nonw'}\\n$alerts{'plch'}");
+                            return;
+                        }
+                        numsec = numsec + validsecs.length;
                     }
 
                     if ((role == 'st') && (numsec > 1)) {
-                        alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}")
+                        if (crstype == 'Community') {
+                            alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
+                        } else {
+                            alert("$alerts{'inco'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
+                        }
                         return;
                     }
                     else {
@@ -5053,6 +5180,7 @@ sub dc_setcourse_js {
     my ($formname,$mode,$context) = @_;
     my ($dc_setcourse_code,$authen_check);
     my $cctext = &Apache::lonnet::plaintext('cc');
+    my $cotext = &Apache::lonnet::plaintext('co');
     my %alerts = &sectioncheck_alerts();
     my $role = 'role';
     if ($mode eq 'upload') {
@@ -5099,7 +5227,11 @@ function setCourse() {
             numsections = numsections + newsecs.length;
         }
         if ((userrole == 'st') && (numsections > 1)) {
-            alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
+            if (document.$formname.crstype.value == 'Community') {
+                alert("$alerts{'inco'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
+            } else {
+                alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
+            }
             return;
         }
         for (var j=0; j<newsecs.length; j++) {
@@ -5111,7 +5243,11 @@ function setCourse() {
                 var groups = document.$formname.groups.value.split(/,/g);
                 for (var k=0; k<groups.length; k++) {
                     if (newsecs[j] == groups[k]) {
-                        alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
+                        if (document.$formname.crstype.value == 'Community') {
+                            alert("'"+newsecs[j]+"' $alerts{'mayc'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
+                        } else {
+                            alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
+                        }
                         return;
                     }
                 }
@@ -5121,13 +5257,21 @@ function setCourse() {
             alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
             section = "";
         }
+        if ((userrole == 'co') && (numsections > 0)) {
+            alert("$alerts{'secd'} $cotext $alerts{'role'}.\\n$alerts{'accr'}.");
+            section = "";
+        }
 SCRIPTTOP
     if ($mode ne 'upload') {
         $dc_setcourse_code .= (<<"ENDSCRIPT");
         var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
         var numcourse = getIndex(document.$formname.dccourse);
         if (numcourse == "-1") {
-            alert("$alerts{'thwa'}");
+            if (document.$formname.type == 'Community') {
+                alert("$alerts{'thwc'}");
+            } else {
+                alert("$alerts{'thwa'}");
+            }
             return;
         }
         else {
@@ -5236,19 +5380,22 @@ ENDSCRIPT
 
 sub sectioncheck_alerts {
     my %alerts = &Apache::lonlocal::texthash(
-                    curd => 'You must select a course in the current domain',
+                    curd => 'You must select a course or community in the current domain',
                     inea => 'In each course, each user may only have one student role at a time',
+                    inco => 'In each community, each user may only have one member role at a time', 
                     youh => 'You had selected',
                     sect => 'sections',
                     plsm => 'Please modify your selections so they include no more than one section',
                     mayn => 'may not be used as the name for a section, as it is a reserved word',
                     plsc => 'Please choose a different section name',
                     mayt => 'may not be used as the name for a section, as it is the name of a course group',
+                    mayc => 'may not be used as the name for a section, as it is the name of a community group',
                     secn => 'Section names and group names must be distinct',
                     secd => 'Section designations do not apply to ',
                     role => 'roles',
                     accr => 'role will be added with access to all sections',
-                    thwa => 'There was a problem with your course selection'
+                    thwa => 'There was a problem with your course selection',
+                    thwc => 'There was a problem with your community selection',
                  );
     return %alerts;
 }