Annotation of loncom/interface/lonuserutils.pm, revision 1.184.4.10.2.8

1.1       raeburn     1: # The LearningOnline Network with CAPA
                      2: # Utility functions for managing LON-CAPA user accounts
                      3: #
1.184.4.10.2.  (raeburn    4:): # $Id: lonuserutils.pm,v 1.184.4.10.2.7 2024/01/05 04:09:47 raeburn Exp $
1.1       raeburn     5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: #
                     28: ###############################################################
                     29: ###############################################################
                     30: 
                     31: package Apache::lonuserutils;
                     32: 
1.175     raeburn    33: =pod
                     34: 
                     35: =head1 NAME
                     36: 
                     37: Apache::lonuserutils.pm
                     38: 
                     39: =head1 SYNOPSIS
                     40: 
                     41:     Utilities for management of users and custom roles
                     42: 
                     43:     Provides subroutines called by loncreateuser.pm
                     44: 
                     45: =head1 OVERVIEW
                     46: 
                     47: =cut
                     48: 
1.1       raeburn    49: use strict;
                     50: use Apache::lonnet;
                     51: use Apache::loncommon();
                     52: use Apache::lonhtmlcommon;
                     53: use Apache::lonlocal;
1.8       raeburn    54: use Apache::longroup;
1.174     raeburn    55: use HTML::Entities;
1.8       raeburn    56: use LONCAPA qw(:DEFAULT :match);
1.1       raeburn    57: 
                     58: ###############################################################
                     59: ###############################################################
                     60: # Drop student from all sections of a course, except optional $csec
                     61: sub modifystudent {
1.52      raeburn    62:     my ($udom,$unam,$courseid,$csec,$desiredhost,$context)=@_;
1.1       raeburn    63:     # if $csec is undefined, drop the student from all the courses matching
                     64:     # this one.  If $csec is defined, drop them from all other sections of
                     65:     # this course and add them to section $csec
1.17      raeburn    66:     my ($cnum,$cdom) = &get_course_identity($courseid);
1.138     raeburn    67:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
1.1       raeburn    68:     my ($tmp) = keys(%roles);
                     69:     # Bail out if we were unable to get the students roles
                     70:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                     71:     # Go through the roles looking for enrollment in this course
                     72:     my $result = '';
                     73:     foreach my $course (keys(%roles)) {
                     74:         if ($course=~m{^/\Q$cdom\E/\Q$cnum\E(?:\/)*(?:\s+)*(\w+)*\_st$}) {
                     75:             # We are in this course
                     76:             my $section=$1;
                     77:             $section='' if ($course eq "/$cdom/$cnum".'_st');
                     78:             if (defined($csec) && $section eq $csec) {
                     79:                 $result .= 'ok:';
                     80:             } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
                     81:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
                     82:                 my $now=time;
                     83:                 # if this is an active role
                     84:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
                     85:                     my $reply=&Apache::lonnet::modifystudent
                     86:                         # dom  name  id mode pass     f     m     l     g
                     87:                         ($udom,$unam,'',  '',  '',undef,undef,undef,undef,
1.22      raeburn    88:                          $section,time,undef,undef,$desiredhost,'','manual',
1.52      raeburn    89:                          '',$courseid,'',$context);
1.1       raeburn    90:                     $result .= $reply.':';
                     91:                 }
                     92:             }
                     93:         }
                     94:     }
                     95:     if ($result eq '') {
1.37      raeburn    96:         $result = &mt('Unable to find section for this student');
1.1       raeburn    97:     } else {
                     98:         $result =~ s/(ok:)+/ok/g;
                     99:     }
                    100:     return $result;
                    101: }
                    102: 
                    103: sub modifyuserrole {
                    104:     my ($context,$setting,$changeauth,$cid,$udom,$uname,$uid,$umode,$upass,
                    105:         $first,$middle,$last,$gene,$sec,$forceid,$desiredhome,$email,$role,
1.184.4.10.2.  (raeburn  106:):         $end,$start,$checkid,$inststatus,$emptyok) = @_;
1.5       raeburn   107:     my ($scope,$userresult,$authresult,$roleresult,$idresult);
1.1       raeburn   108:     if ($setting eq 'course' || $context eq 'course') {
                    109:         $scope = '/'.$cid;
                    110:         $scope =~ s/\_/\//g;
1.103     raeburn   111:         if (($role ne 'cc') && ($role ne 'co') && ($sec ne '')) {
1.1       raeburn   112:             $scope .='/'.$sec;
                    113:         }
1.5       raeburn   114:     } elsif ($context eq 'domain') {
1.1       raeburn   115:         $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn   116:     } elsif ($context eq 'author') {
1.184.4.10.2.  (raeburn  117:):         if ($env{'request.role'} =~ m{^ca\.(/$match_domain/$match_username)$}) {
                    118:):             $scope = $1;
                    119:):         } else {
                    120:):             $scope =  '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                    121:):         }
1.1       raeburn   122:     }
                    123:     if ($context eq 'domain') {
                    124:         my $uhome = &Apache::lonnet::homeserver($uname,$udom);
                    125:         if ($uhome ne 'no_host') {
1.5       raeburn   126:             if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
1.1       raeburn   127:                 if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
                    128:                     ($umode eq 'localauth')) {
                    129:                     $authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
                    130:                 }
                    131:             }
1.5       raeburn   132:             if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
                    133:                 ($env{'form.recurseid'}) && ($checkid)) {
                    134:                 my %userupdate = (
                    135:                                   lastname   => $last,
                    136:                                   middlename => $middle,
                    137:                                   firstname  => $first,
                    138:                                   generation => $gene,
                    139:                                   id         => $uid,
                    140:                                  );
1.184.4.10.2.  (raeburn  141:): 
                    142:):                 # When "Update ID in user's course(s)" and "Force change of existing ID"
                    143:):                 # checkboxes both checked, prevent replacement of name information
                    144:):                 # in classlist.db file(s) for the user's course(s) with blank(s),
                    145:):                 # in the case where the uploaded csv file was without column(s) for
                    146:):                 # the particular field. Fields are: First Name, Middle Names/Initials,
                    147:):                 # Last Name (or the composite: Last Name, First Names), and Generation.
                    148:): 
                    149:):                 my %emptyallowed;
                    150:):                 if ((ref($emptyok) eq 'HASH') && (keys(%{$emptyok}) > 0)) {
                    151:):                     %emptyallowed = %{$emptyok};
                    152:):                 }
                    153:):                 foreach my $field (keys(%userupdate)) {
                    154:):                     if ($userupdate{$field} eq '') {
                    155:):                         unless ($emptyallowed{$field}) {
                    156:):                             delete($userupdate{$field});
                    157:):                         }
                    158:):                     }
                    159:):                 }
1.5       raeburn   160:                 $idresult = &propagate_id_change($uname,$udom,\%userupdate);
                    161:             }
1.1       raeburn   162:         }
                    163:     }
                    164:     $userresult =
                    165:         &Apache::lonnet::modifyuser($udom,$uname,$uid,$umode,$upass,$first,
                    166:                                     $middle,$last,$gene,$forceid,$desiredhome,
1.84      raeburn   167:                                     $email,$inststatus);
1.1       raeburn   168:     if ($userresult eq 'ok') {
1.5       raeburn   169:         if ($role ne '') {
1.22      raeburn   170:             $role =~ s/_/\//g;
1.1       raeburn   171:             $roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
1.52      raeburn   172:                                                       $role,$end,$start,'',
                    173:                                                       '',$context);
1.1       raeburn   174:         }
                    175:     }
1.5       raeburn   176:     return ($userresult,$authresult,$roleresult,$idresult);
1.1       raeburn   177: }
                    178: 
1.5       raeburn   179: sub propagate_id_change {
                    180:     my ($uname,$udom,$user) = @_;
1.12      raeburn   181:     my (@types,@roles);
1.5       raeburn   182:     @types = ('active','future');
                    183:     @roles = ('st');
                    184:     my $idresult;
                    185:     my %roleshash = &Apache::lonnet::get_my_roles($uname,
1.12      raeburn   186:                         $udom,'userroles',\@types,\@roles);
                    187:     my %args = (
                    188:                 one_time => 1,
                    189:                );
1.5       raeburn   190:     foreach my $item (keys(%roleshash)) {
1.22      raeburn   191:         my ($cnum,$cdom,$role) = split(/:/,$item,-1);
1.5       raeburn   192:         my ($start,$end) = split(/:/,$roleshash{$item});
                    193:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.12      raeburn   194:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,$user);
                    195:             my %coursehash = 
                    196:                 &Apache::lonnet::coursedescription($cdom.'_'.$cnum,\%args);
                    197:             my $cdesc = $coursehash{'description'};
                    198:             if ($cdesc eq '') { 
                    199:                 $cdesc = $cdom.'_'.$cnum;
                    200:             }
1.5       raeburn   201:             if ($result eq 'ok') {
1.12      raeburn   202:                 $idresult .= &mt('Classlist update for "[_1]" in "[_2]".',$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   203:             } else {
1.12      raeburn   204:                 $idresult .= &mt('Error: "[_1]" during classlist update for "[_2]" in "[_3]".',$result,$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   205:             }
                    206:         }
                    207:     }
                    208:     return $idresult;
                    209: }
                    210: 
                    211: sub update_classlist {
1.63      raeburn   212:     my ($cdom,$cnum,$udom,$uname,$user,$newend) = @_;
1.6       albertel  213:     my ($uid,$classlistentry);
1.5       raeburn   214:     my $fullname =
                    215:         &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
                    216:                                      $user->{'lastname'},$user->{'generation'},
                    217:                                      'lastname');
                    218:     my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
                    219:                                          $cdom,$cnum);
                    220:     my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
                    221:     my $ididx=&Apache::loncoursedata::CL_ID() - 2;
                    222:     my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
1.63      raeburn   223:     my $endidx = &Apache::loncoursedata::CL_END() - 2;
                    224:     my $startidx = &Apache::loncoursedata::CL_START() - 2;
1.5       raeburn   225:     for (my $i=0; $i<@classinfo; $i++) {
1.63      raeburn   226:         if ($i == $endidx) {
                    227:             if ($newend ne '') {
                    228:                 $classlistentry .= $newend.':';
                    229:             } else {
                    230:                 $classlistentry .= $classinfo[$i].':';
                    231:             }
                    232:         } elsif ($i == $startidx) {
                    233:             if ($newend ne '') {
                    234:                 if ($classinfo[$i] > $newend) {
                    235:                     $classlistentry .= $newend.':';
                    236:                 } else {
                    237:                     $classlistentry .= $classinfo[$i].':';
                    238:                 }
                    239:             } else {
                    240:                 $classlistentry .= $classinfo[$i].':';
                    241:             }
                    242:         } elsif ($i == $ididx) {
1.5       raeburn   243:             if (defined($user->{'id'})) {
                    244:                 $classlistentry .= $user->{'id'}.':';
                    245:             } else {
                    246:                 $classlistentry .= $classinfo[$i].':';
                    247:             }
                    248:         } elsif ($i == $nameidx) {
1.63      raeburn   249:             if (defined($user->{'lastname'})) {
                    250:                 $classlistentry .= $fullname.':';
                    251:             } else {
                    252:                 $classlistentry .= $classinfo[$i].':';
                    253:             }
1.5       raeburn   254:         } else {
                    255:             $classlistentry .= $classinfo[$i].':';
                    256:         }
                    257:     }
                    258:     $classlistentry =~ s/:$//;
                    259:     my $reply=&Apache::lonnet::cput('classlist',
                    260:                                     {"$uname:$udom" => $classlistentry},
                    261:                                     $cdom,$cnum);
                    262:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                    263:         return 'ok';
                    264:     } else {
                    265:         return 'error: '.$reply;
                    266:     }
                    267: }
                    268: 
                    269: 
1.1       raeburn   270: ###############################################################
                    271: ###############################################################
1.2       raeburn   272: # build a role type and role selection form
                    273: sub domain_roles_select {
                    274:     # Set up the role type and role selection boxes when in 
                    275:     # domain context   
                    276:     #
                    277:     # Role types
1.101     raeburn   278:     my @roletypes = ('domain','author','course','community');
1.2       raeburn   279:     my %lt = &role_type_names();
1.149     raeburn   280:     my $onchangefirst = "updateCols('showrole')";
                    281:     my $onchangesecond = "updateCols('showrole')";
1.1       raeburn   282:     #
                    283:     # build up the menu information to be passed to
                    284:     # &Apache::loncommon::linked_select_forms
                    285:     my %select_menus;
1.2       raeburn   286:     if ($env{'form.roletype'} eq '') {
                    287:         $env{'form.roletype'} = 'domain';
                    288:     }
                    289:     foreach my $roletype (@roletypes) {
1.1       raeburn   290:         # set up the text for this domain
1.2       raeburn   291:         $select_menus{$roletype}->{'text'}= $lt{$roletype};
1.102     raeburn   292:         my $crstype;
                    293:         if ($roletype eq 'community') {
                    294:             $crstype = 'Community';
                    295:         }
1.1       raeburn   296:         # we want a choice of 'default' as the default in the second menu
1.2       raeburn   297:         if ($env{'form.roletype'} ne '') {
                    298:             $select_menus{$roletype}->{'default'} = $env{'form.showrole'};
                    299:         } else { 
                    300:             $select_menus{$roletype}->{'default'} = 'Any';
                    301:         }
1.1       raeburn   302:         # Now build up the other items in the second menu
1.2       raeburn   303:         my @roles;
                    304:         if ($roletype eq 'domain') {
                    305:             @roles = &domain_roles();
1.13      raeburn   306:         } elsif ($roletype eq 'author') {
1.2       raeburn   307:             @roles = &construction_space_roles();
                    308:         } else {
1.17      raeburn   309:             my $custom = 1;
1.101     raeburn   310:             @roles = &course_roles('domain',undef,$custom,$roletype);
1.1       raeburn   311:         }
1.2       raeburn   312:         my $order = ['Any',@roles];
                    313:         $select_menus{$roletype}->{'order'} = $order; 
                    314:         foreach my $role (@roles) {
1.5       raeburn   315:             if ($role eq 'cr') {
                    316:                 $select_menus{$roletype}->{'select2'}->{$role} =
                    317:                               &mt('Custom role');
                    318:             } else {
                    319:                 $select_menus{$roletype}->{'select2'}->{$role} = 
1.102     raeburn   320:                               &Apache::lonnet::plaintext($role,$crstype);
1.5       raeburn   321:             }
1.2       raeburn   322:         }
                    323:         $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
1.1       raeburn   324:     }
1.2       raeburn   325:     my $result = &Apache::loncommon::linked_select_forms
                    326:         ('studentform',('&nbsp;'x3).&mt('Role: '),$env{'form.roletype'},
1.101     raeburn   327:          'roletype','showrole',\%select_menus,
1.149     raeburn   328:          ['domain','author','course','community'],$onchangefirst,
                    329:          $onchangesecond);
1.1       raeburn   330:     return $result;
                    331: }
                    332: 
                    333: ###############################################################
                    334: ###############################################################
                    335: sub hidden_input {
                    336:     my ($name,$value) = @_;
                    337:     return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
                    338: }
                    339: 
                    340: sub print_upload_manager_header {
1.123     raeburn   341:     my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission,$crstype,
                    342:         $can_assign)=@_;
1.1       raeburn   343:     my $javascript;
                    344:     #
                    345:     if (! exists($env{'form.upfile_associate'})) {
                    346:         $env{'form.upfile_associate'} = 'forward';
                    347:     }
                    348:     if ($env{'form.associate'} eq 'Reverse Association') {
                    349:         if ( $env{'form.upfile_associate'} ne 'reverse' ) {
                    350:             $env{'form.upfile_associate'} = 'reverse';
                    351:         } else {
                    352:             $env{'form.upfile_associate'} = 'forward';
                    353:         }
                    354:     }
                    355:     if ($env{'form.upfile_associate'} eq 'reverse') {
1.123     raeburn   356:         $javascript=&upload_manager_javascript_reverse_associate($can_assign);
1.1       raeburn   357:     } else {
1.123     raeburn   358:         $javascript=&upload_manager_javascript_forward_associate($can_assign);
1.1       raeburn   359:     }
                    360:     #
                    361:     # Deal with restored settings
                    362:     my $password_choice = '';
                    363:     if (exists($env{'form.ipwd_choice'}) &&
                    364:         $env{'form.ipwd_choice'} ne '') {
                    365:         # If a column was specified for password, assume it is for an
                    366:         # internal password.  This is a bug waiting to be filed (could be
                    367:         # local or krb auth instead of internal) but I do not have the
                    368:         # time to mess around with this now.
                    369:         $password_choice = 'int';
                    370:     }
                    371:     #
1.22      raeburn   372:     my $groupslist;
                    373:     if ($context eq 'course') {
                    374:         $groupslist = &get_groupslist();
                    375:     }
1.1       raeburn   376:     my $javascript_validations =
1.22      raeburn   377:         &javascript_validations('upload',$krbdefdom,$password_choice,undef,
                    378:                                 $env{'request.role.domain'},$context,
1.103     raeburn   379:                                 $groupslist,$crstype);
1.91      bisitz    380:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
1.147     bisitz    381:     $r->print(
                    382:         '<h3>'.&mt('Identify fields in uploaded list')."</h3>\n".
                    383:         '<p class="LC_info">'.
                    384:         &mt('Total number of records found in file: [_1]'
                    385:            ,'<b>'.$distotal.'</b>').
                    386:         "</p>\n"
                    387:     );
                    388:     if ($distotal == 0) {
                    389:         $r->print('<p class="LC_warning">'.&mt('None found').'</p>');
                    390:     }
                    391:     $r->print(
                    392:         '<p>'.
                    393:         &mt('Enter as many fields as you can.').'<br />'.
                    394:         &mt('The system will inform you and bring you back to this page,[_1]if the data selected are insufficient to add users.','<br />').
                    395:         "</p>\n"
                    396:     );
1.1       raeburn   397:     $r->print(&hidden_input('action','upload').
                    398:               &hidden_input('state','got_file').
                    399:               &hidden_input('associate','').
                    400:               &hidden_input('datatoken',$datatoken).
                    401:               &hidden_input('fileupload',$env{'form.fileupload'}).
                    402:               &hidden_input('upfiletype',$env{'form.upfiletype'}).
                    403:               &hidden_input('upfile_associate',$env{'form.upfile_associate'}));
1.147     bisitz    404:     $r->print(
                    405:         '<div class="LC_left_float">'.
                    406:         '<fieldset><legend>'.&mt('Functions').'</legend>'.
                    407:         '<label><input type="checkbox" name="noFirstLine"'.$checked.' />'.
                    408:               &mt('Ignore First Line').'</label>'.
                    409:         ' <input type="button" value="'.&mt('Reverse Association').'" '.
1.59      bisitz    410:               'name="Reverse Association" '.
1.147     bisitz    411:               'onclick="javascript:this.form.associate.value=\'Reverse Association\';submit(this.form);" />'.
                    412:         '</fieldset></div><br clear="all" />'
                    413:     );
                    414:     $r->print(
                    415:         '<script type="text/javascript" language="Javascript">'."\n".
                    416:         '// <![CDATA['."\n".
                    417:         $javascript."\n".$javascript_validations."\n".
                    418:         '// ]]>'."\n".
                    419:         '</script>'
                    420:     );
1.1       raeburn   421: }
                    422: 
                    423: ###############################################################
                    424: ###############################################################
                    425: sub javascript_validations {
1.22      raeburn   426:     my ($mode,$krbdefdom,$curr_authtype,$curr_authfield,$domain,
1.103     raeburn   427:         $context,$groupslist,$crstype)=@_;
1.22      raeburn   428:     my %param = (
                    429:                   kerb_def_dom => $krbdefdom,
                    430:                   curr_authtype => $curr_authtype,
                    431:                 );
1.37      raeburn   432:     if ($mode eq 'upload') {
1.22      raeburn   433:         $param{'formname'} = 'studentform';
1.1       raeburn   434:     } elsif ($mode eq 'createcourse') {
1.22      raeburn   435:         $param{'formname'} = 'ccrs';
1.1       raeburn   436:     } elsif ($mode eq 'modifycourse') {
1.22      raeburn   437:         $param{'formname'} = 'cmod';
                    438:         $param{'mode'} = 'modifycourse',
                    439:         $param{'curr_autharg'} = $curr_authfield;
                    440:     }
                    441: 
1.150     raeburn   442:     my $showcredits;
                    443:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1.160     raeburn   444:     if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'} || $domdefaults{'textbookcredits'}) {
1.150     raeburn   445:         $showcredits = 1;
                    446:     }
                    447: 
1.22      raeburn   448:     my ($setsection_call,$setsections_js);
                    449:     my $finish = "  vf.submit();\n";
                    450:     if ($mode eq 'upload') {
                    451:         if (($context eq 'course') || ($context eq 'domain')) {
                    452:             if ($context eq 'course') {
                    453:                 if ($env{'request.course.sec'} eq '') {
1.109     raeburn   454:                     $setsection_call = 'setSections(document.'.$param{'formname'}.",'$crstype'".');';
1.22      raeburn   455:                     $setsections_js =
                    456:                         &setsections_javascript($param{'formname'},$groupslist,
1.150     raeburn   457:                                                 $mode,'',$crstype,$showcredits);
1.22      raeburn   458:                 } else {
                    459:                     $setsection_call = "'ok'";
                    460:                 }
                    461:             } elsif ($context eq 'domain') {
                    462:                 $setsection_call = 'setCourse()';
1.150     raeburn   463:                 $setsections_js = &dc_setcourse_js($param{'formname'},$mode,
1.184.4.4  raeburn   464:                                                    $context,$showcredits,$domain);
1.22      raeburn   465:             }
                    466:             $finish = "  var checkSec = $setsection_call\n".
                    467:                       "  if (checkSec == 'ok') {\n".
                    468:                       "      vf.submit();\n".
                    469:                       "   }\n";
                    470:         }
1.1       raeburn   471:     }
1.22      raeburn   472:     my $authheader = &Apache::loncommon::authform_header(%param);
1.1       raeburn   473: 
                    474:     my %alert = &Apache::lonlocal::texthash
                    475:         (username => 'You need to specify the username field.',
                    476:          authen   => 'You must choose an authentication type.',
                    477:          krb      => 'You need to specify the Kerberos domain.',
                    478:          ipass    => 'You need to specify the initial password.',
                    479:          name     => 'The optional name field was not specified.',
1.93      bisitz    480:          snum     => 'The optional student/employee ID field was not specified.',
1.1       raeburn   481:          section  => 'The optional section field was not specified.',
1.75      schafran  482:          email    => 'The optional e-mail address field was not specified.',
1.1       raeburn   483:          role     => 'The optional role field was not specified.',
1.57      raeburn   484:          domain   => 'The optional domain field was not specified.',
1.1       raeburn   485:          continue => 'Continue adding users?',
                    486:          );
1.150     raeburn   487:     if ($showcredits) {
                    488:         $alert{'credits'} = &mt('The optional credits field was not specified');
                    489:     }
1.84      raeburn   490:     if (($mode eq 'upload') && ($context eq 'domain')) {
                    491:         $alert{'inststatus'} = &mt('The optional affiliation field was not specified'); 
                    492:     }
1.170     damieng   493:     &js_escape(\%alert);
1.37      raeburn   494:     my $function_name = <<"END";
1.22      raeburn   495: $setsections_js
                    496: 
1.150     raeburn   497: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus,foundcredits) {
1.1       raeburn   498: END
                    499:     my ($authnum,%can_assign) =  &Apache::loncommon::get_assignable_auth($domain);
                    500:     my $auth_checks;
                    501:     if ($mode eq 'createcourse') {
                    502:         $auth_checks .= (<<END);
                    503:     if (vf.autoadds[0].checked == true) {
                    504:         if (current.radiovalue == null || current.radiovalue == 'nochange') {
                    505:             alert('$alert{'authen'}');
                    506:             return;
                    507:         }
                    508:     }
                    509: END
                    510:     } else {
                    511:         $auth_checks .= (<<END);
                    512:     var foundatype=0;
                    513:     if (founduname==0) {
                    514:         alert('$alert{'username'}');
                    515:         return;
                    516:     }
                    517: 
                    518: END
                    519:         if ($authnum > 1) {
                    520:             $auth_checks .= (<<END);
                    521:     if (current.radiovalue == null || current.radiovalue == '' || current.radiovalue == 'nochange') {
                    522:         // They did not check any of the login radiobuttons.
                    523:         alert('$alert{'authen'}');
                    524:         return;
                    525:     }
                    526: END
                    527:         }
                    528:     }
                    529:     if ($mode eq 'createcourse') {
                    530:         $auth_checks .= "
                    531:     if ( (vf.autoadds[0].checked == true) &&
                    532:          (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') ) {
                    533: ";
                    534:     } elsif ($mode eq 'modifycourse') {
                    535:         $auth_checks .= "
1.184.4.10.2.  (raeburn  536:):     if ((current.argfield !== null) && (current.argfield !== undefined) && (current.argfield !== '') && (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '')) {
1.1       raeburn   537: ";
                    538:     }
                    539:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    540:         $auth_checks .= (<<END);
                    541:         var alertmsg = '';
                    542:         switch (current.radiovalue) {
                    543:             case 'krb':
                    544:                 alertmsg = '$alert{'krb'}';
                    545:                 break;
                    546:             default:
                    547:                 alertmsg = '';
                    548:         }
                    549:         if (alertmsg != '') {
                    550:             alert(alertmsg);
                    551:             return;
                    552:         }
                    553:     }
1.150     raeburn   554: /* regexp here to check for non \d \. in credits */
1.1       raeburn   555: END
                    556:     } else {
1.184.4.4  raeburn   557:         my ($numrules,$intargjs) =
1.184.4.10.2.  (raeburn  558:):             &Apache::loncommon::passwd_validation_js('vf.elements[current.argfield].value',$domain);
1.1       raeburn   559:         $auth_checks .= (<<END);
                    560:     foundatype=1;
                    561:     if (current.argfield == null || current.argfield == '') {
1.184.4.4  raeburn   562:         // The login radiobutton checked does not have an associated textbox
                    563:     } else if (vf.elements[current.argfield].value == '') {
1.1       raeburn   564:         var alertmsg = '';
1.38      raeburn   565:         switch (current.radiovalue) {
1.1       raeburn   566:             case 'krb':
                    567:                 alertmsg = '$alert{'krb'}';
                    568:                 break;
1.184.4.4  raeburn   569:             case 'int':
1.1       raeburn   570:                 alertmsg = '$alert{'ipass'}';
                    571:                 break;
                    572:             case 'fsys':
1.184.4.4  raeburn   573:                 alertmsg = '$alert{'ipass'}';
1.1       raeburn   574:                 break;
1.184.4.10  raeburn   575:             case 'loc':
                    576:                 alertmsg = '';
                    577:                 break;
1.1       raeburn   578:             default:
                    579:                 alertmsg = '';
                    580:         }
                    581:         if (alertmsg != '') {
                    582:             alert(alertmsg);
                    583:             return;
                    584:         }
1.184.4.4  raeburn   585:     } else if (current.radiovalue == 'int') {
                    586:         if ($numrules > 0) {
                    587: $intargjs
                    588:         }
1.1       raeburn   589:     }
                    590: END
                    591:     }
                    592:     my $section_checks;
                    593:     my $optional_checks = '';
                    594:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    595:         $optional_checks = (<<END);
                    596:     vf.submit();
                    597: }
                    598: END
                    599:     } else {
                    600:         $section_checks = &section_check_js();
                    601:         $optional_checks = (<<END);
                    602:     var message='';
                    603:     if (foundname==0) {
                    604:         message='$alert{'name'}';
                    605:     }
                    606:     if (foundid==0) {
                    607:         if (message!='') {
                    608:             message+='\\n';
                    609:         }
                    610:         message+='$alert{'snum'}';
                    611:     }
                    612:     if (foundsec==0) {
                    613:         if (message!='') {
                    614:             message+='\\n';
                    615:         }
1.130     raeburn   616:         message+='$alert{'section'}';
1.1       raeburn   617:     }
                    618:     if (foundemail==0) {
                    619:         if (message!='') {
                    620:             message+='\\n';
                    621:         }
                    622:         message+='$alert{'email'}';
                    623:     }
1.57      raeburn   624:     if (foundrole==0) {
                    625:         if (message!='') {
                    626:             message+='\\n';
                    627:         }
                    628:         message+='$alert{'role'}';
                    629:     }
                    630:     if (founddomain==0) {
                    631:         if (message!='') {
                    632:             message+='\\n';
                    633:         }
                    634:         message+='$alert{'domain'}';
                    635:     }
1.84      raeburn   636: END
1.150     raeburn   637:         if ($showcredits) {
                    638:             $optional_checks .= <<END;
                    639:     if (foundcredits==0) {
                    640:         if (message!='') {
                    641:             message+='\\n';
                    642:         }
                    643:         message+='$alert{'credits'}';
                    644:     }
                    645: END
                    646:         }
1.84      raeburn   647:         if (($mode eq 'upload') && ($context eq 'domain')) {
                    648:             $optional_checks .= (<<END);
                    649: 
                    650:     if (foundinststatus==0) {
                    651:         if (message!='') {
                    652:             message+='\\n';
                    653:         }
                    654:         message+='$alert{'inststatus'}';
                    655:     }
                    656: END
                    657:         }
                    658:         $optional_checks .= (<<END);
                    659: 
1.1       raeburn   660:     if (message!='') {
                    661:         message+= '\\n$alert{'continue'}';
                    662:         if (confirm(message)) {
                    663:             vf.state.value='enrolling';
1.22      raeburn   664:             $finish
1.1       raeburn   665:         }
                    666:     } else {
                    667:         vf.state.value='enrolling';
1.22      raeburn   668:         $finish
1.1       raeburn   669:     }
                    670: }
                    671: END
                    672:     }
1.37      raeburn   673:     my $result = $function_name.$auth_checks.$optional_checks."\n".
                    674:                  $section_checks.$authheader;
1.1       raeburn   675:     return $result;
                    676: }
1.184.4.4  raeburn   677: 
1.1       raeburn   678: ###############################################################
                    679: ###############################################################
                    680: sub upload_manager_javascript_forward_associate {
1.123     raeburn   681:     my ($can_assign) = @_;
1.132     raeburn   682:     my ($auth_update,$numbuttons,$argreset);
1.123     raeburn   683:     if (ref($can_assign) eq 'HASH') {
1.132     raeburn   684:         if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
                    685:             $argreset .= "      vf.krbarg.value='';\n";
                    686:             $numbuttons ++ ;
                    687:         }
                    688:         if ($can_assign->{'int'}) {
                    689:             $argreset .= "      vf.intarg.value='';\n";
                    690:             $numbuttons ++;
                    691:         }
                    692:         if ($can_assign->{'loc'}) {
                    693:             $argreset .= "      vf.locarg.value='';\n";
                    694:             $numbuttons ++;
                    695:         }
                    696:         if (!$can_assign->{'int'}) {
1.170     damieng   697:             my $warning = &mt('You may not specify an initial password for each user, as this is only available when new users use LON-CAPA internal authentication.')."\n".
1.132     raeburn   698:                           &mt('Your current role does not have rights to create users with that authentication type.');
1.170     damieng   699:             &js_escape(\$warning);
1.132     raeburn   700:             $auth_update = <<"END";
                    701:    // Currently the initial password field is only supported for internal auth
                    702:    // (see bug 6368).
                    703:    if (nw==9) {
                    704:        eval('vf.f'+tf+'.selectedIndex=0;')
                    705:        alert('$warning');
                    706:    }
                    707: END
                    708:         } elsif ($numbuttons > 1) {
1.123     raeburn   709:             $auth_update = <<"END";
                    710:    // If we set the password, make the password form below correspond to
                    711:    // the new value.
                    712:    if (nw==9) {
                    713:       changed_radio('int',document.studentform);
                    714:       set_auth_radio_buttons('int',document.studentform);
1.132     raeburn   715: $argreset
                    716:    }
                    717: 
1.123     raeburn   718: END
                    719:         }
                    720:     }
                    721: 
1.1       raeburn   722:     return(<<ENDPICK);
                    723: function verify(vf,sec_caller) {
                    724:     var founduname=0;
                    725:     var foundpwd=0;
                    726:     var foundname=0;
                    727:     var foundid=0;
                    728:     var foundsec=0;
                    729:     var foundemail=0;
                    730:     var foundrole=0;
1.57      raeburn   731:     var founddomain=0;
1.84      raeburn   732:     var foundinststatus=0;
1.150     raeburn   733:     var foundcredits=0;
1.1       raeburn   734:     var tw;
                    735:     for (i=0;i<=vf.nfields.value;i++) {
                    736:         tw=eval('vf.f'+i+'.selectedIndex');
                    737:         if (tw==1) { founduname=1; }
                    738:         if ((tw>=2) && (tw<=6)) { foundname=1; }
                    739:         if (tw==7) { foundid=1; }
                    740:         if (tw==8) { foundsec=1; }
                    741:         if (tw==9) { foundpwd=1; }
                    742:         if (tw==10) { foundemail=1; }
                    743:         if (tw==11) { foundrole=1; }
1.57      raeburn   744:         if (tw==12) { founddomain=1; }
1.84      raeburn   745:         if (tw==13) { foundinststatus=1; }
1.150     raeburn   746:         if (tw==14) { foundcredits=1; }
1.1       raeburn   747:     }
1.150     raeburn   748:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus,foundcredits);
1.1       raeburn   749: }
                    750: 
                    751: //
                    752: // vf = this.form
                    753: // tf = column number
                    754: //
                    755: // values of nw
                    756: //
                    757: // 0 = none
                    758: // 1 = username
                    759: // 2 = names (lastname, firstnames)
                    760: // 3 = fname (firstname)
                    761: // 4 = mname (middlename)
                    762: // 5 = lname (lastname)
                    763: // 6 = gen   (generation)
                    764: // 7 = id
                    765: // 8 = section
                    766: // 9 = ipwd  (password)
                    767: // 10 = email address
                    768: // 11 = role
1.57      raeburn   769: // 12 = domain
1.84      raeburn   770: // 13 = inststatus
1.150     raeburn   771: // 14 = foundcredits 
1.1       raeburn   772: 
                    773: function flip(vf,tf) {
                    774:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    775:    var i;
                    776:    // make sure no other columns are labeled the same as this one
                    777:    for (i=0;i<=vf.nfields.value;i++) {
                    778:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    779:           eval('vf.f'+i+'.selectedIndex=0;')
                    780:       }
                    781:    }
                    782:    // If we set this to 'lastname, firstnames', clear out all the ones
                    783:    // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
                    784:    if (nw==2) {
                    785:       for (i=0;i<=vf.nfields.value;i++) {
                    786:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
                    787:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
                    788:              eval('vf.f'+i+'.selectedIndex=0;')
                    789:          }
                    790:       }
                    791:    }
                    792:    // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
                    793:    // clear out any that are set to 'lastname, firstnames' (2)
                    794:    if ((nw>=3) && (nw<=6)) {
                    795:       for (i=0;i<=vf.nfields.value;i++) {
                    796:          if (eval('vf.f'+i+'.selectedIndex')==2) {
                    797:              eval('vf.f'+i+'.selectedIndex=0;')
                    798:          }
                    799:       }
                    800:    }
1.123     raeburn   801:    $auth_update
1.1       raeburn   802: }
                    803: 
                    804: function clearpwd(vf) {
                    805:     var i;
                    806:     for (i=0;i<=vf.nfields.value;i++) {
                    807:         if (eval('vf.f'+i+'.selectedIndex')==9) {
                    808:             eval('vf.f'+i+'.selectedIndex=0;')
                    809:         }
                    810:     }
                    811: }
                    812: 
                    813: ENDPICK
                    814: }
                    815: 
                    816: ###############################################################
                    817: ###############################################################
                    818: sub upload_manager_javascript_reverse_associate {
1.123     raeburn   819:     my ($can_assign) = @_;
1.132     raeburn   820:     my ($auth_update,$numbuttons,$argreset);
1.123     raeburn   821:     if (ref($can_assign) eq 'HASH') {
1.132     raeburn   822:         if ($can_assign->{'krb4'} || $can_assign->{'krb5'}) {
                    823:             $argreset .= "      vf.krbarg.value='';\n";
                    824:             $numbuttons ++ ;
                    825:         }
                    826:         if ($can_assign->{'int'}) {
                    827:             $argreset .= "      vf.intarg.value='';\n";
                    828:             $numbuttons ++;
                    829:         }
                    830:         if ($can_assign->{'loc'}) {
                    831:             $argreset .= "      vf.locarg.value='';\n";
                    832:             $numbuttons ++;
                    833:         }
                    834:         if (!$can_assign->{'int'}) {
                    835:             my $warning = &mt('You may not specify an initial password, as this is only available when new users use LON-CAPA internal authentication.\n').
                    836:                           &mt('Your current role does not have rights to create users with that authentication type.');
1.170     damieng   837:             &js_escape(\$warning);
1.132     raeburn   838:             $auth_update = <<"END";
                    839:    // Currently the initial password field is only supported for internal auth
                    840:    // (see bug 6368).
                    841:    if (tf==8 && nw!=0) {
                    842:        eval('vf.f'+tf+'.selectedIndex=0;')
                    843:        alert('$warning');
                    844:    }
                    845: END
                    846:         } elsif ($numbuttons > 1) {
1.123     raeburn   847:             $auth_update = <<"END";
                    848:    // initial password specified, pick internal authentication
                    849:    if (tf==8 && nw!=0) {
                    850:       changed_radio('int',document.studentform);
                    851:       set_auth_radio_buttons('int',document.studentform);
1.132     raeburn   852: $argreset
                    853:    }
                    854: 
1.123     raeburn   855: END
                    856:         }
                    857:     }
1.132     raeburn   858: 
1.1       raeburn   859:     return(<<ENDPICK);
                    860: function verify(vf,sec_caller) {
                    861:     var founduname=0;
                    862:     var foundpwd=0;
                    863:     var foundname=0;
                    864:     var foundid=0;
                    865:     var foundsec=0;
1.131     raeburn   866:     var foundemail=0;
1.1       raeburn   867:     var foundrole=0;
1.57      raeburn   868:     var founddomain=0;
1.84      raeburn   869:     var foundinststatus=0;
1.150     raeburn   870:     var foundcredits=0;
1.1       raeburn   871:     var tw;
                    872:     for (i=0;i<=vf.nfields.value;i++) {
                    873:         tw=eval('vf.f'+i+'.selectedIndex');
                    874:         if (i==0 && tw!=0) { founduname=1; }
                    875:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
                    876:         if (i==6 && tw!=0) { foundid=1; }
                    877:         if (i==7 && tw!=0) { foundsec=1; }
                    878:         if (i==8 && tw!=0) { foundpwd=1; }
1.130     raeburn   879:         if (i==9 && tw!=0) { foundemail=1; }
                    880:         if (i==10 && tw!=0) { foundrole=1; }
                    881:         if (i==11 && tw!=0) { founddomain=1; }
                    882:         if (i==12 && tw!=0) { foundinstatus=1; }
1.150     raeburn   883:         if (i==13 && tw!=0) { foundcredits=1; }
1.1       raeburn   884:     }
1.150     raeburn   885:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole,founddomain,foundinststatus,foundcredits);
1.1       raeburn   886: }
                    887: 
                    888: function flip(vf,tf) {
                    889:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    890:    var i;
                    891:    // picked the all one name field, reset the other name ones to blank
                    892:    if (tf==1 && nw!=0) {
                    893:       for (i=2;i<=5;i++) {
                    894:          eval('vf.f'+i+'.selectedIndex=0;')
                    895:       }
                    896:    }
                    897:    //picked one of the piecewise name fields, reset the all in
                    898:    //one field to blank
                    899:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
                    900:       eval('vf.f1.selectedIndex=0;')
                    901:    }
1.123     raeburn   902:    $auth_update
1.1       raeburn   903: }
                    904: 
                    905: function clearpwd(vf) {
                    906:     var i;
                    907:     if (eval('vf.f8.selectedIndex')!=0) {
                    908:         eval('vf.f8.selectedIndex=0;')
                    909:     }
                    910: }
                    911: ENDPICK
                    912: }
                    913: 
                    914: ###############################################################
                    915: ###############################################################
                    916: sub print_upload_manager_footer {
1.150     raeburn   917:     my ($r,$i,$keyfields,$defdom,$today,$halfyear,$context,$permission,$crstype,
                    918:         $showcredits) = @_;
1.22      raeburn   919:     my $form = 'document.studentform';
                    920:     my $formname = 'studentform';
1.1       raeburn   921:     my ($krbdef,$krbdefdom) =
                    922:         &Apache::loncommon::get_kerberos_defaults($defdom);
1.22      raeburn   923:     my %param = ( formname => $form,
1.1       raeburn   924:                   kerb_def_dom => $krbdefdom,
                    925:                   kerb_def_auth => $krbdef
                    926:                   );
                    927:     if (exists($env{'form.ipwd_choice'}) &&
                    928:         defined($env{'form.ipwd_choice'}) &&
                    929:         $env{'form.ipwd_choice'} ne '') {
                    930:         $param{'curr_authtype'} = 'int';
                    931:     }
                    932:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    933:     my $intform = &Apache::loncommon::authform_internal(%param);
                    934:     my $locform = &Apache::loncommon::authform_local(%param);
1.22      raeburn   935:     my $date_table = &date_setting_table(undef,undef,$context,undef,
1.101     raeburn   936:                                          $formname,$permission,$crstype);
1.95      bisitz    937: 
1.1       raeburn   938:     my $Str = "\n".'<div class="LC_left_float">';
                    939:     $Str .= &hidden_input('nfields',$i);
                    940:     $Str .= &hidden_input('keyfields',$keyfields);
1.95      bisitz    941: 
                    942:     $Str .= '<h3>'.&mt('Options').'</h3>'
                    943:            .&Apache::lonhtmlcommon::start_pick_box();
                    944: 
                    945:     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Login Type'));
1.1       raeburn   946:     if ($context eq 'domain') {
1.95      bisitz    947:         $Str .= '<p>'
                    948:                .&mt('Change authentication for existing users in domain "[_1]" to these settings?'
                    949:                    ,$defdom)
                    950:                .'&nbsp;<span class="LC_nobreak"><label>'
                    951:                .'<input type="radio" name="changeauth" value="No" checked="checked" />'
                    952:                .&mt('No').'</label>'
                    953:                .'&nbsp;&nbsp;<label>'
                    954:                .'<input type="radio" name="changeauth" value="Yes" />'
                    955:                .&mt('Yes').'</label>'
                    956:                .'</span></p>'; 
1.1       raeburn   957:     } else {
1.95      bisitz    958:         $Str .= '<p class="LC_info">'."\n".
                    959:             &mt('This will not take effect if the user already exists.').
1.1       raeburn   960:             &Apache::loncommon::help_open_topic('Auth_Options').
                    961:             "</p>\n";
                    962:     }
1.97      raeburn   963:     $Str .= &set_login($defdom,$krbform,$intform,$locform);
1.95      bisitz    964: 
1.1       raeburn   965:     my ($home_server_pick,$numlib) =
                    966:         &Apache::loncommon::home_server_form_item($defdom,'lcserver',
                    967:                                                   'default','hide');
                    968:     if ($numlib > 1) {
1.97      raeburn   969:         $Str .= &Apache::lonhtmlcommon::row_closure()
                    970:                .&Apache::lonhtmlcommon::row_title(
1.95      bisitz    971:                     &mt('LON-CAPA Home Server for New Users'))
                    972:                .&mt('LON-CAPA domain: [_1] with home server:','"'.$defdom.'"')
                    973:                .$home_server_pick
                    974:                .&Apache::lonhtmlcommon::row_closure();
                    975:     } else {
1.97      raeburn   976:         $Str .= $home_server_pick.
                    977:                 &Apache::lonhtmlcommon::row_closure();
1.95      bisitz    978:     }
                    979: 
                    980:     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Default domain'))
                    981:            .&Apache::loncommon::select_dom_form($defdom,'defaultdomain',undef,1)
                    982:            .&Apache::lonhtmlcommon::row_closure();
                    983: 
                    984:     $Str .= &Apache::lonhtmlcommon::row_title(&mt('Starting and Ending Dates'))
                    985:            ."<p>\n".$date_table."</p>\n"
                    986:            .&Apache::lonhtmlcommon::row_closure();
                    987: 
1.1       raeburn   988:     if ($context eq 'domain') {
1.95      bisitz    989:         $Str .= &Apache::lonhtmlcommon::row_title(
                    990:                     &mt('Settings for assigning roles'))
                    991:                .&mt('Pick the action to take on roles for these users:').'<br />'
                    992:                .'<span class="LC_nobreak"><label>'
                    993:                .'<input type="radio" name="roleaction" value="norole" checked="checked" />'
                    994:                .'&nbsp;'.&mt('No role changes').'</label>'
                    995:                .'&nbsp;&nbsp;&nbsp;<label>'
                    996:                .'<input type="radio" name="roleaction" value="domain" />'
                    997:                .'&nbsp;'.&mt('Add a domain role').'</label>'
                    998:                .'&nbsp;&nbsp;&nbsp;<label>'
                    999:                .'<input type="radio" name="roleaction" value="course" />'
1.103     raeburn  1000:                .'&nbsp;'.&mt('Add a course/community role').'</label>'
1.95      bisitz   1001:                .'</span>';
                   1002:     } elsif ($context eq 'author') {
                   1003:         $Str .= &Apache::lonhtmlcommon::row_title(
                   1004:                     &mt('Default role'))
                   1005:                .&mt('Choose the role to assign to users without a value specified in the uploaded file.')
1.1       raeburn  1006:     } elsif ($context eq 'course') {
1.150     raeburn  1007:         if ($showcredits) {
                   1008:             $Str .= &Apache::lonhtmlcommon::row_title(
                   1009:                     &mt('Default role, section and credits'))
                   1010:                    .&mt('Choose the role and/or section(s) and/or credits to assign to users without values specified in the uploaded file.');
                   1011:         } else { 
                   1012:             $Str .= &Apache::lonhtmlcommon::row_title(
1.95      bisitz   1013:                     &mt('Default role and section'))
1.150     raeburn  1014:                    .&mt('Choose the role and/or section(s) to assign to users without values specified in the uploaded file.');
                   1015:         }
1.95      bisitz   1016:     } else {
                   1017:         $Str .= &Apache::lonhtmlcommon::row_title(
                   1018:                     &mt('Default role and/or section(s)'))
                   1019:                .&mt('Role and/or section(s) for users without values specified in the uploaded file.');
1.1       raeburn  1020:     }
1.22      raeburn  1021:     if (($context eq 'domain') || ($context eq 'author')) {
1.95      bisitz   1022:         $Str .= '<br />';
1.150     raeburn  1023:         my ($options,$cb_script,$coursepick) = 
                   1024:             &default_role_selector($context,1,'',$showcredits);
1.22      raeburn  1025:         if ($context eq 'domain') {
1.95      bisitz   1026:             $Str .= '<p>'
                   1027:                    .'<b>'.&mt('Domain Level').'</b><br />'
                   1028:                    .$options
                   1029:                    .'</p><p>'
                   1030:                    .'<b>'.&mt('Course Level').'</b>'
                   1031:                    .'</p>'
                   1032:                    .$cb_script.$coursepick
                   1033:                    .&Apache::lonhtmlcommon::row_closure();
1.22      raeburn  1034:         } elsif ($context eq 'author') {
1.95      bisitz   1035:             $Str .= $options
                   1036:                    .&Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
1.22      raeburn  1037:         }
1.1       raeburn  1038:     } else {
1.22      raeburn  1039:         my ($cnum,$cdom) = &get_course_identity();
                   1040:         my $rowtitle = &mt('section');
1.150     raeburn  1041:         my $defaultcredits;
                   1042:         if ($showcredits) {
                   1043:             $defaultcredits = &get_defaultcredits();
                   1044:         }
                   1045:         my $secbox = &section_picker($cdom,$cnum,'Any',$rowtitle,$permission,
                   1046:                                      $context,'upload',$crstype,$showcredits,
                   1047:                                      $defaultcredits);
1.95      bisitz   1048:         $Str .= $secbox
                   1049:                .&Apache::lonhtmlcommon::row_closure();
1.101     raeburn  1050:         my %lt;
                   1051:         if ($crstype eq 'Community') {
                   1052:             %lt = &Apache::lonlocal::texthash (
                   1053:                     disp => 'Display members with current/future access who are not in the uploaded file',
                   1054:                     stus => 'Members selected from this list can be dropped.'
                   1055:             );
                   1056:         } else {
                   1057:             %lt = &Apache::lonlocal::texthash (
                   1058:                     disp => 'Display students with current/future access who are not in the uploaded file',
                   1059:                     stus => 'Students selected from this list can be dropped.'
                   1060:             );
                   1061:         }
1.95      bisitz   1062:         $Str .= &Apache::lonhtmlcommon::row_title(&mt('Full Update'))
1.101     raeburn  1063:                .'<label><input type="checkbox" name="fullup" value="yes" />'
                   1064:                .' '.$lt{'disp'}
1.95      bisitz   1065:                .'</label><br />'
1.101     raeburn  1066:                .$lt{'stus'}
1.95      bisitz   1067:                .&Apache::lonhtmlcommon::row_closure();
1.1       raeburn  1068:     }
1.5       raeburn  1069:     if ($context eq 'course' || $context eq 'domain') {
1.161     bisitz   1070:         $Str .= &Apache::lonhtmlcommon::row_title(&mt('Student/Employee ID'))
                   1071:                .&forceid_change($context)
                   1072:                .&Apache::lonhtmlcommon::row_closure(1); # last row in pick_box
1.5       raeburn  1073:     }
1.95      bisitz   1074: 
                   1075:     $Str .= &Apache::lonhtmlcommon::end_pick_box();
1.73      bisitz   1076:     $Str .= '</div>';
1.95      bisitz   1077: 
                   1078:     # Footer
                   1079:     $Str .= '<div class="LC_clear_float_footer">'
                   1080:            .'<hr />';
1.1       raeburn  1081:     if ($context eq 'course') {
1.95      bisitz   1082:         $Str .= '<p class="LC_info">'
1.103     raeburn  1083:                .&mt('Note: This operation may be time consuming when adding several users.')
1.95      bisitz   1084:                .'</p>';
1.73      bisitz   1085:     }
1.95      bisitz   1086:     $Str .= '<p><input type="button"'
1.96      bisitz   1087:            .' onclick="javascript:verify(this.form,this.form.csec)"'
                   1088:            .' value="'.&mt('Update Users').'" />'
1.95      bisitz   1089:            .'</p>'."\n"
1.73      bisitz   1090:            .'</div>';
1.1       raeburn  1091:     $r->print($Str);
                   1092:     return;
                   1093: }
                   1094: 
1.150     raeburn  1095: sub get_defaultcredits {
                   1096:     my ($cdom,$cnum) = @_;
                   1097:      
                   1098:     if ($cdom eq '' || $cnum eq '') {
                   1099:         return unless ($env{'request.course.id'});
                   1100:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1101:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1102:     }
                   1103:     return unless(($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)); 
                   1104:     my ($defaultcredits,$domdefcredits);
                   1105:     my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
1.160     raeburn  1106:     if ($domdefaults{'officialcredits'} || $domdefaults{'unofficialcredits'} || $domdefaults{'textbookcredits'}) {
1.150     raeburn  1107:         my $instcode = $env{'course.'.$cdom.'_'.$cnum.'.internal.coursecode'};
                   1108:         if ($instcode) {
                   1109:             $domdefcredits = $domdefaults{'officialcredits'};
1.160     raeburn  1110:         } elsif ($env{'course.'.$cdom.'_'.$cnum.'.internal.textbook'}) {
                   1111:             $domdefcredits = $domdefaults{'textbookcredits'};
1.150     raeburn  1112:         } else {
                   1113:             $domdefcredits = $domdefaults{'unofficialcredits'};
                   1114:         }
                   1115:     } else {
                   1116:         return;
                   1117:     }
                   1118: 
                   1119:     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
                   1120:         $defaultcredits = $env{'course.'.$cdom.'_'.$cnum.'.internal.defaultcredits'};
                   1121:     } elsif (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.defaultcredits'})) {
                   1122:         $defaultcredits = $env{'course.'.$cdom.'_'.$cnum.'.internal.defaultcredits'};
                   1123:     } else {
                   1124:         my %crsinfo =
                   1125:             &Apache::lonnet::coursedescription("$cdom/$cnum",{'one_time' => 1});
                   1126:         $defaultcredits = $crsinfo{'internal.defaultcredits'};
                   1127:     }
                   1128:     if ($defaultcredits eq '') {
                   1129:         $defaultcredits = $domdefcredits;
                   1130:     }
                   1131:     return $defaultcredits;
                   1132: }
                   1133: 
1.5       raeburn  1134: sub forceid_change {
                   1135:     my ($context) = @_;
                   1136:     my $output = 
1.161     bisitz   1137:         '<label><input type="checkbox" name="forceid" value="yes" />'
1.164     bisitz   1138:        .&mt('Force change of existing ID')
1.163     raeburn  1139:        .'</label>'.&Apache::loncommon::help_open_topic('ForceIDChange')."\n";
1.5       raeburn  1140:     if ($context eq 'domain') {
1.163     raeburn  1141:         $output .= 
                   1142:             '<br />'
                   1143:            .'<label><input type="checkbox" name="recurseid" value="yes" />'
                   1144:            .&mt("Update ID in user's course(s).").'</label>'."\n";
1.5       raeburn  1145:     }
                   1146:     return $output;
                   1147: }
                   1148: 
1.1       raeburn  1149: ###############################################################
                   1150: ###############################################################
                   1151: sub print_upload_manager_form {
1.150     raeburn  1152:     my ($r,$context,$permission,$crstype,$showcredits) = @_;
1.1       raeburn  1153:     my $firstLine;
                   1154:     my $datatoken;
                   1155:     if (!$env{'form.datatoken'}) {
                   1156:         $datatoken=&Apache::loncommon::upfile_store($r);
                   1157:     } else {
1.184.4.1  raeburn  1158:         $datatoken=&Apache::loncommon::valid_datatoken($env{'form.datatoken'});
                   1159:         if ($datatoken ne '') {
                   1160:             &Apache::loncommon::load_tmp_file($r,$datatoken);
                   1161:         }
                   1162:     }
                   1163:     if ($datatoken eq '') {
                   1164:         $r->print('<p class="LC_error">'.&mt('Error').': '.
                   1165:                   &mt('Invalid datatoken').'</p>');
                   1166:         return 'missingdata';
1.1       raeburn  1167:     }
                   1168:     my @records=&Apache::loncommon::upfile_record_sep();
                   1169:     if($env{'form.noFirstLine'}){
                   1170:         $firstLine=shift(@records);
                   1171:     }
                   1172:     my $total=$#records;
                   1173:     my $distotal=$total+1;
                   1174:     my $today=time;
                   1175:     my $halfyear=$today+15552000;
                   1176:     #
                   1177:     # Restore memorized settings
                   1178:     my $col_setting_names =  { 'username_choice' => 'scalar', # column settings
                   1179:                                'names_choice' => 'scalar',
                   1180:                                'fname_choice' => 'scalar',
                   1181:                                'mname_choice' => 'scalar',
                   1182:                                'lname_choice' => 'scalar',
                   1183:                                'gen_choice' => 'scalar',
                   1184:                                'id_choice' => 'scalar',
                   1185:                                'sec_choice' => 'scalar',
                   1186:                                'ipwd_choice' => 'scalar',
                   1187:                                'email_choice' => 'scalar',
                   1188:                                'role_choice' => 'scalar',
1.57      raeburn  1189:                                'domain_choice' => 'scalar',
1.84      raeburn  1190:                                'inststatus_choice' => 'scalar',
1.1       raeburn  1191:                              };
1.150     raeburn  1192:     if ($showcredits) {
                   1193:         $col_setting_names->{'credits_choice'} = 'scalar';
                   1194:     }
1.1       raeburn  1195:     if ($context eq 'course') {
                   1196:         &Apache::loncommon::restore_course_settings('enrollment_upload',
                   1197:                                                     $col_setting_names);
                   1198:     } else {
                   1199:         &Apache::loncommon::restore_settings($context,'user_upload',
                   1200:                                              $col_setting_names);
                   1201:     }
1.150     raeburn  1202:     my $defdom = $env{'request.role.domain'};
1.1       raeburn  1203:     #
                   1204:     # Determine kerberos parameters as appropriate
                   1205:     my ($krbdef,$krbdefdom) =
                   1206:         &Apache::loncommon::get_kerberos_defaults($defdom);
                   1207:     #
1.123     raeburn  1208:     my ($authnum,%can_assign) =  &Apache::loncommon::get_assignable_auth($defdom);
1.22      raeburn  1209:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
1.123     raeburn  1210:                                  $permission,$crstype,\%can_assign);
1.1       raeburn  1211:     my $i;
                   1212:     my $keyfields;
                   1213:     if ($total>=0) {
                   1214:         my @field=
                   1215:             (['username',&mt('Username'),     $env{'form.username_choice'}],
                   1216:              ['names',&mt('Last Name, First Names'),$env{'form.names_choice'}],
                   1217:              ['fname',&mt('First Name'),      $env{'form.fname_choice'}],
                   1218:              ['mname',&mt('Middle Names/Initials'),$env{'form.mname_choice'}],
                   1219:              ['lname',&mt('Last Name'),       $env{'form.lname_choice'}],
                   1220:              ['gen',  &mt('Generation'),      $env{'form.gen_choice'}],
1.61      bisitz   1221:              ['id',   &mt('Student/Employee ID'),$env{'form.id_choice'}],
1.1       raeburn  1222:              ['sec',  &mt('Section'),          $env{'form.sec_choice'}],
                   1223:              ['ipwd', &mt('Initial Password'),$env{'form.ipwd_choice'}],
                   1224:              ['email',&mt('E-mail Address'),   $env{'form.email_choice'}],
1.57      raeburn  1225:              ['role',&mt('Role'),             $env{'form.role_choice'}],
1.84      raeburn  1226:              ['domain',&mt('Domain'),         $env{'form.domain_choice'}],
                   1227:              ['inststatus',&mt('Affiliation'), $env{'form.inststatus_choice'}]);
1.150     raeburn  1228:         if ($showcredits) {     
                   1229:             push(@field,
                   1230:                  ['credits',&mt('Student Credits'), $env{'form.credits_choice'}]);
                   1231:         }
1.1       raeburn  1232:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   1233:             &Apache::loncommon::csv_print_samples($r,\@records);
                   1234:             $i=&Apache::loncommon::csv_print_select_table($r,\@records,
                   1235:                                                           \@field);
                   1236:             foreach (@field) {
                   1237:                 $keyfields.=$_->[0].',';
                   1238:             }
                   1239:             chop($keyfields);
                   1240:         } else {
                   1241:             unshift(@field,['none','']);
                   1242:             $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
                   1243:                                                             \@field);
                   1244:             my %sone=&Apache::loncommon::record_sep($records[0]);
                   1245:             $keyfields=join(',',sort(keys(%sone)));
                   1246:         }
                   1247:     }
                   1248:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear,
1.150     raeburn  1249:                                  $context,$permission,$crstype,$showcredits);
1.184.4.1  raeburn  1250:     return 'ok';
1.1       raeburn  1251: }
                   1252: 
                   1253: sub setup_date_selectors {
1.22      raeburn  1254:     my ($starttime,$endtime,$mode,$nolink,$formname) = @_;
                   1255:     if ($formname eq '') {
                   1256:         $formname = 'studentform';
                   1257:     }
1.1       raeburn  1258:     if (! defined($starttime)) {
                   1259:         $starttime = time;
                   1260:         unless ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                   1261:             if (exists($env{'course.'.$env{'request.course.id'}.
                   1262:                             '.default_enrollment_start_date'})) {
                   1263:                 $starttime = $env{'course.'.$env{'request.course.id'}.
                   1264:                                   '.default_enrollment_start_date'};
                   1265:             }
                   1266:         }
                   1267:     }
                   1268:     if (! defined($endtime)) {
                   1269:         $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                   1270:         unless ($mode eq 'createcourse') {
                   1271:             if (exists($env{'course.'.$env{'request.course.id'}.
                   1272:                             '.default_enrollment_end_date'})) {
                   1273:                 $endtime = $env{'course.'.$env{'request.course.id'}.
                   1274:                                 '.default_enrollment_end_date'};
                   1275:             }
                   1276:         }
                   1277:     }
1.11      raeburn  1278: 
                   1279:     my $startdateform = 
1.22      raeburn  1280:         &Apache::lonhtmlcommon::date_setter($formname,'startdate',$starttime,
1.11      raeburn  1281:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                   1282: 
                   1283:     my $enddateform = 
1.22      raeburn  1284:         &Apache::lonhtmlcommon::date_setter($formname,'enddate',$endtime,
1.11      raeburn  1285:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                   1286: 
1.1       raeburn  1287:     if ($mode eq 'create_enrolldates') {
                   1288:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1289:                                                             'startenroll',
                   1290:                                                             $starttime);
                   1291:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1292:                                                           'endenroll',
                   1293:                                                           $endtime);
                   1294:     }
                   1295:     if ($mode eq 'create_defaultdates') {
                   1296:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1297:                                                             'startaccess',
                   1298:                                                             $starttime);
                   1299:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                   1300:                                                           'endaccess',
                   1301:                                                           $endtime);
                   1302:     }
                   1303:     return ($startdateform,$enddateform);
                   1304: }
                   1305: 
                   1306: 
                   1307: sub get_dates_from_form {
1.54      raeburn  1308:     my ($startname,$endname) = @_;
                   1309:     if ($startname eq '') {
                   1310:         $startname = 'startdate';
                   1311:     }
                   1312:     if ($endname eq '') {
                   1313:         $endname = 'enddate';
                   1314:     }
                   1315:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form($startname);
                   1316:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form($endname);
1.1       raeburn  1317:     if ($env{'form.no_end_date'}) {
                   1318:         $enddate = 0;
                   1319:     }
                   1320:     return ($startdate,$enddate);
                   1321: }
                   1322: 
                   1323: sub date_setting_table {
1.101     raeburn  1324:     my ($starttime,$endtime,$mode,$bulkaction,$formname,$permission,$crstype) = @_;
1.11      raeburn  1325:     my $nolink;
                   1326:     if ($bulkaction) {
                   1327:         $nolink = 1;
                   1328:     }
                   1329:     my ($startform,$endform) = 
1.22      raeburn  1330:         &setup_date_selectors($starttime,$endtime,$mode,$nolink,$formname);
1.1       raeburn  1331:     my $dateDefault;
                   1332:     if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                   1333:         $dateDefault = '&nbsp;';
1.13      raeburn  1334:     } elsif ($mode ne 'author' && $mode ne 'domain') {
1.11      raeburn  1335:         if (($bulkaction eq 'reenable') || 
                   1336:             ($bulkaction eq 'activate') || 
1.22      raeburn  1337:             ($bulkaction eq 'chgdates') ||
                   1338:             ($env{'form.action'} eq 'upload')) {
                   1339:             if ($env{'request.course.sec'} eq '') {
                   1340:                 $dateDefault = '<span class="LC_nobreak">'.
1.101     raeburn  1341:                     '<label><input type="checkbox" name="makedatesdefault" value="1" /> ';
                   1342:                 if ($crstype eq 'Community') {
                   1343:                     $dateDefault .= &mt("make these dates the default access dates for future community enrollment");
                   1344:                 } else {
                   1345:                     $dateDefault .= &mt("make these dates the default access dates for future course enrollment");
                   1346:                 }
                   1347:                 $dateDefault .= '</label></span>';
1.22      raeburn  1348:             }
1.11      raeburn  1349:         }
1.1       raeburn  1350:     }
1.11      raeburn  1351:     my $perpetual = '<span class="LC_nobreak"><label><input type="checkbox" name="no_end_date"';
1.1       raeburn  1352:     if (defined($endtime) && $endtime == 0) {
1.70      bisitz   1353:         $perpetual .= ' checked="checked"';
1.1       raeburn  1354:     }
1.11      raeburn  1355:     $perpetual.= ' /> '.&mt('no ending date').'</label></span>';
1.1       raeburn  1356:     if ($mode eq 'create_enrolldates') {
                   1357:         $perpetual = '&nbsp;';
                   1358:     }
1.11      raeburn  1359:     my $result = &Apache::lonhtmlcommon::start_pick_box()."\n";
                   1360:     $result .= &Apache::lonhtmlcommon::row_title(&mt('Starting Date'),
                   1361:                                                      'LC_oddrow_value')."\n".
                   1362:                $startform."\n".
                   1363:                &Apache::lonhtmlcommon::row_closure(1).
                   1364:                &Apache::lonhtmlcommon::row_title(&mt('Ending Date'), 
                   1365:                                                      'LC_oddrow_value')."\n".
                   1366:                $endform.'&nbsp;'.$perpetual.
                   1367:                &Apache::lonhtmlcommon::row_closure(1).
1.22      raeburn  1368:                &Apache::lonhtmlcommon::end_pick_box();
1.1       raeburn  1369:     if ($dateDefault) {
                   1370:         $result .=  $dateDefault.'<br />'."\n";
                   1371:     }
                   1372:     return $result;
                   1373: }
                   1374: 
                   1375: sub make_dates_default {
1.101     raeburn  1376:     my ($startdate,$enddate,$context,$crstype) = @_;
1.1       raeburn  1377:     my $result = '';
                   1378:     if ($context eq 'course') {
1.17      raeburn  1379:         my ($cnum,$cdom) = &get_course_identity();
1.1       raeburn  1380:         my $put_result = &Apache::lonnet::put('environment',
                   1381:                 {'default_enrollment_start_date'=>$startdate,
1.17      raeburn  1382:                  'default_enrollment_end_date'  =>$enddate},$cdom,$cnum);
1.1       raeburn  1383:         if ($put_result eq 'ok') {
1.101     raeburn  1384:             if ($crstype eq 'Community') {
                   1385:                 $result .= &mt('Set default start and end access dates for community.');
                   1386:             } else {
                   1387:                 $result .= &mt('Set default start and end access dates for course.');
                   1388:             }
                   1389:             $result .= '<br />'."\n";
1.1       raeburn  1390:             #
                   1391:             # Refresh the course environment
                   1392:             &Apache::lonnet::coursedescription($env{'request.course.id'},
                   1393:                                                {'freshen_cache' => 1});
                   1394:         } else {
1.101     raeburn  1395:             if ($crstype eq 'Community') {
                   1396:                 $result .= &mt('Unable to set default access dates for community');
                   1397:             } else {
                   1398:                 $result .= &mt('Unable to set default access dates for course');
                   1399:             }
                   1400:             $result .= ':'.$put_result.'<br />';
1.1       raeburn  1401:         }
                   1402:     }
                   1403:     return $result;
                   1404: }
                   1405: 
                   1406: sub default_role_selector {
1.150     raeburn  1407:     my ($context,$checkpriv,$crstype,$showcredits) = @_;
1.1       raeburn  1408:     my %customroles;
                   1409:     my ($options,$coursepick,$cb_jscript);
1.13      raeburn  1410:     if ($context ne 'author') {
1.104     raeburn  1411:         %customroles = &my_custom_roles($crstype);
1.1       raeburn  1412:     }
                   1413: 
                   1414:     my %lt=&Apache::lonlocal::texthash(
                   1415:                     'rol'  => "Role",
                   1416:                     'grs'  => "Section",
                   1417:                     'exs'  => "Existing sections",
                   1418:                     'new'  => "New section",
1.150     raeburn  1419:                     'crd'  => "Credits",
1.1       raeburn  1420:                   );
                   1421:     $options = '<select name="defaultrole">'."\n".
                   1422:                ' <option value="">'.&mt('Please select').'</option>'."\n"; 
                   1423:     if ($context eq 'course') {
1.101     raeburn  1424:         $options .= &default_course_roles($context,$checkpriv,$crstype,%customroles);
1.13      raeburn  1425:     } elsif ($context eq 'author') {
1.2       raeburn  1426:         my @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1427:         foreach my $role (@roles) {
                   1428:            my $plrole=&Apache::lonnet::plaintext($role);
                   1429:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
                   1430:         }
                   1431:     } elsif ($context eq 'domain') {
1.2       raeburn  1432:         my @roles = &domain_roles($checkpriv);
1.1       raeburn  1433:         foreach my $role (@roles) {
                   1434:            my $plrole=&Apache::lonnet::plaintext($role);
                   1435:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1436:         }
                   1437:         my $courseform = &Apache::loncommon::selectcourse_link
1.103     raeburn  1438:             ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course/Community');
1.150     raeburn  1439:         my ($credit_elem,$creditsinput);
                   1440:         if ($showcredits) {
                   1441:             $credit_elem = 'credits';
                   1442:             $creditsinput = '<td><input type="text" name="credits" value="" /></td>';
                   1443:         }
1.1       raeburn  1444:         $cb_jscript = 
1.150     raeburn  1445:             &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform','courserole','Course/Community',$credit_elem);
1.1       raeburn  1446:         $coursepick = &Apache::loncommon::start_data_table().
                   1447:                       &Apache::loncommon::start_data_table_header_row().
                   1448:                       '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th>'.
                   1449:                       '<th>'.$lt{'grs'}.'</th>'.
1.150     raeburn  1450:                       '<th>'.$lt{'crd'}.'</th>'.
1.1       raeburn  1451:                       &Apache::loncommon::end_data_table_header_row().
                   1452:                       &Apache::loncommon::start_data_table_row()."\n".
1.103     raeburn  1453:                       '<td><input type="text" name="coursedesc" value="" onfocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc','','','','crstype'".')" /></td>'."\n".
1.1       raeburn  1454:                       '<td><select name="courserole">'."\n".
1.101     raeburn  1455:                       &default_course_roles($context,$checkpriv,'Course',%customroles)."\n".
1.1       raeburn  1456:                       '</select></td><td>'.
                   1457:                       '<table class="LC_createuser">'.
1.162     bisitz   1458:                       '<tr class="LC_section_row"><td valign="top">'.
1.22      raeburn  1459:                       $lt{'exs'}.'<br /><select name="currsec">'.
1.162     bisitz   1460:                       ' <option value="">&lt;--'.&mt('Pick course first').
1.1       raeburn  1461:                       '</select></td>'.
                   1462:                       '<td>&nbsp;&nbsp;</td>'.
                   1463:                       '<td valign="top">'.$lt{'new'}.'<br />'.
                   1464:                       '<input type="text" name="newsec" value="" size="5" />'.
1.22      raeburn  1465:                       '<input type="hidden" name="groups" value="" />'.
                   1466:                       '<input type="hidden" name="sections" value="" />'.
                   1467:                       '<input type="hidden" name="origdom" value="'.
                   1468:                       $env{'request.role.domain'}.'" />'.
                   1469:                       '<input type="hidden" name="dccourse" value="" />'.
                   1470:                       '<input type="hidden" name="dcdomain" value="" />'.
1.103     raeburn  1471:                       '<input type="hidden" name="crstype" value="" />'.
1.150     raeburn  1472:                       '</td></tr></table></td>'.$creditsinput.
1.1       raeburn  1473:                       &Apache::loncommon::end_data_table_row().
1.22      raeburn  1474:                       &Apache::loncommon::end_data_table()."\n";
1.1       raeburn  1475:     }
                   1476:     $options .= '</select>';
                   1477:     return ($options,$cb_jscript,$coursepick);
                   1478: }
                   1479: 
                   1480: sub default_course_roles {
1.101     raeburn  1481:     my ($context,$checkpriv,$crstype,%customroles) = @_;
1.1       raeburn  1482:     my $output;
1.17      raeburn  1483:     my $custom = 1;
1.101     raeburn  1484:     my @roles = &course_roles($context,$checkpriv,$custom,lc($crstype));
1.1       raeburn  1485:     foreach my $role (@roles) {
1.22      raeburn  1486:         if ($role ne 'cr') {
1.101     raeburn  1487:             my $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.22      raeburn  1488:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1489:         }
1.1       raeburn  1490:     }
                   1491:     if (keys(%customroles) > 0) {
1.22      raeburn  1492:         if (grep(/^cr$/,@roles)) {
                   1493:             foreach my $cust (sort(keys(%customroles))) {
                   1494:                 my $custrole='cr_'.$env{'user.domain'}.
                   1495:                              '_'.$env{'user.name'}.'_'.$cust;
                   1496:                 $output .= '  <option value="'.$custrole.'">'.$cust.'</option>';
                   1497:             }
1.1       raeburn  1498:         }
                   1499:     }
                   1500:     return $output;
                   1501: }
                   1502: 
                   1503: sub construction_space_roles {
1.2       raeburn  1504:     my ($checkpriv) = @_;
1.17      raeburn  1505:     my @allroles = &roles_by_context('author');
1.1       raeburn  1506:     my @roles;
1.2       raeburn  1507:     if ($checkpriv) {
                   1508:         foreach my $role (@allroles) {
                   1509:             if (&Apache::lonnet::allowed('c'.$role,$env{'user.domain'}.'/'.$env{'user.name'})) { 
                   1510:                 push(@roles,$role); 
1.184.4.10.2.  (raeburn 1511:):             } elsif ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)$}) {
                   1512:):                 my ($audom,$auname) = ($1,$2);
                   1513:):                 if (($role eq 'ca') || ($role eq 'aa')) {
                   1514:):                     if ((&Apache::lonnet::allowed('v'.$role,,$audom.'/'.$auname)) &&
                   1515:):                         ($env{"environment.internal.manager./$audom/$auname"})) {
                   1516:):                         push(@roles,$role);
                   1517:):                     }
                   1518:):                 }
1.2       raeburn  1519:             }
1.1       raeburn  1520:         }
1.2       raeburn  1521:         return @roles;
                   1522:     } else {
                   1523:         return @allroles;
1.1       raeburn  1524:     }
                   1525: }
                   1526: 
                   1527: sub domain_roles {
1.2       raeburn  1528:     my ($checkpriv) = @_;
1.17      raeburn  1529:     my @allroles = &roles_by_context('domain');
1.1       raeburn  1530:     my @roles;
1.2       raeburn  1531:     if ($checkpriv) {
                   1532:         foreach my $role (@allroles) {
                   1533:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   1534:                 push(@roles,$role);
                   1535:             }
1.1       raeburn  1536:         }
1.2       raeburn  1537:         return @roles;
                   1538:     } else {
                   1539:         return @allroles;
1.1       raeburn  1540:     }
                   1541: }
                   1542: 
                   1543: sub course_roles {
1.101     raeburn  1544:     my ($context,$checkpriv,$custom,$roletype) = @_;
1.102     raeburn  1545:     my $crstype;
                   1546:     if ($roletype eq 'community') {
                   1547:         $crstype = 'Community' ;
                   1548:     } else {
                   1549:         $crstype = 'Course';
                   1550:     }
                   1551:     my @allroles = &roles_by_context('course',$custom,$crstype);
1.1       raeburn  1552:     my @roles;
                   1553:     if ($context eq 'domain') {
                   1554:         @roles = @allroles;
                   1555:     } elsif ($context eq 'course') {
                   1556:         if ($env{'request.course.id'}) {
1.2       raeburn  1557:             if ($checkpriv) { 
                   1558:                 foreach my $role (@allroles) {
                   1559:                     if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   1560:                         push(@roles,$role);
                   1561:                     } else {
1.101     raeburn  1562:                         if ((($role ne 'cc') && ($role ne 'co')) && ($env{'request.course.sec'} ne '')) {
1.22      raeburn  1563:                             if (&Apache::lonnet::allowed('c'.$role,
1.2       raeburn  1564:                                              $env{'request.course.id'}.'/'.
1.22      raeburn  1565:                                              $env{'request.course.sec'})) {
1.2       raeburn  1566:                                 push(@roles,$role);
                   1567:                             }
1.1       raeburn  1568:                         }
                   1569:                     }
                   1570:                 }
1.2       raeburn  1571:             } else {
                   1572:                 @roles = @allroles;
1.1       raeburn  1573:             }
                   1574:         }
                   1575:     }
                   1576:     return @roles;
                   1577: }
                   1578: 
                   1579: sub curr_role_permissions {
1.101     raeburn  1580:     my ($context,$setting,$checkpriv,$type) = @_; 
1.17      raeburn  1581:     my $custom = 1;
1.1       raeburn  1582:     my @roles;
1.13      raeburn  1583:     if ($context eq 'author') {
1.2       raeburn  1584:         @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1585:     } elsif ($context eq 'domain') {
                   1586:         if ($setting eq 'course') {
1.101     raeburn  1587:             @roles = &course_roles($context,$checkpriv,$custom,$type); 
1.1       raeburn  1588:         } else {
1.2       raeburn  1589:             @roles = &domain_roles($checkpriv);
1.1       raeburn  1590:         }
                   1591:     } elsif ($context eq 'course') {
1.101     raeburn  1592:         @roles = &course_roles($context,$checkpriv,$custom,$type);
1.1       raeburn  1593:     }
                   1594:     return @roles;
                   1595: }
                   1596: 
                   1597: # ======================================================= Existing Custom Roles
                   1598: 
                   1599: sub my_custom_roles {
1.175     raeburn  1600:     my ($crstype,$udom,$uname) = @_;
1.1       raeburn  1601:     my %returnhash=();
1.137     raeburn  1602:     my $extra = &Apache::lonnet::freeze_escape({'skipcheck' => 1});
1.175     raeburn  1603:     my %rolehash=&Apache::lonnet::dump('roles',$udom,$uname);
1.104     raeburn  1604:     foreach my $key (keys(%rolehash)) {
1.1       raeburn  1605:         if ($key=~/^rolesdef\_(\w+)$/) {
1.104     raeburn  1606:             if ($crstype eq 'Community') {
                   1607:                 next if ($rolehash{$key} =~ /bre\&S/); 
                   1608:             }
1.1       raeburn  1609:             $returnhash{$1}=$1;
                   1610:         }
                   1611:     }
                   1612:     return %returnhash;
                   1613: }
                   1614: 
1.2       raeburn  1615: sub print_userlist {
                   1616:     my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
1.150     raeburn  1617:         $idlist,$idlist_titles,$showcredits) = @_;
1.2       raeburn  1618:     my $format = $env{'form.output'};
1.1       raeburn  1619:     if (! exists($env{'form.sortby'})) {
                   1620:         $env{'form.sortby'} = 'username';
                   1621:     }
1.184.4.10.2.  (raeburn 1622:):     my ($showstart,$showend);
                   1623:):     if (($env{'form.Status'} eq '') && ($env{'form.phase'} eq '') &&
                   1624:):         ($env{'form.showrole'} eq '') && ($context eq 'course') &&
                   1625:):         ($env{'request.course.id'} ne '') &&
                   1626:):         ($env{'course.'.$env{'request.course.id'}.'.internal.coursecode'} ne '')) {
                   1627:):         my $now = time;
                   1628:):         my $startaccess = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_start_date'};
                   1629:):         my $endaccess = $env{'course.'.$env{'request.course.id'}.'.default_enrollment_end_date'};
                   1630:):         if (($startaccess) && ($startaccess > $now)) {
                   1631:):             $env{'form.Status'} = 'Future';
                   1632:):             $showstart = 1;
                   1633:):         }
                   1634:):         if (($endaccess ne '') && ($endaccess != 0) && ($endaccess < $now)) {
                   1635:):             $env{'form.Status'} = 'Expired';
                   1636:):             undef($showstart);
                   1637:):             $showend = 1;
                   1638:):         }
                   1639:):     }
1.2       raeburn  1640:     if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
                   1641:         $env{'form.Status'} = 'Active';
1.1       raeburn  1642:     }
1.140     raeburn  1643:     my $onchange = "javascript:updateCols('Status');";
1.1       raeburn  1644:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
1.140     raeburn  1645:         ($env{'form.Status'},undef,undef,$onchange);
1.1       raeburn  1646: 
1.2       raeburn  1647:     if ($env{'form.showrole'} eq '') {
1.13      raeburn  1648:         if ($context eq 'course') {
                   1649:             $env{'form.showrole'} = 'st';
                   1650:         } else {
                   1651:             $env{'form.showrole'} = 'Any';            
                   1652:         }
1.2       raeburn  1653:     }
1.1       raeburn  1654:     if (! defined($env{'form.output'}) ||
                   1655:         $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
                   1656:         $env{'form.output'} = 'html';
                   1657:     }
                   1658: 
1.2       raeburn  1659:     my @statuses;
                   1660:     if ($env{'form.Status'} eq 'Any') {
                   1661:         @statuses = ('previous','active','future');
                   1662:     } elsif ($env{'form.Status'} eq 'Expired') {
                   1663:         @statuses = ('previous');
                   1664:     } elsif ($env{'form.Status'} eq 'Active') {
                   1665:         @statuses = ('active');
                   1666:     } elsif ($env{'form.Status'} eq 'Future') {
                   1667:         @statuses = ('future');
                   1668:     }
1.1       raeburn  1669: 
                   1670:     # Interface output
1.2       raeburn  1671:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   1672:               '<input type="hidden" name="action" value="'.
1.1       raeburn  1673:               $env{'form.action'}.'" />');
1.140     raeburn  1674:     $r->print('<div>'."\n");
1.1       raeburn  1675:     if ($env{'form.action'} ne 'modifystudent') {
                   1676:         my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
                   1677:                                            'excel' => "Excel",
                   1678:                                            'html'  => 'HTML');
1.140     raeburn  1679:         my $output_selector = '<select size="1" name="output" onchange="javascript:updateCols('."'output'".');" >';
1.1       raeburn  1680:         foreach my $outputformat ('html','csv','excel') {
1.96      bisitz   1681:             my $option = '<option value="'.$outputformat.'"';
1.1       raeburn  1682:             if ($outputformat eq $env{'form.output'}) {
1.96      bisitz   1683:                 $option .= ' selected="selected"';
1.1       raeburn  1684:             }
                   1685:             $option .='>'.$lt{$outputformat}.'</option>';
                   1686:             $output_selector .= "\n".$option;
                   1687:         }
                   1688:         $output_selector .= '</select>';
1.140     raeburn  1689:         $r->print('<span class="LC_nobreak">'
1.70      bisitz   1690:                  .&mt('Output Format: [_1]',$output_selector)
1.140     raeburn  1691:                  .'</span>'.('&nbsp;'x3));
1.70      bisitz   1692:     }
1.140     raeburn  1693:     $r->print('<span class="LC_nobreak">'
1.70      bisitz   1694:              .&mt('User Status: [_1]',$status_select)
1.140     raeburn  1695:              .'</span>'.('&nbsp;'x3)."\n");
1.2       raeburn  1696:     my $roleselected = '';
                   1697:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1698:        $roleselected = ' selected="selected"'; 
1.2       raeburn  1699:     }
1.53      raeburn  1700:     my ($cnum,$cdom);
                   1701:     $r->print(&role_filter($context));
                   1702:     if ($context eq 'course') {
                   1703:         ($cnum,$cdom) = &get_course_identity();
                   1704:         $r->print(&section_group_filter($cnum,$cdom));
1.2       raeburn  1705:     }
1.140     raeburn  1706:     $r->print('</div><div class="LC_left_float">'.
1.184.4.10.2.  (raeburn 1707:):               &column_checkboxes($context,$mode,$formname,$showcredits,$showstart,$showend).
1.149     raeburn  1708:               '</div>');
1.78      raeburn  1709:     if ($env{'form.phase'} eq '') {
1.149     raeburn  1710:         $r->print('<br clear="all" />'.
                   1711:                   &list_submit_button(&mt('Display List of Users'))."\n".
1.78      raeburn  1712:                   '<input type="hidden" name="phase" value="" /></form>');
                   1713:         return;
                   1714:     }
1.106     raeburn  1715:     if (!(($context eq 'domain') && 
                   1716:           (($env{'form.roletype'} eq 'course') || ($env{'form.roletype'} eq 'community')))) {
1.149     raeburn  1717:         $r->print('<br clear="all" />'.
                   1718:                   &list_submit_button(&mt('Update Display'))."\n");
1.140     raeburn  1719:     }
                   1720: 
1.150     raeburn  1721:     my @cols = &infocolumns($context,$mode,$showcredits);  
1.140     raeburn  1722:     if (!@cols) {
1.152     bisitz   1723:          $r->print('<hr style="clear:both;" /><span class="LC_warning">'.
1.140     raeburn  1724:                    &mt('No user information selected for display.').'</span>'.
                   1725:                    '<input type="hidden" name="phase" value="display" /></form>'."\n");
                   1726:          return;
1.2       raeburn  1727:     }
                   1728:     my ($indexhash,$keylist) = &make_keylist_array();
1.159     raeburn  1729:     my (%userlist,%userinfo,$clearcoursepick,$needauthorquota,$needauthorusage);
1.102     raeburn  1730:     if (($context eq 'domain') && 
                   1731:         ($env{'form.roletype'} eq 'course') || 
                   1732:         ($env{'form.roletype'} eq 'community')) {
                   1733:         my ($crstype,$numcodes,$title,$warning);
                   1734:         if ($env{'form.roletype'} eq 'course') {
                   1735:             $crstype = 'Course';
                   1736:             $numcodes = $totcodes;
                   1737:             $title = &mt('Select Courses');
                   1738:             $warning = &mt('Warning: data retrieval for multiple courses can take considerable time, as this operation is not currently optimized.');
                   1739:         } elsif ($env{'form.roletype'} eq 'community') {
                   1740:             $crstype = 'Community';
                   1741:             $numcodes = 0;
                   1742:             $title = &mt('Select Communities');
                   1743:             $warning = &mt('Warning: data retrieval for multiple communities can take considerable time, as this operation is not currently optimized.');
                   1744:         }
1.120     raeburn  1745:         my @standardnames = &Apache::loncommon::get_standard_codeitems();
1.3       raeburn  1746:         my $courseform =
1.102     raeburn  1747:             &Apache::lonhtmlcommon::course_selection($formname,$numcodes,
1.120     raeburn  1748:                             $codetitles,$idlist,$idlist_titles,$crstype,
                   1749:                             \@standardnames);
1.148     bisitz   1750:         $r->print('<div class="LC_left_float">'.
                   1751:                   '<fieldset><legend>'.$title.'</legend>'."\n".
1.3       raeburn  1752:                   $courseform."\n".
1.148     bisitz   1753:                   '</fieldset></div><br clear="all" />'.
1.106     raeburn  1754:                   '<p><input type="hidden" name="origroletype" value="'.$env{'form.roletype'}.'" />'.
                   1755:                   &list_submit_button(&mt('Update Display')).
1.102     raeburn  1756:                   "\n".'</p><span class="LC_warning">'.$warning.'</span>'."\n");
1.106     raeburn  1757:         $clearcoursepick = 0;
                   1758:         if (($env{'form.origroletype'} ne '') &&
                   1759:             ($env{'form.origroletype'} ne $env{'form.roletype'})) {
                   1760:             $clearcoursepick = 1;
                   1761:         }
                   1762:         if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.147     bisitz   1763:             $r->print('<hr />'.&mt('Searching ...').'<br />&nbsp;<br />');
1.11      raeburn  1764:         }
                   1765:     } else {
1.152     bisitz   1766:         $r->print('<hr style="clear:both;" /><div id="searching">'.&mt('Searching ...').'</div>');
1.3       raeburn  1767:     }
                   1768:     $r->rflush();
1.1       raeburn  1769:     if ($context eq 'course') {
1.46      raeburn  1770:         if (($env{'form.showrole'} eq 'st') || ($env{'form.showrole'} eq 'Any')) { 
1.45      raeburn  1771:             my $classlist = &Apache::loncoursedata::get_classlist();
1.66      raeburn  1772:             if (ref($classlist) eq 'HASH') {
                   1773:                 %userlist = %{$classlist};
                   1774:             }
1.45      raeburn  1775:         }
1.43      raeburn  1776:         if ($env{'form.showrole'} ne 'st') {
                   1777:             my $showroles;
                   1778:             if ($env{'form.showrole'} ne 'Any') {
                   1779:                 $showroles = [$env{'form.showrole'}];
1.3       raeburn  1780:             } else {
1.43      raeburn  1781:                 $showroles = undef;
1.1       raeburn  1782:             }
1.43      raeburn  1783:             my $withsec = 1;
                   1784:             my $hidepriv = 1;
                   1785:             my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   1786:                               \@statuses,$showroles,undef,$withsec,$hidepriv);
                   1787:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
                   1788:                              \%advrolehash,$permission);
1.1       raeburn  1789:         }
1.2       raeburn  1790:     } else {
                   1791:         my (%cstr_roles,%dom_roles);
1.13      raeburn  1792:         if ($context eq 'author') {
1.17      raeburn  1793:             my @possroles = &roles_by_context($context);
1.184.4.10.2.  (raeburn 1794:):             my @allowedroles;
                   1795:):             # List co-authors and assistant co-authors
                   1796:):             my ($auname,$audom);
                   1797:):             if ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)$}) {
                   1798:):                 ($audom,$auname) = ($1,$2);
                   1799:):                 foreach my $role (@possroles) {
                   1800:):                     if ((&Apache::lonnet::allowed('v'.$role,"$audom/$auname")) ||
                   1801:):                         (&Apache::lonnet::allowed('c'.$role,"$audom/$auname"))) {
                   1802:):                         push(@allowedroles,$role);
                   1803:):                     }
                   1804:):                 }
                   1805:):             } elsif ($env{'request.role'} =~ m{^au\./($match_domain)/}) {
                   1806:):                 if ($1 eq $env{'user.domain'}) {
                   1807:):                     $auname = $env{'user.name'};
                   1808:):                     $audom = $env{'user.domain'};
                   1809:):                 }
                   1810:):                 @allowedroles = @possroles;
                   1811:):             }
                   1812:):             if (($auname ne '') && ($audom ne '')) {
                   1813:):                 %cstr_roles = &Apache::lonnet::get_my_roles($auname,$audom,undef,
                   1814:):                                                             \@statuses,\@allowedroles);
                   1815:):                 &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
                   1816:):                                  \%cstr_roles,$permission);
                   1817:):             }
1.2       raeburn  1818:         } elsif ($context eq 'domain') {
                   1819:             if ($env{'form.roletype'} eq 'domain') {
1.159     raeburn  1820:                 if (grep(/^authorusage$/,@cols)) {
                   1821:                     $needauthorusage = 1;
                   1822:                 }
                   1823:                 if (grep(/^authorquota$/,@cols)) {
                   1824:                     $needauthorquota = 1;
                   1825:                 }
1.2       raeburn  1826:                 %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'});
                   1827:                 foreach my $key (keys(%dom_roles)) {
                   1828:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1829:                         &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1830:                                          \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1831:                     }
                   1832:                 }
1.13      raeburn  1833:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1834:                 my %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'},['au']);
                   1835:                 my %coauthors;
                   1836:                 foreach my $key (keys(%dom_roles)) {
                   1837:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1838:                         if ($env{'form.showrole'} eq 'au') {
                   1839:                             &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1840:                                              \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1841:                         } else {
                   1842:                             my @possroles;
                   1843:                             if ($env{'form.showrole'} eq 'Any') {
1.22      raeburn  1844:                                 @possroles = &roles_by_context('author');
1.2       raeburn  1845:                             } else {
                   1846:                                 @possroles = ($env{'form.showrole'}); 
                   1847:                             }
                   1848:                             foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1.22      raeburn  1849:                                 my ($role,$authorname,$authordom) = split(/:/,$author,-1);
1.2       raeburn  1850:                                 my $extent = '/'.$authordom.'/'.$authorname;
                   1851:                                 %{$coauthors{$extent}} =
                   1852:                                     &Apache::lonnet::get_my_roles($authorname,
                   1853:                                        $authordom,undef,\@statuses,\@possroles);
                   1854:                             }
                   1855:                             &gather_userinfo($context,$format,\%userlist,
1.11      raeburn  1856:                                      $indexhash,\%userinfo,\%coauthors,$permission);
1.2       raeburn  1857:                         }
                   1858:                     }
                   1859:                 }
1.101     raeburn  1860:             } elsif (($env{'form.roletype'} eq 'course') ||
                   1861:                      ($env{'form.roletype'} eq 'community')) {
1.106     raeburn  1862:                 if (($env{'form.coursepick'}) && (!$clearcoursepick)) {
1.2       raeburn  1863:                     my %courses = &process_coursepick();
1.39      raeburn  1864:                     my %allusers;
                   1865:                     my $hidepriv = 1;
1.2       raeburn  1866:                     foreach my $cid (keys(%courses)) {
1.17      raeburn  1867:                         my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
1.11      raeburn  1868:                         next if ($cnum eq '' || $cdom eq '');
1.17      raeburn  1869:                         my $custom = 1;
1.2       raeburn  1870:                         my (@roles,@sections,%access,%users,%userdata,
1.6       albertel 1871:                             %statushash);
1.2       raeburn  1872:                         if ($env{'form.showrole'} eq 'Any') {
1.101     raeburn  1873:                             @roles = &course_roles($context,undef,$custom,
                   1874:                                                    $env{'form.roletype'});
1.2       raeburn  1875:                         } else {
                   1876:                             @roles = ($env{'form.showrole'});
                   1877:                         }
                   1878:                         foreach my $role (@roles) {
                   1879:                             %{$users{$role}} = ();
                   1880:                         }
                   1881:                         foreach my $type (@statuses) {
                   1882:                             $access{$type} = $type;
                   1883:                         }
1.39      raeburn  1884:                         &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash,$hidepriv);
1.2       raeburn  1885:                         foreach my $user (keys(%userdata)) {
                   1886:                             next if (ref($userinfo{$user}) eq 'HASH');
                   1887:                             foreach my $item ('fullname','id') {
                   1888:                                 $userinfo{$user}{$item} = $userdata{$user}[$indexhash->{$item}];
                   1889:                             }
                   1890:                         }
                   1891:                         foreach my $role (keys(%users)) {
                   1892:                             foreach my $user (keys(%{$users{$role}})) {
                   1893:                                 my $uniqid = $user.':'.$role;
                   1894:                                 $allusers{$uniqid}{$cid} = { desc => $cdesc,
                   1895:                                                              secs  => $statushash{$user}{$role},
                   1896:                                                            };
                   1897:                             }
                   1898:                         }
                   1899:                     }
                   1900:                     &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1901:                                      \%userinfo,\%allusers,$permission);
1.2       raeburn  1902:                 } else {
1.10      raeburn  1903:                     $r->print('<input type="hidden" name="phase" value="'.
                   1904:                               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1905:                     return;
                   1906:                 }
1.1       raeburn  1907:             }
                   1908:         }
1.3       raeburn  1909:     }
                   1910:     if (keys(%userlist) == 0) {
1.142     bisitz   1911:         my $msg = '';
1.13      raeburn  1912:         if ($context eq 'author') {
1.142     bisitz   1913:             $msg = &mt('There are no co-authors to display.');
1.3       raeburn  1914:         } elsif ($context eq 'domain') {
                   1915:             if ($env{'form.roletype'} eq 'domain') {
1.142     bisitz   1916:                 $msg = &mt('There are no users with domain roles to display.');
1.13      raeburn  1917:             } elsif ($env{'form.roletype'} eq 'author') {
1.142     bisitz   1918:                 $msg = &mt('There are no authors or co-authors to display.');
1.3       raeburn  1919:             } elsif ($env{'form.roletype'} eq 'course') {
1.142     bisitz   1920:                 $msg = &mt('There are no course users to display');
1.101     raeburn  1921:             } elsif ($env{'form.roletype'} eq 'community') {
1.142     bisitz   1922:                 $msg = &mt('There are no community users to display');
1.2       raeburn  1923:             }
1.3       raeburn  1924:         } elsif ($context eq 'course') {
                   1925:             $r->print(&mt('There are no course users to display.')."\n");
                   1926:         }
1.148     bisitz   1927:         $r->print('<p class="LC_info">'.$msg.'</p>'."\n") if $msg;
1.3       raeburn  1928:     } else {
                   1929:         # Print out the available choices
1.4       raeburn  1930:         my $usercount;
1.3       raeburn  1931:         if ($env{'form.action'} eq 'modifystudent') {
1.10      raeburn  1932:             ($usercount) = &show_users_list($r,$context,'view',$permission,
1.150     raeburn  1933:                                  $env{'form.Status'},\%userlist,$keylist,'',
                   1934:                                  $showcredits);
1.1       raeburn  1935:         } else {
1.4       raeburn  1936:             ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1.150     raeburn  1937:                                $permission,$env{'form.Status'},\%userlist,
1.159     raeburn  1938:                                $keylist,'',$showcredits,$needauthorquota,$needauthorusage);
1.4       raeburn  1939:         }
                   1940:         if (!$usercount) {
1.142     bisitz   1941:             $r->print('<br /><span class="LC_info">'
1.72      bisitz   1942:                      .&mt('There are no users matching the search criteria.')
                   1943:                      .'</span>'
                   1944:             ); 
1.2       raeburn  1945:         }
                   1946:     }
1.10      raeburn  1947:     $r->print('<input type="hidden" name="phase" value="'.
                   1948:               $env{'form.phase'}.'" /></form>');
1.140     raeburn  1949:     return;
1.2       raeburn  1950: }
                   1951: 
1.53      raeburn  1952: sub role_filter {
                   1953:     my ($context) = @_;
                   1954:     my $output;
                   1955:     my $roleselected = '';
                   1956:     if ($env{'form.showrole'} eq 'Any') {
1.91      bisitz   1957:        $roleselected = ' selected="selected"';
1.53      raeburn  1958:     }
                   1959:     my ($role_select);
                   1960:     if ($context eq 'domain') {
                   1961:         $role_select = &domain_roles_select();
1.140     raeburn  1962:         $output = '<span class="LC_nobreak">'
1.70      bisitz   1963:                  .&mt('Role Type: [_1]',$role_select)
1.140     raeburn  1964:                  .'</span>';
1.53      raeburn  1965:     } else {
1.140     raeburn  1966:         $role_select = '<select name="showrole" onchange="javascript:updateCols('."'showrole'".');">'."\n".
1.53      raeburn  1967:                        '<option value="Any" '.$roleselected.'>'.
                   1968:                        &mt('Any role').'</option>';
1.101     raeburn  1969:         my ($roletype,$crstype);
                   1970:         if ($context eq 'course') {
                   1971:             $crstype = &Apache::loncommon::course_type();
                   1972:             if ($crstype eq 'Community') {
                   1973:                 $roletype = 'community';
                   1974:             } else {
                   1975:                 $roletype = 'course';
                   1976:             } 
                   1977:         }
                   1978:         my @poss_roles = &curr_role_permissions($context,'','',$roletype);
1.53      raeburn  1979:         foreach my $role (@poss_roles) {
                   1980:             $roleselected = '';
                   1981:             if ($role eq $env{'form.showrole'}) {
1.91      bisitz   1982:                 $roleselected = ' selected="selected"';
1.53      raeburn  1983:             }
                   1984:             my $plrole;
                   1985:             if ($role eq 'cr') {
                   1986:                 $plrole = &mt('Custom role');
                   1987:             } else {
1.101     raeburn  1988:                 $plrole=&Apache::lonnet::plaintext($role,$crstype);
1.53      raeburn  1989:             }
                   1990:             $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
                   1991:         }
                   1992:         $role_select .= '</select>';
1.140     raeburn  1993:         $output = '<span class="LC_nobreak">'
1.70      bisitz   1994:                  .&mt('Role: [_1]',$role_select)
1.140     raeburn  1995:                  .'</span>';
1.53      raeburn  1996:     }
                   1997:     return $output;
                   1998: }
                   1999: 
1.33      raeburn  2000: sub section_group_filter {
                   2001:     my ($cnum,$cdom) = @_;
                   2002:     my @filters;
                   2003:     if ($env{'request.course.sec'} eq '') {
                   2004:         @filters = ('sec');
                   2005:     }
                   2006:     push(@filters,'grp');
                   2007:     my %name = (
                   2008:                  sec => 'secfilter',
                   2009:                  grp => 'grpfilter',
                   2010:                );
                   2011:     my %title = &Apache::lonlocal::texthash (
                   2012:                                               sec  => 'Section(s)',
                   2013:                                               grp  => 'Group(s)',
                   2014:                                               all  => 'all',
                   2015:                                               none => 'none',
                   2016:                                             );
1.47      raeburn  2017:     my $output;
1.33      raeburn  2018:     foreach my $item (@filters) {
1.47      raeburn  2019:         my ($markup,@options); 
1.33      raeburn  2020:         if ($env{'form.'.$name{$item}} eq '') {
                   2021:             $env{'form.'.$name{$item}} = 'all';
                   2022:         }
                   2023:         if ($item eq 'sec') {
1.103     raeburn  2024:             if (($env{'form.showrole'} eq 'cc') || ($env{'form.showrole'} eq 'co')) {
1.33      raeburn  2025:                 $env{'form.'.$name{$item}} = 'none';
                   2026:             }
                   2027:             my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   2028:             @options = sort(keys(%sections_count));
                   2029:         } elsif ($item eq 'grp') {
                   2030:             my %curr_groups = &Apache::longroup::coursegroups();
                   2031:             @options = sort(keys(%curr_groups));
                   2032:         }
                   2033:         if (@options > 0) {
                   2034:             my $currsel;
1.112     bisitz   2035:             $markup = '<select name="'.$name{$item}.'">'."\n";
1.33      raeburn  2036:             foreach my $option ('all','none',@options) { 
                   2037:                 $currsel = '';
                   2038:                 if ($env{'form.'.$name{$item}} eq $option) {
1.96      bisitz   2039:                     $currsel = ' selected="selected"';
1.33      raeburn  2040:                 }
                   2041:                 $markup .= ' <option value="'.$option.'"'.$currsel.'>';
                   2042:                 if (($option eq 'all') || ($option eq 'none')) {
                   2043:                     $markup .= $title{$option};
                   2044:                 } else {
                   2045:                     $markup .= $option;
                   2046:                 }   
                   2047:                 $markup .= '</option>'."\n";
                   2048:             }
                   2049:             $markup .= '</select>'."\n";
1.111     bisitz   2050:             $output .= ('&nbsp;'x3).'<span class="LC_nobreak">'
                   2051:                       .'<label>'.$title{$item}.': '.$markup.'</label>'
                   2052:                       .'</span> ';
1.33      raeburn  2053:         }
                   2054:     }
                   2055:     return $output;
                   2056: }
                   2057: 
1.140     raeburn  2058: sub infocolumns {
1.150     raeburn  2059:     my ($context,$mode,$showcredits) = @_;
1.140     raeburn  2060:     my @cols;
                   2061:     if (($mode eq 'pickauthor') || ($mode eq 'autoenroll')) {
1.150     raeburn  2062:         @cols = &get_cols_array($context,$mode,$showcredits);
1.140     raeburn  2063:     } else {
1.150     raeburn  2064:         my @posscols = &get_cols_array($context,$mode,$showcredits);
1.140     raeburn  2065:         if ($env{'form.phase'} ne '') {
                   2066:             my @checkedcols = &Apache::loncommon::get_env_multiple('form.showcol');
                   2067:             foreach my $col (@checkedcols) {
                   2068:                 if (grep(/^$col$/,@posscols)) {
                   2069:                     push(@cols,$col);
                   2070:                 }
                   2071:             }
                   2072:         } else {
                   2073:             @cols = @posscols;
                   2074:         }
                   2075:     }
                   2076:     return @cols;
                   2077: }
                   2078: 
                   2079: sub get_cols_array {
1.150     raeburn  2080:     my ($context,$mode,$showcredits) = @_;
1.140     raeburn  2081:     my @cols;
                   2082:     if ($mode eq 'pickauthor') {
                   2083:         @cols = ('username','fullname','status','email');
                   2084:     } else {
                   2085:         @cols = ('username','domain','id','fullname');
                   2086:         if ($context eq 'course') {
                   2087:             push(@cols,'section');
                   2088:         }
                   2089:         push(@cols,('start','end','role'));
                   2090:         unless (($mode eq 'autoenroll') && ($env{'form.Status'} ne 'Any')) {
                   2091:             push(@cols,'status');
                   2092:         }
                   2093:         if ($context eq 'course') {
                   2094:             push(@cols,'groups');
                   2095:         }
                   2096:         push(@cols,'email');
                   2097:         if (($context eq 'course') && ($mode ne 'autoenroll')) {
1.150     raeburn  2098:             if ($showcredits) {
                   2099:                 push(@cols,'credits');
                   2100:             }
1.140     raeburn  2101:             push(@cols,'lastlogin','clicker');
                   2102:         }
                   2103:         if (($context eq 'course') && ($mode ne 'autoenroll') &&
                   2104:             ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'})) {
1.156     raeburn  2105:             push(@cols,'photo');
1.140     raeburn  2106:         }
1.149     raeburn  2107:         if ($context eq 'domain') {
1.184.4.10.2.  (raeburn 2108:):             push(@cols,('authorusage','authorquota','extent'));
                   2109:):         }
                   2110:):         if ($context eq 'author') {
                   2111:):             push(@cols,'manager');
1.149     raeburn  2112:         }
1.140     raeburn  2113:     }
                   2114:     return @cols;
                   2115: }
                   2116: 
                   2117: sub column_checkboxes {
1.184.4.10.2.  (raeburn 2118:):     my ($context,$mode,$formname,$showcredits,$showstart,$showend) = @_;
1.150     raeburn  2119:     my @cols = &get_cols_array($context,$mode,$showcredits);
1.140     raeburn  2120:     my @showncols = &Apache::loncommon::get_env_multiple('form.showcol');
                   2121:     my (%disabledchk,%unchecked);
                   2122:     if ($env{'form.phase'} eq '') {
                   2123:         $disabledchk{'status'} = 1;
                   2124:         if ($context eq 'course') {
                   2125:             $disabledchk{'role'} = 1;
                   2126:             $unchecked{'photo'} = 1;
1.149     raeburn  2127:             $unchecked{'clicker'} = 1;
1.150     raeburn  2128:             if ($showcredits) {
                   2129:                 $unchecked{'credits'} = 1;
                   2130:             }
1.184.4.10.2.  (raeburn 2131:):             my %curr_groups = &Apache::longroup::coursegroups();
                   2132:):             unless (keys(%curr_groups)) {
                   2133:):                 $unchecked{'groups'} = 1;
                   2134:):             }
                   2135:):         } elsif ($context eq 'domain') {
1.149     raeburn  2136:             $unchecked{'extent'} = 1; 
1.140     raeburn  2137:         }
1.184.4.10.2.  (raeburn 2138:):         if ($showstart) {
                   2139:):             $unchecked{'lastlogin'} = 1;
                   2140:):         } else {
                   2141:):             $unchecked{'start'} = 1;
                   2142:):         }
                   2143:):         unless ($showend) {
                   2144:):             $unchecked{'end'} = 1;
                   2145:):         }
1.140     raeburn  2146:     } else {
                   2147:         if ($env{'form.Status'} ne 'Any') {
                   2148:             $disabledchk{'status'} = 1;
                   2149:         }
1.146     raeburn  2150:         if (($env{'form.showrole'} ne 'Any') && ($env{'form.showrole'} ne 'cr')) {
                   2151:             $disabledchk{'role'} = 1;
1.140     raeburn  2152:         }
1.149     raeburn  2153:         if ($context eq 'domain') {
                   2154:             if (($env{'form.roletype'} eq 'course') || 
                   2155:                 ($env{'form.roletype'} eq 'community')) {
                   2156:                 $disabledchk{'status'} = 1;
1.159     raeburn  2157:                 $disabledchk{'authorusage'} = 1;
                   2158:                 $disabledchk{'authorquota'} = 1;
1.149     raeburn  2159:             } elsif ($env{'form.roletype'} eq 'domain') {
                   2160:                 $disabledchk{'extent'} = 1; 
                   2161:             }
1.184.4.10.2.  (raeburn 2162:):         } elsif ($context eq 'author') {
                   2163:):             if (($env{'form.Status'} eq 'Expired') ||
                   2164:):                 ($env{'form.showrole'} eq 'aa')) {
                   2165:):                 $disabledchk{'manager'} = 1;
                   2166:):             }
1.149     raeburn  2167:         }
1.140     raeburn  2168:     }
                   2169:     my $numposs = scalar(@cols);
1.149     raeburn  2170:     my $numinrow = 7;
1.140     raeburn  2171:     my %lt = &get_column_names($context);
                   2172:     my $output = '<fieldset><legend>'.&mt('Information to show').'</legend>'."\n".'<span class="LC_nobreak">'.
                   2173:                  '<input type="button" onclick="javascript:checkAll(document.'.$formname.'.showcol);" value="'.&mt('check all').'" />'.
                   2174:                  ('&nbsp;'x3).
                   2175:                  '<input type="button" onclick="javascript:uncheckAll(document.'.$formname.'.showcol);" value="'.&mt('uncheck all').'" />'.
                   2176:                  '</span><table>';
                   2177:     
                   2178:     for (my $i=0; $i<$numposs; $i++) {
                   2179:         my $rem = $i%($numinrow);
                   2180:         if ($rem == 0) {
                   2181:             if ($i > 0) {
                   2182:                 $output .= '</tr>';
                   2183:             }
                   2184:             $output .= '<tr>';
                   2185:         }
                   2186:         my $checked;
                   2187:         if ($env{'form.phase'} eq '') {
                   2188:             $checked = ' checked="checked"';
                   2189:             if ($unchecked{$cols[$i]}) { 
                   2190:                $checked = '';
                   2191:             }
                   2192:             if ($disabledchk{$cols[$i]}) {
                   2193:                 $checked = ' disabled="disabled"';
                   2194:             }
                   2195:         } elsif (grep(/^\Q$cols[$i]\E$/,@showncols)) {
                   2196:             $checked = ' checked="checked"';
                   2197:         } elsif ($disabledchk{$cols[$i]}) {
                   2198:             $checked = ' disabled="disabled"';
                   2199:         }
                   2200:         if ($i == $numposs-1) {
                   2201:             my $colsleft = $numinrow-$rem;
                   2202:             if ($colsleft > 1) {
                   2203:                 $output .= '<td colspan="'.$colsleft.'">';
                   2204:             } else {
                   2205:                 $output .= '<td>';
                   2206:             }
                   2207:         } else {
                   2208:             $output .= '<td>';
                   2209:         }
1.149     raeburn  2210:         my $style;
                   2211:         if ($cols[$i] eq 'extent') {
                   2212:             if (($env{'form.roletype'} eq 'domain') || ($env{'form.roletype'} eq '')) {
                   2213:                 $style = ' style="display: none;"';
                   2214:             } 
1.159     raeburn  2215:         } elsif (($cols[$i] eq 'authorusage') || ($cols[$i] eq 'authorquota')) {
                   2216:             if ($env{'form.roletype'} ne 'domain') {
                   2217:                 $style = ' style="display: none;"';
                   2218:             }
                   2219:         }
1.149     raeburn  2220:         $output .= '<span id="show'.$cols[$i].'"'.$style.'><label>'.
                   2221:                    '<input id="showcol'.$cols[$i].'" type="checkbox" name="showcol" value="'.$cols[$i].'"'.$checked.' /><span id="showcoltext'.$cols[$i].'">'.
                   2222:                    $lt{$cols[$i]}.'</span>'.
                   2223:                    '</label></span></td>';
1.140     raeburn  2224:     }
                   2225:     $output .= '</tr></table></fieldset>';
                   2226:     return $output;
                   2227: }
                   2228: 
1.2       raeburn  2229: sub list_submit_button {
                   2230:     my ($text) = @_;
1.11      raeburn  2231:     return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1.2       raeburn  2232: }
                   2233: 
1.140     raeburn  2234: sub get_column_names {
                   2235:     my ($context) = @_;
                   2236:     my %lt = &Apache::lonlocal::texthash(
                   2237:         'username'   => "username",
                   2238:         'domain'     => "domain",
                   2239:         'id'         => 'ID',
                   2240:         'fullname'   => "name",
                   2241:         'section'    => "section",
                   2242:         'groups'     => "active groups",
                   2243:         'start'      => "start date",
                   2244:         'end'        => "end date",
                   2245:         'status'     => "status",
                   2246:         'role'       => "role",
1.150     raeburn  2247:         'credits'    => "credits",
1.140     raeburn  2248:         'type'       => "enroll type/action",
                   2249:         'email'      => "e-mail address",
                   2250:         'photo'      => "photo",
                   2251:         'lastlogin'  => "last login",
                   2252:         'extent'     => "extent",
1.159     raeburn  2253:         'authorusage' => "disk usage (%)",
                   2254:         'authorquota' => "disk quota (MB)",
1.140     raeburn  2255:         'ca'         => "check all",
                   2256:         'ua'         => "uncheck all",
                   2257:         'clicker'    => "clicker-ID",
1.184.4.10.2.  (raeburn 2258:):         'manager'    => "co-author manager",
1.140     raeburn  2259:     );
                   2260:     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
1.149     raeburn  2261:         $lt{'extent'} = &mt('course(s): description, section(s), status');
1.140     raeburn  2262:     } elsif ($context eq 'domain' && $env{'form.roletype'} eq 'community') {
1.154     raeburn  2263:         $lt{'extent'} = &mt('community(s): description, section(s), status');
1.149     raeburn  2264:     } elsif (($context eq 'author') || 
                   2265:              ($context eq 'domain' && $env{'form.roletype'} eq 'author')) {
                   2266:         $lt{'extent'} = &mt('author');
1.140     raeburn  2267:     }
                   2268:     return %lt;
                   2269: }
                   2270: 
1.2       raeburn  2271: sub gather_userinfo {
1.11      raeburn  2272:     my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1.52      raeburn  2273:     my $viewablesec;
                   2274:     if ($context eq 'course') {
                   2275:         $viewablesec = &viewable_section($permission);
                   2276:     }
1.2       raeburn  2277:     foreach my $item (keys(%{$rolehash})) {
                   2278:         my %userdata;
1.22      raeburn  2279:         if ($context eq 'author') { 
1.2       raeburn  2280:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   2281:                 split(/:/,$item);
                   2282:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1.24      raeburn  2283:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   2284:                                $item,$userlist);
1.22      raeburn  2285:         } elsif ($context eq 'course') {
                   2286:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'},
                   2287:              $userdata{'section'}) = split(/:/,$item,-1);
                   2288:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
                   2289:             if (($viewablesec ne '') && ($userdata{'section'} ne '')) {
                   2290:                 next if ($viewablesec ne $userdata{'section'});
                   2291:             }
1.24      raeburn  2292:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   2293:                                $item,$userlist);
1.2       raeburn  2294:         } elsif ($context eq 'domain') {
                   2295:             if ($env{'form.roletype'} eq 'domain') {
                   2296:                 ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
                   2297:                     split(/:/,$item);
                   2298:                 ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1.24      raeburn  2299:                 &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   2300:                                    $item,$userlist);
1.13      raeburn  2301:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2302:                 if (ref($rolehash->{$item}) eq 'HASH') {
                   2303:                     $userdata{'extent'} = $item;
                   2304:                     foreach my $key (keys(%{$rolehash->{$item}})) {
                   2305:                         ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =  split(/:/,$key);
                   2306:                         ($userdata{'start'},$userdata{'end'}) = 
                   2307:                             split(/:/,$rolehash->{$item}{$key});
                   2308:                         my $uniqid = $key.':'.$item;
1.25      raeburn  2309:                         &build_user_record($context,\%userdata,$userinfo,
                   2310:                                            $indexhash,$uniqid,$userlist);
1.2       raeburn  2311:                     }
                   2312:                 }
1.102     raeburn  2313:             } elsif (($env{'form.roletype'} eq 'course') || 
                   2314:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  2315:                 ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   2316:                     split(/:/,$item);
                   2317:                 if (ref($rolehash->{$item}) eq 'HASH') {
1.11      raeburn  2318:                     my $numcids = keys(%{$rolehash->{$item}});
1.2       raeburn  2319:                     foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
                   2320:                         if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
                   2321:                             my $spanstart = '';
                   2322:                             my $spanend = '; ';
                   2323:                             my $space = ', ';
                   2324:                             if ($format eq 'html' || $format eq 'view') {
                   2325:                                 $spanstart = '<span class="LC_nobreak">';
1.23      raeburn  2326:                                 # FIXME: actions on courses disabled for now
                   2327: #                                if ($permission->{'cusr'}) {
                   2328: #                                    if ($numcids > 1) {
1.25      raeburn  2329: #                                        $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  2330: #                                    } else {
1.25      raeburn  2331: #                                        $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  2332: #                                    }
                   2333: #                                }
1.2       raeburn  2334:                                 $spanend = '</span><br />';
                   2335:                                 $space = ',&nbsp;';
                   2336:                             }
                   2337:                             $userdata{'extent'} .= $spanstart.
                   2338:                                     $rolehash->{$item}{$cid}{'desc'}.$space;
                   2339:                             if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') { 
                   2340:                                 foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1.25      raeburn  2341:                                     if (($env{'form.Status'} eq 'Any') ||
                   2342:                                         ($env{'form.Status'} eq $rolehash->{$item}{$cid}{'secs'}{$sec})) {
                   2343:                                         $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
                   2344:                                         $userdata{'status'} = $rolehash->{$item}{$cid}{'secs'}{$sec};
                   2345:                                     }
1.2       raeburn  2346:                                 }
                   2347:                             }
                   2348:                         }
                   2349:                     }
                   2350:                 }
1.25      raeburn  2351:                 if ($userdata{'status'} ne '') {
                   2352:                     &build_user_record($context,\%userdata,$userinfo,
                   2353:                                        $indexhash,$item,$userlist);
                   2354:                 }
1.2       raeburn  2355:             }
                   2356:         }
                   2357:     }
                   2358:     return;
                   2359: }
                   2360: 
                   2361: sub build_user_record {
1.24      raeburn  2362:     my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11      raeburn  2363:     next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.102     raeburn  2364:     if (!(($context eq 'domain') && (($env{'form.roletype'} eq 'course')
                   2365:                              && ($env{'form.roletype'} eq 'community')))) {
1.24      raeburn  2366:         &process_date_info($userdata);
                   2367:     }
1.2       raeburn  2368:     my $username = $userdata->{'username'};
                   2369:     my $domain = $userdata->{'domain'};
                   2370:     if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1.24      raeburn  2371:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1.2       raeburn  2372:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   2373:     } else {
                   2374:         &aggregate_user_info($domain,$username,$userinfo);
                   2375:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
                   2376:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   2377:     }
                   2378:     foreach my $key (keys(%{$indexhash})) {
                   2379:         if (defined($userdata->{$key})) {
                   2380:             $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
                   2381:         }
                   2382:     }
                   2383:     return;
                   2384: }
                   2385: 
                   2386: sub courses_selector {
                   2387:     my ($cdom,$formname) = @_;
                   2388:     my %codes = ();
                   2389:     my @codetitles = ();
                   2390:     my %cat_titles = ();
                   2391:     my %cat_order = ();
                   2392:     my %idlist = ();
                   2393:     my %idnums = ();
                   2394:     my %idlist_titles = ();
                   2395:     my $caller = 'global';
                   2396:     my $format_reply;
                   2397:     my $jscript = '';
                   2398: 
1.7       albertel 2399:     my $totcodes = 0;
1.184.4.3  raeburn  2400:     my $instcats = &Apache::lonnet::get_dom_instcats($cdom);
                   2401:     if (ref($instcats) eq 'HASH') {
                   2402:         if ((ref($instcats->{'codetitles'}) eq 'ARRAY') && (ref($instcats->{'codes'}) eq 'HASH') &&
                   2403:             (ref($instcats->{'cat_titles'}) eq 'HASH') && (ref($instcats->{'cat_order'}) eq 'HASH')) {
                   2404:             %codes = %{$instcats->{'codes'}};
                   2405:             @codetitles = @{$instcats->{'codetitles'}};
                   2406:             %cat_titles = %{$instcats->{'cat_titles'}};
                   2407:             %cat_order = %{$instcats->{'cat_order'}};
                   2408:             $totcodes = scalar(keys(%codes));
1.2       raeburn  2409:             my $numtypes = @codetitles;
                   2410:             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
                   2411:             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
                   2412:             my $longtitles_str = join('","',@{$longtitles});
                   2413:             my $allidlist = $idlist{$codetitles[0]};
                   2414:             $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
                   2415:             $jscript .= $scripttext;
1.181     raeburn  2416:             $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,\@codetitles);
1.2       raeburn  2417:         }
                   2418:     }
                   2419:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
                   2420: 
                   2421:     my %elements = (
                   2422:                      Year => 'selectbox',
                   2423:                      coursepick => 'radio',
                   2424:                      coursetotal => 'text',
                   2425:                      courselist => 'text',
                   2426:                    );
                   2427:     $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   2428:     if ($env{'form.coursepick'} eq 'category') {
                   2429:         $jscript .= qq|
                   2430: function setCourseCat(formname) {
                   2431:     if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
                   2432:         return;
                   2433:     }
1.120     raeburn  2434:     courseSet('$codetitles[0]');
1.2       raeburn  2435:     for (var j=0; j<formname.Semester.length; j++) {
                   2436:         if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
                   2437:             formname.Semester.options[j].selected = true;
                   2438:         }
                   2439:     }
                   2440:     if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
                   2441:         return;
                   2442:     }
1.120     raeburn  2443:     courseSet('$codetitles[1]');
1.2       raeburn  2444:     for (var j=0; j<formname.Department.length; j++) {
1.184.4.3  raeburn  2445:         if (formname.Department.options[j].value == "$env{'form.Department'}") {
                   2446:             formname.Department.options[j].selected = true;
1.2       raeburn  2447:         }
                   2448:     }
                   2449:     if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
                   2450:         return;
                   2451:     }
1.120     raeburn  2452:     courseSet('$codetitles[2]');
1.2       raeburn  2453:     for (var j=0; j<formname.Number.length; j++) {
                   2454:         if (formname.Number.options[j].value == "$env{'form.Number'}") {
                   2455:             formname.Number.options[j].selected = true;
                   2456:         }
                   2457:     }
                   2458: }
                   2459: |;
                   2460:     }
                   2461:     return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
                   2462:             \%idlist_titles);
                   2463: }
                   2464: 
                   2465: sub course_selector_loadcode {
                   2466:     my ($formname) = @_;
                   2467:     my $loadcode;
                   2468:     if ($env{'form.coursepick'} ne '') {
                   2469:         $loadcode = 'javascript:setFormElements(document.'.$formname.')';
                   2470:         if ($env{'form.coursepick'} eq 'category') {
                   2471:             $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
                   2472:         }
                   2473:     }
                   2474:     return $loadcode;
                   2475: }
                   2476: 
                   2477: sub process_coursepick {
                   2478:     my $coursefilter = $env{'form.coursepick'};
                   2479:     my $cdom = $env{'request.role.domain'};
                   2480:     my %courses;
1.105     raeburn  2481:     my $crssrch = 'Course';
                   2482:     if ($env{'form.roletype'} eq 'community') {
                   2483:         $crssrch = 'Community';
                   2484:     }
1.2       raeburn  2485:     if ($coursefilter eq 'all') {
                   2486:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
1.105     raeburn  2487:                                                  undef,undef,$crssrch);
1.2       raeburn  2488:     } elsif ($coursefilter eq 'category') {
                   2489:         my $instcode = &instcode_from_coursefilter();
                   2490:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
1.105     raeburn  2491:                                                  undef,undef,$crssrch);
1.2       raeburn  2492:     } elsif ($coursefilter eq 'specific') {
                   2493:         if ($env{'form.coursetotal'} > 1) {
                   2494:             my @course_ids = split(/&&/,$env{'form.courselist'});
                   2495:             foreach my $cid (@course_ids) {
                   2496:                 $courses{$cid} = '';
1.1       raeburn  2497:             }
1.2       raeburn  2498:         } else {
                   2499:             $courses{$env{'form.courselist'}} = '';
1.1       raeburn  2500:         }
1.2       raeburn  2501:     }
                   2502:     return %courses;
                   2503: }
                   2504: 
                   2505: sub instcode_from_coursefilter {
                   2506:     my $instcode = '';
                   2507:     my @cats = ('Semester','Year','Department','Number');
                   2508:     foreach my $category (@cats) {
                   2509:         if (defined($env{'form.'.$category})) {
                   2510:             unless ($env{'form.'.$category} eq '-1') {
                   2511:                 $instcode .= $env{'form.'.$category};
                   2512:            }
                   2513:         }
                   2514:     }
                   2515:     if ($instcode eq '') {
                   2516:         $instcode = '.';
                   2517:     }
                   2518:     return $instcode;
                   2519: }
                   2520: 
                   2521: sub make_keylist_array {
                   2522:     my ($index,$keylist);
                   2523:     $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
                   2524:     $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
                   2525:     $index->{'end'} = &Apache::loncoursedata::CL_END();
                   2526:     $index->{'start'} = &Apache::loncoursedata::CL_START();
                   2527:     $index->{'id'} = &Apache::loncoursedata::CL_ID();
                   2528:     $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
                   2529:     $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
                   2530:     $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
                   2531:     $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
                   2532:     $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
                   2533:     $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
                   2534:     $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
                   2535:     $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
                   2536:     $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
1.44      raeburn  2537:     $index->{'photo'} = &Apache::loncoursedata::CL_PHOTO();
1.47      raeburn  2538:     $index->{'thumbnail'} = &Apache::loncoursedata::CL_THUMBNAIL();
1.150     raeburn  2539:     $index->{'credits'} = &Apache::loncoursedata::CL_CREDITS();
1.174     raeburn  2540:     $index->{'instsec'} = &Apache::loncoursedata::CL_INSTSEC();
1.159     raeburn  2541:     $index->{'authorquota'} = &Apache::loncoursedata::CL_AUTHORQUOTA();
                   2542:     $index->{'authorusage'} = &Apache::loncoursedata::CL_AUTHORUSAGE();
1.184.4.10.2.  (raeburn 2543:):     $index->{'manager'} = &Apache::loncoursedata::CL_CAMANAGER();
1.2       raeburn  2544:     foreach my $key (keys(%{$index})) {
                   2545:         $keylist->[$index->{$key}] = $key;
                   2546:     }
                   2547:     return ($index,$keylist);
                   2548: }
                   2549: 
                   2550: sub aggregate_user_info {
                   2551:     my ($udom,$uname,$userinfo) = @_;
                   2552:     my %info=&Apache::lonnet::get('environment',
                   2553:                                   ['firstname','middlename',
                   2554:                                    'lastname','generation','id'],
                   2555:                                    $udom,$uname);
                   2556:     my ($tmp) = keys(%info);
                   2557:     my ($fullname,$id);
                   2558:     if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   2559:         $fullname = 'not available';
                   2560:         $id = 'not available';
                   2561:         &Apache::lonnet::logthis('unable to retrieve environment '.
                   2562:                                  'for '.$uname.':'.$udom);
1.1       raeburn  2563:     } else {
1.2       raeburn  2564:         $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
                   2565:         $id = $info{'id'};
                   2566:     }
                   2567:     $userinfo->{$uname.':'.$udom} = { 
                   2568:                                       fullname => $fullname,
                   2569:                                       id       => $id,
                   2570:                                     };
                   2571:     return;
                   2572: }
1.1       raeburn  2573: 
1.2       raeburn  2574: sub process_date_info {
                   2575:     my ($userdata) = @_;
                   2576:     my $now = time;
1.83      raeburn  2577:     $userdata->{'status'} = 'Active';
1.2       raeburn  2578:     if ($userdata->{'start'} > 0) {
                   2579:         if ($now < $userdata->{'start'}) {
1.83      raeburn  2580:             $userdata->{'status'} = 'Future';
1.2       raeburn  2581:         }
1.1       raeburn  2582:     }
1.2       raeburn  2583:     if ($userdata->{'end'} > 0) {
                   2584:         if ($now > $userdata->{'end'}) {
1.83      raeburn  2585:             $userdata->{'status'} = 'Expired';
1.2       raeburn  2586:         }
                   2587:     }
                   2588:     return;
1.1       raeburn  2589: }
                   2590: 
                   2591: sub show_users_list {
1.150     raeburn  2592:     my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist,$formname,
1.159     raeburn  2593:         $showcredits,$needauthorquota,$needauthorusage)=@_;
1.55      raeburn  2594:     if ($formname eq '') {
                   2595:         $formname = 'studentform';
                   2596:     }
1.159     raeburn  2597:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.1       raeburn  2598:     #
                   2599:     # Variables for excel output
                   2600:     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
                   2601:     #
                   2602:     # Variables for csv output
                   2603:     my ($CSVfile,$CSVfilename);
                   2604:     #
                   2605:     my $sortby = $env{'form.sortby'};
1.3       raeburn  2606:     my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2       raeburn  2607:     if ($context eq 'course') {
1.3       raeburn  2608:         push(@sortable,('section','groups','type'));
1.150     raeburn  2609:         if ($showcredits) {
                   2610:             push(@sortable,'credits');
                   2611:         }
1.2       raeburn  2612:     } else {
1.3       raeburn  2613:         push(@sortable,'extent');
1.159     raeburn  2614:         if (($context eq 'domain') && ($env{'form.roletype'} eq 'domain') &&
                   2615:             (($env{'form.showrole'} eq 'Any') || ($env{'form.showrole'} eq 'au'))) {
                   2616:             push(@sortable,('authorusage','authorquota'));
                   2617:         }
1.184.4.10.2.  (raeburn 2618:):         if ($context eq 'author') {
                   2619:):             push(@sortable,'manager');
                   2620:):         }
1.3       raeburn  2621:     }
1.55      raeburn  2622:     if ($mode eq 'pickauthor') {
                   2623:         @sortable = ('username','fullname','email','status');
                   2624:     }
1.156     raeburn  2625:     my %is_sortable;
1.158     raeburn  2626:     map { $is_sortable{$_} = 1; } @sortable;
1.156     raeburn  2627:     unless ($is_sortable{$sortby}) {
1.3       raeburn  2628:         $sortby = 'username';
1.1       raeburn  2629:     }
1.22      raeburn  2630:     my $setting = $env{'form.roletype'};
1.150     raeburn  2631:     my ($cid,$cdom,$cnum,$classgroups,$crstype,$defaultcredits);
1.1       raeburn  2632:     if ($context eq 'course') {
1.22      raeburn  2633:         $cid = $env{'request.course.id'};
1.101     raeburn  2634:         $crstype = &Apache::loncommon::course_type();
1.17      raeburn  2635:         ($cnum,$cdom) = &get_course_identity($cid);
1.150     raeburn  2636:         $defaultcredits = $env{'course.'.$cid.'.internal.defaultcredits'};
1.2       raeburn  2637:         ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   2638:                                      $userlist,$keylist,$cdom,$cnum);
1.16      raeburn  2639:         if ($mode eq 'autoenroll') {
                   2640:             $env{'form.showrole'} = 'st';
                   2641:         } else {
                   2642:             if ($env{'course.'.$cid.'.internal.showphoto'}) {
                   2643:                 $r->print('
1.1       raeburn  2644: <script type="text/javascript">
1.96      bisitz   2645: // <![CDATA[
1.1       raeburn  2646: function photowindow(photolink) {
                   2647:     var title = "Photo_Viewer";
                   2648:     var options = "scrollbars=1,resizable=1,menubar=0";
                   2649:     options += ",width=240,height=240";
                   2650:     stdeditbrowser = open(photolink,title,options,"1");
                   2651:     stdeditbrowser.focus();
                   2652: }
1.96      bisitz   2653: // ]]>
1.1       raeburn  2654: </script>
1.16      raeburn  2655:                ');
                   2656:             }
                   2657:         }
1.102     raeburn  2658:     } elsif ($context eq 'domain') {
                   2659:         if ($setting eq 'community') {
                   2660:             $crstype = 'Community';
1.105     raeburn  2661:         } elsif ($setting eq 'course') {
1.102     raeburn  2662:             $crstype = 'Course';
                   2663:         }
1.1       raeburn  2664:     }
1.55      raeburn  2665:     if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.40      raeburn  2666:         my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
1.56      raeburn  2667:         my $verify_action_js = &bulkaction_javascript($formname);
1.1       raeburn  2668:         $r->print(<<END);
1.10      raeburn  2669: 
                   2670: <script type="text/javascript" language="Javascript">
1.96      bisitz   2671: // <![CDATA[
1.11      raeburn  2672: 
1.56      raeburn  2673: $verify_action_js
1.10      raeburn  2674: 
                   2675: function username_display_launch(username,domain) {
                   2676:     var target;
1.177     raeburn  2677:     if (!document.$formname.usernamelink.length) {
                   2678:         target = document.$formname.usernamelink.value;
                   2679:     } else {
                   2680:         for (var i=0; i<document.$formname.usernamelink.length; i++) {
                   2681:             if (document.$formname.usernamelink[i].checked) {
                   2682:                target = document.$formname.usernamelink[i].value;
                   2683:             }
1.10      raeburn  2684:         }
                   2685:     }
1.179     raeburn  2686:     if ((target == 'modify') || (target == 'activity')) {
                   2687:         var nextaction = 'singleuser';
                   2688:         if (target == 'activity') {
                   2689:             nextaction = 'accesslogs';
                   2690:         }
1.55      raeburn  2691:         if (document.$formname.userwin.checked == true) {
1.179     raeburn  2692:             var url = '/adm/createuser?srchterm='+username+'&srchdomain='+domain+'&phase=get_user_info&srchin=dom&srchby=uname&srchtype=exact&popup=1&action='+nextaction;
1.50      raeburn  2693:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2694:             modifywin = window.open(url,'',options,1);
                   2695:             modifywin.focus();
                   2696:             return;
                   2697:         } else {
1.55      raeburn  2698:             document.$formname.srchterm.value=username;
                   2699:             document.$formname.srchdomain.value=domain;
                   2700:             document.$formname.phase.value='get_user_info';
1.179     raeburn  2701:             document.$formname.action.value = nextaction;
1.55      raeburn  2702:             document.$formname.submit();
1.50      raeburn  2703:         }
1.10      raeburn  2704:     }
1.48      raeburn  2705:     if (target == 'aboutme') {
1.55      raeburn  2706:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2707:             var url = '/adm/'+domain+'/'+username+'/aboutme?popup=1';
                   2708:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2709:             aboutmewin = window.open(url,'',options,1);
                   2710:             aboutmewin.focus();
                   2711:             return;
                   2712:         } else {
                   2713:             document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
                   2714:         }
1.48      raeburn  2715:     }
1.98      raeburn  2716:     if (target == 'track') {
                   2717:         if (document.$formname.userwin.checked == true) {
                   2718:             var url = '/adm/trackstudent?selected_student='+username+':'+domain+'&only_body=1';
                   2719:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2720:             var trackwin = window.open(url,'',options,1);
                   2721:             trackwin.focus();
                   2722:             return;
                   2723:         } else {
                   2724:             document.location.href = '/adm/trackstudent?selected_student='+username+':'+domain;
                   2725:         }
                   2726:     }
1.10      raeburn  2727: }
1.96      bisitz   2728: // ]]>
1.10      raeburn  2729: </script>
1.11      raeburn  2730: $date_sec_selector
1.1       raeburn  2731: <input type="hidden" name="state" value="$env{'form.state'}" />
                   2732: END
                   2733:     }
                   2734:     $r->print(<<END);
                   2735: <input type="hidden" name="sortby" value="$sortby" />
                   2736: END
1.150     raeburn  2737:     my @cols = &infocolumns($context,$mode,$showcredits);
1.140     raeburn  2738:     my %coltxt = &get_column_names($context);
                   2739:     my %acttxt = &Apache::lonlocal::texthash(
1.11      raeburn  2740:                        'pr'         => "Proceed",
                   2741:                        'ac'         => "Action to take for selected users",
1.56      raeburn  2742:                        'link'       => "Behavior of clickable username link for each user",
1.82      weissno  2743:                        'aboutme'    => "Display a user's personal information page",
1.50      raeburn  2744:                        'owin'       => "Open in a new window",
1.10      raeburn  2745:                        'modify'     => "Modify a user's information",
1.98      raeburn  2746:                        'track'      => "View a user's recent activity",
1.179     raeburn  2747:                        'activity'   => "View a user's access log", 
1.1       raeburn  2748:                       );
1.140     raeburn  2749:     my %lt = (%coltxt,%acttxt);
1.4       raeburn  2750:     my $rolefilter = $env{'form.showrole'};
1.5       raeburn  2751:     if ($env{'form.showrole'} eq 'cr') {
                   2752:         $rolefilter = &mt('custom');  
                   2753:     } elsif ($env{'form.showrole'} ne 'Any') {
1.101     raeburn  2754:         $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'},$crstype);
1.2       raeburn  2755:     }
1.16      raeburn  2756:     my $results_description;
                   2757:     if ($mode ne 'autoenroll') {
                   2758:         $results_description = &results_header_row($rolefilter,$statusmode,
1.102     raeburn  2759:                                                    $context,$permission,$mode,$crstype);
1.140     raeburn  2760:         $r->print('<b>'.$results_description.'</b><br clear="all" />');
1.16      raeburn  2761:     }
1.26      raeburn  2762:     my ($output,$actionselect,%canchange,%canchangesec);
1.55      raeburn  2763:     if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
                   2764:         if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.16      raeburn  2765:             if ($permission->{'cusr'}) {
1.105     raeburn  2766:                 unless (($context eq 'domain') && 
                   2767:                         (($setting eq 'course') || ($setting eq 'community'))) {
                   2768:                     $actionselect = 
                   2769:                         &select_actions($context,$setting,$statusmode,$formname);
                   2770:                 }
1.16      raeburn  2771:             }
                   2772:             $r->print(<<END);
1.10      raeburn  2773: <input type="hidden" name="srchby"  value="uname" />
                   2774: <input type="hidden" name="srchin"   value="dom" />
                   2775: <input type="hidden" name="srchtype" value="exact" />
                   2776: <input type="hidden" name="srchterm" value="" />
1.11      raeburn  2777: <input type="hidden" name="srchdomain" value="" /> 
1.1       raeburn  2778: END
1.16      raeburn  2779:             if ($actionselect) {
1.41      raeburn  2780:                 $output .= <<"END";
1.94      bisitz   2781: <div class="LC_left_float"><fieldset><legend>$lt{'ac'}</legend>
1.56      raeburn  2782: $actionselect
                   2783: <br/><br /><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.$formname.actionlist)" /> &nbsp;
                   2784: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.$formname.actionlist)" /><br /><input type="button" value="$lt{'pr'}" onclick="javascript:verify_action('actionlist')" /></fieldset></div>
1.11      raeburn  2785: END
1.26      raeburn  2786:                 my @allroles;
                   2787:                 if ($env{'form.showrole'} eq 'Any') {
                   2788:                     my $custom = 1;
                   2789:                     if ($context eq 'domain') {
1.101     raeburn  2790:                         @allroles = &roles_by_context($setting,$custom,$crstype);
1.26      raeburn  2791:                     } else {
1.101     raeburn  2792:                         @allroles = &roles_by_context($context,$custom,$crstype);
1.26      raeburn  2793:                     }
                   2794:                 } else {
                   2795:                     @allroles = ($env{'form.showrole'});
                   2796:                 }
                   2797:                 foreach my $role (@allroles) {
                   2798:                     if ($context eq 'domain') {
                   2799:                         if ($setting eq 'domain') {
                   2800:                             if (&Apache::lonnet::allowed('c'.$role,
                   2801:                                     $env{'request.role.domain'})) {
                   2802:                                 $canchange{$role} = 1;
                   2803:                             }
1.31      raeburn  2804:                         } elsif ($setting eq 'author') {
                   2805:                             if (&Apache::lonnet::allowed('c'.$role,
                   2806:                                     $env{'request.role.domain'})) {
                   2807:                                 $canchange{$role} = 1;
                   2808:                             }
1.26      raeburn  2809:                         }
                   2810:                     } elsif ($context eq 'author') {
                   2811:                         if (&Apache::lonnet::allowed('c'.$role,
                   2812:                             $env{'user.domain'}.'/'.$env{'user.name'})) {
                   2813:                             $canchange{$role} = 1;
                   2814:                         }
                   2815:                     } elsif ($context eq 'course') {
                   2816:                         if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   2817:                             $canchange{$role} = 1;
                   2818:                         } elsif ($env{'request.course.sec'} ne '') {
                   2819:                             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   2820:                                 $canchangesec{$role} = $env{'request.course.sec'};
                   2821:                             }
1.141     raeburn  2822:                         } elsif ((($role eq 'co') && ($crstype eq 'Community')) ||
                   2823:                                  (($role eq 'cc') && ($crstype eq 'Course'))) {
                   2824:                             if (&is_courseowner($env{'request.course.id'},
                   2825:                                                 $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'})) {
                   2826:                                 $canchange{$role} = 1;
                   2827:                             }
1.26      raeburn  2828:                         }
                   2829:                     }
                   2830:                 }
1.16      raeburn  2831:             }
1.94      bisitz   2832:             $output .= '<div class="LC_left_float"><fieldset><legend>'.$lt{'link'}.'</legend>'.
1.56      raeburn  2833:                        '<table><tr>';
                   2834:             my @linkdests = ('aboutme');
                   2835:             if ($permission->{'cusr'}) {
                   2836:                 unshift (@linkdests,'modify');
                   2837:             }
1.179     raeburn  2838:             if ($context eq 'course') {
                   2839:                 if (&Apache::lonnet::allowed('vsa', $env{'request.course.id'}) ||
                   2840:                     &Apache::lonnet::allowed('vsa', $env{'request.course.id'}.'/'.
                   2841:                                              $env{'request.course.sec'})) {
                   2842:                     push(@linkdests,'track');
                   2843:                 }
                   2844:             } elsif ($context eq 'domain') {
                   2845:                 if (&Apache::lonnet::allowed('vac',$env{'request.role.domain'})) {
                   2846:                     push(@linkdests,'activity');
                   2847:                 }
1.98      raeburn  2848:             }
1.56      raeburn  2849:             $output .= '<td>';
                   2850:             my $usernamelink = $env{'form.usernamelink'};
                   2851:             if ($usernamelink eq '') {
                   2852:                 $usernamelink = 'aboutme';
                   2853:             }
                   2854:             foreach my $item (@linkdests) {
                   2855:                 my $checkedstr = '';
                   2856:                 if ($item eq $usernamelink) {
1.86      bisitz   2857:                     $checkedstr = ' checked="checked"';
1.56      raeburn  2858:                 }
1.86      bisitz   2859:                 $output .= '<span class="LC_nobreak"><label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.' />&nbsp;'.$lt{$item}.'</label></span><br />';
1.56      raeburn  2860:             }
                   2861:             my $checkwin;
                   2862:             if ($env{'form.userwin'}) {
1.86      bisitz   2863:                 $checkwin = ' checked="checked"';
1.56      raeburn  2864:             }
1.144     bisitz   2865:             $output .=
                   2866:                 '</td><td valign="top"  style="border-left: 1px solid;">'
                   2867:                .'<span class="LC_nobreak"><label>'
                   2868:                .'<input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}
                   2869:                .'</label></span></td></tr></table></fieldset></div>';
1.4       raeburn  2870:         }
1.184     raeburn  2871:         $output .= "\n".'<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
1.1       raeburn  2872:                   &Apache::loncommon::start_data_table().
1.4       raeburn  2873:                   &Apache::loncommon::start_data_table_header_row();
1.1       raeburn  2874:         if ($mode eq 'autoenroll') {
1.4       raeburn  2875:             $output .= "
1.55      raeburn  2876:  <th><a href=\"javascript:document.$formname.sortby.value='type';document.$formname.submit();\">$lt{'type'}</a></th>
1.4       raeburn  2877:             ";
1.1       raeburn  2878:         } else {
1.105     raeburn  2879:             $output .= "\n".'<th>&nbsp;</th>'."\n";
1.11      raeburn  2880:             if ($actionselect) {
1.159     raeburn  2881:                 $output .= '<th class="LC_nobreak" valign="top">'.&mt('Select').'</th>'."\n";
1.11      raeburn  2882:             }
1.1       raeburn  2883:         }
                   2884:         foreach my $item (@cols) {
1.159     raeburn  2885:             $output .= '<th class="LC_nobreak" valign="top">';
1.156     raeburn  2886:             if ($is_sortable{$item}) {
1.159     raeburn  2887:                 $output .= "<a href=\"javascript:document.$formname.sortby.value='$item';document.$formname.submit();\" style=\"text-decoration:none;\">$lt{$item}<span class=\"LC_fontsize_small\"> &#9660;</span></a>";
1.156     raeburn  2888:             } else {
                   2889:                 $output .= $lt{$item};
                   2890:             }
                   2891:             $output .= "</th>\n";
1.1       raeburn  2892:         }
1.2       raeburn  2893:         my %role_types = &role_type_names();
1.16      raeburn  2894:         $output .= &Apache::loncommon::end_data_table_header_row();
1.1       raeburn  2895: # Done with the HTML header line
                   2896:     } elsif ($mode eq 'csv') {
                   2897:         #
                   2898:         # Open a file
                   2899:         $CSVfilename = '/prtspool/'.
1.2       raeburn  2900:                        $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
                   2901:                        time.'_'.rand(1000000000).'.csv';
1.1       raeburn  2902:         unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
                   2903:             $r->log_error("Couldn't open $CSVfilename for output $!");
1.108     bisitz   2904:             $r->print(
                   2905:                 '<p class="LC_error">'
                   2906:                .&mt('Problems occurred in writing the CSV file.')
                   2907:                .' '.&mt('This error has been logged.')
                   2908:                .' '.&mt('Please alert your LON-CAPA administrator.')
                   2909:                .'</p>'
                   2910:             );
1.1       raeburn  2911:             $CSVfile = undef;
                   2912:         }
                   2913:         #
                   2914:         # Write headers and data to file
1.2       raeburn  2915:         print $CSVfile '"'.$results_description.'"'."\n"; 
1.1       raeburn  2916:         print $CSVfile '"'.join('","',map {
                   2917:             &Apache::loncommon::csv_translate($lt{$_})
1.67      droeschl 2918:             } (@cols))."\"\n";
1.1       raeburn  2919:     } elsif ($mode eq 'excel') {
                   2920:         # Create the excel spreadsheet
                   2921:         ($excel_workbook,$excel_filename,$format) =
                   2922:             &Apache::loncommon::create_workbook($r);
                   2923:         return if (! defined($excel_workbook));
                   2924:         $excel_sheet = $excel_workbook->addworksheet('userlist');
1.2       raeburn  2925:         $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1       raeburn  2926:         #
                   2927:         my @colnames = map {$lt{$_}} (@cols);
1.67      droeschl 2928: 
1.1       raeburn  2929:         $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
                   2930:     }
                   2931: 
                   2932: # Done with header lines in all formats
                   2933:     my %index;
                   2934:     my $i;
1.2       raeburn  2935:     foreach my $idx (@$keylist) {
                   2936:         $index{$idx} = $i++;
                   2937:     }
1.184.4.10.2.  (raeburn 2938:):     my $now = time;
1.4       raeburn  2939:     my $usercount = 0;
1.33      raeburn  2940:     my ($secfilter,$grpfilter);
                   2941:     if ($context eq 'course') {
                   2942:         $secfilter = $env{'form.secfilter'};
                   2943:         $grpfilter = $env{'form.grpfilter'};
                   2944:         if ($secfilter eq '') {
                   2945:             $secfilter = 'all';
                   2946:         }
                   2947:         if ($grpfilter eq '') {
                   2948:             $grpfilter = 'all';
                   2949:         }
                   2950:     }
1.83      raeburn  2951:     my %ltstatus = &Apache::lonlocal::texthash(
                   2952:                                                 Active  => 'Active',
                   2953:                                                 Future  => 'Future',
                   2954:                                                 Expired => 'Expired',
                   2955:                                                );
1.184.4.10.2.  (raeburn 2956:):     my (%crslogins,%camanagers);
1.139     raeburn  2957:     if ($context eq 'course') {
1.184.4.10.2.  (raeburn 2958:):         # If this is for a single course get last course "log-in".
1.139     raeburn  2959:         %crslogins=&Apache::lonnet::dump('nohist_crslastlogin',$cdom,$cnum);
1.184.4.10.2.  (raeburn 2960:):     } elsif ($context eq 'author') {
                   2961:):         map { $camanagers{$_.':ca'} = 1; } split(/,/,$env{'environment.authormanagers'});
1.139     raeburn  2962:     }
1.2       raeburn  2963:     # Get groups, role, permanent e-mail so we can sort on them if
                   2964:     # necessary.
                   2965:     foreach my $user (keys(%{$userlist})) {
1.43      raeburn  2966:         if ($user eq '' ) {
                   2967:             delete($userlist->{$user});
                   2968:             next;
                   2969:         }
1.11      raeburn  2970:         if ($context eq 'domain' &&  $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
                   2971:             delete($userlist->{$user});
                   2972:             next;
                   2973:         }
1.2       raeburn  2974:         my ($uname,$udom,$role,$groups,$email);
1.5       raeburn  2975:         if (($statusmode ne 'Any') && 
                   2976:                  ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
                   2977:             delete($userlist->{$user});
                   2978:             next;
                   2979:         }
1.2       raeburn  2980:         if ($context eq 'domain') {
                   2981:             if ($env{'form.roletype'} eq 'domain') {
                   2982:                 ($role,$uname,$udom) = split(/:/,$user);
1.11      raeburn  2983:                 if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
                   2984:                     ($udom eq $env{'request.role.domain'})) {
                   2985:                     delete($userlist->{$user});
                   2986:                     next;
                   2987:                 }
1.13      raeburn  2988:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2989:                 ($uname,$udom,$role) = split(/:/,$user,-1);
1.102     raeburn  2990:             } elsif (($env{'form.roletype'} eq 'course') || 
                   2991:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  2992:                 ($uname,$udom,$role) = split(/:/,$user);
                   2993:             }
                   2994:         } else {
                   2995:             ($uname,$udom,$role) = split(/:/,$user,-1);
                   2996:             if (($context eq 'course') && $role eq '') {
                   2997:                 $role = 'st';
                   2998:             }
                   2999:         }
                   3000:         $userlist->{$user}->[$index{'role'}] = $role;
                   3001:         if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'}  eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
                   3002:             delete($userlist->{$user});
                   3003:             next;
                   3004:         }
1.33      raeburn  3005:         if ($context eq 'course') {
                   3006:             my @ac_groups;
                   3007:             if (ref($classgroups) eq 'HASH') {
                   3008:                 $groups = $classgroups->{$user};
                   3009:             }
                   3010:             if (ref($groups->{'active'}) eq 'HASH') {
                   3011:                 @ac_groups = keys(%{$groups->{'active'}});
                   3012:                 $userlist->{$user}->[$index{'groups'}] = join(', ',@ac_groups);
                   3013:             }
                   3014:             if ($mode ne 'autoenroll') {
                   3015:                 my $section = $userlist->{$user}->[$index{'section'}];
1.43      raeburn  3016:                 if (($env{'request.course.sec'} ne '') && 
                   3017:                     ($section ne $env{'request.course.sec'})) {
                   3018:                     if ($role eq 'st') {
                   3019:                         delete($userlist->{$user});
                   3020:                         next;
                   3021:                     }
                   3022:                 }
1.33      raeburn  3023:                 if ($secfilter eq 'none') {
                   3024:                     if ($section ne '') {
                   3025:                         delete($userlist->{$user});
                   3026:                         next;
                   3027:                     }
                   3028:                 } elsif ($secfilter ne 'all') {
                   3029:                     if ($section ne $secfilter) {
                   3030:                         delete($userlist->{$user});
                   3031:                         next;
                   3032:                     }
                   3033:                 }
                   3034:                 if ($grpfilter eq 'none') {
                   3035:                     if (@ac_groups > 0) {
                   3036:                         delete($userlist->{$user});
                   3037:                         next;
                   3038:                     }
                   3039:                 } elsif ($grpfilter ne 'all') {
                   3040:                     if (!grep(/^\Q$grpfilter\E$/,@ac_groups)) {
                   3041:                         delete($userlist->{$user});
                   3042:                         next;
                   3043:                     }
                   3044:                 }
1.44      raeburn  3045:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.140     raeburn  3046:                     if ((grep/^photo$/,@cols) && ($role eq 'st')) {
1.44      raeburn  3047:                         $userlist->{$user}->[$index{'photo'}] =
1.47      raeburn  3048:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,'jpg');
                   3049:                         $userlist->{$user}->[$index{'thumbnail'}] =
1.44      raeburn  3050:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,
                   3051:                                                                 'gif','thumbnail');
                   3052:                     }
                   3053:                 }
1.150     raeburn  3054:                 if (($role eq 'st') && ($defaultcredits)) {
                   3055:                     if ($userlist->{$user}->[$index{'credits'}] eq '') {
                   3056:                         $userlist->{$user}->[$index{'credits'}] = $defaultcredits;
                   3057:                     }
                   3058:                 }
1.33      raeburn  3059:             }
1.2       raeburn  3060:         }
1.184.4.10.2.  (raeburn 3061:):         if ($context eq 'author') {
                   3062:):             if (($camanagers{$user}) &&
                   3063:):                 ((!defined($userlist->{$user}->[$index{'end'}])) ||
                   3064:):                  ($userlist->{$user}->[$index{'end'}] == 0) ||
                   3065:):                  ($userlist->{$user}->[$index{'end'}] > $now))) {
                   3066:):                 $userlist->{$user}->[$index{'manager'}] = &mt('Yes');
                   3067:):             } else {
                   3068:):                 $userlist->{$user}->[$index{'manager'}] = &mt('No');
                   3069:):             }
                   3070:):         }
1.2       raeburn  3071:         my %emails   = &Apache::loncommon::getemails($uname,$udom);
                   3072:         if ($emails{'permanentemail'} =~ /\S/) {
                   3073:             $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
                   3074:         }
1.159     raeburn  3075:         if (($context eq 'domain') && ($env{'form.roletype'} eq 'domain') && 
                   3076:             ($role eq 'au')) {
                   3077:             my ($disk_quota,$current_disk_usage,$percent); 
                   3078:             if (($needauthorusage) || ($needauthorquota)) {
                   3079:                 $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,'author');
                   3080:             }
                   3081:             if ($needauthorusage) {
                   3082:                 $current_disk_usage =
                   3083:                     &Apache::lonnet::diskusage($udom,$uname,"$londocroot/priv/$udom/$uname");
                   3084:                 if ($disk_quota == 0) {
                   3085:                     $percent = 100.0;
                   3086:                 } else {
                   3087:                     $percent = $current_disk_usage/(10 * $disk_quota);
                   3088:                 }
                   3089:                 $userlist->{$user}->[$index{'authorusage'}] = sprintf("%.0f",$percent);
                   3090:             }
                   3091:             if ($needauthorquota) {
                   3092:                 $userlist->{$user}->[$index{'authorquota'}] = sprintf("%.2f",$disk_quota);
                   3093:             }
                   3094:         }
1.4       raeburn  3095:         $usercount ++;
                   3096:     }
                   3097:     my $autocount = 0;
                   3098:     my $manualcount = 0;
                   3099:     my $lockcount = 0;
                   3100:     my $unlockcount = 0;
                   3101:     if ($usercount) {
                   3102:         $r->print($output);
                   3103:     } else {
                   3104:         if ($mode eq 'autoenroll') {
                   3105:             return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
                   3106:         } else {
                   3107:             return;
                   3108:         }
1.1       raeburn  3109:     }
1.2       raeburn  3110:     #
                   3111:     # Sort the users
1.1       raeburn  3112:     my $index  = $index{$sortby};
                   3113:     my $second = $index{'username'};
                   3114:     my $third  = $index{'domain'};
1.159     raeburn  3115:     my @sorted_users;
                   3116:     if (($sortby eq 'authorquota') || ($sortby eq 'authorusage')) {  
                   3117:         @sorted_users = sort {
                   3118:             $userlist->{$b}->[$index] <=> $userlist->{$a}->[$index]           ||
                   3119:             lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second]) ||
                   3120:             lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
                   3121:             } (keys(%$userlist));
                   3122:     } else {
                   3123:         @sorted_users = sort {
                   3124:             lc($userlist->{$a}->[$index]) cmp lc($userlist->{$b}->[$index])   ||
                   3125:             lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second]) ||
                   3126:             lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
                   3127:             } (keys(%$userlist));
                   3128:     }
1.4       raeburn  3129:     my $rowcount = 0;
1.178     raeburn  3130:     my $disabled;
                   3131:     if ($mode eq 'autoenroll') {
                   3132:         unless ($permission->{'cusr'}) {
                   3133:             $disabled = ' disabled="disabled"';
                   3134:         }
                   3135:     }
1.2       raeburn  3136:     foreach my $user (@sorted_users) {
1.4       raeburn  3137:         my %in;
1.2       raeburn  3138:         my $sdata = $userlist->{$user};
1.4       raeburn  3139:         $rowcount ++; 
1.2       raeburn  3140:         foreach my $item (@{$keylist}) {
                   3141:             $in{$item} = $sdata->[$index{$item}];
                   3142:         }
1.67      droeschl 3143:         my $clickers = (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
                   3144:         if ($clickers!~/\w/) { $clickers='-'; }
1.159     raeburn  3145:         $in{'clicker'} = $clickers;
1.67      droeschl 3146: 	my $role = $in{'role'};
1.102     raeburn  3147:         $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype);
1.136     raeburn  3148:         unless ($mode eq 'excel') {
                   3149:             if (! defined($in{'start'}) || $in{'start'} == 0) {
                   3150:                 $in{'start'} = &mt('none');
                   3151:             } else {
                   3152:                 $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
                   3153:             }
                   3154:             if (! defined($in{'end'}) || $in{'end'} == 0) {
                   3155:                 $in{'end'} = &mt('none');
                   3156:             } else {
                   3157:                 $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
                   3158:             }
1.1       raeburn  3159:         }
1.139     raeburn  3160:         if ($context eq 'course') {
                   3161:             my $lastlogin = $crslogins{$in{'username'}.':'.$in{'domain'}.':'.$in{'section'}.':'.$role};
                   3162:             if ($lastlogin ne '') {
                   3163:                 $in{'lastlogin'} = &Apache::lonlocal::locallocaltime($lastlogin);
                   3164:             }
                   3165:         }
1.55      raeburn  3166:         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  3167:             $r->print(&Apache::loncommon::start_data_table_row());
1.11      raeburn  3168:             my $checkval;
1.16      raeburn  3169:             if ($mode eq 'autoenroll') {
                   3170:                 my $cellentry;
                   3171:                 if ($in{'type'} eq 'auto') {
1.178     raeburn  3172:                     $cellentry = '<b>'.&mt('auto').'</b>&nbsp;<label><input type="checkbox" name="chgauto" value="'.$in{'username'}.':'.$in{'domain'}.'"'.$disabled.' />&nbsp;'.&mt('Change').'</label>';
1.16      raeburn  3173:                     $autocount ++;
                   3174:                 } else {
1.178     raeburn  3175:                     $cellentry = '<table border="0" cellspacing="0"><tr><td rowspan="2"><b>'.&mt('manual').'</b></td><td><span class="LC_nobreak"><label><input type="checkbox" name="chgmanual" value="'.$in{'username'}.':'.$in{'domain'}.'"'.$disabled.' />&nbsp;'.&mt('Change').'</label></span></td></tr><tr><td><span class="LC_nobreak">';
1.16      raeburn  3176:                     $manualcount ++;
                   3177:                     if ($in{'lockedtype'}) {
1.178     raeburn  3178:                         $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'"'.$disabled.' />&nbsp;'.&mt('Unlock').'</label>';
1.16      raeburn  3179:                         $unlockcount ++;
                   3180:                     } else {
1.178     raeburn  3181:                         $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'"'.$disabled.' />&nbsp;'.&mt('Lock').'</label>';
1.16      raeburn  3182:                         $lockcount ++;
1.11      raeburn  3183:                     }
1.76      bisitz   3184:                     $cellentry .= '</span></td></tr></table>';
1.16      raeburn  3185:                 }
                   3186:                 $r->print("<td>$cellentry</td>\n");
                   3187:             } else {
1.55      raeburn  3188:                 if ($mode ne 'pickauthor') {  
                   3189:                     $r->print("<td>$rowcount</td>\n");
                   3190:                 }
1.16      raeburn  3191:                 if ($actionselect) {
1.26      raeburn  3192:                     my $showcheckbox;
                   3193:                     if ($role =~ /^cr\//) {
                   3194:                         $showcheckbox = $canchange{'cr'};
                   3195:                     } else {
                   3196:                         $showcheckbox = $canchange{$role};
                   3197:                     }
                   3198:                     if (!$showcheckbox) {
                   3199:                         if ($context eq 'course') {
                   3200:                             if ($canchangesec{$role} ne '') {
                   3201:                                 if ($canchangesec{$role} eq $in{'section'}) {
                   3202:                                     $showcheckbox = 1;
                   3203:                                 }
                   3204:                             }
1.16      raeburn  3205:                         }
1.26      raeburn  3206:                     }
                   3207:                     if ($showcheckbox) {
                   3208:                         $checkval = $user; 
                   3209:                         if ($context eq 'course') {
1.141     raeburn  3210:                             if (($role eq 'co' || $role eq 'cc') &&
                   3211:                                 ($user =~ /^\Q$env{'user.name'}:$env{'user.domain'}:$role\E/)) {
                   3212:                                 $showcheckbox = 0;
                   3213:                             } else {
                   3214:                                 if ($role eq 'st') {
                   3215:                                     $checkval .= ':st';
                   3216:                                 }
                   3217:                                 $checkval .= ':'.$in{'section'};
                   3218:                                 if ($role eq 'st') {
                   3219:                                     $checkval .= ':'.$in{'type'}.':'.
1.150     raeburn  3220:                                                  $in{'lockedtype'}.':'.
1.174     raeburn  3221:                                                  $in{'credits'}.':'.
                   3222:                                                  &escape($in{'instsec'});
1.141     raeburn  3223:                                 }
                   3224:                              }
                   3225:                         }
                   3226:                         if ($showcheckbox) {
                   3227:                             $r->print('<td><input type="checkbox" name="'.
1.183     raeburn  3228:                                       'actionlist" value="'.
                   3229:                                       &HTML::Entities::encode($checkval,'&<>"').'" />');
                   3230:                             foreach my $item ('start','end') {
                   3231:                                 $r->print('<input type="hidden" name="'.
                   3232:                                           &HTML::Entities::encode($checkval.'_'.$item,'&<>"').'"'.
                   3233:                                           ' value="'.$sdata->[$index{$item}].'" />');
                   3234:                             }
                   3235:                             $r->print('</td>');
1.141     raeburn  3236:                         } else {
                   3237:                             $r->print('<td>&nbsp;</td>');
1.16      raeburn  3238:                         }
1.26      raeburn  3239:                     } else {
                   3240:                         $r->print('<td>&nbsp;</td>');
1.16      raeburn  3241:                     }
1.55      raeburn  3242:                 } elsif ($mode eq 'pickauthor') {
                   3243:                         $r->print('<td><input type="button" name="chooseauthor" onclick="javascript:gochoose('."'$in{'username'}'".');" value="'.&mt('Select').'" /></td>');
1.11      raeburn  3244:                 }
                   3245:             }
1.2       raeburn  3246:             foreach my $item (@cols) {
1.10      raeburn  3247:                 if ($item eq 'username') {
1.48      raeburn  3248:                     $r->print('<td>'.&print_username_link($mode,\%in).'</td>');
1.83      raeburn  3249:                 } elsif ($item eq 'status') {
                   3250:                     my $showitem = $in{$item};
                   3251:                     if (defined($ltstatus{$in{$item}})) {
                   3252:                         $showitem = $ltstatus{$in{$item}};
                   3253:                     }
                   3254:                     $r->print('<td>'.$showitem.'</td>'."\n");
1.140     raeburn  3255:                 } elsif ($item eq 'photo') {
                   3256:                      if (($context eq 'course') && ($mode ne 'autoenroll') && 
                   3257:                          ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'})) { 
                   3258:                          if ($role eq 'st') {
                   3259:                              $r->print('<td align="right"><a href="javascript:photowindow('."'".$in{'photo'}."'".')"><img src="'.$in{'thumbnail'}.'" border="1" alt="" /></a></td>');
                   3260:                          } else {
                   3261:                              $r->print('<td>&nbsp;</td>');
                   3262:                          }
                   3263:                      }
                   3264:                 } elsif ($item eq 'clicker') {
                   3265:                     if (($context eq 'course') && ($mode ne 'autoenroll')) {
                   3266:                         if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   3267:                             my $clickers =
                   3268:                    (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
                   3269:                             if ($clickers!~/\w/) { $clickers='-'; }
                   3270:                             $r->print('<td>'.$clickers.'</td>');
                   3271:                         } else {
                   3272:                              $r->print('<td>&nbsp;</td>'."\n");
                   3273:                         } 
1.149     raeburn  3274:                     }
1.159     raeburn  3275:                 } elsif (($item eq 'authorquota') || ($item eq 'authorusage')) {
                   3276:                     $r->print('<td align="right">'.$in{$item}.'</td>'."\n");
1.10      raeburn  3277:                 } else {
                   3278:                     $r->print('<td>'.$in{$item}.'</td>'."\n");
                   3279:                 }
1.2       raeburn  3280:             }
                   3281:             $r->print(&Apache::loncommon::end_data_table_row());
                   3282:         } elsif ($mode eq 'csv') {
                   3283:             next if (! defined($CSVfile));
                   3284:             # no need to bother with $linkto
                   3285:             my @line = ();
                   3286:             foreach my $item (@cols) {
                   3287:                 push @line,&Apache::loncommon::csv_translate($in{$item});
                   3288:             }
1.67      droeschl 3289:             print $CSVfile '"'.join('","',@line)."\"\n";
1.2       raeburn  3290:         } elsif ($mode eq 'excel') {
                   3291:             my $col = 0;
                   3292:             foreach my $item (@cols) {
                   3293:                 if ($item eq 'start' || $item eq 'end') {
1.136     raeburn  3294:                     if ((defined($in{$item})) && ($in{$item} != 0)) {
1.2       raeburn  3295:                         $excel_sheet->write($row,$col++,
1.136     raeburn  3296:                             &Apache::lonstathelpers::calc_serial($in{$item}),
1.2       raeburn  3297:                                     $format->{'date'});
                   3298:                     } else {
                   3299:                         $excel_sheet->write($row,$col++,'none');
                   3300:                     }
                   3301:                 } else {
                   3302:                     $excel_sheet->write($row,$col++,$in{$item});
                   3303:                 }
                   3304:             }
                   3305:             $row++;
1.1       raeburn  3306:         }
                   3307:     }
1.55      raeburn  3308:     if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  3309:             $r->print(&Apache::loncommon::end_data_table().'<br />');
                   3310:     } elsif ($mode eq 'excel') {
                   3311:         $excel_workbook->close();
1.162     bisitz   3312: 	$r->print('<p>'.&mt('[_1]Your Excel spreadsheet[_2] is ready for download.', '<a href="'.$excel_filename.'">','</a>')."</p>\n");
1.2       raeburn  3313:     } elsif ($mode eq 'csv') {
                   3314:         close($CSVfile);
1.162     bisitz   3315: 	$r->print('<p>'.&mt('[_1]Your CSV file[_2] is ready for download.', '<a href="'.$CSVfilename.'">','</a>')."</p>\n");
1.2       raeburn  3316:         $r->rflush();
                   3317:     }
                   3318:     if ($mode eq 'autoenroll') {
                   3319:         return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
1.4       raeburn  3320:     } else {
                   3321:         return ($usercount);
1.2       raeburn  3322:     }
1.1       raeburn  3323: }
                   3324: 
1.56      raeburn  3325: sub bulkaction_javascript {
                   3326:     my ($formname,$caller) = @_;
                   3327:     my $docstart = 'document';
                   3328:     if ($caller eq 'popup') {
                   3329:         $docstart = 'opener.document';
                   3330:     }
                   3331:     my %lt = &Apache::lonlocal::texthash(
                   3332:               acwi => 'Access will be set to start immediately',
                   3333:               asyo => 'as you did not select an end date in the pop-up window',
                   3334:               accw => 'Access will be set to continue indefinitely',
                   3335:               asyd => 'as you did not select an end date in the pop-up window',
                   3336:               sewi => "Sections will be switched to 'No section'",
                   3337:               ayes => "as you either selected the 'No section' option",
                   3338:               oryo => 'or you did not select a section in the pop-up window',
                   3339:               arol => 'A role with no section will be added',
                   3340:               swbs => 'Sections will be switched to:',
                   3341:               rwba => 'Roles will be added for section(s):',
                   3342:             );
                   3343:     my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
                   3344:     my $noaction = &mt("You need to select an action to take for the user(s) you have selected"); 
                   3345:     my $singconfirm = &mt(' for a single user?');
                   3346:     my $multconfirm = &mt(' for multiple users?');
1.170     damieng  3347:     &js_escape(\$alert);
                   3348:     &js_escape(\$noaction);
                   3349:     &js_escape(\$singconfirm);
                   3350:     &js_escape(\$multconfirm);
1.56      raeburn  3351:     my $output = <<"ENDJS";
                   3352: function verify_action (field) {
                   3353:     var numchecked = 0;
                   3354:     var singconf = '$singconfirm';
                   3355:     var multconf = '$multconfirm';
                   3356:     if ($docstart.$formname.elements[field].length > 0) {
                   3357:         for (i=0; i<$docstart.$formname.elements[field].length; i++) {
                   3358:             if ($docstart.$formname.elements[field][i].checked == true) {
                   3359:                numchecked ++;
                   3360:             }
                   3361:         }
                   3362:     } else {
                   3363:         if ($docstart.$formname.elements[field].checked == true) {
                   3364:             numchecked ++;
                   3365:         }
                   3366:     }
                   3367:     if (numchecked == 0) {
                   3368:         alert("$alert");
                   3369:         return;
                   3370:     } else {
                   3371:         var message = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].text;
                   3372:         var choice = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].value;
                   3373:         if (choice == '') {
                   3374:             alert("$noaction");
                   3375:             return;
                   3376:         } else {
                   3377:             if (numchecked == 1) {
                   3378:                 message += singconf;
                   3379:             } else {
                   3380:                 message += multconf;
                   3381:             }
                   3382: ENDJS
                   3383:     if ($caller ne 'popup') {
                   3384:         $output .= <<"NEWWIN";
                   3385:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate' || choice == 'chgsec') {
                   3386:                 opendatebrowser(document.$formname,'$formname','go');
                   3387:                 return;
                   3388: 
                   3389:             } else {
                   3390:                 if (confirm(message)) {
                   3391:                     document.$formname.phase.value = 'bulkchange';
                   3392:                     document.$formname.submit();
                   3393:                     return;
                   3394:                 }
                   3395:             }
                   3396: NEWWIN
                   3397:     } else {
                   3398:         $output .= <<"POPUP";
                   3399:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate') {
                   3400:                 var datemsg = '';
                   3401:                 if (($docstart.$formname.startdate_month.value == '') &&
                   3402:                     ($docstart.$formname.startdate_day.value  == '') &&
                   3403:                     ($docstart.$formname.startdate_year.value == '')) {
                   3404:                     datemsg = "\\n$lt{'acwi'},\\n$lt{'asyo'}.\\n";
                   3405:                 }
                   3406:                 if (($docstart.$formname.enddate_month.value == '') &&
                   3407:                     ($docstart.$formname.enddate_day.value  == '') &&
                   3408:                     ($docstart.$formname.enddate_year.value == '')) {
                   3409:                     datemsg += "\\n$lt{'accw'},\\n$lt{'asyd'}.\\n";
                   3410:                 }
                   3411:                 if (datemsg != '') {
                   3412:                     message += "\\n"+datemsg;
                   3413:                 }
                   3414:             }
                   3415:             if (choice == 'chgsec') {
                   3416:                 var rolefilter = $docstart.$formname.showrole.options[$docstart.$formname.showrole.selectedIndex].value;
                   3417:                 var retained =  $docstart.$formname.retainsec.value;
                   3418:                 var secshow = $docstart.$formname.newsecs.value;
                   3419:                 if (secshow == '') {
                   3420:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   3421:                         message += "\\n\\n$lt{'sewi'},\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   3422:                     } else {
                   3423:                         message += "\\n\\n$lt{'arol'}\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   3424:                     }
                   3425:                 } else {
                   3426:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   3427:                         message += "\\n\\n$lt{'swbs'} "+secshow+".\\n";
                   3428:                     } else {
                   3429:                         message += "\\n\\n$lt{'rwba'} "+secshow+".\\n";
                   3430:                     }
                   3431:                 }
                   3432:             }
                   3433:             if (confirm(message)) {
                   3434:                 $docstart.$formname.phase.value = 'bulkchange';
                   3435:                 $docstart.$formname.submit();
                   3436:                 window.close();
                   3437:             }
                   3438: POPUP
                   3439:     }
                   3440:     $output .= '
                   3441:         }
                   3442:     }
                   3443: }
                   3444: ';
                   3445:     return $output;
                   3446: }
                   3447: 
1.10      raeburn  3448: sub print_username_link {
1.48      raeburn  3449:     my ($mode,$in) = @_;
1.10      raeburn  3450:     my $output;
1.16      raeburn  3451:     if ($mode eq 'autoenroll') {
                   3452:         $output = $in->{'username'};
1.10      raeburn  3453:     } else {
                   3454:         $output = '<a href="javascript:username_display_launch('.
1.112     bisitz   3455:                   "'$in->{'username'}','$in->{'domain'}'".')">'.
1.10      raeburn  3456:                   $in->{'username'}.'</a>';
                   3457:     }
                   3458:     return $output;
                   3459: }
                   3460: 
1.2       raeburn  3461: sub role_type_names {
                   3462:     my %lt = &Apache::lonlocal::texthash (
1.13      raeburn  3463:                          'domain' => 'Domain Roles',
                   3464:                          'author' => 'Co-Author Roles',
                   3465:                          'course' => 'Course Roles',
1.101     raeburn  3466:                          'community' => 'Community Roles',
1.2       raeburn  3467:              );
                   3468:     return %lt;
                   3469: }
                   3470: 
1.11      raeburn  3471: sub select_actions {
1.55      raeburn  3472:     my ($context,$setting,$statusmode,$formname) = @_;
1.11      raeburn  3473:     my %lt = &Apache::lonlocal::texthash(
                   3474:                 revoke   => "Revoke user roles",
                   3475:                 delete   => "Delete user roles",
                   3476:                 reenable => "Re-enable expired user roles",
                   3477:                 activate => "Make future user roles active now",
                   3478:                 chgdates  => "Change starting/ending dates",
                   3479:                 chgsec   => "Change section associated with user roles",
                   3480:     );
1.150     raeburn  3481:     # FIXME Add an option to change credits for student roles.
1.11      raeburn  3482:     my ($output,$options,%choices);
1.23      raeburn  3483:     # FIXME Disable actions for now for roletype=course in domain context
                   3484:     if ($context eq 'domain' && $setting eq 'course') {
                   3485:         return;
                   3486:     }
1.26      raeburn  3487:     if ($context eq 'course') {
                   3488:         if ($env{'form.showrole'} ne 'Any') {
1.141     raeburn  3489:             my $showactions;
                   3490:             if (&Apache::lonnet::allowed('c'.$env{'form.showrole'},
                   3491:                                           $env{'request.course.id'})) {
                   3492:                 $showactions = 1;  
                   3493:             } elsif ($env{'request.course.sec'} ne '') {
                   3494:                 if (&Apache::lonnet::allowed('c'.$env{'form.showrole'},$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   3495:                     $showactions = 1;
                   3496:                 }
                   3497:             }
                   3498:             unless ($showactions) {
                   3499:                 unless (&is_courseowner($env{'request.course.id'},
                   3500:                                        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'})) {
                   3501:                     return; 
                   3502:                 }
1.26      raeburn  3503:             }
                   3504:         }
                   3505:     }
1.11      raeburn  3506:     if ($statusmode eq 'Any') {
                   3507:         $options .= '
                   3508: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
                   3509:         $choices{'dates'} = 1;
                   3510:     } else {
                   3511:         if ($statusmode eq 'Future') {
                   3512:             $options .= '
                   3513: <option value="activate">'.$lt{'activate'}.'</option>';
                   3514:             $choices{'dates'} = 1;
                   3515:         } elsif ($statusmode eq 'Expired') {
                   3516:             $options .= '
                   3517: <option value="reenable">'.$lt{'reenable'}.'</option>';
                   3518:             $choices{'dates'} = 1;
                   3519:         }
1.13      raeburn  3520:         if ($statusmode eq 'Active' || $statusmode eq 'Future') {
                   3521:             $options .= '
                   3522: <option value="chgdates">'.$lt{'chgdates'}.'</option>
                   3523: <option value="revoke">'.$lt{'revoke'}.'</option>';
                   3524:             $choices{'dates'} = 1;
                   3525:         }
1.11      raeburn  3526:     }
                   3527:     if ($context eq 'domain') {
                   3528:         $options .= '
                   3529: <option value="delete">'.$lt{'delete'}.'</option>';
                   3530:     }
                   3531:     if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
1.26      raeburn  3532:         if (($statusmode ne 'Expired') && ($env{'request.course.sec'} eq '')) {
1.11      raeburn  3533:             $options .= '
                   3534: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
                   3535:             $choices{'sections'} = 1;
                   3536:         }
                   3537:     }
                   3538:     if ($options) {
1.56      raeburn  3539:         $output = '<select name="bulkaction">'."\n".
1.11      raeburn  3540:                   '<option value="" selected="selected">'.
                   3541:                   &mt('Please select').'</option>'."\n".$options."\n".'</select>';
                   3542:         if ($choices{'dates'}) {
                   3543:             $output .= 
                   3544:                 '<input type="hidden" name="startdate_month" value="" />'."\n".
                   3545:                 '<input type="hidden" name="startdate_day" value="" />'."\n".
                   3546:                 '<input type="hidden" name="startdate_year" value="" />'."\n".
                   3547:                 '<input type="hidden" name="startdate_hour" value="" />'."\n".
                   3548:                 '<input type="hidden" name="startdate_minute" value="" />'."\n".
                   3549:                 '<input type="hidden" name="startdate_second" value="" />'."\n".
                   3550:                 '<input type="hidden" name="enddate_month" value="" />'."\n".
                   3551:                 '<input type="hidden" name="enddate_day" value="" />'."\n".
                   3552:                 '<input type="hidden" name="enddate_year" value="" />'."\n".
                   3553:                 '<input type="hidden" name="enddate_hour" value="" />'."\n".
                   3554:                 '<input type="hidden" name="enddate_minute" value="" />'."\n".
1.56      raeburn  3555:                 '<input type="hidden" name="enddate_second" value="" />'."\n".
                   3556:                 '<input type="hidden" name="no_end_date" value="" />'."\n";
1.11      raeburn  3557:             if ($context eq 'course') {
                   3558:                 $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
                   3559:             }
                   3560:         }
                   3561:         if ($choices{'sections'}) {
1.91      bisitz   3562:             $output .= '<input type="hidden" name="retainsec" value="" />'."\n".
                   3563:                        '<input type="hidden" name="newsecs" value="" />'."\n";
1.11      raeburn  3564:         }
                   3565:     }
                   3566:     return $output;
                   3567: }
                   3568: 
                   3569: sub date_section_javascript {
                   3570:     my ($context,$setting) = @_;
1.49      raeburn  3571:     my $title = 'Date_And_Section_Selector';
1.41      raeburn  3572:     my %nopopup = &Apache::lonlocal::texthash (
                   3573:         revoke => "Check the boxes for any users for whom roles are to be revoked, and click 'Proceed'",
                   3574:         delete => "Check the boxes for any users for whom roles are to be deleted, and click 'Proceed'",
                   3575:         none   => "Choose an action to take for selected users",
                   3576:     );  
1.96      bisitz   3577:     my $output = <<"ENDONE";
                   3578: <script type="text/javascript">
                   3579: // <![CDATA[
1.41      raeburn  3580:     function opendatebrowser(callingform,formname,calledby) {
1.11      raeburn  3581:         var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
                   3582:         var url = '/adm/createuser?';
                   3583:         var type = '';
                   3584:         var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
                   3585: ENDONE
                   3586:     if ($context eq 'domain') {
                   3587:         $output .= '
                   3588:         type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
                   3589: ';
                   3590:     }
                   3591:     my $width= '700';
                   3592:     my $height = '400';
                   3593:     $output .= <<"ENDTWO";
                   3594:         url += 'action=dateselect&callingform=' + formname + 
                   3595:                '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
                   3596:         var title = '$title';
                   3597:         var options = 'scrollbars=1,resizable=1,menubar=0';
                   3598:         options += ',width=$width,height=$height';
                   3599:         stdeditbrowser = open(url,title,options,'1');
                   3600:         stdeditbrowser.focus();
                   3601:     }
1.96      bisitz   3602: // ]]>
1.11      raeburn  3603: </script>
                   3604: ENDTWO
                   3605:     return $output;
                   3606: }
                   3607: 
                   3608: sub date_section_selector {
1.150     raeburn  3609:     my ($context,$permission,$crstype,$showcredits) = @_;
1.11      raeburn  3610:     my $callingform = $env{'form.callingform'};
                   3611:     my $formname = 'dateselect';  
                   3612:     my $groupslist = &get_groupslist();
1.150     raeburn  3613:     my $sec_js =
                   3614:         &setsections_javascript($formname,$groupslist,undef,undef,$crstype,
                   3615:                                 $showcredits);
1.11      raeburn  3616:     my $output = <<"END";
                   3617: <script type="text/javascript">
1.96      bisitz   3618: // <![CDATA[
1.11      raeburn  3619: 
                   3620: $sec_js
                   3621: 
                   3622: function saveselections(formname) {
                   3623: 
                   3624: END
                   3625:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3626:         $output .= <<"END";
1.40      raeburn  3627:         if (formname.retainsec.length > 1) {  
                   3628:             for (var i=0; i<formname.retainsec.length; i++) {
                   3629:                 if (formname.retainsec[i].checked == true) {
                   3630:                     opener.document.$callingform.retainsec.value = formname.retainsec[i].value;
                   3631:                 }
                   3632:             }
                   3633:         } else {
                   3634:             opener.document.$callingform.retainsec.value = formname.retainsec.value;
                   3635:         }
1.103     raeburn  3636:         setSections(formname,'$crstype');
1.11      raeburn  3637:         if (seccheck == 'ok') {
                   3638:             opener.document.$callingform.newsecs.value = formname.sections.value;
1.184.4.8  raeburn  3639:         } else {
                   3640:             return;
1.11      raeburn  3641:         }
                   3642: END
                   3643:     } else {
                   3644:         if ($context eq 'course') {
                   3645:             if (($env{'form.bulkaction'} eq 'reenable') || 
                   3646:                 ($env{'form.bulkaction'} eq 'activate') || 
                   3647:                 ($env{'form.bulkaction'} eq 'chgdates')) {
1.26      raeburn  3648:                 if ($env{'request.course.sec'} eq '') {
                   3649:                     $output .= <<"END";
1.11      raeburn  3650:  
                   3651:         if (formname.makedatesdefault.checked == true) {
                   3652:             opener.document.$callingform.makedatesdefault.value = 1;
                   3653:         }
                   3654:         else {
                   3655:             opener.document.$callingform.makedatesdefault.value = 0;
                   3656:         }
                   3657: 
                   3658: END
1.26      raeburn  3659:                 }
1.11      raeburn  3660:             }
                   3661:         }
                   3662:         $output .= <<"END";
                   3663:     opener.document.$callingform.startdate_month.value =  formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
                   3664:     opener.document.$callingform.startdate_day.value =  formname.startdate_day.value;
                   3665:     opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
                   3666:     opener.document.$callingform.startdate_hour.value =  formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
                   3667:     opener.document.$callingform.startdate_minute.value =  formname.startdate_minute.value;
                   3668:     opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
                   3669:     opener.document.$callingform.enddate_month.value =  formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
                   3670:     opener.document.$callingform.enddate_day.value =  formname.enddate_day.value;
                   3671:     opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
                   3672:     opener.document.$callingform.enddate_hour.value =  formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
                   3673:     opener.document.$callingform.enddate_minute.value =  formname.enddate_minute.value;
                   3674:     opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
1.56      raeburn  3675:     if (formname.no_end_date.checked) {
                   3676:         opener.document.$callingform.no_end_date.value = '1';
                   3677:     } else {
                   3678:         opener.document.$callingform.no_end_date.value = '0';
                   3679:     }
1.11      raeburn  3680: END
                   3681:     }
1.56      raeburn  3682:     my $verify_action_js = &bulkaction_javascript($callingform,'popup');
                   3683:     $output .= <<"ENDJS";
                   3684:     verify_action('actionlist');
1.11      raeburn  3685: }
1.56      raeburn  3686: 
                   3687: $verify_action_js
                   3688: 
1.96      bisitz   3689: // ]]>
1.11      raeburn  3690: </script>
1.56      raeburn  3691: ENDJS
1.11      raeburn  3692:     my %lt = &Apache::lonlocal::texthash (
                   3693:                  chac => 'Access dates to apply for selected users',
                   3694:                  chse => 'Changes in section affiliation to apply to selected users',
1.118     raeburn  3695:                  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.',
                   3696:                  forn => 'For a course role that is not "student", users may have roles in more than one section at a time.',
                   3697:                  reta => "Retain each user's current section affiliations?",
1.103     raeburn  3698:                  dnap => '(Does not apply to student roles).',
1.11      raeburn  3699:             );
                   3700:     my ($date_items,$headertext);
                   3701:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3702:         $headertext = $lt{'chse'};
                   3703:     } else {
                   3704:         $headertext = $lt{'chac'};
                   3705:         my $starttime;
                   3706:         if (($env{'form.bulkaction'} eq 'activate') || 
                   3707:             ($env{'form.bulkaction'} eq 'reenable')) {
                   3708:             $starttime = time;
                   3709:         }
                   3710:         $date_items = &date_setting_table($starttime,undef,$context,
1.21      raeburn  3711:                                           $env{'form.bulkaction'},$formname,
1.101     raeburn  3712:                                           $permission,$crstype);
1.11      raeburn  3713:     }
                   3714:     $output .= '<h3>'.$headertext.'</h3>'.
1.118     raeburn  3715:                '<form name="'.$formname.'" method="post" action="">'."\n".
1.11      raeburn  3716:                 $date_items;
                   3717:     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
1.17      raeburn  3718:         my ($cnum,$cdom) = &get_course_identity();
1.103     raeburn  3719:         if ($crstype eq 'Community') {
1.118     raeburn  3720:             $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.');
                   3721:             $lt{'forn'} = &mt('For a community role that is not "member", users may have roles in more than one section at a time.');
1.103     raeburn  3722:             $lt{'dnap'} = &mt('(Does not apply to member roles).'); 
                   3723:         }
1.11      raeburn  3724:         my $info;
                   3725:         if ($env{'form.showrole'} eq 'st') {
                   3726:             $output .= '<p>'.$lt{'fors'}.'</p>'; 
1.26      raeburn  3727:         } elsif ($env{'form.showrole'} eq 'Any') {
1.11      raeburn  3728:             $output .= '<p>'.$lt{'fors'}.'</p>'.
                   3729:                        '<p>'.$lt{'forn'}.'&nbsp;';
                   3730:             $info = $lt{'reta'};
                   3731:         } else {
                   3732:             $output .= '<p>'.$lt{'forn'}.'&nbsp;';
                   3733:             $info = $lt{'reta'};
                   3734:         }
                   3735:         if ($info) {
                   3736:             $info .= '<span class="LC_nobreak">'.
                   3737:                      '<label><input type="radio" name="retainsec" value="1" '.
                   3738:                      'checked="checked" />'.&mt('Yes').'</label>&nbsp;&nbsp;'.
                   3739:                      '<label><input type="radio" name="retainsec" value="0" />'.
                   3740:                      &mt('No').'</label></span>';
                   3741:             if ($env{'form.showrole'} eq 'Any') {
                   3742:                 $info .= '<br />'.$lt{'dnap'};
                   3743:             }
                   3744:             $info .= '</p>';
                   3745:         } else {
                   3746:             $info = '<input type="hidden" name="retainsec" value="0" />'; 
                   3747:         }
1.21      raeburn  3748:         my $rowtitle = &mt('New section to assign');
1.150     raeburn  3749:         my $secbox = &section_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,
                   3750:                                      $permission,$context,'chgsec',$crstype);
1.11      raeburn  3751:         $output .= $info.$secbox;
                   3752:     }
                   3753:     $output .= '<p>'.
1.81      schafran 3754: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
1.11      raeburn  3755: '</form>';
                   3756:     return $output;
                   3757: }
                   3758: 
1.17      raeburn  3759: sub section_picker {
1.150     raeburn  3760:     my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode,$crstype,
                   3761:         $showcredits,$credits) = @_;
1.17      raeburn  3762:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   3763:     my $sections_select .= &course_sections(\%sections_count,$role);
1.118     raeburn  3764:     my $secbox = '<div>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
1.17      raeburn  3765:     if ($mode eq 'upload') {
                   3766:         my ($options,$cb_script,$coursepick) =
1.150     raeburn  3767:             &default_role_selector($context,1,$crstype,$showcredits);
1.59      bisitz   3768:         $secbox .= &Apache::lonhtmlcommon::row_title(&mt('role'),'LC_oddrow_value').
1.17      raeburn  3769:                    $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3770:     }
                   3771:     $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
                   3772:     if ($env{'request.course.sec'} eq '') {
                   3773:         $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
                   3774:                    '<td align="center">'.&mt('Existing sections')."\n".
                   3775:                    '<br />'.$sections_select.'</td><td align="center">'.
                   3776:                    &mt('New section').'<br />'."\n".
1.118     raeburn  3777:                    '<input type="text" name="newsec" size="15" value="" />'."\n".
1.17      raeburn  3778:                    '<input type="hidden" name="sections" value="" />'."\n".
                   3779:                    '</td></tr></table>'."\n";
                   3780:     } else {
1.149     raeburn  3781:         $secbox .= '<input type="hidden" name="sections" value="'.
1.17      raeburn  3782:                    $env{'request.course.sec'}.'" />'.
                   3783:                    $env{'request.course.sec'};
                   3784:     }
1.150     raeburn  3785:     $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3786:     unless ($mode eq 'chgsec') {
                   3787:         if ($showcredits) {
                   3788:             $secbox .= 
                   3789:                 &Apache::lonhtmlcommon::row_title(&mt('credits (students)'),
                   3790:                                                   'LC_evenrow_value')."\n".
                   3791:                 '<input type="text" name="credits" size="3" value="'.$credits.'" />'."\n".
                   3792:                 &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3793:         }
                   3794:     }
                   3795:     $secbox .= &Apache::lonhtmlcommon::end_pick_box().'</div>';
1.17      raeburn  3796:     return $secbox;
                   3797: }
                   3798: 
1.2       raeburn  3799: sub results_header_row {
1.102     raeburn  3800:     my ($rolefilter,$statusmode,$context,$permission,$mode,$crstype) = @_;
1.5       raeburn  3801:     my ($description,$showfilter);
                   3802:     if ($rolefilter ne 'Any') {
                   3803:         $showfilter = $rolefilter;
                   3804:     }
1.2       raeburn  3805:     if ($context eq 'course') {
1.24      raeburn  3806:         if ($mode eq 'csv' || $mode eq 'excel') {
1.102     raeburn  3807:             if ($crstype eq 'Community') {
                   3808:                 $description = &mt('Community - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3809:             } else {
                   3810:                 $description = &mt('Course - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3811:             }
1.24      raeburn  3812:         }
1.2       raeburn  3813:         if ($statusmode eq 'Expired') {
1.102     raeburn  3814:             if ($crstype eq 'Community') {
                   3815:                 $description .= &mt('Users in community with expired [_1] roles',$showfilter);
                   3816:             } else {
                   3817:                 $description .= &mt('Users in course with expired [_1] roles',$showfilter);
                   3818:             }
1.11      raeburn  3819:         } elsif ($statusmode eq 'Future') {
1.102     raeburn  3820:             if ($crstype eq 'Community') {
                   3821:                 $description .= &mt('Users in community with future [_1] roles',$showfilter);
                   3822:             } else {
                   3823:                 $description .= &mt('Users in course with future [_1] roles',$showfilter);
                   3824:             }
1.2       raeburn  3825:         } elsif ($statusmode eq 'Active') {
1.102     raeburn  3826:             if ($crstype eq 'Community') {
                   3827:                 $description .= &mt('Users in community with active [_1] roles',$showfilter);
                   3828:             } else {
                   3829:                 $description .= &mt('Users in course with active [_1] roles',$showfilter);
                   3830:             }
1.2       raeburn  3831:         } else {
                   3832:             if ($rolefilter eq 'Any') {
1.102     raeburn  3833:                 if ($crstype eq 'Community') {
                   3834:                     $description .= &mt('All users in community');
                   3835:                 } else {
                   3836:                     $description .= &mt('All users in course');
                   3837:                 }
1.2       raeburn  3838:             } else {
1.102     raeburn  3839:                 if ($crstype eq 'Community') {
                   3840:                     $description .= &mt('All users in community with [_1] roles',$rolefilter);
                   3841:                 } else {
                   3842:                     $description .= &mt('All users in course with [_1] roles',$rolefilter);
                   3843:                 }
1.2       raeburn  3844:             }
                   3845:         }
1.33      raeburn  3846:         my $constraint;
1.26      raeburn  3847:         my $viewablesec = &viewable_section($permission);
                   3848:         if ($viewablesec ne '') {
1.15      raeburn  3849:             if ($env{'form.showrole'} eq 'st') {
1.33      raeburn  3850:                 $constraint = &mt('only users in section "[_1]"',$viewablesec);
1.103     raeburn  3851:             } elsif (($env{'form.showrole'} ne 'cc') && ($env{'form.showrole'} ne 'co')) {
1.33      raeburn  3852:                 $constraint = &mt('only users affiliated with no section or section "[_1]"',$viewablesec);
                   3853:             }
                   3854:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3855:                 if ($env{'form.grpfilter'} eq 'none') {
                   3856:                     $constraint .= &mt(' and not in any group');
                   3857:                 } else {
                   3858:                     $constraint .= &mt(' and members of group: "[_1]"',$env{'form.grpfilter'});
                   3859:                 }
                   3860:             }
                   3861:         } else {
                   3862:             if (($env{'form.secfilter'} ne 'all') && ($env{'form.secfilter'} ne '')) {
                   3863:                 if ($env{'form.secfilter'} eq 'none') {
                   3864:                     $constraint = &mt('only users affiliated with no section');
                   3865:                 } else {
                   3866:                     $constraint = &mt('only users affiliated with section "[_1]"',$env{'form.secfilter'});
                   3867:                 }
                   3868:             }
                   3869:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3870:                 if ($env{'form.grpfilter'} eq 'none') {
                   3871:                     if ($constraint eq '') {
                   3872:                         $constraint = &mt('only users not in any group');
                   3873:                     } else {
                   3874:                         $constraint .= &mt(' and also not in any group'); 
                   3875:                     }
                   3876:                 } else {
                   3877:                     if ($constraint eq '') {
                   3878:                         $constraint = &mt('only members of group: "[_1]"',$env{'form.grpfilter'});
                   3879:                     } else {
                   3880:                         $constraint .= &mt(' and also members of group: "[_1]"'.$env{'form.grpfilter'});
                   3881:                     }
                   3882:                 }
1.15      raeburn  3883:             }
                   3884:         }
1.33      raeburn  3885:         if ($constraint ne '') {
                   3886:             $description .= ' ('.$constraint.')';
                   3887:         } 
1.13      raeburn  3888:     } elsif ($context eq 'author') {
1.14      raeburn  3889:         $description = 
1.73      bisitz   3890:             &mt('Author space for [_1]'
                   3891:                 ,'<span class="LC_cusr_emph">'
                   3892:                 .&Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})
                   3893:                 .'</span>')
                   3894:             .':&nbsp;&nbsp;';
1.2       raeburn  3895:         if ($statusmode eq 'Expired') {
1.5       raeburn  3896:             $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2       raeburn  3897:         } elsif ($statusmode eq 'Future') {
1.5       raeburn  3898:             $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2       raeburn  3899:         } elsif ($statusmode eq 'Active') {
1.5       raeburn  3900:             $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2       raeburn  3901:         } else {
                   3902:             if ($rolefilter eq 'Any') {
1.5       raeburn  3903:                 $description .= &mt('All co-authors');
1.2       raeburn  3904:             } else {
                   3905:                 $description .= &mt('All co-authors with [_1] roles',$rolefilter);
                   3906:             }
                   3907:         }
                   3908:     } elsif ($context eq 'domain') {
                   3909:         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.73      bisitz   3910:         $description = &mt('Domain - [_1]:',$domdesc).' ';
1.2       raeburn  3911:         if ($env{'form.roletype'} eq 'domain') {
                   3912:             if ($statusmode eq 'Expired') {
1.5       raeburn  3913:                 $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3914:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3915:                 $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2       raeburn  3916:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3917:                 $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2       raeburn  3918:             } else {
                   3919:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3920:                     $description .= &mt('All users in domain');
1.2       raeburn  3921:                 } else {
                   3922:                     $description .= &mt('All users in domain with [_1] roles',$rolefilter);
                   3923:                 }
                   3924:             }
1.13      raeburn  3925:         } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  3926:             if ($statusmode eq 'Expired') {
1.5       raeburn  3927:                 $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3928:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3929:                 $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2       raeburn  3930:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3931:                $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2       raeburn  3932:             } else {
                   3933:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3934:                     $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2       raeburn  3935:                 } else {
1.116     bisitz   3936:                     $description .= &mt('All co-authors in domain with [_1] roles',$rolefilter);
1.2       raeburn  3937:                 }
                   3938:             }
1.102     raeburn  3939:         } elsif (($env{'form.roletype'} eq 'course') || 
                   3940:                  ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  3941:             my $coursefilter = $env{'form.coursepick'};
1.102     raeburn  3942:             if ($env{'form.roletype'} eq 'course') {
                   3943:                 if ($coursefilter eq 'category') {
                   3944:                     my $instcode = &instcode_from_coursefilter();
                   3945:                     if ($instcode eq '.') {
                   3946:                         $description .= &mt('All courses in domain').' - ';
                   3947:                     } else {
                   3948:                         $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
                   3949:                     }
                   3950:                 } elsif ($coursefilter eq 'selected') {
                   3951:                     $description .= &mt('Selected courses in domain').' - ';
                   3952:                 } elsif ($coursefilter eq 'all') {
1.2       raeburn  3953:                     $description .= &mt('All courses in domain').' - ';
                   3954:                 }
1.102     raeburn  3955:             } elsif ($env{'form.roletype'} eq 'community') {
                   3956:                 if ($coursefilter eq 'selected') {
                   3957:                     $description .= &mt('Selected communities in domain').' - ';
                   3958:                 } elsif ($coursefilter eq 'all') {
                   3959:                     $description .= &mt('All communities in domain').' - ';
                   3960:                 }
1.2       raeburn  3961:             }
                   3962:             if ($statusmode eq 'Expired') {
1.5       raeburn  3963:                 $description .= &mt('users with expired [_1] roles',$showfilter);
1.2       raeburn  3964:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3965:                 $description .= &mt('users with future [_1] roles',$showfilter);
1.2       raeburn  3966:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3967:                 $description .= &mt('users with active [_1] roles',$showfilter);
1.2       raeburn  3968:             } else {
                   3969:                 if ($rolefilter eq 'Any') {
                   3970:                     $description .= &mt('all users');
                   3971:                 } else {
                   3972:                     $description .= &mt('users with [_1] roles',$rolefilter);
                   3973:                 }
                   3974:             }
                   3975:         }
                   3976:     }
                   3977:     return $description;
                   3978: }
1.22      raeburn  3979: 
                   3980: sub viewable_section {
                   3981:     my ($permission) = @_;
                   3982:     my $viewablesec;
                   3983:     if (ref($permission) eq 'HASH') {
                   3984:         if (exists($permission->{'view_section'})) {
                   3985:             $viewablesec = $permission->{'view_section'};
                   3986:         } elsif (exists($permission->{'cusr_section'})) {
                   3987:             $viewablesec = $permission->{'cusr_section'};
                   3988:         }
                   3989:     }
                   3990:     return $viewablesec;
                   3991: }
                   3992: 
1.2       raeburn  3993:     
1.1       raeburn  3994: #################################################
                   3995: #################################################
                   3996: sub show_drop_list {
1.101     raeburn  3997:     my ($r,$classlist,$nosort,$permission,$crstype) = @_;
1.29      raeburn  3998:     my $cid = $env{'request.course.id'};
1.17      raeburn  3999:     my ($cnum,$cdom) = &get_course_identity($cid);
1.1       raeburn  4000:     if (! exists($env{'form.sortby'})) {
                   4001:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   4002:                                                 ['sortby']);
                   4003:     }
                   4004:     my $sortby = $env{'form.sortby'};
                   4005:     if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
                   4006:         $sortby = 'username';
                   4007:     }
                   4008:     my $action = "drop";
1.17      raeburn  4009:     my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.1       raeburn  4010:     $r->print(<<END);
                   4011: <input type="hidden" name="sortby" value="$sortby" />
                   4012: <input type="hidden" name="action" value="$action" />
                   4013: <input type="hidden" name="state"  value="done" />
1.17      raeburn  4014: <script type="text/javascript" language="Javascript">
1.96      bisitz   4015: // <![CDATA[
1.17      raeburn  4016: $check_uncheck_js
1.96      bisitz   4017: // ]]>
1.1       raeburn  4018: </script>
1.86      bisitz   4019: <input type="hidden" name="phase" value="four" />
1.1       raeburn  4020: END
1.30      raeburn  4021:     my ($indexhash,$keylist) = &make_keylist_array();
                   4022:     my $studentcount = 0;
                   4023:     if (ref($classlist) eq 'HASH') {
                   4024:         foreach my $student (keys(%{$classlist})) {
                   4025:             my $sdata = $classlist->{$student}; 
                   4026:             my $status = $sdata->[$indexhash->{'status'}];
                   4027:             my $section = $sdata->[$indexhash->{'section'}];
                   4028:             if ($status ne 'Active') {
                   4029:                 delete($classlist->{$student});
                   4030:                 next;
                   4031:             }
                   4032:             if ($env{'request.course.sec'} ne '') {
                   4033:                 if ($section ne $env{'request.course.sec'}) {
                   4034:                     delete($classlist->{$student});
                   4035:                     next;
                   4036:                 }
                   4037:             }
                   4038:             $studentcount ++;
                   4039:         }
                   4040:     }
                   4041:     if (!$studentcount) {
1.143     bisitz   4042:        my $msg = '';
1.101     raeburn  4043:         if ($crstype eq 'Community') {
1.143     bisitz   4044:             $msg = &mt('There are no members to drop.');
1.101     raeburn  4045:         } else {
1.143     bisitz   4046:             $msg = &mt('There are no students to drop.');
1.101     raeburn  4047:         }
1.143     bisitz   4048:         $r->print('<p class="LC_info">'.$msg.'</p>');
1.30      raeburn  4049:         return;
                   4050:     }
                   4051:     my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   4052:                                               $classlist,$keylist,$cdom,$cnum);
                   4053:     my %lt=&Apache::lonlocal::texthash('usrn'   => "username",
                   4054:                                        'dom'    => "domain",
1.162     bisitz   4055:                                        'id'     => "ID",
1.30      raeburn  4056:                                        'sn'     => "student name",
1.101     raeburn  4057:                                        'mn'     => "member name",
1.30      raeburn  4058:                                        'sec'    => "section",
                   4059:                                        'start'  => "start date",
                   4060:                                        'end'    => "end date",
                   4061:                                        'groups' => "active groups",
                   4062:                                       );
1.101     raeburn  4063:     my $nametitle = $lt{'sn'};
                   4064:     if ($crstype eq 'Community') {
                   4065:         $nametitle = $lt{'mn'};
                   4066:     }
1.1       raeburn  4067:     if ($nosort) {
1.17      raeburn  4068:         $r->print(&Apache::loncommon::start_data_table().
                   4069:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  4070:         $r->print(<<END);
                   4071:     <th>&nbsp;</th>
                   4072:     <th>$lt{'usrn'}</th>
                   4073:     <th>$lt{'dom'}</th>
1.162     bisitz   4074:     <th>$lt{'id'}</th>
1.101     raeburn  4075:     <th>$nametitle</th>
1.1       raeburn  4076:     <th>$lt{'sec'}</th>
                   4077:     <th>$lt{'start'}</th>
                   4078:     <th>$lt{'end'}</th>
                   4079:     <th>$lt{'groups'}</th>
                   4080: END
1.17      raeburn  4081:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  4082:     } else  {
1.17      raeburn  4083:         $r->print(&Apache::loncommon::start_data_table().
                   4084:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  4085:         $r->print(<<END);
1.17      raeburn  4086:     <th>&nbsp;</th>
1.1       raeburn  4087:     <th>
1.162     bisitz   4088:        <a href="/adm/createuser?action=$action&amp;sortby=username">$lt{'usrn'}</a>
1.1       raeburn  4089:     </th><th>
1.162     bisitz   4090:        <a href="/adm/createuser?action=$action&amp;sortby=domain">$lt{'dom'}</a>
1.1       raeburn  4091:     </th><th>
1.162     bisitz   4092:        <a href="/adm/createuser?action=$action&amp;sortby=id">$lt{'id'}</a>
1.1       raeburn  4093:     </th><th>
1.162     bisitz   4094:        <a href="/adm/createuser?action=$action&amp;sortby=fullname">$nametitle</a>
1.1       raeburn  4095:     </th><th>
1.162     bisitz   4096:        <a href="/adm/createuser?action=$action&amp;sortby=section">$lt{'sec'}</a>
1.1       raeburn  4097:     </th><th>
1.162     bisitz   4098:        <a href="/adm/createuser?action=$action&amp;sortby=start">$lt{'start'}</a>
1.1       raeburn  4099:     </th><th>
1.162     bisitz   4100:        <a href="/adm/createuser?action=$action&amp;sortby=end">$lt{'end'}</a>
1.1       raeburn  4101:     </th><th>
1.162     bisitz   4102:        <a href="/adm/createuser?action=$action&amp;sortby=groups">$lt{'groups'}</a>
1.1       raeburn  4103:     </th>
                   4104: END
1.17      raeburn  4105:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  4106:     }
                   4107:     #
                   4108:     # Sort the students
1.30      raeburn  4109:     my $index  = $indexhash->{$sortby};
                   4110:     my $second = $indexhash->{'username'};
                   4111:     my $third  = $indexhash->{'domain'};
1.1       raeburn  4112:     my @Sorted_Students = sort {
                   4113:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   4114:             ||
                   4115:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   4116:             ||
                   4117:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1.30      raeburn  4118:         } (keys(%{$classlist}));
1.1       raeburn  4119:     foreach my $student (@Sorted_Students) {
                   4120:         my $error;
                   4121:         my $sdata = $classlist->{$student};
1.30      raeburn  4122:         my $username = $sdata->[$indexhash->{'username'}];
                   4123:         my $domain   = $sdata->[$indexhash->{'domain'}];
                   4124:         my $section  = $sdata->[$indexhash->{'section'}];
                   4125:         my $name     = $sdata->[$indexhash->{'fullname'}];
                   4126:         my $id       = $sdata->[$indexhash->{'id'}];
                   4127:         my $start    = $sdata->[$indexhash->{'start'}];
                   4128:         my $end      = $sdata->[$indexhash->{'end'}];
1.1       raeburn  4129:         my $groups = $classgroups->{$student};
                   4130:         my $active_groups;
                   4131:         if (ref($groups->{active}) eq 'HASH') {
                   4132:             $active_groups = join(', ',keys(%{$groups->{'active'}}));
                   4133:         }
                   4134:         if (! defined($start) || $start == 0) {
                   4135:             $start = &mt('none');
                   4136:         } else {
                   4137:             $start = &Apache::lonlocal::locallocaltime($start);
                   4138:         }
                   4139:         if (! defined($end) || $end == 0) {
                   4140:             $end = &mt('none');
                   4141:         } else {
                   4142:             $end = &Apache::lonlocal::locallocaltime($end);
                   4143:         }
1.17      raeburn  4144:         my $studentkey = $student.':'.$section;
1.30      raeburn  4145:         my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$indexhash->{'start'}].'" />';
1.1       raeburn  4146:         #
                   4147:         $r->print(&Apache::loncommon::start_data_table_row());
                   4148:         $r->print(<<"END");
1.86      bisitz   4149:     <td><input type="checkbox" name="droplist" value="$studentkey" /></td>
1.1       raeburn  4150:     <td>$username</td>
                   4151:     <td>$domain</td>
                   4152:     <td>$id</td>
                   4153:     <td>$name</td>
                   4154:     <td>$section</td>
1.29      raeburn  4155:     <td>$start $startitem</td>
1.1       raeburn  4156:     <td>$end</td>
                   4157:     <td>$active_groups</td>
                   4158: END
                   4159:         $r->print(&Apache::loncommon::end_data_table_row());
                   4160:     }
                   4161:     $r->print(&Apache::loncommon::end_data_table().'<br />');
                   4162:     %lt=&Apache::lonlocal::texthash(
1.29      raeburn  4163:                        'dp'   => "Drop Students",
1.101     raeburn  4164:                        'dm'   => "Drop Members",
1.1       raeburn  4165:                        'ca'   => "check all",
                   4166:                        'ua'   => "uncheck all",
                   4167:                                        );
1.101     raeburn  4168:     my $btn = $lt{'dp'};
                   4169:     if ($crstype eq 'Community') {
                   4170:         $btn = $lt{'dm'}; 
                   4171:     }
1.1       raeburn  4172:     $r->print(<<"END");
1.89      bisitz   4173: <p>
1.86      bisitz   4174: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)" /> &nbsp;
                   4175: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)" />
1.89      bisitz   4176: </p>
                   4177: <p>
1.101     raeburn  4178: <input type="submit" value="$btn" />
1.89      bisitz   4179: </p>
1.1       raeburn  4180: END
                   4181:     return;
                   4182: }
                   4183: 
                   4184: #
                   4185: # Print out the initial form to get the file containing a list of users
                   4186: #
                   4187: sub print_first_users_upload_form {
                   4188:     my ($r,$context) = @_;
                   4189:     my $str;
1.86      bisitz   4190:     $str  = '<input type="hidden" name="phase" value="two" />';
1.1       raeburn  4191:     $str .= '<input type="hidden" name="action" value="upload" />';
1.101     raeburn  4192:     $str .= '<input type="hidden" name="state"  value="got_file" />';
1.95      bisitz   4193: 
1.147     bisitz   4194:     $str .= &Apache::grades::checkforfile_js();
                   4195: 
1.85      bisitz   4196:     $str .= '<h2>'.&mt('Upload a file containing information about users').'</h2>'."\n";
1.95      bisitz   4197: 
                   4198:     # Excel and CSV Help
1.147     bisitz   4199:     $str .= '<div class="LC_columnSection">'
1.95      bisitz   4200:            .&Apache::loncommon::help_open_topic("Course_Create_Class_List",
                   4201:                 &mt("How do I create a users list from a spreadsheet"))
1.147     bisitz   4202:            .' '.&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
1.95      bisitz   4203:                 &mt("How do I create a CSV file from a spreadsheet"))
1.147     bisitz   4204:            ."</div>\n";
1.95      bisitz   4205:     $str .= &Apache::lonhtmlcommon::start_pick_box()
1.103     raeburn  4206:            .&Apache::lonhtmlcommon::row_title(&mt('File'));
                   4207:     if (&Apache::lonlocal::current_language() ne 'en') {
                   4208:         if ($context eq 'course') { 
                   4209:             $str .= '<p class="LC_info">'."\n"
                   4210:                    .&mt('Please upload an UTF8 encoded file to ensure a correct character encoding in your classlist.')."\n"
                   4211:                    .'</p>'."\n";
                   4212:         }
                   4213:     }
                   4214:     $str .= &Apache::loncommon::upfile_select_html()
1.95      bisitz   4215:            .&Apache::lonhtmlcommon::row_closure()
                   4216:            .&Apache::lonhtmlcommon::row_title(
                   4217:                 '<label for="noFirstLine">'
                   4218:                .&mt('Ignore First Line')
                   4219:                .'</label>')
                   4220:            .'<input type="checkbox" name="noFirstLine" id="noFirstLine" />'
                   4221:            .&Apache::lonhtmlcommon::row_closure(1)
                   4222:            .&Apache::lonhtmlcommon::end_pick_box();
                   4223: 
                   4224:     $str .= '<p>'
1.184.4.2  raeburn  4225:            .'<input type="button" name="fileupload" value="'.&mt('Next').'"'
1.147     bisitz   4226:            .' onclick="javascript:checkUpload(this.form);" />'
1.95      bisitz   4227:            .'</p>';
                   4228: 
1.1       raeburn  4229:     $r->print($str);
                   4230:     return;
                   4231: }
                   4232: 
                   4233: # ================================================= Drop/Add from uploaded file
                   4234: sub upfile_drop_add {
1.150     raeburn  4235:     my ($r,$context,$permission,$showcredits) = @_;
1.184.4.1  raeburn  4236:     my $datatoken = &Apache::loncommon::valid_datatoken($env{'form.datatoken'});
                   4237:     if ($datatoken ne '') {
                   4238:         &Apache::loncommon::load_tmp_file($r,$datatoken);
                   4239:     }
1.1       raeburn  4240:     my @userdata=&Apache::loncommon::upfile_record_sep();
                   4241:     if($env{'form.noFirstLine'}){shift(@userdata);}
                   4242:     my @keyfields = split(/\,/,$env{'form.keyfields'});
                   4243:     my %fields=();
                   4244:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
                   4245:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   4246:             if ($env{'form.f'.$i} ne 'none') {
                   4247:                 $fields{$keyfields[$i]}=$env{'form.f'.$i};
                   4248:             }
                   4249:         } else {
                   4250:             $fields{$env{'form.f'.$i}}=$keyfields[$i];
                   4251:         }
                   4252:     }
                   4253:     #
                   4254:     # Store the field choices away
1.150     raeburn  4255:     my @storefields = qw/username names fname mname lname gen id 
                   4256:                          sec ipwd email role domain inststatus/;
                   4257:     if ($showcredits) {
                   4258:         push (@storefields,'credits');
                   4259:     }
                   4260:     my %fieldstype; 
                   4261:     foreach my $field (@storefields) {
1.1       raeburn  4262:         $env{'form.'.$field.'_choice'}=$fields{$field};
1.150     raeburn  4263:         $fieldstype{$field.'_choice'} = 'scalar';
1.1       raeburn  4264:     }
1.150     raeburn  4265:     &Apache::loncommon::store_course_settings('enrollment_upload',\%fieldstype);
1.184.4.10.2.  (raeburn 4266:):     my ($cid,$crstype,$setting,$crsdom,$crsnum,$oldcrsuserdoms,%emptyok);
1.101     raeburn  4267:     if ($context eq 'domain') {
                   4268:         $setting = $env{'form.roleaction'};
1.184.4.10.2.  (raeburn 4269:):         if (exists($fields{'names'})) {
                   4270:):             map { $emptyok{$_} = 1; } ('lastname','firstname','middlename');
                   4271:):         } else {
                   4272:):             if (exists($fields{'lname'})) {
                   4273:):                 $emptyok{'lastname'} = 1;
                   4274:):             }
                   4275:):             if (exists($fields{'fname'})) {
                   4276:):                 $emptyok{'firstname'} = 1;
                   4277:):             }
                   4278:):             if (exists($fields{'mname'})) {
                   4279:):                 $emptyok{'middlename'} = 1;
                   4280:):             }
                   4281:):         }
                   4282:):         if (exists($fields{'gen'})) {
                   4283:):             $emptyok{'generation'} = 1;
                   4284:):         }
1.101     raeburn  4285:     }
                   4286:     if ($env{'request.course.id'} ne '') {
                   4287:         $cid = $env{'request.course.id'};
                   4288:         $crstype = &Apache::loncommon::course_type();
1.184.4.10.2.  (raeburn 4289:):         $crsdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4290:):         $crsnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.101     raeburn  4291:     } elsif ($setting eq 'course') {
                   4292:         if (&Apache::lonnet::is_course($env{'form.dcdomain'},$env{'form.dccourse'})) {
                   4293:             $cid = $env{'form.dcdomain'}.'_'.$env{'form.dccourse'};
                   4294:             $crstype = &Apache::loncommon::course_type($cid);
1.184.4.10.2.  (raeburn 4295:):             $crsdom = $env{'form.dcdomain'};
                   4296:):             $crsnum = $env{'form.dccourse'};
                   4297:):             if (exists($env{'course.'.$cid.'.internal.userdomains'})) {
                   4298:):                 $oldcrsuserdoms = 1;
                   4299:):             }
                   4300:):             my %coursedesc = &Apache::lonnet::coursedescription($cid,{ one_time => 1 });
                   4301:):             $env{'course.'.$cid.'.internal.userdomains'} = $coursedesc{'internal.userdomains'};
1.101     raeburn  4302:         }
                   4303:     }
1.1       raeburn  4304:     my ($startdate,$enddate) = &get_dates_from_form();
                   4305:     if ($env{'form.makedatesdefault'}) {
1.101     raeburn  4306:         $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  4307:     }
                   4308:     # Determine domain and desired host (home server)
1.57      raeburn  4309:     my $defdom=$env{'request.role.domain'};
                   4310:     my $domain;
                   4311:     if ($env{'form.defaultdomain'} ne '') {
                   4312:         $domain = $env{'form.defaultdomain'};
                   4313:     } else {
                   4314:         $domain = $defdom;
                   4315:     }
1.1       raeburn  4316:     my $desiredhost = $env{'form.lcserver'};
                   4317:     if (lc($desiredhost) eq 'default') {
                   4318:         $desiredhost = undef;
                   4319:     } else {
1.57      raeburn  4320:         my %home_servers = &Apache::lonnet::get_servers($defdom,'library');
1.1       raeburn  4321:         if (! exists($home_servers{$desiredhost})) {
1.184.4.1  raeburn  4322:             $r->print('<p class="LC_error">'.&mt('Error').': '.
                   4323:                       &mt('Invalid home server specified').'</p>');
1.1       raeburn  4324:             $r->print(&Apache::loncommon::end_page());
1.184.4.1  raeburn  4325:             return 'invalidhome';
1.1       raeburn  4326:         }
                   4327:     }
                   4328:     # Determine authentication mechanism
                   4329:     my $changeauth;
                   4330:     if ($context eq 'domain') {
                   4331:         $changeauth = $env{'form.changeauth'};
                   4332:     }
                   4333:     my $amode  = '';
                   4334:     my $genpwd = '';
1.184.4.4  raeburn  4335:     my @genpwdfail;
1.1       raeburn  4336:     if ($env{'form.login'} eq 'krb') {
                   4337:         $amode='krb';
                   4338:         $amode.=$env{'form.krbver'};
                   4339:         $genpwd=$env{'form.krbarg'};
                   4340:     } elsif ($env{'form.login'} eq 'int') {
                   4341:         $amode='internal';
                   4342:         if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
                   4343:             $genpwd=$env{'form.intarg'};
1.184.4.4  raeburn  4344:             @genpwdfail =
                   4345:                 &Apache::loncommon::check_passwd_rules($domain,$genpwd);
1.1       raeburn  4346:         }
                   4347:     } elsif ($env{'form.login'} eq 'loc') {
                   4348:         $amode='localauth';
                   4349:         if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
                   4350:             $genpwd=$env{'form.locarg'};
                   4351:         }
                   4352:     }
                   4353:     if ($amode =~ /^krb/) {
                   4354:         if (! defined($genpwd) || $genpwd eq '') {
                   4355:             $r->print('<span class="Error">'.
                   4356:                       &mt('Unable to enroll users').' '.
                   4357:                       &mt('No Kerberos domain was specified.').'</span></p>');
                   4358:             $amode = ''; # This causes the loop below to be skipped
                   4359:         }
                   4360:     }
1.150     raeburn  4361:     my ($defaultsec,$defaultrole,$defaultcredits,$commoncredits);
1.1       raeburn  4362:     if ($context eq 'domain') {
                   4363:         if ($setting eq 'domain') {
                   4364:             $defaultrole = $env{'form.defaultrole'};
                   4365:         } elsif ($setting eq 'course') {
                   4366:             $defaultrole = $env{'form.courserole'};
1.27      raeburn  4367:             $defaultsec = $env{'form.sections'};
1.150     raeburn  4368:             if ($showcredits) {
                   4369:                 $commoncredits = $env{'form.credits'};
                   4370:                 if ($crstype ne 'Community') {
                   4371:                     my %coursehash=&Apache::lonnet::coursedescription($cid);
                   4372:                     $defaultcredits = $coursehash{'internal.defaultcredits'};
                   4373:                 }
                   4374:             }
1.149     raeburn  4375:         }
1.13      raeburn  4376:     } elsif ($context eq 'author') {
1.1       raeburn  4377:         $defaultrole = $env{'form.defaultrole'};
1.27      raeburn  4378:     } elsif ($context eq 'course') {
                   4379:         $defaultrole = $env{'form.defaultrole'};
                   4380:         $defaultsec = $env{'form.sections'};
1.150     raeburn  4381:         if ($showcredits) {
                   4382:             $commoncredits = $env{'form.credits'};
                   4383:             $defaultcredits = $env{'course.'.$cid.'.internal.defaultcredits'};
                   4384:         }
1.1       raeburn  4385:     }
1.27      raeburn  4386:     # Check to see if user information can be changed
                   4387:     my @userinfo = ('firstname','middlename','lastname','generation',
                   4388:                     'permanentemail','id');
                   4389:     my %canmodify;
                   4390:     if (&Apache::lonnet::allowed('mau',$domain)) {
1.84      raeburn  4391:         push(@userinfo,'inststatus');
1.27      raeburn  4392:         foreach my $field (@userinfo) {
                   4393:             $canmodify{$field} = 1;
                   4394:         }
                   4395:     }
                   4396:     my (%userlist,%modifiable_fields,@poss_roles);
                   4397:     my $secidx = &Apache::loncoursedata::CL_SECTION();
1.102     raeburn  4398:     my @courseroles = &roles_by_context('course',1,$crstype);
1.27      raeburn  4399:     if (!&Apache::lonnet::allowed('mau',$domain)) {
                   4400:         if ($context eq 'course' || $context eq 'author') {
1.101     raeburn  4401:             @poss_roles =  &curr_role_permissions($context,'','',$crstype);
1.27      raeburn  4402:             my @statuses = ('active','future');
                   4403:             my ($indexhash,$keylist) = &make_keylist_array();
                   4404:             my %info;
                   4405:             foreach my $role (@poss_roles) {
                   4406:                 %{$modifiable_fields{$role}} = &can_modify_userinfo($context,$domain,
                   4407:                                                         \@userinfo,[$role]);
                   4408:             }
                   4409:             if ($context eq 'course') {
                   4410:                 my ($cnum,$cdom) = &get_course_identity();
                   4411:                 my $roster = &Apache::loncoursedata::get_classlist();
1.66      raeburn  4412:                 if (ref($roster) eq 'HASH') {
                   4413:                     %userlist = %{$roster};
                   4414:                 }
1.27      raeburn  4415:                 my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   4416:                                                          \@statuses,\@poss_roles);
                   4417:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   4418:                                 \%advrolehash,$permission);
                   4419:             } elsif ($context eq 'author') {
                   4420:                 my %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   4421:                                                   \@statuses,\@poss_roles);
                   4422:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   4423:                              \%cstr_roles,$permission);
                   4424:             }
                   4425:         }
1.1       raeburn  4426:     }
1.184.4.1  raeburn  4427:     if ($datatoken eq '') {
                   4428:         $r->print('<p class="LC_error">'.&mt('Error').': '.
                   4429:                   &mt('Invalid datatoken').'</p>');
                   4430:         return 'missingdata';
                   4431:     }
1.1       raeburn  4432:     if ( $domain eq &LONCAPA::clean_domain($domain)
                   4433:         && ($amode ne '')) {
                   4434:         #######################################
                   4435:         ##         Add/Modify Users          ##
                   4436:         #######################################
                   4437:         if ($context eq 'course') {
                   4438:             $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13      raeburn  4439:         } elsif ($context eq 'author') {
1.1       raeburn  4440:             $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
                   4441:         } else {
                   4442:             $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
                   4443:         }
1.87      bisitz   4444:         $r->rflush;
                   4445: 
1.1       raeburn  4446:         my %counts = (
                   4447:                        user => 0,
                   4448:                        auth => 0,
                   4449:                        role => 0,
                   4450:                      );
                   4451:         my $flushc=0;
                   4452:         my %student=();
1.42      raeburn  4453:         my (%curr_groups,@sections,@cleansec,$defaultwarn,$groupwarn);
1.1       raeburn  4454:         my %userchg;
1.27      raeburn  4455:         if ($context eq 'course' || $setting eq 'course') {
                   4456:             if ($context eq 'course') {
                   4457:                 # Get information about course groups
                   4458:                 %curr_groups = &Apache::longroup::coursegroups();
                   4459:             } elsif ($setting eq 'course') {
                   4460:                 if ($cid) {
                   4461:                     %curr_groups =
                   4462:                         &Apache::longroup::coursegroups($env{'form.dcdomain'},
                   4463:                                                         $env{'form.dccourse'});
                   4464:                 }
                   4465:             }
                   4466:             # determine section number
                   4467:             if ($defaultsec =~ /,/) {
                   4468:                 push(@sections,split(/,/,$defaultsec));
                   4469:             } else {
                   4470:                 push(@sections,$defaultsec);
                   4471:             }
                   4472:             # remove non alphanumeric values from section
                   4473:             foreach my $item (@sections) {
                   4474:                 $item =~ s/\W//g;
                   4475:                 if ($item eq "none" || $item eq 'all') {
                   4476:                     $defaultwarn = &mt('Default section name [_1] could not be used as it is a reserved word.',$item);
                   4477:                 } elsif ($item ne ''  && exists($curr_groups{$item})) {
                   4478:                     $groupwarn = &mt('Default section name "[_1]" is the name of a course group. Section names and group names must be distinct.',$item);
                   4479:                 } elsif ($item ne '') {
                   4480:                     push(@cleansec,$item);
                   4481:                 }
                   4482:             }
                   4483:             if ($defaultwarn) {
                   4484:                 $r->print($defaultwarn.'<br />');
                   4485:             }
                   4486:             if ($groupwarn) {
                   4487:                 $r->print($groupwarn.'<br />');
                   4488:             }
1.1       raeburn  4489:         }
1.124     raeburn  4490:         my (%curr_rules,%got_rules,%alerts,%cancreate);
1.104     raeburn  4491:         my %customroles = &my_custom_roles($crstype);
1.101     raeburn  4492:         my @permitted_roles = 
1.124     raeburn  4493:             &roles_on_upload($context,$setting,$crstype,%customroles);
                   4494:         my %longtypes = &Apache::lonlocal::texthash(
                   4495:                             official   => 'Institutional',
                   4496:                             unofficial => 'Non-institutional',
                   4497:                         );
1.135     raeburn  4498:         my $newuserdom = $env{'request.role.domain'};
                   4499:         map { $cancreate{$_} = &can_create_user($newuserdom,$context,$_); } keys(%longtypes);
1.1       raeburn  4500:         # Get new users list
1.184.4.4  raeburn  4501:         my (%existinguser,%userinfo,%disallow,%rulematch,%inst_results,%alerts,%checkuname,
                   4502:             %showpasswdrules,$haspasswdmap);
1.171     raeburn  4503:         my $counter = -1;
1.27      raeburn  4504:         foreach my $line (@userdata) {
1.171     raeburn  4505:             $counter ++;
1.42      raeburn  4506:             my @secs;
1.27      raeburn  4507:             my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  4508:             # Determine user name
1.128     raeburn  4509:             $entries{$fields{'username'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4510:             unless (($entries{$fields{'username'}} eq '') ||
                   4511:                     (!defined($entries{$fields{'username'}}))) {
                   4512:                 my ($fname, $mname, $lname,$gen) = ('','','','');
                   4513:                 if (defined($fields{'names'})) {
                   4514:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   4515:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
                   4516:                 } else {
                   4517:                     if (defined($fields{'fname'})) {
                   4518:                         $fname=$entries{$fields{'fname'}};
                   4519:                     }
                   4520:                     if (defined($fields{'mname'})) {
                   4521:                         $mname=$entries{$fields{'mname'}};
                   4522:                     }
                   4523:                     if (defined($fields{'lname'})) {
                   4524:                         $lname=$entries{$fields{'lname'}};
                   4525:                     }
                   4526:                     if (defined($fields{'gen'})) {
                   4527:                         $gen=$entries{$fields{'gen'}};
                   4528:                     }
                   4529:                 }
1.128     raeburn  4530: 
1.1       raeburn  4531:                 if ($entries{$fields{'username'}}
                   4532:                     ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
1.128     raeburn  4533:                     my $nowhitespace;
                   4534:                     if ($entries{$fields{'username'}} =~ /\s/) {
                   4535:                         $nowhitespace = ' - '.&mt('usernames may not contain spaces.');
                   4536:                     }
1.171     raeburn  4537:                     $disallow{$counter} =
1.157     bisitz   4538:                         &mt('Unacceptable username [_1] for user [_2] [_3] [_4] [_5]',
1.171     raeburn  4539:                             '"<b>'.$entries{$fields{'username'}}.'</b>"',
                   4540:                             $fname,$mname,$lname,$gen).$nowhitespace;
1.27      raeburn  4541:                     next;
1.1       raeburn  4542:                 } else {
1.129     raeburn  4543:                     $entries{$fields{'domain'}} =~ s/^\s+|\s+$//g;
1.71      droeschl 4544:                     if ($entries{$fields{'domain'}} 
1.57      raeburn  4545:                         ne &LONCAPA::clean_domain($entries{$fields{'domain'}})) {
1.171     raeburn  4546:                         $disallow{$counter} =
1.157     bisitz   4547:                             &mt('Unacceptable domain [_1] for user [_2] [_3] [_4] [_5]',
1.171     raeburn  4548:                                 '"<b>'.$entries{$fields{'domain'}}.'</b>"',
                   4549:                                 $fname,$mname,$lname,$gen);
                   4550:                         next;
1.57      raeburn  4551:                     }
1.5       raeburn  4552:                     my $username = $entries{$fields{'username'}};
1.57      raeburn  4553:                     my $userdomain = $entries{$fields{'domain'}};
                   4554:                     if ($userdomain eq '') {
                   4555:                         $userdomain = $domain;
                   4556:                     }
1.27      raeburn  4557:                     if (defined($fields{'sec'})) {
                   4558:                         if (defined($entries{$fields{'sec'}})) {
1.42      raeburn  4559:                             $entries{$fields{'sec'}} =~ s/\W//g;
1.27      raeburn  4560:                             my $item = $entries{$fields{'sec'}};
                   4561:                             if ($item eq "none" || $item eq 'all') {
1.171     raeburn  4562:                                 $disallow{$counter} =
                   4563:                                     &mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a reserved word.',
                   4564:                                         '<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item);
1.27      raeburn  4565:                                 next;
                   4566:                             } elsif (exists($curr_groups{$item})) {
1.171     raeburn  4567:                                 $disallow{$counter} =
                   4568:                                     &mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a course group.',
                   4569:                                         '<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item).' '.
                   4570:                                     &mt('Section names and group names must be distinct.');
1.27      raeburn  4571:                                 next;
                   4572:                             } else {
                   4573:                                 push(@secs,$item);
                   4574:                             }
                   4575:                         }
                   4576:                     }
                   4577:                     if ($env{'request.course.sec'} ne '') {
                   4578:                         @secs = ($env{'request.course.sec'});
1.57      raeburn  4579:                         if (ref($userlist{$username.':'.$userdomain}) eq 'ARRAY') {
                   4580:                             my $currsec = $userlist{$username.':'.$userdomain}[$secidx];
1.27      raeburn  4581:                             if ($currsec ne $env{'request.course.sec'}) {
1.171     raeburn  4582:                                 $disallow{$counter} =
                   4583:                                     &mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]".',
                   4584:                                         '<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$secs[0]);
1.27      raeburn  4585:                                 if ($currsec eq '') {
1.171     raeburn  4586:                                     $disallow{$counter} .=
                   4587:                                         &mt('This user already has an active/future student role in the course, unaffiliated to any section.');
1.27      raeburn  4588: 
                   4589:                                 } else {
1.171     raeburn  4590:                                     $disallow{$counter} .=
                   4591:                                         &mt('This user already has an active/future role in section "[_1]" of the course.',$currsec);
1.27      raeburn  4592:                                 }
1.171     raeburn  4593:                                 $disallow{$counter} .=
                   4594:                                     '<br />'.
                   4595:                                     &mt('Although your current role has privileges to add students to section "[_1]", you do not have privileges to modify existing enrollments in other sections.',
                   4596:                                         $secs[0]);
1.27      raeburn  4597:                                 next;
1.1       raeburn  4598:                             }
                   4599:                         }
1.27      raeburn  4600:                     } elsif ($context eq 'course' || $setting eq 'course') {
                   4601:                         if (@secs == 0) {
                   4602:                             @secs = @cleansec;
1.1       raeburn  4603:                         }
                   4604:                     }
                   4605:                     # determine id number
                   4606:                     my $id='';
                   4607:                     if (defined($fields{'id'})) {
                   4608:                         if (defined($entries{$fields{'id'}})) {
                   4609:                             $id=$entries{$fields{'id'}};
                   4610:                         }
                   4611:                         $id=~tr/A-Z/a-z/;
                   4612:                     }
                   4613:                     # determine email address
                   4614:                     my $email='';
                   4615:                     if (defined($fields{'email'})) {
1.129     raeburn  4616:                         $entries{$fields{'email'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4617:                         if (defined($entries{$fields{'email'}})) {
                   4618:                             $email=$entries{$fields{'email'}};
1.84      raeburn  4619:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
                   4620:                         }
                   4621:                     }
                   4622:                     # determine affiliation
                   4623:                     my $inststatus='';
                   4624:                     if (defined($fields{'inststatus'})) {
                   4625:                         if (defined($entries{$fields{'inststatus'}})) {
                   4626:                             $inststatus=$entries{$fields{'inststatus'}};
                   4627:                         }
1.1       raeburn  4628:                     }
                   4629:                     # determine user password
1.184.4.4  raeburn  4630:                     my $password;
                   4631:                     my $passwdfromfile;
1.1       raeburn  4632:                     if (defined($fields{'ipwd'})) {
                   4633:                         if ($entries{$fields{'ipwd'}}) {
                   4634:                             $password=$entries{$fields{'ipwd'}};
1.184.4.4  raeburn  4635:                             $passwdfromfile = 1;
                   4636:                             if ($env{'form.login'} eq 'int') {
                   4637:                                 my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
                   4638:                                 if (($uhome eq 'no_host') || ($changeauth)) {
                   4639:                                     my @brokepwdrules =
                   4640:                                         &Apache::loncommon::check_passwd_rules($domain,$password);
                   4641:                                     if (@brokepwdrules) {
                   4642:                                         $disallow{$counter} = &mt('[_1]: Password included in file for this user did not meet requirements.',
                   4643:                                                                   '<b>'.$username.'</b>');
                   4644:                                         map { $showpasswdrules{$_} = 1; } @brokepwdrules;
                   4645:                                         next;
                   4646:                                     }
                   4647:                                 }
                   4648:                             }
1.1       raeburn  4649:                         }
                   4650:                     }
1.184.4.4  raeburn  4651:                     unless ($passwdfromfile) {
                   4652:                         if ($env{'form.login'} eq 'int') {
                   4653:                             if (@genpwdfail) {
                   4654:                                 my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
                   4655:                                 if (($uhome eq 'no_host') || ($changeauth)) {
                   4656:                                     $disallow{$counter} = &mt('[_1]: No specific password in file for this user; default password did not meet requirements',
                   4657:                                                               '<b>'.$username.'</b>');
                   4658:                                     unless ($haspasswdmap) {
                   4659:                                         map { $showpasswdrules{$_} = 1; } @genpwdfail;
                   4660:                                         $haspasswdmap = 1;
                   4661:                                     }
                   4662:                                 }
                   4663:                                 next;
                   4664:                             }
                   4665:                         }
                   4666:                         $password = $genpwd;
                   4667:                     }
1.1       raeburn  4668:                     # determine user role
                   4669:                     my $role = '';
                   4670:                     if (defined($fields{'role'})) {
                   4671:                         if ($entries{$fields{'role'}}) {
1.42      raeburn  4672:                             $entries{$fields{'role'}}  =~ s/(\s+$|^\s+)//g;
                   4673:                             if ($entries{$fields{'role'}} ne '') {
                   4674:                                 if (grep(/^\Q$entries{$fields{'role'}}\E$/,@permitted_roles)) {
                   4675:                                     $role = $entries{$fields{'role'}};
1.27      raeburn  4676:                                 }
                   4677:                             }
                   4678:                             if ($role eq '') {
                   4679:                                 my $rolestr = join(', ',@permitted_roles);
1.171     raeburn  4680:                                 $disallow{$counter} =
                   4681:                                     &mt('[_1]: You do not have permission to add the requested role [_2] for the user.'
                   4682:                                         ,'<b>'.$entries{$fields{'username'}}.'</b>'
                   4683:                                         ,$entries{$fields{'role'}})
                   4684:                                         .'<br />'
                   4685:                                         .&mt('Allowable role(s) is/are: [_1].',$rolestr);
1.1       raeburn  4686:                                 next;
                   4687:                             }
                   4688:                         }
                   4689:                     }
                   4690:                     if ($role eq '') {
                   4691:                         $role = $defaultrole;
                   4692:                     }
                   4693:                     # Clean up whitespace
1.129     raeburn  4694:                     foreach (\$id,\$fname,\$mname,\$lname,\$gen,\$inststatus) {
1.1       raeburn  4695:                         $$_ =~ s/(\s+$|^\s+)//g;
                   4696:                     }
1.150     raeburn  4697:                     my $credits;
                   4698:                     if ($showcredits) {
                   4699:                         if (($role eq 'st') && ($crstype ne 'Community')) {
                   4700:                             $credits = $entries{$fields{'credits'}};
                   4701:                             if ($credits ne '') {
                   4702:                                 $credits =~ s/[^\d\.]//g;
                   4703:                             }
                   4704:                             if ($credits eq '') {
                   4705:                                 $credits = $commoncredits;
                   4706:                             }
                   4707:                             if ($credits eq $defaultcredits) {
                   4708:                                 undef($credits);
                   4709:                             }
                   4710:                         }
                   4711:                     }
1.5       raeburn  4712:                     # check against rules
                   4713:                     my $checkid = 0;
                   4714:                     my $newuser = 0;
1.57      raeburn  4715:                     my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
1.5       raeburn  4716:                     if ($uhome eq 'no_host') {
1.135     raeburn  4717:                         if ($userdomain ne $newuserdom) {
                   4718:                             if ($context eq 'course') {
1.171     raeburn  4719:                                 $disallow{$counter} =
                   4720:                                     &mt('[_1]: The domain specified ([_2]) is different to that of the course.',
                   4721:                                        '<b>'.$username.'</b>',$userdomain);
1.135     raeburn  4722:                             } elsif ($context eq 'author') {
1.171     raeburn  4723:                                 $disallow{$counter} =
                   4724:                                     &mt('[_1]: The domain specified ([_2]) is different to that of the author.',
                   4725:                                         '<b>'.$username.'</b>',$userdomain); 
1.135     raeburn  4726:                             } else {
1.171     raeburn  4727:                                 $disallow{$counter} =
                   4728:                                     &mt('[_1]: The domain specified ([_2]) is different to that of your current role.',
                   4729:                                         '<b>'.$username.'</b>',$userdomain);
1.135     raeburn  4730:                             }
1.171     raeburn  4731:                             $disallow{$counter} .=
                   4732:                                 &mt('The user does not already exist, and you may not create a new user in a different domain.');
1.124     raeburn  4733:                             next;
1.171     raeburn  4734:                         } else {
                   4735:                             unless ($password || $env{'form.login'} eq 'loc') {
                   4736:                                 $disallow{$counter} =
                   4737:                                     &mt('[_1]: This is a new user but no default password was provided, and the authentication type requires one.',
                   4738:                                         '<b>'.$username.'</b>');
                   4739:                                 next;
                   4740:                             }
1.124     raeburn  4741:                         }
1.5       raeburn  4742:                         $checkid = 1;
                   4743:                         $newuser = 1;
1.172     raeburn  4744:                         $checkuname{$username.':'.$newuserdom} = { 'newuser' => $newuser, 'id' => $id };
1.13      raeburn  4745:                     } else {
1.27      raeburn  4746:                         if ($context eq 'course' || $context eq 'author') {
1.57      raeburn  4747:                             if ($userdomain eq $domain ) {
                   4748:                                 if ($role eq '') {
                   4749:                                     my @checkroles;
                   4750:                                     foreach my $role (@poss_roles) {
                   4751:                                         my $endkey;
                   4752:                                         if ($role ne 'st') {
                   4753:                                             $endkey = ':'.$role;
                   4754:                                         }
                   4755:                                         if (exists($userlist{$username.':'.$userdomain.$endkey})) {
                   4756:                                             if (!grep(/^\Q$role\E$/,@checkroles)) {
                   4757:                                                 push(@checkroles,$role);
                   4758:                                             }
                   4759:                                         }
1.27      raeburn  4760:                                     }
1.57      raeburn  4761:                                     if (@checkroles > 0) {
                   4762:                                         %canmodify = &can_modify_userinfo($context,$domain,\@userinfo,\@checkroles);
1.27      raeburn  4763:                                     }
1.57      raeburn  4764:                                 } elsif (ref($modifiable_fields{$role}) eq 'HASH') {
                   4765:                                     %canmodify = %{$modifiable_fields{$role}};
1.27      raeburn  4766:                                 }
                   4767:                             }
1.57      raeburn  4768:                             my @newinfo = (\$fname,\$mname,\$lname,\$gen,\$email,\$id);
1.84      raeburn  4769:                             for (my $i=0; $i<@newinfo; $i++) {
1.57      raeburn  4770:                                 if (${$newinfo[$i]} ne '') {
                   4771:                                     if (!$canmodify{$userinfo[$i]}) {
                   4772:                                         ${$newinfo[$i]} = '';
                   4773:                                     }
1.27      raeburn  4774:                                 }
                   4775:                             }
                   4776:                         }
1.171     raeburn  4777:                         if ($id) {
                   4778:                             $existinguser{$userdomain}{$username} = $id;
                   4779:                         }
1.5       raeburn  4780:                     }
1.171     raeburn  4781:                     $userinfo{$counter} = {
                   4782:                                           username   => $username,
                   4783:                                           domain     => $userdomain,
                   4784:                                           fname      => $fname,
                   4785:                                           mname      => $mname,
                   4786:                                           lname      => $lname,
                   4787:                                           gen        => $gen,
                   4788:                                           email      => $email,
                   4789:                                           id         => $id, 
                   4790:                                           password   => $password,
                   4791:                                           inststatus => $inststatus,
                   4792:                                           role       => $role,
                   4793:                                           sections   => \@secs,
                   4794:                                           credits    => $credits,
                   4795:                                           newuser    => $newuser,
                   4796:                                           checkid    => $checkid,
                   4797:                                         };
                   4798:                 }
                   4799:             }
                   4800:         } # end of foreach (@userdata)
                   4801:         if ($counter > -1) {
                   4802:             my $total = $counter + 1;
                   4803:             my %checkids;
1.172     raeburn  4804:             if ((keys(%existinguser)) || (keys(%checkuname))) {
                   4805:                 $r->print(&mt('Please be patient -- checking for institutional data ...'));
                   4806:                 $r->rflush();
                   4807:                 if (keys(%existinguser)) {
                   4808:                     foreach my $dom (keys(%existinguser)) {
                   4809:                         if (ref($existinguser{$dom}) eq 'HASH') {
                   4810:                             my %idhash = &Apache::lonnet::idrget($dom,keys(%{$existinguser{$dom}}));
                   4811:                             foreach my $username (keys(%{$existinguser{$dom}})) {
                   4812:                                 if ($idhash{$username} ne $existinguser{$dom}{$username}) {
                   4813:                                     $checkids{$username.':'.$dom} = {
                   4814:                                                                     'id' => $existinguser{$dom}{$username},
                   4815:                                                                     };
                   4816:                                 }
                   4817:                             }
                   4818:                             if (keys(%checkids)) {
                   4819:                                 &Apache::loncommon::user_rule_check(\%checkids,{ 'id' => 1 },
                   4820:                                                                     \%alerts,\%rulematch,
                   4821:                                                                     \%inst_results,\%curr_rules,
                   4822:                                                                     \%got_rules);
1.171     raeburn  4823:                             }
                   4824:                         }
                   4825:                     }
                   4826:                 }
1.172     raeburn  4827:                 if (keys(%checkuname)) {
                   4828:                     &Apache::loncommon::user_rule_check(\%checkuname,{ 'username' => 1, 'id' => 1, },
                   4829:                                                         \%alerts,\%rulematch,\%inst_results,
                   4830:                                                         \%curr_rules,\%got_rules);
                   4831:                 }
                   4832:                 $r->print(' '.&mt('done').'<br /><br />');
                   4833:                 $r->rflush();
1.171     raeburn  4834:             }
1.172     raeburn  4835:             my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,$total);
1.171     raeburn  4836:             $r->print('<ul>');
                   4837:             for (my $i=0; $i<=$counter; $i++) {
                   4838:                 if ($disallow{$i}) {
                   4839:                     $r->print('<li>'.$disallow{$i}.'</li>');
                   4840:                 } elsif (ref($userinfo{$i}) eq 'HASH') {
                   4841:                     my $password = $userinfo{$i}{'password'}; 
                   4842:                     my $newuser = $userinfo{$i}{'newuser'};
                   4843:                     my $checkid = $userinfo{$i}{'checkid'};
                   4844:                     my $id = $userinfo{$i}{'id'};
                   4845:                     my $role = $userinfo{$i}{'role'};
                   4846:                     my @secs;
                   4847:                     if (ref($userinfo{$i}{'sections'}) eq 'ARRAY') {
                   4848:                         @secs = @{$userinfo{$i}{'sections'}};
                   4849:                     }
                   4850:                     my $fname = $userinfo{$i}{'fname'};
                   4851:                     my $mname = $userinfo{$i}{'mname'}; 
                   4852:                     my $lname = $userinfo{$i}{'lname'};
                   4853:                     my $gen = $userinfo{$i}{'gen'};
                   4854:                     my $email = $userinfo{$i}{'email'};
                   4855:                     my $inststatus = $userinfo{$i}{'inststatus'};
                   4856:                     my $credits = $userinfo{$i}{'credits'};
                   4857:                     my $username = $userinfo{$i}{'username'};
                   4858:                     my $userdomain = $userinfo{$i}{'domain'};
                   4859:                     my $user = $username.':'.$userdomain;
                   4860:                     if ($newuser) {
                   4861:                         if (ref($alerts{'username'}) eq 'HASH') {
                   4862:                             if (ref($alerts{'username'}{$userdomain}) eq 'HASH') {
                   4863:                                 if ($alerts{'username'}{$userdomain}{$username}) {
                   4864:                                     $r->print('<li>'.
                   4865:                                               &mt('[_1]: matches the username format at your institution, but is not known to your directory service.','<b>'.$username.'</b>').'<br />'.
                   4866:                                               &mt('Consequently, the user was not created.').'</li>');
                   4867:                                     next;
                   4868:                                 }
                   4869:                             }
                   4870:                         }
1.172     raeburn  4871:                         if (ref($inst_results{$user}) eq 'HASH') {
                   4872:                             if ($inst_results{$user}{'firstname'} ne '') {
                   4873:                                 $fname = $inst_results{$user}{'firstname'};
                   4874:                             }
                   4875:                             if ($inst_results{$user}{'middlename'} ne '') {
                   4876:                                 $mname = $inst_results{$user}{'middlename'};
                   4877:                             }
                   4878:                             if ($inst_results{$user}{'lasttname'} ne '') {
                   4879:                                 $lname = $inst_results{$user}{'lastname'};
                   4880:                             }
                   4881:                             if ($inst_results{$user}{'permanentemail'} ne '') {
                   4882:                                 $email = $inst_results{$user}{'permanentemail'};
                   4883:                             }
                   4884:                             if ($inst_results{$user}{'id'} ne '') {
                   4885:                                 $id = $inst_results{$user}{'id'};
                   4886:                                 $checkid = 0;
                   4887:                             }
                   4888:                             if (ref($inst_results{$user}{'inststatus'}) eq 'ARRAY') {
                   4889:                                 $inststatus = join(':',@{$inst_results{$user}{'inststatus'}});
                   4890:                             }
                   4891:                         }
                   4892:                         if (($checkid) && ($id ne '')) {
                   4893:                             if (ref($alerts{'id'}) eq 'HASH') {
                   4894:                                 if (ref($alerts{'id'}{$userdomain}) eq 'HASH') {
                   4895:                                     if ($alerts{'id'}{$userdomain}{$username}) {
                   4896:                                         $r->print('<li>'.
                   4897:                                                   &mt('[_1]: has a student/employee ID matching the format at your institution, but the ID is not found by your directory service.',
                   4898:                                                   '<b>'.$username.'</b>').'<br />'.
                   4899:                                                   &mt('Consequently, the user was not created.').'</li>');
                   4900:                                         next;
                   4901:                                     }
                   4902:                                 }
                   4903:                             }
                   4904:                         }
1.171     raeburn  4905:                         my $usertype = 'unofficial';
                   4906:                         if (ref($rulematch{$user}) eq 'HASH') {
                   4907:                             if ($rulematch{$user}{'username'}) {
                   4908:                                 $usertype = 'official';
1.5       raeburn  4909:                             }
                   4910:                         }
1.171     raeburn  4911:                         unless ($cancreate{$usertype}) {
                   4912:                             my $showtype = $longtypes{$usertype};
                   4913:                             $r->print('<li>'.
                   4914:                                       &mt('[_1]: The user does not exist, and you are not permitted to create users of type: [_2].','<b>'.$username.'</b>',$showtype).'</li>');
                   4915:                             next;
                   4916:                         }
1.172     raeburn  4917:                     } elsif ($id ne '') {
1.171     raeburn  4918:                         if (exists($checkids{$user})) {
                   4919:                             $checkid = 1; 
1.5       raeburn  4920:                             if (ref($alerts{'id'}) eq 'HASH') {
1.57      raeburn  4921:                                 if (ref($alerts{'id'}{$userdomain}) eq 'HASH') {
1.172     raeburn  4922:                                     if ($alerts{'id'}{$userdomain}{$username}) {
1.171     raeburn  4923:                                         $r->print('<li>'.
1.172     raeburn  4924:                                                   &mt('[_1]: has a student/employee ID matching the format at your institution, but the ID is not found by your directory service.',
1.124     raeburn  4925:                                                   '<b>'.$username.'</b>').'<br />'.
1.172     raeburn  4926:                                                   &mt('Consequently, the ID was not changed.').'</li>');
                   4927:                                         $id = '';
1.124     raeburn  4928:                                     }
1.5       raeburn  4929:                                 }
                   4930:                             }
                   4931:                         }
                   4932:                     }
1.171     raeburn  4933:                     my $multiple = 0;
                   4934:                     my ($userresult,$authresult,$roleresult,$idresult);
                   4935:                     my (%userres,%authres,%roleres,%idres);
                   4936:                     my $singlesec = '';
                   4937:                     if ($role eq 'st') {
1.184.4.9  raeburn  4938:                         if (($context eq 'domain') && ($changeauth eq 'Yes') && (!$newuser)) {
                   4939:                             if ((&Apache::lonnet::allowed('mau',$userdomain)) &&
                   4940:                                 (&Apache::lonnet::homeserver($username,$userdomain) ne 'no_host')) {
                   4941:                                 if ((($amode =~ /^krb4|krb5|internal$/) && $password ne '') ||
                   4942:                                      ($amode eq 'localauth')) {
                   4943:                                     $authresult =
                   4944:                                         &Apache::lonnet::modifyuserauth($userdomain,$username,$amode,$password);
                   4945:                                 }
                   4946:                             }
                   4947:                         }
1.171     raeburn  4948:                         my $sec;
                   4949:                         if (ref($userinfo{$i}{'sections'}) eq 'ARRAY') {
1.42      raeburn  4950:                             if (@secs > 0) {
                   4951:                                 $sec = $secs[0];
1.27      raeburn  4952:                             }
1.171     raeburn  4953:                         }
                   4954:                         &modifystudent($userdomain,$username,$cid,$sec,
                   4955:                                        $desiredhost,$context);
                   4956:                         $roleresult =
                   4957:                             &Apache::lonnet::modifystudent
                   4958:                                 ($userdomain,$username,$id,$amode,$password,
                   4959:                                  $fname,$mname,$lname,$gen,$sec,$enddate,
                   4960:                                  $startdate,$env{'form.forceid'},
                   4961:                                  $desiredhost,$email,'manual','',$cid,
                   4962:                                  '',$context,$inststatus,$credits);
                   4963:                         $userresult = $roleresult;
                   4964:                     } else {
                   4965:                         if ($role ne '') { 
                   4966:                             if ($context eq 'course' || $setting eq 'course') {
                   4967:                                 if ($customroles{$role}) {
                   4968:                                     $role = 'cr_'.$env{'user.domain'}.'_'.
                   4969:                                             $env{'user.name'}.'_'.$role;
                   4970:                                 }
                   4971:                                 if (($role ne 'cc') && ($role ne 'co')) { 
                   4972:                                    if (@secs > 1) {
                   4973:                                         $multiple = 1;
                   4974:                                         foreach my $sec (@secs) {
                   4975:                                             ($userres{$sec},$authres{$sec},$roleres{$sec},$idres{$sec}) =
                   4976:                                             &modifyuserrole($context,$setting,
                   4977:                                                 $changeauth,$cid,$userdomain,$username,
                   4978:                                                 $id,$amode,$password,$fname,
                   4979:                                                 $mname,$lname,$gen,$sec,
                   4980:                                                 $env{'form.forceid'},$desiredhost,
                   4981:                                                 $email,$role,$enddate,
                   4982:                                                 $startdate,$checkid,$inststatus);
1.42      raeburn  4983:                                         }
1.171     raeburn  4984:                                     } elsif (@secs > 0) {
                   4985:                                         $singlesec = $secs[0];
1.27      raeburn  4986:                                     }
                   4987:                                 }
                   4988:                             }
1.184.4.7  raeburn  4989:                         }
                   4990:                         if (!$multiple) {
                   4991:                             ($userresult,$authresult,$roleresult,$idresult) = 
                   4992:                                 &modifyuserrole($context,$setting,
                   4993:                                                 $changeauth,$cid,$userdomain,$username, 
                   4994:                                                 $id,$amode,$password,$fname,
                   4995:                                                 $mname,$lname,$gen,$singlesec,
                   4996:                                                 $env{'form.forceid'},$desiredhost,
                   4997:                                                 $email,$role,$enddate,$startdate,
1.184.4.10.2.  (raeburn 4998:):                                                 $checkid,$inststatus,\%emptyok);
1.27      raeburn  4999:                         }
1.171     raeburn  5000:                     }
                   5001:                     if ($multiple) {
                   5002:                         foreach my $sec (sort(keys(%userres))) {
                   5003:                             $flushc =
1.27      raeburn  5004:                                 &user_change_result($r,$userres{$sec},$authres{$sec},
                   5005:                                                     $roleres{$sec},$idres{$sec},\%counts,$flushc,
1.57      raeburn  5006:                                                     $username,$userdomain,\%userchg);
1.27      raeburn  5007: 
1.1       raeburn  5008:                         }
                   5009:                     } else {
1.171     raeburn  5010:                         $flushc = 
                   5011:                             &user_change_result($r,$userresult,$authresult,
                   5012:                                                 $roleresult,$idresult,\%counts,$flushc,
                   5013:                                                 $username,$userdomain,\%userchg);
1.1       raeburn  5014:                     }
                   5015:                 }
1.172     raeburn  5016:                 &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last user');
1.171     raeburn  5017:             } # end of loop
1.172     raeburn  5018:             $r->print('</ul>');
1.171     raeburn  5019:             &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.184.4.10.2.  (raeburn 5020:):             if (($context eq 'domain') && ($setting eq 'course')) {
                   5021:):                 unless ($oldcrsuserdoms) {
                   5022:):                     if (exists($env{'course.'.$cid.'.internal.userdomains'})) {
                   5023:):                         delete($env{'course.'.$cid.'.internal.userdomains'});
                   5024:):                     }
                   5025:):                 }
                   5026:):             }
1.171     raeburn  5027:         }
1.1       raeburn  5028:         # Flush the course logs so reverse user roles immediately updated
1.126     raeburn  5029:         $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.29      raeburn  5030:         $r->print("</p>\n<p>\n".&mt('Processed [quant,_1,user].',$counts{'user'}).
1.1       raeburn  5031:                   "</p>\n");
                   5032:         if ($counts{'role'} > 0) {
                   5033:             $r->print("<p>\n".
1.184.4.1  raeburn  5034:                       &mt('Roles added for [quant,_1,user].',$counts{'role'}).' '.
                   5035:                       &mt('If a user is currently logged-in to LON-CAPA, any new roles which are active will be available when the user next logs in.').
                   5036:                       "</p>\n");
1.29      raeburn  5037:         } else {
                   5038:             $r->print('<p>'.&mt('No roles added').'</p>');
1.1       raeburn  5039:         }
                   5040:         if ($counts{'auth'} > 0) {
                   5041:             $r->print("<p>\n".
                   5042:                       &mt('Authentication changed for [_1] existing users.',
                   5043:                           $counts{'auth'})."</p>\n");
                   5044:         }
1.13      raeburn  5045:         $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.184.4.4  raeburn  5046:         $r->print(&passwdrule_alerts($domain,\%showpasswdrules));
1.1       raeburn  5047:         #####################################
1.29      raeburn  5048:         # Display list of students to drop  #
1.1       raeburn  5049:         #####################################
                   5050:         if ($env{'form.fullup'} eq 'yes') {
1.29      raeburn  5051:             $r->print('<h3>'.&mt('Students to Drop')."</h3>\n");
1.1       raeburn  5052:             #  Get current classlist
1.30      raeburn  5053:             my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  5054:             if (! defined($classlist)) {
1.184.4.1  raeburn  5055:                 $r->print('<p class="LC_info">'.
                   5056:                           &mt('There are no students with current/future access to the course.').
                   5057:                           '</p>'."\n");
1.66      raeburn  5058:             } elsif (ref($classlist) eq 'HASH') {
1.1       raeburn  5059:                 # Remove the students we just added from the list of students.
1.30      raeburn  5060:                 foreach my $line (@userdata) {
                   5061:                     my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  5062:                     unless (($entries{$fields{'username'}} eq '') ||
                   5063:                             (!defined($entries{$fields{'username'}}))) {
                   5064:                         delete($classlist->{$entries{$fields{'username'}}.
                   5065:                                                 ':'.$domain});
                   5066:                     }
                   5067:                 }
                   5068:                 # Print out list of dropped students.
1.30      raeburn  5069:                 &show_drop_list($r,$classlist,'nosort',$permission);
1.1       raeburn  5070:             }
                   5071:         }
                   5072:     } # end of unless
1.184.4.1  raeburn  5073:     return 'ok';
1.1       raeburn  5074: }
                   5075: 
1.13      raeburn  5076: sub print_namespacing_alerts {
                   5077:     my ($domain,$alerts,$curr_rules) = @_;
                   5078:     my $output;
                   5079:     if (ref($alerts) eq 'HASH') {
                   5080:         if (keys(%{$alerts}) > 0) {
                   5081:             if (ref($alerts->{'username'}) eq 'HASH') {
                   5082:                 foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
                   5083:                     my $count;
                   5084:                     if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
                   5085:                         $count = keys(%{$alerts->{'username'}{$dom}});
                   5086:                     }
                   5087:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   5088:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   5089:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   5090:                                         'username',$domdesc,$count,'upload');
                   5091:                     }
                   5092:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   5093:                                    $domdesc,$curr_rules->{$dom}{'username'},
                   5094:                                    'username');
                   5095:                 }
                   5096:             }
                   5097:             if (ref($alerts->{'id'}) eq 'HASH') {
                   5098:                 foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
                   5099:                     my $count;
                   5100:                     if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
                   5101:                         $count = keys(%{$alerts->{'id'}{$dom}});
                   5102:                     }
                   5103:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   5104:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   5105:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   5106:                                               'id',$domdesc,$count,'upload');
                   5107:                     }
                   5108:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   5109:                                     $domdesc,$curr_rules->{$dom}{'id'},'id');
                   5110:                 }
                   5111:             }
                   5112:         }
                   5113:     }
                   5114: }
                   5115: 
1.184.4.4  raeburn  5116: sub passwdrule_alerts {
                   5117:     my ($domain,$passwdrules) = @_;
                   5118:     my $warning;
                   5119:     if (ref($passwdrules) eq 'HASH') {
                   5120:         my %showrules = %{$passwdrules};
                   5121:         if (keys(%showrules)) {
                   5122:             my %passwdconf = &Apache::lonnet::get_passwdconf($domain);
                   5123:             $warning = '<b>'.&mt('Password requirement(s) unmet for one or more users:').'</b><ul>';
                   5124:             if ($showrules{'min'}) {
1.184.4.10  raeburn  5125:                 my $min = $passwdconf{'min'};
                   5126:                 if ($min eq '') {
                   5127:                     $min = $Apache::lonnet::passwdmin;
                   5128:                 }
                   5129:                 $warning .= '<li>'.&mt('minimum [quant,_1,character]',$min).'</li>';
1.184.4.4  raeburn  5130:             }
                   5131:             if ($showrules{'max'}) {
                   5132:                 $warning .= '<li>'.&mt('maximum [quant,_1,character]',$passwdconf{'max'}).'</li>';
                   5133:             }
                   5134:             if ($showrules{'uc'}) {
                   5135:                 $warning .= '<li>'.&mt('contain at least one upper case letter').'</li>';
                   5136:             }
                   5137:             if ($showrules{'lc'}) {
                   5138:                 $warning .= '<li>'.&mt('contain at least one lower case letter').'</li>';
                   5139:             }
                   5140:             if ($showrules{'num'}) {
                   5141:                 $warning .= '<li>'.&mt('contain at least one number').'</li>';
                   5142:             }
                   5143:             if ($showrules{'spec'}) {
                   5144:                 $warning .= '<li>'.&mt('contain at least one non-alphanumeric').'</li>';
                   5145:             }
                   5146:             $warning .= '</ul>';
                   5147:         }
                   5148:     }
                   5149:     return $warning;
                   5150: }
                   5151: 
1.1       raeburn  5152: sub user_change_result {
1.29      raeburn  5153:     my ($r,$userresult,$authresult,$roleresult,$idresult,$counts,$flushc,
1.57      raeburn  5154:         $username,$userdomain,$userchg) = @_;
1.1       raeburn  5155:     my $okresult = 0;
1.171     raeburn  5156:     my @status;
1.1       raeburn  5157:     if ($userresult ne 'ok') {
                   5158:         if ($userresult =~ /^error:(.+)$/) {
                   5159:             my $error = $1;
1.171     raeburn  5160:             push(@status,
                   5161:                  &mt('[_1]: Unable to add/modify: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  5162:         }
                   5163:     } else {
                   5164:         $counts->{'user'} ++;
                   5165:         $okresult = 1;
                   5166:     }
                   5167:     if ($authresult ne 'ok') {
                   5168:         if ($authresult =~ /^error:(.+)$/) {
                   5169:             my $error = $1;
1.171     raeburn  5170:             push(@status, 
                   5171:                  &mt('[_1]: Unable to modify authentication: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  5172:         } 
                   5173:     } else {
                   5174:         $counts->{'auth'} ++;
                   5175:         $okresult = 1;
                   5176:     }
                   5177:     if ($roleresult ne 'ok') {
                   5178:         if ($roleresult =~ /^error:(.+)$/) {
                   5179:             my $error = $1;
1.171     raeburn  5180:             push(@status,
                   5181:                  &mt('[_1]: Unable to add role: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  5182:         }
                   5183:     } else {
                   5184:         $counts->{'role'} ++;
                   5185:         $okresult = 1;
                   5186:     }
                   5187:     if ($okresult) {
                   5188:         $flushc++;
1.57      raeburn  5189:         $userchg->{$username.':'.$userdomain}=1;
1.1       raeburn  5190:         if ($flushc>15) {
                   5191:             $r->rflush;
                   5192:             $flushc=0;
                   5193:         }
                   5194:     }
1.29      raeburn  5195:     if ($idresult) {
1.171     raeburn  5196:         push(@status,$idresult);
                   5197:     }
                   5198:     if (@status) {
                   5199:         $r->print('<li>'.join('<br />',@status).'</li>');
1.29      raeburn  5200:     }
1.1       raeburn  5201:     return $flushc;
                   5202: }
                   5203: 
                   5204: # ========================================================= Menu Phase Two Drop
1.17      raeburn  5205: sub print_drop_menu {
1.101     raeburn  5206:     my ($r,$context,$permission,$crstype) = @_;
                   5207:     my $heading;
                   5208:     if ($crstype eq 'Community') {
                   5209:         $heading = &mt("Drop Members");
                   5210:     } else {
                   5211:         $heading = &mt("Drop Students");
                   5212:     }
                   5213:     $r->print('<h3>'.$heading.'</h3>'."\n".
1.153     bisitz   5214:               '<form name="studentform" method="post" action="">'."\n");
1.30      raeburn  5215:     my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  5216:     if (! defined($classlist)) {
1.143     bisitz   5217:         my $msg = '';
1.101     raeburn  5218:         if ($crstype eq 'Community') {
1.143     bisitz   5219:             $msg = &mt('There are no members currently enrolled.');
1.101     raeburn  5220:         } else {
1.143     bisitz   5221:             $msg = &mt('There are no students currently enrolled.');
1.101     raeburn  5222:         }
1.143     bisitz   5223:         $r->print('<p class="LC_info">'.$msg."</p>\n");
1.30      raeburn  5224:     } else {
1.101     raeburn  5225:         &show_drop_list($r,$classlist,'nosort',$permission,$crstype);
1.1       raeburn  5226:     }
1.162     bisitz   5227:     $r->print('</form>');
1.1       raeburn  5228:     return;
                   5229: }
                   5230: 
                   5231: # ================================================================== Phase four
                   5232: 
1.11      raeburn  5233: sub update_user_list {
1.118     raeburn  5234:     my ($r,$context,$setting,$choice,$crstype) = @_;
1.11      raeburn  5235:     my $now = time;
1.1       raeburn  5236:     my $count=0;
1.101     raeburn  5237:     if ($context eq 'course') {
                   5238:         $crstype = &Apache::loncommon::course_type();
                   5239:     }
1.11      raeburn  5240:     my @changelist;
1.29      raeburn  5241:     if ($choice eq 'drop') {
                   5242:         @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
                   5243:     } else {
1.11      raeburn  5244:         @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
                   5245:     }
                   5246:     my %result_text = ( ok    => { 'revoke'   => 'Revoked',
                   5247:                                    'delete'   => 'Deleted',
                   5248:                                    'reenable' => 'Re-enabled',
1.17      raeburn  5249:                                    'activate' => 'Activated',
                   5250:                                    'chgdates' => 'Changed Access Dates for',
1.118     raeburn  5251:                                    'chgsec'   => 'Changed section(s) for',
1.17      raeburn  5252:                                    'drop'     => 'Dropped',
1.11      raeburn  5253:                                  },
                   5254:                         error => {'revoke'    => 'revoking',
                   5255:                                   'delete'    => 'deleting',
                   5256:                                   'reenable'  => 're-enabling',
                   5257:                                   'activate'  => 'activating',
1.17      raeburn  5258:                                   'chgdates'  => 'changing access dates for',
                   5259:                                   'chgsec'    => 'changing section for',
                   5260:                                   'drop'      => 'dropping',
1.11      raeburn  5261:                                  },
                   5262:                       );
                   5263:     my ($startdate,$enddate);
                   5264:     if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
                   5265:         ($startdate,$enddate) = &get_dates_from_form();
                   5266:     }
                   5267:     foreach my $item (@changelist) {
1.118     raeburn  5268:         my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,
                   5269:             @sections,$scopestem,$singlesec,$showsecs,$warn_singlesec,
1.174     raeburn  5270:             $nothingtodo,$keepnosection,$credits,$instsec);
1.17      raeburn  5271:         if ($choice eq 'drop') {
                   5272:             ($uname,$udom,$sec) = split(/:/,$item,-1);
                   5273:             $role = 'st';
                   5274:             $cid = $env{'request.course.id'};
                   5275:             $scopestem = '/'.$cid;
                   5276:             $scopestem =~s/\_/\//g;
                   5277:             if ($sec eq '') {
                   5278:                 $scope = $scopestem;
                   5279:             } else {
                   5280:                 $scope = $scopestem.'/'.$sec;
                   5281:             }
                   5282:         } elsif ($context eq 'course') {
1.174     raeburn  5283:             ($uname,$udom,$role,$sec,$type,$locktype,$credits,$instsec) =
                   5284:                 split(/\:/,$item,8);
                   5285:             $instsec = &unescape($instsec);
1.11      raeburn  5286:             $cid = $env{'request.course.id'};
                   5287:             $scopestem = '/'.$cid;
                   5288:             $scopestem =~s/\_/\//g;
                   5289:             if ($sec eq '') {
                   5290:                 $scope = $scopestem;
                   5291:             } else {
                   5292:                 $scope = $scopestem.'/'.$sec;
                   5293:             }
1.13      raeburn  5294:         } elsif ($context eq 'author') {
1.11      raeburn  5295:             ($uname,$udom,$role) = split(/\:/,$item,-1);
                   5296:             $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                   5297:         } elsif ($context eq 'domain') {
                   5298:             if ($setting eq 'domain') {
                   5299:                 ($role,$uname,$udom) = split(/\:/,$item,-1);
                   5300:                 $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn  5301:             } elsif ($setting eq 'author') { 
1.11      raeburn  5302:                 ($uname,$udom,$role,$scope) = split(/\:/,$item);
                   5303:             } elsif ($setting eq 'course') {
1.174     raeburn  5304:                 ($uname,$udom,$role,$cid,$sec,$type,$locktype,$credits,$instsec) = 
                   5305:                     split(/\:/,$item,9);
                   5306:                 $instsec = &unescape($instsec);
1.11      raeburn  5307:                 $scope = '/'.$cid;
                   5308:                 $scope =~s/\_/\//g;
                   5309:                 if ($sec ne '') {
                   5310:                     $scope .= '/'.$sec;
                   5311:                 }
                   5312:             }
                   5313:         }
1.101     raeburn  5314:         my $plrole = &Apache::lonnet::plaintext($role,$crstype);
1.11      raeburn  5315:         my $start = $env{'form.'.$item.'_start'};
                   5316:         my $end = $env{'form.'.$item.'_end'};
1.17      raeburn  5317:         if ($choice eq 'drop') {
                   5318:             # drop students
                   5319:             $end = $now;
                   5320:             $type = 'manual';
                   5321:             $result =
1.52      raeburn  5322:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.17      raeburn  5323:         } elsif ($choice eq 'revoke') {
                   5324:             # revoke or delete user role
1.11      raeburn  5325:             $end = $now; 
                   5326:             if ($role eq 'st') {
                   5327:                 $result = 
1.174     raeburn  5328:                     &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.11      raeburn  5329:             } else {
                   5330:                 $result = 
1.52      raeburn  5331:                     &Apache::lonnet::revokerole($udom,$uname,$scope,$role,
                   5332:                                                 '','',$context);
1.11      raeburn  5333:             }
                   5334:         } elsif ($choice eq 'delete') {
                   5335:             if ($role eq 'st') {
1.174     raeburn  5336:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$now,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.29      raeburn  5337:             }
                   5338:             $result =
                   5339:                 &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
1.52      raeburn  5340:                                             $start,1,'',$context);
1.11      raeburn  5341:         } else {
                   5342:             #reenable, activate, change access dates or change section
                   5343:             if ($choice ne 'chgsec') {
                   5344:                 $start = $startdate; 
                   5345:                 $end = $enddate;
                   5346:             }
                   5347:             if ($choice eq 'reenable') {
                   5348:                 if ($role eq 'st') {
1.174     raeburn  5349:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.11      raeburn  5350:                 } else {
                   5351:                     $result = 
                   5352:                         &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  5353:                                                     $now,'','',$context);
1.11      raeburn  5354:                 }
                   5355:             } elsif ($choice eq 'activate') {
                   5356:                 if ($role eq 'st') {
1.174     raeburn  5357:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.11      raeburn  5358:                 } else {
                   5359:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  5360:                                             $now,'','',$context);
1.11      raeburn  5361:                 }
                   5362:             } elsif ($choice eq 'chgdates') {
                   5363:                 if ($role eq 'st') {
1.174     raeburn  5364:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.11      raeburn  5365:                 } else {
                   5366:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  5367:                                                 $start,'','',$context);
1.11      raeburn  5368:                 }
                   5369:             } elsif ($choice eq 'chgsec') {
                   5370:                 my (@newsecs,$revresult,$nochg,@retained);
1.103     raeburn  5371:                 if (($role ne 'cc') && ($role ne 'co')) {
1.117     raeburn  5372:                     my @secs = sort(split(/,/,$env{'form.newsecs'}));
                   5373:                     if (@secs) {
                   5374:                         my %curr_groups = &Apache::longroup::coursegroups();
                   5375:                         foreach my $sec (@secs) {
                   5376:                             next if (($sec =~ /\W/) || ($sec eq 'none') ||
                   5377:                             (exists($curr_groups{$sec})));
                   5378:                             push(@newsecs,$sec);
                   5379:                         }
                   5380:                     }
1.11      raeburn  5381:                 }
                   5382:                 # remove existing section if not to be retained.   
1.118     raeburn  5383:                 if (!$env{'form.retainsec'} || ($role eq 'st')) {
1.11      raeburn  5384:                     if ($sec eq '') {
                   5385:                         if (@newsecs == 0) {
1.118     raeburn  5386:                             $result = 'ok';
1.11      raeburn  5387:                             $nochg = 1;
1.118     raeburn  5388:                             $nothingtodo = 1;
1.40      raeburn  5389:                         } else {
                   5390:                             $revresult =
                   5391:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  5392:                                                             $scope,$role,
                   5393:                                                             '','',$context);
1.40      raeburn  5394:                         } 
1.11      raeburn  5395:                     } else {
1.28      raeburn  5396:                         if (@newsecs > 0) {
                   5397:                             if (grep(/^\Q$sec\E$/,@newsecs)) {
                   5398:                                 push(@retained,$sec);
                   5399:                             } else {
                   5400:                                 $revresult =
                   5401:                                     &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  5402:                                                                 $scope,$role,
                   5403:                                                                 '','',$context);
1.28      raeburn  5404:                             }
                   5405:                         } else {
1.11      raeburn  5406:                             $revresult =
1.28      raeburn  5407:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  5408:                                                             $scope,$role,
                   5409:                                                             '','',$context);
1.11      raeburn  5410:                         }
                   5411:                     }
                   5412:                 } else {
1.28      raeburn  5413:                     if ($sec eq '') {
                   5414:                         $nochg = 1;
1.118     raeburn  5415:                         $keepnosection = 1;
                   5416:                     } else {
1.28      raeburn  5417:                         push(@retained,$sec);
                   5418:                     }
1.11      raeburn  5419:                 }
                   5420:                 # add new sections
1.118     raeburn  5421:                 my (@diffs,@shownew);
                   5422:                 if (@retained) {
                   5423:                     @diffs = &Apache::loncommon::compare_arrays(\@retained,\@newsecs);
                   5424:                 } else {
                   5425:                     @diffs = @newsecs;
                   5426:                 }
1.11      raeburn  5427:                 if (@newsecs == 0) {
1.118     raeburn  5428:                     if ($nochg) {
                   5429:                         $result = 'ok';
                   5430:                         $nothingtodo = 1;
                   5431:                     } else {
1.28      raeburn  5432:                         if ($role eq 'st') {
                   5433:                             $result = 
1.174     raeburn  5434:                                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.28      raeburn  5435:                         } else {
                   5436:                             my $newscope = $scopestem;
1.52      raeburn  5437:                             $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start,'','',$context);
1.11      raeburn  5438:                         }
                   5439:                     }
1.118     raeburn  5440:                     $showsecs = &mt('No section');
                   5441:                 } elsif (@diffs == 0) {
                   5442:                     $result = 'ok';
                   5443:                     $nothingtodo = 1;
1.11      raeburn  5444:                 } else {
1.118     raeburn  5445:                     foreach my $newsec (@newsecs) {
1.11      raeburn  5446:                         if (!grep(/^\Q$newsec\E$/,@retained)) {
                   5447:                             if ($role eq 'st') {
1.174     raeburn  5448:                                 $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.118     raeburn  5449:                                 if (@newsecs > 1) {
                   5450:                                     my $showsingle; 
                   5451:                                     if ($newsec eq '') {
                   5452:                                         $showsingle = &mt('No section');
                   5453:                                     } else {
                   5454:                                         $showsingle = $newsec;
                   5455:                                     }
                   5456:                                     if ($crstype eq 'Community') {
                   5457:                                         $warn_singlesec = &mt('Although more than one section was indicated, a role was only added for the first section - [_1], as each community member may only be in one section at a time.','<i>'.$showsingle.'</i>');
                   5458:                                     } else { 
                   5459:                                         $warn_singlesec = &mt('Although more than one section was indicated, a role was only added for the first section - [_1], as each student may only be in one section of a course at a time.','<i>'.$showsingle.'</i>');
                   5460:                                     }
                   5461:                                     $showsecs = $showsingle; 
                   5462:                                     last;
                   5463:                                 } else {
                   5464:                                     if ($newsec eq '') {
                   5465:                                         $showsecs = &mt('No section');
                   5466:                                     } else {
                   5467:                                         $showsecs = $newsec;
                   5468:                                     }
                   5469:                                 }
1.11      raeburn  5470:                             } else {
                   5471:                                 my $newscope = $scopestem;
                   5472:                                 if ($newsec ne '') {
                   5473:                                    $newscope .= '/'.$newsec;
1.118     raeburn  5474:                                    push(@shownew,$newsec); 
1.11      raeburn  5475:                                 }
                   5476:                                 $result = &Apache::lonnet::assignrole($udom,$uname,
                   5477:                                                         $newscope,$role,$end,$start);
1.118     raeburn  5478:                                 
1.11      raeburn  5479:                             }
                   5480:                         }
                   5481:                     }
                   5482:                 }
1.118     raeburn  5483:                 unless ($role eq 'st') {
                   5484:                     unless ($showsecs) {
                   5485:                         my @tolist = sort(@shownew,@retained);
                   5486:                         if ($keepnosection) {
                   5487:                             push(@tolist,&mt('No section'));
                   5488:                         }
                   5489:                         $showsecs = join(', ',@tolist);
                   5490:                     }
                   5491:                 }
1.11      raeburn  5492:             }
                   5493:         }
1.17      raeburn  5494:         my $extent = $scope;
                   5495:         if ($choice eq 'drop' || $context eq 'course') {
                   5496:             my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
                   5497:             if ($cdesc) {
                   5498:                 $extent = $cdesc;
                   5499:             }
                   5500:         }
1.1       raeburn  5501:         if ($result eq 'ok' || $result eq 'ok:') {
1.118     raeburn  5502:             my $dates;
                   5503:             if (($choice eq 'chgsec') || ($choice eq 'chgdates')) {
                   5504:                 $dates = &dates_feedback($start,$end,$now);
                   5505:             }
                   5506:             if ($choice eq 'chgsec') {
                   5507:                 if ($nothingtodo) {
                   5508:                     $r->print(&mt("Section assignment for role of '[_1]' in [_2] for '[_3]' unchanged.",$plrole,$extent,'<i>'.
                   5509:                           &Apache::loncommon::plainname($uname,$udom).
                   5510:                           '</i>').' ');
                   5511:                     if ($sec eq '') {
                   5512:                         $r->print(&mt('[_1]No section[_2] - [_3]','<b>','</b>',$dates));
                   5513:                     } else {
                   5514:                         $r->print(&mt('Section(s): [_1] - [_2]',
                   5515:                                       '<b>'.$showsecs.'</b>',$dates));
                   5516:                     }
                   5517:                     $r->print('<br />');
                   5518:                 } else {
                   5519:                     $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' to [_4] - [_5]",$plrole,$extent,
                   5520:                         '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
                   5521:                         '<b>'.$showsecs.'</b>',$dates).'<br />');
                   5522:                    $count ++;
                   5523:                }
                   5524:                if ($warn_singlesec) {
                   5525:                    $r->print('<div class="LC_warning">'.$warn_singlesec.'</div>');
                   5526:                }
                   5527:             } elsif ($choice eq 'chgdates') {
                   5528:                 $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' - [_4]",$plrole,$extent, 
1.121     raeburn  5529:                       '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
1.118     raeburn  5530:                       $dates).'<br />');
                   5531:                $count ++;
                   5532:             } else {
                   5533:                 $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]'.",$plrole,$extent,
1.121     raeburn  5534:                       '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>').
1.118     raeburn  5535:                           '<br />');
                   5536:                 $count ++;
                   5537:             }
1.1       raeburn  5538:         } else {
                   5539:             $r->print(
1.118     raeburn  5540:                 &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for '[_3]': [_4].",
                   5541:                     $plrole,$extent,
1.121     raeburn  5542:                     '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
1.118     raeburn  5543:                     $result).'<br />');
1.11      raeburn  5544:         }
                   5545:     }
1.32      raeburn  5546:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n");
1.33      raeburn  5547:     if ($choice eq 'drop') {
                   5548:         $r->print('<input type="hidden" name="action" value="listusers" />'."\n".
                   5549:                   '<input type="hidden" name="Status" value="Active" />'."\n".
                   5550:                   '<input type="hidden" name="showrole" value="st" />'."\n");
                   5551:     } else {
                   5552:         foreach my $item ('action','sortby','roletype','showrole','Status','secfilter','grpfilter') {
                   5553:             if ($env{'form.'.$item} ne '') {
                   5554:                 $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.
                   5555:                           '" />'."\n");
                   5556:             }
1.32      raeburn  5557:         }
                   5558:     }
1.144     bisitz   5559:     $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} [quant,_1,user role,user roles,no user roles].",$count).'</b></p>');
1.11      raeburn  5560:     if ($count > 0) {
1.17      raeburn  5561:         if ($choice eq 'revoke' || $choice eq 'drop') {
1.74      bisitz   5562:             $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.').'</p>');
1.11      raeburn  5563:         }
                   5564:         # Flush the course logs so reverse user roles immediately updated
1.126     raeburn  5565:         $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.11      raeburn  5566:     }
                   5567:     if ($env{'form.makedatesdefault'}) {
                   5568:         if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
1.101     raeburn  5569:             $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  5570:         }
                   5571:     }
1.33      raeburn  5572:     my $linktext = &mt('Display User Lists');
                   5573:     if ($choice eq 'drop') {
                   5574:         $linktext = &mt('Display current class roster');
                   5575:     }
1.144     bisitz   5576:     $r->print(
                   5577:         &Apache::lonhtmlcommon::actionbox(
                   5578:             ['<a href="javascript:document.studentform.submit()">'.$linktext.'</a>'])
                   5579:        .'</form>'."\n");
1.1       raeburn  5580: }
                   5581: 
1.118     raeburn  5582: sub dates_feedback {
                   5583:     my ($start,$end,$now) = @_;
                   5584:     my $dates;
                   5585:     if ($start < $now) {
                   5586:         if ($end == 0) {
1.147     bisitz   5587:             $dates = &mt('role(s) active now; no end date');
1.118     raeburn  5588:         } elsif ($end > $now) {
                   5589:             $dates = &mt('role(s) active now; ends [_1].',&Apache::lonlocal::locallocaltime($end));
                   5590:         } else {
                   5591:             $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
                   5592:         }
                   5593:      } else {
                   5594:         if ($end == 0 || $end > $now) {
                   5595:             $dates = &mt('future role(s); starts: [_1].',&Apache::lonlocal::locallocaltime($start));
                   5596:         } else {
                   5597:             $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
                   5598:         }
                   5599:     }
                   5600:     return $dates;
                   5601: }
                   5602: 
1.8       raeburn  5603: sub classlist_drop {
1.29      raeburn  5604:     my ($scope,$uname,$udom,$now) = @_;
1.8       raeburn  5605:     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
1.29      raeburn  5606:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.8       raeburn  5607:         if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
1.63      raeburn  5608:             my %user;
                   5609:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,\%user,$now);
1.8       raeburn  5610:             return &mt('Drop from classlist: [_1]',
                   5611:                        '<b>'.$result.'</b>').'<br />';
                   5612:         }
                   5613:     }
                   5614: }
                   5615: 
                   5616: sub active_student_roles {
                   5617:     my ($cnum,$cdom,$uname,$udom) = @_;
                   5618:     my %roles =
                   5619:         &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   5620:                                       ['future','active'],['st']);
                   5621:     return exists($roles{"$cnum:$cdom:st"});
                   5622: }
                   5623: 
1.1       raeburn  5624: sub section_check_js {
1.8       raeburn  5625:     my $groupslist= &get_groupslist();
1.170     damieng  5626:     my %js_lt = &Apache::lonlocal::texthash(
                   5627:         mayn   => 'may not be used as the name for a section, as it is a reserved word.',
                   5628:         plch   => 'Please choose a different section name.',
                   5629:         mnot   => 'may not be used as a section name, as it is the name of a course group.',
                   5630:         secn   => 'Section names and group names must be distinct. Please choose a different section name.',
                   5631:     );
                   5632:     &js_escape(\%js_lt);
1.1       raeburn  5633:     return <<"END";
                   5634: function validate(caller) {
1.9       raeburn  5635:     var groups = new Array($groupslist);
1.1       raeburn  5636:     var secname = caller.value;
                   5637:     if ((secname == 'all') || (secname == 'none')) {
1.170     damieng  5638:         alert("'"+secname+"' $js_lt{'mayn'}\\n$js_lt{'plch'}");
1.1       raeburn  5639:         return 'error';
                   5640:     }
                   5641:     if (secname != '') {
                   5642:         for (var k=0; k<groups.length; k++) {
                   5643:             if (secname == groups[k]) {
1.170     damieng  5644:                 alert("'"+secname+"' $js_lt{'mnot'}\\n$js_lt{'secn'}");
1.1       raeburn  5645:                 return 'error';
                   5646:             }
                   5647:         }
                   5648:     }
                   5649:     return 'ok';
                   5650: }
                   5651: END
                   5652: }
                   5653: 
                   5654: sub set_login {
                   5655:     my ($dom,$authformkrb,$authformint,$authformloc) = @_;
                   5656:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   5657:     my $response;
                   5658:     my ($authnum,%can_assign) =
                   5659:         &Apache::loncommon::get_assignable_auth($dom);
                   5660:     if ($authnum) {
                   5661:         $response = &Apache::loncommon::start_data_table();
                   5662:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   5663:             $response .= &Apache::loncommon::start_data_table_row().
                   5664:                          '<td>'.$authformkrb.'</td>'.
                   5665:                          &Apache::loncommon::end_data_table_row()."\n";
                   5666:         }
                   5667:         if ($can_assign{'int'}) {
                   5668:             $response .= &Apache::loncommon::start_data_table_row().
                   5669:                          '<td>'.$authformint.'</td>'.
                   5670:                          &Apache::loncommon::end_data_table_row()."\n"
                   5671:         }
                   5672:         if ($can_assign{'loc'}) {
                   5673:             $response .= &Apache::loncommon::start_data_table_row().
                   5674:                          '<td>'.$authformloc.'</td>'.
                   5675:                          &Apache::loncommon::end_data_table_row()."\n";
                   5676:         }
                   5677:         $response .= &Apache::loncommon::end_data_table();
                   5678:     }
                   5679:     return $response;
                   5680: }
                   5681: 
1.8       raeburn  5682: sub course_sections {
1.178     raeburn  5683:     my ($sections_count,$role,$current_sec,$disabled) = @_;
1.8       raeburn  5684:     my $output = '';
1.169     raeburn  5685:     my @sections = (sort {$a <=> $b} keys(%{$sections_count}));
1.29      raeburn  5686:     my $numsec = scalar(@sections);
1.92      bisitz   5687:     my $is_selected = ' selected="selected"';
1.29      raeburn  5688:     if ($numsec <= 1) {
1.178     raeburn  5689:         $output = '<select name="currsec_'.$role.'"'.$disabled.'>'."\n".
1.51      raeburn  5690:                   '  <option value="">'.&mt('Select').'</option>'."\n";
                   5691:         if ($current_sec eq 'none') {
                   5692:             $output .=       
                   5693:                   '  <option value=""'.$is_selected.'>'.&mt('No section').'</option>'."\n";
                   5694:         } else {
                   5695:             $output .=
1.29      raeburn  5696:                   '  <option value="">'.&mt('No section').'</option>'."\n";
1.51      raeburn  5697:         }
1.29      raeburn  5698:         if ($numsec == 1) {
1.51      raeburn  5699:             if ($current_sec eq $sections[0]) {
                   5700:                 $output .=
                   5701:                   '  <option value="'.$sections[0].'"'.$is_selected.'>'.$sections[0].'</option>'."\n";
                   5702:             } else {
                   5703:                 $output .=  
1.8       raeburn  5704:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
1.51      raeburn  5705:             }
1.29      raeburn  5706:         }
1.8       raeburn  5707:     } else {
                   5708:         $output = '<select name="currsec_'.$role.'" ';
                   5709:         my $multiple = 4;
                   5710:         if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
1.29      raeburn  5711:         if ($role eq 'st') {
1.178     raeburn  5712:             $output .= $disabled.'>'."\n".
1.51      raeburn  5713:                        '  <option value="">'.&mt('Select').'</option>'."\n";
                   5714:             if ($current_sec eq 'none') {
                   5715:                 $output .= 
                   5716:                        '  <option value=""'.$is_selected.'>'.&mt('No section')."</option>\n";
                   5717:             } else {
                   5718:                 $output .=
1.29      raeburn  5719:                        '  <option value="">'.&mt('No section')."</option>\n";
1.51      raeburn  5720:             }
1.29      raeburn  5721:         } else {
1.178     raeburn  5722:             $output .= 'multiple="multiple" size="'.$multiple.'"'.$disabled.'>'."\n";
1.29      raeburn  5723:         }
1.8       raeburn  5724:         foreach my $sec (@sections) {
1.51      raeburn  5725:             if ($current_sec eq $sec) {
                   5726:                 $output .= '<option value="'.$sec.'"'.$is_selected.'>'.$sec."</option>\n";
                   5727:             } else {
                   5728:                 $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
                   5729:             }
1.8       raeburn  5730:         }
                   5731:     }
                   5732:     $output .= '</select>';
                   5733:     return $output;
                   5734: }
                   5735: 
                   5736: sub get_groupslist {
                   5737:     my $groupslist;
                   5738:     my %curr_groups = &Apache::longroup::coursegroups();
                   5739:     if (%curr_groups) {
                   5740:         $groupslist = join('","',sort(keys(%curr_groups)));
                   5741:         $groupslist = '"'.$groupslist.'"';
                   5742:     }
1.11      raeburn  5743:     return $groupslist; 
1.8       raeburn  5744: }
                   5745: 
                   5746: sub setsections_javascript {
1.150     raeburn  5747:     my ($formname,$groupslist,$mode,$checkauth,$crstype,$showcredits) = @_;
1.28      raeburn  5748:     my ($checkincluded,$finish,$rolecode,$setsection_js);
                   5749:     if ($mode eq 'upload') {
                   5750:         $checkincluded = 'formname.name == "'.$formname.'"';
                   5751:         $finish = "return 'ok';";
                   5752:         $rolecode = "var role = formname.defaultrole.options[formname.defaultrole.selectedIndex].value;\n";
                   5753:     } elsif ($formname eq 'cu') {
1.150     raeburn  5754:         if (($crstype eq 'Course') && ($showcredits)) {
                   5755:             $checkincluded = "((role == 'st') && (formname.elements[i-2].checked == true)) || ((role != 'st') && (formname.elements[i-1].checked == true))";
                   5756:         } else {
                   5757:             $checkincluded = 'formname.elements[i-1].checked == true';
                   5758:         }
1.37      raeburn  5759:         if ($checkauth) {
                   5760:             $finish = "var authcheck = auth_check();\n".
                   5761:                       "   if (authcheck == 'ok') {\n".
                   5762:                       "       formname.submit();\n".
                   5763:                       "   }\n";
                   5764:         } else {
                   5765:             $finish = 'formname.submit()';
                   5766:         }
1.28      raeburn  5767:         $rolecode = "var match = str.split('_');
                   5768:                 var role = match[3];\n";
1.165     raeburn  5769:     } elsif (($formname eq 'enrollstudent') || ($formname eq 'selfenroll')) {
1.28      raeburn  5770:         $checkincluded = 'formname.name == "'.$formname.'"';
1.37      raeburn  5771:         if ($checkauth) {
                   5772:             $finish = "var authcheck = auth_check();\n".
                   5773:                       "   if (authcheck == 'ok') {\n".
                   5774:                       "       formname.submit();\n".
                   5775:                       "   }\n";
                   5776:         } else {
                   5777:             $finish = 'formname.submit()';
                   5778:         }
1.28      raeburn  5779:         $rolecode = "var match = str.split('_');
                   5780:                 var role = match[1];\n";
1.8       raeburn  5781:     } else {
1.28      raeburn  5782:         $checkincluded = 'formname.name == "'.$formname.'"'; 
1.8       raeburn  5783:         $finish = "seccheck = 'ok';";
1.28      raeburn  5784:         $rolecode = "var match = str.split('_');
                   5785:                 var role = match[1];\n";
1.11      raeburn  5786:         $setsection_js = "var seccheck = 'alert';"; 
1.8       raeburn  5787:     }
                   5788:     my %alerts = &Apache::lonlocal::texthash(
                   5789:                     secd => 'Section designations do not apply to Course Coordinator roles.',
1.103     raeburn  5790:                     sedn => 'Section designations do not apply to Coordinator roles.',
1.8       raeburn  5791:                     accr => 'A course coordinator role will be added with access to all sections.',
1.103     raeburn  5792:                     acor => 'A coordinator role will be added with access to all sections',
1.8       raeburn  5793:                     inea => 'In each course, each user may only have one student role at a time.',
1.125     raeburn  5794:                     inco => 'In each community, each user may only have one member role at a time.',
1.145     raeburn  5795:                     youh => 'You had selected',
1.8       raeburn  5796:                     secs => 'sections.',
                   5797:                     plmo => 'Please modify your selections so they include no more than one section.',
                   5798:                     mayn => 'may not be used as the name for a section, as it is a reserved word.',
                   5799:                     plch => 'Please choose a different section name.',
                   5800:                     mnot => 'may not be used as a section name, as it is the name of a course group.',
                   5801:                     secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.113     raeburn  5802:                     nonw => 'Section names may only contain letters or numbers.',
1.170     damieng  5803:                  );
                   5804:     &js_escape(\%alerts);
1.8       raeburn  5805:     $setsection_js .= <<"ENDSECCODE";
                   5806: 
1.103     raeburn  5807: function setSections(formname,crstype) {
1.8       raeburn  5808:     var re1 = /^currsec_/;
1.113     raeburn  5809:     var re2 =/\\W/;
1.115     raeburn  5810:     var trimleading = /^\\s+/;
                   5811:     var trimtrailing = /\\s+\$/;
1.8       raeburn  5812:     var groups = new Array($groupslist);
                   5813:     for (var i=0;i<formname.elements.length;i++) {
                   5814:         var str = formname.elements[i].name;
1.168     raeburn  5815:         if (typeof(str) === "undefined") {
                   5816:             continue;
                   5817:         }
1.8       raeburn  5818:         var checkcurr = str.match(re1);
                   5819:         if (checkcurr != null) {
1.115     raeburn  5820:             var num = i;
1.150     raeburn  5821:             $rolecode
1.8       raeburn  5822:             if ($checkincluded) {
1.103     raeburn  5823:                 if (role == 'cc' || role == 'co') {
                   5824:                     if (role == 'cc') {
                   5825:                         alert("$alerts{'secd'}\\n$alerts{'accr'}");
                   5826:                     } else {
                   5827:                         alert("$alerts{'sedn'}\\n$alerts{'acor'}");
                   5828:                     }
                   5829:                 } else {
1.8       raeburn  5830:                     var sections = '';
                   5831:                     var numsec = 0;
1.115     raeburn  5832:                     var fromexisting = new Array();
                   5833:                     for (var j=0; j<formname.elements[num].length; j++) {
                   5834:                         if (formname.elements[num].options[j].selected == true ) {
                   5835:                             var addsec = formname.elements[num].options[j].value;
1.119     raeburn  5836:                             if ((addsec != "") && (addsec != null)) {
1.115     raeburn  5837:                                 fromexisting.push(addsec);
1.8       raeburn  5838:                                 if (numsec == 0) {
1.115     raeburn  5839:                                     sections = addsec;
                   5840:                                 } else {
                   5841:                                     sections = sections + "," +  addsec;
1.8       raeburn  5842:                                 }
1.115     raeburn  5843:                                 numsec ++;
1.8       raeburn  5844:                             }
                   5845:                         }
                   5846:                     }
1.115     raeburn  5847:                     var newsecs = formname.elements[num+1].value;
1.113     raeburn  5848:                     var validsecs = new Array();
1.115     raeburn  5849:                     var validsecstr = '';
1.113     raeburn  5850:                     var badsecs = new Array();
1.8       raeburn  5851:                     if (newsecs != null && newsecs != "") {
1.115     raeburn  5852:                         var numsplit;
                   5853:                         if (newsecs.indexOf(',') == -1) {
                   5854:                             numsplit = new Array(newsecs);
                   5855:                         } else {
                   5856:                             numsplit = newsecs.split(/,/g);
                   5857:                         }
1.117     raeburn  5858:                         for (var m=0; m<numsplit.length; m++) {
                   5859:                             var newsec = numsplit[m];
1.115     raeburn  5860:                             newsec = newsec.replace(trimleading,'');
                   5861:                             newsec = newsec.replace(trimtrailing,'');
                   5862:                             if (re2.test(newsec) == true) {
                   5863:                                 badsecs.push(newsec);
1.113     raeburn  5864:                             } else {
1.115     raeburn  5865:                                 if (newsec != '') {
                   5866:                                     var isnew = 1;
                   5867:                                     if (fromexisting != null) {
1.117     raeburn  5868:                                         for (var n=0; n<fromexisting.length; n++) {
                   5869:                                             if (newsec == fromexisting[n]) {
1.115     raeburn  5870:                                                 isnew = 0;
                   5871:                                             }
                   5872:                                         }
                   5873:                                     }
                   5874:                                     if (isnew == 1) {
                   5875:                                         validsecs.push(newsec);
                   5876:                                     }
                   5877:                                 }
1.113     raeburn  5878:                             }
                   5879:                         }
                   5880:                         if (badsecs.length > 0) {
                   5881:                             alert("$alerts{'nonw'}\\n$alerts{'plch'}");
                   5882:                             return;
                   5883:                         }
                   5884:                         numsec = numsec + validsecs.length;
1.8       raeburn  5885:                     }
                   5886:                     if ((role == 'st') && (numsec > 1)) {
1.103     raeburn  5887:                         if (crstype == 'Community') {
                   5888:                             alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5889:                         } else {
                   5890:                             alert("$alerts{'inco'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5891:                         }
1.8       raeburn  5892:                         return;
1.115     raeburn  5893:                     } else {
                   5894:                         if (validsecs != null) {
                   5895:                             for (var j=0; j<validsecs.length; j++) {
                   5896:                                 if (validsecstr == '' || validsecstr == null) {
                   5897:                                     validsecstr = validsecs[j];
                   5898:                                 } else {
                   5899:                                     validsecstr += ','+validsecs[j];
                   5900:                                 }
                   5901:                                 if ((validsecs[j] == 'all') ||
                   5902:                                     (validsecs[j] == 'none')) {
                   5903:                                     alert("'"+validsecs[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
1.8       raeburn  5904:                                     return;
                   5905:                                 }
                   5906:                                 for (var k=0; k<groups.length; k++) {
1.115     raeburn  5907:                                     if (validsecs[j] == groups[k]) {
                   5908:                                         alert("'"+validsecs[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
1.8       raeburn  5909:                                         return;
                   5910:                                     }
                   5911:                                 }
                   5912:                             }
                   5913:                         }
                   5914:                     }
1.115     raeburn  5915:                     if ((validsecstr != '') && (validsecstr != null)) {
1.117     raeburn  5916:                         if ((sections == '') || (sections == null)) {
                   5917:                             sections = validsecstr;
                   5918:                         } else {
1.115     raeburn  5919:                             sections = sections + "," + validsecstr;
                   5920:                         }
                   5921:                     }
                   5922:                     formname.elements[num+2].value = sections;
1.8       raeburn  5923:                 }
                   5924:             }
                   5925:         }
                   5926:     }
                   5927:     $finish
                   5928: }
                   5929: ENDSECCODE
1.11      raeburn  5930:     return $setsection_js; 
1.8       raeburn  5931: }
                   5932: 
1.15      raeburn  5933: sub can_create_user {
                   5934:     my ($dom,$context,$usertype) = @_;
                   5935:     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   5936:     my $cancreate = 1;
1.28      raeburn  5937:     if (&Apache::lonnet::allowed('mau',$dom)) {
                   5938:         return $cancreate;
1.178     raeburn  5939:     } elsif ($context eq 'domain') {
                   5940:         $cancreate = 0;
                   5941:         return $cancreate;
1.28      raeburn  5942:     }
1.15      raeburn  5943:     if (ref($domconf{'usercreation'}) eq 'HASH') {
                   5944:         if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
1.100     raeburn  5945:             if ($context eq 'course' || $context eq 'author' || $context eq 'requestcrs') {
1.15      raeburn  5946:                 my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
                   5947:                 if ($creation eq 'none') {
                   5948:                     $cancreate = 0;
                   5949:                 } elsif ($creation ne 'any') {
                   5950:                     if (defined($usertype)) {
                   5951:                         if ($creation ne $usertype) {
                   5952:                             $cancreate = 0;
                   5953:                         }
                   5954:                     }
                   5955:                 }
                   5956:             }
                   5957:         }
                   5958:     }
                   5959:     return $cancreate;
                   5960: }
                   5961: 
1.20      raeburn  5962: sub can_modify_userinfo {
                   5963:     my ($context,$dom,$fields,$userroles) = @_;
                   5964:     my %domconfig =
                   5965:        &Apache::lonnet::get_dom('configuration',['usermodification'],
                   5966:                                 $dom);
                   5967:     my %canmodify;
                   5968:     if (ref($fields) eq 'ARRAY') {
                   5969:         foreach my $field (@{$fields}) {
                   5970:             $canmodify{$field}  = 0;
                   5971:             if (&Apache::lonnet::allowed('mau',$dom)) {
                   5972:                 $canmodify{$field} = 1;
                   5973:             } else {
                   5974:                 if (ref($domconfig{'usermodification'}) eq 'HASH') {
                   5975:                     if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
                   5976:                         if (ref($userroles) eq 'ARRAY') {
                   5977:                             foreach my $role (@{$userroles}) {
                   5978:                                 my $testrole;
1.60      raeburn  5979:                                 if ($context eq 'selfcreate') {
                   5980:                                     $testrole = $role;
1.20      raeburn  5981:                                 } else {
1.60      raeburn  5982:                                     if ($role =~ /^cr\//) {
                   5983:                                         $testrole = 'cr';
                   5984:                                     } else {
                   5985:                                         $testrole = $role;
                   5986:                                     }
1.20      raeburn  5987:                                 }
                   5988:                                 if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
                   5989:                                     if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
                   5990:                                         $canmodify{$field} = 1;
                   5991:                                         last;
                   5992:                                     }
                   5993:                                 }
                   5994:                             }
                   5995:                         } else {
                   5996:                             foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
                   5997:                                 if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
                   5998:                                     if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
                   5999:                                         $canmodify{$field} = 1;
                   6000:                                         last;
                   6001:                                     }
                   6002:                                 }
                   6003:                             }
                   6004:                         }
                   6005:                     }
                   6006:                 } elsif ($context eq 'course') {
                   6007:                     if (ref($userroles) eq 'ARRAY') {
                   6008:                         if (grep(/^st$/,@{$userroles})) {
                   6009:                             $canmodify{$field} = 1;
                   6010:                         }
                   6011:                     } else {
                   6012:                         $canmodify{$field} = 1;
                   6013:                     }
                   6014:                 }
                   6015:             }
                   6016:         }
                   6017:     }
                   6018:     return %canmodify;
                   6019: }
                   6020: 
1.184.4.6  raeburn  6021: sub can_change_internalpass {
                   6022:     my ($uname,$udom,$crstype,$permission) = @_;
                   6023:     my $canchange;
                   6024:     if (&Apache::lonnet::allowed('mau',$udom)) {
                   6025:         $canchange = 1;
                   6026:     } elsif ((ref($permission) eq 'HASH') && ($permission->{'mip'}) &&
                   6027:              ($udom eq $env{'request.role.domain'})) {
                   6028:         unless ($env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'}) {
                   6029:             my ($cnum,$cdom) = &get_course_identity();
                   6030:             if ((&Apache::lonnet::is_course_owner($cdom,$cnum)) && ($udom eq $env{'user.domain'})) {
                   6031:                 my @userstatuses = ('default');
                   6032:                 my %userenv = &Apache::lonnet::userenvironment($udom,$uname,'inststatus');
                   6033:                 if ($userenv{'inststatus'} ne '') {
                   6034:                     @userstatuses =  split(/:/,$userenv{'inststatus'});
                   6035:                 }
                   6036:                 my $noupdate = 1;
                   6037:                 my %passwdconf = &Apache::lonnet::get_passwdconf($cdom);
                   6038:                 if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
                   6039:                     if (ref($passwdconf{'crsownerchg'}{'for'}) eq 'ARRAY') {
                   6040:                         foreach my $status (@userstatuses) {
                   6041:                             if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'for'}})) {
                   6042:                                 undef($noupdate);
                   6043:                                 last;
                   6044:                             }
                   6045:                         }
                   6046:                     }
                   6047:                 }
                   6048:                 if ($noupdate) {
                   6049:                     return;
                   6050:                 }
                   6051:                 my %owned = &Apache::lonnet::courseiddump($cdom,'.',1,'.',
                   6052:                                                           $env{'user.name'}.':'.$env{'user.domain'},
                   6053:                                                           undef,undef,undef,'.');
                   6054:                 my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   6055:                                                               ['active','future']);
                   6056:                 foreach my $key (keys(%roleshash)) {
                   6057:                     my ($name,$domain,$role) = split(/:/,$key);
                   6058:                     if ($role eq 'st') {
                   6059:                         next if (($name eq $cnum) && ($domain eq $cdom));
                   6060:                         if ($owned{$domain.'_'.$name}) {
                   6061:                             if (ref($owned{$domain.'_'.$name}) eq 'HASH') {
                   6062:                                 if ($owned{$domain.'_'.$name}{'nopasswdchg'}) {
                   6063:                                     $noupdate = 1;
                   6064:                                     last;
                   6065:                                 }
                   6066:                             }
                   6067:                         } else {
                   6068:                             $noupdate = 1;
                   6069:                             last;
                   6070:                         }
                   6071:                     } else {
                   6072:                         $noupdate = 1;
                   6073:                         last;
                   6074:                     }
                   6075:                 }
                   6076:                 unless ($noupdate) {
                   6077:                     $canchange = 1;
                   6078:                 }
                   6079:             }
                   6080:         }
                   6081:     }
                   6082:     return $canchange;
                   6083: }
                   6084: 
1.18      raeburn  6085: sub check_usertype {
1.134     raeburn  6086:     my ($dom,$uname,$rules,$curr_rules,$got_rules) = @_;
1.18      raeburn  6087:     my $usertype;
1.134     raeburn  6088:     if ((ref($got_rules) eq 'HASH') && (ref($curr_rules) eq 'HASH')) {
                   6089:         if (!$got_rules->{$dom}) {
                   6090:             my %domconfig = &Apache::lonnet::get_dom('configuration',
                   6091:                                               ['usercreation'],$dom);
                   6092:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   6093:                 foreach my $item ('username','id') {
                   6094:                     if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
                   6095:                         $curr_rules->{$dom}{$item} =
                   6096:                                 $domconfig{'usercreation'}{$item.'_rule'};
                   6097:                     }
                   6098:                 }
                   6099:             }
                   6100:             $got_rules->{$dom} = 1;
                   6101:         }
                   6102:         if (ref($rules) eq 'HASH') {
                   6103:             my @user_rules;
                   6104:             if (ref($curr_rules->{$dom}{'username'}) eq 'ARRAY') {
                   6105:                 foreach my $rule (keys(%{$rules})) {
                   6106:                     if (grep(/^\Q$rule\E/,@{$curr_rules->{$dom}{'username'}})) {
                   6107:                         push(@user_rules,$rule);
                   6108:                     }
                   6109:                 } 
                   6110:             }
                   6111:             if (@user_rules > 0) {
                   6112:                 my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
                   6113:                 if (keys(%rule_check) > 0) {
                   6114:                     $usertype = 'unofficial';
                   6115:                     foreach my $item (keys(%rule_check)) {
                   6116:                         if ($rule_check{$item}) {
                   6117:                             $usertype = 'official';
                   6118:                             last;
                   6119:                         }
1.18      raeburn  6120:                     }
                   6121:                 }
                   6122:             }
                   6123:         }
                   6124:     }
                   6125:     return $usertype;
                   6126: }
                   6127: 
1.17      raeburn  6128: sub roles_by_context {
1.101     raeburn  6129:     my ($context,$custom,$crstype) = @_;
1.17      raeburn  6130:     my @allroles;
                   6131:     if ($context eq 'course') {
1.99      raeburn  6132:         @allroles = ('st');
                   6133:         if ($env{'request.role'} =~ m{^dc\./}) {
                   6134:             push(@allroles,'ad');
                   6135:         }
1.101     raeburn  6136:         push(@allroles,('ta','ep','in'));
                   6137:         if ($crstype eq 'Community') {
                   6138:             push(@allroles,'co');
                   6139:         } else {
                   6140:             push(@allroles,'cc');
                   6141:         }
1.17      raeburn  6142:         if ($custom) {
                   6143:             push(@allroles,'cr');
                   6144:         }
                   6145:     } elsif ($context eq 'author') {
                   6146:         @allroles = ('ca','aa');
                   6147:     } elsif ($context eq 'domain') {
1.182     raeburn  6148:         @allroles = ('li','ad','dg','dh','da','sc','au','dc');
1.17      raeburn  6149:     }
                   6150:     return @allroles;
                   6151: }
                   6152: 
1.16      raeburn  6153: sub get_permission {
1.101     raeburn  6154:     my ($context,$crstype) = @_;
1.16      raeburn  6155:     my %permission;
                   6156:     if ($context eq 'course') {
1.17      raeburn  6157:         my $custom = 1;
1.101     raeburn  6158:         my @allroles = &roles_by_context($context,$custom,$crstype);
1.17      raeburn  6159:         foreach my $role (@allroles) {
                   6160:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   6161:                 $permission{'cusr'} = 1;
                   6162:                 last;
                   6163:             }
1.16      raeburn  6164:         }
                   6165:         if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
                   6166:             $permission{'custom'} = 1;
                   6167:         }
                   6168:         if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
                   6169:             $permission{'view'} = 1;
                   6170:         }
                   6171:         if (!$permission{'view'}) {
                   6172:             my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   6173:             $permission{'view'} =  &Apache::lonnet::allowed('vcl',$scope);
                   6174:             if ($permission{'view'}) {
                   6175:                 $permission{'view_section'} = $env{'request.course.sec'};
                   6176:             }
                   6177:         }
1.17      raeburn  6178:         if (!$permission{'cusr'}) {
                   6179:             if ($env{'request.course.sec'} ne '') {
                   6180:                 my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   6181:                 $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
                   6182:                 if ($permission{'cusr'}) {
                   6183:                     $permission{'cusr_section'} = $env{'request.course.sec'};
                   6184:                 }
                   6185:             }
                   6186:         }
1.16      raeburn  6187:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
                   6188:             $permission{'grp_manage'} = 1;
                   6189:         }
1.165     raeburn  6190:         if ($permission{'cusr'}) {
                   6191:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6192:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   6193:             my %coursehash = (
                   6194:                 'internal.selfenrollmgrdc' => $env{'course.'.$env{'request.course.id'}.'.internal.selfenrollmgrdc'},
                   6195:                 'internal.selfenrollmgrcc' => $env{'course.'.$env{'request.course.id'}.'.internal.selfenrollmgrcc'},
                   6196:                 'internal.coursecode'      => $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'},
                   6197:                 'internal.textbook'        =>$env{'course.'.$env{'request.course.id'}.'.internal.textbook'},
                   6198:             );
                   6199:             my ($managed_by_cc,$managed_by_dc) = &selfenrollment_administration($cdom,$cnum,$crstype,\%coursehash);
                   6200:             if (ref($managed_by_cc) eq 'ARRAY') {
                   6201:                 if (@{$managed_by_cc}) {
                   6202:                     $permission{'selfenrolladmin'} = 1;
                   6203:                 }
                   6204:             }
1.184.4.10.2.  (raeburn 6205:):             unless ($permission{'selfenrolladmin'}) {
                   6206:):                 $permission{'selfenrollview'} = 1;
                   6207:):             }
1.165     raeburn  6208:         }
1.180     raeburn  6209:         if ($env{'request.course.id'}) {
1.184.4.6  raeburn  6210:             my $user;
                   6211:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   6212:                 $user = $env{'user.name'}.':'.$env{'user.domain'};
                   6213:             }
1.180     raeburn  6214:             if (($user ne '') && ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq
                   6215:                                   $user)) {
                   6216:                 $permission{'owner'} = 1;
1.184.4.6  raeburn  6217:                 if (&Apache::lonnet::allowed('mip',$env{'request.course.id'})) {
                   6218:                     $permission{'mip'} = 1;
                   6219:                 }
1.180     raeburn  6220:             } elsif (($user ne '') && ($env{'course.'.$env{'request.course.id'}.'.internal.co-owners'} ne '')) {
                   6221:                 if (grep(/^\Q$user\E$/,split(/,/,$env{'course.'.$env{'request.course.id'}.'.internal.co-owners'}))) {
                   6222:                     $permission{'co-owner'} = 1;
                   6223:                 }
                   6224:             }
                   6225:         }
1.16      raeburn  6226:     } elsif ($context eq 'author') {
1.184.4.10.2.  (raeburn 6227:):         my $audom = $env{'request.role.domain'};
                   6228:):         my $auname = $env{'user.name'};
                   6229:):         if ((&Apache::lonnet::allowed('cca',"$audom/$auname")) ||
                   6230:):             (&Apache::lonnet::allowed('caa',"$audom/$auname"))) {
                   6231:):             $permission{'author'} = 1;
                   6232:):             $permission{'cusr'} = 1;
                   6233:):             $permission{'view'} = 1;
                   6234:):         }
                   6235:):     } elsif ($context eq 'coauthor') {
                   6236:):         my ($audom,$auname) = ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)$});
                   6237:):         if ((&Apache::lonnet::allowed('vca',"$audom/$auname")) ||
                   6238:):             (&Apache::lonnet::allowed('vaa',"$audom/$auname"))) {
                   6239:):             if ($env{"environment.internal.manager./$audom/$auname"}) {
                   6240:):                 $permission{'cusr'} = 1;
                   6241:):                 $permission{'view'} = 1;
                   6242:):             }
                   6243:):         }
1.16      raeburn  6244:     } else {
1.17      raeburn  6245:         my @allroles = &roles_by_context($context);
                   6246:         foreach my $role (@allroles) {
1.28      raeburn  6247:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   6248:                 $permission{'cusr'} = 1;
1.17      raeburn  6249:                 last;
                   6250:             }
                   6251:         }
                   6252:         if (!$permission{'cusr'}) {
                   6253:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   6254:                 $permission{'cusr'} = 1;
                   6255:             }
1.16      raeburn  6256:         }
                   6257:         if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
                   6258:             $permission{'custom'} = 1;
                   6259:         }
1.176     raeburn  6260:         if (&Apache::lonnet::allowed('vac',$env{'request.role.domain'})) {
                   6261:             $permission{'activity'} = 1;
                   6262:         }
1.178     raeburn  6263:         if (&Apache::lonnet::allowed('vur',$env{'request.role.domain'})) {
                   6264:             $permission{'view'} = 1;
                   6265:         }
1.180     raeburn  6266:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   6267:             $permission{'owner'} = 1;
                   6268:         }
1.16      raeburn  6269:     }
                   6270:     my $allowed = 0;
1.184.4.10.2.  (raeburn 6271:):     foreach my $key (keys(%permission)) {
                   6272:):         next if (($key eq 'owner') || ($key eq 'co-owner') || ($key eq 'author'));
                   6273:):         if ($permission{$key}) { $allowed=1; last; }
1.16      raeburn  6274:     }
                   6275:     return (\%permission,$allowed);
                   6276: }
                   6277: 
                   6278: # ==================================================== Figure out author access
                   6279: 
                   6280: sub authorpriv {
                   6281:     my ($auname,$audom)=@_;
                   6282:     unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
                   6283:          || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }    return 1;
                   6284: }
                   6285: 
1.184.4.10.2.  (raeburn 6286:): sub coauthorpriv {
                   6287:):     my ($auname,$audom)=@_;
                   6288:):     my $uname = $env{'user.name'};
                   6289:):     my $udom = $env{'user.domain'};
                   6290:):     if (((&Apache::lonnet::allowed('vca',"$udom/$uname")) ||
                   6291:):          (&Apache::lonnet::allowed('vaa',"$udom/$uname"))) &&
                   6292:):          ($env{"environment.internal.manager./$audom/$auname"})) {
                   6293:):         return 1;
                   6294:):     }
                   6295:):     return '';
                   6296:): }
                   6297:): 
1.27      raeburn  6298: sub roles_on_upload {
1.101     raeburn  6299:     my ($context,$setting,$crstype,%customroles) = @_;
1.27      raeburn  6300:     my (@possible_roles,@permitted_roles);
1.101     raeburn  6301:     @possible_roles = &curr_role_permissions($context,$setting,1,$crstype);
1.27      raeburn  6302:     foreach my $role (@possible_roles) {
                   6303:         if ($role eq 'cr') {
                   6304:             push(@permitted_roles,keys(%customroles));
                   6305:         } else {
                   6306:             push(@permitted_roles,$role);
                   6307:         }
                   6308:     }
1.42      raeburn  6309:     return @permitted_roles;
1.27      raeburn  6310: }
                   6311: 
1.17      raeburn  6312: sub get_course_identity {
                   6313:     my ($cid) = @_;
                   6314:     my ($cnum,$cdom,$cdesc);
                   6315:     if ($cid eq '') {
                   6316:         $cid = $env{'request.course.id'}
                   6317:     }
                   6318:     if ($cid ne '') {
                   6319:         $cnum = $env{'course.'.$cid.'.num'};
                   6320:         $cdom = $env{'course.'.$cid.'.domain'};
                   6321:         $cdesc = $env{'course.'.$cid.'.description'};
                   6322:         if ($cnum eq '' || $cdom eq '') {
                   6323:             my %coursehash =
                   6324:                 &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
                   6325:             $cdom = $coursehash{'domain'};
                   6326:             $cnum = $coursehash{'num'};
                   6327:             $cdesc = $coursehash{'description'};
                   6328:         }
                   6329:     }
                   6330:     return ($cnum,$cdom,$cdesc);
                   6331: }
                   6332: 
1.19      raeburn  6333: sub dc_setcourse_js {
1.184.4.4  raeburn  6334:     my ($formname,$mode,$context,$showcredits,$domain) = @_;
1.37      raeburn  6335:     my ($dc_setcourse_code,$authen_check);
1.19      raeburn  6336:     my $cctext = &Apache::lonnet::plaintext('cc');
1.103     raeburn  6337:     my $cotext = &Apache::lonnet::plaintext('co');
1.19      raeburn  6338:     my %alerts = &sectioncheck_alerts();
                   6339:     my $role = 'role';
                   6340:     if ($mode eq 'upload') {
                   6341:         $role = 'courserole';
1.37      raeburn  6342:     } else {
1.184.4.4  raeburn  6343:         $authen_check = &verify_authen($formname,$context,$domain);
1.19      raeburn  6344:     }
                   6345:     $dc_setcourse_code = (<<"SCRIPTTOP");
1.37      raeburn  6346: $authen_check
                   6347: 
1.19      raeburn  6348: function setCourse() {
                   6349:     var course = document.$formname.dccourse.value;
                   6350:     if (course != "") {
                   6351:         if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
                   6352:             alert("$alerts{'curd'}");
                   6353:             return;
                   6354:         }
                   6355:         var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
                   6356:         var section="";
                   6357:         var numsections = 0;
                   6358:         var newsecs = new Array();
                   6359:         for (var i=0; i<document.$formname.currsec.length; i++) {
                   6360:             if (document.$formname.currsec.options[i].selected == true ) {
                   6361:                 if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
                   6362:                     if (numsections == 0) {
                   6363:                         section = document.$formname.currsec.options[i].value
                   6364:                         numsections = 1;
                   6365:                     }
                   6366:                     else {
                   6367:                         section = section + "," +  document.$formname.currsec.options[i].value
                   6368:                         numsections ++;
                   6369:                     }
                   6370:                 }
                   6371:             }
                   6372:         }
                   6373:         if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
                   6374:             if (numsections == 0) {
                   6375:                 section = document.$formname.newsec.value
                   6376:             }
                   6377:             else {
                   6378:                 section = section + "," +  document.$formname.newsec.value
                   6379:             }
                   6380:             newsecs = document.$formname.newsec.value.split(/,/g);
                   6381:             numsections = numsections + newsecs.length;
                   6382:         }
                   6383:         if ((userrole == 'st') && (numsections > 1)) {
1.103     raeburn  6384:             if (document.$formname.crstype.value == 'Community') {
                   6385:                 alert("$alerts{'inco'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   6386:             } else {
                   6387:                 alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   6388:             }
1.19      raeburn  6389:             return;
                   6390:         }
                   6391:         for (var j=0; j<newsecs.length; j++) {
                   6392:             if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
                   6393:                 alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
                   6394:                 return;
                   6395:             }
                   6396:             if (document.$formname.groups.value != '') {
                   6397:                 var groups = document.$formname.groups.value.split(/,/g);
                   6398:                 for (var k=0; k<groups.length; k++) {
                   6399:                     if (newsecs[j] == groups[k]) {
1.103     raeburn  6400:                         if (document.$formname.crstype.value == 'Community') {
                   6401:                             alert("'"+newsecs[j]+"' $alerts{'mayc'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   6402:                         } else {
                   6403:                             alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   6404:                         }
1.19      raeburn  6405:                         return;
                   6406:                     }
                   6407:                 }
                   6408:             }
                   6409:         }
                   6410:         if ((userrole == 'cc') && (numsections > 0)) {
                   6411:             alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
                   6412:             section = "";
                   6413:         }
1.103     raeburn  6414:         if ((userrole == 'co') && (numsections > 0)) {
                   6415:             alert("$alerts{'secd'} $cotext $alerts{'role'}.\\n$alerts{'accr'}.");
                   6416:             section = "";
                   6417:         }
1.19      raeburn  6418: SCRIPTTOP
                   6419:     if ($mode ne 'upload') {
1.150     raeburn  6420:         $dc_setcourse_code .= (<<"SCRIPTMID");
1.19      raeburn  6421:         var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
                   6422:         var numcourse = getIndex(document.$formname.dccourse);
                   6423:         if (numcourse == "-1") {
1.103     raeburn  6424:             if (document.$formname.type == 'Community') {
                   6425:                 alert("$alerts{'thwc'}");
                   6426:             } else {
                   6427:                 alert("$alerts{'thwa'}");
                   6428:             }
1.19      raeburn  6429:             return;
                   6430:         }
                   6431:         else {
                   6432:             document.$formname.elements[numcourse].name = "act"+coursename;
                   6433:             var numnewsec = getIndex(document.$formname.newsec);
                   6434:             if (numnewsec != "-1") {
                   6435:                 document.$formname.elements[numnewsec].name = "sec"+coursename;
                   6436:                 document.$formname.elements[numnewsec].value = section;
                   6437:             }
                   6438:             var numstart = getIndex(document.$formname.start);
                   6439:             if (numstart != "-1") {
                   6440:                 document.$formname.elements[numstart].name = "start"+coursename;
                   6441:             }
                   6442:             var numend = getIndex(document.$formname.end);
                   6443:             if (numend != "-1") {
                   6444:                 document.$formname.elements[numend].name = "end"+coursename
                   6445:             }
1.150     raeburn  6446: SCRIPTMID
                   6447:         if ($showcredits) {
                   6448:             $dc_setcourse_code .= <<ENDCRED;
                   6449:             var numcredits = getIndex(document.$formname.credits);
                   6450:             if (numcredits != "-1") {
                   6451:                 document.$formname.elements[numcredits].name = "credits"+coursename;
                   6452:             }
                   6453: ENDCRED
                   6454:         }
                   6455:         $dc_setcourse_code .= <<ENDSCRIPT; 
1.19      raeburn  6456:         }
                   6457:     }
1.37      raeburn  6458:     var authcheck = auth_check();
                   6459:     if (authcheck == 'ok') {
                   6460:         document.$formname.submit();
                   6461:     }
1.19      raeburn  6462: }
                   6463: ENDSCRIPT
                   6464:     } else {
                   6465:         $dc_setcourse_code .=  "
                   6466:         document.$formname.sections.value = section;
                   6467:     }
                   6468:     return 'ok';
                   6469: }
                   6470: ";
                   6471:     }
                   6472:     $dc_setcourse_code .= (<<"ENDSCRIPT");
                   6473: 
                   6474:     function getIndex(caller) {
                   6475:         for (var i=0;i<document.$formname.elements.length;i++) {
                   6476:             if (document.$formname.elements[i] == caller) {
                   6477:                 return i;
                   6478:             }
                   6479:         }
                   6480:         return -1;
                   6481:     }
                   6482: ENDSCRIPT
1.37      raeburn  6483:     return $dc_setcourse_code;
                   6484: }
                   6485: 
                   6486: sub verify_authen {
1.184.4.4  raeburn  6487:     my ($formname,$context,$domain) = @_;
1.37      raeburn  6488:     my %alerts = &authcheck_alerts();
                   6489:     my $finish = "return 'ok';";
                   6490:     if ($context eq 'author') {
                   6491:         $finish = "document.$formname.submit();";
                   6492:     }
1.184.4.4  raeburn  6493:     my ($numrules,$intargjs) =
1.184.4.10.2.  (raeburn 6494:):         &Apache::loncommon::passwd_validation_js('argpicked',$domain);
1.37      raeburn  6495:     my $outcome = <<"ENDSCRIPT";
                   6496: 
                   6497: function auth_check() {
                   6498:     var logintype;
                   6499:     if (document.$formname.login.length) {
                   6500:         if (document.$formname.login.length > 0) {
                   6501:             var loginpicked = 0;
                   6502:             for (var i=0; i<document.$formname.login.length; i++) {
                   6503:                 if (document.$formname.login[i].checked == true) {
                   6504:                     loginpicked = 1;
                   6505:                     logintype = document.$formname.login[i].value;
                   6506:                 }
                   6507:             }
                   6508:             if (loginpicked == 0) {
                   6509:                 alert("$alerts{'authen'}");
                   6510:                 return;
                   6511:             }
                   6512:         }
                   6513:     } else {
                   6514:         logintype = document.$formname.login.value;
                   6515:     }
                   6516:     if (logintype == 'nochange') {
                   6517:         return 'ok';
                   6518:     }
                   6519:     var argpicked = document.$formname.elements[logintype+'arg'].value;
                   6520:     if ((argpicked == null) || (argpicked == '') || (typeof argpicked == 'undefined')) {
                   6521:         var alertmsg = '';
                   6522:         switch (logintype) {
                   6523:             case 'krb':
                   6524:                 alertmsg = '$alerts{'krb'}';
                   6525:                 break;
                   6526:             case 'int':
                   6527:                 alertmsg = '$alerts{'ipass'}';
1.184.4.4  raeburn  6528:                 break;
1.37      raeburn  6529:             case 'fsys':
                   6530:                 alertmsg = '$alerts{'ipass'}';
                   6531:                 break;
                   6532:             case 'loc':
                   6533:                 alertmsg = '';
                   6534:                 break;
                   6535:             default:
                   6536:                 alertmsg = '';
                   6537:         }
                   6538:         if (alertmsg != '') {
                   6539:             alert(alertmsg);
                   6540:             return;
                   6541:         }
1.184.4.4  raeburn  6542:     } else if (logintype == 'int') {
                   6543:         var numrules = $numrules;
                   6544:         if (numrules > 0) {
                   6545: $intargjs
                   6546:         }
1.37      raeburn  6547:     }
                   6548:     $finish
                   6549: }
                   6550: ENDSCRIPT
1.19      raeburn  6551: }
                   6552: 
                   6553: sub sectioncheck_alerts {
                   6554:     my %alerts = &Apache::lonlocal::texthash(
1.103     raeburn  6555:                     curd => 'You must select a course or community in the current domain',
1.19      raeburn  6556:                     inea => 'In each course, each user may only have one student role at a time',
1.103     raeburn  6557:                     inco => 'In each community, each user may only have one member role at a time', 
1.19      raeburn  6558:                     youh => 'You had selected',
                   6559:                     sect => 'sections',
                   6560:                     plsm => 'Please modify your selections so they include no more than one section',
                   6561:                     mayn => 'may not be used as the name for a section, as it is a reserved word',
                   6562:                     plsc => 'Please choose a different section name',
                   6563:                     mayt => 'may not be used as the name for a section, as it is the name of a course group',
1.103     raeburn  6564:                     mayc => 'may not be used as the name for a section, as it is the name of a community group',
1.19      raeburn  6565:                     secn => 'Section names and group names must be distinct',
                   6566:                     secd => 'Section designations do not apply to ',
                   6567:                     role => 'roles',
                   6568:                     accr => 'role will be added with access to all sections',
1.103     raeburn  6569:                     thwa => 'There was a problem with your course selection',
                   6570:                     thwc => 'There was a problem with your community selection',
1.19      raeburn  6571:                  );
1.170     damieng  6572:     &js_escape(\%alerts);
1.19      raeburn  6573:     return %alerts;
                   6574: }
1.17      raeburn  6575: 
1.37      raeburn  6576: sub authcheck_alerts {
                   6577:     my %alerts = 
                   6578:         &Apache::lonlocal::texthash(
                   6579:                     authen => 'You must choose an authentication type.',
                   6580:                     krb    => 'You need to specify the Kerberos domain.',
                   6581:                     ipass  => 'You need to specify the initial password.',
                   6582:         );
1.170     damieng  6583:     &js_escape(\%alerts);
1.37      raeburn  6584:     return %alerts;
                   6585: }
                   6586: 
1.141     raeburn  6587: sub is_courseowner {
                   6588:     my ($thiscourse,$courseowner) = @_;
                   6589:     if ($courseowner eq '') {
                   6590:         if ($env{'request.course.id'} eq $thiscourse) {
                   6591:             $courseowner = $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
                   6592:         }
                   6593:     }
                   6594:     if ($courseowner ne '') {
                   6595:         if ($courseowner eq $env{'user.name'}.':'.$env{'user.domain'}) {
                   6596:             return 1;
                   6597:         }
                   6598:     }
                   6599:     return;
                   6600: }
                   6601: 
1.165     raeburn  6602: sub get_selfenroll_titles {
                   6603:     my @row = ('types','registered','enroll_dates','access_dates','section',
                   6604:                'approval','limit');
                   6605:     my %lt = &Apache::lonlocal::texthash (
                   6606:                 types        => 'Users allowed to self-enroll',
                   6607:                 registered   => 'Registration status (official courses)' ,
                   6608:                 enroll_dates => 'Dates self-enrollment available',
                   6609:                 access_dates => 'Access dates for self-enrolling users',
                   6610:                 section      => "Self-enrolling users' section",
                   6611:                 approval     => 'Processing of requests',
                   6612:                 limit        => 'Enrollment limit',
                   6613:              );
                   6614:     return (\@row,\%lt);
                   6615: }
                   6616: 
                   6617: sub selfenroll_default_descs {
                   6618:     my %desc = (
                   6619:                  types => {
                   6620:                             dom => &mt('Course domain'),
                   6621:                             all => &mt('Any domain'),
                   6622:                             ''  => &mt('None'),
                   6623:                           },
                   6624:                  limit => {
                   6625:                             none         => &mt('No limit'),
                   6626:                             allstudents  => &mt('Limit by total students'),
                   6627:                             selfenrolled => &mt('Limit by total self-enrolled'),
                   6628:                           },
                   6629:                  approval => {
                   6630:                                 '0' => &mt('Processed automatically'),
                   6631:                                 '1' => &mt('Queued for approval'),
                   6632:                                 '2' => &mt('Queued, pending validation'),
                   6633:                              },
                   6634:                  registered => {
                   6635:                                  0 => 'No registration required',
                   6636:                                  1 => 'Registered students only',
                   6637:                                },
                   6638:                );
                   6639:     return %desc;
                   6640: }
                   6641: 
                   6642: sub selfenroll_validation_types {
                   6643:     my @items = ('url','fields','button','markup');
                   6644:     my %names =  &Apache::lonlocal::texthash (
                   6645:             url      => 'Web address of validation server/script',
                   6646:             fields   => 'Form fields to send to validator',
                   6647:             button   => 'Text for validation button',
                   6648:             markup   => 'Validation description (HTML)',
                   6649:     );
1.167     raeburn  6650:     my @fields = ('username','domain','uniquecode','course','coursetype','description');
1.165     raeburn  6651:     return (\@items,\%names,\@fields);
                   6652: }
                   6653: 
                   6654: sub get_extended_type {
                   6655:     my ($cdom,$cnum,$crstype,$current) = @_;
                   6656:     my $type = 'unofficial';
                   6657:     my %settings;
                   6658:     if (ref($current) eq 'HASH') {
                   6659:         %settings = %{$current};
                   6660:     } else {
                   6661:         %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.textbook'],$cdom,$cnum);
                   6662:     }
                   6663:     if ($crstype eq 'Community') {
                   6664:         $type = 'community';
1.173     raeburn  6665:     } elsif ($crstype eq 'Placement') {
                   6666:         $type = 'placement';
1.165     raeburn  6667:     } elsif ($settings{'internal.coursecode'}) {
                   6668:         $type = 'official';
                   6669:     } elsif ($settings{'internal.textbook'}) {
                   6670:         $type = 'textbook';
                   6671:     }
                   6672:     return $type;
                   6673: }
                   6674: 
                   6675: sub selfenrollment_administration {
                   6676:     my ($cdom,$cnum,$crstype,$coursehash) = @_;
                   6677:     my %settings;
                   6678:     if (ref($coursehash) eq 'HASH') {
                   6679:         %settings = %{$coursehash};
                   6680:     } else {
                   6681:         %settings = &Apache::lonnet::get('environment',
                   6682:                         ['internal.selfenrollmgrdc','internal.selfenrollmgrcc',
                   6683:                          'internal.coursecode','internal.textbook'],$cdom,$cnum);
                   6684:     }
                   6685:     my ($possconfigs) = &get_selfenroll_titles(); 
                   6686:     my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
                   6687:     my $selfenrolltype = &get_extended_type($cdom,$cnum,$crstype,\%settings);
                   6688: 
                   6689:     my (@in_course,@in_domain); 
                   6690:     if ($settings{'internal.selfenrollmgrcc'} ne '') {
                   6691:         @in_course = split(/,/,$settings{'internal.selfenrollmgrcc'}); 
                   6692:         my @diffs = &Apache::loncommon::compare_arrays($possconfigs,\@in_course);
                   6693:         unless (@diffs) {
                   6694:             return (\@in_course,\@in_domain);
                   6695:         }
                   6696:     }
                   6697:     if ($settings{'internal.selfenrollmgrdc'} ne '') {
1.184.4.10.2.  (raeburn 6698:):         @in_domain = split(/,/,$settings{'internal.selfenrollmgrdc'});
1.165     raeburn  6699:         my @diffs = &Apache::loncommon::compare_arrays(\@in_domain,$possconfigs);
                   6700:         unless (@diffs) {
                   6701:             return (\@in_course,\@in_domain);
                   6702:         }
                   6703:     }
                   6704:     my @combined = @in_course;
                   6705:     push(@combined,@in_domain);
                   6706:     my @diffs = &Apache::loncommon::compare_arrays(\@combined,$possconfigs); 
                   6707:     unless (@diffs) {
                   6708:         return (\@in_course,\@in_domain);
                   6709:     }
                   6710:     if ($domdefaults{$selfenrolltype.'selfenrolladmdc'} eq '') {
                   6711:         push(@in_course,@diffs);
                   6712:     } else {
                   6713:         my @defaultdc = split(/,/,$domdefaults{$selfenrolltype.'selfenrolladmdc'});
                   6714:         foreach my $item (@diffs) {
                   6715:             if (grep(/^\Q$item\E$/,@defaultdc)) {
                   6716:                 push(@in_domain,$item);
                   6717:             } else {
                   6718:                 push(@in_course,$item);
                   6719:             }
                   6720:         }
                   6721:     }
                   6722:     return (\@in_course,\@in_domain);
                   6723: }
                   6724: 
1.175     raeburn  6725: sub custom_role_header {
                   6726:     my ($context,$crstype,$templaterolerefs,$prefix) = @_;
                   6727:     my %lt = &Apache::lonlocal::texthash(
                   6728:                  sele => 'Select a Template',
                   6729:     );
                   6730:     my ($context_code,$button_code);
                   6731:     if ($context eq 'domain') {
                   6732:         $context_code = &custom_coursetype_switch($crstype,$prefix);
                   6733:     }
                   6734:     if (ref($templaterolerefs) eq 'ARRAY') {
                   6735:         foreach my $role (@{$templaterolerefs}) {
                   6736:             my $display = 'inline';
                   6737:             if (($context eq 'domain') && ($role eq 'co')) {
                   6738:                 $display = 'none';
                   6739:             }
                   6740:             $button_code .= &make_button_code($role,$crstype,$display,$prefix).' ';
                   6741:         }
                   6742:     }
                   6743:     return <<"END";
                   6744: <div class="LC_left_float">
                   6745: <fieldset>
                   6746: <legend>$lt{'sele'}</legend>
                   6747: $button_code
                   6748: </fieldset></div>
                   6749: $context_code
                   6750: <br clear="all" />
                   6751: END
                   6752: }
                   6753: 
                   6754: sub custom_coursetype_switch {
                   6755:     my ($crstype,$prefix) = @_;
                   6756:     my ($checkedcourse,$checkedcommunity);
                   6757:     if ($crstype eq 'Community') {
                   6758:         $checkedcommunity = ' checked="checked"';
                   6759:     } else {
                   6760:         $checkedcourse = ' checked="checked"';
                   6761:     }
                   6762:     my %lt = &Apache::lonlocal::texthash(
                   6763:         cont => 'Context',
                   6764:         cour => 'Course',
                   6765:         comm => 'Community',
                   6766:     );
                   6767:     return <<"END";
                   6768: <div class="LC_left_float">
                   6769: <fieldset>
                   6770: <legend>$lt{'cont'}</legend>
                   6771: <label>
                   6772: <input type="radio" name="${prefix}_custrolecrstype" value="Course"$checkedcourse onclick="javascript:customSwitchType('$prefix');" />
                   6773: $lt{'cour'}
                   6774: </label>&nbsp;&nbsp;
                   6775: <label>
                   6776: <input type="radio" name="${prefix}_custrolecrstype" value="Community"$checkedcommunity onclick="javascript:customSwitchType('$prefix');" />
                   6777: $lt{'comm'}
                   6778: </label>
                   6779: </fieldset>
                   6780: </div>
                   6781: END
                   6782: }
                   6783: 
                   6784: sub custom_role_table {
1.180     raeburn  6785:     my ($crstype,$full,$levels,$levelscurrent,$prefix,$add_class,$id) = @_;
1.175     raeburn  6786:     return unless ((ref($full) eq 'HASH') && (ref($levels) eq 'HASH') &&
                   6787:                    (ref($levelscurrent) eq 'HASH'));
                   6788:     my %lt=&Apache::lonlocal::texthash (
                   6789:                     'prv'  => "Privilege",
                   6790:                     'crl'  => "Course Level",
                   6791:                     'dml'  => "Domain Level",
                   6792:                     'ssl'  => "System Level");
                   6793:     my %cr = (
                   6794:                course => '_c',
                   6795:                domain => '_d',
                   6796:                system => '_s',
                   6797:              );
                   6798: 
1.180     raeburn  6799:     my $output=&Apache::loncommon::start_data_table($add_class,$id).
1.175     raeburn  6800:                &Apache::loncommon::start_data_table_header_row().
                   6801:                '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
                   6802:                '</th><th>'.$lt{'ssl'}.'</th>'.
                   6803:                &Apache::loncommon::end_data_table_header_row();
                   6804:     foreach my $priv (sort(keys(%{$full}))) {
                   6805:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
                   6806:         $output .= &Apache::loncommon::start_data_table_row().
                   6807:                   '<td><span id="'.$prefix.$priv.'">'.$privtext.'</span></td>';
                   6808:         foreach my $type ('course','domain','system') {
                   6809:             if (($type eq 'system') && ($priv eq 'bre') && ($crstype eq 'Community')) {
                   6810:                 $output .= '<td>&nbsp;</td>';
                   6811:             } else {
                   6812:                 $output .= '<td>'.
                   6813:                   ($levels->{$type}{$priv}?'<input type="checkbox" id="'.$prefix.$priv.$cr{$type}.'"'.
                   6814:                   ' name="'.$prefix.$priv.$cr{$type}.'"'.
                   6815:                   ($levelscurrent->{$type}{$priv}?' checked="checked"':'').' />':'&nbsp;').
                   6816:                   '</td>';
                   6817:             }
                   6818:         }
                   6819:         $output .= &Apache::loncommon::end_data_table_row();
                   6820:     }
                   6821:     $output .= &Apache::loncommon::end_data_table();
                   6822:     return $output;
                   6823: }
                   6824: 
                   6825: sub custom_role_privs {
                   6826:     my ($privs,$full,$levels,$levelscurrent)= @_;
                   6827:     return unless ((ref($privs) eq 'HASH') && (ref($full) eq 'HASH') &&
                   6828:                    (ref($levels) eq 'HASH') && (ref($levelscurrent) eq 'HASH'));
                   6829:     my %cr = (
                   6830:                course => 'cr:c',
                   6831:                domain => 'cr:d',
                   6832:                system => 'cr:s',
                   6833:              );
                   6834:     foreach my $type ('course','domain','system') {
                   6835:         foreach my $item (split(/\:/,$Apache::lonnet::pr{$cr{$type}})) {
                   6836:             my ($priv,$restrict)=split(/\&/,$item);
                   6837:             if (!$restrict) { $restrict='F'; }
                   6838:             $levels->{$type}->{$priv}=$restrict;
                   6839:             if ($privs->{$type}=~/\:$priv/) {
                   6840:                 $levelscurrent->{$type}->{$priv}=1;
                   6841:             }
                   6842:             $full->{$priv}=1;
                   6843:         }
                   6844:     }
                   6845:     return;
                   6846: }
                   6847: 
                   6848: sub custom_template_roles {
                   6849:     my ($context,$crstype) = @_;
                   6850:     my @template_roles = ("in","ta","ep");
                   6851:     if (($context eq 'domain') || ($context eq 'domprefs')) {
                   6852:         push(@template_roles,"ad");
                   6853:     }
                   6854:     push(@template_roles,"st");
                   6855:     if ($context eq 'domain') {
                   6856:         unshift(@template_roles,('co','cc'));
                   6857:     } else {
                   6858:         if ($crstype eq 'Community') {
                   6859:             unshift(@template_roles,'co');
                   6860:         } else {
                   6861:             unshift(@template_roles,'cc');
                   6862:         }
                   6863:     }
                   6864:     return @template_roles;
                   6865: }
                   6866: 
                   6867: sub custom_roledefs_js {
                   6868:     my ($context,$crstype,$formname,$full,$templaterolesref,$jsback) = @_;
                   6869:     my $button_code = "\n";
                   6870:     my $head_script = "\n";
                   6871:     my (%roletitlestr,$rolenamestr);
                   6872:     my %role_titles = (
                   6873:                         Course    => [],
                   6874:                         Community => [],
                   6875:                       );
                   6876:     $head_script .= '<script type="text/javascript">'."\n"
                   6877:                    .'// <![CDATA['."\n";
                   6878:     if (ref($templaterolesref) eq 'ARRAY') {
                   6879:         if ($context eq 'domain') {
                   6880:             $rolenamestr = join("','",@{$templaterolesref});
                   6881:         }
                   6882:         foreach my $role (@{$templaterolesref}) {
                   6883:             $head_script .= &make_script_template($role,$crstype,$formname);
                   6884:             if ($context eq 'domain') {
                   6885:                 foreach my $type ('Course','Community') {
                   6886:                     push(@{$role_titles{$type}},&Apache::lonnet::plaintext($role,$type));
                   6887:                 }
                   6888:             }
                   6889:         }
                   6890:     }
                   6891:     if ($context eq 'domain') {
                   6892:         foreach my $type ('Course','Community') {
                   6893:             $roletitlestr{$type} = join("','",@{$role_titles{$type}});
                   6894:         }
                   6895:         my %pt = (
                   6896:             Community => {
                   6897:                            cst => &mt('Grant/revoke role of Member'),
                   6898:                            mdc => &mt('Edit community contents'),
                   6899:                            pch => &mt('Post discussion on community resources'),
                   6900:                            pfo => &mt('Print for other users and entire community'),
                   6901:                          },
                   6902:             Course    => {
                   6903:                            cst => &mt('Grant/revoke role of Student'),
                   6904:                            mdc => &mt('Edit course contents'),
                   6905:                            pch => &mt('Post discussion on course resources'),
                   6906:                            pfo => &mt('Print for other users and entire course'),
                   6907:                          },
                   6908:         );
                   6909:         $head_script .= <<"ENDJS";
                   6910: function customSwitchType(prefix) {
                   6911:     var privnames = new Array('cst','mdc','pch','pfo');
                   6912:     var privtxtcrs = new Array('$pt{Course}{cst}','$pt{Course}{mdc}','$pt{Course}{pch}','$pt{Course}{pfo}');
                   6913:     var privtxtcom = new Array('$pt{Community}{cst}','$pt{Community}{mdc}','$pt{Community}{pch}','$pt{Community}{pfo}');
                   6914:     var rolenames = new Array('$rolenamestr');
                   6915:     var rolescrs = new Array('$roletitlestr{Course}');
                   6916:     var rolescom = new Array('$roletitlestr{Community}');
                   6917:     var radio = prefix+'_custrolecrstype';
                   6918:     if (document.$formname.elements[radio].length > 1) {
                   6919:         for (var i=0; i<document.$formname.elements[radio].length; i++) {
                   6920:             if (document.$formname.elements[radio][i].checked) {
                   6921:                 if ((document.getElementById(prefix+'bre_s')) && (document.getElementById(prefix+'bro_s'))) {
                   6922:                     if (document.$formname.elements[radio][i].value == 'Community') {
                   6923:                         if (document.getElementById(prefix+'bre_s').checked) {
                   6924:                             document.getElementById(prefix+'bro_s').checked = true;
                   6925:                             document.getElementById(prefix+'bre_s').checked = false;
                   6926: 
                   6927:                         }
                   6928:                         document.getElementById(prefix+'bre_s').style.visibility = 'hidden';
                   6929:                     } else {
                   6930:                         document.getElementById(prefix+'bre_s').style.visibility = 'visible';
                   6931:                         if (document.getElementById(prefix+'bro_s').checked) {
                   6932:                             document.getElementById(prefix+'bre_s').checked = true;
                   6933:                             document.getElementById(prefix+'bro_s').checked = false;
                   6934:                         }
                   6935:                     }
                   6936:                 }
                   6937:                 for (var j=0; j<privnames.length; j++) {
                   6938:                     if (document.getElementById(prefix+privnames[j])) {
                   6939:                         if (document.getElementById(prefix+privnames[j])) {
                   6940:                             if (document.$formname.elements[radio][i].value == 'Course') {
                   6941:                                 document.getElementById(prefix+privnames[j]).innerHTML = privtxtcrs[j];
                   6942:                             } else {
                   6943:                                 document.getElementById(prefix+privnames[j]).innerHTML = privtxtcom[j];
                   6944:                             }
                   6945:                         }
                   6946:                     }
                   6947:                 }
                   6948:                 for (var j=0; j<rolenames.length; j++) {
                   6949:                     if (document.getElementById(prefix+rolenames[j])) {
                   6950:                         if (document.getElementById(prefix+rolenames[j])) {
                   6951:                             if (document.$formname.elements[radio][i].value == 'Course') {
                   6952:                                 document.getElementById(prefix+rolenames[j]).value = rolescrs[j];
                   6953:                                 if (rolenames[j] == 'cc') {
                   6954:                                     document.getElementById(prefix+rolenames[j]).style.display = 'inline';
                   6955:                                 }
                   6956:                                 if (rolenames[j] == 'co') {
                   6957:                                     document.getElementById(prefix+rolenames[j]).style.display = 'none';
                   6958:                                 }
                   6959:                             } else {
                   6960:                                 document.getElementById(prefix+rolenames[j]).value = rolescom[j];
                   6961:                                 if (rolenames[j] == 'cc') {
                   6962:                                     document.getElementById(prefix+rolenames[j]).style.display = 'none';
                   6963:                                 }
                   6964:                                 if (rolenames[j] == 'co') {
                   6965:                                     document.getElementById(prefix+rolenames[j]).style.display = 'inline';
                   6966:                                 }
                   6967:                             }
                   6968:                         }
                   6969:                     }
                   6970:                 }
                   6971:             }
                   6972:         }
                   6973:     }
                   6974:     return;
                   6975: }
                   6976: ENDJS
                   6977:     }
                   6978:     $head_script .= "\n".$jsback."\n"
                   6979:                    .'// ]]>'."\n"
                   6980:                    .'</script>'."\n";
                   6981:     return $head_script;
                   6982: }
                   6983: 
                   6984: # --------------------------------------------------------
                   6985: sub make_script_template {
                   6986:     my ($role,$crstype,$formname) = @_;
                   6987:     my $return_script = 'function set_'.$role.'(prefix) {'."\n";
                   6988:     my (%full_by_level,%role_priv);
                   6989:     foreach my $level ('c','d','s') {
                   6990:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:'.$level})) {
                   6991:             next if (($level eq 's') && ($crstype eq 'Community') && ($item eq 'bre&S'));
                   6992:             my ($priv,$restrict)=split(/\&/,$item);
                   6993:             $full_by_level{$level}{$priv}=1;
                   6994:         }
                   6995:         $role_priv{$level} = {};
                   6996:         my @temp = split(/:/,$Apache::lonnet::pr{$role.':'.$level});
                   6997:         foreach my $priv (@temp) {
                   6998:             my ($priv_item, $dummy) = split(/\&/,$priv);
                   6999:             $role_priv{$level}{$priv_item} = 1;
                   7000:         }
                   7001:     }
                   7002:     my %to_check = (
                   7003:                       c => ['c','d','s'],
                   7004:                       d => ['d','s'],
                   7005:                       s => ['s'],
                   7006:                    );
                   7007:     foreach my $level ('c','d','s') {
                   7008:         if (ref($full_by_level{$level}) eq 'HASH') {
                   7009:             foreach my $priv (keys(%{$full_by_level{$level}})) {
                   7010:                 my $value = 'false';
                   7011:                 if (ref($to_check{$level}) eq 'ARRAY') {
                   7012:                     foreach my $lett (@{$to_check{$level}}) {
                   7013:                         if (exists($role_priv{$lett}{$priv})) {
                   7014:                             $value = 'true';
                   7015:                             last;
                   7016:                         }
                   7017:                     }
                   7018:                     $return_script .= "document.$formname.elements[prefix+'".$priv."_".$level."'].checked = $value;\n";
                   7019:                 }
                   7020:             }
                   7021:         }
                   7022:     }
                   7023:     $return_script .= '}'."\n";
                   7024:     return ($return_script);
                   7025: }
                   7026: # ----------------------------------------------------------
                   7027: sub make_button_code {
                   7028:     my ($role,$crstype,$display,$prefix) = @_;
                   7029:     my $label = &Apache::lonnet::plaintext($role,$crstype);
                   7030:     my $button_code = '<input type="button" onclick="set_'.$role."('$prefix'".')" '.
                   7031:                       'id="'.$prefix.$role.'" value="'.$label.'" '.
                   7032:                       'style="display:'.$display.'" />';
                   7033:     return ($button_code);
                   7034: }
                   7035: 
                   7036: sub custom_role_update {
                   7037:     my ($rolename,$prefix) = @_;
                   7038: # ------------------------------------------------------- What can be assigned?
                   7039:     my %privs = (
                   7040:                       c => '',
                   7041:                       d => '',
                   7042:                       s => '',
                   7043:                     );
                   7044:     foreach my $level (keys(%privs)) {
                   7045:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:'.$level})) {
                   7046:             my ($priv,$restrict)=split(/\&/,$item);
                   7047:             if (!$restrict) { $restrict=''; }
                   7048:             if ($env{'form.'.$prefix.$priv.'_'.$level}) {
                   7049:                 $privs{$level} .=':'.$item;
                   7050:             }
                   7051:         }
                   7052:     }
                   7053:     return %privs;
                   7054: }
                   7055: 
1.180     raeburn  7056: sub adhoc_status_types {
                   7057:     my ($cdom,$context,$role,$selectedref,$othertitle,$usertypes,$types,$disabled) = @_;
                   7058:     my $output = &Apache::loncommon::start_data_table();
                   7059:     my $numinrow = 3;
                   7060:     my $rem;
                   7061:     if (ref($types) eq 'ARRAY') {
                   7062:         for (my $i=0; $i<@{$types}; $i++) {
                   7063:             if (defined($usertypes->{$types->[$i]})) {
                   7064:                 my $rem = $i%($numinrow);
                   7065:                 if ($rem == 0) {
                   7066:                     if ($i > 0) {
                   7067:                         $output .= &Apache::loncommon::end_data_table_row();
                   7068:                     }
                   7069:                     $output .= &Apache::loncommon::start_data_table_row();
                   7070:                 }
                   7071:                 my $check;
                   7072:                 if (ref($selectedref) eq 'ARRAY') {
                   7073:                     if (grep(/^\Q$types->[$i]\E$/,@{$selectedref})) {
                   7074:                         $check = ' checked="checked"';
                   7075:                     }
                   7076:                 }
                   7077:                 $output .= '<td>'.
                   7078:                            '<span class="LC_nobreak"><label>'.
                   7079:                            '<input type="checkbox" name="'.$context.$role.'_status" '.
                   7080:                            'value="'.$types->[$i].'"'.$check.$disabled.' />'.
                   7081:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   7082:             }
                   7083:         }
                   7084:         $rem = @{$types}%($numinrow);
                   7085:     }
                   7086:     my $colsleft = $numinrow - $rem;
                   7087:     if (($rem == 0) && (@{$types} > 0)) {
                   7088:         $output .= &Apache::loncommon::start_data_table_row();
                   7089:     }
                   7090:     if ($colsleft > 1) {
                   7091:         $output .= '<td colspan="'.$colsleft.'">';
                   7092:     } else {
                   7093:         $output .= '<td>';
                   7094:     }
                   7095:     my $defcheck;
                   7096:     if (ref($selectedref) eq 'ARRAY') {
                   7097:         if (grep(/^default$/,@{$selectedref})) {
                   7098:             $defcheck = ' checked="checked"';
                   7099:         }
                   7100:     }
                   7101:     $output .= '<span class="LC_nobreak"><label>'.
                   7102:                '<input type="checkbox" name="'.$context.$role.'_status"'.
                   7103:                'value="default"'.$defcheck.$disabled.' />'.
                   7104:                $othertitle.'</label></span></td>'.
                   7105:                &Apache::loncommon::end_data_table_row().
                   7106:                &Apache::loncommon::end_data_table();
                   7107:     return $output;
                   7108: }
                   7109: 
                   7110: sub adhoc_staff {
                   7111:     my ($access,$context,$role,$selectedref,$adhocref,$disabled) = @_;
                   7112:     my $output;
                   7113:     if (ref($adhocref) eq 'HASH') {
                   7114:         my %by_fullname;
                   7115:         my $numinrow = 4;
                   7116:         my $rem;
                   7117:         my @personnel = keys(%{$adhocref});
                   7118:         if (@personnel) {
                   7119:             foreach my $person (@personnel) {
                   7120:                 my ($uname,$udom) = split(/:/,$person);
                   7121:                 my $fullname = &Apache::loncommon::plainname($uname,$udom,'lastname');
                   7122:                 $by_fullname{$fullname} = $person;
                   7123:             }
                   7124:             my @sorted = sort(keys(%by_fullname));
                   7125:             my $count = scalar(@sorted);
                   7126:             $output = &Apache::loncommon::start_data_table();
                   7127:             for (my $i=0; $i<$count; $i++) {
                   7128:                 my $rem = $i%($numinrow);
                   7129:                 if ($rem == 0) {
                   7130:                     if ($i > 0) {
                   7131:                         $output .= &Apache::loncommon::end_data_table_row();
                   7132:                     }
                   7133:                     $output .= &Apache::loncommon::start_data_table_row();
                   7134:                 }
                   7135:                 my $check;
                   7136:                 my $user = $by_fullname{$sorted[$i]};
                   7137:                 if (ref($selectedref) eq 'ARRAY') {
                   7138:                     if (grep(/^\Q$user\E$/,@{$selectedref})) {
                   7139:                         $check = ' checked="checked"';
                   7140:                     }
                   7141:                 }
                   7142:                 if ($i == $count-1) {
                   7143:                     my $colsleft = $numinrow - $rem;
                   7144:                     if ($colsleft > 1) {
                   7145:                         $output .= '<td colspan="'.$colsleft.'">';
                   7146:                     } else {
                   7147:                         $output .= '<td>';
                   7148:                     }
                   7149:                 } else {
                   7150:                     $output .= '<td>';
                   7151:                 }
                   7152:                 $output .= '<span class="LC_nobreak"><label>'.
                   7153:                            '<input type="checkbox" name="'.$context.$role.'_staff_'.$access.'" '.
                   7154:                            'value="'.$user.'"'.$check.$disabled.' />'.$sorted[$i].
                   7155:                            '</label></span></td>';
                   7156:                 if ($i == $count-1) {
                   7157:                     $output .= &Apache::loncommon::end_data_table_row();
                   7158:                 }
                   7159:             }
                   7160:             $output .= &Apache::loncommon::end_data_table();
                   7161:         }
                   7162:     }
                   7163:     return $output;
                   7164: }
                   7165: 
                   7166: 
1.1       raeburn  7167: 1;
                   7168: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>