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

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.8 2024/08/24 23:49:48 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.184.4.10.2.  (raeburn 2283:):             next if (($userdata{'username'} eq '') && ($userdata{'domain'} eq ''));
1.24      raeburn  2284:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   2285:                                $item,$userlist);
1.22      raeburn  2286:         } elsif ($context eq 'course') {
                   2287:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'},
                   2288:              $userdata{'section'}) = split(/:/,$item,-1);
                   2289:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
                   2290:             if (($viewablesec ne '') && ($userdata{'section'} ne '')) {
                   2291:                 next if ($viewablesec ne $userdata{'section'});
                   2292:             }
1.24      raeburn  2293:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   2294:                                $item,$userlist);
1.2       raeburn  2295:         } elsif ($context eq 'domain') {
                   2296:             if ($env{'form.roletype'} eq 'domain') {
                   2297:                 ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
                   2298:                     split(/:/,$item);
                   2299:                 ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1.24      raeburn  2300:                 &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   2301:                                    $item,$userlist);
1.13      raeburn  2302:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2303:                 if (ref($rolehash->{$item}) eq 'HASH') {
                   2304:                     $userdata{'extent'} = $item;
                   2305:                     foreach my $key (keys(%{$rolehash->{$item}})) {
                   2306:                         ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =  split(/:/,$key);
                   2307:                         ($userdata{'start'},$userdata{'end'}) = 
                   2308:                             split(/:/,$rolehash->{$item}{$key});
                   2309:                         my $uniqid = $key.':'.$item;
1.25      raeburn  2310:                         &build_user_record($context,\%userdata,$userinfo,
                   2311:                                            $indexhash,$uniqid,$userlist);
1.2       raeburn  2312:                     }
                   2313:                 }
1.102     raeburn  2314:             } elsif (($env{'form.roletype'} eq 'course') || 
                   2315:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  2316:                 ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   2317:                     split(/:/,$item);
                   2318:                 if (ref($rolehash->{$item}) eq 'HASH') {
1.11      raeburn  2319:                     my $numcids = keys(%{$rolehash->{$item}});
1.2       raeburn  2320:                     foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
                   2321:                         if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
                   2322:                             my $spanstart = '';
                   2323:                             my $spanend = '; ';
                   2324:                             my $space = ', ';
                   2325:                             if ($format eq 'html' || $format eq 'view') {
                   2326:                                 $spanstart = '<span class="LC_nobreak">';
1.23      raeburn  2327:                                 # FIXME: actions on courses disabled for now
                   2328: #                                if ($permission->{'cusr'}) {
                   2329: #                                    if ($numcids > 1) {
1.25      raeburn  2330: #                                        $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  2331: #                                    } else {
1.25      raeburn  2332: #                                        $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  2333: #                                    }
                   2334: #                                }
1.2       raeburn  2335:                                 $spanend = '</span><br />';
                   2336:                                 $space = ',&nbsp;';
                   2337:                             }
                   2338:                             $userdata{'extent'} .= $spanstart.
                   2339:                                     $rolehash->{$item}{$cid}{'desc'}.$space;
                   2340:                             if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') { 
                   2341:                                 foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1.25      raeburn  2342:                                     if (($env{'form.Status'} eq 'Any') ||
                   2343:                                         ($env{'form.Status'} eq $rolehash->{$item}{$cid}{'secs'}{$sec})) {
                   2344:                                         $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
                   2345:                                         $userdata{'status'} = $rolehash->{$item}{$cid}{'secs'}{$sec};
                   2346:                                     }
1.2       raeburn  2347:                                 }
                   2348:                             }
                   2349:                         }
                   2350:                     }
                   2351:                 }
1.25      raeburn  2352:                 if ($userdata{'status'} ne '') {
                   2353:                     &build_user_record($context,\%userdata,$userinfo,
                   2354:                                        $indexhash,$item,$userlist);
                   2355:                 }
1.2       raeburn  2356:             }
                   2357:         }
                   2358:     }
                   2359:     return;
                   2360: }
                   2361: 
                   2362: sub build_user_record {
1.24      raeburn  2363:     my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11      raeburn  2364:     next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.102     raeburn  2365:     if (!(($context eq 'domain') && (($env{'form.roletype'} eq 'course')
                   2366:                              && ($env{'form.roletype'} eq 'community')))) {
1.24      raeburn  2367:         &process_date_info($userdata);
                   2368:     }
1.2       raeburn  2369:     my $username = $userdata->{'username'};
                   2370:     my $domain = $userdata->{'domain'};
                   2371:     if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1.24      raeburn  2372:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1.2       raeburn  2373:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   2374:     } else {
                   2375:         &aggregate_user_info($domain,$username,$userinfo);
                   2376:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
                   2377:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   2378:     }
                   2379:     foreach my $key (keys(%{$indexhash})) {
                   2380:         if (defined($userdata->{$key})) {
                   2381:             $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
                   2382:         }
                   2383:     }
                   2384:     return;
                   2385: }
                   2386: 
                   2387: sub courses_selector {
                   2388:     my ($cdom,$formname) = @_;
                   2389:     my %codes = ();
                   2390:     my @codetitles = ();
                   2391:     my %cat_titles = ();
                   2392:     my %cat_order = ();
                   2393:     my %idlist = ();
                   2394:     my %idnums = ();
                   2395:     my %idlist_titles = ();
                   2396:     my $caller = 'global';
                   2397:     my $format_reply;
                   2398:     my $jscript = '';
                   2399: 
1.7       albertel 2400:     my $totcodes = 0;
1.184.4.3  raeburn  2401:     my $instcats = &Apache::lonnet::get_dom_instcats($cdom);
                   2402:     if (ref($instcats) eq 'HASH') {
                   2403:         if ((ref($instcats->{'codetitles'}) eq 'ARRAY') && (ref($instcats->{'codes'}) eq 'HASH') &&
                   2404:             (ref($instcats->{'cat_titles'}) eq 'HASH') && (ref($instcats->{'cat_order'}) eq 'HASH')) {
                   2405:             %codes = %{$instcats->{'codes'}};
                   2406:             @codetitles = @{$instcats->{'codetitles'}};
                   2407:             %cat_titles = %{$instcats->{'cat_titles'}};
                   2408:             %cat_order = %{$instcats->{'cat_order'}};
                   2409:             $totcodes = scalar(keys(%codes));
1.2       raeburn  2410:             my $numtypes = @codetitles;
                   2411:             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
                   2412:             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
                   2413:             my $longtitles_str = join('","',@{$longtitles});
                   2414:             my $allidlist = $idlist{$codetitles[0]};
                   2415:             $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
                   2416:             $jscript .= $scripttext;
1.181     raeburn  2417:             $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,\@codetitles);
1.2       raeburn  2418:         }
                   2419:     }
                   2420:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
                   2421: 
                   2422:     my %elements = (
                   2423:                      Year => 'selectbox',
                   2424:                      coursepick => 'radio',
                   2425:                      coursetotal => 'text',
                   2426:                      courselist => 'text',
                   2427:                    );
                   2428:     $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   2429:     if ($env{'form.coursepick'} eq 'category') {
                   2430:         $jscript .= qq|
                   2431: function setCourseCat(formname) {
                   2432:     if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
                   2433:         return;
                   2434:     }
1.120     raeburn  2435:     courseSet('$codetitles[0]');
1.2       raeburn  2436:     for (var j=0; j<formname.Semester.length; j++) {
                   2437:         if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
                   2438:             formname.Semester.options[j].selected = true;
                   2439:         }
                   2440:     }
                   2441:     if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
                   2442:         return;
                   2443:     }
1.120     raeburn  2444:     courseSet('$codetitles[1]');
1.2       raeburn  2445:     for (var j=0; j<formname.Department.length; j++) {
1.184.4.3  raeburn  2446:         if (formname.Department.options[j].value == "$env{'form.Department'}") {
                   2447:             formname.Department.options[j].selected = true;
1.2       raeburn  2448:         }
                   2449:     }
                   2450:     if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
                   2451:         return;
                   2452:     }
1.120     raeburn  2453:     courseSet('$codetitles[2]');
1.2       raeburn  2454:     for (var j=0; j<formname.Number.length; j++) {
                   2455:         if (formname.Number.options[j].value == "$env{'form.Number'}") {
                   2456:             formname.Number.options[j].selected = true;
                   2457:         }
                   2458:     }
                   2459: }
                   2460: |;
                   2461:     }
                   2462:     return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
                   2463:             \%idlist_titles);
                   2464: }
                   2465: 
                   2466: sub course_selector_loadcode {
                   2467:     my ($formname) = @_;
                   2468:     my $loadcode;
                   2469:     if ($env{'form.coursepick'} ne '') {
                   2470:         $loadcode = 'javascript:setFormElements(document.'.$formname.')';
                   2471:         if ($env{'form.coursepick'} eq 'category') {
                   2472:             $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
                   2473:         }
                   2474:     }
                   2475:     return $loadcode;
                   2476: }
                   2477: 
                   2478: sub process_coursepick {
                   2479:     my $coursefilter = $env{'form.coursepick'};
                   2480:     my $cdom = $env{'request.role.domain'};
                   2481:     my %courses;
1.105     raeburn  2482:     my $crssrch = 'Course';
                   2483:     if ($env{'form.roletype'} eq 'community') {
                   2484:         $crssrch = 'Community';
                   2485:     }
1.2       raeburn  2486:     if ($coursefilter eq 'all') {
                   2487:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
1.105     raeburn  2488:                                                  undef,undef,$crssrch);
1.2       raeburn  2489:     } elsif ($coursefilter eq 'category') {
                   2490:         my $instcode = &instcode_from_coursefilter();
                   2491:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
1.105     raeburn  2492:                                                  undef,undef,$crssrch);
1.2       raeburn  2493:     } elsif ($coursefilter eq 'specific') {
                   2494:         if ($env{'form.coursetotal'} > 1) {
                   2495:             my @course_ids = split(/&&/,$env{'form.courselist'});
                   2496:             foreach my $cid (@course_ids) {
                   2497:                 $courses{$cid} = '';
1.1       raeburn  2498:             }
1.2       raeburn  2499:         } else {
                   2500:             $courses{$env{'form.courselist'}} = '';
1.1       raeburn  2501:         }
1.2       raeburn  2502:     }
                   2503:     return %courses;
                   2504: }
                   2505: 
                   2506: sub instcode_from_coursefilter {
                   2507:     my $instcode = '';
                   2508:     my @cats = ('Semester','Year','Department','Number');
                   2509:     foreach my $category (@cats) {
                   2510:         if (defined($env{'form.'.$category})) {
                   2511:             unless ($env{'form.'.$category} eq '-1') {
                   2512:                 $instcode .= $env{'form.'.$category};
                   2513:            }
                   2514:         }
                   2515:     }
                   2516:     if ($instcode eq '') {
                   2517:         $instcode = '.';
                   2518:     }
                   2519:     return $instcode;
                   2520: }
                   2521: 
                   2522: sub make_keylist_array {
                   2523:     my ($index,$keylist);
                   2524:     $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
                   2525:     $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
                   2526:     $index->{'end'} = &Apache::loncoursedata::CL_END();
                   2527:     $index->{'start'} = &Apache::loncoursedata::CL_START();
                   2528:     $index->{'id'} = &Apache::loncoursedata::CL_ID();
                   2529:     $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
                   2530:     $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
                   2531:     $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
                   2532:     $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
                   2533:     $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
                   2534:     $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
                   2535:     $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
                   2536:     $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
                   2537:     $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
1.44      raeburn  2538:     $index->{'photo'} = &Apache::loncoursedata::CL_PHOTO();
1.47      raeburn  2539:     $index->{'thumbnail'} = &Apache::loncoursedata::CL_THUMBNAIL();
1.150     raeburn  2540:     $index->{'credits'} = &Apache::loncoursedata::CL_CREDITS();
1.174     raeburn  2541:     $index->{'instsec'} = &Apache::loncoursedata::CL_INSTSEC();
1.159     raeburn  2542:     $index->{'authorquota'} = &Apache::loncoursedata::CL_AUTHORQUOTA();
                   2543:     $index->{'authorusage'} = &Apache::loncoursedata::CL_AUTHORUSAGE();
1.184.4.10.2.  (raeburn 2544:):     $index->{'manager'} = &Apache::loncoursedata::CL_CAMANAGER();
1.2       raeburn  2545:     foreach my $key (keys(%{$index})) {
                   2546:         $keylist->[$index->{$key}] = $key;
                   2547:     }
                   2548:     return ($index,$keylist);
                   2549: }
                   2550: 
                   2551: sub aggregate_user_info {
                   2552:     my ($udom,$uname,$userinfo) = @_;
                   2553:     my %info=&Apache::lonnet::get('environment',
                   2554:                                   ['firstname','middlename',
                   2555:                                    'lastname','generation','id'],
                   2556:                                    $udom,$uname);
                   2557:     my ($tmp) = keys(%info);
                   2558:     my ($fullname,$id);
                   2559:     if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   2560:         $fullname = 'not available';
                   2561:         $id = 'not available';
                   2562:         &Apache::lonnet::logthis('unable to retrieve environment '.
                   2563:                                  'for '.$uname.':'.$udom);
1.1       raeburn  2564:     } else {
1.2       raeburn  2565:         $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
                   2566:         $id = $info{'id'};
                   2567:     }
                   2568:     $userinfo->{$uname.':'.$udom} = { 
                   2569:                                       fullname => $fullname,
                   2570:                                       id       => $id,
                   2571:                                     };
                   2572:     return;
                   2573: }
1.1       raeburn  2574: 
1.2       raeburn  2575: sub process_date_info {
                   2576:     my ($userdata) = @_;
                   2577:     my $now = time;
1.83      raeburn  2578:     $userdata->{'status'} = 'Active';
1.2       raeburn  2579:     if ($userdata->{'start'} > 0) {
                   2580:         if ($now < $userdata->{'start'}) {
1.83      raeburn  2581:             $userdata->{'status'} = 'Future';
1.2       raeburn  2582:         }
1.1       raeburn  2583:     }
1.2       raeburn  2584:     if ($userdata->{'end'} > 0) {
                   2585:         if ($now > $userdata->{'end'}) {
1.83      raeburn  2586:             $userdata->{'status'} = 'Expired';
1.2       raeburn  2587:         }
                   2588:     }
                   2589:     return;
1.1       raeburn  2590: }
                   2591: 
                   2592: sub show_users_list {
1.150     raeburn  2593:     my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist,$formname,
1.159     raeburn  2594:         $showcredits,$needauthorquota,$needauthorusage)=@_;
1.55      raeburn  2595:     if ($formname eq '') {
                   2596:         $formname = 'studentform';
                   2597:     }
1.159     raeburn  2598:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.1       raeburn  2599:     #
                   2600:     # Variables for excel output
                   2601:     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
                   2602:     #
                   2603:     # Variables for csv output
                   2604:     my ($CSVfile,$CSVfilename);
                   2605:     #
                   2606:     my $sortby = $env{'form.sortby'};
1.3       raeburn  2607:     my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2       raeburn  2608:     if ($context eq 'course') {
1.3       raeburn  2609:         push(@sortable,('section','groups','type'));
1.150     raeburn  2610:         if ($showcredits) {
                   2611:             push(@sortable,'credits');
                   2612:         }
1.2       raeburn  2613:     } else {
1.3       raeburn  2614:         push(@sortable,'extent');
1.159     raeburn  2615:         if (($context eq 'domain') && ($env{'form.roletype'} eq 'domain') &&
                   2616:             (($env{'form.showrole'} eq 'Any') || ($env{'form.showrole'} eq 'au'))) {
                   2617:             push(@sortable,('authorusage','authorquota'));
                   2618:         }
1.184.4.10.2.  (raeburn 2619:):         if ($context eq 'author') {
                   2620:):             push(@sortable,'manager');
                   2621:):         }
1.3       raeburn  2622:     }
1.55      raeburn  2623:     if ($mode eq 'pickauthor') {
                   2624:         @sortable = ('username','fullname','email','status');
                   2625:     }
1.156     raeburn  2626:     my %is_sortable;
1.158     raeburn  2627:     map { $is_sortable{$_} = 1; } @sortable;
1.156     raeburn  2628:     unless ($is_sortable{$sortby}) {
1.3       raeburn  2629:         $sortby = 'username';
1.1       raeburn  2630:     }
1.22      raeburn  2631:     my $setting = $env{'form.roletype'};
1.150     raeburn  2632:     my ($cid,$cdom,$cnum,$classgroups,$crstype,$defaultcredits);
1.1       raeburn  2633:     if ($context eq 'course') {
1.22      raeburn  2634:         $cid = $env{'request.course.id'};
1.101     raeburn  2635:         $crstype = &Apache::loncommon::course_type();
1.17      raeburn  2636:         ($cnum,$cdom) = &get_course_identity($cid);
1.150     raeburn  2637:         $defaultcredits = $env{'course.'.$cid.'.internal.defaultcredits'};
1.2       raeburn  2638:         ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   2639:                                      $userlist,$keylist,$cdom,$cnum);
1.16      raeburn  2640:         if ($mode eq 'autoenroll') {
                   2641:             $env{'form.showrole'} = 'st';
                   2642:         } else {
                   2643:             if ($env{'course.'.$cid.'.internal.showphoto'}) {
                   2644:                 $r->print('
1.1       raeburn  2645: <script type="text/javascript">
1.96      bisitz   2646: // <![CDATA[
1.1       raeburn  2647: function photowindow(photolink) {
                   2648:     var title = "Photo_Viewer";
                   2649:     var options = "scrollbars=1,resizable=1,menubar=0";
                   2650:     options += ",width=240,height=240";
                   2651:     stdeditbrowser = open(photolink,title,options,"1");
                   2652:     stdeditbrowser.focus();
                   2653: }
1.96      bisitz   2654: // ]]>
1.1       raeburn  2655: </script>
1.16      raeburn  2656:                ');
                   2657:             }
                   2658:         }
1.102     raeburn  2659:     } elsif ($context eq 'domain') {
                   2660:         if ($setting eq 'community') {
                   2661:             $crstype = 'Community';
1.105     raeburn  2662:         } elsif ($setting eq 'course') {
1.102     raeburn  2663:             $crstype = 'Course';
                   2664:         }
1.1       raeburn  2665:     }
1.55      raeburn  2666:     if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.40      raeburn  2667:         my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode);
1.56      raeburn  2668:         my $verify_action_js = &bulkaction_javascript($formname);
1.1       raeburn  2669:         $r->print(<<END);
1.10      raeburn  2670: 
                   2671: <script type="text/javascript" language="Javascript">
1.96      bisitz   2672: // <![CDATA[
1.11      raeburn  2673: 
1.56      raeburn  2674: $verify_action_js
1.10      raeburn  2675: 
                   2676: function username_display_launch(username,domain) {
                   2677:     var target;
1.177     raeburn  2678:     if (!document.$formname.usernamelink.length) {
                   2679:         target = document.$formname.usernamelink.value;
                   2680:     } else {
                   2681:         for (var i=0; i<document.$formname.usernamelink.length; i++) {
                   2682:             if (document.$formname.usernamelink[i].checked) {
                   2683:                target = document.$formname.usernamelink[i].value;
                   2684:             }
1.10      raeburn  2685:         }
                   2686:     }
1.179     raeburn  2687:     if ((target == 'modify') || (target == 'activity')) {
                   2688:         var nextaction = 'singleuser';
                   2689:         if (target == 'activity') {
                   2690:             nextaction = 'accesslogs';
                   2691:         }
1.55      raeburn  2692:         if (document.$formname.userwin.checked == true) {
1.179     raeburn  2693:             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  2694:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2695:             modifywin = window.open(url,'',options,1);
                   2696:             modifywin.focus();
                   2697:             return;
                   2698:         } else {
1.55      raeburn  2699:             document.$formname.srchterm.value=username;
                   2700:             document.$formname.srchdomain.value=domain;
                   2701:             document.$formname.phase.value='get_user_info';
1.179     raeburn  2702:             document.$formname.action.value = nextaction;
1.55      raeburn  2703:             document.$formname.submit();
1.50      raeburn  2704:         }
1.10      raeburn  2705:     }
1.48      raeburn  2706:     if (target == 'aboutme') {
1.55      raeburn  2707:         if (document.$formname.userwin.checked == true) {
1.50      raeburn  2708:             var url = '/adm/'+domain+'/'+username+'/aboutme?popup=1';
                   2709:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2710:             aboutmewin = window.open(url,'',options,1);
                   2711:             aboutmewin.focus();
                   2712:             return;
                   2713:         } else {
                   2714:             document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
                   2715:         }
1.48      raeburn  2716:     }
1.98      raeburn  2717:     if (target == 'track') {
                   2718:         if (document.$formname.userwin.checked == true) {
                   2719:             var url = '/adm/trackstudent?selected_student='+username+':'+domain+'&only_body=1';
                   2720:             var options = 'height=600,width=800,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no';
                   2721:             var trackwin = window.open(url,'',options,1);
                   2722:             trackwin.focus();
                   2723:             return;
                   2724:         } else {
                   2725:             document.location.href = '/adm/trackstudent?selected_student='+username+':'+domain;
                   2726:         }
                   2727:     }
1.10      raeburn  2728: }
1.96      bisitz   2729: // ]]>
1.10      raeburn  2730: </script>
1.11      raeburn  2731: $date_sec_selector
1.1       raeburn  2732: <input type="hidden" name="state" value="$env{'form.state'}" />
                   2733: END
                   2734:     }
                   2735:     $r->print(<<END);
                   2736: <input type="hidden" name="sortby" value="$sortby" />
                   2737: END
1.150     raeburn  2738:     my @cols = &infocolumns($context,$mode,$showcredits);
1.140     raeburn  2739:     my %coltxt = &get_column_names($context);
                   2740:     my %acttxt = &Apache::lonlocal::texthash(
1.11      raeburn  2741:                        'pr'         => "Proceed",
                   2742:                        'ac'         => "Action to take for selected users",
1.56      raeburn  2743:                        'link'       => "Behavior of clickable username link for each user",
1.82      weissno  2744:                        'aboutme'    => "Display a user's personal information page",
1.50      raeburn  2745:                        'owin'       => "Open in a new window",
1.10      raeburn  2746:                        'modify'     => "Modify a user's information",
1.98      raeburn  2747:                        'track'      => "View a user's recent activity",
1.179     raeburn  2748:                        'activity'   => "View a user's access log", 
1.1       raeburn  2749:                       );
1.140     raeburn  2750:     my %lt = (%coltxt,%acttxt);
1.4       raeburn  2751:     my $rolefilter = $env{'form.showrole'};
1.5       raeburn  2752:     if ($env{'form.showrole'} eq 'cr') {
                   2753:         $rolefilter = &mt('custom');  
                   2754:     } elsif ($env{'form.showrole'} ne 'Any') {
1.101     raeburn  2755:         $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'},$crstype);
1.2       raeburn  2756:     }
1.16      raeburn  2757:     my $results_description;
                   2758:     if ($mode ne 'autoenroll') {
                   2759:         $results_description = &results_header_row($rolefilter,$statusmode,
1.102     raeburn  2760:                                                    $context,$permission,$mode,$crstype);
1.140     raeburn  2761:         $r->print('<b>'.$results_description.'</b><br clear="all" />');
1.16      raeburn  2762:     }
1.26      raeburn  2763:     my ($output,$actionselect,%canchange,%canchangesec);
1.55      raeburn  2764:     if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
                   2765:         if ($mode ne 'autoenroll' && $mode ne 'pickauthor') {
1.16      raeburn  2766:             if ($permission->{'cusr'}) {
1.105     raeburn  2767:                 unless (($context eq 'domain') && 
                   2768:                         (($setting eq 'course') || ($setting eq 'community'))) {
                   2769:                     $actionselect = 
                   2770:                         &select_actions($context,$setting,$statusmode,$formname);
                   2771:                 }
1.16      raeburn  2772:             }
                   2773:             $r->print(<<END);
1.10      raeburn  2774: <input type="hidden" name="srchby"  value="uname" />
                   2775: <input type="hidden" name="srchin"   value="dom" />
                   2776: <input type="hidden" name="srchtype" value="exact" />
                   2777: <input type="hidden" name="srchterm" value="" />
1.11      raeburn  2778: <input type="hidden" name="srchdomain" value="" /> 
1.1       raeburn  2779: END
1.16      raeburn  2780:             if ($actionselect) {
1.41      raeburn  2781:                 $output .= <<"END";
1.94      bisitz   2782: <div class="LC_left_float"><fieldset><legend>$lt{'ac'}</legend>
1.56      raeburn  2783: $actionselect
                   2784: <br/><br /><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.$formname.actionlist)" /> &nbsp;
                   2785: <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  2786: END
1.26      raeburn  2787:                 my @allroles;
                   2788:                 if ($env{'form.showrole'} eq 'Any') {
                   2789:                     my $custom = 1;
                   2790:                     if ($context eq 'domain') {
1.101     raeburn  2791:                         @allroles = &roles_by_context($setting,$custom,$crstype);
1.26      raeburn  2792:                     } else {
1.101     raeburn  2793:                         @allroles = &roles_by_context($context,$custom,$crstype);
1.26      raeburn  2794:                     }
                   2795:                 } else {
                   2796:                     @allroles = ($env{'form.showrole'});
                   2797:                 }
                   2798:                 foreach my $role (@allroles) {
                   2799:                     if ($context eq 'domain') {
                   2800:                         if ($setting eq 'domain') {
                   2801:                             if (&Apache::lonnet::allowed('c'.$role,
                   2802:                                     $env{'request.role.domain'})) {
                   2803:                                 $canchange{$role} = 1;
                   2804:                             }
1.31      raeburn  2805:                         } elsif ($setting eq 'author') {
                   2806:                             if (&Apache::lonnet::allowed('c'.$role,
                   2807:                                     $env{'request.role.domain'})) {
                   2808:                                 $canchange{$role} = 1;
                   2809:                             }
1.26      raeburn  2810:                         }
                   2811:                     } elsif ($context eq 'author') {
                   2812:                         if (&Apache::lonnet::allowed('c'.$role,
                   2813:                             $env{'user.domain'}.'/'.$env{'user.name'})) {
                   2814:                             $canchange{$role} = 1;
                   2815:                         }
                   2816:                     } elsif ($context eq 'course') {
                   2817:                         if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   2818:                             $canchange{$role} = 1;
                   2819:                         } elsif ($env{'request.course.sec'} ne '') {
                   2820:                             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   2821:                                 $canchangesec{$role} = $env{'request.course.sec'};
                   2822:                             }
1.141     raeburn  2823:                         } elsif ((($role eq 'co') && ($crstype eq 'Community')) ||
                   2824:                                  (($role eq 'cc') && ($crstype eq 'Course'))) {
                   2825:                             if (&is_courseowner($env{'request.course.id'},
                   2826:                                                 $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'})) {
                   2827:                                 $canchange{$role} = 1;
                   2828:                             }
1.26      raeburn  2829:                         }
                   2830:                     }
                   2831:                 }
1.16      raeburn  2832:             }
1.94      bisitz   2833:             $output .= '<div class="LC_left_float"><fieldset><legend>'.$lt{'link'}.'</legend>'.
1.56      raeburn  2834:                        '<table><tr>';
                   2835:             my @linkdests = ('aboutme');
                   2836:             if ($permission->{'cusr'}) {
                   2837:                 unshift (@linkdests,'modify');
                   2838:             }
1.179     raeburn  2839:             if ($context eq 'course') {
                   2840:                 if (&Apache::lonnet::allowed('vsa', $env{'request.course.id'}) ||
                   2841:                     &Apache::lonnet::allowed('vsa', $env{'request.course.id'}.'/'.
                   2842:                                              $env{'request.course.sec'})) {
                   2843:                     push(@linkdests,'track');
                   2844:                 }
                   2845:             } elsif ($context eq 'domain') {
                   2846:                 if (&Apache::lonnet::allowed('vac',$env{'request.role.domain'})) {
                   2847:                     push(@linkdests,'activity');
                   2848:                 }
1.98      raeburn  2849:             }
1.56      raeburn  2850:             $output .= '<td>';
                   2851:             my $usernamelink = $env{'form.usernamelink'};
                   2852:             if ($usernamelink eq '') {
                   2853:                 $usernamelink = 'aboutme';
                   2854:             }
                   2855:             foreach my $item (@linkdests) {
                   2856:                 my $checkedstr = '';
                   2857:                 if ($item eq $usernamelink) {
1.86      bisitz   2858:                     $checkedstr = ' checked="checked"';
1.56      raeburn  2859:                 }
1.86      bisitz   2860:                 $output .= '<span class="LC_nobreak"><label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.' />&nbsp;'.$lt{$item}.'</label></span><br />';
1.56      raeburn  2861:             }
                   2862:             my $checkwin;
                   2863:             if ($env{'form.userwin'}) {
1.86      bisitz   2864:                 $checkwin = ' checked="checked"';
1.56      raeburn  2865:             }
1.144     bisitz   2866:             $output .=
                   2867:                 '</td><td valign="top"  style="border-left: 1px solid;">'
                   2868:                .'<span class="LC_nobreak"><label>'
                   2869:                .'<input type="checkbox" name="userwin" value="1"'.$checkwin.' />'.$lt{'owin'}
                   2870:                .'</label></span></td></tr></table></fieldset></div>';
1.4       raeburn  2871:         }
1.184     raeburn  2872:         $output .= "\n".'<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
1.1       raeburn  2873:                   &Apache::loncommon::start_data_table().
1.4       raeburn  2874:                   &Apache::loncommon::start_data_table_header_row();
1.1       raeburn  2875:         if ($mode eq 'autoenroll') {
1.4       raeburn  2876:             $output .= "
1.55      raeburn  2877:  <th><a href=\"javascript:document.$formname.sortby.value='type';document.$formname.submit();\">$lt{'type'}</a></th>
1.4       raeburn  2878:             ";
1.1       raeburn  2879:         } else {
1.105     raeburn  2880:             $output .= "\n".'<th>&nbsp;</th>'."\n";
1.11      raeburn  2881:             if ($actionselect) {
1.159     raeburn  2882:                 $output .= '<th class="LC_nobreak" valign="top">'.&mt('Select').'</th>'."\n";
1.11      raeburn  2883:             }
1.1       raeburn  2884:         }
                   2885:         foreach my $item (@cols) {
1.159     raeburn  2886:             $output .= '<th class="LC_nobreak" valign="top">';
1.156     raeburn  2887:             if ($is_sortable{$item}) {
1.159     raeburn  2888:                 $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  2889:             } else {
                   2890:                 $output .= $lt{$item};
                   2891:             }
                   2892:             $output .= "</th>\n";
1.1       raeburn  2893:         }
1.2       raeburn  2894:         my %role_types = &role_type_names();
1.16      raeburn  2895:         $output .= &Apache::loncommon::end_data_table_header_row();
1.1       raeburn  2896: # Done with the HTML header line
                   2897:     } elsif ($mode eq 'csv') {
                   2898:         #
                   2899:         # Open a file
                   2900:         $CSVfilename = '/prtspool/'.
1.2       raeburn  2901:                        $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
                   2902:                        time.'_'.rand(1000000000).'.csv';
1.1       raeburn  2903:         unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
                   2904:             $r->log_error("Couldn't open $CSVfilename for output $!");
1.108     bisitz   2905:             $r->print(
                   2906:                 '<p class="LC_error">'
                   2907:                .&mt('Problems occurred in writing the CSV file.')
                   2908:                .' '.&mt('This error has been logged.')
                   2909:                .' '.&mt('Please alert your LON-CAPA administrator.')
                   2910:                .'</p>'
                   2911:             );
1.1       raeburn  2912:             $CSVfile = undef;
                   2913:         }
                   2914:         #
                   2915:         # Write headers and data to file
1.2       raeburn  2916:         print $CSVfile '"'.$results_description.'"'."\n"; 
1.1       raeburn  2917:         print $CSVfile '"'.join('","',map {
                   2918:             &Apache::loncommon::csv_translate($lt{$_})
1.67      droeschl 2919:             } (@cols))."\"\n";
1.1       raeburn  2920:     } elsif ($mode eq 'excel') {
                   2921:         # Create the excel spreadsheet
                   2922:         ($excel_workbook,$excel_filename,$format) =
                   2923:             &Apache::loncommon::create_workbook($r);
                   2924:         return if (! defined($excel_workbook));
                   2925:         $excel_sheet = $excel_workbook->addworksheet('userlist');
1.2       raeburn  2926:         $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1       raeburn  2927:         #
                   2928:         my @colnames = map {$lt{$_}} (@cols);
1.67      droeschl 2929: 
1.1       raeburn  2930:         $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
                   2931:     }
                   2932: 
                   2933: # Done with header lines in all formats
                   2934:     my %index;
                   2935:     my $i;
1.2       raeburn  2936:     foreach my $idx (@$keylist) {
                   2937:         $index{$idx} = $i++;
                   2938:     }
1.184.4.10.2.  (raeburn 2939:):     my $now = time;
1.4       raeburn  2940:     my $usercount = 0;
1.33      raeburn  2941:     my ($secfilter,$grpfilter);
                   2942:     if ($context eq 'course') {
                   2943:         $secfilter = $env{'form.secfilter'};
                   2944:         $grpfilter = $env{'form.grpfilter'};
                   2945:         if ($secfilter eq '') {
                   2946:             $secfilter = 'all';
                   2947:         }
                   2948:         if ($grpfilter eq '') {
                   2949:             $grpfilter = 'all';
                   2950:         }
                   2951:     }
1.83      raeburn  2952:     my %ltstatus = &Apache::lonlocal::texthash(
                   2953:                                                 Active  => 'Active',
                   2954:                                                 Future  => 'Future',
                   2955:                                                 Expired => 'Expired',
                   2956:                                                );
1.184.4.10.2.  (raeburn 2957:):     my (%crslogins,%camanagers);
1.139     raeburn  2958:     if ($context eq 'course') {
1.184.4.10.2.  (raeburn 2959:):         # If this is for a single course get last course "log-in".
1.139     raeburn  2960:         %crslogins=&Apache::lonnet::dump('nohist_crslastlogin',$cdom,$cnum);
1.184.4.10.2.  (raeburn 2961:):     } elsif ($context eq 'author') {
                   2962:):         my $authormanagers;
                   2963:):         if ($env{'request.role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
                   2964:):             my %envhash = &Apache::lonnet::userenvironment($1,$2,'authormanagers');
                   2965:):             $authormanagers = $envhash{'authormanagers'};
                   2966:):         } else {
                   2967:):             $authormanagers = $env{'environment.authormanagers'};
                   2968:):         }
                   2969:):         if ($authormanagers ne '') {
                   2970:):             map { $camanagers{$_.':ca'} = 1; } split(/,/,$authormanagers);
                   2971:):         }
1.139     raeburn  2972:     }
1.2       raeburn  2973:     # Get groups, role, permanent e-mail so we can sort on them if
                   2974:     # necessary.
                   2975:     foreach my $user (keys(%{$userlist})) {
1.43      raeburn  2976:         if ($user eq '' ) {
                   2977:             delete($userlist->{$user});
                   2978:             next;
                   2979:         }
1.11      raeburn  2980:         if ($context eq 'domain' &&  $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
                   2981:             delete($userlist->{$user});
                   2982:             next;
                   2983:         }
1.2       raeburn  2984:         my ($uname,$udom,$role,$groups,$email);
1.5       raeburn  2985:         if (($statusmode ne 'Any') && 
                   2986:                  ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
                   2987:             delete($userlist->{$user});
                   2988:             next;
                   2989:         }
1.2       raeburn  2990:         if ($context eq 'domain') {
                   2991:             if ($env{'form.roletype'} eq 'domain') {
                   2992:                 ($role,$uname,$udom) = split(/:/,$user);
1.11      raeburn  2993:                 if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
                   2994:                     ($udom eq $env{'request.role.domain'})) {
                   2995:                     delete($userlist->{$user});
                   2996:                     next;
                   2997:                 }
1.13      raeburn  2998:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2999:                 ($uname,$udom,$role) = split(/:/,$user,-1);
1.102     raeburn  3000:             } elsif (($env{'form.roletype'} eq 'course') || 
                   3001:                      ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  3002:                 ($uname,$udom,$role) = split(/:/,$user);
                   3003:             }
                   3004:         } else {
                   3005:             ($uname,$udom,$role) = split(/:/,$user,-1);
                   3006:             if (($context eq 'course') && $role eq '') {
                   3007:                 $role = 'st';
                   3008:             }
                   3009:         }
                   3010:         $userlist->{$user}->[$index{'role'}] = $role;
                   3011:         if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'}  eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
                   3012:             delete($userlist->{$user});
                   3013:             next;
                   3014:         }
1.33      raeburn  3015:         if ($context eq 'course') {
                   3016:             my @ac_groups;
                   3017:             if (ref($classgroups) eq 'HASH') {
                   3018:                 $groups = $classgroups->{$user};
                   3019:             }
                   3020:             if (ref($groups->{'active'}) eq 'HASH') {
                   3021:                 @ac_groups = keys(%{$groups->{'active'}});
                   3022:                 $userlist->{$user}->[$index{'groups'}] = join(', ',@ac_groups);
                   3023:             }
                   3024:             if ($mode ne 'autoenroll') {
                   3025:                 my $section = $userlist->{$user}->[$index{'section'}];
1.43      raeburn  3026:                 if (($env{'request.course.sec'} ne '') && 
                   3027:                     ($section ne $env{'request.course.sec'})) {
                   3028:                     if ($role eq 'st') {
                   3029:                         delete($userlist->{$user});
                   3030:                         next;
                   3031:                     }
                   3032:                 }
1.33      raeburn  3033:                 if ($secfilter eq 'none') {
                   3034:                     if ($section ne '') {
                   3035:                         delete($userlist->{$user});
                   3036:                         next;
                   3037:                     }
                   3038:                 } elsif ($secfilter ne 'all') {
                   3039:                     if ($section ne $secfilter) {
                   3040:                         delete($userlist->{$user});
                   3041:                         next;
                   3042:                     }
                   3043:                 }
                   3044:                 if ($grpfilter eq 'none') {
                   3045:                     if (@ac_groups > 0) {
                   3046:                         delete($userlist->{$user});
                   3047:                         next;
                   3048:                     }
                   3049:                 } elsif ($grpfilter ne 'all') {
                   3050:                     if (!grep(/^\Q$grpfilter\E$/,@ac_groups)) {
                   3051:                         delete($userlist->{$user});
                   3052:                         next;
                   3053:                     }
                   3054:                 }
1.44      raeburn  3055:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.140     raeburn  3056:                     if ((grep/^photo$/,@cols) && ($role eq 'st')) {
1.44      raeburn  3057:                         $userlist->{$user}->[$index{'photo'}] =
1.47      raeburn  3058:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,'jpg');
                   3059:                         $userlist->{$user}->[$index{'thumbnail'}] =
1.44      raeburn  3060:                             &Apache::lonnet::retrievestudentphoto($udom,$uname,
                   3061:                                                                 'gif','thumbnail');
                   3062:                     }
                   3063:                 }
1.150     raeburn  3064:                 if (($role eq 'st') && ($defaultcredits)) {
                   3065:                     if ($userlist->{$user}->[$index{'credits'}] eq '') {
                   3066:                         $userlist->{$user}->[$index{'credits'}] = $defaultcredits;
                   3067:                     }
                   3068:                 }
1.33      raeburn  3069:             }
1.2       raeburn  3070:         }
1.184.4.10.2.  (raeburn 3071:):         if ($context eq 'author') {
                   3072:):             if (($camanagers{$user}) &&
                   3073:):                 ((!defined($userlist->{$user}->[$index{'end'}])) ||
                   3074:):                  ($userlist->{$user}->[$index{'end'}] == 0) ||
                   3075:):                  ($userlist->{$user}->[$index{'end'}] > $now))) {
                   3076:):                 $userlist->{$user}->[$index{'manager'}] = &mt('Yes');
                   3077:):             } else {
                   3078:):                 $userlist->{$user}->[$index{'manager'}] = &mt('No');
                   3079:):             }
                   3080:):         }
1.2       raeburn  3081:         my %emails   = &Apache::loncommon::getemails($uname,$udom);
                   3082:         if ($emails{'permanentemail'} =~ /\S/) {
                   3083:             $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
                   3084:         }
1.159     raeburn  3085:         if (($context eq 'domain') && ($env{'form.roletype'} eq 'domain') && 
                   3086:             ($role eq 'au')) {
                   3087:             my ($disk_quota,$current_disk_usage,$percent); 
                   3088:             if (($needauthorusage) || ($needauthorquota)) {
                   3089:                 $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,'author');
                   3090:             }
                   3091:             if ($needauthorusage) {
                   3092:                 $current_disk_usage =
                   3093:                     &Apache::lonnet::diskusage($udom,$uname,"$londocroot/priv/$udom/$uname");
                   3094:                 if ($disk_quota == 0) {
                   3095:                     $percent = 100.0;
                   3096:                 } else {
                   3097:                     $percent = $current_disk_usage/(10 * $disk_quota);
                   3098:                 }
                   3099:                 $userlist->{$user}->[$index{'authorusage'}] = sprintf("%.0f",$percent);
                   3100:             }
                   3101:             if ($needauthorquota) {
                   3102:                 $userlist->{$user}->[$index{'authorquota'}] = sprintf("%.2f",$disk_quota);
                   3103:             }
                   3104:         }
1.4       raeburn  3105:         $usercount ++;
                   3106:     }
                   3107:     my $autocount = 0;
                   3108:     my $manualcount = 0;
                   3109:     my $lockcount = 0;
                   3110:     my $unlockcount = 0;
                   3111:     if ($usercount) {
                   3112:         $r->print($output);
                   3113:     } else {
                   3114:         if ($mode eq 'autoenroll') {
                   3115:             return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
                   3116:         } else {
                   3117:             return;
                   3118:         }
1.1       raeburn  3119:     }
1.2       raeburn  3120:     #
                   3121:     # Sort the users
1.1       raeburn  3122:     my $index  = $index{$sortby};
                   3123:     my $second = $index{'username'};
                   3124:     my $third  = $index{'domain'};
1.159     raeburn  3125:     my @sorted_users;
                   3126:     if (($sortby eq 'authorquota') || ($sortby eq 'authorusage')) {  
                   3127:         @sorted_users = sort {
                   3128:             $userlist->{$b}->[$index] <=> $userlist->{$a}->[$index]           ||
                   3129:             lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second]) ||
                   3130:             lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
                   3131:             } (keys(%$userlist));
                   3132:     } else {
                   3133:         @sorted_users = sort {
                   3134:             lc($userlist->{$a}->[$index]) cmp lc($userlist->{$b}->[$index])   ||
                   3135:             lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second]) ||
                   3136:             lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
                   3137:             } (keys(%$userlist));
                   3138:     }
1.4       raeburn  3139:     my $rowcount = 0;
1.178     raeburn  3140:     my $disabled;
                   3141:     if ($mode eq 'autoenroll') {
                   3142:         unless ($permission->{'cusr'}) {
                   3143:             $disabled = ' disabled="disabled"';
                   3144:         }
                   3145:     }
1.2       raeburn  3146:     foreach my $user (@sorted_users) {
1.4       raeburn  3147:         my %in;
1.2       raeburn  3148:         my $sdata = $userlist->{$user};
1.4       raeburn  3149:         $rowcount ++; 
1.2       raeburn  3150:         foreach my $item (@{$keylist}) {
                   3151:             $in{$item} = $sdata->[$index{$item}];
                   3152:         }
1.67      droeschl 3153:         my $clickers = (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
                   3154:         if ($clickers!~/\w/) { $clickers='-'; }
1.159     raeburn  3155:         $in{'clicker'} = $clickers;
1.67      droeschl 3156: 	my $role = $in{'role'};
1.102     raeburn  3157:         $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}],$crstype);
1.136     raeburn  3158:         unless ($mode eq 'excel') {
                   3159:             if (! defined($in{'start'}) || $in{'start'} == 0) {
                   3160:                 $in{'start'} = &mt('none');
                   3161:             } else {
                   3162:                 $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
                   3163:             }
                   3164:             if (! defined($in{'end'}) || $in{'end'} == 0) {
                   3165:                 $in{'end'} = &mt('none');
                   3166:             } else {
                   3167:                 $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
                   3168:             }
1.1       raeburn  3169:         }
1.139     raeburn  3170:         if ($context eq 'course') {
                   3171:             my $lastlogin = $crslogins{$in{'username'}.':'.$in{'domain'}.':'.$in{'section'}.':'.$role};
                   3172:             if ($lastlogin ne '') {
                   3173:                 $in{'lastlogin'} = &Apache::lonlocal::locallocaltime($lastlogin);
                   3174:             }
                   3175:         }
1.55      raeburn  3176:         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  3177:             $r->print(&Apache::loncommon::start_data_table_row());
1.11      raeburn  3178:             my $checkval;
1.16      raeburn  3179:             if ($mode eq 'autoenroll') {
                   3180:                 my $cellentry;
                   3181:                 if ($in{'type'} eq 'auto') {
1.178     raeburn  3182:                     $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  3183:                     $autocount ++;
                   3184:                 } else {
1.178     raeburn  3185:                     $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  3186:                     $manualcount ++;
                   3187:                     if ($in{'lockedtype'}) {
1.178     raeburn  3188:                         $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'"'.$disabled.' />&nbsp;'.&mt('Unlock').'</label>';
1.16      raeburn  3189:                         $unlockcount ++;
                   3190:                     } else {
1.178     raeburn  3191:                         $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'"'.$disabled.' />&nbsp;'.&mt('Lock').'</label>';
1.16      raeburn  3192:                         $lockcount ++;
1.11      raeburn  3193:                     }
1.76      bisitz   3194:                     $cellentry .= '</span></td></tr></table>';
1.16      raeburn  3195:                 }
                   3196:                 $r->print("<td>$cellentry</td>\n");
                   3197:             } else {
1.55      raeburn  3198:                 if ($mode ne 'pickauthor') {  
                   3199:                     $r->print("<td>$rowcount</td>\n");
                   3200:                 }
1.16      raeburn  3201:                 if ($actionselect) {
1.26      raeburn  3202:                     my $showcheckbox;
                   3203:                     if ($role =~ /^cr\//) {
                   3204:                         $showcheckbox = $canchange{'cr'};
                   3205:                     } else {
                   3206:                         $showcheckbox = $canchange{$role};
                   3207:                     }
                   3208:                     if (!$showcheckbox) {
                   3209:                         if ($context eq 'course') {
                   3210:                             if ($canchangesec{$role} ne '') {
                   3211:                                 if ($canchangesec{$role} eq $in{'section'}) {
                   3212:                                     $showcheckbox = 1;
                   3213:                                 }
                   3214:                             }
1.16      raeburn  3215:                         }
1.26      raeburn  3216:                     }
                   3217:                     if ($showcheckbox) {
                   3218:                         $checkval = $user; 
                   3219:                         if ($context eq 'course') {
1.141     raeburn  3220:                             if (($role eq 'co' || $role eq 'cc') &&
                   3221:                                 ($user =~ /^\Q$env{'user.name'}:$env{'user.domain'}:$role\E/)) {
                   3222:                                 $showcheckbox = 0;
                   3223:                             } else {
                   3224:                                 if ($role eq 'st') {
                   3225:                                     $checkval .= ':st';
                   3226:                                 }
                   3227:                                 $checkval .= ':'.$in{'section'};
                   3228:                                 if ($role eq 'st') {
                   3229:                                     $checkval .= ':'.$in{'type'}.':'.
1.150     raeburn  3230:                                                  $in{'lockedtype'}.':'.
1.174     raeburn  3231:                                                  $in{'credits'}.':'.
                   3232:                                                  &escape($in{'instsec'});
1.141     raeburn  3233:                                 }
                   3234:                              }
                   3235:                         }
                   3236:                         if ($showcheckbox) {
                   3237:                             $r->print('<td><input type="checkbox" name="'.
1.183     raeburn  3238:                                       'actionlist" value="'.
                   3239:                                       &HTML::Entities::encode($checkval,'&<>"').'" />');
                   3240:                             foreach my $item ('start','end') {
                   3241:                                 $r->print('<input type="hidden" name="'.
                   3242:                                           &HTML::Entities::encode($checkval.'_'.$item,'&<>"').'"'.
                   3243:                                           ' value="'.$sdata->[$index{$item}].'" />');
                   3244:                             }
                   3245:                             $r->print('</td>');
1.141     raeburn  3246:                         } else {
                   3247:                             $r->print('<td>&nbsp;</td>');
1.16      raeburn  3248:                         }
1.26      raeburn  3249:                     } else {
                   3250:                         $r->print('<td>&nbsp;</td>');
1.16      raeburn  3251:                     }
1.55      raeburn  3252:                 } elsif ($mode eq 'pickauthor') {
                   3253:                         $r->print('<td><input type="button" name="chooseauthor" onclick="javascript:gochoose('."'$in{'username'}'".');" value="'.&mt('Select').'" /></td>');
1.11      raeburn  3254:                 }
                   3255:             }
1.2       raeburn  3256:             foreach my $item (@cols) {
1.10      raeburn  3257:                 if ($item eq 'username') {
1.48      raeburn  3258:                     $r->print('<td>'.&print_username_link($mode,\%in).'</td>');
1.83      raeburn  3259:                 } elsif ($item eq 'status') {
                   3260:                     my $showitem = $in{$item};
                   3261:                     if (defined($ltstatus{$in{$item}})) {
                   3262:                         $showitem = $ltstatus{$in{$item}};
                   3263:                     }
                   3264:                     $r->print('<td>'.$showitem.'</td>'."\n");
1.140     raeburn  3265:                 } elsif ($item eq 'photo') {
                   3266:                      if (($context eq 'course') && ($mode ne 'autoenroll') && 
                   3267:                          ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'})) { 
                   3268:                          if ($role eq 'st') {
                   3269:                              $r->print('<td align="right"><a href="javascript:photowindow('."'".$in{'photo'}."'".')"><img src="'.$in{'thumbnail'}.'" border="1" alt="" /></a></td>');
                   3270:                          } else {
                   3271:                              $r->print('<td>&nbsp;</td>');
                   3272:                          }
                   3273:                      }
                   3274:                 } elsif ($item eq 'clicker') {
                   3275:                     if (($context eq 'course') && ($mode ne 'autoenroll')) {
                   3276:                         if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   3277:                             my $clickers =
                   3278:                    (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
                   3279:                             if ($clickers!~/\w/) { $clickers='-'; }
                   3280:                             $r->print('<td>'.$clickers.'</td>');
                   3281:                         } else {
                   3282:                              $r->print('<td>&nbsp;</td>'."\n");
                   3283:                         } 
1.149     raeburn  3284:                     }
1.159     raeburn  3285:                 } elsif (($item eq 'authorquota') || ($item eq 'authorusage')) {
                   3286:                     $r->print('<td align="right">'.$in{$item}.'</td>'."\n");
1.10      raeburn  3287:                 } else {
                   3288:                     $r->print('<td>'.$in{$item}.'</td>'."\n");
                   3289:                 }
1.2       raeburn  3290:             }
                   3291:             $r->print(&Apache::loncommon::end_data_table_row());
                   3292:         } elsif ($mode eq 'csv') {
                   3293:             next if (! defined($CSVfile));
                   3294:             # no need to bother with $linkto
                   3295:             my @line = ();
                   3296:             foreach my $item (@cols) {
                   3297:                 push @line,&Apache::loncommon::csv_translate($in{$item});
                   3298:             }
1.67      droeschl 3299:             print $CSVfile '"'.join('","',@line)."\"\n";
1.2       raeburn  3300:         } elsif ($mode eq 'excel') {
                   3301:             my $col = 0;
                   3302:             foreach my $item (@cols) {
                   3303:                 if ($item eq 'start' || $item eq 'end') {
1.136     raeburn  3304:                     if ((defined($in{$item})) && ($in{$item} != 0)) {
1.2       raeburn  3305:                         $excel_sheet->write($row,$col++,
1.136     raeburn  3306:                             &Apache::lonstathelpers::calc_serial($in{$item}),
1.2       raeburn  3307:                                     $format->{'date'});
                   3308:                     } else {
                   3309:                         $excel_sheet->write($row,$col++,'none');
                   3310:                     }
                   3311:                 } else {
                   3312:                     $excel_sheet->write($row,$col++,$in{$item});
                   3313:                 }
                   3314:             }
                   3315:             $row++;
1.1       raeburn  3316:         }
                   3317:     }
1.55      raeburn  3318:     if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll' || $mode eq 'pickauthor') {
1.2       raeburn  3319:             $r->print(&Apache::loncommon::end_data_table().'<br />');
                   3320:     } elsif ($mode eq 'excel') {
                   3321:         $excel_workbook->close();
1.162     bisitz   3322: 	$r->print('<p>'.&mt('[_1]Your Excel spreadsheet[_2] is ready for download.', '<a href="'.$excel_filename.'">','</a>')."</p>\n");
1.2       raeburn  3323:     } elsif ($mode eq 'csv') {
                   3324:         close($CSVfile);
1.162     bisitz   3325: 	$r->print('<p>'.&mt('[_1]Your CSV file[_2] is ready for download.', '<a href="'.$CSVfilename.'">','</a>')."</p>\n");
1.2       raeburn  3326:         $r->rflush();
                   3327:     }
                   3328:     if ($mode eq 'autoenroll') {
                   3329:         return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
1.4       raeburn  3330:     } else {
                   3331:         return ($usercount);
1.2       raeburn  3332:     }
1.1       raeburn  3333: }
                   3334: 
1.56      raeburn  3335: sub bulkaction_javascript {
                   3336:     my ($formname,$caller) = @_;
                   3337:     my $docstart = 'document';
                   3338:     if ($caller eq 'popup') {
                   3339:         $docstart = 'opener.document';
                   3340:     }
                   3341:     my %lt = &Apache::lonlocal::texthash(
                   3342:               acwi => 'Access will be set to start immediately',
                   3343:               asyo => 'as you did not select an end date in the pop-up window',
                   3344:               accw => 'Access will be set to continue indefinitely',
                   3345:               asyd => 'as you did not select an end date in the pop-up window',
                   3346:               sewi => "Sections will be switched to 'No section'",
                   3347:               ayes => "as you either selected the 'No section' option",
                   3348:               oryo => 'or you did not select a section in the pop-up window',
                   3349:               arol => 'A role with no section will be added',
                   3350:               swbs => 'Sections will be switched to:',
                   3351:               rwba => 'Roles will be added for section(s):',
                   3352:             );
                   3353:     my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
                   3354:     my $noaction = &mt("You need to select an action to take for the user(s) you have selected"); 
                   3355:     my $singconfirm = &mt(' for a single user?');
                   3356:     my $multconfirm = &mt(' for multiple users?');
1.170     damieng  3357:     &js_escape(\$alert);
                   3358:     &js_escape(\$noaction);
                   3359:     &js_escape(\$singconfirm);
                   3360:     &js_escape(\$multconfirm);
1.56      raeburn  3361:     my $output = <<"ENDJS";
                   3362: function verify_action (field) {
                   3363:     var numchecked = 0;
                   3364:     var singconf = '$singconfirm';
                   3365:     var multconf = '$multconfirm';
                   3366:     if ($docstart.$formname.elements[field].length > 0) {
                   3367:         for (i=0; i<$docstart.$formname.elements[field].length; i++) {
                   3368:             if ($docstart.$formname.elements[field][i].checked == true) {
                   3369:                numchecked ++;
                   3370:             }
                   3371:         }
                   3372:     } else {
                   3373:         if ($docstart.$formname.elements[field].checked == true) {
                   3374:             numchecked ++;
                   3375:         }
                   3376:     }
                   3377:     if (numchecked == 0) {
                   3378:         alert("$alert");
                   3379:         return;
                   3380:     } else {
                   3381:         var message = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].text;
                   3382:         var choice = $docstart.$formname.bulkaction[$docstart.$formname.bulkaction.selectedIndex].value;
                   3383:         if (choice == '') {
                   3384:             alert("$noaction");
                   3385:             return;
                   3386:         } else {
                   3387:             if (numchecked == 1) {
                   3388:                 message += singconf;
                   3389:             } else {
                   3390:                 message += multconf;
                   3391:             }
                   3392: ENDJS
                   3393:     if ($caller ne 'popup') {
                   3394:         $output .= <<"NEWWIN";
                   3395:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate' || choice == 'chgsec') {
                   3396:                 opendatebrowser(document.$formname,'$formname','go');
                   3397:                 return;
                   3398: 
                   3399:             } else {
                   3400:                 if (confirm(message)) {
                   3401:                     document.$formname.phase.value = 'bulkchange';
                   3402:                     document.$formname.submit();
                   3403:                     return;
                   3404:                 }
                   3405:             }
                   3406: NEWWIN
                   3407:     } else {
                   3408:         $output .= <<"POPUP";
                   3409:             if (choice == 'chgdates' || choice == 'reenable' || choice == 'activate') {
                   3410:                 var datemsg = '';
                   3411:                 if (($docstart.$formname.startdate_month.value == '') &&
                   3412:                     ($docstart.$formname.startdate_day.value  == '') &&
                   3413:                     ($docstart.$formname.startdate_year.value == '')) {
                   3414:                     datemsg = "\\n$lt{'acwi'},\\n$lt{'asyo'}.\\n";
                   3415:                 }
                   3416:                 if (($docstart.$formname.enddate_month.value == '') &&
                   3417:                     ($docstart.$formname.enddate_day.value  == '') &&
                   3418:                     ($docstart.$formname.enddate_year.value == '')) {
                   3419:                     datemsg += "\\n$lt{'accw'},\\n$lt{'asyd'}.\\n";
                   3420:                 }
                   3421:                 if (datemsg != '') {
                   3422:                     message += "\\n"+datemsg;
                   3423:                 }
                   3424:             }
                   3425:             if (choice == 'chgsec') {
                   3426:                 var rolefilter = $docstart.$formname.showrole.options[$docstart.$formname.showrole.selectedIndex].value;
                   3427:                 var retained =  $docstart.$formname.retainsec.value;
                   3428:                 var secshow = $docstart.$formname.newsecs.value;
                   3429:                 if (secshow == '') {
                   3430:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   3431:                         message += "\\n\\n$lt{'sewi'},\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   3432:                     } else {
                   3433:                         message += "\\n\\n$lt{'arol'}\\n$lt{'ayes'},\\n$lt{'oryo'}.\\n";
                   3434:                     }
                   3435:                 } else {
                   3436:                     if (rolefilter == 'st' || retained == 0 || retained == "") {
                   3437:                         message += "\\n\\n$lt{'swbs'} "+secshow+".\\n";
                   3438:                     } else {
                   3439:                         message += "\\n\\n$lt{'rwba'} "+secshow+".\\n";
                   3440:                     }
                   3441:                 }
                   3442:             }
                   3443:             if (confirm(message)) {
                   3444:                 $docstart.$formname.phase.value = 'bulkchange';
                   3445:                 $docstart.$formname.submit();
                   3446:                 window.close();
                   3447:             }
                   3448: POPUP
                   3449:     }
                   3450:     $output .= '
                   3451:         }
                   3452:     }
                   3453: }
                   3454: ';
                   3455:     return $output;
                   3456: }
                   3457: 
1.10      raeburn  3458: sub print_username_link {
1.48      raeburn  3459:     my ($mode,$in) = @_;
1.10      raeburn  3460:     my $output;
1.16      raeburn  3461:     if ($mode eq 'autoenroll') {
                   3462:         $output = $in->{'username'};
1.10      raeburn  3463:     } else {
                   3464:         $output = '<a href="javascript:username_display_launch('.
1.112     bisitz   3465:                   "'$in->{'username'}','$in->{'domain'}'".')">'.
1.10      raeburn  3466:                   $in->{'username'}.'</a>';
                   3467:     }
                   3468:     return $output;
                   3469: }
                   3470: 
1.2       raeburn  3471: sub role_type_names {
                   3472:     my %lt = &Apache::lonlocal::texthash (
1.13      raeburn  3473:                          'domain' => 'Domain Roles',
                   3474:                          'author' => 'Co-Author Roles',
                   3475:                          'course' => 'Course Roles',
1.101     raeburn  3476:                          'community' => 'Community Roles',
1.2       raeburn  3477:              );
                   3478:     return %lt;
                   3479: }
                   3480: 
1.11      raeburn  3481: sub select_actions {
1.55      raeburn  3482:     my ($context,$setting,$statusmode,$formname) = @_;
1.11      raeburn  3483:     my %lt = &Apache::lonlocal::texthash(
                   3484:                 revoke   => "Revoke user roles",
                   3485:                 delete   => "Delete user roles",
                   3486:                 reenable => "Re-enable expired user roles",
                   3487:                 activate => "Make future user roles active now",
                   3488:                 chgdates  => "Change starting/ending dates",
                   3489:                 chgsec   => "Change section associated with user roles",
                   3490:     );
1.150     raeburn  3491:     # FIXME Add an option to change credits for student roles.
1.11      raeburn  3492:     my ($output,$options,%choices);
1.23      raeburn  3493:     # FIXME Disable actions for now for roletype=course in domain context
                   3494:     if ($context eq 'domain' && $setting eq 'course') {
                   3495:         return;
                   3496:     }
1.26      raeburn  3497:     if ($context eq 'course') {
                   3498:         if ($env{'form.showrole'} ne 'Any') {
1.141     raeburn  3499:             my $showactions;
                   3500:             if (&Apache::lonnet::allowed('c'.$env{'form.showrole'},
                   3501:                                           $env{'request.course.id'})) {
                   3502:                 $showactions = 1;  
                   3503:             } elsif ($env{'request.course.sec'} ne '') {
                   3504:                 if (&Apache::lonnet::allowed('c'.$env{'form.showrole'},$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   3505:                     $showactions = 1;
                   3506:                 }
                   3507:             }
                   3508:             unless ($showactions) {
                   3509:                 unless (&is_courseowner($env{'request.course.id'},
                   3510:                                        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'})) {
                   3511:                     return; 
                   3512:                 }
1.26      raeburn  3513:             }
                   3514:         }
                   3515:     }
1.11      raeburn  3516:     if ($statusmode eq 'Any') {
                   3517:         $options .= '
                   3518: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
                   3519:         $choices{'dates'} = 1;
                   3520:     } else {
                   3521:         if ($statusmode eq 'Future') {
                   3522:             $options .= '
                   3523: <option value="activate">'.$lt{'activate'}.'</option>';
                   3524:             $choices{'dates'} = 1;
                   3525:         } elsif ($statusmode eq 'Expired') {
                   3526:             $options .= '
                   3527: <option value="reenable">'.$lt{'reenable'}.'</option>';
                   3528:             $choices{'dates'} = 1;
                   3529:         }
1.13      raeburn  3530:         if ($statusmode eq 'Active' || $statusmode eq 'Future') {
                   3531:             $options .= '
                   3532: <option value="chgdates">'.$lt{'chgdates'}.'</option>
                   3533: <option value="revoke">'.$lt{'revoke'}.'</option>';
                   3534:             $choices{'dates'} = 1;
                   3535:         }
1.11      raeburn  3536:     }
                   3537:     if ($context eq 'domain') {
                   3538:         $options .= '
                   3539: <option value="delete">'.$lt{'delete'}.'</option>';
                   3540:     }
                   3541:     if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
1.26      raeburn  3542:         if (($statusmode ne 'Expired') && ($env{'request.course.sec'} eq '')) {
1.11      raeburn  3543:             $options .= '
                   3544: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
                   3545:             $choices{'sections'} = 1;
                   3546:         }
                   3547:     }
                   3548:     if ($options) {
1.56      raeburn  3549:         $output = '<select name="bulkaction">'."\n".
1.11      raeburn  3550:                   '<option value="" selected="selected">'.
                   3551:                   &mt('Please select').'</option>'."\n".$options."\n".'</select>';
                   3552:         if ($choices{'dates'}) {
                   3553:             $output .= 
                   3554:                 '<input type="hidden" name="startdate_month" value="" />'."\n".
                   3555:                 '<input type="hidden" name="startdate_day" value="" />'."\n".
                   3556:                 '<input type="hidden" name="startdate_year" value="" />'."\n".
                   3557:                 '<input type="hidden" name="startdate_hour" value="" />'."\n".
                   3558:                 '<input type="hidden" name="startdate_minute" value="" />'."\n".
                   3559:                 '<input type="hidden" name="startdate_second" value="" />'."\n".
                   3560:                 '<input type="hidden" name="enddate_month" value="" />'."\n".
                   3561:                 '<input type="hidden" name="enddate_day" value="" />'."\n".
                   3562:                 '<input type="hidden" name="enddate_year" value="" />'."\n".
                   3563:                 '<input type="hidden" name="enddate_hour" value="" />'."\n".
                   3564:                 '<input type="hidden" name="enddate_minute" value="" />'."\n".
1.56      raeburn  3565:                 '<input type="hidden" name="enddate_second" value="" />'."\n".
                   3566:                 '<input type="hidden" name="no_end_date" value="" />'."\n";
1.11      raeburn  3567:             if ($context eq 'course') {
                   3568:                 $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
                   3569:             }
                   3570:         }
                   3571:         if ($choices{'sections'}) {
1.91      bisitz   3572:             $output .= '<input type="hidden" name="retainsec" value="" />'."\n".
                   3573:                        '<input type="hidden" name="newsecs" value="" />'."\n";
1.11      raeburn  3574:         }
                   3575:     }
                   3576:     return $output;
                   3577: }
                   3578: 
                   3579: sub date_section_javascript {
                   3580:     my ($context,$setting) = @_;
1.49      raeburn  3581:     my $title = 'Date_And_Section_Selector';
1.41      raeburn  3582:     my %nopopup = &Apache::lonlocal::texthash (
                   3583:         revoke => "Check the boxes for any users for whom roles are to be revoked, and click 'Proceed'",
                   3584:         delete => "Check the boxes for any users for whom roles are to be deleted, and click 'Proceed'",
                   3585:         none   => "Choose an action to take for selected users",
                   3586:     );  
1.96      bisitz   3587:     my $output = <<"ENDONE";
                   3588: <script type="text/javascript">
                   3589: // <![CDATA[
1.41      raeburn  3590:     function opendatebrowser(callingform,formname,calledby) {
1.11      raeburn  3591:         var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
                   3592:         var url = '/adm/createuser?';
                   3593:         var type = '';
                   3594:         var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
                   3595: ENDONE
                   3596:     if ($context eq 'domain') {
                   3597:         $output .= '
                   3598:         type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
                   3599: ';
                   3600:     }
                   3601:     my $width= '700';
                   3602:     my $height = '400';
                   3603:     $output .= <<"ENDTWO";
                   3604:         url += 'action=dateselect&callingform=' + formname + 
                   3605:                '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
                   3606:         var title = '$title';
                   3607:         var options = 'scrollbars=1,resizable=1,menubar=0';
                   3608:         options += ',width=$width,height=$height';
                   3609:         stdeditbrowser = open(url,title,options,'1');
                   3610:         stdeditbrowser.focus();
                   3611:     }
1.96      bisitz   3612: // ]]>
1.11      raeburn  3613: </script>
                   3614: ENDTWO
                   3615:     return $output;
                   3616: }
                   3617: 
                   3618: sub date_section_selector {
1.150     raeburn  3619:     my ($context,$permission,$crstype,$showcredits) = @_;
1.11      raeburn  3620:     my $callingform = $env{'form.callingform'};
                   3621:     my $formname = 'dateselect';  
                   3622:     my $groupslist = &get_groupslist();
1.150     raeburn  3623:     my $sec_js =
                   3624:         &setsections_javascript($formname,$groupslist,undef,undef,$crstype,
                   3625:                                 $showcredits);
1.11      raeburn  3626:     my $output = <<"END";
                   3627: <script type="text/javascript">
1.96      bisitz   3628: // <![CDATA[
1.11      raeburn  3629: 
                   3630: $sec_js
                   3631: 
                   3632: function saveselections(formname) {
                   3633: 
                   3634: END
                   3635:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3636:         $output .= <<"END";
1.40      raeburn  3637:         if (formname.retainsec.length > 1) {  
                   3638:             for (var i=0; i<formname.retainsec.length; i++) {
                   3639:                 if (formname.retainsec[i].checked == true) {
                   3640:                     opener.document.$callingform.retainsec.value = formname.retainsec[i].value;
                   3641:                 }
                   3642:             }
                   3643:         } else {
                   3644:             opener.document.$callingform.retainsec.value = formname.retainsec.value;
                   3645:         }
1.103     raeburn  3646:         setSections(formname,'$crstype');
1.11      raeburn  3647:         if (seccheck == 'ok') {
                   3648:             opener.document.$callingform.newsecs.value = formname.sections.value;
1.184.4.8  raeburn  3649:         } else {
                   3650:             return;
1.11      raeburn  3651:         }
                   3652: END
                   3653:     } else {
                   3654:         if ($context eq 'course') {
                   3655:             if (($env{'form.bulkaction'} eq 'reenable') || 
                   3656:                 ($env{'form.bulkaction'} eq 'activate') || 
                   3657:                 ($env{'form.bulkaction'} eq 'chgdates')) {
1.26      raeburn  3658:                 if ($env{'request.course.sec'} eq '') {
                   3659:                     $output .= <<"END";
1.11      raeburn  3660:  
                   3661:         if (formname.makedatesdefault.checked == true) {
                   3662:             opener.document.$callingform.makedatesdefault.value = 1;
                   3663:         }
                   3664:         else {
                   3665:             opener.document.$callingform.makedatesdefault.value = 0;
                   3666:         }
                   3667: 
                   3668: END
1.26      raeburn  3669:                 }
1.11      raeburn  3670:             }
                   3671:         }
                   3672:         $output .= <<"END";
                   3673:     opener.document.$callingform.startdate_month.value =  formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
                   3674:     opener.document.$callingform.startdate_day.value =  formname.startdate_day.value;
                   3675:     opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
                   3676:     opener.document.$callingform.startdate_hour.value =  formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
                   3677:     opener.document.$callingform.startdate_minute.value =  formname.startdate_minute.value;
                   3678:     opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
                   3679:     opener.document.$callingform.enddate_month.value =  formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
                   3680:     opener.document.$callingform.enddate_day.value =  formname.enddate_day.value;
                   3681:     opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
                   3682:     opener.document.$callingform.enddate_hour.value =  formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
                   3683:     opener.document.$callingform.enddate_minute.value =  formname.enddate_minute.value;
                   3684:     opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
1.56      raeburn  3685:     if (formname.no_end_date.checked) {
                   3686:         opener.document.$callingform.no_end_date.value = '1';
                   3687:     } else {
                   3688:         opener.document.$callingform.no_end_date.value = '0';
                   3689:     }
1.11      raeburn  3690: END
                   3691:     }
1.56      raeburn  3692:     my $verify_action_js = &bulkaction_javascript($callingform,'popup');
                   3693:     $output .= <<"ENDJS";
                   3694:     verify_action('actionlist');
1.11      raeburn  3695: }
1.56      raeburn  3696: 
                   3697: $verify_action_js
                   3698: 
1.96      bisitz   3699: // ]]>
1.11      raeburn  3700: </script>
1.56      raeburn  3701: ENDJS
1.11      raeburn  3702:     my %lt = &Apache::lonlocal::texthash (
                   3703:                  chac => 'Access dates to apply for selected users',
                   3704:                  chse => 'Changes in section affiliation to apply to selected users',
1.118     raeburn  3705:                  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.',
                   3706:                  forn => 'For a course role that is not "student", users may have roles in more than one section at a time.',
                   3707:                  reta => "Retain each user's current section affiliations?",
1.103     raeburn  3708:                  dnap => '(Does not apply to student roles).',
1.11      raeburn  3709:             );
                   3710:     my ($date_items,$headertext);
                   3711:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   3712:         $headertext = $lt{'chse'};
                   3713:     } else {
                   3714:         $headertext = $lt{'chac'};
                   3715:         my $starttime;
                   3716:         if (($env{'form.bulkaction'} eq 'activate') || 
                   3717:             ($env{'form.bulkaction'} eq 'reenable')) {
                   3718:             $starttime = time;
                   3719:         }
                   3720:         $date_items = &date_setting_table($starttime,undef,$context,
1.21      raeburn  3721:                                           $env{'form.bulkaction'},$formname,
1.101     raeburn  3722:                                           $permission,$crstype);
1.11      raeburn  3723:     }
                   3724:     $output .= '<h3>'.$headertext.'</h3>'.
1.118     raeburn  3725:                '<form name="'.$formname.'" method="post" action="">'."\n".
1.11      raeburn  3726:                 $date_items;
                   3727:     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
1.17      raeburn  3728:         my ($cnum,$cdom) = &get_course_identity();
1.103     raeburn  3729:         if ($crstype eq 'Community') {
1.118     raeburn  3730:             $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.');
                   3731:             $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  3732:             $lt{'dnap'} = &mt('(Does not apply to member roles).'); 
                   3733:         }
1.11      raeburn  3734:         my $info;
                   3735:         if ($env{'form.showrole'} eq 'st') {
                   3736:             $output .= '<p>'.$lt{'fors'}.'</p>'; 
1.26      raeburn  3737:         } elsif ($env{'form.showrole'} eq 'Any') {
1.11      raeburn  3738:             $output .= '<p>'.$lt{'fors'}.'</p>'.
                   3739:                        '<p>'.$lt{'forn'}.'&nbsp;';
                   3740:             $info = $lt{'reta'};
                   3741:         } else {
                   3742:             $output .= '<p>'.$lt{'forn'}.'&nbsp;';
                   3743:             $info = $lt{'reta'};
                   3744:         }
                   3745:         if ($info) {
                   3746:             $info .= '<span class="LC_nobreak">'.
                   3747:                      '<label><input type="radio" name="retainsec" value="1" '.
                   3748:                      'checked="checked" />'.&mt('Yes').'</label>&nbsp;&nbsp;'.
                   3749:                      '<label><input type="radio" name="retainsec" value="0" />'.
                   3750:                      &mt('No').'</label></span>';
                   3751:             if ($env{'form.showrole'} eq 'Any') {
                   3752:                 $info .= '<br />'.$lt{'dnap'};
                   3753:             }
                   3754:             $info .= '</p>';
                   3755:         } else {
                   3756:             $info = '<input type="hidden" name="retainsec" value="0" />'; 
                   3757:         }
1.21      raeburn  3758:         my $rowtitle = &mt('New section to assign');
1.150     raeburn  3759:         my $secbox = &section_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,
                   3760:                                      $permission,$context,'chgsec',$crstype);
1.11      raeburn  3761:         $output .= $info.$secbox;
                   3762:     }
                   3763:     $output .= '<p>'.
1.81      schafran 3764: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
1.11      raeburn  3765: '</form>';
                   3766:     return $output;
                   3767: }
                   3768: 
1.17      raeburn  3769: sub section_picker {
1.150     raeburn  3770:     my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode,$crstype,
                   3771:         $showcredits,$credits) = @_;
1.17      raeburn  3772:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   3773:     my $sections_select .= &course_sections(\%sections_count,$role);
1.118     raeburn  3774:     my $secbox = '<div>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
1.17      raeburn  3775:     if ($mode eq 'upload') {
                   3776:         my ($options,$cb_script,$coursepick) =
1.150     raeburn  3777:             &default_role_selector($context,1,$crstype,$showcredits);
1.59      bisitz   3778:         $secbox .= &Apache::lonhtmlcommon::row_title(&mt('role'),'LC_oddrow_value').
1.17      raeburn  3779:                    $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3780:     }
                   3781:     $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
                   3782:     if ($env{'request.course.sec'} eq '') {
                   3783:         $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
                   3784:                    '<td align="center">'.&mt('Existing sections')."\n".
                   3785:                    '<br />'.$sections_select.'</td><td align="center">'.
                   3786:                    &mt('New section').'<br />'."\n".
1.118     raeburn  3787:                    '<input type="text" name="newsec" size="15" value="" />'."\n".
1.17      raeburn  3788:                    '<input type="hidden" name="sections" value="" />'."\n".
                   3789:                    '</td></tr></table>'."\n";
                   3790:     } else {
1.149     raeburn  3791:         $secbox .= '<input type="hidden" name="sections" value="'.
1.17      raeburn  3792:                    $env{'request.course.sec'}.'" />'.
                   3793:                    $env{'request.course.sec'};
                   3794:     }
1.150     raeburn  3795:     $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3796:     unless ($mode eq 'chgsec') {
                   3797:         if ($showcredits) {
                   3798:             $secbox .= 
                   3799:                 &Apache::lonhtmlcommon::row_title(&mt('credits (students)'),
                   3800:                                                   'LC_evenrow_value')."\n".
                   3801:                 '<input type="text" name="credits" size="3" value="'.$credits.'" />'."\n".
                   3802:                 &Apache::lonhtmlcommon::row_closure(1)."\n";
                   3803:         }
                   3804:     }
                   3805:     $secbox .= &Apache::lonhtmlcommon::end_pick_box().'</div>';
1.17      raeburn  3806:     return $secbox;
                   3807: }
                   3808: 
1.2       raeburn  3809: sub results_header_row {
1.102     raeburn  3810:     my ($rolefilter,$statusmode,$context,$permission,$mode,$crstype) = @_;
1.5       raeburn  3811:     my ($description,$showfilter);
                   3812:     if ($rolefilter ne 'Any') {
                   3813:         $showfilter = $rolefilter;
                   3814:     }
1.2       raeburn  3815:     if ($context eq 'course') {
1.24      raeburn  3816:         if ($mode eq 'csv' || $mode eq 'excel') {
1.102     raeburn  3817:             if ($crstype eq 'Community') {
                   3818:                 $description = &mt('Community - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3819:             } else {
                   3820:                 $description = &mt('Course - [_1]:',$env{'course.'.$env{'request.course.id'}.'.description'}).' ';
                   3821:             }
1.24      raeburn  3822:         }
1.2       raeburn  3823:         if ($statusmode eq 'Expired') {
1.102     raeburn  3824:             if ($crstype eq 'Community') {
                   3825:                 $description .= &mt('Users in community with expired [_1] roles',$showfilter);
                   3826:             } else {
                   3827:                 $description .= &mt('Users in course with expired [_1] roles',$showfilter);
                   3828:             }
1.11      raeburn  3829:         } elsif ($statusmode eq 'Future') {
1.102     raeburn  3830:             if ($crstype eq 'Community') {
                   3831:                 $description .= &mt('Users in community with future [_1] roles',$showfilter);
                   3832:             } else {
                   3833:                 $description .= &mt('Users in course with future [_1] roles',$showfilter);
                   3834:             }
1.2       raeburn  3835:         } elsif ($statusmode eq 'Active') {
1.102     raeburn  3836:             if ($crstype eq 'Community') {
                   3837:                 $description .= &mt('Users in community with active [_1] roles',$showfilter);
                   3838:             } else {
                   3839:                 $description .= &mt('Users in course with active [_1] roles',$showfilter);
                   3840:             }
1.2       raeburn  3841:         } else {
                   3842:             if ($rolefilter eq 'Any') {
1.102     raeburn  3843:                 if ($crstype eq 'Community') {
                   3844:                     $description .= &mt('All users in community');
                   3845:                 } else {
                   3846:                     $description .= &mt('All users in course');
                   3847:                 }
1.2       raeburn  3848:             } else {
1.102     raeburn  3849:                 if ($crstype eq 'Community') {
                   3850:                     $description .= &mt('All users in community with [_1] roles',$rolefilter);
                   3851:                 } else {
                   3852:                     $description .= &mt('All users in course with [_1] roles',$rolefilter);
                   3853:                 }
1.2       raeburn  3854:             }
                   3855:         }
1.33      raeburn  3856:         my $constraint;
1.26      raeburn  3857:         my $viewablesec = &viewable_section($permission);
                   3858:         if ($viewablesec ne '') {
1.15      raeburn  3859:             if ($env{'form.showrole'} eq 'st') {
1.33      raeburn  3860:                 $constraint = &mt('only users in section "[_1]"',$viewablesec);
1.103     raeburn  3861:             } elsif (($env{'form.showrole'} ne 'cc') && ($env{'form.showrole'} ne 'co')) {
1.33      raeburn  3862:                 $constraint = &mt('only users affiliated with no section or section "[_1]"',$viewablesec);
                   3863:             }
                   3864:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3865:                 if ($env{'form.grpfilter'} eq 'none') {
                   3866:                     $constraint .= &mt(' and not in any group');
                   3867:                 } else {
                   3868:                     $constraint .= &mt(' and members of group: "[_1]"',$env{'form.grpfilter'});
                   3869:                 }
                   3870:             }
                   3871:         } else {
                   3872:             if (($env{'form.secfilter'} ne 'all') && ($env{'form.secfilter'} ne '')) {
                   3873:                 if ($env{'form.secfilter'} eq 'none') {
                   3874:                     $constraint = &mt('only users affiliated with no section');
                   3875:                 } else {
                   3876:                     $constraint = &mt('only users affiliated with section "[_1]"',$env{'form.secfilter'});
                   3877:                 }
                   3878:             }
                   3879:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   3880:                 if ($env{'form.grpfilter'} eq 'none') {
                   3881:                     if ($constraint eq '') {
                   3882:                         $constraint = &mt('only users not in any group');
                   3883:                     } else {
                   3884:                         $constraint .= &mt(' and also not in any group'); 
                   3885:                     }
                   3886:                 } else {
                   3887:                     if ($constraint eq '') {
                   3888:                         $constraint = &mt('only members of group: "[_1]"',$env{'form.grpfilter'});
                   3889:                     } else {
                   3890:                         $constraint .= &mt(' and also members of group: "[_1]"'.$env{'form.grpfilter'});
                   3891:                     }
                   3892:                 }
1.15      raeburn  3893:             }
                   3894:         }
1.33      raeburn  3895:         if ($constraint ne '') {
                   3896:             $description .= ' ('.$constraint.')';
                   3897:         } 
1.13      raeburn  3898:     } elsif ($context eq 'author') {
1.184.4.10.2.  (raeburn 3899:):         my ($auname,$audom);
                   3900:):         if ($env{'request.role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
                   3901:):             ($audom,$auname) = ($1,$2);
                   3902:):         } else {
                   3903:):             ($audom,$auname) = ($env{'user.domain'},$env{'user.name'});
                   3904:):         }
1.14      raeburn  3905:         $description = 
1.73      bisitz   3906:             &mt('Author space for [_1]'
                   3907:                 ,'<span class="LC_cusr_emph">'
1.184.4.10.2.  (raeburn 3908:):                 .&Apache::loncommon::plainname($auname,$audom)
1.73      bisitz   3909:                 .'</span>')
                   3910:             .':&nbsp;&nbsp;';
1.2       raeburn  3911:         if ($statusmode eq 'Expired') {
1.5       raeburn  3912:             $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2       raeburn  3913:         } elsif ($statusmode eq 'Future') {
1.5       raeburn  3914:             $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2       raeburn  3915:         } elsif ($statusmode eq 'Active') {
1.5       raeburn  3916:             $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2       raeburn  3917:         } else {
                   3918:             if ($rolefilter eq 'Any') {
1.5       raeburn  3919:                 $description .= &mt('All co-authors');
1.2       raeburn  3920:             } else {
                   3921:                 $description .= &mt('All co-authors with [_1] roles',$rolefilter);
                   3922:             }
                   3923:         }
                   3924:     } elsif ($context eq 'domain') {
                   3925:         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
1.73      bisitz   3926:         $description = &mt('Domain - [_1]:',$domdesc).' ';
1.2       raeburn  3927:         if ($env{'form.roletype'} eq 'domain') {
                   3928:             if ($statusmode eq 'Expired') {
1.5       raeburn  3929:                 $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3930:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3931:                 $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2       raeburn  3932:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3933:                 $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2       raeburn  3934:             } else {
                   3935:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3936:                     $description .= &mt('All users in domain');
1.2       raeburn  3937:                 } else {
                   3938:                     $description .= &mt('All users in domain with [_1] roles',$rolefilter);
                   3939:                 }
                   3940:             }
1.13      raeburn  3941:         } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  3942:             if ($statusmode eq 'Expired') {
1.5       raeburn  3943:                 $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2       raeburn  3944:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3945:                 $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2       raeburn  3946:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3947:                $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2       raeburn  3948:             } else {
                   3949:                 if ($rolefilter eq 'Any') {
1.5       raeburn  3950:                     $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2       raeburn  3951:                 } else {
1.116     bisitz   3952:                     $description .= &mt('All co-authors in domain with [_1] roles',$rolefilter);
1.2       raeburn  3953:                 }
                   3954:             }
1.102     raeburn  3955:         } elsif (($env{'form.roletype'} eq 'course') || 
                   3956:                  ($env{'form.roletype'} eq 'community')) {
1.2       raeburn  3957:             my $coursefilter = $env{'form.coursepick'};
1.102     raeburn  3958:             if ($env{'form.roletype'} eq 'course') {
                   3959:                 if ($coursefilter eq 'category') {
                   3960:                     my $instcode = &instcode_from_coursefilter();
                   3961:                     if ($instcode eq '.') {
                   3962:                         $description .= &mt('All courses in domain').' - ';
                   3963:                     } else {
                   3964:                         $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
                   3965:                     }
                   3966:                 } elsif ($coursefilter eq 'selected') {
                   3967:                     $description .= &mt('Selected courses in domain').' - ';
                   3968:                 } elsif ($coursefilter eq 'all') {
1.2       raeburn  3969:                     $description .= &mt('All courses in domain').' - ';
                   3970:                 }
1.102     raeburn  3971:             } elsif ($env{'form.roletype'} eq 'community') {
                   3972:                 if ($coursefilter eq 'selected') {
                   3973:                     $description .= &mt('Selected communities in domain').' - ';
                   3974:                 } elsif ($coursefilter eq 'all') {
                   3975:                     $description .= &mt('All communities in domain').' - ';
                   3976:                 }
1.2       raeburn  3977:             }
                   3978:             if ($statusmode eq 'Expired') {
1.5       raeburn  3979:                 $description .= &mt('users with expired [_1] roles',$showfilter);
1.2       raeburn  3980:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  3981:                 $description .= &mt('users with future [_1] roles',$showfilter);
1.2       raeburn  3982:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  3983:                 $description .= &mt('users with active [_1] roles',$showfilter);
1.2       raeburn  3984:             } else {
                   3985:                 if ($rolefilter eq 'Any') {
                   3986:                     $description .= &mt('all users');
                   3987:                 } else {
                   3988:                     $description .= &mt('users with [_1] roles',$rolefilter);
                   3989:                 }
                   3990:             }
                   3991:         }
                   3992:     }
                   3993:     return $description;
                   3994: }
1.22      raeburn  3995: 
                   3996: sub viewable_section {
                   3997:     my ($permission) = @_;
                   3998:     my $viewablesec;
                   3999:     if (ref($permission) eq 'HASH') {
                   4000:         if (exists($permission->{'view_section'})) {
                   4001:             $viewablesec = $permission->{'view_section'};
                   4002:         } elsif (exists($permission->{'cusr_section'})) {
                   4003:             $viewablesec = $permission->{'cusr_section'};
                   4004:         }
                   4005:     }
                   4006:     return $viewablesec;
                   4007: }
                   4008: 
1.2       raeburn  4009:     
1.1       raeburn  4010: #################################################
                   4011: #################################################
                   4012: sub show_drop_list {
1.101     raeburn  4013:     my ($r,$classlist,$nosort,$permission,$crstype) = @_;
1.29      raeburn  4014:     my $cid = $env{'request.course.id'};
1.17      raeburn  4015:     my ($cnum,$cdom) = &get_course_identity($cid);
1.1       raeburn  4016:     if (! exists($env{'form.sortby'})) {
                   4017:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   4018:                                                 ['sortby']);
                   4019:     }
                   4020:     my $sortby = $env{'form.sortby'};
                   4021:     if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
                   4022:         $sortby = 'username';
                   4023:     }
                   4024:     my $action = "drop";
1.17      raeburn  4025:     my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.1       raeburn  4026:     $r->print(<<END);
                   4027: <input type="hidden" name="sortby" value="$sortby" />
                   4028: <input type="hidden" name="action" value="$action" />
                   4029: <input type="hidden" name="state"  value="done" />
1.17      raeburn  4030: <script type="text/javascript" language="Javascript">
1.96      bisitz   4031: // <![CDATA[
1.17      raeburn  4032: $check_uncheck_js
1.96      bisitz   4033: // ]]>
1.1       raeburn  4034: </script>
1.86      bisitz   4035: <input type="hidden" name="phase" value="four" />
1.1       raeburn  4036: END
1.30      raeburn  4037:     my ($indexhash,$keylist) = &make_keylist_array();
                   4038:     my $studentcount = 0;
                   4039:     if (ref($classlist) eq 'HASH') {
                   4040:         foreach my $student (keys(%{$classlist})) {
                   4041:             my $sdata = $classlist->{$student}; 
                   4042:             my $status = $sdata->[$indexhash->{'status'}];
                   4043:             my $section = $sdata->[$indexhash->{'section'}];
                   4044:             if ($status ne 'Active') {
                   4045:                 delete($classlist->{$student});
                   4046:                 next;
                   4047:             }
                   4048:             if ($env{'request.course.sec'} ne '') {
                   4049:                 if ($section ne $env{'request.course.sec'}) {
                   4050:                     delete($classlist->{$student});
                   4051:                     next;
                   4052:                 }
                   4053:             }
                   4054:             $studentcount ++;
                   4055:         }
                   4056:     }
                   4057:     if (!$studentcount) {
1.143     bisitz   4058:        my $msg = '';
1.101     raeburn  4059:         if ($crstype eq 'Community') {
1.143     bisitz   4060:             $msg = &mt('There are no members to drop.');
1.101     raeburn  4061:         } else {
1.143     bisitz   4062:             $msg = &mt('There are no students to drop.');
1.101     raeburn  4063:         }
1.143     bisitz   4064:         $r->print('<p class="LC_info">'.$msg.'</p>');
1.30      raeburn  4065:         return;
                   4066:     }
                   4067:     my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   4068:                                               $classlist,$keylist,$cdom,$cnum);
                   4069:     my %lt=&Apache::lonlocal::texthash('usrn'   => "username",
                   4070:                                        'dom'    => "domain",
1.162     bisitz   4071:                                        'id'     => "ID",
1.30      raeburn  4072:                                        'sn'     => "student name",
1.101     raeburn  4073:                                        'mn'     => "member name",
1.30      raeburn  4074:                                        'sec'    => "section",
                   4075:                                        'start'  => "start date",
                   4076:                                        'end'    => "end date",
                   4077:                                        'groups' => "active groups",
                   4078:                                       );
1.101     raeburn  4079:     my $nametitle = $lt{'sn'};
                   4080:     if ($crstype eq 'Community') {
                   4081:         $nametitle = $lt{'mn'};
                   4082:     }
1.1       raeburn  4083:     if ($nosort) {
1.17      raeburn  4084:         $r->print(&Apache::loncommon::start_data_table().
                   4085:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  4086:         $r->print(<<END);
                   4087:     <th>&nbsp;</th>
                   4088:     <th>$lt{'usrn'}</th>
                   4089:     <th>$lt{'dom'}</th>
1.162     bisitz   4090:     <th>$lt{'id'}</th>
1.101     raeburn  4091:     <th>$nametitle</th>
1.1       raeburn  4092:     <th>$lt{'sec'}</th>
                   4093:     <th>$lt{'start'}</th>
                   4094:     <th>$lt{'end'}</th>
                   4095:     <th>$lt{'groups'}</th>
                   4096: END
1.17      raeburn  4097:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  4098:     } else  {
1.17      raeburn  4099:         $r->print(&Apache::loncommon::start_data_table().
                   4100:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  4101:         $r->print(<<END);
1.17      raeburn  4102:     <th>&nbsp;</th>
1.1       raeburn  4103:     <th>
1.162     bisitz   4104:        <a href="/adm/createuser?action=$action&amp;sortby=username">$lt{'usrn'}</a>
1.1       raeburn  4105:     </th><th>
1.162     bisitz   4106:        <a href="/adm/createuser?action=$action&amp;sortby=domain">$lt{'dom'}</a>
1.1       raeburn  4107:     </th><th>
1.162     bisitz   4108:        <a href="/adm/createuser?action=$action&amp;sortby=id">$lt{'id'}</a>
1.1       raeburn  4109:     </th><th>
1.162     bisitz   4110:        <a href="/adm/createuser?action=$action&amp;sortby=fullname">$nametitle</a>
1.1       raeburn  4111:     </th><th>
1.162     bisitz   4112:        <a href="/adm/createuser?action=$action&amp;sortby=section">$lt{'sec'}</a>
1.1       raeburn  4113:     </th><th>
1.162     bisitz   4114:        <a href="/adm/createuser?action=$action&amp;sortby=start">$lt{'start'}</a>
1.1       raeburn  4115:     </th><th>
1.162     bisitz   4116:        <a href="/adm/createuser?action=$action&amp;sortby=end">$lt{'end'}</a>
1.1       raeburn  4117:     </th><th>
1.162     bisitz   4118:        <a href="/adm/createuser?action=$action&amp;sortby=groups">$lt{'groups'}</a>
1.1       raeburn  4119:     </th>
                   4120: END
1.17      raeburn  4121:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  4122:     }
                   4123:     #
                   4124:     # Sort the students
1.30      raeburn  4125:     my $index  = $indexhash->{$sortby};
                   4126:     my $second = $indexhash->{'username'};
                   4127:     my $third  = $indexhash->{'domain'};
1.1       raeburn  4128:     my @Sorted_Students = sort {
                   4129:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   4130:             ||
                   4131:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   4132:             ||
                   4133:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1.30      raeburn  4134:         } (keys(%{$classlist}));
1.1       raeburn  4135:     foreach my $student (@Sorted_Students) {
                   4136:         my $error;
                   4137:         my $sdata = $classlist->{$student};
1.30      raeburn  4138:         my $username = $sdata->[$indexhash->{'username'}];
                   4139:         my $domain   = $sdata->[$indexhash->{'domain'}];
                   4140:         my $section  = $sdata->[$indexhash->{'section'}];
                   4141:         my $name     = $sdata->[$indexhash->{'fullname'}];
                   4142:         my $id       = $sdata->[$indexhash->{'id'}];
                   4143:         my $start    = $sdata->[$indexhash->{'start'}];
                   4144:         my $end      = $sdata->[$indexhash->{'end'}];
1.1       raeburn  4145:         my $groups = $classgroups->{$student};
                   4146:         my $active_groups;
                   4147:         if (ref($groups->{active}) eq 'HASH') {
                   4148:             $active_groups = join(', ',keys(%{$groups->{'active'}}));
                   4149:         }
                   4150:         if (! defined($start) || $start == 0) {
                   4151:             $start = &mt('none');
                   4152:         } else {
                   4153:             $start = &Apache::lonlocal::locallocaltime($start);
                   4154:         }
                   4155:         if (! defined($end) || $end == 0) {
                   4156:             $end = &mt('none');
                   4157:         } else {
                   4158:             $end = &Apache::lonlocal::locallocaltime($end);
                   4159:         }
1.17      raeburn  4160:         my $studentkey = $student.':'.$section;
1.30      raeburn  4161:         my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$indexhash->{'start'}].'" />';
1.1       raeburn  4162:         #
                   4163:         $r->print(&Apache::loncommon::start_data_table_row());
                   4164:         $r->print(<<"END");
1.86      bisitz   4165:     <td><input type="checkbox" name="droplist" value="$studentkey" /></td>
1.1       raeburn  4166:     <td>$username</td>
                   4167:     <td>$domain</td>
                   4168:     <td>$id</td>
                   4169:     <td>$name</td>
                   4170:     <td>$section</td>
1.29      raeburn  4171:     <td>$start $startitem</td>
1.1       raeburn  4172:     <td>$end</td>
                   4173:     <td>$active_groups</td>
                   4174: END
                   4175:         $r->print(&Apache::loncommon::end_data_table_row());
                   4176:     }
                   4177:     $r->print(&Apache::loncommon::end_data_table().'<br />');
                   4178:     %lt=&Apache::lonlocal::texthash(
1.29      raeburn  4179:                        'dp'   => "Drop Students",
1.101     raeburn  4180:                        'dm'   => "Drop Members",
1.1       raeburn  4181:                        'ca'   => "check all",
                   4182:                        'ua'   => "uncheck all",
                   4183:                                        );
1.101     raeburn  4184:     my $btn = $lt{'dp'};
                   4185:     if ($crstype eq 'Community') {
                   4186:         $btn = $lt{'dm'}; 
                   4187:     }
1.1       raeburn  4188:     $r->print(<<"END");
1.89      bisitz   4189: <p>
1.86      bisitz   4190: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)" /> &nbsp;
                   4191: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)" />
1.89      bisitz   4192: </p>
                   4193: <p>
1.101     raeburn  4194: <input type="submit" value="$btn" />
1.89      bisitz   4195: </p>
1.1       raeburn  4196: END
                   4197:     return;
                   4198: }
                   4199: 
                   4200: #
                   4201: # Print out the initial form to get the file containing a list of users
                   4202: #
                   4203: sub print_first_users_upload_form {
                   4204:     my ($r,$context) = @_;
                   4205:     my $str;
1.86      bisitz   4206:     $str  = '<input type="hidden" name="phase" value="two" />';
1.1       raeburn  4207:     $str .= '<input type="hidden" name="action" value="upload" />';
1.101     raeburn  4208:     $str .= '<input type="hidden" name="state"  value="got_file" />';
1.95      bisitz   4209: 
1.147     bisitz   4210:     $str .= &Apache::grades::checkforfile_js();
                   4211: 
1.85      bisitz   4212:     $str .= '<h2>'.&mt('Upload a file containing information about users').'</h2>'."\n";
1.95      bisitz   4213: 
                   4214:     # Excel and CSV Help
1.147     bisitz   4215:     $str .= '<div class="LC_columnSection">'
1.95      bisitz   4216:            .&Apache::loncommon::help_open_topic("Course_Create_Class_List",
                   4217:                 &mt("How do I create a users list from a spreadsheet"))
1.147     bisitz   4218:            .' '.&Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
1.95      bisitz   4219:                 &mt("How do I create a CSV file from a spreadsheet"))
1.147     bisitz   4220:            ."</div>\n";
1.95      bisitz   4221:     $str .= &Apache::lonhtmlcommon::start_pick_box()
1.103     raeburn  4222:            .&Apache::lonhtmlcommon::row_title(&mt('File'));
                   4223:     if (&Apache::lonlocal::current_language() ne 'en') {
                   4224:         if ($context eq 'course') { 
                   4225:             $str .= '<p class="LC_info">'."\n"
                   4226:                    .&mt('Please upload an UTF8 encoded file to ensure a correct character encoding in your classlist.')."\n"
                   4227:                    .'</p>'."\n";
                   4228:         }
                   4229:     }
                   4230:     $str .= &Apache::loncommon::upfile_select_html()
1.95      bisitz   4231:            .&Apache::lonhtmlcommon::row_closure()
                   4232:            .&Apache::lonhtmlcommon::row_title(
                   4233:                 '<label for="noFirstLine">'
                   4234:                .&mt('Ignore First Line')
                   4235:                .'</label>')
                   4236:            .'<input type="checkbox" name="noFirstLine" id="noFirstLine" />'
                   4237:            .&Apache::lonhtmlcommon::row_closure(1)
                   4238:            .&Apache::lonhtmlcommon::end_pick_box();
                   4239: 
                   4240:     $str .= '<p>'
1.184.4.2  raeburn  4241:            .'<input type="button" name="fileupload" value="'.&mt('Next').'"'
1.147     bisitz   4242:            .' onclick="javascript:checkUpload(this.form);" />'
1.95      bisitz   4243:            .'</p>';
                   4244: 
1.1       raeburn  4245:     $r->print($str);
                   4246:     return;
                   4247: }
                   4248: 
                   4249: # ================================================= Drop/Add from uploaded file
                   4250: sub upfile_drop_add {
1.150     raeburn  4251:     my ($r,$context,$permission,$showcredits) = @_;
1.184.4.1  raeburn  4252:     my $datatoken = &Apache::loncommon::valid_datatoken($env{'form.datatoken'});
                   4253:     if ($datatoken ne '') {
                   4254:         &Apache::loncommon::load_tmp_file($r,$datatoken);
                   4255:     }
1.1       raeburn  4256:     my @userdata=&Apache::loncommon::upfile_record_sep();
                   4257:     if($env{'form.noFirstLine'}){shift(@userdata);}
                   4258:     my @keyfields = split(/\,/,$env{'form.keyfields'});
                   4259:     my %fields=();
                   4260:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
                   4261:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   4262:             if ($env{'form.f'.$i} ne 'none') {
                   4263:                 $fields{$keyfields[$i]}=$env{'form.f'.$i};
                   4264:             }
                   4265:         } else {
                   4266:             $fields{$env{'form.f'.$i}}=$keyfields[$i];
                   4267:         }
                   4268:     }
                   4269:     #
                   4270:     # Store the field choices away
1.150     raeburn  4271:     my @storefields = qw/username names fname mname lname gen id 
                   4272:                          sec ipwd email role domain inststatus/;
                   4273:     if ($showcredits) {
                   4274:         push (@storefields,'credits');
                   4275:     }
                   4276:     my %fieldstype; 
                   4277:     foreach my $field (@storefields) {
1.1       raeburn  4278:         $env{'form.'.$field.'_choice'}=$fields{$field};
1.150     raeburn  4279:         $fieldstype{$field.'_choice'} = 'scalar';
1.1       raeburn  4280:     }
1.150     raeburn  4281:     &Apache::loncommon::store_course_settings('enrollment_upload',\%fieldstype);
1.184.4.10.2.  (raeburn 4282:):     my ($cid,$crstype,$setting,$crsdom,$crsnum,$oldcrsuserdoms,%emptyok);
1.101     raeburn  4283:     if ($context eq 'domain') {
                   4284:         $setting = $env{'form.roleaction'};
1.184.4.10.2.  (raeburn 4285:):         if (exists($fields{'names'})) {
                   4286:):             map { $emptyok{$_} = 1; } ('lastname','firstname','middlename');
                   4287:):         } else {
                   4288:):             if (exists($fields{'lname'})) {
                   4289:):                 $emptyok{'lastname'} = 1;
                   4290:):             }
                   4291:):             if (exists($fields{'fname'})) {
                   4292:):                 $emptyok{'firstname'} = 1;
                   4293:):             }
                   4294:):             if (exists($fields{'mname'})) {
                   4295:):                 $emptyok{'middlename'} = 1;
                   4296:):             }
                   4297:):         }
                   4298:):         if (exists($fields{'gen'})) {
                   4299:):             $emptyok{'generation'} = 1;
                   4300:):         }
1.101     raeburn  4301:     }
                   4302:     if ($env{'request.course.id'} ne '') {
                   4303:         $cid = $env{'request.course.id'};
                   4304:         $crstype = &Apache::loncommon::course_type();
1.184.4.10.2.  (raeburn 4305:):         $crsdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4306:):         $crsnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1.101     raeburn  4307:     } elsif ($setting eq 'course') {
                   4308:         if (&Apache::lonnet::is_course($env{'form.dcdomain'},$env{'form.dccourse'})) {
                   4309:             $cid = $env{'form.dcdomain'}.'_'.$env{'form.dccourse'};
                   4310:             $crstype = &Apache::loncommon::course_type($cid);
1.184.4.10.2.  (raeburn 4311:):             $crsdom = $env{'form.dcdomain'};
                   4312:):             $crsnum = $env{'form.dccourse'};
                   4313:):             if (exists($env{'course.'.$cid.'.internal.userdomains'})) {
                   4314:):                 $oldcrsuserdoms = 1;
                   4315:):             }
                   4316:):             my %coursedesc = &Apache::lonnet::coursedescription($cid,{ one_time => 1 });
                   4317:):             $env{'course.'.$cid.'.internal.userdomains'} = $coursedesc{'internal.userdomains'};
1.101     raeburn  4318:         }
                   4319:     }
1.1       raeburn  4320:     my ($startdate,$enddate) = &get_dates_from_form();
                   4321:     if ($env{'form.makedatesdefault'}) {
1.101     raeburn  4322:         $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  4323:     }
                   4324:     # Determine domain and desired host (home server)
1.57      raeburn  4325:     my $defdom=$env{'request.role.domain'};
                   4326:     my $domain;
                   4327:     if ($env{'form.defaultdomain'} ne '') {
                   4328:         $domain = $env{'form.defaultdomain'};
                   4329:     } else {
                   4330:         $domain = $defdom;
                   4331:     }
1.1       raeburn  4332:     my $desiredhost = $env{'form.lcserver'};
                   4333:     if (lc($desiredhost) eq 'default') {
                   4334:         $desiredhost = undef;
                   4335:     } else {
1.57      raeburn  4336:         my %home_servers = &Apache::lonnet::get_servers($defdom,'library');
1.1       raeburn  4337:         if (! exists($home_servers{$desiredhost})) {
1.184.4.1  raeburn  4338:             $r->print('<p class="LC_error">'.&mt('Error').': '.
                   4339:                       &mt('Invalid home server specified').'</p>');
1.1       raeburn  4340:             $r->print(&Apache::loncommon::end_page());
1.184.4.1  raeburn  4341:             return 'invalidhome';
1.1       raeburn  4342:         }
                   4343:     }
                   4344:     # Determine authentication mechanism
                   4345:     my $changeauth;
                   4346:     if ($context eq 'domain') {
                   4347:         $changeauth = $env{'form.changeauth'};
                   4348:     }
                   4349:     my $amode  = '';
                   4350:     my $genpwd = '';
1.184.4.4  raeburn  4351:     my @genpwdfail;
1.1       raeburn  4352:     if ($env{'form.login'} eq 'krb') {
                   4353:         $amode='krb';
                   4354:         $amode.=$env{'form.krbver'};
                   4355:         $genpwd=$env{'form.krbarg'};
                   4356:     } elsif ($env{'form.login'} eq 'int') {
                   4357:         $amode='internal';
                   4358:         if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
                   4359:             $genpwd=$env{'form.intarg'};
1.184.4.4  raeburn  4360:             @genpwdfail =
                   4361:                 &Apache::loncommon::check_passwd_rules($domain,$genpwd);
1.1       raeburn  4362:         }
                   4363:     } elsif ($env{'form.login'} eq 'loc') {
                   4364:         $amode='localauth';
                   4365:         if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
                   4366:             $genpwd=$env{'form.locarg'};
                   4367:         }
                   4368:     }
                   4369:     if ($amode =~ /^krb/) {
                   4370:         if (! defined($genpwd) || $genpwd eq '') {
                   4371:             $r->print('<span class="Error">'.
                   4372:                       &mt('Unable to enroll users').' '.
                   4373:                       &mt('No Kerberos domain was specified.').'</span></p>');
                   4374:             $amode = ''; # This causes the loop below to be skipped
                   4375:         }
                   4376:     }
1.150     raeburn  4377:     my ($defaultsec,$defaultrole,$defaultcredits,$commoncredits);
1.1       raeburn  4378:     if ($context eq 'domain') {
                   4379:         if ($setting eq 'domain') {
                   4380:             $defaultrole = $env{'form.defaultrole'};
                   4381:         } elsif ($setting eq 'course') {
                   4382:             $defaultrole = $env{'form.courserole'};
1.27      raeburn  4383:             $defaultsec = $env{'form.sections'};
1.150     raeburn  4384:             if ($showcredits) {
                   4385:                 $commoncredits = $env{'form.credits'};
                   4386:                 if ($crstype ne 'Community') {
                   4387:                     my %coursehash=&Apache::lonnet::coursedescription($cid);
                   4388:                     $defaultcredits = $coursehash{'internal.defaultcredits'};
                   4389:                 }
                   4390:             }
1.149     raeburn  4391:         }
1.13      raeburn  4392:     } elsif ($context eq 'author') {
1.1       raeburn  4393:         $defaultrole = $env{'form.defaultrole'};
1.27      raeburn  4394:     } elsif ($context eq 'course') {
                   4395:         $defaultrole = $env{'form.defaultrole'};
                   4396:         $defaultsec = $env{'form.sections'};
1.150     raeburn  4397:         if ($showcredits) {
                   4398:             $commoncredits = $env{'form.credits'};
                   4399:             $defaultcredits = $env{'course.'.$cid.'.internal.defaultcredits'};
                   4400:         }
1.1       raeburn  4401:     }
1.27      raeburn  4402:     # Check to see if user information can be changed
                   4403:     my @userinfo = ('firstname','middlename','lastname','generation',
                   4404:                     'permanentemail','id');
                   4405:     my %canmodify;
                   4406:     if (&Apache::lonnet::allowed('mau',$domain)) {
1.84      raeburn  4407:         push(@userinfo,'inststatus');
1.27      raeburn  4408:         foreach my $field (@userinfo) {
                   4409:             $canmodify{$field} = 1;
                   4410:         }
                   4411:     }
                   4412:     my (%userlist,%modifiable_fields,@poss_roles);
                   4413:     my $secidx = &Apache::loncoursedata::CL_SECTION();
1.102     raeburn  4414:     my @courseroles = &roles_by_context('course',1,$crstype);
1.27      raeburn  4415:     if (!&Apache::lonnet::allowed('mau',$domain)) {
                   4416:         if ($context eq 'course' || $context eq 'author') {
1.101     raeburn  4417:             @poss_roles =  &curr_role_permissions($context,'','',$crstype);
1.27      raeburn  4418:             my @statuses = ('active','future');
                   4419:             my ($indexhash,$keylist) = &make_keylist_array();
                   4420:             my %info;
                   4421:             foreach my $role (@poss_roles) {
                   4422:                 %{$modifiable_fields{$role}} = &can_modify_userinfo($context,$domain,
                   4423:                                                         \@userinfo,[$role]);
                   4424:             }
                   4425:             if ($context eq 'course') {
                   4426:                 my ($cnum,$cdom) = &get_course_identity();
                   4427:                 my $roster = &Apache::loncoursedata::get_classlist();
1.66      raeburn  4428:                 if (ref($roster) eq 'HASH') {
                   4429:                     %userlist = %{$roster};
                   4430:                 }
1.27      raeburn  4431:                 my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   4432:                                                          \@statuses,\@poss_roles);
                   4433:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   4434:                                 \%advrolehash,$permission);
                   4435:             } elsif ($context eq 'author') {
                   4436:                 my %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   4437:                                                   \@statuses,\@poss_roles);
                   4438:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   4439:                              \%cstr_roles,$permission);
                   4440:             }
                   4441:         }
1.1       raeburn  4442:     }
1.184.4.1  raeburn  4443:     if ($datatoken eq '') {
                   4444:         $r->print('<p class="LC_error">'.&mt('Error').': '.
                   4445:                   &mt('Invalid datatoken').'</p>');
                   4446:         return 'missingdata';
                   4447:     }
1.1       raeburn  4448:     if ( $domain eq &LONCAPA::clean_domain($domain)
                   4449:         && ($amode ne '')) {
                   4450:         #######################################
                   4451:         ##         Add/Modify Users          ##
                   4452:         #######################################
                   4453:         if ($context eq 'course') {
                   4454:             $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13      raeburn  4455:         } elsif ($context eq 'author') {
1.1       raeburn  4456:             $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
                   4457:         } else {
                   4458:             $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
                   4459:         }
1.87      bisitz   4460:         $r->rflush;
                   4461: 
1.1       raeburn  4462:         my %counts = (
                   4463:                        user => 0,
                   4464:                        auth => 0,
                   4465:                        role => 0,
                   4466:                      );
                   4467:         my $flushc=0;
                   4468:         my %student=();
1.42      raeburn  4469:         my (%curr_groups,@sections,@cleansec,$defaultwarn,$groupwarn);
1.1       raeburn  4470:         my %userchg;
1.27      raeburn  4471:         if ($context eq 'course' || $setting eq 'course') {
                   4472:             if ($context eq 'course') {
                   4473:                 # Get information about course groups
                   4474:                 %curr_groups = &Apache::longroup::coursegroups();
                   4475:             } elsif ($setting eq 'course') {
                   4476:                 if ($cid) {
                   4477:                     %curr_groups =
                   4478:                         &Apache::longroup::coursegroups($env{'form.dcdomain'},
                   4479:                                                         $env{'form.dccourse'});
                   4480:                 }
                   4481:             }
                   4482:             # determine section number
                   4483:             if ($defaultsec =~ /,/) {
                   4484:                 push(@sections,split(/,/,$defaultsec));
                   4485:             } else {
                   4486:                 push(@sections,$defaultsec);
                   4487:             }
                   4488:             # remove non alphanumeric values from section
                   4489:             foreach my $item (@sections) {
                   4490:                 $item =~ s/\W//g;
                   4491:                 if ($item eq "none" || $item eq 'all') {
                   4492:                     $defaultwarn = &mt('Default section name [_1] could not be used as it is a reserved word.',$item);
                   4493:                 } elsif ($item ne ''  && exists($curr_groups{$item})) {
                   4494:                     $groupwarn = &mt('Default section name "[_1]" is the name of a course group. Section names and group names must be distinct.',$item);
                   4495:                 } elsif ($item ne '') {
                   4496:                     push(@cleansec,$item);
                   4497:                 }
                   4498:             }
                   4499:             if ($defaultwarn) {
                   4500:                 $r->print($defaultwarn.'<br />');
                   4501:             }
                   4502:             if ($groupwarn) {
                   4503:                 $r->print($groupwarn.'<br />');
                   4504:             }
1.1       raeburn  4505:         }
1.124     raeburn  4506:         my (%curr_rules,%got_rules,%alerts,%cancreate);
1.104     raeburn  4507:         my %customroles = &my_custom_roles($crstype);
1.101     raeburn  4508:         my @permitted_roles = 
1.124     raeburn  4509:             &roles_on_upload($context,$setting,$crstype,%customroles);
                   4510:         my %longtypes = &Apache::lonlocal::texthash(
                   4511:                             official   => 'Institutional',
                   4512:                             unofficial => 'Non-institutional',
                   4513:                         );
1.135     raeburn  4514:         my $newuserdom = $env{'request.role.domain'};
                   4515:         map { $cancreate{$_} = &can_create_user($newuserdom,$context,$_); } keys(%longtypes);
1.1       raeburn  4516:         # Get new users list
1.184.4.4  raeburn  4517:         my (%existinguser,%userinfo,%disallow,%rulematch,%inst_results,%alerts,%checkuname,
                   4518:             %showpasswdrules,$haspasswdmap);
1.171     raeburn  4519:         my $counter = -1;
1.27      raeburn  4520:         foreach my $line (@userdata) {
1.171     raeburn  4521:             $counter ++;
1.42      raeburn  4522:             my @secs;
1.27      raeburn  4523:             my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  4524:             # Determine user name
1.128     raeburn  4525:             $entries{$fields{'username'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4526:             unless (($entries{$fields{'username'}} eq '') ||
                   4527:                     (!defined($entries{$fields{'username'}}))) {
                   4528:                 my ($fname, $mname, $lname,$gen) = ('','','','');
                   4529:                 if (defined($fields{'names'})) {
                   4530:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   4531:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
                   4532:                 } else {
                   4533:                     if (defined($fields{'fname'})) {
                   4534:                         $fname=$entries{$fields{'fname'}};
                   4535:                     }
                   4536:                     if (defined($fields{'mname'})) {
                   4537:                         $mname=$entries{$fields{'mname'}};
                   4538:                     }
                   4539:                     if (defined($fields{'lname'})) {
                   4540:                         $lname=$entries{$fields{'lname'}};
                   4541:                     }
                   4542:                     if (defined($fields{'gen'})) {
                   4543:                         $gen=$entries{$fields{'gen'}};
                   4544:                     }
                   4545:                 }
1.128     raeburn  4546: 
1.1       raeburn  4547:                 if ($entries{$fields{'username'}}
                   4548:                     ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
1.128     raeburn  4549:                     my $nowhitespace;
                   4550:                     if ($entries{$fields{'username'}} =~ /\s/) {
                   4551:                         $nowhitespace = ' - '.&mt('usernames may not contain spaces.');
                   4552:                     }
1.171     raeburn  4553:                     $disallow{$counter} =
1.157     bisitz   4554:                         &mt('Unacceptable username [_1] for user [_2] [_3] [_4] [_5]',
1.171     raeburn  4555:                             '"<b>'.$entries{$fields{'username'}}.'</b>"',
                   4556:                             $fname,$mname,$lname,$gen).$nowhitespace;
1.27      raeburn  4557:                     next;
1.1       raeburn  4558:                 } else {
1.129     raeburn  4559:                     $entries{$fields{'domain'}} =~ s/^\s+|\s+$//g;
1.71      droeschl 4560:                     if ($entries{$fields{'domain'}} 
1.57      raeburn  4561:                         ne &LONCAPA::clean_domain($entries{$fields{'domain'}})) {
1.171     raeburn  4562:                         $disallow{$counter} =
1.157     bisitz   4563:                             &mt('Unacceptable domain [_1] for user [_2] [_3] [_4] [_5]',
1.171     raeburn  4564:                                 '"<b>'.$entries{$fields{'domain'}}.'</b>"',
                   4565:                                 $fname,$mname,$lname,$gen);
                   4566:                         next;
1.57      raeburn  4567:                     }
1.5       raeburn  4568:                     my $username = $entries{$fields{'username'}};
1.57      raeburn  4569:                     my $userdomain = $entries{$fields{'domain'}};
                   4570:                     if ($userdomain eq '') {
                   4571:                         $userdomain = $domain;
                   4572:                     }
1.27      raeburn  4573:                     if (defined($fields{'sec'})) {
                   4574:                         if (defined($entries{$fields{'sec'}})) {
1.42      raeburn  4575:                             $entries{$fields{'sec'}} =~ s/\W//g;
1.27      raeburn  4576:                             my $item = $entries{$fields{'sec'}};
                   4577:                             if ($item eq "none" || $item eq 'all') {
1.171     raeburn  4578:                                 $disallow{$counter} =
                   4579:                                     &mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a reserved word.',
                   4580:                                         '<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item);
1.27      raeburn  4581:                                 next;
                   4582:                             } elsif (exists($curr_groups{$item})) {
1.171     raeburn  4583:                                 $disallow{$counter} =
                   4584:                                     &mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a course group.',
                   4585:                                         '<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$item).' '.
                   4586:                                     &mt('Section names and group names must be distinct.');
1.27      raeburn  4587:                                 next;
                   4588:                             } else {
                   4589:                                 push(@secs,$item);
                   4590:                             }
                   4591:                         }
                   4592:                     }
                   4593:                     if ($env{'request.course.sec'} ne '') {
                   4594:                         @secs = ($env{'request.course.sec'});
1.57      raeburn  4595:                         if (ref($userlist{$username.':'.$userdomain}) eq 'ARRAY') {
                   4596:                             my $currsec = $userlist{$username.':'.$userdomain}[$secidx];
1.27      raeburn  4597:                             if ($currsec ne $env{'request.course.sec'}) {
1.171     raeburn  4598:                                 $disallow{$counter} =
                   4599:                                     &mt('[_1]: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]".',
                   4600:                                         '<b>'.$username.'</b>',$fname,$mname,$lname,$gen,$secs[0]);
1.27      raeburn  4601:                                 if ($currsec eq '') {
1.171     raeburn  4602:                                     $disallow{$counter} .=
                   4603:                                         &mt('This user already has an active/future student role in the course, unaffiliated to any section.');
1.27      raeburn  4604: 
                   4605:                                 } else {
1.171     raeburn  4606:                                     $disallow{$counter} .=
                   4607:                                         &mt('This user already has an active/future role in section "[_1]" of the course.',$currsec);
1.27      raeburn  4608:                                 }
1.171     raeburn  4609:                                 $disallow{$counter} .=
                   4610:                                     '<br />'.
                   4611:                                     &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.',
                   4612:                                         $secs[0]);
1.27      raeburn  4613:                                 next;
1.1       raeburn  4614:                             }
                   4615:                         }
1.27      raeburn  4616:                     } elsif ($context eq 'course' || $setting eq 'course') {
                   4617:                         if (@secs == 0) {
                   4618:                             @secs = @cleansec;
1.1       raeburn  4619:                         }
                   4620:                     }
                   4621:                     # determine id number
                   4622:                     my $id='';
                   4623:                     if (defined($fields{'id'})) {
                   4624:                         if (defined($entries{$fields{'id'}})) {
                   4625:                             $id=$entries{$fields{'id'}};
                   4626:                         }
                   4627:                         $id=~tr/A-Z/a-z/;
                   4628:                     }
                   4629:                     # determine email address
                   4630:                     my $email='';
                   4631:                     if (defined($fields{'email'})) {
1.129     raeburn  4632:                         $entries{$fields{'email'}} =~ s/^\s+|\s+$//g;
1.1       raeburn  4633:                         if (defined($entries{$fields{'email'}})) {
                   4634:                             $email=$entries{$fields{'email'}};
1.84      raeburn  4635:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }
                   4636:                         }
                   4637:                     }
                   4638:                     # determine affiliation
                   4639:                     my $inststatus='';
                   4640:                     if (defined($fields{'inststatus'})) {
                   4641:                         if (defined($entries{$fields{'inststatus'}})) {
                   4642:                             $inststatus=$entries{$fields{'inststatus'}};
                   4643:                         }
1.1       raeburn  4644:                     }
                   4645:                     # determine user password
1.184.4.4  raeburn  4646:                     my $password;
                   4647:                     my $passwdfromfile;
1.1       raeburn  4648:                     if (defined($fields{'ipwd'})) {
                   4649:                         if ($entries{$fields{'ipwd'}}) {
                   4650:                             $password=$entries{$fields{'ipwd'}};
1.184.4.4  raeburn  4651:                             $passwdfromfile = 1;
                   4652:                             if ($env{'form.login'} eq 'int') {
                   4653:                                 my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
                   4654:                                 if (($uhome eq 'no_host') || ($changeauth)) {
                   4655:                                     my @brokepwdrules =
                   4656:                                         &Apache::loncommon::check_passwd_rules($domain,$password);
                   4657:                                     if (@brokepwdrules) {
                   4658:                                         $disallow{$counter} = &mt('[_1]: Password included in file for this user did not meet requirements.',
                   4659:                                                                   '<b>'.$username.'</b>');
                   4660:                                         map { $showpasswdrules{$_} = 1; } @brokepwdrules;
                   4661:                                         next;
                   4662:                                     }
                   4663:                                 }
                   4664:                             }
1.1       raeburn  4665:                         }
                   4666:                     }
1.184.4.4  raeburn  4667:                     unless ($passwdfromfile) {
                   4668:                         if ($env{'form.login'} eq 'int') {
                   4669:                             if (@genpwdfail) {
                   4670:                                 my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
                   4671:                                 if (($uhome eq 'no_host') || ($changeauth)) {
                   4672:                                     $disallow{$counter} = &mt('[_1]: No specific password in file for this user; default password did not meet requirements',
                   4673:                                                               '<b>'.$username.'</b>');
                   4674:                                     unless ($haspasswdmap) {
                   4675:                                         map { $showpasswdrules{$_} = 1; } @genpwdfail;
                   4676:                                         $haspasswdmap = 1;
                   4677:                                     }
                   4678:                                 }
                   4679:                                 next;
                   4680:                             }
                   4681:                         }
                   4682:                         $password = $genpwd;
                   4683:                     }
1.1       raeburn  4684:                     # determine user role
                   4685:                     my $role = '';
                   4686:                     if (defined($fields{'role'})) {
                   4687:                         if ($entries{$fields{'role'}}) {
1.42      raeburn  4688:                             $entries{$fields{'role'}}  =~ s/(\s+$|^\s+)//g;
                   4689:                             if ($entries{$fields{'role'}} ne '') {
                   4690:                                 if (grep(/^\Q$entries{$fields{'role'}}\E$/,@permitted_roles)) {
                   4691:                                     $role = $entries{$fields{'role'}};
1.27      raeburn  4692:                                 }
                   4693:                             }
                   4694:                             if ($role eq '') {
                   4695:                                 my $rolestr = join(', ',@permitted_roles);
1.171     raeburn  4696:                                 $disallow{$counter} =
                   4697:                                     &mt('[_1]: You do not have permission to add the requested role [_2] for the user.'
                   4698:                                         ,'<b>'.$entries{$fields{'username'}}.'</b>'
                   4699:                                         ,$entries{$fields{'role'}})
                   4700:                                         .'<br />'
                   4701:                                         .&mt('Allowable role(s) is/are: [_1].',$rolestr);
1.1       raeburn  4702:                                 next;
                   4703:                             }
                   4704:                         }
                   4705:                     }
                   4706:                     if ($role eq '') {
                   4707:                         $role = $defaultrole;
                   4708:                     }
                   4709:                     # Clean up whitespace
1.129     raeburn  4710:                     foreach (\$id,\$fname,\$mname,\$lname,\$gen,\$inststatus) {
1.1       raeburn  4711:                         $$_ =~ s/(\s+$|^\s+)//g;
                   4712:                     }
1.150     raeburn  4713:                     my $credits;
                   4714:                     if ($showcredits) {
                   4715:                         if (($role eq 'st') && ($crstype ne 'Community')) {
                   4716:                             $credits = $entries{$fields{'credits'}};
                   4717:                             if ($credits ne '') {
                   4718:                                 $credits =~ s/[^\d\.]//g;
                   4719:                             }
                   4720:                             if ($credits eq '') {
                   4721:                                 $credits = $commoncredits;
                   4722:                             }
                   4723:                             if ($credits eq $defaultcredits) {
                   4724:                                 undef($credits);
                   4725:                             }
                   4726:                         }
                   4727:                     }
1.5       raeburn  4728:                     # check against rules
                   4729:                     my $checkid = 0;
                   4730:                     my $newuser = 0;
1.57      raeburn  4731:                     my $uhome=&Apache::lonnet::homeserver($username,$userdomain);
1.5       raeburn  4732:                     if ($uhome eq 'no_host') {
1.135     raeburn  4733:                         if ($userdomain ne $newuserdom) {
                   4734:                             if ($context eq 'course') {
1.171     raeburn  4735:                                 $disallow{$counter} =
                   4736:                                     &mt('[_1]: The domain specified ([_2]) is different to that of the course.',
                   4737:                                        '<b>'.$username.'</b>',$userdomain);
1.135     raeburn  4738:                             } elsif ($context eq 'author') {
1.171     raeburn  4739:                                 $disallow{$counter} =
                   4740:                                     &mt('[_1]: The domain specified ([_2]) is different to that of the author.',
                   4741:                                         '<b>'.$username.'</b>',$userdomain); 
1.135     raeburn  4742:                             } else {
1.171     raeburn  4743:                                 $disallow{$counter} =
                   4744:                                     &mt('[_1]: The domain specified ([_2]) is different to that of your current role.',
                   4745:                                         '<b>'.$username.'</b>',$userdomain);
1.135     raeburn  4746:                             }
1.171     raeburn  4747:                             $disallow{$counter} .=
                   4748:                                 &mt('The user does not already exist, and you may not create a new user in a different domain.');
1.124     raeburn  4749:                             next;
1.171     raeburn  4750:                         } else {
                   4751:                             unless ($password || $env{'form.login'} eq 'loc') {
                   4752:                                 $disallow{$counter} =
                   4753:                                     &mt('[_1]: This is a new user but no default password was provided, and the authentication type requires one.',
                   4754:                                         '<b>'.$username.'</b>');
                   4755:                                 next;
                   4756:                             }
1.124     raeburn  4757:                         }
1.5       raeburn  4758:                         $checkid = 1;
                   4759:                         $newuser = 1;
1.172     raeburn  4760:                         $checkuname{$username.':'.$newuserdom} = { 'newuser' => $newuser, 'id' => $id };
1.13      raeburn  4761:                     } else {
1.27      raeburn  4762:                         if ($context eq 'course' || $context eq 'author') {
1.57      raeburn  4763:                             if ($userdomain eq $domain ) {
                   4764:                                 if ($role eq '') {
                   4765:                                     my @checkroles;
                   4766:                                     foreach my $role (@poss_roles) {
                   4767:                                         my $endkey;
                   4768:                                         if ($role ne 'st') {
                   4769:                                             $endkey = ':'.$role;
                   4770:                                         }
                   4771:                                         if (exists($userlist{$username.':'.$userdomain.$endkey})) {
                   4772:                                             if (!grep(/^\Q$role\E$/,@checkroles)) {
                   4773:                                                 push(@checkroles,$role);
                   4774:                                             }
                   4775:                                         }
1.27      raeburn  4776:                                     }
1.57      raeburn  4777:                                     if (@checkroles > 0) {
                   4778:                                         %canmodify = &can_modify_userinfo($context,$domain,\@userinfo,\@checkroles);
1.27      raeburn  4779:                                     }
1.57      raeburn  4780:                                 } elsif (ref($modifiable_fields{$role}) eq 'HASH') {
                   4781:                                     %canmodify = %{$modifiable_fields{$role}};
1.27      raeburn  4782:                                 }
                   4783:                             }
1.57      raeburn  4784:                             my @newinfo = (\$fname,\$mname,\$lname,\$gen,\$email,\$id);
1.84      raeburn  4785:                             for (my $i=0; $i<@newinfo; $i++) {
1.57      raeburn  4786:                                 if (${$newinfo[$i]} ne '') {
                   4787:                                     if (!$canmodify{$userinfo[$i]}) {
                   4788:                                         ${$newinfo[$i]} = '';
                   4789:                                     }
1.27      raeburn  4790:                                 }
                   4791:                             }
                   4792:                         }
1.171     raeburn  4793:                         if ($id) {
                   4794:                             $existinguser{$userdomain}{$username} = $id;
                   4795:                         }
1.5       raeburn  4796:                     }
1.171     raeburn  4797:                     $userinfo{$counter} = {
                   4798:                                           username   => $username,
                   4799:                                           domain     => $userdomain,
                   4800:                                           fname      => $fname,
                   4801:                                           mname      => $mname,
                   4802:                                           lname      => $lname,
                   4803:                                           gen        => $gen,
                   4804:                                           email      => $email,
                   4805:                                           id         => $id, 
                   4806:                                           password   => $password,
                   4807:                                           inststatus => $inststatus,
                   4808:                                           role       => $role,
                   4809:                                           sections   => \@secs,
                   4810:                                           credits    => $credits,
                   4811:                                           newuser    => $newuser,
                   4812:                                           checkid    => $checkid,
                   4813:                                         };
                   4814:                 }
                   4815:             }
                   4816:         } # end of foreach (@userdata)
                   4817:         if ($counter > -1) {
                   4818:             my $total = $counter + 1;
                   4819:             my %checkids;
1.172     raeburn  4820:             if ((keys(%existinguser)) || (keys(%checkuname))) {
                   4821:                 $r->print(&mt('Please be patient -- checking for institutional data ...'));
                   4822:                 $r->rflush();
                   4823:                 if (keys(%existinguser)) {
                   4824:                     foreach my $dom (keys(%existinguser)) {
                   4825:                         if (ref($existinguser{$dom}) eq 'HASH') {
                   4826:                             my %idhash = &Apache::lonnet::idrget($dom,keys(%{$existinguser{$dom}}));
                   4827:                             foreach my $username (keys(%{$existinguser{$dom}})) {
                   4828:                                 if ($idhash{$username} ne $existinguser{$dom}{$username}) {
                   4829:                                     $checkids{$username.':'.$dom} = {
                   4830:                                                                     'id' => $existinguser{$dom}{$username},
                   4831:                                                                     };
                   4832:                                 }
                   4833:                             }
                   4834:                             if (keys(%checkids)) {
                   4835:                                 &Apache::loncommon::user_rule_check(\%checkids,{ 'id' => 1 },
                   4836:                                                                     \%alerts,\%rulematch,
                   4837:                                                                     \%inst_results,\%curr_rules,
                   4838:                                                                     \%got_rules);
1.171     raeburn  4839:                             }
                   4840:                         }
                   4841:                     }
                   4842:                 }
1.172     raeburn  4843:                 if (keys(%checkuname)) {
                   4844:                     &Apache::loncommon::user_rule_check(\%checkuname,{ 'username' => 1, 'id' => 1, },
                   4845:                                                         \%alerts,\%rulematch,\%inst_results,
                   4846:                                                         \%curr_rules,\%got_rules);
                   4847:                 }
                   4848:                 $r->print(' '.&mt('done').'<br /><br />');
                   4849:                 $r->rflush();
1.171     raeburn  4850:             }
1.172     raeburn  4851:             my %prog_state = &Apache::lonhtmlcommon::Create_PrgWin($r,$total);
1.171     raeburn  4852:             $r->print('<ul>');
                   4853:             for (my $i=0; $i<=$counter; $i++) {
                   4854:                 if ($disallow{$i}) {
                   4855:                     $r->print('<li>'.$disallow{$i}.'</li>');
                   4856:                 } elsif (ref($userinfo{$i}) eq 'HASH') {
                   4857:                     my $password = $userinfo{$i}{'password'}; 
                   4858:                     my $newuser = $userinfo{$i}{'newuser'};
                   4859:                     my $checkid = $userinfo{$i}{'checkid'};
                   4860:                     my $id = $userinfo{$i}{'id'};
                   4861:                     my $role = $userinfo{$i}{'role'};
                   4862:                     my @secs;
                   4863:                     if (ref($userinfo{$i}{'sections'}) eq 'ARRAY') {
                   4864:                         @secs = @{$userinfo{$i}{'sections'}};
                   4865:                     }
                   4866:                     my $fname = $userinfo{$i}{'fname'};
                   4867:                     my $mname = $userinfo{$i}{'mname'}; 
                   4868:                     my $lname = $userinfo{$i}{'lname'};
                   4869:                     my $gen = $userinfo{$i}{'gen'};
                   4870:                     my $email = $userinfo{$i}{'email'};
                   4871:                     my $inststatus = $userinfo{$i}{'inststatus'};
                   4872:                     my $credits = $userinfo{$i}{'credits'};
                   4873:                     my $username = $userinfo{$i}{'username'};
                   4874:                     my $userdomain = $userinfo{$i}{'domain'};
                   4875:                     my $user = $username.':'.$userdomain;
                   4876:                     if ($newuser) {
                   4877:                         if (ref($alerts{'username'}) eq 'HASH') {
                   4878:                             if (ref($alerts{'username'}{$userdomain}) eq 'HASH') {
                   4879:                                 if ($alerts{'username'}{$userdomain}{$username}) {
                   4880:                                     $r->print('<li>'.
                   4881:                                               &mt('[_1]: matches the username format at your institution, but is not known to your directory service.','<b>'.$username.'</b>').'<br />'.
                   4882:                                               &mt('Consequently, the user was not created.').'</li>');
                   4883:                                     next;
                   4884:                                 }
                   4885:                             }
                   4886:                         }
1.172     raeburn  4887:                         if (ref($inst_results{$user}) eq 'HASH') {
                   4888:                             if ($inst_results{$user}{'firstname'} ne '') {
                   4889:                                 $fname = $inst_results{$user}{'firstname'};
                   4890:                             }
                   4891:                             if ($inst_results{$user}{'middlename'} ne '') {
                   4892:                                 $mname = $inst_results{$user}{'middlename'};
                   4893:                             }
                   4894:                             if ($inst_results{$user}{'lasttname'} ne '') {
                   4895:                                 $lname = $inst_results{$user}{'lastname'};
                   4896:                             }
                   4897:                             if ($inst_results{$user}{'permanentemail'} ne '') {
                   4898:                                 $email = $inst_results{$user}{'permanentemail'};
                   4899:                             }
                   4900:                             if ($inst_results{$user}{'id'} ne '') {
                   4901:                                 $id = $inst_results{$user}{'id'};
                   4902:                                 $checkid = 0;
                   4903:                             }
                   4904:                             if (ref($inst_results{$user}{'inststatus'}) eq 'ARRAY') {
                   4905:                                 $inststatus = join(':',@{$inst_results{$user}{'inststatus'}});
                   4906:                             }
                   4907:                         }
                   4908:                         if (($checkid) && ($id ne '')) {
                   4909:                             if (ref($alerts{'id'}) eq 'HASH') {
                   4910:                                 if (ref($alerts{'id'}{$userdomain}) eq 'HASH') {
                   4911:                                     if ($alerts{'id'}{$userdomain}{$username}) {
                   4912:                                         $r->print('<li>'.
                   4913:                                                   &mt('[_1]: has a student/employee ID matching the format at your institution, but the ID is not found by your directory service.',
                   4914:                                                   '<b>'.$username.'</b>').'<br />'.
                   4915:                                                   &mt('Consequently, the user was not created.').'</li>');
                   4916:                                         next;
                   4917:                                     }
                   4918:                                 }
                   4919:                             }
                   4920:                         }
1.171     raeburn  4921:                         my $usertype = 'unofficial';
                   4922:                         if (ref($rulematch{$user}) eq 'HASH') {
                   4923:                             if ($rulematch{$user}{'username'}) {
                   4924:                                 $usertype = 'official';
1.5       raeburn  4925:                             }
                   4926:                         }
1.171     raeburn  4927:                         unless ($cancreate{$usertype}) {
                   4928:                             my $showtype = $longtypes{$usertype};
                   4929:                             $r->print('<li>'.
                   4930:                                       &mt('[_1]: The user does not exist, and you are not permitted to create users of type: [_2].','<b>'.$username.'</b>',$showtype).'</li>');
                   4931:                             next;
                   4932:                         }
1.172     raeburn  4933:                     } elsif ($id ne '') {
1.171     raeburn  4934:                         if (exists($checkids{$user})) {
                   4935:                             $checkid = 1; 
1.5       raeburn  4936:                             if (ref($alerts{'id'}) eq 'HASH') {
1.57      raeburn  4937:                                 if (ref($alerts{'id'}{$userdomain}) eq 'HASH') {
1.172     raeburn  4938:                                     if ($alerts{'id'}{$userdomain}{$username}) {
1.171     raeburn  4939:                                         $r->print('<li>'.
1.172     raeburn  4940:                                                   &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  4941:                                                   '<b>'.$username.'</b>').'<br />'.
1.172     raeburn  4942:                                                   &mt('Consequently, the ID was not changed.').'</li>');
                   4943:                                         $id = '';
1.124     raeburn  4944:                                     }
1.5       raeburn  4945:                                 }
                   4946:                             }
                   4947:                         }
                   4948:                     }
1.171     raeburn  4949:                     my $multiple = 0;
                   4950:                     my ($userresult,$authresult,$roleresult,$idresult);
                   4951:                     my (%userres,%authres,%roleres,%idres);
                   4952:                     my $singlesec = '';
                   4953:                     if ($role eq 'st') {
1.184.4.9  raeburn  4954:                         if (($context eq 'domain') && ($changeauth eq 'Yes') && (!$newuser)) {
                   4955:                             if ((&Apache::lonnet::allowed('mau',$userdomain)) &&
                   4956:                                 (&Apache::lonnet::homeserver($username,$userdomain) ne 'no_host')) {
                   4957:                                 if ((($amode =~ /^krb4|krb5|internal$/) && $password ne '') ||
                   4958:                                      ($amode eq 'localauth')) {
                   4959:                                     $authresult =
                   4960:                                         &Apache::lonnet::modifyuserauth($userdomain,$username,$amode,$password);
                   4961:                                 }
                   4962:                             }
                   4963:                         }
1.171     raeburn  4964:                         my $sec;
                   4965:                         if (ref($userinfo{$i}{'sections'}) eq 'ARRAY') {
1.42      raeburn  4966:                             if (@secs > 0) {
                   4967:                                 $sec = $secs[0];
1.27      raeburn  4968:                             }
1.171     raeburn  4969:                         }
                   4970:                         &modifystudent($userdomain,$username,$cid,$sec,
                   4971:                                        $desiredhost,$context);
                   4972:                         $roleresult =
                   4973:                             &Apache::lonnet::modifystudent
                   4974:                                 ($userdomain,$username,$id,$amode,$password,
                   4975:                                  $fname,$mname,$lname,$gen,$sec,$enddate,
                   4976:                                  $startdate,$env{'form.forceid'},
                   4977:                                  $desiredhost,$email,'manual','',$cid,
                   4978:                                  '',$context,$inststatus,$credits);
                   4979:                         $userresult = $roleresult;
                   4980:                     } else {
                   4981:                         if ($role ne '') { 
                   4982:                             if ($context eq 'course' || $setting eq 'course') {
                   4983:                                 if ($customroles{$role}) {
                   4984:                                     $role = 'cr_'.$env{'user.domain'}.'_'.
                   4985:                                             $env{'user.name'}.'_'.$role;
                   4986:                                 }
                   4987:                                 if (($role ne 'cc') && ($role ne 'co')) { 
                   4988:                                    if (@secs > 1) {
                   4989:                                         $multiple = 1;
                   4990:                                         foreach my $sec (@secs) {
                   4991:                                             ($userres{$sec},$authres{$sec},$roleres{$sec},$idres{$sec}) =
                   4992:                                             &modifyuserrole($context,$setting,
                   4993:                                                 $changeauth,$cid,$userdomain,$username,
                   4994:                                                 $id,$amode,$password,$fname,
                   4995:                                                 $mname,$lname,$gen,$sec,
                   4996:                                                 $env{'form.forceid'},$desiredhost,
                   4997:                                                 $email,$role,$enddate,
                   4998:                                                 $startdate,$checkid,$inststatus);
1.42      raeburn  4999:                                         }
1.171     raeburn  5000:                                     } elsif (@secs > 0) {
                   5001:                                         $singlesec = $secs[0];
1.27      raeburn  5002:                                     }
                   5003:                                 }
                   5004:                             }
1.184.4.7  raeburn  5005:                         }
                   5006:                         if (!$multiple) {
                   5007:                             ($userresult,$authresult,$roleresult,$idresult) = 
                   5008:                                 &modifyuserrole($context,$setting,
                   5009:                                                 $changeauth,$cid,$userdomain,$username, 
                   5010:                                                 $id,$amode,$password,$fname,
                   5011:                                                 $mname,$lname,$gen,$singlesec,
                   5012:                                                 $env{'form.forceid'},$desiredhost,
                   5013:                                                 $email,$role,$enddate,$startdate,
1.184.4.10.2.  (raeburn 5014:):                                                 $checkid,$inststatus,\%emptyok);
1.27      raeburn  5015:                         }
1.171     raeburn  5016:                     }
                   5017:                     if ($multiple) {
                   5018:                         foreach my $sec (sort(keys(%userres))) {
                   5019:                             $flushc =
1.27      raeburn  5020:                                 &user_change_result($r,$userres{$sec},$authres{$sec},
                   5021:                                                     $roleres{$sec},$idres{$sec},\%counts,$flushc,
1.57      raeburn  5022:                                                     $username,$userdomain,\%userchg);
1.27      raeburn  5023: 
1.1       raeburn  5024:                         }
                   5025:                     } else {
1.171     raeburn  5026:                         $flushc = 
                   5027:                             &user_change_result($r,$userresult,$authresult,
                   5028:                                                 $roleresult,$idresult,\%counts,$flushc,
                   5029:                                                 $username,$userdomain,\%userchg);
1.1       raeburn  5030:                     }
                   5031:                 }
1.172     raeburn  5032:                 &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,'last user');
1.171     raeburn  5033:             } # end of loop
1.172     raeburn  5034:             $r->print('</ul>');
1.171     raeburn  5035:             &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.184.4.10.2.  (raeburn 5036:):             if (($context eq 'domain') && ($setting eq 'course')) {
                   5037:):                 unless ($oldcrsuserdoms) {
                   5038:):                     if (exists($env{'course.'.$cid.'.internal.userdomains'})) {
                   5039:):                         delete($env{'course.'.$cid.'.internal.userdomains'});
                   5040:):                     }
                   5041:):                 }
                   5042:):             }
1.171     raeburn  5043:         }
1.1       raeburn  5044:         # Flush the course logs so reverse user roles immediately updated
1.126     raeburn  5045:         $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.29      raeburn  5046:         $r->print("</p>\n<p>\n".&mt('Processed [quant,_1,user].',$counts{'user'}).
1.1       raeburn  5047:                   "</p>\n");
                   5048:         if ($counts{'role'} > 0) {
                   5049:             $r->print("<p>\n".
1.184.4.1  raeburn  5050:                       &mt('Roles added for [quant,_1,user].',$counts{'role'}).' '.
                   5051:                       &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.').
                   5052:                       "</p>\n");
1.29      raeburn  5053:         } else {
                   5054:             $r->print('<p>'.&mt('No roles added').'</p>');
1.1       raeburn  5055:         }
                   5056:         if ($counts{'auth'} > 0) {
                   5057:             $r->print("<p>\n".
                   5058:                       &mt('Authentication changed for [_1] existing users.',
                   5059:                           $counts{'auth'})."</p>\n");
                   5060:         }
1.13      raeburn  5061:         $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.184.4.4  raeburn  5062:         $r->print(&passwdrule_alerts($domain,\%showpasswdrules));
1.1       raeburn  5063:         #####################################
1.29      raeburn  5064:         # Display list of students to drop  #
1.1       raeburn  5065:         #####################################
                   5066:         if ($env{'form.fullup'} eq 'yes') {
1.29      raeburn  5067:             $r->print('<h3>'.&mt('Students to Drop')."</h3>\n");
1.1       raeburn  5068:             #  Get current classlist
1.30      raeburn  5069:             my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  5070:             if (! defined($classlist)) {
1.184.4.1  raeburn  5071:                 $r->print('<p class="LC_info">'.
                   5072:                           &mt('There are no students with current/future access to the course.').
                   5073:                           '</p>'."\n");
1.66      raeburn  5074:             } elsif (ref($classlist) eq 'HASH') {
1.1       raeburn  5075:                 # Remove the students we just added from the list of students.
1.30      raeburn  5076:                 foreach my $line (@userdata) {
                   5077:                     my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  5078:                     unless (($entries{$fields{'username'}} eq '') ||
                   5079:                             (!defined($entries{$fields{'username'}}))) {
                   5080:                         delete($classlist->{$entries{$fields{'username'}}.
                   5081:                                                 ':'.$domain});
                   5082:                     }
                   5083:                 }
                   5084:                 # Print out list of dropped students.
1.30      raeburn  5085:                 &show_drop_list($r,$classlist,'nosort',$permission);
1.1       raeburn  5086:             }
                   5087:         }
                   5088:     } # end of unless
1.184.4.1  raeburn  5089:     return 'ok';
1.1       raeburn  5090: }
                   5091: 
1.13      raeburn  5092: sub print_namespacing_alerts {
                   5093:     my ($domain,$alerts,$curr_rules) = @_;
                   5094:     my $output;
                   5095:     if (ref($alerts) eq 'HASH') {
                   5096:         if (keys(%{$alerts}) > 0) {
                   5097:             if (ref($alerts->{'username'}) eq 'HASH') {
                   5098:                 foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
                   5099:                     my $count;
                   5100:                     if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
                   5101:                         $count = keys(%{$alerts->{'username'}{$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:                                         'username',$domdesc,$count,'upload');
                   5107:                     }
                   5108:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   5109:                                    $domdesc,$curr_rules->{$dom}{'username'},
                   5110:                                    'username');
                   5111:                 }
                   5112:             }
                   5113:             if (ref($alerts->{'id'}) eq 'HASH') {
                   5114:                 foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
                   5115:                     my $count;
                   5116:                     if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
                   5117:                         $count = keys(%{$alerts->{'id'}{$dom}});
                   5118:                     }
                   5119:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   5120:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   5121:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   5122:                                               'id',$domdesc,$count,'upload');
                   5123:                     }
                   5124:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   5125:                                     $domdesc,$curr_rules->{$dom}{'id'},'id');
                   5126:                 }
                   5127:             }
                   5128:         }
                   5129:     }
                   5130: }
                   5131: 
1.184.4.4  raeburn  5132: sub passwdrule_alerts {
                   5133:     my ($domain,$passwdrules) = @_;
                   5134:     my $warning;
                   5135:     if (ref($passwdrules) eq 'HASH') {
                   5136:         my %showrules = %{$passwdrules};
                   5137:         if (keys(%showrules)) {
                   5138:             my %passwdconf = &Apache::lonnet::get_passwdconf($domain);
                   5139:             $warning = '<b>'.&mt('Password requirement(s) unmet for one or more users:').'</b><ul>';
                   5140:             if ($showrules{'min'}) {
1.184.4.10  raeburn  5141:                 my $min = $passwdconf{'min'};
                   5142:                 if ($min eq '') {
                   5143:                     $min = $Apache::lonnet::passwdmin;
                   5144:                 }
                   5145:                 $warning .= '<li>'.&mt('minimum [quant,_1,character]',$min).'</li>';
1.184.4.4  raeburn  5146:             }
                   5147:             if ($showrules{'max'}) {
                   5148:                 $warning .= '<li>'.&mt('maximum [quant,_1,character]',$passwdconf{'max'}).'</li>';
                   5149:             }
                   5150:             if ($showrules{'uc'}) {
                   5151:                 $warning .= '<li>'.&mt('contain at least one upper case letter').'</li>';
                   5152:             }
                   5153:             if ($showrules{'lc'}) {
                   5154:                 $warning .= '<li>'.&mt('contain at least one lower case letter').'</li>';
                   5155:             }
                   5156:             if ($showrules{'num'}) {
                   5157:                 $warning .= '<li>'.&mt('contain at least one number').'</li>';
                   5158:             }
                   5159:             if ($showrules{'spec'}) {
                   5160:                 $warning .= '<li>'.&mt('contain at least one non-alphanumeric').'</li>';
                   5161:             }
                   5162:             $warning .= '</ul>';
                   5163:         }
                   5164:     }
                   5165:     return $warning;
                   5166: }
                   5167: 
1.1       raeburn  5168: sub user_change_result {
1.29      raeburn  5169:     my ($r,$userresult,$authresult,$roleresult,$idresult,$counts,$flushc,
1.57      raeburn  5170:         $username,$userdomain,$userchg) = @_;
1.1       raeburn  5171:     my $okresult = 0;
1.171     raeburn  5172:     my @status;
1.1       raeburn  5173:     if ($userresult ne 'ok') {
                   5174:         if ($userresult =~ /^error:(.+)$/) {
                   5175:             my $error = $1;
1.171     raeburn  5176:             push(@status,
                   5177:                  &mt('[_1]: Unable to add/modify: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  5178:         }
                   5179:     } else {
                   5180:         $counts->{'user'} ++;
                   5181:         $okresult = 1;
                   5182:     }
                   5183:     if ($authresult ne 'ok') {
                   5184:         if ($authresult =~ /^error:(.+)$/) {
                   5185:             my $error = $1;
1.171     raeburn  5186:             push(@status, 
                   5187:                  &mt('[_1]: Unable to modify authentication: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  5188:         } 
                   5189:     } else {
                   5190:         $counts->{'auth'} ++;
                   5191:         $okresult = 1;
                   5192:     }
                   5193:     if ($roleresult ne 'ok') {
                   5194:         if ($roleresult =~ /^error:(.+)$/) {
                   5195:             my $error = $1;
1.171     raeburn  5196:             push(@status,
                   5197:                  &mt('[_1]: Unable to add role: [_2]','<b>'.$username.':'.$userdomain.'</b>',$error));
1.1       raeburn  5198:         }
                   5199:     } else {
                   5200:         $counts->{'role'} ++;
                   5201:         $okresult = 1;
                   5202:     }
                   5203:     if ($okresult) {
                   5204:         $flushc++;
1.57      raeburn  5205:         $userchg->{$username.':'.$userdomain}=1;
1.1       raeburn  5206:         if ($flushc>15) {
                   5207:             $r->rflush;
                   5208:             $flushc=0;
                   5209:         }
                   5210:     }
1.29      raeburn  5211:     if ($idresult) {
1.171     raeburn  5212:         push(@status,$idresult);
                   5213:     }
                   5214:     if (@status) {
                   5215:         $r->print('<li>'.join('<br />',@status).'</li>');
1.29      raeburn  5216:     }
1.1       raeburn  5217:     return $flushc;
                   5218: }
                   5219: 
                   5220: # ========================================================= Menu Phase Two Drop
1.17      raeburn  5221: sub print_drop_menu {
1.101     raeburn  5222:     my ($r,$context,$permission,$crstype) = @_;
                   5223:     my $heading;
                   5224:     if ($crstype eq 'Community') {
                   5225:         $heading = &mt("Drop Members");
                   5226:     } else {
                   5227:         $heading = &mt("Drop Students");
                   5228:     }
                   5229:     $r->print('<h3>'.$heading.'</h3>'."\n".
1.153     bisitz   5230:               '<form name="studentform" method="post" action="">'."\n");
1.30      raeburn  5231:     my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  5232:     if (! defined($classlist)) {
1.143     bisitz   5233:         my $msg = '';
1.101     raeburn  5234:         if ($crstype eq 'Community') {
1.143     bisitz   5235:             $msg = &mt('There are no members currently enrolled.');
1.101     raeburn  5236:         } else {
1.143     bisitz   5237:             $msg = &mt('There are no students currently enrolled.');
1.101     raeburn  5238:         }
1.143     bisitz   5239:         $r->print('<p class="LC_info">'.$msg."</p>\n");
1.30      raeburn  5240:     } else {
1.101     raeburn  5241:         &show_drop_list($r,$classlist,'nosort',$permission,$crstype);
1.1       raeburn  5242:     }
1.162     bisitz   5243:     $r->print('</form>');
1.1       raeburn  5244:     return;
                   5245: }
                   5246: 
                   5247: # ================================================================== Phase four
                   5248: 
1.11      raeburn  5249: sub update_user_list {
1.118     raeburn  5250:     my ($r,$context,$setting,$choice,$crstype) = @_;
1.11      raeburn  5251:     my $now = time;
1.1       raeburn  5252:     my $count=0;
1.101     raeburn  5253:     if ($context eq 'course') {
                   5254:         $crstype = &Apache::loncommon::course_type();
                   5255:     }
1.11      raeburn  5256:     my @changelist;
1.29      raeburn  5257:     if ($choice eq 'drop') {
                   5258:         @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
                   5259:     } else {
1.11      raeburn  5260:         @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
                   5261:     }
                   5262:     my %result_text = ( ok    => { 'revoke'   => 'Revoked',
                   5263:                                    'delete'   => 'Deleted',
                   5264:                                    'reenable' => 'Re-enabled',
1.17      raeburn  5265:                                    'activate' => 'Activated',
                   5266:                                    'chgdates' => 'Changed Access Dates for',
1.118     raeburn  5267:                                    'chgsec'   => 'Changed section(s) for',
1.17      raeburn  5268:                                    'drop'     => 'Dropped',
1.11      raeburn  5269:                                  },
                   5270:                         error => {'revoke'    => 'revoking',
                   5271:                                   'delete'    => 'deleting',
                   5272:                                   'reenable'  => 're-enabling',
                   5273:                                   'activate'  => 'activating',
1.17      raeburn  5274:                                   'chgdates'  => 'changing access dates for',
                   5275:                                   'chgsec'    => 'changing section for',
                   5276:                                   'drop'      => 'dropping',
1.11      raeburn  5277:                                  },
                   5278:                       );
                   5279:     my ($startdate,$enddate);
                   5280:     if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
                   5281:         ($startdate,$enddate) = &get_dates_from_form();
                   5282:     }
                   5283:     foreach my $item (@changelist) {
1.118     raeburn  5284:         my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,
                   5285:             @sections,$scopestem,$singlesec,$showsecs,$warn_singlesec,
1.174     raeburn  5286:             $nothingtodo,$keepnosection,$credits,$instsec);
1.17      raeburn  5287:         if ($choice eq 'drop') {
                   5288:             ($uname,$udom,$sec) = split(/:/,$item,-1);
                   5289:             $role = 'st';
                   5290:             $cid = $env{'request.course.id'};
                   5291:             $scopestem = '/'.$cid;
                   5292:             $scopestem =~s/\_/\//g;
                   5293:             if ($sec eq '') {
                   5294:                 $scope = $scopestem;
                   5295:             } else {
                   5296:                 $scope = $scopestem.'/'.$sec;
                   5297:             }
                   5298:         } elsif ($context eq 'course') {
1.174     raeburn  5299:             ($uname,$udom,$role,$sec,$type,$locktype,$credits,$instsec) =
                   5300:                 split(/\:/,$item,8);
                   5301:             $instsec = &unescape($instsec);
1.11      raeburn  5302:             $cid = $env{'request.course.id'};
                   5303:             $scopestem = '/'.$cid;
                   5304:             $scopestem =~s/\_/\//g;
                   5305:             if ($sec eq '') {
                   5306:                 $scope = $scopestem;
                   5307:             } else {
                   5308:                 $scope = $scopestem.'/'.$sec;
                   5309:             }
1.13      raeburn  5310:         } elsif ($context eq 'author') {
1.11      raeburn  5311:             ($uname,$udom,$role) = split(/\:/,$item,-1);
                   5312:             $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                   5313:         } elsif ($context eq 'domain') {
                   5314:             if ($setting eq 'domain') {
                   5315:                 ($role,$uname,$udom) = split(/\:/,$item,-1);
                   5316:                 $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn  5317:             } elsif ($setting eq 'author') { 
1.11      raeburn  5318:                 ($uname,$udom,$role,$scope) = split(/\:/,$item);
                   5319:             } elsif ($setting eq 'course') {
1.174     raeburn  5320:                 ($uname,$udom,$role,$cid,$sec,$type,$locktype,$credits,$instsec) = 
                   5321:                     split(/\:/,$item,9);
                   5322:                 $instsec = &unescape($instsec);
1.11      raeburn  5323:                 $scope = '/'.$cid;
                   5324:                 $scope =~s/\_/\//g;
                   5325:                 if ($sec ne '') {
                   5326:                     $scope .= '/'.$sec;
                   5327:                 }
                   5328:             }
                   5329:         }
1.101     raeburn  5330:         my $plrole = &Apache::lonnet::plaintext($role,$crstype);
1.11      raeburn  5331:         my $start = $env{'form.'.$item.'_start'};
                   5332:         my $end = $env{'form.'.$item.'_end'};
1.17      raeburn  5333:         if ($choice eq 'drop') {
                   5334:             # drop students
                   5335:             $end = $now;
                   5336:             $type = 'manual';
                   5337:             $result =
1.52      raeburn  5338:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context);
1.17      raeburn  5339:         } elsif ($choice eq 'revoke') {
                   5340:             # revoke or delete user role
1.11      raeburn  5341:             $end = $now; 
                   5342:             if ($role eq 'st') {
                   5343:                 $result = 
1.174     raeburn  5344:                     &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.11      raeburn  5345:             } else {
                   5346:                 $result = 
1.52      raeburn  5347:                     &Apache::lonnet::revokerole($udom,$uname,$scope,$role,
                   5348:                                                 '','',$context);
1.11      raeburn  5349:             }
                   5350:         } elsif ($choice eq 'delete') {
                   5351:             if ($role eq 'st') {
1.174     raeburn  5352:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$now,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.29      raeburn  5353:             }
                   5354:             $result =
                   5355:                 &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
1.52      raeburn  5356:                                             $start,1,'',$context);
1.11      raeburn  5357:         } else {
                   5358:             #reenable, activate, change access dates or change section
                   5359:             if ($choice ne 'chgsec') {
                   5360:                 $start = $startdate; 
                   5361:                 $end = $enddate;
                   5362:             }
                   5363:             if ($choice eq 'reenable') {
                   5364:                 if ($role eq 'st') {
1.174     raeburn  5365:                     $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  5366:                 } else {
                   5367:                     $result = 
                   5368:                         &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  5369:                                                     $now,'','',$context);
1.11      raeburn  5370:                 }
                   5371:             } elsif ($choice eq 'activate') {
                   5372:                 if ($role eq 'st') {
1.174     raeburn  5373:                     $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  5374:                 } else {
                   5375:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  5376:                                             $now,'','',$context);
1.11      raeburn  5377:                 }
                   5378:             } elsif ($choice eq 'chgdates') {
                   5379:                 if ($role eq 'st') {
1.174     raeburn  5380:                     $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  5381:                 } else {
                   5382:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
1.52      raeburn  5383:                                                 $start,'','',$context);
1.11      raeburn  5384:                 }
                   5385:             } elsif ($choice eq 'chgsec') {
                   5386:                 my (@newsecs,$revresult,$nochg,@retained);
1.103     raeburn  5387:                 if (($role ne 'cc') && ($role ne 'co')) {
1.117     raeburn  5388:                     my @secs = sort(split(/,/,$env{'form.newsecs'}));
                   5389:                     if (@secs) {
                   5390:                         my %curr_groups = &Apache::longroup::coursegroups();
                   5391:                         foreach my $sec (@secs) {
                   5392:                             next if (($sec =~ /\W/) || ($sec eq 'none') ||
                   5393:                             (exists($curr_groups{$sec})));
                   5394:                             push(@newsecs,$sec);
                   5395:                         }
                   5396:                     }
1.11      raeburn  5397:                 }
                   5398:                 # remove existing section if not to be retained.   
1.118     raeburn  5399:                 if (!$env{'form.retainsec'} || ($role eq 'st')) {
1.11      raeburn  5400:                     if ($sec eq '') {
                   5401:                         if (@newsecs == 0) {
1.118     raeburn  5402:                             $result = 'ok';
1.11      raeburn  5403:                             $nochg = 1;
1.118     raeburn  5404:                             $nothingtodo = 1;
1.40      raeburn  5405:                         } else {
                   5406:                             $revresult =
                   5407:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  5408:                                                             $scope,$role,
                   5409:                                                             '','',$context);
1.40      raeburn  5410:                         } 
1.11      raeburn  5411:                     } else {
1.28      raeburn  5412:                         if (@newsecs > 0) {
                   5413:                             if (grep(/^\Q$sec\E$/,@newsecs)) {
                   5414:                                 push(@retained,$sec);
                   5415:                             } else {
                   5416:                                 $revresult =
                   5417:                                     &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  5418:                                                                 $scope,$role,
                   5419:                                                                 '','',$context);
1.28      raeburn  5420:                             }
                   5421:                         } else {
1.11      raeburn  5422:                             $revresult =
1.28      raeburn  5423:                                 &Apache::lonnet::revokerole($udom,$uname,
1.52      raeburn  5424:                                                             $scope,$role,
                   5425:                                                             '','',$context);
1.11      raeburn  5426:                         }
                   5427:                     }
                   5428:                 } else {
1.28      raeburn  5429:                     if ($sec eq '') {
                   5430:                         $nochg = 1;
1.118     raeburn  5431:                         $keepnosection = 1;
                   5432:                     } else {
1.28      raeburn  5433:                         push(@retained,$sec);
                   5434:                     }
1.11      raeburn  5435:                 }
                   5436:                 # add new sections
1.118     raeburn  5437:                 my (@diffs,@shownew);
                   5438:                 if (@retained) {
                   5439:                     @diffs = &Apache::loncommon::compare_arrays(\@retained,\@newsecs);
                   5440:                 } else {
                   5441:                     @diffs = @newsecs;
                   5442:                 }
1.11      raeburn  5443:                 if (@newsecs == 0) {
1.118     raeburn  5444:                     if ($nochg) {
                   5445:                         $result = 'ok';
                   5446:                         $nothingtodo = 1;
                   5447:                     } else {
1.28      raeburn  5448:                         if ($role eq 'st') {
                   5449:                             $result = 
1.174     raeburn  5450:                                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid,'',$context,$credits,$instsec);
1.28      raeburn  5451:                         } else {
                   5452:                             my $newscope = $scopestem;
1.52      raeburn  5453:                             $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start,'','',$context);
1.11      raeburn  5454:                         }
                   5455:                     }
1.118     raeburn  5456:                     $showsecs = &mt('No section');
                   5457:                 } elsif (@diffs == 0) {
                   5458:                     $result = 'ok';
                   5459:                     $nothingtodo = 1;
1.11      raeburn  5460:                 } else {
1.118     raeburn  5461:                     foreach my $newsec (@newsecs) {
1.11      raeburn  5462:                         if (!grep(/^\Q$newsec\E$/,@retained)) {
                   5463:                             if ($role eq 'st') {
1.174     raeburn  5464:                                 $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  5465:                                 if (@newsecs > 1) {
                   5466:                                     my $showsingle; 
                   5467:                                     if ($newsec eq '') {
                   5468:                                         $showsingle = &mt('No section');
                   5469:                                     } else {
                   5470:                                         $showsingle = $newsec;
                   5471:                                     }
                   5472:                                     if ($crstype eq 'Community') {
                   5473:                                         $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>');
                   5474:                                     } else { 
                   5475:                                         $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>');
                   5476:                                     }
                   5477:                                     $showsecs = $showsingle; 
                   5478:                                     last;
                   5479:                                 } else {
                   5480:                                     if ($newsec eq '') {
                   5481:                                         $showsecs = &mt('No section');
                   5482:                                     } else {
                   5483:                                         $showsecs = $newsec;
                   5484:                                     }
                   5485:                                 }
1.11      raeburn  5486:                             } else {
                   5487:                                 my $newscope = $scopestem;
                   5488:                                 if ($newsec ne '') {
                   5489:                                    $newscope .= '/'.$newsec;
1.118     raeburn  5490:                                    push(@shownew,$newsec); 
1.11      raeburn  5491:                                 }
                   5492:                                 $result = &Apache::lonnet::assignrole($udom,$uname,
                   5493:                                                         $newscope,$role,$end,$start);
1.118     raeburn  5494:                                 
1.11      raeburn  5495:                             }
                   5496:                         }
                   5497:                     }
                   5498:                 }
1.118     raeburn  5499:                 unless ($role eq 'st') {
                   5500:                     unless ($showsecs) {
                   5501:                         my @tolist = sort(@shownew,@retained);
                   5502:                         if ($keepnosection) {
                   5503:                             push(@tolist,&mt('No section'));
                   5504:                         }
                   5505:                         $showsecs = join(', ',@tolist);
                   5506:                     }
                   5507:                 }
1.11      raeburn  5508:             }
                   5509:         }
1.17      raeburn  5510:         my $extent = $scope;
                   5511:         if ($choice eq 'drop' || $context eq 'course') {
                   5512:             my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
                   5513:             if ($cdesc) {
                   5514:                 $extent = $cdesc;
                   5515:             }
                   5516:         }
1.1       raeburn  5517:         if ($result eq 'ok' || $result eq 'ok:') {
1.118     raeburn  5518:             my $dates;
                   5519:             if (($choice eq 'chgsec') || ($choice eq 'chgdates')) {
                   5520:                 $dates = &dates_feedback($start,$end,$now);
                   5521:             }
                   5522:             if ($choice eq 'chgsec') {
                   5523:                 if ($nothingtodo) {
                   5524:                     $r->print(&mt("Section assignment for role of '[_1]' in [_2] for '[_3]' unchanged.",$plrole,$extent,'<i>'.
                   5525:                           &Apache::loncommon::plainname($uname,$udom).
                   5526:                           '</i>').' ');
                   5527:                     if ($sec eq '') {
                   5528:                         $r->print(&mt('[_1]No section[_2] - [_3]','<b>','</b>',$dates));
                   5529:                     } else {
                   5530:                         $r->print(&mt('Section(s): [_1] - [_2]',
                   5531:                                       '<b>'.$showsecs.'</b>',$dates));
                   5532:                     }
                   5533:                     $r->print('<br />');
                   5534:                 } else {
                   5535:                     $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' to [_4] - [_5]",$plrole,$extent,
                   5536:                         '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
                   5537:                         '<b>'.$showsecs.'</b>',$dates).'<br />');
                   5538:                    $count ++;
                   5539:                }
                   5540:                if ($warn_singlesec) {
                   5541:                    $r->print('<div class="LC_warning">'.$warn_singlesec.'</div>');
                   5542:                }
                   5543:             } elsif ($choice eq 'chgdates') {
                   5544:                 $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]' - [_4]",$plrole,$extent, 
1.121     raeburn  5545:                       '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
1.118     raeburn  5546:                       $dates).'<br />');
                   5547:                $count ++;
                   5548:             } else {
                   5549:                 $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for '[_3]'.",$plrole,$extent,
1.121     raeburn  5550:                       '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>').
1.118     raeburn  5551:                           '<br />');
                   5552:                 $count ++;
                   5553:             }
1.1       raeburn  5554:         } else {
                   5555:             $r->print(
1.118     raeburn  5556:                 &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for '[_3]': [_4].",
                   5557:                     $plrole,$extent,
1.121     raeburn  5558:                     '<i>'.&Apache::loncommon::plainname($uname,$udom).'</i>',
1.118     raeburn  5559:                     $result).'<br />');
1.11      raeburn  5560:         }
                   5561:     }
1.32      raeburn  5562:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n");
1.33      raeburn  5563:     if ($choice eq 'drop') {
                   5564:         $r->print('<input type="hidden" name="action" value="listusers" />'."\n".
                   5565:                   '<input type="hidden" name="Status" value="Active" />'."\n".
                   5566:                   '<input type="hidden" name="showrole" value="st" />'."\n");
                   5567:     } else {
                   5568:         foreach my $item ('action','sortby','roletype','showrole','Status','secfilter','grpfilter') {
                   5569:             if ($env{'form.'.$item} ne '') {
                   5570:                 $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.
                   5571:                           '" />'."\n");
                   5572:             }
1.32      raeburn  5573:         }
                   5574:     }
1.144     bisitz   5575:     $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} [quant,_1,user role,user roles,no user roles].",$count).'</b></p>');
1.11      raeburn  5576:     if ($count > 0) {
1.17      raeburn  5577:         if ($choice eq 'revoke' || $choice eq 'drop') {
1.74      bisitz   5578:             $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.').'</p>');
1.11      raeburn  5579:         }
                   5580:         # Flush the course logs so reverse user roles immediately updated
1.126     raeburn  5581:         $r->register_cleanup(\&Apache::lonnet::flushcourselogs);
1.11      raeburn  5582:     }
                   5583:     if ($env{'form.makedatesdefault'}) {
                   5584:         if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
1.101     raeburn  5585:             $r->print(&make_dates_default($startdate,$enddate,$context,$crstype));
1.1       raeburn  5586:         }
                   5587:     }
1.33      raeburn  5588:     my $linktext = &mt('Display User Lists');
                   5589:     if ($choice eq 'drop') {
                   5590:         $linktext = &mt('Display current class roster');
                   5591:     }
1.144     bisitz   5592:     $r->print(
                   5593:         &Apache::lonhtmlcommon::actionbox(
                   5594:             ['<a href="javascript:document.studentform.submit()">'.$linktext.'</a>'])
                   5595:        .'</form>'."\n");
1.1       raeburn  5596: }
                   5597: 
1.118     raeburn  5598: sub dates_feedback {
                   5599:     my ($start,$end,$now) = @_;
                   5600:     my $dates;
                   5601:     if ($start < $now) {
                   5602:         if ($end == 0) {
1.147     bisitz   5603:             $dates = &mt('role(s) active now; no end date');
1.118     raeburn  5604:         } elsif ($end > $now) {
                   5605:             $dates = &mt('role(s) active now; ends [_1].',&Apache::lonlocal::locallocaltime($end));
                   5606:         } else {
                   5607:             $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
                   5608:         }
                   5609:      } else {
                   5610:         if ($end == 0 || $end > $now) {
                   5611:             $dates = &mt('future role(s); starts: [_1].',&Apache::lonlocal::locallocaltime($start));
                   5612:         } else {
                   5613:             $dates = &mt('role(s) expired: [_1].',&Apache::lonlocal::locallocaltime($end));
                   5614:         }
                   5615:     }
                   5616:     return $dates;
                   5617: }
                   5618: 
1.8       raeburn  5619: sub classlist_drop {
1.29      raeburn  5620:     my ($scope,$uname,$udom,$now) = @_;
1.8       raeburn  5621:     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
1.29      raeburn  5622:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.8       raeburn  5623:         if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
1.63      raeburn  5624:             my %user;
                   5625:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,\%user,$now);
1.8       raeburn  5626:             return &mt('Drop from classlist: [_1]',
                   5627:                        '<b>'.$result.'</b>').'<br />';
                   5628:         }
                   5629:     }
                   5630: }
                   5631: 
                   5632: sub active_student_roles {
                   5633:     my ($cnum,$cdom,$uname,$udom) = @_;
                   5634:     my %roles =
                   5635:         &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   5636:                                       ['future','active'],['st']);
                   5637:     return exists($roles{"$cnum:$cdom:st"});
                   5638: }
                   5639: 
1.1       raeburn  5640: sub section_check_js {
1.8       raeburn  5641:     my $groupslist= &get_groupslist();
1.170     damieng  5642:     my %js_lt = &Apache::lonlocal::texthash(
                   5643:         mayn   => 'may not be used as the name for a section, as it is a reserved word.',
                   5644:         plch   => 'Please choose a different section name.',
                   5645:         mnot   => 'may not be used as a section name, as it is the name of a course group.',
                   5646:         secn   => 'Section names and group names must be distinct. Please choose a different section name.',
                   5647:     );
                   5648:     &js_escape(\%js_lt);
1.1       raeburn  5649:     return <<"END";
                   5650: function validate(caller) {
1.9       raeburn  5651:     var groups = new Array($groupslist);
1.1       raeburn  5652:     var secname = caller.value;
                   5653:     if ((secname == 'all') || (secname == 'none')) {
1.170     damieng  5654:         alert("'"+secname+"' $js_lt{'mayn'}\\n$js_lt{'plch'}");
1.1       raeburn  5655:         return 'error';
                   5656:     }
                   5657:     if (secname != '') {
                   5658:         for (var k=0; k<groups.length; k++) {
                   5659:             if (secname == groups[k]) {
1.170     damieng  5660:                 alert("'"+secname+"' $js_lt{'mnot'}\\n$js_lt{'secn'}");
1.1       raeburn  5661:                 return 'error';
                   5662:             }
                   5663:         }
                   5664:     }
                   5665:     return 'ok';
                   5666: }
                   5667: END
                   5668: }
                   5669: 
                   5670: sub set_login {
                   5671:     my ($dom,$authformkrb,$authformint,$authformloc) = @_;
                   5672:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   5673:     my $response;
                   5674:     my ($authnum,%can_assign) =
                   5675:         &Apache::loncommon::get_assignable_auth($dom);
                   5676:     if ($authnum) {
                   5677:         $response = &Apache::loncommon::start_data_table();
                   5678:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   5679:             $response .= &Apache::loncommon::start_data_table_row().
                   5680:                          '<td>'.$authformkrb.'</td>'.
                   5681:                          &Apache::loncommon::end_data_table_row()."\n";
                   5682:         }
                   5683:         if ($can_assign{'int'}) {
                   5684:             $response .= &Apache::loncommon::start_data_table_row().
                   5685:                          '<td>'.$authformint.'</td>'.
                   5686:                          &Apache::loncommon::end_data_table_row()."\n"
                   5687:         }
                   5688:         if ($can_assign{'loc'}) {
                   5689:             $response .= &Apache::loncommon::start_data_table_row().
                   5690:                          '<td>'.$authformloc.'</td>'.
                   5691:                          &Apache::loncommon::end_data_table_row()."\n";
                   5692:         }
                   5693:         $response .= &Apache::loncommon::end_data_table();
                   5694:     }
                   5695:     return $response;
                   5696: }
                   5697: 
1.8       raeburn  5698: sub course_sections {
1.178     raeburn  5699:     my ($sections_count,$role,$current_sec,$disabled) = @_;
1.8       raeburn  5700:     my $output = '';
1.169     raeburn  5701:     my @sections = (sort {$a <=> $b} keys(%{$sections_count}));
1.29      raeburn  5702:     my $numsec = scalar(@sections);
1.92      bisitz   5703:     my $is_selected = ' selected="selected"';
1.29      raeburn  5704:     if ($numsec <= 1) {
1.178     raeburn  5705:         $output = '<select name="currsec_'.$role.'"'.$disabled.'>'."\n".
1.51      raeburn  5706:                   '  <option value="">'.&mt('Select').'</option>'."\n";
                   5707:         if ($current_sec eq 'none') {
                   5708:             $output .=       
                   5709:                   '  <option value=""'.$is_selected.'>'.&mt('No section').'</option>'."\n";
                   5710:         } else {
                   5711:             $output .=
1.29      raeburn  5712:                   '  <option value="">'.&mt('No section').'</option>'."\n";
1.51      raeburn  5713:         }
1.29      raeburn  5714:         if ($numsec == 1) {
1.51      raeburn  5715:             if ($current_sec eq $sections[0]) {
                   5716:                 $output .=
                   5717:                   '  <option value="'.$sections[0].'"'.$is_selected.'>'.$sections[0].'</option>'."\n";
                   5718:             } else {
                   5719:                 $output .=  
1.8       raeburn  5720:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
1.51      raeburn  5721:             }
1.29      raeburn  5722:         }
1.8       raeburn  5723:     } else {
                   5724:         $output = '<select name="currsec_'.$role.'" ';
                   5725:         my $multiple = 4;
                   5726:         if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
1.29      raeburn  5727:         if ($role eq 'st') {
1.178     raeburn  5728:             $output .= $disabled.'>'."\n".
1.51      raeburn  5729:                        '  <option value="">'.&mt('Select').'</option>'."\n";
                   5730:             if ($current_sec eq 'none') {
                   5731:                 $output .= 
                   5732:                        '  <option value=""'.$is_selected.'>'.&mt('No section')."</option>\n";
                   5733:             } else {
                   5734:                 $output .=
1.29      raeburn  5735:                        '  <option value="">'.&mt('No section')."</option>\n";
1.51      raeburn  5736:             }
1.29      raeburn  5737:         } else {
1.178     raeburn  5738:             $output .= 'multiple="multiple" size="'.$multiple.'"'.$disabled.'>'."\n";
1.29      raeburn  5739:         }
1.8       raeburn  5740:         foreach my $sec (@sections) {
1.51      raeburn  5741:             if ($current_sec eq $sec) {
                   5742:                 $output .= '<option value="'.$sec.'"'.$is_selected.'>'.$sec."</option>\n";
                   5743:             } else {
                   5744:                 $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
                   5745:             }
1.8       raeburn  5746:         }
                   5747:     }
                   5748:     $output .= '</select>';
                   5749:     return $output;
                   5750: }
                   5751: 
                   5752: sub get_groupslist {
                   5753:     my $groupslist;
                   5754:     my %curr_groups = &Apache::longroup::coursegroups();
                   5755:     if (%curr_groups) {
                   5756:         $groupslist = join('","',sort(keys(%curr_groups)));
                   5757:         $groupslist = '"'.$groupslist.'"';
                   5758:     }
1.11      raeburn  5759:     return $groupslist; 
1.8       raeburn  5760: }
                   5761: 
                   5762: sub setsections_javascript {
1.150     raeburn  5763:     my ($formname,$groupslist,$mode,$checkauth,$crstype,$showcredits) = @_;
1.28      raeburn  5764:     my ($checkincluded,$finish,$rolecode,$setsection_js);
                   5765:     if ($mode eq 'upload') {
                   5766:         $checkincluded = 'formname.name == "'.$formname.'"';
                   5767:         $finish = "return 'ok';";
                   5768:         $rolecode = "var role = formname.defaultrole.options[formname.defaultrole.selectedIndex].value;\n";
                   5769:     } elsif ($formname eq 'cu') {
1.150     raeburn  5770:         if (($crstype eq 'Course') && ($showcredits)) {
                   5771:             $checkincluded = "((role == 'st') && (formname.elements[i-2].checked == true)) || ((role != 'st') && (formname.elements[i-1].checked == true))";
                   5772:         } else {
                   5773:             $checkincluded = 'formname.elements[i-1].checked == true';
                   5774:         }
1.37      raeburn  5775:         if ($checkauth) {
                   5776:             $finish = "var authcheck = auth_check();\n".
                   5777:                       "   if (authcheck == 'ok') {\n".
                   5778:                       "       formname.submit();\n".
                   5779:                       "   }\n";
                   5780:         } else {
                   5781:             $finish = 'formname.submit()';
                   5782:         }
1.28      raeburn  5783:         $rolecode = "var match = str.split('_');
                   5784:                 var role = match[3];\n";
1.165     raeburn  5785:     } elsif (($formname eq 'enrollstudent') || ($formname eq 'selfenroll')) {
1.28      raeburn  5786:         $checkincluded = 'formname.name == "'.$formname.'"';
1.37      raeburn  5787:         if ($checkauth) {
                   5788:             $finish = "var authcheck = auth_check();\n".
                   5789:                       "   if (authcheck == 'ok') {\n".
                   5790:                       "       formname.submit();\n".
                   5791:                       "   }\n";
                   5792:         } else {
                   5793:             $finish = 'formname.submit()';
                   5794:         }
1.28      raeburn  5795:         $rolecode = "var match = str.split('_');
                   5796:                 var role = match[1];\n";
1.8       raeburn  5797:     } else {
1.28      raeburn  5798:         $checkincluded = 'formname.name == "'.$formname.'"'; 
1.8       raeburn  5799:         $finish = "seccheck = 'ok';";
1.28      raeburn  5800:         $rolecode = "var match = str.split('_');
                   5801:                 var role = match[1];\n";
1.11      raeburn  5802:         $setsection_js = "var seccheck = 'alert';"; 
1.8       raeburn  5803:     }
                   5804:     my %alerts = &Apache::lonlocal::texthash(
                   5805:                     secd => 'Section designations do not apply to Course Coordinator roles.',
1.103     raeburn  5806:                     sedn => 'Section designations do not apply to Coordinator roles.',
1.8       raeburn  5807:                     accr => 'A course coordinator role will be added with access to all sections.',
1.103     raeburn  5808:                     acor => 'A coordinator role will be added with access to all sections',
1.8       raeburn  5809:                     inea => 'In each course, each user may only have one student role at a time.',
1.125     raeburn  5810:                     inco => 'In each community, each user may only have one member role at a time.',
1.145     raeburn  5811:                     youh => 'You had selected',
1.8       raeburn  5812:                     secs => 'sections.',
                   5813:                     plmo => 'Please modify your selections so they include no more than one section.',
                   5814:                     mayn => 'may not be used as the name for a section, as it is a reserved word.',
                   5815:                     plch => 'Please choose a different section name.',
                   5816:                     mnot => 'may not be used as a section name, as it is the name of a course group.',
                   5817:                     secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.113     raeburn  5818:                     nonw => 'Section names may only contain letters or numbers.',
1.170     damieng  5819:                  );
                   5820:     &js_escape(\%alerts);
1.8       raeburn  5821:     $setsection_js .= <<"ENDSECCODE";
                   5822: 
1.103     raeburn  5823: function setSections(formname,crstype) {
1.8       raeburn  5824:     var re1 = /^currsec_/;
1.113     raeburn  5825:     var re2 =/\\W/;
1.115     raeburn  5826:     var trimleading = /^\\s+/;
                   5827:     var trimtrailing = /\\s+\$/;
1.8       raeburn  5828:     var groups = new Array($groupslist);
                   5829:     for (var i=0;i<formname.elements.length;i++) {
                   5830:         var str = formname.elements[i].name;
1.168     raeburn  5831:         if (typeof(str) === "undefined") {
                   5832:             continue;
                   5833:         }
1.8       raeburn  5834:         var checkcurr = str.match(re1);
                   5835:         if (checkcurr != null) {
1.115     raeburn  5836:             var num = i;
1.150     raeburn  5837:             $rolecode
1.8       raeburn  5838:             if ($checkincluded) {
1.103     raeburn  5839:                 if (role == 'cc' || role == 'co') {
                   5840:                     if (role == 'cc') {
                   5841:                         alert("$alerts{'secd'}\\n$alerts{'accr'}");
                   5842:                     } else {
                   5843:                         alert("$alerts{'sedn'}\\n$alerts{'acor'}");
                   5844:                     }
                   5845:                 } else {
1.8       raeburn  5846:                     var sections = '';
                   5847:                     var numsec = 0;
1.115     raeburn  5848:                     var fromexisting = new Array();
                   5849:                     for (var j=0; j<formname.elements[num].length; j++) {
                   5850:                         if (formname.elements[num].options[j].selected == true ) {
                   5851:                             var addsec = formname.elements[num].options[j].value;
1.119     raeburn  5852:                             if ((addsec != "") && (addsec != null)) {
1.115     raeburn  5853:                                 fromexisting.push(addsec);
1.8       raeburn  5854:                                 if (numsec == 0) {
1.115     raeburn  5855:                                     sections = addsec;
                   5856:                                 } else {
                   5857:                                     sections = sections + "," +  addsec;
1.8       raeburn  5858:                                 }
1.115     raeburn  5859:                                 numsec ++;
1.8       raeburn  5860:                             }
                   5861:                         }
                   5862:                     }
1.115     raeburn  5863:                     var newsecs = formname.elements[num+1].value;
1.113     raeburn  5864:                     var validsecs = new Array();
1.115     raeburn  5865:                     var validsecstr = '';
1.113     raeburn  5866:                     var badsecs = new Array();
1.8       raeburn  5867:                     if (newsecs != null && newsecs != "") {
1.115     raeburn  5868:                         var numsplit;
                   5869:                         if (newsecs.indexOf(',') == -1) {
                   5870:                             numsplit = new Array(newsecs);
                   5871:                         } else {
                   5872:                             numsplit = newsecs.split(/,/g);
                   5873:                         }
1.117     raeburn  5874:                         for (var m=0; m<numsplit.length; m++) {
                   5875:                             var newsec = numsplit[m];
1.115     raeburn  5876:                             newsec = newsec.replace(trimleading,'');
                   5877:                             newsec = newsec.replace(trimtrailing,'');
                   5878:                             if (re2.test(newsec) == true) {
                   5879:                                 badsecs.push(newsec);
1.113     raeburn  5880:                             } else {
1.115     raeburn  5881:                                 if (newsec != '') {
                   5882:                                     var isnew = 1;
                   5883:                                     if (fromexisting != null) {
1.117     raeburn  5884:                                         for (var n=0; n<fromexisting.length; n++) {
                   5885:                                             if (newsec == fromexisting[n]) {
1.115     raeburn  5886:                                                 isnew = 0;
                   5887:                                             }
                   5888:                                         }
                   5889:                                     }
                   5890:                                     if (isnew == 1) {
                   5891:                                         validsecs.push(newsec);
                   5892:                                     }
                   5893:                                 }
1.113     raeburn  5894:                             }
                   5895:                         }
                   5896:                         if (badsecs.length > 0) {
                   5897:                             alert("$alerts{'nonw'}\\n$alerts{'plch'}");
                   5898:                             return;
                   5899:                         }
                   5900:                         numsec = numsec + validsecs.length;
1.8       raeburn  5901:                     }
                   5902:                     if ((role == 'st') && (numsec > 1)) {
1.103     raeburn  5903:                         if (crstype == 'Community') {
                   5904:                             alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5905:                         } else {
                   5906:                             alert("$alerts{'inco'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}");
                   5907:                         }
1.8       raeburn  5908:                         return;
1.115     raeburn  5909:                     } else {
                   5910:                         if (validsecs != null) {
                   5911:                             for (var j=0; j<validsecs.length; j++) {
                   5912:                                 if (validsecstr == '' || validsecstr == null) {
                   5913:                                     validsecstr = validsecs[j];
                   5914:                                 } else {
                   5915:                                     validsecstr += ','+validsecs[j];
                   5916:                                 }
                   5917:                                 if ((validsecs[j] == 'all') ||
                   5918:                                     (validsecs[j] == 'none')) {
                   5919:                                     alert("'"+validsecs[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
1.8       raeburn  5920:                                     return;
                   5921:                                 }
                   5922:                                 for (var k=0; k<groups.length; k++) {
1.115     raeburn  5923:                                     if (validsecs[j] == groups[k]) {
                   5924:                                         alert("'"+validsecs[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
1.8       raeburn  5925:                                         return;
                   5926:                                     }
                   5927:                                 }
                   5928:                             }
                   5929:                         }
                   5930:                     }
1.115     raeburn  5931:                     if ((validsecstr != '') && (validsecstr != null)) {
1.117     raeburn  5932:                         if ((sections == '') || (sections == null)) {
                   5933:                             sections = validsecstr;
                   5934:                         } else {
1.115     raeburn  5935:                             sections = sections + "," + validsecstr;
                   5936:                         }
                   5937:                     }
                   5938:                     formname.elements[num+2].value = sections;
1.8       raeburn  5939:                 }
                   5940:             }
                   5941:         }
                   5942:     }
                   5943:     $finish
                   5944: }
                   5945: ENDSECCODE
1.11      raeburn  5946:     return $setsection_js; 
1.8       raeburn  5947: }
                   5948: 
1.15      raeburn  5949: sub can_create_user {
                   5950:     my ($dom,$context,$usertype) = @_;
                   5951:     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   5952:     my $cancreate = 1;
1.28      raeburn  5953:     if (&Apache::lonnet::allowed('mau',$dom)) {
                   5954:         return $cancreate;
1.178     raeburn  5955:     } elsif ($context eq 'domain') {
                   5956:         $cancreate = 0;
                   5957:         return $cancreate;
1.28      raeburn  5958:     }
1.15      raeburn  5959:     if (ref($domconf{'usercreation'}) eq 'HASH') {
                   5960:         if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
1.100     raeburn  5961:             if ($context eq 'course' || $context eq 'author' || $context eq 'requestcrs') {
1.15      raeburn  5962:                 my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
                   5963:                 if ($creation eq 'none') {
                   5964:                     $cancreate = 0;
                   5965:                 } elsif ($creation ne 'any') {
                   5966:                     if (defined($usertype)) {
                   5967:                         if ($creation ne $usertype) {
                   5968:                             $cancreate = 0;
                   5969:                         }
                   5970:                     }
                   5971:                 }
                   5972:             }
                   5973:         }
                   5974:     }
                   5975:     return $cancreate;
                   5976: }
                   5977: 
1.20      raeburn  5978: sub can_modify_userinfo {
                   5979:     my ($context,$dom,$fields,$userroles) = @_;
                   5980:     my %domconfig =
                   5981:        &Apache::lonnet::get_dom('configuration',['usermodification'],
                   5982:                                 $dom);
1.184.4.10.2.  (raeburn 5983:):     if (($context eq 'author') &&
                   5984:):         ($env{'request.role'} =~ m{^(ca|aa)\./$match_domain/$match_username$})) {
                   5985:):         $context = 'coauthor';
                   5986:):     }
1.20      raeburn  5987:     my %canmodify;
                   5988:     if (ref($fields) eq 'ARRAY') {
                   5989:         foreach my $field (@{$fields}) {
                   5990:             $canmodify{$field}  = 0;
                   5991:             if (&Apache::lonnet::allowed('mau',$dom)) {
                   5992:                 $canmodify{$field} = 1;
                   5993:             } else {
                   5994:                 if (ref($domconfig{'usermodification'}) eq 'HASH') {
                   5995:                     if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
                   5996:                         if (ref($userroles) eq 'ARRAY') {
                   5997:                             foreach my $role (@{$userroles}) {
                   5998:                                 my $testrole;
1.60      raeburn  5999:                                 if ($context eq 'selfcreate') {
                   6000:                                     $testrole = $role;
1.20      raeburn  6001:                                 } else {
1.60      raeburn  6002:                                     if ($role =~ /^cr\//) {
                   6003:                                         $testrole = 'cr';
                   6004:                                     } else {
                   6005:                                         $testrole = $role;
                   6006:                                     }
1.20      raeburn  6007:                                 }
                   6008:                                 if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
                   6009:                                     if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
                   6010:                                         $canmodify{$field} = 1;
                   6011:                                         last;
                   6012:                                     }
                   6013:                                 }
                   6014:                             }
                   6015:                         } else {
                   6016:                             foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
                   6017:                                 if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
                   6018:                                     if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
                   6019:                                         $canmodify{$field} = 1;
                   6020:                                         last;
                   6021:                                     }
                   6022:                                 }
                   6023:                             }
                   6024:                         }
                   6025:                     }
                   6026:                 } elsif ($context eq 'course') {
                   6027:                     if (ref($userroles) eq 'ARRAY') {
                   6028:                         if (grep(/^st$/,@{$userroles})) {
                   6029:                             $canmodify{$field} = 1;
                   6030:                         }
                   6031:                     } else {
                   6032:                         $canmodify{$field} = 1;
                   6033:                     }
                   6034:                 }
                   6035:             }
                   6036:         }
                   6037:     }
                   6038:     return %canmodify;
                   6039: }
                   6040: 
1.184.4.6  raeburn  6041: sub can_change_internalpass {
                   6042:     my ($uname,$udom,$crstype,$permission) = @_;
                   6043:     my $canchange;
                   6044:     if (&Apache::lonnet::allowed('mau',$udom)) {
                   6045:         $canchange = 1;
                   6046:     } elsif ((ref($permission) eq 'HASH') && ($permission->{'mip'}) &&
                   6047:              ($udom eq $env{'request.role.domain'})) {
                   6048:         unless ($env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'}) {
                   6049:             my ($cnum,$cdom) = &get_course_identity();
                   6050:             if ((&Apache::lonnet::is_course_owner($cdom,$cnum)) && ($udom eq $env{'user.domain'})) {
                   6051:                 my @userstatuses = ('default');
                   6052:                 my %userenv = &Apache::lonnet::userenvironment($udom,$uname,'inststatus');
                   6053:                 if ($userenv{'inststatus'} ne '') {
                   6054:                     @userstatuses =  split(/:/,$userenv{'inststatus'});
                   6055:                 }
                   6056:                 my $noupdate = 1;
                   6057:                 my %passwdconf = &Apache::lonnet::get_passwdconf($cdom);
                   6058:                 if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
                   6059:                     if (ref($passwdconf{'crsownerchg'}{'for'}) eq 'ARRAY') {
                   6060:                         foreach my $status (@userstatuses) {
                   6061:                             if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'for'}})) {
                   6062:                                 undef($noupdate);
                   6063:                                 last;
                   6064:                             }
                   6065:                         }
                   6066:                     }
                   6067:                 }
                   6068:                 if ($noupdate) {
                   6069:                     return;
                   6070:                 }
                   6071:                 my %owned = &Apache::lonnet::courseiddump($cdom,'.',1,'.',
                   6072:                                                           $env{'user.name'}.':'.$env{'user.domain'},
                   6073:                                                           undef,undef,undef,'.');
                   6074:                 my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   6075:                                                               ['active','future']);
                   6076:                 foreach my $key (keys(%roleshash)) {
                   6077:                     my ($name,$domain,$role) = split(/:/,$key);
                   6078:                     if ($role eq 'st') {
                   6079:                         next if (($name eq $cnum) && ($domain eq $cdom));
                   6080:                         if ($owned{$domain.'_'.$name}) {
                   6081:                             if (ref($owned{$domain.'_'.$name}) eq 'HASH') {
                   6082:                                 if ($owned{$domain.'_'.$name}{'nopasswdchg'}) {
                   6083:                                     $noupdate = 1;
                   6084:                                     last;
                   6085:                                 }
                   6086:                             }
                   6087:                         } else {
                   6088:                             $noupdate = 1;
                   6089:                             last;
                   6090:                         }
                   6091:                     } else {
                   6092:                         $noupdate = 1;
                   6093:                         last;
                   6094:                     }
                   6095:                 }
                   6096:                 unless ($noupdate) {
                   6097:                     $canchange = 1;
                   6098:                 }
                   6099:             }
                   6100:         }
                   6101:     }
                   6102:     return $canchange;
                   6103: }
                   6104: 
1.18      raeburn  6105: sub check_usertype {
1.134     raeburn  6106:     my ($dom,$uname,$rules,$curr_rules,$got_rules) = @_;
1.18      raeburn  6107:     my $usertype;
1.134     raeburn  6108:     if ((ref($got_rules) eq 'HASH') && (ref($curr_rules) eq 'HASH')) {
                   6109:         if (!$got_rules->{$dom}) {
                   6110:             my %domconfig = &Apache::lonnet::get_dom('configuration',
                   6111:                                               ['usercreation'],$dom);
                   6112:             if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   6113:                 foreach my $item ('username','id') {
                   6114:                     if (ref($domconfig{'usercreation'}{$item.'_rule'}) eq 'ARRAY') {
                   6115:                         $curr_rules->{$dom}{$item} =
                   6116:                                 $domconfig{'usercreation'}{$item.'_rule'};
                   6117:                     }
                   6118:                 }
                   6119:             }
                   6120:             $got_rules->{$dom} = 1;
                   6121:         }
                   6122:         if (ref($rules) eq 'HASH') {
                   6123:             my @user_rules;
                   6124:             if (ref($curr_rules->{$dom}{'username'}) eq 'ARRAY') {
                   6125:                 foreach my $rule (keys(%{$rules})) {
                   6126:                     if (grep(/^\Q$rule\E/,@{$curr_rules->{$dom}{'username'}})) {
                   6127:                         push(@user_rules,$rule);
                   6128:                     }
                   6129:                 } 
                   6130:             }
                   6131:             if (@user_rules > 0) {
                   6132:                 my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
                   6133:                 if (keys(%rule_check) > 0) {
                   6134:                     $usertype = 'unofficial';
                   6135:                     foreach my $item (keys(%rule_check)) {
                   6136:                         if ($rule_check{$item}) {
                   6137:                             $usertype = 'official';
                   6138:                             last;
                   6139:                         }
1.18      raeburn  6140:                     }
                   6141:                 }
                   6142:             }
                   6143:         }
                   6144:     }
                   6145:     return $usertype;
                   6146: }
                   6147: 
1.17      raeburn  6148: sub roles_by_context {
1.101     raeburn  6149:     my ($context,$custom,$crstype) = @_;
1.17      raeburn  6150:     my @allroles;
                   6151:     if ($context eq 'course') {
1.99      raeburn  6152:         @allroles = ('st');
                   6153:         if ($env{'request.role'} =~ m{^dc\./}) {
                   6154:             push(@allroles,'ad');
                   6155:         }
1.101     raeburn  6156:         push(@allroles,('ta','ep','in'));
                   6157:         if ($crstype eq 'Community') {
                   6158:             push(@allroles,'co');
                   6159:         } else {
                   6160:             push(@allroles,'cc');
                   6161:         }
1.17      raeburn  6162:         if ($custom) {
                   6163:             push(@allroles,'cr');
                   6164:         }
                   6165:     } elsif ($context eq 'author') {
                   6166:         @allroles = ('ca','aa');
                   6167:     } elsif ($context eq 'domain') {
1.182     raeburn  6168:         @allroles = ('li','ad','dg','dh','da','sc','au','dc');
1.17      raeburn  6169:     }
                   6170:     return @allroles;
                   6171: }
                   6172: 
1.16      raeburn  6173: sub get_permission {
1.101     raeburn  6174:     my ($context,$crstype) = @_;
1.16      raeburn  6175:     my %permission;
                   6176:     if ($context eq 'course') {
1.17      raeburn  6177:         my $custom = 1;
1.101     raeburn  6178:         my @allroles = &roles_by_context($context,$custom,$crstype);
1.17      raeburn  6179:         foreach my $role (@allroles) {
                   6180:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   6181:                 $permission{'cusr'} = 1;
                   6182:                 last;
                   6183:             }
1.16      raeburn  6184:         }
                   6185:         if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
                   6186:             $permission{'custom'} = 1;
                   6187:         }
                   6188:         if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
                   6189:             $permission{'view'} = 1;
                   6190:         }
                   6191:         if (!$permission{'view'}) {
                   6192:             my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   6193:             $permission{'view'} =  &Apache::lonnet::allowed('vcl',$scope);
                   6194:             if ($permission{'view'}) {
                   6195:                 $permission{'view_section'} = $env{'request.course.sec'};
                   6196:             }
                   6197:         }
1.17      raeburn  6198:         if (!$permission{'cusr'}) {
                   6199:             if ($env{'request.course.sec'} ne '') {
                   6200:                 my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   6201:                 $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
                   6202:                 if ($permission{'cusr'}) {
                   6203:                     $permission{'cusr_section'} = $env{'request.course.sec'};
                   6204:                 }
                   6205:             }
                   6206:         }
1.16      raeburn  6207:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
                   6208:             $permission{'grp_manage'} = 1;
                   6209:         }
1.165     raeburn  6210:         if ($permission{'cusr'}) {
                   6211:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6212:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   6213:             my %coursehash = (
                   6214:                 'internal.selfenrollmgrdc' => $env{'course.'.$env{'request.course.id'}.'.internal.selfenrollmgrdc'},
                   6215:                 'internal.selfenrollmgrcc' => $env{'course.'.$env{'request.course.id'}.'.internal.selfenrollmgrcc'},
                   6216:                 'internal.coursecode'      => $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'},
                   6217:                 'internal.textbook'        =>$env{'course.'.$env{'request.course.id'}.'.internal.textbook'},
                   6218:             );
                   6219:             my ($managed_by_cc,$managed_by_dc) = &selfenrollment_administration($cdom,$cnum,$crstype,\%coursehash);
                   6220:             if (ref($managed_by_cc) eq 'ARRAY') {
                   6221:                 if (@{$managed_by_cc}) {
                   6222:                     $permission{'selfenrolladmin'} = 1;
                   6223:                 }
                   6224:             }
1.184.4.10.2.  (raeburn 6225:):             unless ($permission{'selfenrolladmin'}) {
                   6226:):                 $permission{'selfenrollview'} = 1;
                   6227:):             }
1.165     raeburn  6228:         }
1.180     raeburn  6229:         if ($env{'request.course.id'}) {
1.184.4.6  raeburn  6230:             my $user;
                   6231:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   6232:                 $user = $env{'user.name'}.':'.$env{'user.domain'};
                   6233:             }
1.180     raeburn  6234:             if (($user ne '') && ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq
                   6235:                                   $user)) {
                   6236:                 $permission{'owner'} = 1;
1.184.4.6  raeburn  6237:                 if (&Apache::lonnet::allowed('mip',$env{'request.course.id'})) {
                   6238:                     $permission{'mip'} = 1;
                   6239:                 }
1.180     raeburn  6240:             } elsif (($user ne '') && ($env{'course.'.$env{'request.course.id'}.'.internal.co-owners'} ne '')) {
                   6241:                 if (grep(/^\Q$user\E$/,split(/,/,$env{'course.'.$env{'request.course.id'}.'.internal.co-owners'}))) {
                   6242:                     $permission{'co-owner'} = 1;
                   6243:                 }
                   6244:             }
                   6245:         }
1.16      raeburn  6246:     } elsif ($context eq 'author') {
1.184.4.10.2.  (raeburn 6247:):         my $audom = $env{'request.role.domain'};
                   6248:):         my $auname = $env{'user.name'};
                   6249:):         if ((&Apache::lonnet::allowed('cca',"$audom/$auname")) ||
                   6250:):             (&Apache::lonnet::allowed('caa',"$audom/$auname"))) {
                   6251:):             $permission{'author'} = 1;
                   6252:):             $permission{'cusr'} = 1;
                   6253:):             $permission{'view'} = 1;
                   6254:):         }
                   6255:):     } elsif ($context eq 'coauthor') {
                   6256:):         my ($audom,$auname) = ($env{'request.role'} =~ m{^ca\./($match_domain)/($match_username)$});
                   6257:):         if ((&Apache::lonnet::allowed('vca',"$audom/$auname")) ||
                   6258:):             (&Apache::lonnet::allowed('vaa',"$audom/$auname"))) {
                   6259:):             if ($env{"environment.internal.manager./$audom/$auname"}) {
                   6260:):                 $permission{'cusr'} = 1;
                   6261:):                 $permission{'view'} = 1;
                   6262:):             }
                   6263:):         }
1.16      raeburn  6264:     } else {
1.17      raeburn  6265:         my @allroles = &roles_by_context($context);
                   6266:         foreach my $role (@allroles) {
1.28      raeburn  6267:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   6268:                 $permission{'cusr'} = 1;
1.17      raeburn  6269:                 last;
                   6270:             }
                   6271:         }
                   6272:         if (!$permission{'cusr'}) {
                   6273:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   6274:                 $permission{'cusr'} = 1;
                   6275:             }
1.16      raeburn  6276:         }
                   6277:         if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
                   6278:             $permission{'custom'} = 1;
                   6279:         }
1.176     raeburn  6280:         if (&Apache::lonnet::allowed('vac',$env{'request.role.domain'})) {
                   6281:             $permission{'activity'} = 1;
                   6282:         }
1.178     raeburn  6283:         if (&Apache::lonnet::allowed('vur',$env{'request.role.domain'})) {
                   6284:             $permission{'view'} = 1;
                   6285:         }
1.180     raeburn  6286:         if (&Apache::lonnet::allowed('ccc',$env{'request.role.domain'})) {
                   6287:             $permission{'owner'} = 1;
                   6288:         }
1.16      raeburn  6289:     }
                   6290:     my $allowed = 0;
1.184.4.10.2.  (raeburn 6291:):     foreach my $key (keys(%permission)) {
                   6292:):         next if (($key eq 'owner') || ($key eq 'co-owner') || ($key eq 'author'));
                   6293:):         if ($permission{$key}) { $allowed=1; last; }
1.16      raeburn  6294:     }
                   6295:     return (\%permission,$allowed);
                   6296: }
                   6297: 
                   6298: # ==================================================== Figure out author access
                   6299: 
                   6300: sub authorpriv {
                   6301:     my ($auname,$audom)=@_;
                   6302:     unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
                   6303:          || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }    return 1;
                   6304: }
                   6305: 
1.184.4.10.2.  (raeburn 6306:): sub coauthorpriv {
                   6307:):     my ($auname,$audom)=@_;
                   6308:):     my $uname = $env{'user.name'};
                   6309:):     my $udom = $env{'user.domain'};
                   6310:):     if (((&Apache::lonnet::allowed('vca',"$udom/$uname")) ||
                   6311:):          (&Apache::lonnet::allowed('vaa',"$udom/$uname"))) &&
                   6312:):          ($env{"environment.internal.manager./$audom/$auname"})) {
                   6313:):         return 1;
                   6314:):     }
                   6315:):     return '';
                   6316:): }
                   6317:): 
1.27      raeburn  6318: sub roles_on_upload {
1.101     raeburn  6319:     my ($context,$setting,$crstype,%customroles) = @_;
1.27      raeburn  6320:     my (@possible_roles,@permitted_roles);
1.101     raeburn  6321:     @possible_roles = &curr_role_permissions($context,$setting,1,$crstype);
1.27      raeburn  6322:     foreach my $role (@possible_roles) {
                   6323:         if ($role eq 'cr') {
                   6324:             push(@permitted_roles,keys(%customroles));
                   6325:         } else {
                   6326:             push(@permitted_roles,$role);
                   6327:         }
                   6328:     }
1.42      raeburn  6329:     return @permitted_roles;
1.27      raeburn  6330: }
                   6331: 
1.17      raeburn  6332: sub get_course_identity {
                   6333:     my ($cid) = @_;
                   6334:     my ($cnum,$cdom,$cdesc);
                   6335:     if ($cid eq '') {
                   6336:         $cid = $env{'request.course.id'}
                   6337:     }
                   6338:     if ($cid ne '') {
                   6339:         $cnum = $env{'course.'.$cid.'.num'};
                   6340:         $cdom = $env{'course.'.$cid.'.domain'};
                   6341:         $cdesc = $env{'course.'.$cid.'.description'};
                   6342:         if ($cnum eq '' || $cdom eq '') {
                   6343:             my %coursehash =
                   6344:                 &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
                   6345:             $cdom = $coursehash{'domain'};
                   6346:             $cnum = $coursehash{'num'};
                   6347:             $cdesc = $coursehash{'description'};
                   6348:         }
                   6349:     }
                   6350:     return ($cnum,$cdom,$cdesc);
                   6351: }
                   6352: 
1.19      raeburn  6353: sub dc_setcourse_js {
1.184.4.4  raeburn  6354:     my ($formname,$mode,$context,$showcredits,$domain) = @_;
1.37      raeburn  6355:     my ($dc_setcourse_code,$authen_check);
1.19      raeburn  6356:     my $cctext = &Apache::lonnet::plaintext('cc');
1.103     raeburn  6357:     my $cotext = &Apache::lonnet::plaintext('co');
1.19      raeburn  6358:     my %alerts = &sectioncheck_alerts();
                   6359:     my $role = 'role';
                   6360:     if ($mode eq 'upload') {
                   6361:         $role = 'courserole';
1.37      raeburn  6362:     } else {
1.184.4.4  raeburn  6363:         $authen_check = &verify_authen($formname,$context,$domain);
1.19      raeburn  6364:     }
                   6365:     $dc_setcourse_code = (<<"SCRIPTTOP");
1.37      raeburn  6366: $authen_check
                   6367: 
1.19      raeburn  6368: function setCourse() {
                   6369:     var course = document.$formname.dccourse.value;
                   6370:     if (course != "") {
                   6371:         if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
                   6372:             alert("$alerts{'curd'}");
                   6373:             return;
                   6374:         }
                   6375:         var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
                   6376:         var section="";
                   6377:         var numsections = 0;
                   6378:         var newsecs = new Array();
                   6379:         for (var i=0; i<document.$formname.currsec.length; i++) {
                   6380:             if (document.$formname.currsec.options[i].selected == true ) {
                   6381:                 if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
                   6382:                     if (numsections == 0) {
                   6383:                         section = document.$formname.currsec.options[i].value
                   6384:                         numsections = 1;
                   6385:                     }
                   6386:                     else {
                   6387:                         section = section + "," +  document.$formname.currsec.options[i].value
                   6388:                         numsections ++;
                   6389:                     }
                   6390:                 }
                   6391:             }
                   6392:         }
                   6393:         if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
                   6394:             if (numsections == 0) {
                   6395:                 section = document.$formname.newsec.value
                   6396:             }
                   6397:             else {
                   6398:                 section = section + "," +  document.$formname.newsec.value
                   6399:             }
                   6400:             newsecs = document.$formname.newsec.value.split(/,/g);
                   6401:             numsections = numsections + newsecs.length;
                   6402:         }
                   6403:         if ((userrole == 'st') && (numsections > 1)) {
1.103     raeburn  6404:             if (document.$formname.crstype.value == 'Community') {
                   6405:                 alert("$alerts{'inco'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   6406:             } else {
                   6407:                 alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   6408:             }
1.19      raeburn  6409:             return;
                   6410:         }
                   6411:         for (var j=0; j<newsecs.length; j++) {
                   6412:             if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
                   6413:                 alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
                   6414:                 return;
                   6415:             }
                   6416:             if (document.$formname.groups.value != '') {
                   6417:                 var groups = document.$formname.groups.value.split(/,/g);
                   6418:                 for (var k=0; k<groups.length; k++) {
                   6419:                     if (newsecs[j] == groups[k]) {
1.103     raeburn  6420:                         if (document.$formname.crstype.value == 'Community') {
                   6421:                             alert("'"+newsecs[j]+"' $alerts{'mayc'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   6422:                         } else {
                   6423:                             alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   6424:                         }
1.19      raeburn  6425:                         return;
                   6426:                     }
                   6427:                 }
                   6428:             }
                   6429:         }
                   6430:         if ((userrole == 'cc') && (numsections > 0)) {
                   6431:             alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
                   6432:             section = "";
                   6433:         }
1.103     raeburn  6434:         if ((userrole == 'co') && (numsections > 0)) {
                   6435:             alert("$alerts{'secd'} $cotext $alerts{'role'}.\\n$alerts{'accr'}.");
                   6436:             section = "";
                   6437:         }
1.19      raeburn  6438: SCRIPTTOP
                   6439:     if ($mode ne 'upload') {
1.150     raeburn  6440:         $dc_setcourse_code .= (<<"SCRIPTMID");
1.19      raeburn  6441:         var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
                   6442:         var numcourse = getIndex(document.$formname.dccourse);
                   6443:         if (numcourse == "-1") {
1.103     raeburn  6444:             if (document.$formname.type == 'Community') {
                   6445:                 alert("$alerts{'thwc'}");
                   6446:             } else {
                   6447:                 alert("$alerts{'thwa'}");
                   6448:             }
1.19      raeburn  6449:             return;
                   6450:         }
                   6451:         else {
                   6452:             document.$formname.elements[numcourse].name = "act"+coursename;
                   6453:             var numnewsec = getIndex(document.$formname.newsec);
                   6454:             if (numnewsec != "-1") {
                   6455:                 document.$formname.elements[numnewsec].name = "sec"+coursename;
                   6456:                 document.$formname.elements[numnewsec].value = section;
                   6457:             }
                   6458:             var numstart = getIndex(document.$formname.start);
                   6459:             if (numstart != "-1") {
                   6460:                 document.$formname.elements[numstart].name = "start"+coursename;
                   6461:             }
                   6462:             var numend = getIndex(document.$formname.end);
                   6463:             if (numend != "-1") {
                   6464:                 document.$formname.elements[numend].name = "end"+coursename
                   6465:             }
1.150     raeburn  6466: SCRIPTMID
                   6467:         if ($showcredits) {
                   6468:             $dc_setcourse_code .= <<ENDCRED;
                   6469:             var numcredits = getIndex(document.$formname.credits);
                   6470:             if (numcredits != "-1") {
                   6471:                 document.$formname.elements[numcredits].name = "credits"+coursename;
                   6472:             }
                   6473: ENDCRED
                   6474:         }
                   6475:         $dc_setcourse_code .= <<ENDSCRIPT; 
1.19      raeburn  6476:         }
                   6477:     }
1.37      raeburn  6478:     var authcheck = auth_check();
                   6479:     if (authcheck == 'ok') {
                   6480:         document.$formname.submit();
                   6481:     }
1.19      raeburn  6482: }
                   6483: ENDSCRIPT
                   6484:     } else {
                   6485:         $dc_setcourse_code .=  "
                   6486:         document.$formname.sections.value = section;
                   6487:     }
                   6488:     return 'ok';
                   6489: }
                   6490: ";
                   6491:     }
                   6492:     $dc_setcourse_code .= (<<"ENDSCRIPT");
                   6493: 
                   6494:     function getIndex(caller) {
                   6495:         for (var i=0;i<document.$formname.elements.length;i++) {
                   6496:             if (document.$formname.elements[i] == caller) {
                   6497:                 return i;
                   6498:             }
                   6499:         }
                   6500:         return -1;
                   6501:     }
                   6502: ENDSCRIPT
1.37      raeburn  6503:     return $dc_setcourse_code;
                   6504: }
                   6505: 
                   6506: sub verify_authen {
1.184.4.4  raeburn  6507:     my ($formname,$context,$domain) = @_;
1.37      raeburn  6508:     my %alerts = &authcheck_alerts();
                   6509:     my $finish = "return 'ok';";
                   6510:     if ($context eq 'author') {
                   6511:         $finish = "document.$formname.submit();";
                   6512:     }
1.184.4.4  raeburn  6513:     my ($numrules,$intargjs) =
1.184.4.10.2.  (raeburn 6514:):         &Apache::loncommon::passwd_validation_js('argpicked',$domain);
1.37      raeburn  6515:     my $outcome = <<"ENDSCRIPT";
                   6516: 
                   6517: function auth_check() {
                   6518:     var logintype;
                   6519:     if (document.$formname.login.length) {
                   6520:         if (document.$formname.login.length > 0) {
                   6521:             var loginpicked = 0;
                   6522:             for (var i=0; i<document.$formname.login.length; i++) {
                   6523:                 if (document.$formname.login[i].checked == true) {
                   6524:                     loginpicked = 1;
                   6525:                     logintype = document.$formname.login[i].value;
                   6526:                 }
                   6527:             }
                   6528:             if (loginpicked == 0) {
                   6529:                 alert("$alerts{'authen'}");
                   6530:                 return;
                   6531:             }
                   6532:         }
                   6533:     } else {
                   6534:         logintype = document.$formname.login.value;
                   6535:     }
                   6536:     if (logintype == 'nochange') {
                   6537:         return 'ok';
                   6538:     }
                   6539:     var argpicked = document.$formname.elements[logintype+'arg'].value;
                   6540:     if ((argpicked == null) || (argpicked == '') || (typeof argpicked == 'undefined')) {
                   6541:         var alertmsg = '';
                   6542:         switch (logintype) {
                   6543:             case 'krb':
                   6544:                 alertmsg = '$alerts{'krb'}';
                   6545:                 break;
                   6546:             case 'int':
                   6547:                 alertmsg = '$alerts{'ipass'}';
1.184.4.4  raeburn  6548:                 break;
1.37      raeburn  6549:             case 'fsys':
                   6550:                 alertmsg = '$alerts{'ipass'}';
                   6551:                 break;
                   6552:             case 'loc':
                   6553:                 alertmsg = '';
                   6554:                 break;
                   6555:             default:
                   6556:                 alertmsg = '';
                   6557:         }
                   6558:         if (alertmsg != '') {
                   6559:             alert(alertmsg);
                   6560:             return;
                   6561:         }
1.184.4.4  raeburn  6562:     } else if (logintype == 'int') {
                   6563:         var numrules = $numrules;
                   6564:         if (numrules > 0) {
                   6565: $intargjs
                   6566:         }
1.37      raeburn  6567:     }
                   6568:     $finish
                   6569: }
                   6570: ENDSCRIPT
1.19      raeburn  6571: }
                   6572: 
                   6573: sub sectioncheck_alerts {
                   6574:     my %alerts = &Apache::lonlocal::texthash(
1.103     raeburn  6575:                     curd => 'You must select a course or community in the current domain',
1.19      raeburn  6576:                     inea => 'In each course, each user may only have one student role at a time',
1.103     raeburn  6577:                     inco => 'In each community, each user may only have one member role at a time', 
1.19      raeburn  6578:                     youh => 'You had selected',
                   6579:                     sect => 'sections',
                   6580:                     plsm => 'Please modify your selections so they include no more than one section',
                   6581:                     mayn => 'may not be used as the name for a section, as it is a reserved word',
                   6582:                     plsc => 'Please choose a different section name',
                   6583:                     mayt => 'may not be used as the name for a section, as it is the name of a course group',
1.103     raeburn  6584:                     mayc => 'may not be used as the name for a section, as it is the name of a community group',
1.19      raeburn  6585:                     secn => 'Section names and group names must be distinct',
                   6586:                     secd => 'Section designations do not apply to ',
                   6587:                     role => 'roles',
                   6588:                     accr => 'role will be added with access to all sections',
1.103     raeburn  6589:                     thwa => 'There was a problem with your course selection',
                   6590:                     thwc => 'There was a problem with your community selection',
1.19      raeburn  6591:                  );
1.170     damieng  6592:     &js_escape(\%alerts);
1.19      raeburn  6593:     return %alerts;
                   6594: }
1.17      raeburn  6595: 
1.37      raeburn  6596: sub authcheck_alerts {
                   6597:     my %alerts = 
                   6598:         &Apache::lonlocal::texthash(
                   6599:                     authen => 'You must choose an authentication type.',
                   6600:                     krb    => 'You need to specify the Kerberos domain.',
                   6601:                     ipass  => 'You need to specify the initial password.',
                   6602:         );
1.170     damieng  6603:     &js_escape(\%alerts);
1.37      raeburn  6604:     return %alerts;
                   6605: }
                   6606: 
1.141     raeburn  6607: sub is_courseowner {
                   6608:     my ($thiscourse,$courseowner) = @_;
                   6609:     if ($courseowner eq '') {
                   6610:         if ($env{'request.course.id'} eq $thiscourse) {
                   6611:             $courseowner = $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
                   6612:         }
                   6613:     }
                   6614:     if ($courseowner ne '') {
                   6615:         if ($courseowner eq $env{'user.name'}.':'.$env{'user.domain'}) {
                   6616:             return 1;
                   6617:         }
                   6618:     }
                   6619:     return;
                   6620: }
                   6621: 
1.165     raeburn  6622: sub get_selfenroll_titles {
                   6623:     my @row = ('types','registered','enroll_dates','access_dates','section',
                   6624:                'approval','limit');
                   6625:     my %lt = &Apache::lonlocal::texthash (
                   6626:                 types        => 'Users allowed to self-enroll',
                   6627:                 registered   => 'Registration status (official courses)' ,
                   6628:                 enroll_dates => 'Dates self-enrollment available',
                   6629:                 access_dates => 'Access dates for self-enrolling users',
                   6630:                 section      => "Self-enrolling users' section",
                   6631:                 approval     => 'Processing of requests',
                   6632:                 limit        => 'Enrollment limit',
                   6633:              );
                   6634:     return (\@row,\%lt);
                   6635: }
                   6636: 
                   6637: sub selfenroll_default_descs {
                   6638:     my %desc = (
                   6639:                  types => {
                   6640:                             dom => &mt('Course domain'),
                   6641:                             all => &mt('Any domain'),
                   6642:                             ''  => &mt('None'),
                   6643:                           },
                   6644:                  limit => {
                   6645:                             none         => &mt('No limit'),
                   6646:                             allstudents  => &mt('Limit by total students'),
                   6647:                             selfenrolled => &mt('Limit by total self-enrolled'),
                   6648:                           },
                   6649:                  approval => {
                   6650:                                 '0' => &mt('Processed automatically'),
                   6651:                                 '1' => &mt('Queued for approval'),
                   6652:                                 '2' => &mt('Queued, pending validation'),
                   6653:                              },
                   6654:                  registered => {
                   6655:                                  0 => 'No registration required',
                   6656:                                  1 => 'Registered students only',
                   6657:                                },
                   6658:                );
                   6659:     return %desc;
                   6660: }
                   6661: 
                   6662: sub selfenroll_validation_types {
                   6663:     my @items = ('url','fields','button','markup');
                   6664:     my %names =  &Apache::lonlocal::texthash (
                   6665:             url      => 'Web address of validation server/script',
                   6666:             fields   => 'Form fields to send to validator',
                   6667:             button   => 'Text for validation button',
                   6668:             markup   => 'Validation description (HTML)',
                   6669:     );
1.167     raeburn  6670:     my @fields = ('username','domain','uniquecode','course','coursetype','description');
1.165     raeburn  6671:     return (\@items,\%names,\@fields);
                   6672: }
                   6673: 
                   6674: sub get_extended_type {
                   6675:     my ($cdom,$cnum,$crstype,$current) = @_;
                   6676:     my $type = 'unofficial';
                   6677:     my %settings;
                   6678:     if (ref($current) eq 'HASH') {
                   6679:         %settings = %{$current};
                   6680:     } else {
                   6681:         %settings = &Apache::lonnet::get('environment',['internal.coursecode','internal.textbook'],$cdom,$cnum);
                   6682:     }
                   6683:     if ($crstype eq 'Community') {
                   6684:         $type = 'community';
1.173     raeburn  6685:     } elsif ($crstype eq 'Placement') {
                   6686:         $type = 'placement';
1.165     raeburn  6687:     } elsif ($settings{'internal.coursecode'}) {
                   6688:         $type = 'official';
                   6689:     } elsif ($settings{'internal.textbook'}) {
                   6690:         $type = 'textbook';
                   6691:     }
                   6692:     return $type;
                   6693: }
                   6694: 
                   6695: sub selfenrollment_administration {
                   6696:     my ($cdom,$cnum,$crstype,$coursehash) = @_;
                   6697:     my %settings;
                   6698:     if (ref($coursehash) eq 'HASH') {
                   6699:         %settings = %{$coursehash};
                   6700:     } else {
                   6701:         %settings = &Apache::lonnet::get('environment',
                   6702:                         ['internal.selfenrollmgrdc','internal.selfenrollmgrcc',
                   6703:                          'internal.coursecode','internal.textbook'],$cdom,$cnum);
                   6704:     }
                   6705:     my ($possconfigs) = &get_selfenroll_titles(); 
                   6706:     my %domdefaults = &Apache::lonnet::get_domain_defaults($cdom);
                   6707:     my $selfenrolltype = &get_extended_type($cdom,$cnum,$crstype,\%settings);
                   6708: 
                   6709:     my (@in_course,@in_domain); 
                   6710:     if ($settings{'internal.selfenrollmgrcc'} ne '') {
                   6711:         @in_course = split(/,/,$settings{'internal.selfenrollmgrcc'}); 
                   6712:         my @diffs = &Apache::loncommon::compare_arrays($possconfigs,\@in_course);
                   6713:         unless (@diffs) {
                   6714:             return (\@in_course,\@in_domain);
                   6715:         }
                   6716:     }
                   6717:     if ($settings{'internal.selfenrollmgrdc'} ne '') {
1.184.4.10.2.  (raeburn 6718:):         @in_domain = split(/,/,$settings{'internal.selfenrollmgrdc'});
1.165     raeburn  6719:         my @diffs = &Apache::loncommon::compare_arrays(\@in_domain,$possconfigs);
                   6720:         unless (@diffs) {
                   6721:             return (\@in_course,\@in_domain);
                   6722:         }
                   6723:     }
                   6724:     my @combined = @in_course;
                   6725:     push(@combined,@in_domain);
                   6726:     my @diffs = &Apache::loncommon::compare_arrays(\@combined,$possconfigs); 
                   6727:     unless (@diffs) {
                   6728:         return (\@in_course,\@in_domain);
                   6729:     }
                   6730:     if ($domdefaults{$selfenrolltype.'selfenrolladmdc'} eq '') {
                   6731:         push(@in_course,@diffs);
                   6732:     } else {
                   6733:         my @defaultdc = split(/,/,$domdefaults{$selfenrolltype.'selfenrolladmdc'});
                   6734:         foreach my $item (@diffs) {
                   6735:             if (grep(/^\Q$item\E$/,@defaultdc)) {
                   6736:                 push(@in_domain,$item);
                   6737:             } else {
                   6738:                 push(@in_course,$item);
                   6739:             }
                   6740:         }
                   6741:     }
                   6742:     return (\@in_course,\@in_domain);
                   6743: }
                   6744: 
1.175     raeburn  6745: sub custom_role_header {
                   6746:     my ($context,$crstype,$templaterolerefs,$prefix) = @_;
                   6747:     my %lt = &Apache::lonlocal::texthash(
                   6748:                  sele => 'Select a Template',
                   6749:     );
                   6750:     my ($context_code,$button_code);
                   6751:     if ($context eq 'domain') {
                   6752:         $context_code = &custom_coursetype_switch($crstype,$prefix);
                   6753:     }
                   6754:     if (ref($templaterolerefs) eq 'ARRAY') {
                   6755:         foreach my $role (@{$templaterolerefs}) {
                   6756:             my $display = 'inline';
                   6757:             if (($context eq 'domain') && ($role eq 'co')) {
                   6758:                 $display = 'none';
                   6759:             }
                   6760:             $button_code .= &make_button_code($role,$crstype,$display,$prefix).' ';
                   6761:         }
                   6762:     }
                   6763:     return <<"END";
                   6764: <div class="LC_left_float">
                   6765: <fieldset>
                   6766: <legend>$lt{'sele'}</legend>
                   6767: $button_code
                   6768: </fieldset></div>
                   6769: $context_code
                   6770: <br clear="all" />
                   6771: END
                   6772: }
                   6773: 
                   6774: sub custom_coursetype_switch {
                   6775:     my ($crstype,$prefix) = @_;
                   6776:     my ($checkedcourse,$checkedcommunity);
                   6777:     if ($crstype eq 'Community') {
                   6778:         $checkedcommunity = ' checked="checked"';
                   6779:     } else {
                   6780:         $checkedcourse = ' checked="checked"';
                   6781:     }
                   6782:     my %lt = &Apache::lonlocal::texthash(
                   6783:         cont => 'Context',
                   6784:         cour => 'Course',
                   6785:         comm => 'Community',
                   6786:     );
                   6787:     return <<"END";
                   6788: <div class="LC_left_float">
                   6789: <fieldset>
                   6790: <legend>$lt{'cont'}</legend>
                   6791: <label>
                   6792: <input type="radio" name="${prefix}_custrolecrstype" value="Course"$checkedcourse onclick="javascript:customSwitchType('$prefix');" />
                   6793: $lt{'cour'}
                   6794: </label>&nbsp;&nbsp;
                   6795: <label>
                   6796: <input type="radio" name="${prefix}_custrolecrstype" value="Community"$checkedcommunity onclick="javascript:customSwitchType('$prefix');" />
                   6797: $lt{'comm'}
                   6798: </label>
                   6799: </fieldset>
                   6800: </div>
                   6801: END
                   6802: }
                   6803: 
                   6804: sub custom_role_table {
1.180     raeburn  6805:     my ($crstype,$full,$levels,$levelscurrent,$prefix,$add_class,$id) = @_;
1.175     raeburn  6806:     return unless ((ref($full) eq 'HASH') && (ref($levels) eq 'HASH') &&
                   6807:                    (ref($levelscurrent) eq 'HASH'));
                   6808:     my %lt=&Apache::lonlocal::texthash (
                   6809:                     'prv'  => "Privilege",
                   6810:                     'crl'  => "Course Level",
                   6811:                     'dml'  => "Domain Level",
                   6812:                     'ssl'  => "System Level");
                   6813:     my %cr = (
                   6814:                course => '_c',
                   6815:                domain => '_d',
                   6816:                system => '_s',
                   6817:              );
                   6818: 
1.180     raeburn  6819:     my $output=&Apache::loncommon::start_data_table($add_class,$id).
1.175     raeburn  6820:                &Apache::loncommon::start_data_table_header_row().
                   6821:                '<th>'.$lt{'prv'}.'</th><th>'.$lt{'crl'}.'</th><th>'.$lt{'dml'}.
                   6822:                '</th><th>'.$lt{'ssl'}.'</th>'.
                   6823:                &Apache::loncommon::end_data_table_header_row();
                   6824:     foreach my $priv (sort(keys(%{$full}))) {
                   6825:         my $privtext = &Apache::lonnet::plaintext($priv,$crstype);
                   6826:         $output .= &Apache::loncommon::start_data_table_row().
                   6827:                   '<td><span id="'.$prefix.$priv.'">'.$privtext.'</span></td>';
                   6828:         foreach my $type ('course','domain','system') {
                   6829:             if (($type eq 'system') && ($priv eq 'bre') && ($crstype eq 'Community')) {
                   6830:                 $output .= '<td>&nbsp;</td>';
                   6831:             } else {
                   6832:                 $output .= '<td>'.
                   6833:                   ($levels->{$type}{$priv}?'<input type="checkbox" id="'.$prefix.$priv.$cr{$type}.'"'.
                   6834:                   ' name="'.$prefix.$priv.$cr{$type}.'"'.
                   6835:                   ($levelscurrent->{$type}{$priv}?' checked="checked"':'').' />':'&nbsp;').
                   6836:                   '</td>';
                   6837:             }
                   6838:         }
                   6839:         $output .= &Apache::loncommon::end_data_table_row();
                   6840:     }
                   6841:     $output .= &Apache::loncommon::end_data_table();
                   6842:     return $output;
                   6843: }
                   6844: 
                   6845: sub custom_role_privs {
                   6846:     my ($privs,$full,$levels,$levelscurrent)= @_;
                   6847:     return unless ((ref($privs) eq 'HASH') && (ref($full) eq 'HASH') &&
                   6848:                    (ref($levels) eq 'HASH') && (ref($levelscurrent) eq 'HASH'));
                   6849:     my %cr = (
                   6850:                course => 'cr:c',
                   6851:                domain => 'cr:d',
                   6852:                system => 'cr:s',
                   6853:              );
                   6854:     foreach my $type ('course','domain','system') {
                   6855:         foreach my $item (split(/\:/,$Apache::lonnet::pr{$cr{$type}})) {
                   6856:             my ($priv,$restrict)=split(/\&/,$item);
                   6857:             if (!$restrict) { $restrict='F'; }
                   6858:             $levels->{$type}->{$priv}=$restrict;
                   6859:             if ($privs->{$type}=~/\:$priv/) {
                   6860:                 $levelscurrent->{$type}->{$priv}=1;
                   6861:             }
                   6862:             $full->{$priv}=1;
                   6863:         }
                   6864:     }
                   6865:     return;
                   6866: }
                   6867: 
                   6868: sub custom_template_roles {
                   6869:     my ($context,$crstype) = @_;
                   6870:     my @template_roles = ("in","ta","ep");
                   6871:     if (($context eq 'domain') || ($context eq 'domprefs')) {
                   6872:         push(@template_roles,"ad");
                   6873:     }
                   6874:     push(@template_roles,"st");
                   6875:     if ($context eq 'domain') {
                   6876:         unshift(@template_roles,('co','cc'));
                   6877:     } else {
                   6878:         if ($crstype eq 'Community') {
                   6879:             unshift(@template_roles,'co');
                   6880:         } else {
                   6881:             unshift(@template_roles,'cc');
                   6882:         }
                   6883:     }
                   6884:     return @template_roles;
                   6885: }
                   6886: 
                   6887: sub custom_roledefs_js {
                   6888:     my ($context,$crstype,$formname,$full,$templaterolesref,$jsback) = @_;
                   6889:     my $button_code = "\n";
                   6890:     my $head_script = "\n";
                   6891:     my (%roletitlestr,$rolenamestr);
                   6892:     my %role_titles = (
                   6893:                         Course    => [],
                   6894:                         Community => [],
                   6895:                       );
                   6896:     $head_script .= '<script type="text/javascript">'."\n"
                   6897:                    .'// <![CDATA['."\n";
                   6898:     if (ref($templaterolesref) eq 'ARRAY') {
                   6899:         if ($context eq 'domain') {
                   6900:             $rolenamestr = join("','",@{$templaterolesref});
                   6901:         }
                   6902:         foreach my $role (@{$templaterolesref}) {
                   6903:             $head_script .= &make_script_template($role,$crstype,$formname);
                   6904:             if ($context eq 'domain') {
                   6905:                 foreach my $type ('Course','Community') {
                   6906:                     push(@{$role_titles{$type}},&Apache::lonnet::plaintext($role,$type));
                   6907:                 }
                   6908:             }
                   6909:         }
                   6910:     }
                   6911:     if ($context eq 'domain') {
                   6912:         foreach my $type ('Course','Community') {
                   6913:             $roletitlestr{$type} = join("','",@{$role_titles{$type}});
                   6914:         }
                   6915:         my %pt = (
                   6916:             Community => {
                   6917:                            cst => &mt('Grant/revoke role of Member'),
                   6918:                            mdc => &mt('Edit community contents'),
                   6919:                            pch => &mt('Post discussion on community resources'),
                   6920:                            pfo => &mt('Print for other users and entire community'),
                   6921:                          },
                   6922:             Course    => {
                   6923:                            cst => &mt('Grant/revoke role of Student'),
                   6924:                            mdc => &mt('Edit course contents'),
                   6925:                            pch => &mt('Post discussion on course resources'),
                   6926:                            pfo => &mt('Print for other users and entire course'),
                   6927:                          },
                   6928:         );
                   6929:         $head_script .= <<"ENDJS";
                   6930: function customSwitchType(prefix) {
                   6931:     var privnames = new Array('cst','mdc','pch','pfo');
                   6932:     var privtxtcrs = new Array('$pt{Course}{cst}','$pt{Course}{mdc}','$pt{Course}{pch}','$pt{Course}{pfo}');
                   6933:     var privtxtcom = new Array('$pt{Community}{cst}','$pt{Community}{mdc}','$pt{Community}{pch}','$pt{Community}{pfo}');
                   6934:     var rolenames = new Array('$rolenamestr');
                   6935:     var rolescrs = new Array('$roletitlestr{Course}');
                   6936:     var rolescom = new Array('$roletitlestr{Community}');
                   6937:     var radio = prefix+'_custrolecrstype';
                   6938:     if (document.$formname.elements[radio].length > 1) {
                   6939:         for (var i=0; i<document.$formname.elements[radio].length; i++) {
                   6940:             if (document.$formname.elements[radio][i].checked) {
                   6941:                 if ((document.getElementById(prefix+'bre_s')) && (document.getElementById(prefix+'bro_s'))) {
                   6942:                     if (document.$formname.elements[radio][i].value == 'Community') {
                   6943:                         if (document.getElementById(prefix+'bre_s').checked) {
                   6944:                             document.getElementById(prefix+'bro_s').checked = true;
                   6945:                             document.getElementById(prefix+'bre_s').checked = false;
                   6946: 
                   6947:                         }
                   6948:                         document.getElementById(prefix+'bre_s').style.visibility = 'hidden';
                   6949:                     } else {
                   6950:                         document.getElementById(prefix+'bre_s').style.visibility = 'visible';
                   6951:                         if (document.getElementById(prefix+'bro_s').checked) {
                   6952:                             document.getElementById(prefix+'bre_s').checked = true;
                   6953:                             document.getElementById(prefix+'bro_s').checked = false;
                   6954:                         }
                   6955:                     }
                   6956:                 }
                   6957:                 for (var j=0; j<privnames.length; j++) {
                   6958:                     if (document.getElementById(prefix+privnames[j])) {
                   6959:                         if (document.getElementById(prefix+privnames[j])) {
                   6960:                             if (document.$formname.elements[radio][i].value == 'Course') {
                   6961:                                 document.getElementById(prefix+privnames[j]).innerHTML = privtxtcrs[j];
                   6962:                             } else {
                   6963:                                 document.getElementById(prefix+privnames[j]).innerHTML = privtxtcom[j];
                   6964:                             }
                   6965:                         }
                   6966:                     }
                   6967:                 }
                   6968:                 for (var j=0; j<rolenames.length; j++) {
                   6969:                     if (document.getElementById(prefix+rolenames[j])) {
                   6970:                         if (document.getElementById(prefix+rolenames[j])) {
                   6971:                             if (document.$formname.elements[radio][i].value == 'Course') {
                   6972:                                 document.getElementById(prefix+rolenames[j]).value = rolescrs[j];
                   6973:                                 if (rolenames[j] == 'cc') {
                   6974:                                     document.getElementById(prefix+rolenames[j]).style.display = 'inline';
                   6975:                                 }
                   6976:                                 if (rolenames[j] == 'co') {
                   6977:                                     document.getElementById(prefix+rolenames[j]).style.display = 'none';
                   6978:                                 }
                   6979:                             } else {
                   6980:                                 document.getElementById(prefix+rolenames[j]).value = rolescom[j];
                   6981:                                 if (rolenames[j] == 'cc') {
                   6982:                                     document.getElementById(prefix+rolenames[j]).style.display = 'none';
                   6983:                                 }
                   6984:                                 if (rolenames[j] == 'co') {
                   6985:                                     document.getElementById(prefix+rolenames[j]).style.display = 'inline';
                   6986:                                 }
                   6987:                             }
                   6988:                         }
                   6989:                     }
                   6990:                 }
                   6991:             }
                   6992:         }
                   6993:     }
                   6994:     return;
                   6995: }
                   6996: ENDJS
                   6997:     }
                   6998:     $head_script .= "\n".$jsback."\n"
                   6999:                    .'// ]]>'."\n"
                   7000:                    .'</script>'."\n";
                   7001:     return $head_script;
                   7002: }
                   7003: 
                   7004: # --------------------------------------------------------
                   7005: sub make_script_template {
                   7006:     my ($role,$crstype,$formname) = @_;
                   7007:     my $return_script = 'function set_'.$role.'(prefix) {'."\n";
                   7008:     my (%full_by_level,%role_priv);
                   7009:     foreach my $level ('c','d','s') {
                   7010:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:'.$level})) {
                   7011:             next if (($level eq 's') && ($crstype eq 'Community') && ($item eq 'bre&S'));
                   7012:             my ($priv,$restrict)=split(/\&/,$item);
                   7013:             $full_by_level{$level}{$priv}=1;
                   7014:         }
                   7015:         $role_priv{$level} = {};
                   7016:         my @temp = split(/:/,$Apache::lonnet::pr{$role.':'.$level});
                   7017:         foreach my $priv (@temp) {
                   7018:             my ($priv_item, $dummy) = split(/\&/,$priv);
                   7019:             $role_priv{$level}{$priv_item} = 1;
                   7020:         }
                   7021:     }
                   7022:     my %to_check = (
                   7023:                       c => ['c','d','s'],
                   7024:                       d => ['d','s'],
                   7025:                       s => ['s'],
                   7026:                    );
                   7027:     foreach my $level ('c','d','s') {
                   7028:         if (ref($full_by_level{$level}) eq 'HASH') {
                   7029:             foreach my $priv (keys(%{$full_by_level{$level}})) {
                   7030:                 my $value = 'false';
                   7031:                 if (ref($to_check{$level}) eq 'ARRAY') {
                   7032:                     foreach my $lett (@{$to_check{$level}}) {
                   7033:                         if (exists($role_priv{$lett}{$priv})) {
                   7034:                             $value = 'true';
                   7035:                             last;
                   7036:                         }
                   7037:                     }
                   7038:                     $return_script .= "document.$formname.elements[prefix+'".$priv."_".$level."'].checked = $value;\n";
                   7039:                 }
                   7040:             }
                   7041:         }
                   7042:     }
                   7043:     $return_script .= '}'."\n";
                   7044:     return ($return_script);
                   7045: }
                   7046: # ----------------------------------------------------------
                   7047: sub make_button_code {
                   7048:     my ($role,$crstype,$display,$prefix) = @_;
                   7049:     my $label = &Apache::lonnet::plaintext($role,$crstype);
                   7050:     my $button_code = '<input type="button" onclick="set_'.$role."('$prefix'".')" '.
                   7051:                       'id="'.$prefix.$role.'" value="'.$label.'" '.
                   7052:                       'style="display:'.$display.'" />';
                   7053:     return ($button_code);
                   7054: }
                   7055: 
                   7056: sub custom_role_update {
                   7057:     my ($rolename,$prefix) = @_;
                   7058: # ------------------------------------------------------- What can be assigned?
                   7059:     my %privs = (
                   7060:                       c => '',
                   7061:                       d => '',
                   7062:                       s => '',
                   7063:                     );
                   7064:     foreach my $level (keys(%privs)) {
                   7065:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:'.$level})) {
                   7066:             my ($priv,$restrict)=split(/\&/,$item);
                   7067:             if (!$restrict) { $restrict=''; }
                   7068:             if ($env{'form.'.$prefix.$priv.'_'.$level}) {
                   7069:                 $privs{$level} .=':'.$item;
                   7070:             }
                   7071:         }
                   7072:     }
                   7073:     return %privs;
                   7074: }
                   7075: 
1.180     raeburn  7076: sub adhoc_status_types {
                   7077:     my ($cdom,$context,$role,$selectedref,$othertitle,$usertypes,$types,$disabled) = @_;
                   7078:     my $output = &Apache::loncommon::start_data_table();
                   7079:     my $numinrow = 3;
                   7080:     my $rem;
                   7081:     if (ref($types) eq 'ARRAY') {
                   7082:         for (my $i=0; $i<@{$types}; $i++) {
                   7083:             if (defined($usertypes->{$types->[$i]})) {
                   7084:                 my $rem = $i%($numinrow);
                   7085:                 if ($rem == 0) {
                   7086:                     if ($i > 0) {
                   7087:                         $output .= &Apache::loncommon::end_data_table_row();
                   7088:                     }
                   7089:                     $output .= &Apache::loncommon::start_data_table_row();
                   7090:                 }
                   7091:                 my $check;
                   7092:                 if (ref($selectedref) eq 'ARRAY') {
                   7093:                     if (grep(/^\Q$types->[$i]\E$/,@{$selectedref})) {
                   7094:                         $check = ' checked="checked"';
                   7095:                     }
                   7096:                 }
                   7097:                 $output .= '<td>'.
                   7098:                            '<span class="LC_nobreak"><label>'.
                   7099:                            '<input type="checkbox" name="'.$context.$role.'_status" '.
                   7100:                            'value="'.$types->[$i].'"'.$check.$disabled.' />'.
                   7101:                            $usertypes->{$types->[$i]}.'</label></span></td>';
                   7102:             }
                   7103:         }
                   7104:         $rem = @{$types}%($numinrow);
                   7105:     }
                   7106:     my $colsleft = $numinrow - $rem;
                   7107:     if (($rem == 0) && (@{$types} > 0)) {
                   7108:         $output .= &Apache::loncommon::start_data_table_row();
                   7109:     }
                   7110:     if ($colsleft > 1) {
                   7111:         $output .= '<td colspan="'.$colsleft.'">';
                   7112:     } else {
                   7113:         $output .= '<td>';
                   7114:     }
                   7115:     my $defcheck;
                   7116:     if (ref($selectedref) eq 'ARRAY') {
                   7117:         if (grep(/^default$/,@{$selectedref})) {
                   7118:             $defcheck = ' checked="checked"';
                   7119:         }
                   7120:     }
                   7121:     $output .= '<span class="LC_nobreak"><label>'.
                   7122:                '<input type="checkbox" name="'.$context.$role.'_status"'.
                   7123:                'value="default"'.$defcheck.$disabled.' />'.
                   7124:                $othertitle.'</label></span></td>'.
                   7125:                &Apache::loncommon::end_data_table_row().
                   7126:                &Apache::loncommon::end_data_table();
                   7127:     return $output;
                   7128: }
                   7129: 
                   7130: sub adhoc_staff {
                   7131:     my ($access,$context,$role,$selectedref,$adhocref,$disabled) = @_;
                   7132:     my $output;
                   7133:     if (ref($adhocref) eq 'HASH') {
                   7134:         my %by_fullname;
                   7135:         my $numinrow = 4;
                   7136:         my $rem;
                   7137:         my @personnel = keys(%{$adhocref});
                   7138:         if (@personnel) {
                   7139:             foreach my $person (@personnel) {
                   7140:                 my ($uname,$udom) = split(/:/,$person);
                   7141:                 my $fullname = &Apache::loncommon::plainname($uname,$udom,'lastname');
                   7142:                 $by_fullname{$fullname} = $person;
                   7143:             }
                   7144:             my @sorted = sort(keys(%by_fullname));
                   7145:             my $count = scalar(@sorted);
                   7146:             $output = &Apache::loncommon::start_data_table();
                   7147:             for (my $i=0; $i<$count; $i++) {
                   7148:                 my $rem = $i%($numinrow);
                   7149:                 if ($rem == 0) {
                   7150:                     if ($i > 0) {
                   7151:                         $output .= &Apache::loncommon::end_data_table_row();
                   7152:                     }
                   7153:                     $output .= &Apache::loncommon::start_data_table_row();
                   7154:                 }
                   7155:                 my $check;
                   7156:                 my $user = $by_fullname{$sorted[$i]};
                   7157:                 if (ref($selectedref) eq 'ARRAY') {
                   7158:                     if (grep(/^\Q$user\E$/,@{$selectedref})) {
                   7159:                         $check = ' checked="checked"';
                   7160:                     }
                   7161:                 }
                   7162:                 if ($i == $count-1) {
                   7163:                     my $colsleft = $numinrow - $rem;
                   7164:                     if ($colsleft > 1) {
                   7165:                         $output .= '<td colspan="'.$colsleft.'">';
                   7166:                     } else {
                   7167:                         $output .= '<td>';
                   7168:                     }
                   7169:                 } else {
                   7170:                     $output .= '<td>';
                   7171:                 }
                   7172:                 $output .= '<span class="LC_nobreak"><label>'.
                   7173:                            '<input type="checkbox" name="'.$context.$role.'_staff_'.$access.'" '.
                   7174:                            'value="'.$user.'"'.$check.$disabled.' />'.$sorted[$i].
                   7175:                            '</label></span></td>';
                   7176:                 if ($i == $count-1) {
                   7177:                     $output .= &Apache::loncommon::end_data_table_row();
                   7178:                 }
                   7179:             }
                   7180:             $output .= &Apache::loncommon::end_data_table();
                   7181:         }
                   7182:     }
                   7183:     return $output;
                   7184: }
                   7185: 
                   7186: 
1.1       raeburn  7187: 1;
                   7188: 

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