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

1.1       raeburn     1: # The LearningOnline Network with CAPA
                      2: # Utility functions for managing LON-CAPA user accounts
                      3: #
1.38    ! raeburn     4: # $Id: lonuserutils.pm,v 1.37 2008/01/01 17:24:59 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: 
                     33: use strict;
                     34: use Apache::lonnet;
                     35: use Apache::loncommon();
                     36: use Apache::lonhtmlcommon;
                     37: use Apache::lonlocal;
1.8       raeburn    38: use Apache::longroup;
                     39: use LONCAPA qw(:DEFAULT :match);
1.1       raeburn    40: 
                     41: ###############################################################
                     42: ###############################################################
                     43: # Drop student from all sections of a course, except optional $csec
                     44: sub modifystudent {
                     45:     my ($udom,$unam,$courseid,$csec,$desiredhost)=@_;
                     46:     # if $csec is undefined, drop the student from all the courses matching
                     47:     # this one.  If $csec is defined, drop them from all other sections of
                     48:     # this course and add them to section $csec
1.17      raeburn    49:     my ($cnum,$cdom) = &get_course_identity($courseid);
1.1       raeburn    50:     my %roles = &Apache::lonnet::dump('roles',$udom,$unam);
                     51:     my ($tmp) = keys(%roles);
                     52:     # Bail out if we were unable to get the students roles
                     53:     return "$1" if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                     54:     # Go through the roles looking for enrollment in this course
                     55:     my $result = '';
                     56:     foreach my $course (keys(%roles)) {
                     57:         if ($course=~m{^/\Q$cdom\E/\Q$cnum\E(?:\/)*(?:\s+)*(\w+)*\_st$}) {
                     58:             # We are in this course
                     59:             my $section=$1;
                     60:             $section='' if ($course eq "/$cdom/$cnum".'_st');
                     61:             if (defined($csec) && $section eq $csec) {
                     62:                 $result .= 'ok:';
                     63:             } elsif ( ((!$section) && (!$csec)) || ($section ne $csec) ) {
                     64:                 my (undef,$end,$start)=split(/\_/,$roles{$course});
                     65:                 my $now=time;
                     66:                 # if this is an active role
                     67:                 if (!($start && ($now<$start)) || !($end && ($now>$end))) {
                     68:                     my $reply=&Apache::lonnet::modifystudent
                     69:                         # dom  name  id mode pass     f     m     l     g
                     70:                         ($udom,$unam,'',  '',  '',undef,undef,undef,undef,
1.22      raeburn    71:                          $section,time,undef,undef,$desiredhost,'','manual',
                     72:                          '',$courseid);
1.1       raeburn    73:                     $result .= $reply.':';
                     74:                 }
                     75:             }
                     76:         }
                     77:     }
                     78:     if ($result eq '') {
1.37      raeburn    79:         $result = &mt('Unable to find section for this student');
1.1       raeburn    80:     } else {
                     81:         $result =~ s/(ok:)+/ok/g;
                     82:     }
                     83:     return $result;
                     84: }
                     85: 
                     86: sub modifyuserrole {
                     87:     my ($context,$setting,$changeauth,$cid,$udom,$uname,$uid,$umode,$upass,
                     88:         $first,$middle,$last,$gene,$sec,$forceid,$desiredhome,$email,$role,
1.5       raeburn    89:         $end,$start,$checkid) = @_;
                     90:     my ($scope,$userresult,$authresult,$roleresult,$idresult);
1.1       raeburn    91:     if ($setting eq 'course' || $context eq 'course') {
                     92:         $scope = '/'.$cid;
                     93:         $scope =~ s/\_/\//g;
                     94:         if ($role ne 'cc' && $sec ne '') {
                     95:             $scope .='/'.$sec;
                     96:         }
1.5       raeburn    97:     } elsif ($context eq 'domain') {
1.1       raeburn    98:         $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn    99:     } elsif ($context eq 'author') {
1.1       raeburn   100:         $scope =  '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                    101:     }
                    102:     if ($context eq 'domain') {
                    103:         my $uhome = &Apache::lonnet::homeserver($uname,$udom);
                    104:         if ($uhome ne 'no_host') {
1.5       raeburn   105:             if (($changeauth eq 'Yes') && (&Apache::lonnet::allowed('mau',$udom))) {
1.1       raeburn   106:                 if ((($umode =~ /^krb4|krb5|internal$/) && $upass ne '') ||
                    107:                     ($umode eq 'localauth')) {
                    108:                     $authresult = &Apache::lonnet::modifyuserauth($udom,$uname,$umode,$upass);
                    109:                 }
                    110:             }
1.5       raeburn   111:             if (($forceid) && (&Apache::lonnet::allowed('mau',$udom)) &&
                    112:                 ($env{'form.recurseid'}) && ($checkid)) {
                    113:                 my %userupdate = (
                    114:                                   lastname   => $last,
                    115:                                   middlename => $middle,
                    116:                                   firstname  => $first,
                    117:                                   generation => $gene,
                    118:                                   id         => $uid,
                    119:                                  );
                    120:                 $idresult = &propagate_id_change($uname,$udom,\%userupdate);
                    121:             }
1.1       raeburn   122:         }
                    123:     }
                    124:     $userresult =
                    125:         &Apache::lonnet::modifyuser($udom,$uname,$uid,$umode,$upass,$first,
                    126:                                     $middle,$last,$gene,$forceid,$desiredhome,
                    127:                                     $email,$role,$start,$end);
                    128:     if ($userresult eq 'ok') {
1.5       raeburn   129:         if ($role ne '') {
1.22      raeburn   130:             $role =~ s/_/\//g;
1.1       raeburn   131:             $roleresult = &Apache::lonnet::assignrole($udom,$uname,$scope,
                    132:                                                       $role,$end,$start);
                    133:         }
                    134:     }
1.5       raeburn   135:     return ($userresult,$authresult,$roleresult,$idresult);
1.1       raeburn   136: }
                    137: 
1.5       raeburn   138: sub propagate_id_change {
                    139:     my ($uname,$udom,$user) = @_;
1.12      raeburn   140:     my (@types,@roles);
1.5       raeburn   141:     @types = ('active','future');
                    142:     @roles = ('st');
                    143:     my $idresult;
                    144:     my %roleshash = &Apache::lonnet::get_my_roles($uname,
1.12      raeburn   145:                         $udom,'userroles',\@types,\@roles);
                    146:     my %args = (
                    147:                 one_time => 1,
                    148:                );
1.5       raeburn   149:     foreach my $item (keys(%roleshash)) {
1.22      raeburn   150:         my ($cnum,$cdom,$role) = split(/:/,$item,-1);
1.5       raeburn   151:         my ($start,$end) = split(/:/,$roleshash{$item});
                    152:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
1.12      raeburn   153:             my $result = &update_classlist($cdom,$cnum,$udom,$uname,$user);
                    154:             my %coursehash = 
                    155:                 &Apache::lonnet::coursedescription($cdom.'_'.$cnum,\%args);
                    156:             my $cdesc = $coursehash{'description'};
                    157:             if ($cdesc eq '') { 
                    158:                 $cdesc = $cdom.'_'.$cnum;
                    159:             }
1.5       raeburn   160:             if ($result eq 'ok') {
1.12      raeburn   161:                 $idresult .= &mt('Classlist update for "[_1]" in "[_2]".',$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   162:             } else {
1.12      raeburn   163:                 $idresult .= &mt('Error: "[_1]" during classlist update for "[_2]" in "[_3]".',$result,$uname.':'.$udom,$cdesc).'<br />'."\n";
1.5       raeburn   164:             }
                    165:         }
                    166:     }
                    167:     return $idresult;
                    168: }
                    169: 
                    170: sub update_classlist {
                    171:     my ($cdom,$cnum,$udom,$uname,$user) = @_;
1.6       albertel  172:     my ($uid,$classlistentry);
1.5       raeburn   173:     my $fullname =
                    174:         &Apache::lonnet::format_name($user->{'firstname'},$user->{'middlename'},
                    175:                                      $user->{'lastname'},$user->{'generation'},
                    176:                                      'lastname');
                    177:     my %classhash = &Apache::lonnet::get('classlist',[$uname.':'.$udom],
                    178:                                          $cdom,$cnum);
                    179:     my @classinfo = split(/:/,$classhash{$uname.':'.$udom});
                    180:     my $ididx=&Apache::loncoursedata::CL_ID() - 2;
                    181:     my $nameidx=&Apache::loncoursedata::CL_FULLNAME() - 2;
                    182:     for (my $i=0; $i<@classinfo; $i++) {
                    183:         if ($i == $ididx) {
                    184:             if (defined($user->{'id'})) {
                    185:                 $classlistentry .= $user->{'id'}.':';
                    186:             } else {
                    187:                 $classlistentry .= $classinfo[$i].':';
                    188:             }
                    189:         } elsif ($i == $nameidx) {
                    190:             $classlistentry .= $fullname.':';
                    191:         } else {
                    192:             $classlistentry .= $classinfo[$i].':';
                    193:         }
                    194:     }
                    195:     $classlistentry =~ s/:$//;
                    196:     my $reply=&Apache::lonnet::cput('classlist',
                    197:                                     {"$uname:$udom" => $classlistentry},
                    198:                                     $cdom,$cnum);
                    199:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                    200:         return 'ok';
                    201:     } else {
                    202:         return 'error: '.$reply;
                    203:     }
                    204: }
                    205: 
                    206: 
1.1       raeburn   207: ###############################################################
                    208: ###############################################################
1.2       raeburn   209: # build a role type and role selection form
                    210: sub domain_roles_select {
                    211:     # Set up the role type and role selection boxes when in 
                    212:     # domain context   
                    213:     #
                    214:     # Role types
1.13      raeburn   215:     my @roletypes = ('domain','author','course');
1.2       raeburn   216:     my %lt = &role_type_names();
1.1       raeburn   217:     #
                    218:     # build up the menu information to be passed to
                    219:     # &Apache::loncommon::linked_select_forms
                    220:     my %select_menus;
1.2       raeburn   221:     if ($env{'form.roletype'} eq '') {
                    222:         $env{'form.roletype'} = 'domain';
                    223:     }
                    224:     foreach my $roletype (@roletypes) {
1.1       raeburn   225:         # set up the text for this domain
1.2       raeburn   226:         $select_menus{$roletype}->{'text'}= $lt{$roletype};
1.1       raeburn   227:         # we want a choice of 'default' as the default in the second menu
1.2       raeburn   228:         if ($env{'form.roletype'} ne '') {
                    229:             $select_menus{$roletype}->{'default'} = $env{'form.showrole'};
                    230:         } else { 
                    231:             $select_menus{$roletype}->{'default'} = 'Any';
                    232:         }
1.1       raeburn   233:         # Now build up the other items in the second menu
1.2       raeburn   234:         my @roles;
                    235:         if ($roletype eq 'domain') {
                    236:             @roles = &domain_roles();
1.13      raeburn   237:         } elsif ($roletype eq 'author') {
1.2       raeburn   238:             @roles = &construction_space_roles();
                    239:         } else {
1.17      raeburn   240:             my $custom = 1;
                    241:             @roles = &course_roles('domain',undef,$custom);
1.1       raeburn   242:         }
1.2       raeburn   243:         my $order = ['Any',@roles];
                    244:         $select_menus{$roletype}->{'order'} = $order; 
                    245:         foreach my $role (@roles) {
1.5       raeburn   246:             if ($role eq 'cr') {
                    247:                 $select_menus{$roletype}->{'select2'}->{$role} =
                    248:                               &mt('Custom role');
                    249:             } else {
                    250:                 $select_menus{$roletype}->{'select2'}->{$role} = 
                    251:                               &Apache::lonnet::plaintext($role);
                    252:             }
1.2       raeburn   253:         }
                    254:         $select_menus{$roletype}->{'select2'}->{'Any'} = &mt('Any');
1.1       raeburn   255:     }
1.2       raeburn   256:     my $result = &Apache::loncommon::linked_select_forms
                    257:         ('studentform',('&nbsp;'x3).&mt('Role: '),$env{'form.roletype'},
1.13      raeburn   258:          'roletype','showrole',\%select_menus,['domain','author','course']);
1.1       raeburn   259:     return $result;
                    260: }
                    261: 
                    262: ###############################################################
                    263: ###############################################################
                    264: sub hidden_input {
                    265:     my ($name,$value) = @_;
                    266:     return '<input type="hidden" name="'.$name.'" value="'.$value.'" />'."\n";
                    267: }
                    268: 
                    269: sub print_upload_manager_header {
1.22      raeburn   270:     my ($r,$datatoken,$distotal,$krbdefdom,$context,$permission)=@_;
1.1       raeburn   271:     my $javascript;
                    272:     #
                    273:     if (! exists($env{'form.upfile_associate'})) {
                    274:         $env{'form.upfile_associate'} = 'forward';
                    275:     }
                    276:     if ($env{'form.associate'} eq 'Reverse Association') {
                    277:         if ( $env{'form.upfile_associate'} ne 'reverse' ) {
                    278:             $env{'form.upfile_associate'} = 'reverse';
                    279:         } else {
                    280:             $env{'form.upfile_associate'} = 'forward';
                    281:         }
                    282:     }
                    283:     if ($env{'form.upfile_associate'} eq 'reverse') {
                    284:         $javascript=&upload_manager_javascript_reverse_associate();
                    285:     } else {
                    286:         $javascript=&upload_manager_javascript_forward_associate();
                    287:     }
                    288:     #
                    289:     # Deal with restored settings
                    290:     my $password_choice = '';
                    291:     if (exists($env{'form.ipwd_choice'}) &&
                    292:         $env{'form.ipwd_choice'} ne '') {
                    293:         # If a column was specified for password, assume it is for an
                    294:         # internal password.  This is a bug waiting to be filed (could be
                    295:         # local or krb auth instead of internal) but I do not have the
                    296:         # time to mess around with this now.
                    297:         $password_choice = 'int';
                    298:     }
                    299:     #
1.22      raeburn   300:     my $groupslist;
                    301:     if ($context eq 'course') {
                    302:         $groupslist = &get_groupslist();
                    303:     }
1.1       raeburn   304:     my $javascript_validations =
1.22      raeburn   305:         &javascript_validations('upload',$krbdefdom,$password_choice,undef,
                    306:                                 $env{'request.role.domain'},$context,
1.33      raeburn   307:                                 $groupslist);
1.1       raeburn   308:     my $checked=(($env{'form.noFirstLine'})?' checked="checked" ':'');
                    309:     $r->print(&mt('Total number of records found in file: <b>[_1]</b>.',$distotal).
                    310:               "<br />\n");
                    311:     $r->print('<div class="LC_left_float"><h3>'.
                    312:               &mt('Identify fields in uploaded list')."</h3>\n");
                    313:     $r->print(&mt('Enter as many fields as you can.<br /> The system will inform you and bring you back to this page, <br /> if the data selected are insufficient to add users.')."<br />\n");
                    314:     $r->print(&hidden_input('action','upload').
                    315:               &hidden_input('state','got_file').
                    316:               &hidden_input('associate','').
                    317:               &hidden_input('datatoken',$datatoken).
                    318:               &hidden_input('fileupload',$env{'form.fileupload'}).
                    319:               &hidden_input('upfiletype',$env{'form.upfiletype'}).
                    320:               &hidden_input('upfile_associate',$env{'form.upfile_associate'}));
                    321:     $r->print('<br /><input type="button" value="Reverse Association" '.
                    322:               'name="'.&mt('Reverse Association').'" '.
                    323:               'onClick="javascript:this.form.associate.value=\'Reverse Association\';submit(this.form);" />');
                    324:     $r->print('<label><input type="checkbox" name="noFirstLine"'.$checked.'/>'.
                    325:               &mt('Ignore First Line').'</label>');
                    326:     $r->print("<br /><br />\n".
                    327:               '<script type="text/javascript" language="Javascript">'."\n".
                    328:               $javascript."\n".$javascript_validations.'</script>');
                    329: }
                    330: 
                    331: ###############################################################
                    332: ###############################################################
                    333: sub javascript_validations {
1.22      raeburn   334:     my ($mode,$krbdefdom,$curr_authtype,$curr_authfield,$domain,
1.33      raeburn   335:         $context,$groupslist)=@_;
1.22      raeburn   336:     my %param = (
                    337:                   kerb_def_dom => $krbdefdom,
                    338:                   curr_authtype => $curr_authtype,
                    339:                 );
1.37      raeburn   340:     if ($mode eq 'upload') {
1.22      raeburn   341:         $param{'formname'} = 'studentform';
1.1       raeburn   342:     } elsif ($mode eq 'createcourse') {
1.22      raeburn   343:         $param{'formname'} = 'ccrs';
1.1       raeburn   344:     } elsif ($mode eq 'modifycourse') {
1.22      raeburn   345:         $param{'formname'} = 'cmod';
                    346:         $param{'mode'} = 'modifycourse',
                    347:         $param{'curr_autharg'} = $curr_authfield;
                    348:     }
                    349: 
                    350:     my ($setsection_call,$setsections_js);
                    351:     my $finish = "  vf.submit();\n";
                    352:     if ($mode eq 'upload') {
                    353:         if (($context eq 'course') || ($context eq 'domain')) {
                    354:             if ($context eq 'course') {
                    355:                 if ($env{'request.course.sec'} eq '') {
                    356:                     $setsection_call = 'setSections(document.'.$param{'formname'}.');';
                    357:                     $setsections_js =
                    358:                         &setsections_javascript($param{'formname'},$groupslist,
                    359:                                                 $mode);
                    360:                 } else {
                    361:                     $setsection_call = "'ok'";
                    362:                 }
                    363:             } elsif ($context eq 'domain') {
                    364:                 $setsection_call = 'setCourse()';
1.37      raeburn   365:                 $setsections_js = &dc_setcourse_js($param{'formname'},$mode,$context);
1.22      raeburn   366:             }
                    367:             $finish = "  var checkSec = $setsection_call\n".
                    368:                       "  if (checkSec == 'ok') {\n".
                    369:                       "      vf.submit();\n".
                    370:                       "   }\n";
                    371:         }
1.1       raeburn   372:     }
1.22      raeburn   373:     my $authheader = &Apache::loncommon::authform_header(%param);
1.1       raeburn   374: 
                    375:     my %alert = &Apache::lonlocal::texthash
                    376:         (username => 'You need to specify the username field.',
                    377:          authen   => 'You must choose an authentication type.',
                    378:          krb      => 'You need to specify the Kerberos domain.',
                    379:          ipass    => 'You need to specify the initial password.',
                    380:          name     => 'The optional name field was not specified.',
                    381:          snum     => 'The optional ID number field was not specified.',
                    382:          section  => 'The optional section field was not specified.',
                    383:          email    => 'The optional email address field was not specified.',
                    384:          role     => 'The optional role field was not specified.',
                    385:          continue => 'Continue adding users?',
                    386:          );
1.37      raeburn   387:     my $function_name = <<"END";
1.22      raeburn   388: $setsections_js
                    389: 
1.1       raeburn   390: function verify_message (vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail) {
                    391: END
                    392:     my ($authnum,%can_assign) =  &Apache::loncommon::get_assignable_auth($domain);
                    393:     my $auth_checks;
                    394:     if ($mode eq 'createcourse') {
                    395:         $auth_checks .= (<<END);
                    396:     if (vf.autoadds[0].checked == true) {
                    397:         if (current.radiovalue == null || current.radiovalue == 'nochange') {
                    398:             alert('$alert{'authen'}');
                    399:             return;
                    400:         }
                    401:     }
                    402: END
                    403:     } else {
                    404:         $auth_checks .= (<<END);
                    405:     var foundatype=0;
                    406:     if (founduname==0) {
                    407:         alert('$alert{'username'}');
                    408:         return;
                    409:     }
                    410: 
                    411: END
                    412:         if ($authnum > 1) {
                    413:             $auth_checks .= (<<END);
                    414:     if (current.radiovalue == null || current.radiovalue == '' || current.radiovalue == 'nochange') {
                    415:         // They did not check any of the login radiobuttons.
                    416:         alert('$alert{'authen'}');
                    417:         return;
                    418:     }
                    419: END
                    420:         }
                    421:     }
                    422:     if ($mode eq 'createcourse') {
                    423:         $auth_checks .= "
                    424:     if ( (vf.autoadds[0].checked == true) &&
                    425:          (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') ) {
                    426: ";
                    427:     } elsif ($mode eq 'modifycourse') {
                    428:         $auth_checks .= "
                    429:     if (vf.elements[current.argfield].value == null || vf.elements[current.argfield].value == '') {
                    430: ";
                    431:     }
                    432:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    433:         $auth_checks .= (<<END);
                    434:         var alertmsg = '';
                    435:         switch (current.radiovalue) {
                    436:             case 'krb':
                    437:                 alertmsg = '$alert{'krb'}';
                    438:                 break;
                    439:             default:
                    440:                 alertmsg = '';
                    441:         }
                    442:         if (alertmsg != '') {
                    443:             alert(alertmsg);
                    444:             return;
                    445:         }
                    446:     }
                    447: END
                    448:     } else {
                    449:         $auth_checks .= (<<END);
                    450:     foundatype=1;
                    451:     if (current.argfield == null || current.argfield == '') {
                    452:         var alertmsg = '';
1.38    ! raeburn   453:         switch (current.radiovalue) {
1.1       raeburn   454:             case 'krb':
                    455:                 alertmsg = '$alert{'krb'}';
                    456:                 break;
                    457:             case 'loc':
                    458:             case 'fsys':
                    459:                 alertmsg = '$alert{'ipass'}';
                    460:                 break;
                    461:             case 'fsys':
                    462:                 alertmsg = '';
                    463:                 break;
                    464:             default:
                    465:                 alertmsg = '';
                    466:         }
                    467:         if (alertmsg != '') {
                    468:             alert(alertmsg);
                    469:             return;
                    470:         }
                    471:     }
                    472: END
                    473:     }
                    474:     my $section_checks;
                    475:     my $optional_checks = '';
                    476:     if ( ($mode eq 'createcourse') || ($mode eq 'modifycourse') ) {
                    477:         $optional_checks = (<<END);
                    478:     vf.submit();
                    479: }
                    480: END
                    481:     } else {
                    482:         $section_checks = &section_check_js();
                    483:         $optional_checks = (<<END);
                    484:     var message='';
                    485:     if (foundname==0) {
                    486:         message='$alert{'name'}';
                    487:     }
                    488:     if (foundid==0) {
                    489:         if (message!='') {
                    490:             message+='\\n';
                    491:         }
                    492:         message+='$alert{'snum'}';
                    493:     }
                    494:     if (foundsec==0) {
                    495:         if (message!='') {
                    496:             message+='\\n';
                    497:         }
                    498:     }
                    499:     if (foundemail==0) {
                    500:         if (message!='') {
                    501:             message+='\\n';
                    502:         }
                    503:         message+='$alert{'email'}';
                    504:     }
                    505:     if (message!='') {
                    506:         message+= '\\n$alert{'continue'}';
                    507:         if (confirm(message)) {
                    508:             vf.state.value='enrolling';
1.22      raeburn   509:             $finish
1.1       raeburn   510:         }
                    511:     } else {
                    512:         vf.state.value='enrolling';
1.22      raeburn   513:         $finish
1.1       raeburn   514:     }
                    515: }
                    516: END
                    517:     }
1.37      raeburn   518:     my $result = $function_name.$auth_checks.$optional_checks."\n".
                    519:                  $section_checks.$authheader;
1.1       raeburn   520:     return $result;
                    521: }
                    522: ###############################################################
                    523: ###############################################################
                    524: sub upload_manager_javascript_forward_associate {
                    525:     return(<<ENDPICK);
                    526: function verify(vf,sec_caller) {
                    527:     var founduname=0;
                    528:     var foundpwd=0;
                    529:     var foundname=0;
                    530:     var foundid=0;
                    531:     var foundsec=0;
                    532:     var foundemail=0;
                    533:     var foundrole=0;
                    534:     var tw;
                    535:     for (i=0;i<=vf.nfields.value;i++) {
                    536:         tw=eval('vf.f'+i+'.selectedIndex');
                    537:         if (tw==1) { founduname=1; }
                    538:         if ((tw>=2) && (tw<=6)) { foundname=1; }
                    539:         if (tw==7) { foundid=1; }
                    540:         if (tw==8) { foundsec=1; }
                    541:         if (tw==9) { foundpwd=1; }
                    542:         if (tw==10) { foundemail=1; }
                    543:         if (tw==11) { foundrole=1; }
                    544:     }
                    545:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundemail,foundrole);
                    546: }
                    547: 
                    548: //
                    549: // vf = this.form
                    550: // tf = column number
                    551: //
                    552: // values of nw
                    553: //
                    554: // 0 = none
                    555: // 1 = username
                    556: // 2 = names (lastname, firstnames)
                    557: // 3 = fname (firstname)
                    558: // 4 = mname (middlename)
                    559: // 5 = lname (lastname)
                    560: // 6 = gen   (generation)
                    561: // 7 = id
                    562: // 8 = section
                    563: // 9 = ipwd  (password)
                    564: // 10 = email address
                    565: // 11 = role
                    566: 
                    567: function flip(vf,tf) {
                    568:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    569:    var i;
                    570:    // make sure no other columns are labeled the same as this one
                    571:    for (i=0;i<=vf.nfields.value;i++) {
                    572:       if ((i!=tf) && (eval('vf.f'+i+'.selectedIndex')==nw)) {
                    573:           eval('vf.f'+i+'.selectedIndex=0;')
                    574:       }
                    575:    }
                    576:    // If we set this to 'lastname, firstnames', clear out all the ones
                    577:    // set to 'fname','mname','lname','gen' (3,4,5,6) currently.
                    578:    if (nw==2) {
                    579:       for (i=0;i<=vf.nfields.value;i++) {
                    580:          if ((eval('vf.f'+i+'.selectedIndex')>=3) &&
                    581:              (eval('vf.f'+i+'.selectedIndex')<=6)) {
                    582:              eval('vf.f'+i+'.selectedIndex=0;')
                    583:          }
                    584:       }
                    585:    }
                    586:    // If we set this to one of 'fname','mname','lname','gen' (3,4,5,6),
                    587:    // clear out any that are set to 'lastname, firstnames' (2)
                    588:    if ((nw>=3) && (nw<=6)) {
                    589:       for (i=0;i<=vf.nfields.value;i++) {
                    590:          if (eval('vf.f'+i+'.selectedIndex')==2) {
                    591:              eval('vf.f'+i+'.selectedIndex=0;')
                    592:          }
                    593:       }
                    594:    }
                    595:    // If we set the password, make the password form below correspond to
                    596:    // the new value.
                    597:    if (nw==9) {
                    598:        changed_radio('int',document.studentform);
                    599:        set_auth_radio_buttons('int',document.studentform);
                    600:        vf.intarg.value='';
                    601:        vf.krbarg.value='';
                    602:        vf.locarg.value='';
                    603:    }
                    604: }
                    605: 
                    606: function clearpwd(vf) {
                    607:     var i;
                    608:     for (i=0;i<=vf.nfields.value;i++) {
                    609:         if (eval('vf.f'+i+'.selectedIndex')==9) {
                    610:             eval('vf.f'+i+'.selectedIndex=0;')
                    611:         }
                    612:     }
                    613: }
                    614: 
                    615: ENDPICK
                    616: }
                    617: 
                    618: ###############################################################
                    619: ###############################################################
                    620: sub upload_manager_javascript_reverse_associate {
                    621:     return(<<ENDPICK);
                    622: function verify(vf,sec_caller) {
                    623:     var founduname=0;
                    624:     var foundpwd=0;
                    625:     var foundname=0;
                    626:     var foundid=0;
                    627:     var foundsec=0;
                    628:     var foundrole=0;
                    629:     var tw;
                    630:     for (i=0;i<=vf.nfields.value;i++) {
                    631:         tw=eval('vf.f'+i+'.selectedIndex');
                    632:         if (i==0 && tw!=0) { founduname=1; }
                    633:         if (((i>=1) && (i<=5)) && tw!=0 ) { foundname=1; }
                    634:         if (i==6 && tw!=0) { foundid=1; }
                    635:         if (i==7 && tw!=0) { foundsec=1; }
                    636:         if (i==8 && tw!=0) { foundpwd=1; }
                    637:         if (i==9 && tw!=0) { foundrole=1; }
                    638:     }
                    639:     verify_message(vf,founduname,foundpwd,foundname,foundid,foundsec,foundrole);
                    640: }
                    641: 
                    642: function flip(vf,tf) {
                    643:    var nw=eval('vf.f'+tf+'.selectedIndex');
                    644:    var i;
                    645:    // picked the all one name field, reset the other name ones to blank
                    646:    if (tf==1 && nw!=0) {
                    647:       for (i=2;i<=5;i++) {
                    648:          eval('vf.f'+i+'.selectedIndex=0;')
                    649:       }
                    650:    }
                    651:    //picked one of the piecewise name fields, reset the all in
                    652:    //one field to blank
                    653:    if ((tf>=2) && (tf<=5) && (nw!=0)) {
                    654:       eval('vf.f1.selectedIndex=0;')
                    655:    }
                    656:    // intial password specified, pick internal authentication
                    657:    if (tf==8 && nw!=0) {
                    658:        changed_radio('int',document.studentform);
                    659:        set_auth_radio_buttons('int',document.studentform);
                    660:        vf.krbarg.value='';
                    661:        vf.intarg.value='';
                    662:        vf.locarg.value='';
                    663:    }
                    664: }
                    665: 
                    666: function clearpwd(vf) {
                    667:     var i;
                    668:     if (eval('vf.f8.selectedIndex')!=0) {
                    669:         eval('vf.f8.selectedIndex=0;')
                    670:     }
                    671: }
                    672: ENDPICK
                    673: }
                    674: 
                    675: ###############################################################
                    676: ###############################################################
                    677: sub print_upload_manager_footer {
1.22      raeburn   678:     my ($r,$i,$keyfields,$defdom,$today,$halfyear,$context,$permission) = @_;
                    679:     my $form = 'document.studentform';
                    680:     my $formname = 'studentform';
1.1       raeburn   681:     my ($krbdef,$krbdefdom) =
                    682:         &Apache::loncommon::get_kerberos_defaults($defdom);
1.22      raeburn   683:     my %param = ( formname => $form,
1.1       raeburn   684:                   kerb_def_dom => $krbdefdom,
                    685:                   kerb_def_auth => $krbdef
                    686:                   );
                    687:     if (exists($env{'form.ipwd_choice'}) &&
                    688:         defined($env{'form.ipwd_choice'}) &&
                    689:         $env{'form.ipwd_choice'} ne '') {
                    690:         $param{'curr_authtype'} = 'int';
                    691:     }
                    692:     my $krbform = &Apache::loncommon::authform_kerberos(%param);
                    693:     my $intform = &Apache::loncommon::authform_internal(%param);
                    694:     my $locform = &Apache::loncommon::authform_local(%param);
1.22      raeburn   695:     my $date_table = &date_setting_table(undef,undef,$context,undef,
                    696:                                          $formname,$permission);
1.1       raeburn   697:     my $Str = "\n".'<div class="LC_left_float">';
                    698:     $Str .= &hidden_input('nfields',$i);
                    699:     $Str .= &hidden_input('keyfields',$keyfields);
                    700:     $Str .= "<h3>".&mt('Login Type')."</h3>\n";
                    701:     if ($context eq 'domain') {
                    702:         $Str .= '<p>'.&mt('Change authentication for existing users to these settings?').'&nbsp;<span class="LC_nobreak"><label><input type="radio" name="changeauth" value="No" checked="checked" />'.&mt('No').'</label>&nbsp;&nbsp;<label><input type="radio" name="changeauth" value="Yes" />'.&mt('Yes').'</label></span></p>'; 
                    703:     } else {
                    704:         $Str .= "<p>\n".
                    705:             &mt('Note: this will not take effect if the user already exists').
                    706:             &Apache::loncommon::help_open_topic('Auth_Options').
                    707:             "</p>\n";
                    708:     }
                    709:     $Str .= &set_login($defdom,$krbform,$intform,$locform);
                    710:     my ($home_server_pick,$numlib) =
                    711:         &Apache::loncommon::home_server_form_item($defdom,'lcserver',
                    712:                                                   'default','hide');
                    713:     if ($numlib > 1) {
                    714:         $Str .= '<h3>'.&mt('LON-CAPA Home Server for New Users')."</h3>\n".
                    715:                 &mt('LON-CAPA domain: [_1] with home server: [_2]',$defdom,
                    716:                 $home_server_pick).'<br />';
                    717:     } else {
                    718:         $Str .= $home_server_pick;
                    719:     }
                    720:     $Str .= '<h3>'.&mt('Starting and Ending Dates').
                    721:             "</h3>\n";
                    722:     $Str .= "<p>\n".$date_table."</p>\n";
                    723:     if ($context eq 'domain') {
                    724:         $Str .= '<h3>'.&mt('Settings for assigning roles:').'</h3>'."\n".
                    725:                 &mt('Pick the action to take on roles for these users:').'<br /><span class="LC_nobreak"><label><input type="radio" name="roleaction" value="norole" checked="checked" />&nbsp;'.&mt('No role changes').'</label>&nbsp;&nbsp;&nbsp;<label><input type="radio" name="roleaction" value="domain" />&nbsp;'.&mt('Add a domain role').'</label>&nbsp;&nbsp;&nbsp;<label><input type="radio" name="roleaction" value="course" />&nbsp;'.&mt('Add a course role').'</label></span>';
                    726:     }
1.13      raeburn   727:     if ($context eq 'author') {
1.1       raeburn   728:         $Str .= '<h3>'.&mt('Default role')."</h3>\n".
1.25      raeburn   729:                 &mt('Choose the role to assign to users without a value specified in the uploaded file');
1.1       raeburn   730:     } elsif ($context eq 'course') {
                    731:         $Str .= '<h3>'.&mt('Default role and section')."</h3>\n".
1.25      raeburn   732:                 &mt('Choose the role and/or section(s) to assign to users without values specified in the uploaded file');
1.1       raeburn   733:     } else {
1.25      raeburn   734:         $Str .= '<br /><br /><b>'.&mt('Default role and/or section(s)')."</b><br />\n".
                    735:                 &mt('Role and/or section(s) for users without values specified in the uploaded file.');
1.1       raeburn   736:     }
1.22      raeburn   737:     $Str .= '<br />';
                    738:     if (($context eq 'domain') || ($context eq 'author')) {
                    739:         my ($options,$cb_script,$coursepick) = &default_role_selector($context,1);
                    740:         if ($context eq 'domain') {
                    741:             $Str .= '<span class="LC_role_level">'.&mt('Domain Level').'</span><br />'.$options.'<br /><br /><span class="LC_role_level">'.&mt('Course Level').'</span><br />'.$cb_script.$coursepick;
                    742:         } elsif ($context eq 'author') {
                    743:             $Str .= $options;
                    744:         }
1.1       raeburn   745:     } else {
1.22      raeburn   746:         my ($cnum,$cdom) = &get_course_identity();
                    747:         my $rowtitle = &mt('section');
                    748:         my $secbox = &section_picker($cdom,$cnum,'Any',$rowtitle,
                    749:                                      $permission,$context,'upload');
                    750:         $Str .= $secbox."<h3>".&mt('Full Update')."</h3>\n".
                    751:                 '<p><label><input type="checkbox" name="fullup" value="yes">'.
1.36      raeburn   752:                 ' '.&mt('Display students with current/future access who are not in the uploaded file.').'</label><br />'.&mt('Students selected from this list can be dropped.').'</p>'."\n";
1.1       raeburn   753:     }
1.5       raeburn   754:     if ($context eq 'course' || $context eq 'domain') {
                    755:         $Str .= &forceid_change($context);
                    756:     }
1.1       raeburn   757:     $Str .= '</div><div class="LC_clear_float_footer"><br /><input type="button"'.
                    758:               'onClick="javascript:verify(this.form,this.form.csec)" '.
                    759:         'value="Update Users" />'."<br />\n";
                    760:     if ($context eq 'course') {
                    761:         $Str .= &mt('Note: for large courses, this operation may be time '.
                    762:                     'consuming');
                    763:     }
                    764:     $Str .= '</div>';
                    765:     $r->print($Str);
                    766:     return;
                    767: }
                    768: 
1.5       raeburn   769: sub forceid_change {
                    770:     my ($context) = @_;
                    771:     my $output = 
                    772:         "<h3>".&mt('ID/Student Number')."</h3>\n".
                    773:         "<p>\n".'<label><input type="checkbox" name="forceid" value="yes">'.
                    774:         &mt('Disable ID/Student Number Safeguard and Force Change '.
                    775:         'of Conflicting IDs').'</label><br />'."\n".
1.25      raeburn   776:         &mt('(only do if you know what you are doing.)')."\n";
1.5       raeburn   777:     if ($context eq 'domain') {
1.25      raeburn   778:         $output .= '<br /><label><input type="checkbox" name="recurseid"'.
1.5       raeburn   779:                    ' value="yes">'. 
1.13      raeburn   780:   &mt('Update ID/Student Number in courses in which user is Active/Future student,<br />(if forcing change).').
1.25      raeburn   781:                    '</label>'."\n";
1.5       raeburn   782:     }
1.25      raeburn   783:     $output .= '</p>';
1.5       raeburn   784:     return $output;
                    785: }
                    786: 
1.1       raeburn   787: ###############################################################
                    788: ###############################################################
                    789: sub print_upload_manager_form {
1.21      raeburn   790:     my ($r,$context,$permission) = @_;
1.1       raeburn   791:     my $firstLine;
                    792:     my $datatoken;
                    793:     if (!$env{'form.datatoken'}) {
                    794:         $datatoken=&Apache::loncommon::upfile_store($r);
                    795:     } else {
                    796:         $datatoken=$env{'form.datatoken'};
                    797:         &Apache::loncommon::load_tmp_file($r);
                    798:     }
                    799:     my @records=&Apache::loncommon::upfile_record_sep();
                    800:     if($env{'form.noFirstLine'}){
                    801:         $firstLine=shift(@records);
                    802:     }
                    803:     my $total=$#records;
                    804:     my $distotal=$total+1;
                    805:     my $today=time;
                    806:     my $halfyear=$today+15552000;
                    807:     #
                    808:     # Restore memorized settings
                    809:     my $col_setting_names =  { 'username_choice' => 'scalar', # column settings
                    810:                                'names_choice' => 'scalar',
                    811:                                'fname_choice' => 'scalar',
                    812:                                'mname_choice' => 'scalar',
                    813:                                'lname_choice' => 'scalar',
                    814:                                'gen_choice' => 'scalar',
                    815:                                'id_choice' => 'scalar',
                    816:                                'sec_choice' => 'scalar',
                    817:                                'ipwd_choice' => 'scalar',
                    818:                                'email_choice' => 'scalar',
                    819:                                'role_choice' => 'scalar',
                    820:                              };
                    821:     my $defdom = $env{'request.role.domain'};
                    822:     if ($context eq 'course') {
                    823:         &Apache::loncommon::restore_course_settings('enrollment_upload',
                    824:                                                     $col_setting_names);
                    825:     } else {
                    826:         &Apache::loncommon::restore_settings($context,'user_upload',
                    827:                                              $col_setting_names);
                    828:     }
                    829:     #
                    830:     # Determine kerberos parameters as appropriate
                    831:     my ($krbdef,$krbdefdom) =
                    832:         &Apache::loncommon::get_kerberos_defaults($defdom);
                    833:     #
1.22      raeburn   834:     &print_upload_manager_header($r,$datatoken,$distotal,$krbdefdom,$context,
                    835:                                  $permission);
1.1       raeburn   836:     my $i;
                    837:     my $keyfields;
                    838:     if ($total>=0) {
                    839:         my @field=
                    840:             (['username',&mt('Username'),     $env{'form.username_choice'}],
                    841:              ['names',&mt('Last Name, First Names'),$env{'form.names_choice'}],
                    842:              ['fname',&mt('First Name'),      $env{'form.fname_choice'}],
                    843:              ['mname',&mt('Middle Names/Initials'),$env{'form.mname_choice'}],
                    844:              ['lname',&mt('Last Name'),       $env{'form.lname_choice'}],
                    845:              ['gen',  &mt('Generation'),      $env{'form.gen_choice'}],
                    846:              ['id',   &mt('ID/Student Number'),$env{'form.id_choice'}],
                    847:              ['sec',  &mt('Section'),          $env{'form.sec_choice'}],
                    848:              ['ipwd', &mt('Initial Password'),$env{'form.ipwd_choice'}],
                    849:              ['email',&mt('E-mail Address'),   $env{'form.email_choice'}],
                    850:              ['role',&mt('Role'),             $env{'form.role_choice'}]);
                    851:         if ($env{'form.upfile_associate'} eq 'reverse') {
                    852:             &Apache::loncommon::csv_print_samples($r,\@records);
                    853:             $i=&Apache::loncommon::csv_print_select_table($r,\@records,
                    854:                                                           \@field);
                    855:             foreach (@field) {
                    856:                 $keyfields.=$_->[0].',';
                    857:             }
                    858:             chop($keyfields);
                    859:         } else {
                    860:             unshift(@field,['none','']);
                    861:             $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
                    862:                                                             \@field);
                    863:             my %sone=&Apache::loncommon::record_sep($records[0]);
                    864:             $keyfields=join(',',sort(keys(%sone)));
                    865:         }
                    866:     }
                    867:     $r->print('</div>');
                    868:     &print_upload_manager_footer($r,$i,$keyfields,$defdom,$today,$halfyear,
1.22      raeburn   869:                                  $context,$permission);
1.1       raeburn   870: }
                    871: 
                    872: sub setup_date_selectors {
1.22      raeburn   873:     my ($starttime,$endtime,$mode,$nolink,$formname) = @_;
                    874:     if ($formname eq '') {
                    875:         $formname = 'studentform';
                    876:     }
1.1       raeburn   877:     if (! defined($starttime)) {
                    878:         $starttime = time;
                    879:         unless ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                    880:             if (exists($env{'course.'.$env{'request.course.id'}.
                    881:                             '.default_enrollment_start_date'})) {
                    882:                 $starttime = $env{'course.'.$env{'request.course.id'}.
                    883:                                   '.default_enrollment_start_date'};
                    884:             }
                    885:         }
                    886:     }
                    887:     if (! defined($endtime)) {
                    888:         $endtime = time+(6*30*24*60*60); # 6 months from now, approx
                    889:         unless ($mode eq 'createcourse') {
                    890:             if (exists($env{'course.'.$env{'request.course.id'}.
                    891:                             '.default_enrollment_end_date'})) {
                    892:                 $endtime = $env{'course.'.$env{'request.course.id'}.
                    893:                                 '.default_enrollment_end_date'};
                    894:             }
                    895:         }
                    896:     }
1.11      raeburn   897: 
                    898:     my $startdateform = 
1.22      raeburn   899:         &Apache::lonhtmlcommon::date_setter($formname,'startdate',$starttime,
1.11      raeburn   900:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                    901: 
                    902:     my $enddateform = 
1.22      raeburn   903:         &Apache::lonhtmlcommon::date_setter($formname,'enddate',$endtime,
1.11      raeburn   904:             undef,undef,undef,undef,undef,undef,undef,$nolink);
                    905: 
1.1       raeburn   906:     if ($mode eq 'create_enrolldates') {
                    907:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                    908:                                                             'startenroll',
                    909:                                                             $starttime);
                    910:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                    911:                                                           'endenroll',
                    912:                                                           $endtime);
                    913:     }
                    914:     if ($mode eq 'create_defaultdates') {
                    915:         $startdateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                    916:                                                             'startaccess',
                    917:                                                             $starttime);
                    918:         $enddateform = &Apache::lonhtmlcommon::date_setter('ccrs',
                    919:                                                           'endaccess',
                    920:                                                           $endtime);
                    921:     }
                    922:     return ($startdateform,$enddateform);
                    923: }
                    924: 
                    925: 
                    926: sub get_dates_from_form {
                    927:     my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate');
                    928:     my $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate');
                    929:     if ($env{'form.no_end_date'}) {
                    930:         $enddate = 0;
                    931:     }
                    932:     return ($startdate,$enddate);
                    933: }
                    934: 
                    935: sub date_setting_table {
1.22      raeburn   936:     my ($starttime,$endtime,$mode,$bulkaction,$formname,$permission) = @_;
1.11      raeburn   937:     my $nolink;
                    938:     if ($bulkaction) {
                    939:         $nolink = 1;
                    940:     }
                    941:     my ($startform,$endform) = 
1.22      raeburn   942:         &setup_date_selectors($starttime,$endtime,$mode,$nolink,$formname);
1.1       raeburn   943:     my $dateDefault;
                    944:     if ($mode eq 'create_enrolldates' || $mode eq 'create_defaultdates') {
                    945:         $dateDefault = '&nbsp;';
1.13      raeburn   946:     } elsif ($mode ne 'author' && $mode ne 'domain') {
1.11      raeburn   947:         if (($bulkaction eq 'reenable') || 
                    948:             ($bulkaction eq 'activate') || 
1.22      raeburn   949:             ($bulkaction eq 'chgdates') ||
                    950:             ($env{'form.action'} eq 'upload')) {
                    951:             if ($env{'request.course.sec'} eq '') {
                    952:                 $dateDefault = '<span class="LC_nobreak">'.
1.25      raeburn   953:                     '<label><input type="checkbox" name="makedatesdefault" value="1" /> '.
1.22      raeburn   954:                     &mt('make these dates the default access dates for future student enrollment').
                    955:                     '</label></span>';
                    956:             }
1.11      raeburn   957:         }
1.1       raeburn   958:     }
1.11      raeburn   959:     my $perpetual = '<span class="LC_nobreak"><label><input type="checkbox" name="no_end_date"';
1.1       raeburn   960:     if (defined($endtime) && $endtime == 0) {
                    961:         $perpetual .= ' checked';
                    962:     }
1.11      raeburn   963:     $perpetual.= ' /> '.&mt('no ending date').'</label></span>';
1.1       raeburn   964:     if ($mode eq 'create_enrolldates') {
                    965:         $perpetual = '&nbsp;';
                    966:     }
1.11      raeburn   967:     my $result = &Apache::lonhtmlcommon::start_pick_box()."\n";
                    968:     $result .= &Apache::lonhtmlcommon::row_title(&mt('Starting Date'),
                    969:                                                      'LC_oddrow_value')."\n".
                    970:                $startform."\n".
                    971:                &Apache::lonhtmlcommon::row_closure(1).
                    972:                &Apache::lonhtmlcommon::row_title(&mt('Ending Date'), 
                    973:                                                      'LC_oddrow_value')."\n".
                    974:                $endform.'&nbsp;'.$perpetual.
                    975:                &Apache::lonhtmlcommon::row_closure(1).
1.22      raeburn   976:                &Apache::lonhtmlcommon::end_pick_box();
1.1       raeburn   977:     if ($dateDefault) {
                    978:         $result .=  $dateDefault.'<br />'."\n";
                    979:     }
                    980:     return $result;
                    981: }
                    982: 
                    983: sub make_dates_default {
                    984:     my ($startdate,$enddate,$context) = @_;
                    985:     my $result = '';
                    986:     if ($context eq 'course') {
1.17      raeburn   987:         my ($cnum,$cdom) = &get_course_identity();
1.1       raeburn   988:         my $put_result = &Apache::lonnet::put('environment',
                    989:                 {'default_enrollment_start_date'=>$startdate,
1.17      raeburn   990:                  'default_enrollment_end_date'  =>$enddate},$cdom,$cnum);
1.1       raeburn   991:         if ($put_result eq 'ok') {
1.25      raeburn   992:             $result .= &mt('Set default start and end access dates for course.').
1.11      raeburn   993:                        '<br />'."\n";
1.1       raeburn   994:             #
                    995:             # Refresh the course environment
                    996:             &Apache::lonnet::coursedescription($env{'request.course.id'},
                    997:                                                {'freshen_cache' => 1});
                    998:         } else {
1.25      raeburn   999:             $result .= &mt('Unable to set default access dates for course.').":".$put_result.
1.1       raeburn  1000:                        '<br />';
                   1001:         }
                   1002:     }
                   1003:     return $result;
                   1004: }
                   1005: 
                   1006: sub default_role_selector {
1.2       raeburn  1007:     my ($context,$checkpriv) = @_;
1.1       raeburn  1008:     my %customroles;
                   1009:     my ($options,$coursepick,$cb_jscript);
1.13      raeburn  1010:     if ($context ne 'author') {
1.1       raeburn  1011:         %customroles = &my_custom_roles();
                   1012:     }
                   1013: 
                   1014:     my %lt=&Apache::lonlocal::texthash(
                   1015:                     'rol'  => "Role",
                   1016:                     'grs'  => "Section",
                   1017:                     'exs'  => "Existing sections",
                   1018:                     'new'  => "New section",
                   1019:                   );
                   1020:     $options = '<select name="defaultrole">'."\n".
                   1021:                ' <option value="">'.&mt('Please select').'</option>'."\n"; 
                   1022:     if ($context eq 'course') {
1.2       raeburn  1023:         $options .= &default_course_roles($context,$checkpriv,%customroles);
1.13      raeburn  1024:     } elsif ($context eq 'author') {
1.2       raeburn  1025:         my @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1026:         foreach my $role (@roles) {
                   1027:            my $plrole=&Apache::lonnet::plaintext($role);
                   1028:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>'."\n";
                   1029:         }
                   1030:     } elsif ($context eq 'domain') {
1.2       raeburn  1031:         my @roles = &domain_roles($checkpriv);
1.1       raeburn  1032:         foreach my $role (@roles) {
                   1033:            my $plrole=&Apache::lonnet::plaintext($role);
                   1034:            $options .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1035:         }
                   1036:         my $courseform = &Apache::loncommon::selectcourse_link
1.22      raeburn  1037:             ('studentform','dccourse','dcdomain','coursedesc',"$env{'request.role.domain'}",undef,'Course');
1.1       raeburn  1038:         $cb_jscript = 
1.22      raeburn  1039:             &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'},'currsec','studentform');
1.1       raeburn  1040:         $coursepick = &Apache::loncommon::start_data_table().
                   1041:                       &Apache::loncommon::start_data_table_header_row().
                   1042:                       '<th>'.$courseform.'</th><th>'.$lt{'rol'}.'</th>'.
                   1043:                       '<th>'.$lt{'grs'}.'</th>'.
                   1044:                       &Apache::loncommon::end_data_table_header_row().
                   1045:                       &Apache::loncommon::start_data_table_row()."\n".
1.22      raeburn  1046:                       '<td><input type="text" name="coursedesc" value="" onFocus="this.blur();opencrsbrowser('."'studentform','dccourse','dcdomain','coursedesc',''".')" /></td>'."\n".
1.1       raeburn  1047:                       '<td><select name="courserole">'."\n".
1.2       raeburn  1048:                       &default_course_roles($context,$checkpriv,%customroles)."\n".
1.1       raeburn  1049:                       '</select></td><td>'.
                   1050:                       '<table class="LC_createuser">'.
                   1051:                       '<tr class="LC_section_row"><td valign"top">'.
1.22      raeburn  1052:                       $lt{'exs'}.'<br /><select name="currsec">'.
1.1       raeburn  1053:                       ' <option value=""><--'.&mt('Pick course first').
                   1054:                       '</select></td>'.
                   1055:                       '<td>&nbsp;&nbsp;</td>'.
                   1056:                       '<td valign="top">'.$lt{'new'}.'<br />'.
                   1057:                       '<input type="text" name="newsec" value="" size="5" />'.
1.22      raeburn  1058:                       '<input type="hidden" name="groups" value="" />'.
                   1059:                       '<input type="hidden" name="sections" value="" />'.
                   1060:                       '<input type="hidden" name="origdom" value="'.
                   1061:                       $env{'request.role.domain'}.'" />'.
                   1062:                       '<input type="hidden" name="dccourse" value="" />'.
                   1063:                       '<input type="hidden" name="dcdomain" value="" />'.
                   1064:                       '</td></tr></table></td>'.
1.1       raeburn  1065:                       &Apache::loncommon::end_data_table_row().
1.22      raeburn  1066:                       &Apache::loncommon::end_data_table()."\n";
1.1       raeburn  1067:     }
                   1068:     $options .= '</select>';
                   1069:     return ($options,$cb_jscript,$coursepick);
                   1070: }
                   1071: 
                   1072: sub default_course_roles {
1.2       raeburn  1073:     my ($context,$checkpriv,%customroles) = @_;
1.1       raeburn  1074:     my $output;
1.17      raeburn  1075:     my $custom = 1;
                   1076:     my @roles = &course_roles($context,$checkpriv,$custom);
1.1       raeburn  1077:     foreach my $role (@roles) {
1.22      raeburn  1078:         if ($role ne 'cr') {
                   1079:             my $plrole=&Apache::lonnet::plaintext($role);
                   1080:             $output .= '  <option value="'.$role.'">'.$plrole.'</option>';
                   1081:         }
1.1       raeburn  1082:     }
                   1083:     if (keys(%customroles) > 0) {
1.22      raeburn  1084:         if (grep(/^cr$/,@roles)) {
                   1085:             foreach my $cust (sort(keys(%customroles))) {
                   1086:                 my $custrole='cr_'.$env{'user.domain'}.
                   1087:                              '_'.$env{'user.name'}.'_'.$cust;
                   1088:                 $output .= '  <option value="'.$custrole.'">'.$cust.'</option>';
                   1089:             }
1.1       raeburn  1090:         }
                   1091:     }
                   1092:     return $output;
                   1093: }
                   1094: 
                   1095: sub construction_space_roles {
1.2       raeburn  1096:     my ($checkpriv) = @_;
1.17      raeburn  1097:     my @allroles = &roles_by_context('author');
1.1       raeburn  1098:     my @roles;
1.2       raeburn  1099:     if ($checkpriv) {
                   1100:         foreach my $role (@allroles) {
                   1101:             if (&Apache::lonnet::allowed('c'.$role,$env{'user.domain'}.'/'.$env{'user.name'})) { 
                   1102:                 push(@roles,$role); 
                   1103:             }
1.1       raeburn  1104:         }
1.2       raeburn  1105:         return @roles;
                   1106:     } else {
                   1107:         return @allroles;
1.1       raeburn  1108:     }
                   1109: }
                   1110: 
                   1111: sub domain_roles {
1.2       raeburn  1112:     my ($checkpriv) = @_;
1.17      raeburn  1113:     my @allroles = &roles_by_context('domain');
1.1       raeburn  1114:     my @roles;
1.2       raeburn  1115:     if ($checkpriv) {
                   1116:         foreach my $role (@allroles) {
                   1117:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   1118:                 push(@roles,$role);
                   1119:             }
1.1       raeburn  1120:         }
1.2       raeburn  1121:         return @roles;
                   1122:     } else {
                   1123:         return @allroles;
1.1       raeburn  1124:     }
                   1125: }
                   1126: 
                   1127: sub course_roles {
1.17      raeburn  1128:     my ($context,$checkpriv,$custom) = @_;
                   1129:     my @allroles = &roles_by_context('course',$custom);
1.1       raeburn  1130:     my @roles;
                   1131:     if ($context eq 'domain') {
                   1132:         @roles = @allroles;
                   1133:     } elsif ($context eq 'course') {
                   1134:         if ($env{'request.course.id'}) {
1.2       raeburn  1135:             if ($checkpriv) { 
                   1136:                 foreach my $role (@allroles) {
                   1137:                     if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   1138:                         push(@roles,$role);
                   1139:                     } else {
1.22      raeburn  1140:                         if ($role ne 'cc' && $env{'request.course.sec'} ne '') {
                   1141:                             if (&Apache::lonnet::allowed('c'.$role,
1.2       raeburn  1142:                                              $env{'request.course.id'}.'/'.
1.22      raeburn  1143:                                              $env{'request.course.sec'})) {
1.2       raeburn  1144:                                 push(@roles,$role);
                   1145:                             }
1.1       raeburn  1146:                         }
                   1147:                     }
                   1148:                 }
1.2       raeburn  1149:             } else {
                   1150:                 @roles = @allroles;
1.1       raeburn  1151:             }
                   1152:         }
                   1153:     }
                   1154:     return @roles;
                   1155: }
                   1156: 
                   1157: sub curr_role_permissions {
1.2       raeburn  1158:     my ($context,$setting,$checkpriv) = @_; 
1.17      raeburn  1159:     my $custom = 1;
1.1       raeburn  1160:     my @roles;
1.13      raeburn  1161:     if ($context eq 'author') {
1.2       raeburn  1162:         @roles = &construction_space_roles($checkpriv);
1.1       raeburn  1163:     } elsif ($context eq 'domain') {
                   1164:         if ($setting eq 'course') {
1.17      raeburn  1165:             @roles = &course_roles($context,$checkpriv,$custom); 
1.1       raeburn  1166:         } else {
1.2       raeburn  1167:             @roles = &domain_roles($checkpriv);
1.1       raeburn  1168:         }
                   1169:     } elsif ($context eq 'course') {
1.17      raeburn  1170:         @roles = &course_roles($context,$checkpriv,$custom);
1.1       raeburn  1171:     }
                   1172:     return @roles;
                   1173: }
                   1174: 
                   1175: # ======================================================= Existing Custom Roles
                   1176: 
                   1177: sub my_custom_roles {
                   1178:     my %returnhash=();
                   1179:     my %rolehash=&Apache::lonnet::dump('roles');
                   1180:     foreach my $key (keys %rolehash) {
                   1181:         if ($key=~/^rolesdef\_(\w+)$/) {
                   1182:             $returnhash{$1}=$1;
                   1183:         }
                   1184:     }
                   1185:     return %returnhash;
                   1186: }
                   1187: 
1.2       raeburn  1188: sub print_userlist {
                   1189:     my ($r,$mode,$permission,$context,$formname,$totcodes,$codetitles,
                   1190:         $idlist,$idlist_titles) = @_;
                   1191:     my $format = $env{'form.output'};
1.1       raeburn  1192:     if (! exists($env{'form.sortby'})) {
                   1193:         $env{'form.sortby'} = 'username';
                   1194:     }
1.2       raeburn  1195:     if ($env{'form.Status'} !~ /^(Any|Expired|Active|Future)$/) {
                   1196:         $env{'form.Status'} = 'Active';
1.1       raeburn  1197:     }
                   1198:     my $status_select = &Apache::lonhtmlcommon::StatusOptions
1.2       raeburn  1199:         ($env{'form.Status'});
1.1       raeburn  1200: 
1.2       raeburn  1201:     if ($env{'form.showrole'} eq '') {
1.13      raeburn  1202:         if ($context eq 'course') {
                   1203:             $env{'form.showrole'} = 'st';
                   1204:         } else {
                   1205:             $env{'form.showrole'} = 'Any';            
                   1206:         }
1.2       raeburn  1207:     }
1.1       raeburn  1208:     if (! defined($env{'form.output'}) ||
                   1209:         $env{'form.output'} !~ /^(csv|excel|html)$/ ) {
                   1210:         $env{'form.output'} = 'html';
                   1211:     }
                   1212: 
1.2       raeburn  1213:     my @statuses;
                   1214:     if ($env{'form.Status'} eq 'Any') {
                   1215:         @statuses = ('previous','active','future');
                   1216:     } elsif ($env{'form.Status'} eq 'Expired') {
                   1217:         @statuses = ('previous');
                   1218:     } elsif ($env{'form.Status'} eq 'Active') {
                   1219:         @statuses = ('active');
                   1220:     } elsif ($env{'form.Status'} eq 'Future') {
                   1221:         @statuses = ('future');
                   1222:     }
1.1       raeburn  1223: 
1.2       raeburn  1224: #    if ($context eq 'course') { 
                   1225: #        $r->print(&display_adv_courseroles());
                   1226: #    }
1.1       raeburn  1227:     #
                   1228:     # Interface output
1.2       raeburn  1229:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   1230:               '<input type="hidden" name="action" value="'.
1.1       raeburn  1231:               $env{'form.action'}.'" />');
                   1232:     $r->print("<p>\n");
                   1233:     if ($env{'form.action'} ne 'modifystudent') {
                   1234:         my %lt=&Apache::lonlocal::texthash('csv' => "CSV",
                   1235:                                            'excel' => "Excel",
                   1236:                                            'html'  => 'HTML');
                   1237:         my $output_selector = '<select size="1" name="output" >';
                   1238:         foreach my $outputformat ('html','csv','excel') {
                   1239:             my $option = '<option value="'.$outputformat.'" ';
                   1240:             if ($outputformat eq $env{'form.output'}) {
                   1241:                 $option .= 'selected ';
                   1242:             }
                   1243:             $option .='>'.$lt{$outputformat}.'</option>';
                   1244:             $output_selector .= "\n".$option;
                   1245:         }
                   1246:         $output_selector .= '</select>';
                   1247:         $r->print('<label>'.&mt('Output Format: [_1]',$output_selector).'</label>'.('&nbsp;'x3));
                   1248:     }
1.2       raeburn  1249:     $r->print('<label>'.&mt('User Status: [_1]',$status_select).'</label>'.('&nbsp;'x3)."\n");
                   1250:     my $roleselected = '';
                   1251:     if ($env{'form.showrole'} eq 'Any') {
                   1252:        $roleselected = ' selected="selected" '; 
                   1253:     }
1.33      raeburn  1254:     my ($role_select,$cnum,$cdom);
1.2       raeburn  1255:     if ($context eq 'domain') {
                   1256:         $role_select = &domain_roles_select();
                   1257:         $r->print('<label>'.&mt('Role Type: [_1]',$role_select).'</label>');
                   1258:     } else {
                   1259:         $role_select = '<select name="showrole">'."\n".
                   1260:                        '<option value="Any" '.$roleselected.'>'.
                   1261:                        &mt('Any role').'</option>';
                   1262:         my @poss_roles = &curr_role_permissions($context);
                   1263:         foreach my $role (@poss_roles) {
                   1264:             $roleselected = '';
                   1265:             if ($role eq $env{'form.showrole'}) {
                   1266:                 $roleselected = ' selected="selected" ';
                   1267:             }
1.22      raeburn  1268:             my $plrole;
                   1269:             if ($role eq 'cr') {
                   1270:                 $plrole = &mt('Custom role');
                   1271:             } else {
                   1272:                 $plrole=&Apache::lonnet::plaintext($role);
                   1273:             }
1.2       raeburn  1274:             $role_select .= '<option value="'.$role.'"'.$roleselected.'>'.$plrole.'</option>';
                   1275:         }
1.22      raeburn  1276:         $role_select .= '</select>';
1.2       raeburn  1277:         $r->print('<label>'.&mt('Role: [_1]',$role_select).'</label>');
1.33      raeburn  1278:         if ($context eq 'course') {
                   1279:             ($cnum,$cdom) = &get_course_identity();
                   1280:             $r->print(&section_group_filter($cnum,$cdom));
                   1281:         }
1.2       raeburn  1282:     }
                   1283:     if (!(($context eq 'domain') && ($env{'form.roletype'} eq 'course'))) {
1.25      raeburn  1284:         $r->print('&nbsp;'.&list_submit_button(&mt('Update Display')).
                   1285:                   "\n</p>\n");
1.2       raeburn  1286:     }
                   1287:     my ($indexhash,$keylist) = &make_keylist_array();
                   1288:     my (%userlist,%userinfo);
1.3       raeburn  1289:     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
                   1290:         my $courseform =
                   1291:             &Apache::lonhtmlcommon::course_selection($formname,$totcodes,
                   1292:                                          $codetitles,$idlist,$idlist_titles);
                   1293:         $r->print('<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n".
                   1294:                   &Apache::lonhtmlcommon::start_pick_box()."\n".
                   1295:                   &Apache::lonhtmlcommon::row_title(&mt('Select Course(s)'),
                   1296:                                                     'LC_oddrow_value')."\n".
                   1297:                   $courseform."\n".
                   1298:                   &Apache::lonhtmlcommon::row_closure(1).
                   1299:                   &Apache::lonhtmlcommon::end_pick_box().'</p>'.
                   1300:                   '<p>'.&list_submit_button(&mt('Update Display')).
1.34      raeburn  1301:                   "\n".'</p><span class="LC_warning">'.&mt('Warning: data retrieval for multiple courses can take considerable time, as this operation is not currently optimized.').'</span>'."\n");
1.11      raeburn  1302:         if ($env{'form.coursepick'}) {
                   1303:             $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
                   1304:         }
                   1305:     } else {
                   1306:         $r->print('<hr />'.&mt('Searching').' ...<br />&nbsp;<br />');
1.3       raeburn  1307:     }
                   1308:     $r->rflush();
1.1       raeburn  1309:     if ($context eq 'course') {
1.3       raeburn  1310:         my $classlist = &Apache::loncoursedata::get_classlist();
                   1311:         my $secidx = &Apache::loncoursedata::CL_SECTION();
1.22      raeburn  1312:         my $viewablesec = &viewable_section($permission);
1.3       raeburn  1313:         foreach my $student (keys(%{$classlist})) {
1.22      raeburn  1314:             my $section = $classlist->{$student}[$secidx];
                   1315:             if ($viewablesec ne '') {
                   1316:                 if ($section ne $viewablesec) {
1.3       raeburn  1317:                     next;
                   1318:                 } else {
                   1319:                     $userlist{$student} = $classlist->{$student};
1.1       raeburn  1320:                 }
1.3       raeburn  1321:             } else {
                   1322:                 $userlist{$student} = $classlist->{$student};
1.1       raeburn  1323:             }
                   1324:         }
1.2       raeburn  1325:         my $showroles;
                   1326:         if ($env{'form.showrole'} ne 'Any') {
                   1327:             $showroles = [$env{'form.showrole'}];
1.1       raeburn  1328:         } else {
1.2       raeburn  1329:             $showroles = undef;
                   1330:         }
1.22      raeburn  1331:         my $withsec = 1;
1.2       raeburn  1332:         my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
1.22      raeburn  1333:                                     \@statuses,$showroles,undef,$withsec);
1.2       raeburn  1334:         &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11      raeburn  1335:                          \%advrolehash,$permission);
1.2       raeburn  1336:     } else {
                   1337:         my (%cstr_roles,%dom_roles);
1.13      raeburn  1338:         if ($context eq 'author') {
1.2       raeburn  1339:             # List co-authors and assistant co-authors
1.17      raeburn  1340:             my @possroles = &roles_by_context($context);
1.2       raeburn  1341:             %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   1342:                                               \@statuses,\@possroles);
                   1343:             &gather_userinfo($context,$format,\%userlist,$indexhash,\%userinfo,
1.11      raeburn  1344:                              \%cstr_roles,$permission);
1.2       raeburn  1345:         } elsif ($context eq 'domain') {
                   1346:             if ($env{'form.roletype'} eq 'domain') {
                   1347:                 %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'});
                   1348:                 foreach my $key (keys(%dom_roles)) {
                   1349:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1350:                         &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1351:                                          \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1352:                     }
                   1353:                 }
1.13      raeburn  1354:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1355:                 my %dom_roles = &Apache::lonnet::get_domain_roles($env{'request.role.domain'},['au']);
                   1356:                 my %coauthors;
                   1357:                 foreach my $key (keys(%dom_roles)) {
                   1358:                     if (ref($dom_roles{$key}) eq 'HASH') {
                   1359:                         if ($env{'form.showrole'} eq 'au') {
                   1360:                             &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1361:                                              \%userinfo,$dom_roles{$key},$permission);
1.2       raeburn  1362:                         } else {
                   1363:                             my @possroles;
                   1364:                             if ($env{'form.showrole'} eq 'Any') {
1.22      raeburn  1365:                                 @possroles = &roles_by_context('author');
1.2       raeburn  1366:                             } else {
                   1367:                                 @possroles = ($env{'form.showrole'}); 
                   1368:                             }
                   1369:                             foreach my $author (sort(keys(%{$dom_roles{$key}}))) {
1.22      raeburn  1370:                                 my ($role,$authorname,$authordom) = split(/:/,$author,-1);
1.2       raeburn  1371:                                 my $extent = '/'.$authordom.'/'.$authorname;
                   1372:                                 %{$coauthors{$extent}} =
                   1373:                                     &Apache::lonnet::get_my_roles($authorname,
                   1374:                                        $authordom,undef,\@statuses,\@possroles);
                   1375:                             }
                   1376:                             &gather_userinfo($context,$format,\%userlist,
1.11      raeburn  1377:                                      $indexhash,\%userinfo,\%coauthors,$permission);
1.2       raeburn  1378:                         }
                   1379:                     }
                   1380:                 }
                   1381:             } elsif ($env{'form.roletype'} eq 'course') {
                   1382:                 if ($env{'form.coursepick'}) {
                   1383:                     my %courses = &process_coursepick();
                   1384:                     my %allusers; 
                   1385:                     foreach my $cid (keys(%courses)) {
1.17      raeburn  1386:                         my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
1.11      raeburn  1387:                         next if ($cnum eq '' || $cdom eq '');
1.17      raeburn  1388:                         my $custom = 1;
1.2       raeburn  1389:                         my (@roles,@sections,%access,%users,%userdata,
1.6       albertel 1390:                             %statushash);
1.2       raeburn  1391:                         if ($env{'form.showrole'} eq 'Any') {
1.17      raeburn  1392:                             @roles = &course_roles($context,undef,$custom);
1.2       raeburn  1393:                         } else {
                   1394:                             @roles = ($env{'form.showrole'});
                   1395:                         }
                   1396:                         foreach my $role (@roles) {
                   1397:                             %{$users{$role}} = ();
                   1398:                         }
                   1399:                         foreach my $type (@statuses) {
                   1400:                             $access{$type} = $type;
                   1401:                         }
                   1402:                         &Apache::loncommon::get_course_users($cdom,$cnum,\%access,\@roles,\@sections,\%users,\%userdata,\%statushash);
                   1403:                         foreach my $user (keys(%userdata)) {
                   1404:                             next if (ref($userinfo{$user}) eq 'HASH');
                   1405:                             foreach my $item ('fullname','id') {
                   1406:                                 $userinfo{$user}{$item} = $userdata{$user}[$indexhash->{$item}];
                   1407:                             }
                   1408:                         }
                   1409:                         foreach my $role (keys(%users)) {
                   1410:                             foreach my $user (keys(%{$users{$role}})) {
                   1411:                                 my $uniqid = $user.':'.$role;
                   1412:                                 $allusers{$uniqid}{$cid} = { desc => $cdesc,
                   1413:                                                              secs  => $statushash{$user}{$role},
                   1414:                                                            };
                   1415:                             }
                   1416:                         }
                   1417:                     }
                   1418:                     &gather_userinfo($context,$format,\%userlist,$indexhash,
1.11      raeburn  1419:                                      \%userinfo,\%allusers,$permission);
1.2       raeburn  1420:                 } else {
1.10      raeburn  1421:                     $r->print('<input type="hidden" name="phase" value="'.
                   1422:                               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1423:                     return;
                   1424:                 }
1.1       raeburn  1425:             }
                   1426:         }
1.3       raeburn  1427:     }
                   1428:     if (keys(%userlist) == 0) {
1.13      raeburn  1429:         if ($context eq 'author') {
1.3       raeburn  1430:             $r->print(&mt('There are no co-authors to display.')."\n");
                   1431:         } elsif ($context eq 'domain') {
                   1432:             if ($env{'form.roletype'} eq 'domain') {
                   1433:                 $r->print(&mt('There are no users with domain roles to display.')."\n");
1.13      raeburn  1434:             } elsif ($env{'form.roletype'} eq 'author') {
1.3       raeburn  1435:                 $r->print(&mt('There are no authors or co-authors to display.')."\n");
                   1436:             } elsif ($env{'form.roletype'} eq 'course') {
                   1437:                 $r->print(&mt('There are no course users to display')."\n"); 
1.2       raeburn  1438:             }
1.3       raeburn  1439:         } elsif ($context eq 'course') {
                   1440:             $r->print(&mt('There are no course users to display.')."\n");
                   1441:         }
                   1442:     } else {
                   1443:         # Print out the available choices
1.4       raeburn  1444:         my $usercount;
1.3       raeburn  1445:         if ($env{'form.action'} eq 'modifystudent') {
1.10      raeburn  1446:             ($usercount) = &show_users_list($r,$context,'view',$permission,
1.4       raeburn  1447:                                  $env{'form.Status'},\%userlist,$keylist);
1.1       raeburn  1448:         } else {
1.4       raeburn  1449:             ($usercount) = &show_users_list($r,$context,$env{'form.output'},
1.10      raeburn  1450:                                $permission,$env{'form.Status'},\%userlist,$keylist);
1.4       raeburn  1451:         }
                   1452:         if (!$usercount) {
                   1453:             $r->print('<br />'.&mt('There are no users matching the search criteria.')); 
1.2       raeburn  1454:         }
                   1455:     }
1.10      raeburn  1456:     $r->print('<input type="hidden" name="phase" value="'.
                   1457:               $env{'form.phase'}.'" /></form>');
1.2       raeburn  1458: }
                   1459: 
1.33      raeburn  1460: sub section_group_filter {
                   1461:     my ($cnum,$cdom) = @_;
                   1462:     my @filters;
                   1463:     if ($env{'request.course.sec'} eq '') {
                   1464:         @filters = ('sec');
                   1465:     }
                   1466:     push(@filters,'grp');
                   1467:     my %name = (
                   1468:                  sec => 'secfilter',
                   1469:                  grp => 'grpfilter',
                   1470:                );
                   1471:     my %title = &Apache::lonlocal::texthash (
                   1472:                                               sec  => 'Section(s)',
                   1473:                                               grp  => 'Group(s)',
                   1474:                                               all  => 'all',
                   1475:                                               none => 'none',
                   1476:                                             );
                   1477:     my ($output,@options);
                   1478:     foreach my $item (@filters) {
                   1479:         my $markup; 
                   1480:         if ($env{'form.'.$name{$item}} eq '') {
                   1481:             $env{'form.'.$name{$item}} = 'all';
                   1482:         }
                   1483:         if ($item eq 'sec') {
                   1484:             if ($env{'form.showrole'} eq 'cc') {
                   1485:                 $env{'form.'.$name{$item}} = 'none';
                   1486:             }
                   1487:             my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   1488:             @options = sort(keys(%sections_count));
                   1489:         } elsif ($item eq 'grp') {
                   1490:             my %curr_groups = &Apache::longroup::coursegroups();
                   1491:             @options = sort(keys(%curr_groups));
                   1492:         }
                   1493:         if (@options > 0) {
                   1494:             my $currsel;
                   1495:             $markup = '<select name="'.$name{$item}.'" />'."\n";
                   1496:             foreach my $option ('all','none',@options) { 
                   1497:                 $currsel = '';
                   1498:                 if ($env{'form.'.$name{$item}} eq $option) {
                   1499:                     $currsel = ' selected="selected" ';
                   1500:                 }
                   1501:                 $markup .= ' <option value="'.$option.'"'.$currsel.'>';
                   1502:                 if (($option eq 'all') || ($option eq 'none')) {
                   1503:                     $markup .= $title{$option};
                   1504:                 } else {
                   1505:                     $markup .= $option;
                   1506:                 }   
                   1507:                 $markup .= '</option>'."\n";
                   1508:             }
                   1509:             $markup .= '</select>'."\n";
                   1510:             $output .= ('&nbsp;'x3).'<label>'.$title{$item}.': '.$markup.'</label>';
                   1511:         }
                   1512:     }
                   1513:     return $output;
                   1514: }
                   1515: 
1.2       raeburn  1516: sub list_submit_button {
                   1517:     my ($text) = @_;
1.11      raeburn  1518:     return '<input type="button" name="updatedisplay" value="'.$text.'" onclick="javascript:display_update()" />';
1.2       raeburn  1519: }
                   1520: 
                   1521: sub gather_userinfo {
1.11      raeburn  1522:     my ($context,$format,$userlist,$indexhash,$userinfo,$rolehash,$permission) = @_;
1.2       raeburn  1523:     foreach my $item (keys(%{$rolehash})) {
                   1524:         my %userdata;
1.22      raeburn  1525:         if ($context eq 'author') { 
1.2       raeburn  1526:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1527:                 split(/:/,$item);
                   1528:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1529:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1530:                                $item,$userlist);
1.22      raeburn  1531:         } elsif ($context eq 'course') {
                   1532:             my $viewablesec = &viewable_section($permission);
                   1533:             ($userdata{'username'},$userdata{'domain'},$userdata{'role'},
                   1534:              $userdata{'section'}) = split(/:/,$item,-1);
                   1535:             ($userdata{'start'},$userdata{'end'})=split(/:/,$rolehash->{$item});
                   1536:             if (($viewablesec ne '') && ($userdata{'section'} ne '')) {
                   1537:                 next if ($viewablesec ne $userdata{'section'});
                   1538:             }
1.24      raeburn  1539:             &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1540:                                $item,$userlist);
1.2       raeburn  1541:         } elsif ($context eq 'domain') {
                   1542:             if ($env{'form.roletype'} eq 'domain') {
                   1543:                 ($userdata{'role'},$userdata{'username'},$userdata{'domain'}) =
                   1544:                     split(/:/,$item);
                   1545:                 ($userdata{'end'},$userdata{'start'})=split(/:/,$rolehash->{$item});
1.24      raeburn  1546:                 &build_user_record($context,\%userdata,$userinfo,$indexhash,
                   1547:                                    $item,$userlist);
1.13      raeburn  1548:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  1549:                 if (ref($rolehash->{$item}) eq 'HASH') {
                   1550:                     $userdata{'extent'} = $item;
                   1551:                     foreach my $key (keys(%{$rolehash->{$item}})) {
                   1552:                         ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =  split(/:/,$key);
                   1553:                         ($userdata{'start'},$userdata{'end'}) = 
                   1554:                             split(/:/,$rolehash->{$item}{$key});
                   1555:                         my $uniqid = $key.':'.$item;
1.25      raeburn  1556:                         &build_user_record($context,\%userdata,$userinfo,
                   1557:                                            $indexhash,$uniqid,$userlist);
1.2       raeburn  1558:                     }
                   1559:                 }
                   1560:             } elsif ($env{'form.roletype'} eq 'course') {
                   1561:                 ($userdata{'username'},$userdata{'domain'},$userdata{'role'}) =
                   1562:                     split(/:/,$item);
                   1563:                 if (ref($rolehash->{$item}) eq 'HASH') {
1.11      raeburn  1564:                     my $numcids = keys(%{$rolehash->{$item}});
1.2       raeburn  1565:                     foreach my $cid (sort(keys(%{$rolehash->{$item}}))) {
                   1566:                         if (ref($rolehash->{$item}{$cid}) eq 'HASH') {
                   1567:                             my $spanstart = '';
                   1568:                             my $spanend = '; ';
                   1569:                             my $space = ', ';
                   1570:                             if ($format eq 'html' || $format eq 'view') {
                   1571:                                 $spanstart = '<span class="LC_nobreak">';
1.23      raeburn  1572:                                 # FIXME: actions on courses disabled for now
                   1573: #                                if ($permission->{'cusr'}) {
                   1574: #                                    if ($numcids > 1) {
1.25      raeburn  1575: #                                        $spanstart .= '<input type="radio" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1576: #                                    } else {
1.25      raeburn  1577: #                                        $spanstart .= '<input type="hidden" name="'.$item.'" value="'.$cid.'" />&nbsp;';
1.23      raeburn  1578: #                                    }
                   1579: #                                }
1.2       raeburn  1580:                                 $spanend = '</span><br />';
                   1581:                                 $space = ',&nbsp;';
                   1582:                             }
                   1583:                             $userdata{'extent'} .= $spanstart.
                   1584:                                     $rolehash->{$item}{$cid}{'desc'}.$space;
                   1585:                             if (ref($rolehash->{$item}{$cid}{'secs'}) eq 'HASH') { 
                   1586:                                 foreach my $sec (sort(keys(%{$rolehash->{$item}{$cid}{'secs'}}))) {
1.25      raeburn  1587:                                     if (($env{'form.Status'} eq 'Any') ||
                   1588:                                         ($env{'form.Status'} eq $rolehash->{$item}{$cid}{'secs'}{$sec})) {
                   1589:                                         $userdata{'extent'} .= $sec.$space.$rolehash->{$item}{$cid}{'secs'}{$sec}.$spanend;
                   1590:                                         $userdata{'status'} = $rolehash->{$item}{$cid}{'secs'}{$sec};
                   1591:                                     }
1.2       raeburn  1592:                                 }
                   1593:                             }
                   1594:                         }
                   1595:                     }
                   1596:                 }
1.25      raeburn  1597:                 if ($userdata{'status'} ne '') {
                   1598:                     &build_user_record($context,\%userdata,$userinfo,
                   1599:                                        $indexhash,$item,$userlist);
                   1600:                 }
1.2       raeburn  1601:             }
                   1602:         }
                   1603:     }
                   1604:     return;
                   1605: }
                   1606: 
                   1607: sub build_user_record {
1.24      raeburn  1608:     my ($context,$userdata,$userinfo,$indexhash,$record_key,$userlist) = @_;
1.11      raeburn  1609:     next if ($userdata->{'start'} eq '-1' && $userdata->{'end'} eq '-1');
1.24      raeburn  1610:     if (!(($context eq 'domain') && ($env{'form.roletype'} eq 'course'))) {
                   1611:         &process_date_info($userdata);
                   1612:     }
1.2       raeburn  1613:     my $username = $userdata->{'username'};
                   1614:     my $domain = $userdata->{'domain'};
                   1615:     if (ref($userinfo->{$username.':'.$domain}) eq 'HASH') {
1.24      raeburn  1616:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
1.2       raeburn  1617:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   1618:     } else {
                   1619:         &aggregate_user_info($domain,$username,$userinfo);
                   1620:         $userdata->{'fullname'} = $userinfo->{$username.':'.$domain}{'fullname'};
                   1621:         $userdata->{'id'} = $userinfo->{$username.':'.$domain}{'id'};
                   1622:     }
                   1623:     foreach my $key (keys(%{$indexhash})) {
                   1624:         if (defined($userdata->{$key})) {
                   1625:             $userlist->{$record_key}[$indexhash->{$key}] = $userdata->{$key};
                   1626:         }
                   1627:     }
                   1628:     return;
                   1629: }
                   1630: 
                   1631: sub courses_selector {
                   1632:     my ($cdom,$formname) = @_;
                   1633:     my %coursecodes = ();
                   1634:     my %codes = ();
                   1635:     my @codetitles = ();
                   1636:     my %cat_titles = ();
                   1637:     my %cat_order = ();
                   1638:     my %idlist = ();
                   1639:     my %idnums = ();
                   1640:     my %idlist_titles = ();
                   1641:     my $caller = 'global';
                   1642:     my $format_reply;
                   1643:     my $jscript = '';
                   1644: 
1.7       albertel 1645:     my $totcodes = 0;
                   1646:     $totcodes =
1.2       raeburn  1647:         &Apache::courseclassifier::retrieve_instcodes(\%coursecodes,
                   1648:                                                       $cdom,$totcodes);
                   1649:     if ($totcodes > 0) {
                   1650:         $format_reply =
                   1651:              &Apache::lonnet::auto_instcode_format($caller,$cdom,\%coursecodes,
                   1652:                                 \%codes,\@codetitles,\%cat_titles,\%cat_order);
                   1653:         if ($format_reply eq 'ok') {
                   1654:             my $numtypes = @codetitles;
                   1655:             &Apache::courseclassifier::build_code_selections(\%codes,\@codetitles,\%cat_titles,\%cat_order,\%idlist,\%idnums,\%idlist_titles);
                   1656:             my ($scripttext,$longtitles) = &Apache::courseclassifier::javascript_definitions(\@codetitles,\%idlist,\%idlist_titles,\%idnums,\%cat_titles);
                   1657:             my $longtitles_str = join('","',@{$longtitles});
                   1658:             my $allidlist = $idlist{$codetitles[0]};
                   1659:             $jscript .= &Apache::courseclassifier::courseset_js_start($formname,$longtitles_str,$allidlist);
                   1660:             $jscript .= $scripttext;
                   1661:             $jscript .= &Apache::courseclassifier::javascript_code_selections($formname,@codetitles);
                   1662:         }
                   1663:     }
                   1664:     my $cb_jscript = &Apache::loncommon::coursebrowser_javascript($cdom);
                   1665: 
                   1666:     my %elements = (
                   1667:                      Year => 'selectbox',
                   1668:                      coursepick => 'radio',
                   1669:                      coursetotal => 'text',
                   1670:                      courselist => 'text',
                   1671:                    );
                   1672:     $jscript .= &Apache::lonhtmlcommon::set_form_elements(\%elements);
                   1673:     if ($env{'form.coursepick'} eq 'category') {
                   1674:         $jscript .= qq|
                   1675: function setCourseCat(formname) {
                   1676:     if (formname.Year.options[formname.Year.selectedIndex].value == -1) {
                   1677:         return;
                   1678:     }
                   1679:     courseSet('Year');
                   1680:     for (var j=0; j<formname.Semester.length; j++) {
                   1681:         if (formname.Semester.options[j].value == "$env{'form.Semester'}") {
                   1682:             formname.Semester.options[j].selected = true;
                   1683:         }
                   1684:     }
                   1685:     if (formname.Semester.options[formname.Semester.selectedIndex].value == -1) {
                   1686:         return;
                   1687:     }
                   1688:     courseSet('Semester');
                   1689:     for (var j=0; j<formname.Department.length; j++) {
                   1690:         if (formname.Department.options[j].value == "$env{'form.Department'}") {            formname.Department.options[j].selected = true;
                   1691:         }
                   1692:     }
                   1693:     if (formname.Department.options[formname.Department.selectedIndex].value == -1) {
                   1694:         return;
                   1695:     }
                   1696:     courseSet('Department');
                   1697:     for (var j=0; j<formname.Number.length; j++) {
                   1698:         if (formname.Number.options[j].value == "$env{'form.Number'}") {
                   1699:             formname.Number.options[j].selected = true;
                   1700:         }
                   1701:     }
                   1702: }
                   1703: |;
                   1704:     }
                   1705:     return ($cb_jscript,$jscript,$totcodes,\@codetitles,\%idlist,
                   1706:             \%idlist_titles);
                   1707: }
                   1708: 
                   1709: sub course_selector_loadcode {
                   1710:     my ($formname) = @_;
                   1711:     my $loadcode;
                   1712:     if ($env{'form.coursepick'} ne '') {
                   1713:         $loadcode = 'javascript:setFormElements(document.'.$formname.')';
                   1714:         if ($env{'form.coursepick'} eq 'category') {
                   1715:             $loadcode .= ';javascript:setCourseCat(document.'.$formname.')';
                   1716:         }
                   1717:     }
                   1718:     return $loadcode;
                   1719: }
                   1720: 
                   1721: sub process_coursepick {
                   1722:     my $coursefilter = $env{'form.coursepick'};
                   1723:     my $cdom = $env{'request.role.domain'};
                   1724:     my %courses;
                   1725:     if ($coursefilter eq 'all') {
                   1726:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.','.','.','.',
                   1727:                                                  undef,undef,'Course');
                   1728:     } elsif ($coursefilter eq 'category') {
                   1729:         my $instcode = &instcode_from_coursefilter();
                   1730:         %courses = &Apache::lonnet::courseiddump($cdom,'.','.',$instcode,'.','.',
                   1731:                                                  undef,undef,'Course');
                   1732:     } elsif ($coursefilter eq 'specific') {
                   1733:         if ($env{'form.coursetotal'} > 1) {
                   1734:             my @course_ids = split(/&&/,$env{'form.courselist'});
                   1735:             foreach my $cid (@course_ids) {
                   1736:                 $courses{$cid} = '';
1.1       raeburn  1737:             }
1.2       raeburn  1738:         } else {
                   1739:             $courses{$env{'form.courselist'}} = '';
1.1       raeburn  1740:         }
1.2       raeburn  1741:     }
                   1742:     return %courses;
                   1743: }
                   1744: 
                   1745: sub instcode_from_coursefilter {
                   1746:     my $instcode = '';
                   1747:     my @cats = ('Semester','Year','Department','Number');
                   1748:     foreach my $category (@cats) {
                   1749:         if (defined($env{'form.'.$category})) {
                   1750:             unless ($env{'form.'.$category} eq '-1') {
                   1751:                 $instcode .= $env{'form.'.$category};
                   1752:            }
                   1753:         }
                   1754:     }
                   1755:     if ($instcode eq '') {
                   1756:         $instcode = '.';
                   1757:     }
                   1758:     return $instcode;
                   1759: }
                   1760: 
                   1761: sub display_adv_courseroles {
                   1762:     my $output;
                   1763:     #
                   1764:     # List course personnel
                   1765:     my %coursepersonnel = 
                   1766:        &Apache::lonnet::get_course_adv_roles($env{'request.course.id'});
                   1767:     #
                   1768:     $output = '<br />'.&Apache::loncommon::start_data_table();
                   1769:     foreach my $role (sort(keys(%coursepersonnel))) {
                   1770:         next if ($role =~ /^\s*$/);
                   1771:         $output .= &Apache::loncommon::start_data_table_row().
                   1772:                   '<td>'.$role.'</td><td>';
                   1773:         foreach my $user (split(',',$coursepersonnel{$role})) {
                   1774:             my ($puname,$pudom)=split(':',$user);
                   1775:             $output .= ' '.&Apache::loncommon::aboutmewrapper(
                   1776:                        &Apache::loncommon::plainname($puname,$pudom),
                   1777:                        $puname,$pudom);
                   1778:         }
                   1779:         $output .= '</td>'.&Apache::loncommon::end_data_table_row();
                   1780:     }
                   1781:     $output .= &Apache::loncommon::end_data_table();
                   1782: }
                   1783: 
                   1784: sub make_keylist_array {
                   1785:     my ($index,$keylist);
                   1786:     $index->{'domain'} = &Apache::loncoursedata::CL_SDOM();
                   1787:     $index->{'username'} = &Apache::loncoursedata::CL_SNAME();
                   1788:     $index->{'end'} = &Apache::loncoursedata::CL_END();
                   1789:     $index->{'start'} = &Apache::loncoursedata::CL_START();
                   1790:     $index->{'id'} = &Apache::loncoursedata::CL_ID();
                   1791:     $index->{'section'} = &Apache::loncoursedata::CL_SECTION();
                   1792:     $index->{'fullname'} = &Apache::loncoursedata::CL_FULLNAME();
                   1793:     $index->{'status'} = &Apache::loncoursedata::CL_STATUS();
                   1794:     $index->{'type'} = &Apache::loncoursedata::CL_TYPE();
                   1795:     $index->{'lockedtype'} = &Apache::loncoursedata::CL_LOCKEDTYPE();
                   1796:     $index->{'groups'} = &Apache::loncoursedata::CL_GROUP();
                   1797:     $index->{'email'} = &Apache::loncoursedata::CL_PERMANENTEMAIL();
                   1798:     $index->{'role'} = &Apache::loncoursedata::CL_ROLE();
                   1799:     $index->{'extent'} = &Apache::loncoursedata::CL_EXTENT();
                   1800:     foreach my $key (keys(%{$index})) {
                   1801:         $keylist->[$index->{$key}] = $key;
                   1802:     }
                   1803:     return ($index,$keylist);
                   1804: }
                   1805: 
                   1806: sub aggregate_user_info {
                   1807:     my ($udom,$uname,$userinfo) = @_;
                   1808:     my %info=&Apache::lonnet::get('environment',
                   1809:                                   ['firstname','middlename',
                   1810:                                    'lastname','generation','id'],
                   1811:                                    $udom,$uname);
                   1812:     my ($tmp) = keys(%info);
                   1813:     my ($fullname,$id);
                   1814:     if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   1815:         $fullname = 'not available';
                   1816:         $id = 'not available';
                   1817:         &Apache::lonnet::logthis('unable to retrieve environment '.
                   1818:                                  'for '.$uname.':'.$udom);
1.1       raeburn  1819:     } else {
1.2       raeburn  1820:         $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
                   1821:         $id = $info{'id'};
                   1822:     }
                   1823:     $userinfo->{$uname.':'.$udom} = { 
                   1824:                                       fullname => $fullname,
                   1825:                                       id       => $id,
                   1826:                                     };
                   1827:     return;
                   1828: }
1.1       raeburn  1829: 
1.2       raeburn  1830: sub process_date_info {
                   1831:     my ($userdata) = @_;
                   1832:     my $now = time;
                   1833:     $userdata->{'status'} = 'Active';
                   1834:     if ($userdata->{'start'} > 0) {
                   1835:         if ($now < $userdata->{'start'}) {
                   1836:             $userdata->{'status'} = 'Future';
                   1837:         }
1.1       raeburn  1838:     }
1.2       raeburn  1839:     if ($userdata->{'end'} > 0) {
                   1840:         if ($now > $userdata->{'end'}) {
                   1841:             $userdata->{'status'} = 'Expired';
                   1842:         }
                   1843:     }
                   1844:     return;
1.1       raeburn  1845: }
                   1846: 
                   1847: sub show_users_list {
1.10      raeburn  1848:     my ($r,$context,$mode,$permission,$statusmode,$userlist,$keylist)=@_;
1.1       raeburn  1849:     #
                   1850:     # Variables for excel output
                   1851:     my ($excel_workbook, $excel_sheet, $excel_filename,$row,$format);
                   1852:     #
                   1853:     # Variables for csv output
                   1854:     my ($CSVfile,$CSVfilename);
                   1855:     #
                   1856:     my $sortby = $env{'form.sortby'};
1.3       raeburn  1857:     my @sortable = ('username','domain','id','fullname','start','end','email','role');
1.2       raeburn  1858:     if ($context eq 'course') {
1.3       raeburn  1859:         push(@sortable,('section','groups','type'));
1.2       raeburn  1860:     } else {
1.3       raeburn  1861:         push(@sortable,'extent');
                   1862:     }
                   1863:     if (!grep(/^\Q$sortby\E$/,@sortable)) {
                   1864:         $sortby = 'username';
1.1       raeburn  1865:     }
1.22      raeburn  1866:     my $setting = $env{'form.roletype'};
1.35      raeburn  1867:     my ($cid,$cdom,$cnum,$classgroups,$displayphotos,$displayclickers);
1.1       raeburn  1868:     if ($context eq 'course') {
1.22      raeburn  1869:         $cid = $env{'request.course.id'};
1.17      raeburn  1870:         ($cnum,$cdom) = &get_course_identity($cid);
1.2       raeburn  1871:         ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   1872:                                      $userlist,$keylist,$cdom,$cnum);
1.16      raeburn  1873:         if ($mode eq 'autoenroll') {
                   1874:             $env{'form.showrole'} = 'st';
                   1875:         } else {
                   1876:             if (! exists($env{'form.displayphotos'})) {
                   1877:                 $env{'form.displayphotos'} = 'off';
                   1878:             }
                   1879:             $displayphotos = $env{'form.displayphotos'};
                   1880:             if (! exists($env{'form.displayclickers'})) {
                   1881:                 $env{'form.displayclickers'} = 'off';
                   1882:             }
                   1883:             $displayclickers = $env{'form.displayclickers'};
                   1884:             if ($env{'course.'.$cid.'.internal.showphoto'}) {
                   1885:                 $r->print('
1.1       raeburn  1886: <script type="text/javascript">
                   1887: function photowindow(photolink) {
                   1888:     var title = "Photo_Viewer";
                   1889:     var options = "scrollbars=1,resizable=1,menubar=0";
                   1890:     options += ",width=240,height=240";
                   1891:     stdeditbrowser = open(photolink,title,options,"1");
                   1892:     stdeditbrowser.focus();
                   1893: }
                   1894: </script>
1.16      raeburn  1895:                ');
                   1896:             }
                   1897:             $r->print(<<END);
1.1       raeburn  1898: <input type="hidden" name="displayphotos" value="$displayphotos" />
                   1899: <input type="hidden" name="displayclickers" value="$displayclickers" />
                   1900: END
1.16      raeburn  1901:         }
1.1       raeburn  1902:     }
1.16      raeburn  1903:     if ($mode ne 'autoenroll') {
1.11      raeburn  1904:         my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
                   1905:         my $alert = &mt("You must select at least one user by checking a user's 'Select' checkbox");
1.25      raeburn  1906:         my $singconfirm = &mt(' for a single user?');
                   1907:         my $multconfirm = &mt(' for multiple users?');
1.11      raeburn  1908:         my $date_sec_selector = &date_section_javascript($context,$setting,$statusmode); 
1.1       raeburn  1909:         $r->print(<<END);
1.10      raeburn  1910: 
                   1911: <script type="text/javascript" language="Javascript">
1.11      raeburn  1912: $check_uncheck_js
                   1913: 
                   1914: function verify_action (field) {
                   1915:     var numchecked = 0;
                   1916:     var singconf = '$singconfirm';
                   1917:     var multconf = '$multconfirm';
                   1918:     if (field.length > 0) {
                   1919:         for (i = 0; i < field.length; i++) {
                   1920:             if (field[i].checked == true) {
                   1921:                numchecked ++;
                   1922:             }
                   1923:         }
                   1924:     } else {
                   1925:         if (field.checked == true) {
                   1926:             numchecked ++;
                   1927:         }
                   1928:     }
                   1929:     if (numchecked == 0) {
                   1930:         alert("$alert");
                   1931:     } 
                   1932:     else {
                   1933:         var message = document.studentform.bulkaction[document.studentform.bulkaction.selectedIndex].text;
                   1934:         if (numchecked == 1) { 
                   1935:             message += singconf;
                   1936:         } 
                   1937:         else {
                   1938:             message += multconf; 
                   1939:         }
                   1940:         if (confirm(message)) {
                   1941:             document.studentform.phase.value = 'bulkchange';
                   1942:             document.studentform.submit();
                   1943:         }
                   1944:     }
                   1945: }
1.10      raeburn  1946: 
                   1947: function username_display_launch(username,domain) {
                   1948:     var target;
                   1949:     for (var i=0; i<document.studentform.usernamelink.length; i++) {
                   1950:         if (document.studentform.usernamelink[i].checked) {
                   1951:             target = document.studentform.usernamelink[i].value;
                   1952:         }
                   1953:     }
                   1954:     if (target == 'modify') {
                   1955:         document.studentform.srchterm.value=username;
                   1956:         document.studentform.srchdomain.value=domain;
                   1957:         document.studentform.phase.value='get_user_info';
                   1958:         document.studentform.action.value = 'singleuser';
                   1959:         document.studentform.submit();
                   1960:     }
                   1961:     else {
                   1962:         document.location.href = '/adm/'+domain+'/'+username+'/aboutme';
                   1963:     }
                   1964: }
                   1965: </script>
1.11      raeburn  1966: $date_sec_selector
1.1       raeburn  1967: <input type="hidden" name="state" value="$env{'form.state'}" />
                   1968: END
                   1969:     }
                   1970:     $r->print(<<END);
                   1971: <input type="hidden" name="sortby" value="$sortby" />
                   1972: END
                   1973: 
                   1974:     my %lt=&Apache::lonlocal::texthash(
                   1975:                        'username'   => "username",
                   1976:                        'domain'     => "domain",
                   1977:                        'id'         => 'ID',
                   1978:                        'fullname'   => "name",
                   1979:                        'section'    => "section",
                   1980:                        'groups'     => "active groups",
                   1981:                        'start'      => "start date",
                   1982:                        'end'        => "end date",
                   1983:                        'status'     => "status",
1.2       raeburn  1984:                        'role'       => "role",
1.1       raeburn  1985:                        'type'       => "enroll type/action",
                   1986:                        'email'      => "email address",
                   1987:                        'clicker'    => "clicker id",
                   1988:                        'photo'      => "photo",
1.2       raeburn  1989:                        'extent'     => "extent",
1.11      raeburn  1990:                        'pr'         => "Proceed",
                   1991:                        'ca'         => "check all",
                   1992:                        'ua'         => "uncheck all",
                   1993:                        'ac'         => "Action to take for selected users",
1.10      raeburn  1994:                        'link'       => "Behavior of username links",
                   1995:                        'aboutme'    => "Display a user's personal page",
                   1996:                        'modify'     => "Modify a user's information",
1.1       raeburn  1997:                       );
1.2       raeburn  1998:     if ($context eq 'domain' && $env{'form.roletype'} eq 'course') {
                   1999:         $lt{'extent'} = &mt('Course(s): description, section(s), status');
1.13      raeburn  2000:     } elsif ($context eq 'author') {
1.2       raeburn  2001:         $lt{'extent'} = &mt('Author'); 
                   2002:     }
1.1       raeburn  2003:     my @cols = ('username','domain','id','fullname');
                   2004:     if ($context eq 'course') {
                   2005:         push(@cols,'section');
                   2006:     }
1.2       raeburn  2007:     if (!($context eq 'domain' && $env{'form.roletype'} eq 'course')) { 
                   2008:         push(@cols,('start','end'));
                   2009:     }
1.3       raeburn  2010:     if ($env{'form.showrole'} eq 'Any' || $env{'form.showrole'} eq 'cr') {
1.2       raeburn  2011:         push(@cols,'role');
                   2012:     }
1.13      raeburn  2013:     if ($context eq 'domain' && ($env{'form.roletype'} eq 'author' ||
1.2       raeburn  2014:                                 $env{'form.roletype'} eq 'course')) {
                   2015:         push (@cols,'extent');
                   2016:     }
                   2017:     if (($statusmode eq 'Any') && 
                   2018:         (!($context eq 'domain' && $env{'form.roletype'} eq 'course'))) {
1.1       raeburn  2019:         push(@cols,'status');
                   2020:     }
                   2021:     if ($context eq 'course') {
                   2022:         push(@cols,'groups');
                   2023:     }
                   2024:     push(@cols,'email');
                   2025: 
1.4       raeburn  2026:     my $rolefilter = $env{'form.showrole'};
1.5       raeburn  2027:     if ($env{'form.showrole'} eq 'cr') {
                   2028:         $rolefilter = &mt('custom');  
                   2029:     } elsif ($env{'form.showrole'} ne 'Any') {
1.2       raeburn  2030:         $rolefilter = &Apache::lonnet::plaintext($env{'form.showrole'});
                   2031:     }
1.16      raeburn  2032:     my $results_description;
                   2033:     if ($mode ne 'autoenroll') {
                   2034:         $results_description = &results_header_row($rolefilter,$statusmode,
1.24      raeburn  2035:                                                    $context,$permission,$mode);
1.16      raeburn  2036:         $r->print('<b>'.$results_description.'</b><br />');
                   2037:     }
1.26      raeburn  2038:     my ($output,$actionselect,%canchange,%canchangesec);
1.16      raeburn  2039:     if ($mode eq 'html' || $mode eq 'view' || $mode eq 'autoenroll') {
                   2040:         if ($mode ne 'autoenroll') {
                   2041:             if ($permission->{'cusr'}) {
                   2042:                 $actionselect = &select_actions($context,$setting,$statusmode);
                   2043:             }
                   2044:             $r->print(<<END);
1.10      raeburn  2045: <input type="hidden" name="srchby"  value="uname" />
                   2046: <input type="hidden" name="srchin"   value="dom" />
                   2047: <input type="hidden" name="srchtype" value="exact" />
                   2048: <input type="hidden" name="srchterm" value="" />
1.11      raeburn  2049: <input type="hidden" name="srchdomain" value="" /> 
1.1       raeburn  2050: END
1.16      raeburn  2051:             $output = '<p>';
                   2052:             my @linkdests = ('aboutme');
                   2053:             if ($permission->{'cusr'}) {
                   2054:                 push (@linkdests,'modify');
                   2055:                 $output .= '<span class="LC_nobreak">'.$lt{'link'}.':&nbsp;';
                   2056:                 my $usernamelink = $env{'form.usernamelink'};
                   2057:                 if ($usernamelink eq '') {
                   2058:                     $usernamelink = 'aboutme';
                   2059:                 }
                   2060:                 foreach my $item (@linkdests) {
                   2061:                     my $checkedstr = '';
                   2062:                     if ($item eq $usernamelink) {
                   2063:                         $checkedstr = ' checked="checked" ';
                   2064:                     }
                   2065:                     $output .= '<label><input type="radio" name="usernamelink" value="'.$item.'"'.$checkedstr.'>&nbsp;'.$lt{$item}.'</label>&nbsp;&nbsp;';
                   2066:                 }
                   2067:                 $output .= '</span><br />';
                   2068:             } else {
                   2069:                 $output .= &mt("Click on a username to view the user's personal page.").'<br />';
                   2070:             }
                   2071:             if ($actionselect) {
                   2072:                 $output .= <<"END"; 
1.11      raeburn  2073: $lt{'ac'}:&nbsp;$actionselect <input type="button" value="$lt{'pr'}" onclick="javascript:verify_action(document.studentform.actionlist)" /></p>
                   2074: <p><input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.actionlist)" /> &nbsp;
                   2075: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.actionlist)" />
                   2076: END
1.26      raeburn  2077:                 my @allroles;
                   2078:                 if ($env{'form.showrole'} eq 'Any') {
                   2079:                     my $custom = 1;
                   2080:                     if ($context eq 'domain') {
                   2081:                         @allroles = &roles_by_context($setting,$custom);
                   2082:                     } else {
                   2083:                         @allroles = &roles_by_context($context,$custom);
                   2084:                     }
                   2085:                 } else {
                   2086:                     @allroles = ($env{'form.showrole'});
                   2087:                 }
                   2088:                 foreach my $role (@allroles) {
                   2089:                     if ($context eq 'domain') {
                   2090:                         if ($setting eq 'domain') {
                   2091:                             if (&Apache::lonnet::allowed('c'.$role,
                   2092:                                     $env{'request.role.domain'})) {
                   2093:                                 $canchange{$role} = 1;
                   2094:                             }
1.31      raeburn  2095:                         } elsif ($setting eq 'author') {
                   2096:                             if (&Apache::lonnet::allowed('c'.$role,
                   2097:                                     $env{'request.role.domain'})) {
                   2098:                                 $canchange{$role} = 1;
                   2099:                             }
1.26      raeburn  2100:                         }
                   2101:                     } elsif ($context eq 'author') {
                   2102:                         if (&Apache::lonnet::allowed('c'.$role,
                   2103:                             $env{'user.domain'}.'/'.$env{'user.name'})) {
                   2104:                             $canchange{$role} = 1;
                   2105:                         }
                   2106:                     } elsif ($context eq 'course') {
                   2107:                         if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   2108:                             $canchange{$role} = 1;
                   2109:                         } elsif ($env{'request.course.sec'} ne '') {
                   2110:                             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   2111:                                 $canchangesec{$role} = $env{'request.course.sec'};
                   2112:                             }
                   2113:                         }
                   2114:                     }
                   2115:                 }
1.16      raeburn  2116:             }
1.4       raeburn  2117:         }
                   2118:         $output .= "\n<p>\n".
1.1       raeburn  2119:                   &Apache::loncommon::start_data_table().
1.4       raeburn  2120:                   &Apache::loncommon::start_data_table_header_row();
1.1       raeburn  2121:         if ($mode eq 'autoenroll') {
1.4       raeburn  2122:             $output .= "
1.1       raeburn  2123:  <th><a href=\"javascript:document.studentform.sortby.value='type';document.studentform.submit();\">$lt{'type'}</a></th>
1.4       raeburn  2124:             ";
1.1       raeburn  2125:         } else {
1.11      raeburn  2126:             $output .= "\n".'<th>'.&mt('Count').'</th>'."\n";
                   2127:             if ($actionselect) {
                   2128:                 $output .= '<th>'.&mt('Select').'</th>'."\n";
                   2129:             }
1.1       raeburn  2130:         }
                   2131:         foreach my $item (@cols) {
1.4       raeburn  2132:             $output .= "<th><a href=\"javascript:document.studentform.sortby.value='$item';document.studentform.submit();\">$lt{$item}</a></th>\n";
1.1       raeburn  2133:         }
1.2       raeburn  2134:         my %role_types = &role_type_names();
1.16      raeburn  2135:         if ($context eq 'course' && $mode ne 'autoenroll') {
1.4       raeburn  2136:             if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   2137:                 # Clicker display on or off?
                   2138:                 my %clicker_options = &Apache::lonlocal::texthash(
                   2139:                                                             'on' => 'Show',
                   2140:                                                             'off' => 'Hide',
                   2141:                                                            );
                   2142:                 my $clickerchg = 'on';
                   2143:                 if ($displayclickers eq 'on') {
                   2144:                     $clickerchg = 'off';
                   2145:                 }
                   2146:                 $output .= '    <th>'."\n".'     '.
                   2147:                     '<a href="javascript:document.studentform.displayclickers.value='.
1.1       raeburn  2148:                       "'".$clickerchg."'".';document.studentform.submit();">'.
                   2149:                       $clicker_options{$clickerchg}.'</a>&nbsp;'.$lt{'clicker'}."\n".
1.4       raeburn  2150:                       '    </th>'."\n";
1.1       raeburn  2151: 
1.4       raeburn  2152:                 # Photo display on or off?
                   2153:                 if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2154:                     my %photo_options = &Apache::lonlocal::texthash(
                   2155:                                                             'on' => 'Show',
                   2156:                                                             'off' => 'Hide',
                   2157:                                                                 );
                   2158:                     my $photochg = 'on';
                   2159:                     if ($displayphotos eq 'on') {
                   2160:                         $photochg = 'off';
                   2161:                     }
                   2162:                     $output .= '    <th>'."\n".'     '.
                   2163:                 '<a href="javascript:document.studentform.displayphotos.value='.
1.1       raeburn  2164:                       "'".$photochg."'".';document.studentform.submit();">'.
                   2165:                       $photo_options{$photochg}.'</a>&nbsp;'.$lt{'photo'}."\n".
1.4       raeburn  2166:                       '    </th>'."\n";
                   2167:                 }
1.1       raeburn  2168:             }
1.4       raeburn  2169:         }
1.16      raeburn  2170:         $output .= &Apache::loncommon::end_data_table_header_row();
1.1       raeburn  2171: # Done with the HTML header line
                   2172:     } elsif ($mode eq 'csv') {
                   2173:         #
                   2174:         # Open a file
                   2175:         $CSVfilename = '/prtspool/'.
1.2       raeburn  2176:                        $env{'user.name'}.'_'.$env{'user.domain'}.'_'.
                   2177:                        time.'_'.rand(1000000000).'.csv';
1.1       raeburn  2178:         unless ($CSVfile = Apache::File->new('>/home/httpd'.$CSVfilename)) {
                   2179:             $r->log_error("Couldn't open $CSVfilename for output $!");
                   2180:             $r->print("Problems occured in writing the csv file.  ".
                   2181:                       "This error has been logged.  ".
                   2182:                       "Please alert your LON-CAPA administrator.");
                   2183:             $CSVfile = undef;
                   2184:         }
                   2185:         #
                   2186:         # Write headers and data to file
1.2       raeburn  2187:         print $CSVfile '"'.$results_description.'"'."\n"; 
1.1       raeburn  2188:         print $CSVfile '"'.join('","',map {
                   2189:             &Apache::loncommon::csv_translate($lt{$_})
                   2190:             } (@cols)).'"'."\n";
                   2191:     } elsif ($mode eq 'excel') {
                   2192:         # Create the excel spreadsheet
                   2193:         ($excel_workbook,$excel_filename,$format) =
                   2194:             &Apache::loncommon::create_workbook($r);
                   2195:         return if (! defined($excel_workbook));
                   2196:         $excel_sheet = $excel_workbook->addworksheet('userlist');
1.2       raeburn  2197:         $excel_sheet->write($row++,0,$results_description,$format->{'h2'});
1.1       raeburn  2198:         #
                   2199:         my @colnames = map {$lt{$_}} (@cols);
                   2200:         $excel_sheet->write($row++,0,\@colnames,$format->{'bold'});
                   2201:     }
                   2202: 
                   2203: # Done with header lines in all formats
                   2204: 
                   2205:     my %index;
                   2206:     my $i;
1.2       raeburn  2207:     foreach my $idx (@$keylist) {
                   2208:         $index{$idx} = $i++;
                   2209:     }
1.4       raeburn  2210:     my $usercount = 0;
1.33      raeburn  2211:     my ($secfilter,$grpfilter);
                   2212:     if ($context eq 'course') {
                   2213:         $secfilter = $env{'form.secfilter'};
                   2214:         $grpfilter = $env{'form.grpfilter'};
                   2215:         if ($secfilter eq '') {
                   2216:             $secfilter = 'all';
                   2217:         }
                   2218:         if ($grpfilter eq '') {
                   2219:             $grpfilter = 'all';
                   2220:         }
                   2221:     }
1.2       raeburn  2222:     # Get groups, role, permanent e-mail so we can sort on them if
                   2223:     # necessary.
                   2224:     foreach my $user (keys(%{$userlist})) {
1.11      raeburn  2225:         if ($context eq 'domain' &&  $user eq $env{'request.role.domain'}.'-domainconfig:'.$env{'request.role.domain'}) {
                   2226:             delete($userlist->{$user});
                   2227:             next;
                   2228:         }
1.2       raeburn  2229:         my ($uname,$udom,$role,$groups,$email);
1.5       raeburn  2230:         if (($statusmode ne 'Any') && 
                   2231:                  ($userlist->{$user}->[$index{'status'}] ne $statusmode)) {
                   2232:             delete($userlist->{$user});
                   2233:             next;
                   2234:         }
1.2       raeburn  2235:         if ($context eq 'domain') {
                   2236:             if ($env{'form.roletype'} eq 'domain') {
                   2237:                 ($role,$uname,$udom) = split(/:/,$user);
1.11      raeburn  2238:                 if (($uname eq $env{'request.role.domain'}.'-domainconfig') &&
                   2239:                     ($udom eq $env{'request.role.domain'})) {
                   2240:                     delete($userlist->{$user});
                   2241:                     next;
                   2242:                 }
1.13      raeburn  2243:             } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2244:                 ($uname,$udom,$role) = split(/:/,$user,-1);
                   2245:             } elsif ($env{'form.roletype'} eq 'course') {
                   2246:                 ($uname,$udom,$role) = split(/:/,$user);
                   2247:             }
                   2248:         } else {
                   2249:             ($uname,$udom,$role) = split(/:/,$user,-1);
                   2250:             if (($context eq 'course') && $role eq '') {
                   2251:                 $role = 'st';
                   2252:             }
                   2253:         }
                   2254:         $userlist->{$user}->[$index{'role'}] = $role;
                   2255:         if (($env{'form.showrole'} ne 'Any') && (!($env{'form.showrole'}  eq 'cr' && $role =~ /^cr\//)) && ($role ne $env{'form.showrole'})) {
                   2256:             delete($userlist->{$user});
                   2257:             next;
                   2258:         }
1.33      raeburn  2259:         if ($context eq 'course') {
                   2260:             my @ac_groups;
                   2261:             if (ref($classgroups) eq 'HASH') {
                   2262:                 $groups = $classgroups->{$user};
                   2263:             }
                   2264:             if (ref($groups->{'active'}) eq 'HASH') {
                   2265:                 @ac_groups = keys(%{$groups->{'active'}});
                   2266:                 $userlist->{$user}->[$index{'groups'}] = join(', ',@ac_groups);
                   2267:             }
                   2268:             if ($mode ne 'autoenroll') {
                   2269:                 my $section = $userlist->{$user}->[$index{'section'}];
                   2270:                 if ($secfilter eq 'none') {
                   2271:                     if ($section ne '') {
                   2272:                         delete($userlist->{$user});
                   2273:                         next;
                   2274:                     }
                   2275:                 } elsif ($secfilter ne 'all') {
                   2276:                     if ($section ne $secfilter) {
                   2277:                         delete($userlist->{$user});
                   2278:                         next;
                   2279:                     }
                   2280:                 }
                   2281:                 if ($grpfilter eq 'none') {
                   2282:                     if (@ac_groups > 0) {
                   2283:                         delete($userlist->{$user});
                   2284:                         next;
                   2285:                     }
                   2286:                 } elsif ($grpfilter ne 'all') {
                   2287:                     if (!grep(/^\Q$grpfilter\E$/,@ac_groups)) {
                   2288:                         delete($userlist->{$user});
                   2289:                         next;
                   2290:                     }
                   2291:                 }
                   2292:             }
1.2       raeburn  2293:         }
                   2294:         my %emails   = &Apache::loncommon::getemails($uname,$udom);
                   2295:         if ($emails{'permanentemail'} =~ /\S/) {
                   2296:             $userlist->{$user}->[$index{'email'}] = $emails{'permanentemail'};
                   2297:         }
1.4       raeburn  2298:         $usercount ++;
                   2299:     }
                   2300:     my $autocount = 0;
                   2301:     my $manualcount = 0;
                   2302:     my $lockcount = 0;
                   2303:     my $unlockcount = 0;
                   2304:     if ($usercount) {
                   2305:         $r->print($output);
                   2306:     } else {
                   2307:         if ($mode eq 'autoenroll') {
                   2308:             return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
                   2309:         } else {
                   2310:             return;
                   2311:         }
1.1       raeburn  2312:     }
1.2       raeburn  2313:     #
                   2314:     # Sort the users
1.1       raeburn  2315:     my $index  = $index{$sortby};
                   2316:     my $second = $index{'username'};
                   2317:     my $third  = $index{'domain'};
1.2       raeburn  2318:     my @sorted_users = sort {
                   2319:         lc($userlist->{$a}->[$index])  cmp lc($userlist->{$b}->[$index])
1.1       raeburn  2320:             ||
1.2       raeburn  2321:         lc($userlist->{$a}->[$second]) cmp lc($userlist->{$b}->[$second])            ||
                   2322:         lc($userlist->{$a}->[$third]) cmp lc($userlist->{$b}->[$third])
                   2323:         } (keys(%$userlist));
1.4       raeburn  2324:     my $rowcount = 0;
1.2       raeburn  2325:     foreach my $user (@sorted_users) {
1.4       raeburn  2326:         my %in;
1.2       raeburn  2327:         my $sdata = $userlist->{$user};
1.4       raeburn  2328:         $rowcount ++; 
1.2       raeburn  2329:         foreach my $item (@{$keylist}) {
                   2330:             $in{$item} = $sdata->[$index{$item}];
                   2331:         }
1.11      raeburn  2332:         my $role = $in{'role'};
1.2       raeburn  2333:         $in{'role'}=&Apache::lonnet::plaintext($sdata->[$index{'role'}]); 
                   2334:         if (! defined($in{'start'}) || $in{'start'} == 0) {
                   2335:             $in{'start'} = &mt('none');
                   2336:         } else {
                   2337:             $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
1.1       raeburn  2338:         }
1.2       raeburn  2339:         if (! defined($in{'end'}) || $in{'end'} == 0) {
                   2340:             $in{'end'} = &mt('none');
1.1       raeburn  2341:         } else {
1.2       raeburn  2342:             $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
1.1       raeburn  2343:         }
1.2       raeburn  2344:         if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
                   2345:             $r->print(&Apache::loncommon::start_data_table_row());
1.11      raeburn  2346:             my $checkval;
1.16      raeburn  2347:             if ($mode eq 'autoenroll') {
                   2348:                 my $cellentry;
                   2349:                 if ($in{'type'} eq 'auto') {
                   2350:                     $cellentry = '<b>'.&mt('auto').'</b>&nbsp;<label><input type="checkbox" name="chgauto" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label>';
                   2351:                     $autocount ++;
                   2352:                 } else {
                   2353:                     $cellentry = '<table border="0" cellspacing="0"><tr><td rowspan="2"><b>'.&mt('manual').'</b></td><td><nobr><label><input type="checkbox" name="chgmanual" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;Change</label></nobr></td></tr><tr><td><nobr>';
                   2354:                     $manualcount ++;
                   2355:                     if ($in{'lockedtype'}) {
                   2356:                         $cellentry .= '<label><input type="checkbox" name="unlockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Unlock').'</label>';
                   2357:                         $unlockcount ++;
                   2358:                     } else {
                   2359:                         $cellentry .= '<label><input type="checkbox" name="lockchg" value="'.$in{'username'}.':'.$in{'domain'}.'" />&nbsp;'.&mt('Lock').'</label>';
                   2360:                         $lockcount ++;
1.11      raeburn  2361:                     }
1.16      raeburn  2362:                     $cellentry .= '</nobr></td></tr></table>';
                   2363:                 }
                   2364:                 $r->print("<td>$cellentry</td>\n");
                   2365:             } else {
                   2366:                 $r->print("<td>$rowcount</td>\n");
                   2367:                 if ($actionselect) {
1.26      raeburn  2368:                     my $showcheckbox;
                   2369:                     if ($role =~ /^cr\//) {
                   2370:                         $showcheckbox = $canchange{'cr'};
                   2371:                     } else {
                   2372:                         $showcheckbox = $canchange{$role};
                   2373:                     }
                   2374:                     if (!$showcheckbox) {
                   2375:                         if ($context eq 'course') {
                   2376:                             if ($canchangesec{$role} ne '') {
                   2377:                                 if ($canchangesec{$role} eq $in{'section'}) {
                   2378:                                     $showcheckbox = 1;
                   2379:                                 }
                   2380:                             }
1.16      raeburn  2381:                         }
1.26      raeburn  2382:                     }
                   2383:                     if ($showcheckbox) {
                   2384:                         $checkval = $user; 
                   2385:                         if ($context eq 'course') {
                   2386:                             if ($role eq 'st') {
                   2387:                                 $checkval .= ':st';
                   2388:                             }
                   2389:                             $checkval .= ':'.$in{'section'};
                   2390:                             if ($role eq 'st') {
                   2391:                                 $checkval .= ':'.$in{'type'}.':'.
                   2392:                                              $in{'lockedtype'};
                   2393:                             }
1.16      raeburn  2394:                         }
1.26      raeburn  2395:                         $r->print('<td><input type="checkbox" name="'.
                   2396:                                   'actionlist" value="'.$checkval.'"></td>');
                   2397:                     } else {
                   2398:                         $r->print('<td>&nbsp;</td>');
1.16      raeburn  2399:                     }
1.11      raeburn  2400:                 }
                   2401:             }
1.2       raeburn  2402:             foreach my $item (@cols) {
1.10      raeburn  2403:                 if ($item eq 'username') {
1.16      raeburn  2404:                     $r->print('<td>'.&print_username_link($mode,$permission,
                   2405:                                                           \%in).'</td>');
                   2406:                 } elsif (($item eq 'start' || $item eq 'end') && ($actionselect)) {
1.11      raeburn  2407:                     $r->print('<td>'.$in{$item}.'<input type="hidden" name="'.$checkval.'_'.$item.'" value="'.$sdata->[$index{$item}].'" /></td>'."\n");
1.10      raeburn  2408:                 } else {
                   2409:                     $r->print('<td>'.$in{$item}.'</td>'."\n");
                   2410:                 }
1.2       raeburn  2411:             }
1.16      raeburn  2412:             if (($context eq 'course') && ($mode ne 'autoenroll')) {
1.4       raeburn  2413:                 if ($env{'form.showrole'} eq 'st' || $env{'form.showrole'} eq 'Any') {
                   2414:                     if ($displayclickers eq 'on') {
                   2415:                         my $clickers =
1.2       raeburn  2416:                    (&Apache::lonnet::userenvironment($in{'domain'},$in{'username'},'clickers'))[1];
1.4       raeburn  2417:                         if ($clickers!~/\w/) { $clickers='-'; }
                   2418:                         $r->print('<td>'.$clickers.'</td>');
1.2       raeburn  2419:                     } else {
                   2420:                         $r->print('    <td>&nbsp;</td>  ');
                   2421:                     }
1.4       raeburn  2422:                     if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
                   2423:                         if ($displayphotos eq 'on' && $sdata->[$index{'role'}] eq 'st') {
                   2424:                             my $imgurl =
                   2425:                         &Apache::lonnet::retrievestudentphoto($in{'domain'},$in{'username'},
                   2426:                                                           'gif','thumbnail');
                   2427:                             $r->print('    <td align="right"><a href="javascript:photowindow('."'".&Apache::lonnet::studentphoto($in{'domain'},$in{'username'},'jpg')."'".')"><img src="'.$imgurl.'" border="1"></a></td>');
                   2428:                         } else {
                   2429:                             $r->print('    <td>&nbsp;</td>  ');
                   2430:                         }
                   2431:                     }
1.2       raeburn  2432:                 }
                   2433:             }
                   2434:             $r->print(&Apache::loncommon::end_data_table_row());
                   2435:         } elsif ($mode eq 'csv') {
                   2436:             next if (! defined($CSVfile));
                   2437:             # no need to bother with $linkto
                   2438:             if (! defined($in{'start'}) || $in{'start'} == 0) {
                   2439:                 $in{'start'} = &mt('none');
                   2440:             } else {
                   2441:                 $in{'start'} = &Apache::lonlocal::locallocaltime($in{'start'});
                   2442:             }
                   2443:             if (! defined($in{'end'}) || $in{'end'} == 0) {
                   2444:                 $in{'end'} = &mt('none');
                   2445:             } else {
                   2446:                 $in{'end'} = &Apache::lonlocal::locallocaltime($in{'end'});
                   2447:             }
                   2448:             my @line = ();
                   2449:             foreach my $item (@cols) {
                   2450:                 push @line,&Apache::loncommon::csv_translate($in{$item});
                   2451:             }
                   2452:             print $CSVfile '"'.join('","',@line).'"'."\n";
                   2453:         } elsif ($mode eq 'excel') {
                   2454:             my $col = 0;
                   2455:             foreach my $item (@cols) {
                   2456:                 if ($item eq 'start' || $item eq 'end') {
                   2457:                     if (defined($item) && $item != 0) {
                   2458:                         $excel_sheet->write($row,$col++,
                   2459:                             &Apache::lonstathelpers::calc_serial($in{item}),
                   2460:                                     $format->{'date'});
                   2461:                     } else {
                   2462:                         $excel_sheet->write($row,$col++,'none');
                   2463:                     }
                   2464:                 } else {
                   2465:                     $excel_sheet->write($row,$col++,$in{$item});
                   2466:                 }
                   2467:             }
                   2468:             $row++;
1.1       raeburn  2469:         }
                   2470:     }
1.2       raeburn  2471:     if ($mode eq 'view' || $mode eq 'html' || $mode eq 'autoenroll') {
                   2472:             $r->print(&Apache::loncommon::end_data_table().'<br />');
                   2473:     } elsif ($mode eq 'excel') {
                   2474:         $excel_workbook->close();
                   2475:         $r->print('<p><a href="'.$excel_filename.'">'.
                   2476:                   &mt('Your Excel spreadsheet').'</a> '.&mt('is ready for download').'.</p>'."\n");
                   2477:     } elsif ($mode eq 'csv') {
                   2478:         close($CSVfile);
                   2479:         $r->print('<a href="'.$CSVfilename.'">'.
                   2480:                   &mt('Your CSV file').'</a> is ready for download.'.
                   2481:                   "\n");
                   2482:         $r->rflush();
                   2483:     }
                   2484:     if ($mode eq 'autoenroll') {
                   2485:         return ($usercount,$autocount,$manualcount,$lockcount,$unlockcount);
1.4       raeburn  2486:     } else {
                   2487:         return ($usercount);
1.2       raeburn  2488:     }
1.1       raeburn  2489: }
                   2490: 
1.10      raeburn  2491: sub print_username_link {
1.16      raeburn  2492:     my ($mode,$permission,$in) = @_;
1.10      raeburn  2493:     my $output;
1.16      raeburn  2494:     if ($mode eq 'autoenroll') {
                   2495:         $output = $in->{'username'};
                   2496:     } elsif (!$permission->{'cusr'}) {
1.10      raeburn  2497:         $output = &Apache::loncommon::aboutmewrapper($in->{'username'},
                   2498:                                                      $in->{'username'},
                   2499:                                                      $in->{'domain'});
                   2500:     } else {
                   2501:         $output = '<a href="javascript:username_display_launch('.
                   2502:                   "'$in->{'username'}','$in->{'domain'}'".')" />'.
                   2503:                   $in->{'username'}.'</a>';
                   2504:     }
                   2505:     return $output;
                   2506: }
                   2507: 
1.2       raeburn  2508: sub role_type_names {
                   2509:     my %lt = &Apache::lonlocal::texthash (
1.13      raeburn  2510:                          'domain' => 'Domain Roles',
                   2511:                          'author' => 'Co-Author Roles',
                   2512:                          'course' => 'Course Roles',
1.2       raeburn  2513:              );
                   2514:     return %lt;
                   2515: }
                   2516: 
1.11      raeburn  2517: sub select_actions {
                   2518:     my ($context,$setting,$statusmode) = @_;
                   2519:     my %lt = &Apache::lonlocal::texthash(
                   2520:                 revoke   => "Revoke user roles",
                   2521:                 delete   => "Delete user roles",
                   2522:                 reenable => "Re-enable expired user roles",
                   2523:                 activate => "Make future user roles active now",
                   2524:                 chgdates  => "Change starting/ending dates",
                   2525:                 chgsec   => "Change section associated with user roles",
                   2526:     );
                   2527:     my ($output,$options,%choices);
1.23      raeburn  2528:     # FIXME Disable actions for now for roletype=course in domain context
                   2529:     if ($context eq 'domain' && $setting eq 'course') {
                   2530:         return;
                   2531:     }
1.26      raeburn  2532:     if ($context eq 'course') {
                   2533:         if ($env{'form.showrole'} ne 'Any') {
                   2534:              if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},
                   2535:                                            $env{'request.course.id'})) {
                   2536:                  if ($env{'request.course.sec'} eq '') {
                   2537:                      return;
                   2538:                  } else {
                   2539:                      if (!&Apache::lonnet::allowed('c'.$env{'form.showrole'},$env{'request.course.id'}.'/'.$env{'request.course.sec'})) {
                   2540:                          return;
                   2541:                      }
                   2542:                  }
                   2543:             }
                   2544:         }
                   2545:     }
1.11      raeburn  2546:     if ($statusmode eq 'Any') {
                   2547:         $options .= '
                   2548: <option value="chgdates">'.$lt{'chgdates'}.'</option>';
                   2549:         $choices{'dates'} = 1;
                   2550:     } else {
                   2551:         if ($statusmode eq 'Future') {
                   2552:             $options .= '
                   2553: <option value="activate">'.$lt{'activate'}.'</option>';
                   2554:             $choices{'dates'} = 1;
                   2555:         } elsif ($statusmode eq 'Expired') {
                   2556:             $options .= '
                   2557: <option value="reenable">'.$lt{'reenable'}.'</option>';
                   2558:             $choices{'dates'} = 1;
                   2559:         }
1.13      raeburn  2560:         if ($statusmode eq 'Active' || $statusmode eq 'Future') {
                   2561:             $options .= '
                   2562: <option value="chgdates">'.$lt{'chgdates'}.'</option>
                   2563: <option value="revoke">'.$lt{'revoke'}.'</option>';
                   2564:             $choices{'dates'} = 1;
                   2565:         }
1.11      raeburn  2566:     }
                   2567:     if ($context eq 'domain') {
                   2568:         $options .= '
                   2569: <option value="delete">'.$lt{'delete'}.'</option>';
                   2570:     }
                   2571:     if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course')) {
1.26      raeburn  2572:         if (($statusmode ne 'Expired') && ($env{'request.course.sec'} eq '')) {
1.11      raeburn  2573:             $options .= '
                   2574: <option value="chgsec">'.$lt{'chgsec'}.'</option>';
                   2575:             $choices{'sections'} = 1;
                   2576:         }
                   2577:     }
                   2578:     if ($options) {
                   2579:         $output = '<select name="bulkaction" onchange="javascript:opendatebrowser(this.form,'."'studentform'".')" />'."\n".
                   2580:                   '<option value="" selected="selected">'.
                   2581:                   &mt('Please select').'</option>'."\n".$options."\n".'</select>';
                   2582:         if ($choices{'dates'}) {
                   2583:             $output .= 
                   2584:                 '<input type="hidden" name="startdate_month" value="" />'."\n".
                   2585:                 '<input type="hidden" name="startdate_day" value="" />'."\n".
                   2586:                 '<input type="hidden" name="startdate_year" value="" />'."\n".
                   2587:                 '<input type="hidden" name="startdate_hour" value="" />'."\n".
                   2588:                 '<input type="hidden" name="startdate_minute" value="" />'."\n".
                   2589:                 '<input type="hidden" name="startdate_second" value="" />'."\n".
                   2590:                 '<input type="hidden" name="enddate_month" value="" />'."\n".
                   2591:                 '<input type="hidden" name="enddate_day" value="" />'."\n".
                   2592:                 '<input type="hidden" name="enddate_year" value="" />'."\n".
                   2593:                 '<input type="hidden" name="enddate_hour" value="" />'."\n".
                   2594:                 '<input type="hidden" name="enddate_minute" value="" />'."\n".
                   2595:                 '<input type="hidden" name="enddate_second" value="" />'."\n";
                   2596:             if ($context eq 'course') {
                   2597:                 $output .= '<input type="hidden" name="makedatesdefault" value="" />'."\n";
                   2598:             }
                   2599:         }
                   2600:         if ($choices{'sections'}) {
                   2601:             $output .= '<input type="hidden" name="retainsec" value= "" />'."\n".
                   2602:                        '<input type="hidden" name="newsecs" value= "" />'."\n";
                   2603:         }
                   2604:     }
                   2605:     return $output;
                   2606: }
                   2607: 
                   2608: sub date_section_javascript {
                   2609:     my ($context,$setting) = @_;
                   2610:     my $title;
                   2611:     if (($context eq 'course') || ($context eq 'domain' && $setting eq 'course'))  {
                   2612:         $title = &mt('Date and Section selector');
                   2613:     } else {
                   2614:         $title = &mt('Date selector');
                   2615:     }
                   2616:     my $output = '
                   2617: <script type="text/javascript">
                   2618:     var stdeditbrowser;'."\n";
                   2619:     $output .= <<"ENDONE";
                   2620:     function opendatebrowser(callingform,formname) {
                   2621:         var bulkaction = callingform.bulkaction.options[callingform.bulkaction.selectedIndex].value;
                   2622:         if (bulkaction == 'revoke' || bulkaction == 'delete' || bulkaction == '') {
                   2623:             return;
                   2624:         }
                   2625:         var url = '/adm/createuser?';
                   2626:         var type = '';
                   2627:         var showrole = callingform.showrole.options[callingform.showrole.selectedIndex].value;
                   2628: ENDONE
                   2629:     if ($context eq 'domain') {
                   2630:         $output .= '
                   2631:         type = callingform.roletype.options[callingform.roletype.selectedIndex].value;
                   2632: ';
                   2633:     }
                   2634:     my $width= '700';
                   2635:     my $height = '400';
                   2636:     $output .= <<"ENDTWO";
                   2637:         url += 'action=dateselect&callingform=' + formname + 
                   2638:                '&roletype='+type+'&showrole='+showrole +'&bulkaction='+bulkaction;
                   2639:         var title = '$title';
                   2640:         var options = 'scrollbars=1,resizable=1,menubar=0';
                   2641:         options += ',width=$width,height=$height';
                   2642:         stdeditbrowser = open(url,title,options,'1');
                   2643:         stdeditbrowser.focus();
                   2644:     }
                   2645: </script>
                   2646: ENDTWO
                   2647:     return $output;
                   2648: }
                   2649: 
                   2650: sub date_section_selector {
1.21      raeburn  2651:     my ($context,$permission) = @_;
1.11      raeburn  2652:     my $callingform = $env{'form.callingform'};
                   2653:     my $formname = 'dateselect';  
                   2654:     my $groupslist = &get_groupslist();
                   2655:     my $sec_js = &setsections_javascript($formname,$groupslist);
                   2656:     my $output = <<"END";
                   2657: <script type="text/javascript">
                   2658: 
                   2659: $sec_js
                   2660: 
                   2661: function saveselections(formname) {
                   2662: 
                   2663: END
                   2664:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   2665:         $output .= <<"END";
                   2666:         opener.document.$callingform.retainsec.value = formname.retainsec.value;
                   2667:         setSections(formname);
                   2668:         if (seccheck == 'ok') {
                   2669:             opener.document.$callingform.newsecs.value = formname.sections.value;
                   2670:             window.close();
                   2671:         }
                   2672:         return;
                   2673: END
                   2674:     } else {
                   2675:         if ($context eq 'course') {
                   2676:             if (($env{'form.bulkaction'} eq 'reenable') || 
                   2677:                 ($env{'form.bulkaction'} eq 'activate') || 
                   2678:                 ($env{'form.bulkaction'} eq 'chgdates')) {
1.26      raeburn  2679:                 if ($env{'request.course.sec'} eq '') {
                   2680:                     $output .= <<"END";
1.11      raeburn  2681:  
                   2682:         if (formname.makedatesdefault.checked == true) {
                   2683:             opener.document.$callingform.makedatesdefault.value = 1;
                   2684:         }
                   2685:         else {
                   2686:             opener.document.$callingform.makedatesdefault.value = 0;
                   2687:         }
                   2688: 
                   2689: END
1.26      raeburn  2690:                 }
1.11      raeburn  2691:             }
                   2692:         }
                   2693:         $output .= <<"END";
                   2694:     opener.document.$callingform.startdate_month.value =  formname.startdate_month.options[formname.startdate_month.selectedIndex].value;
                   2695:     opener.document.$callingform.startdate_day.value =  formname.startdate_day.value;
                   2696:     opener.document.$callingform.startdate_year.value = formname.startdate_year.value;
                   2697:     opener.document.$callingform.startdate_hour.value =  formname.startdate_hour.options[formname.startdate_hour.selectedIndex].value;
                   2698:     opener.document.$callingform.startdate_minute.value =  formname.startdate_minute.value;
                   2699:     opener.document.$callingform.startdate_second.value = formname.startdate_second.value;
                   2700:     opener.document.$callingform.enddate_month.value =  formname.enddate_month.options[formname.enddate_month.selectedIndex].value;
                   2701:     opener.document.$callingform.enddate_day.value =  formname.enddate_day.value;
                   2702:     opener.document.$callingform.enddate_year.value = formname.enddate_year.value;
                   2703:     opener.document.$callingform.enddate_hour.value =  formname.enddate_hour.options[formname.enddate_hour.selectedIndex].value;
                   2704:     opener.document.$callingform.enddate_minute.value =  formname.enddate_minute.value;
                   2705:     opener.document.$callingform.enddate_second.value = formname.enddate_second.value;
                   2706:     window.close();
                   2707: END
                   2708:     }
                   2709:     $output .= '
                   2710: }
                   2711: </script>
                   2712: ';
                   2713:     my %lt = &Apache::lonlocal::texthash (
                   2714:                  chac => 'Access dates to apply for selected users',
                   2715:                  chse => 'Changes in section affiliation to apply to selected users',
                   2716:                  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.',
                   2717:                  forn => 'For a role in a course that is not a student role, a user may have roles in more than one section of a course at a time.',
                   2718:                  reta => "Retain each user's current section affiliations?", 
                   2719:                  dnap => '(Does not apply to student roles).', 
                   2720:             );
                   2721:     my ($date_items,$headertext);
                   2722:     if ($env{'form.bulkaction'} eq 'chgsec') {
                   2723:         $headertext = $lt{'chse'};
                   2724:     } else {
                   2725:         $headertext = $lt{'chac'};
                   2726:         my $starttime;
                   2727:         if (($env{'form.bulkaction'} eq 'activate') || 
                   2728:             ($env{'form.bulkaction'} eq 'reenable')) {
                   2729:             $starttime = time;
                   2730:         }
                   2731:         $date_items = &date_setting_table($starttime,undef,$context,
1.21      raeburn  2732:                                           $env{'form.bulkaction'},$formname,
                   2733:                                           $permission);
1.11      raeburn  2734:     }
                   2735:     $output .= '<h3>'.$headertext.'</h3>'.
                   2736:                '<form name="'.$formname.'" method="post">'."\n".
                   2737:                 $date_items;
                   2738:     if ($context eq 'course' && $env{'form.bulkaction'} eq 'chgsec') {
1.17      raeburn  2739:         my ($cnum,$cdom) = &get_course_identity();
1.11      raeburn  2740:         my $info;
                   2741:         if ($env{'form.showrole'} eq 'st') {
                   2742:             $output .= '<p>'.$lt{'fors'}.'</p>'; 
1.26      raeburn  2743:         } elsif ($env{'form.showrole'} eq 'Any') {
1.11      raeburn  2744:             $output .= '<p>'.$lt{'fors'}.'</p>'.
                   2745:                        '<p>'.$lt{'forn'}.'&nbsp;';
                   2746:             $info = $lt{'reta'};
                   2747:         } else {
                   2748:             $output .= '<p>'.$lt{'forn'}.'&nbsp;';
                   2749:             $info = $lt{'reta'};
                   2750:         }
                   2751:         if ($info) {
                   2752:             $info .= '<span class="LC_nobreak">'.
                   2753:                      '<label><input type="radio" name="retainsec" value="1" '.
                   2754:                      'checked="checked" />'.&mt('Yes').'</label>&nbsp;&nbsp;'.
                   2755:                      '<label><input type="radio" name="retainsec" value="0" />'.
                   2756:                      &mt('No').'</label></span>';
                   2757:             if ($env{'form.showrole'} eq 'Any') {
                   2758:                 $info .= '<br />'.$lt{'dnap'};
                   2759:             }
                   2760:             $info .= '</p>';
                   2761:         } else {
                   2762:             $info = '<input type="hidden" name="retainsec" value="0" />'; 
                   2763:         }
1.21      raeburn  2764:         my $rowtitle = &mt('New section to assign');
                   2765:         my $secbox = &section_picker($cdom,$cnum,$env{'form.showrole'},$rowtitle,$permission,$context);
1.11      raeburn  2766:         $output .= $info.$secbox;
                   2767:     }
                   2768:     $output .= '<p>'.
                   2769: &mt('Use "Save" to update the main window with your selections.').'<br /><br />'.
                   2770: '<input type="button" name="dateselection" value="'.&mt('Save').'" onclick="javascript:saveselections(this.form)" /></p>'."\n".
                   2771: '</form>';
                   2772:     return $output;
                   2773: }
                   2774: 
1.17      raeburn  2775: sub section_picker {
                   2776:     my ($cdom,$cnum,$role,$rowtitle,$permission,$context,$mode) = @_;
                   2777:     my %sections_count = &Apache::loncommon::get_sections($cdom,$cnum);
                   2778:     my $sections_select .= &course_sections(\%sections_count,$role);
                   2779:     my $secbox = '<p>'.&Apache::lonhtmlcommon::start_pick_box()."\n";
                   2780:     if ($mode eq 'upload') {
                   2781:         my ($options,$cb_script,$coursepick) =
                   2782:             &default_role_selector($context,1);
                   2783:         $secbox .= &Apache::lonhtmlcommon::row_title('role','LC_oddrow_value').
                   2784:                    $options. &Apache::lonhtmlcommon::row_closure(1)."\n";
                   2785:     }
                   2786:     $secbox .= &Apache::lonhtmlcommon::row_title($rowtitle,'LC_oddrow_value')."\n";
                   2787:     if ($env{'request.course.sec'} eq '') {
                   2788:         $secbox .= '<table class="LC_createuser"><tr class="LC_section_row">'."\n".
                   2789:                    '<td align="center">'.&mt('Existing sections')."\n".
                   2790:                    '<br />'.$sections_select.'</td><td align="center">'.
                   2791:                    &mt('New section').'<br />'."\n".
                   2792:                    '<input type="text" name="newsec" size="15" />'."\n".
                   2793:                    '<input type="hidden" name="sections" value="" />'."\n".
                   2794:                    '</td></tr></table>'."\n";
                   2795:     } else {
                   2796:        $secbox .= '<input type="hidden" name="sections" value="'.
                   2797:                    $env{'request.course.sec'}.'" />'.
                   2798:                    $env{'request.course.sec'};
                   2799:     }
                   2800:     $secbox .= &Apache::lonhtmlcommon::row_closure(1)."\n".
                   2801:                &Apache::lonhtmlcommon::end_pick_box().'</p>';
                   2802:     return $secbox;
                   2803: }
                   2804: 
1.2       raeburn  2805: sub results_header_row {
1.24      raeburn  2806:     my ($rolefilter,$statusmode,$context,$permission,$mode) = @_;
1.5       raeburn  2807:     my ($description,$showfilter);
                   2808:     if ($rolefilter ne 'Any') {
                   2809:         $showfilter = $rolefilter;
                   2810:     }
1.2       raeburn  2811:     if ($context eq 'course') {
1.24      raeburn  2812:         if ($mode eq 'csv' || $mode eq 'excel') {
                   2813:             $description = &mt('Course - ').$env{'course.'.$env{'request.course.id'}.'.description'}.': ';
                   2814:         }
1.2       raeburn  2815:         if ($statusmode eq 'Expired') {
1.5       raeburn  2816:             $description .= &mt('Users in course with expired [_1] roles',$showfilter);
1.11      raeburn  2817:         } elsif ($statusmode eq 'Future') {
1.5       raeburn  2818:             $description .= &mt('Users in course with future [_1] roles',$showfilter);
1.2       raeburn  2819:         } elsif ($statusmode eq 'Active') {
1.5       raeburn  2820:             $description .= &mt('Users in course with active [_1] roles',$showfilter);
1.2       raeburn  2821:         } else {
                   2822:             if ($rolefilter eq 'Any') {
                   2823:                 $description .= &mt('All users in course');
                   2824:             } else {
                   2825:                 $description .= &mt('All users in course with [_1] roles',$rolefilter);
                   2826:             }
                   2827:         }
1.33      raeburn  2828:         my $constraint;
1.26      raeburn  2829:         my $viewablesec = &viewable_section($permission);
                   2830:         if ($viewablesec ne '') {
1.15      raeburn  2831:             if ($env{'form.showrole'} eq 'st') {
1.33      raeburn  2832:                 $constraint = &mt('only users in section "[_1]"',$viewablesec);
1.26      raeburn  2833:             } elsif ($env{'form.showrole'} ne 'cc') {
1.33      raeburn  2834:                 $constraint = &mt('only users affiliated with no section or section "[_1]"',$viewablesec);
                   2835:             }
                   2836:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   2837:                 if ($env{'form.grpfilter'} eq 'none') {
                   2838:                     $constraint .= &mt(' and not in any group');
                   2839:                 } else {
                   2840:                     $constraint .= &mt(' and members of group: "[_1]"',$env{'form.grpfilter'});
                   2841:                 }
                   2842:             }
                   2843:         } else {
                   2844:             if (($env{'form.secfilter'} ne 'all') && ($env{'form.secfilter'} ne '')) {
                   2845:                 if ($env{'form.secfilter'} eq 'none') {
                   2846:                     $constraint = &mt('only users affiliated with no section');
                   2847:                 } else {
                   2848:                     $constraint = &mt('only users affiliated with section "[_1]"',$env{'form.secfilter'});
                   2849:                 }
                   2850:             }
                   2851:             if (($env{'form.grpfilter'} ne 'all') && ($env{'form.grpfilter'} ne '')) {
                   2852:                 if ($env{'form.grpfilter'} eq 'none') {
                   2853:                     if ($constraint eq '') {
                   2854:                         $constraint = &mt('only users not in any group');
                   2855:                     } else {
                   2856:                         $constraint .= &mt(' and also not in any group'); 
                   2857:                     }
                   2858:                 } else {
                   2859:                     if ($constraint eq '') {
                   2860:                         $constraint = &mt('only members of group: "[_1]"',$env{'form.grpfilter'});
                   2861:                     } else {
                   2862:                         $constraint .= &mt(' and also members of group: "[_1]"'.$env{'form.grpfilter'});
                   2863:                     }
                   2864:                 }
1.15      raeburn  2865:             }
                   2866:         }
1.33      raeburn  2867:         if ($constraint ne '') {
                   2868:             $description .= ' ('.$constraint.')';
                   2869:         } 
1.13      raeburn  2870:     } elsif ($context eq 'author') {
1.14      raeburn  2871:         $description = 
                   2872:             &mt('Author space for <span class="LC_cusr_emph">[_1]</span>',
                   2873:         &Apache::loncommon::plainname($env{'user.name'},$env{'user.domain'})).':&nbsp;&nbsp;';
1.2       raeburn  2874:         if ($statusmode eq 'Expired') {
1.5       raeburn  2875:             $description .= &mt('Co-authors with expired [_1] roles',$showfilter);
1.2       raeburn  2876:         } elsif ($statusmode eq 'Future') {
1.5       raeburn  2877:             $description .= &mt('Co-authors with future [_1] roles',$showfilter);
1.2       raeburn  2878:         } elsif ($statusmode eq 'Active') {
1.5       raeburn  2879:             $description .= &mt('Co-authors with active [_1] roles',$showfilter);
1.2       raeburn  2880:         } else {
                   2881:             if ($rolefilter eq 'Any') {
1.5       raeburn  2882:                 $description .= &mt('All co-authors');
1.2       raeburn  2883:             } else {
                   2884:                 $description .= &mt('All co-authors with [_1] roles',$rolefilter);
                   2885:             }
                   2886:         }
                   2887:     } elsif ($context eq 'domain') {
                   2888:         my $domdesc = &Apache::lonnet::domain($env{'request.role.domain'},'description');
                   2889:         $description = &mt('Domain - ').$domdesc.': ';
                   2890:         if ($env{'form.roletype'} eq 'domain') {
                   2891:             if ($statusmode eq 'Expired') {
1.5       raeburn  2892:                 $description .= &mt('Users in domain with expired [_1] roles',$showfilter);
1.2       raeburn  2893:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  2894:                 $description .= &mt('Users in domain with future [_1] roles',$showfilter);
1.2       raeburn  2895:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  2896:                 $description .= &mt('Users in domain with active [_1] roles',$showfilter);
1.2       raeburn  2897:             } else {
                   2898:                 if ($rolefilter eq 'Any') {
1.5       raeburn  2899:                     $description .= &mt('All users in domain');
1.2       raeburn  2900:                 } else {
                   2901:                     $description .= &mt('All users in domain with [_1] roles',$rolefilter);
                   2902:                 }
                   2903:             }
1.13      raeburn  2904:         } elsif ($env{'form.roletype'} eq 'author') {
1.2       raeburn  2905:             if ($statusmode eq 'Expired') {
1.5       raeburn  2906:                 $description .= &mt('Co-authors in domain with expired [_1] roles',$showfilter);
1.2       raeburn  2907:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  2908:                 $description .= &mt('Co-authors in domain with future [_1] roles',$showfilter);
1.2       raeburn  2909:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  2910:                $description .= &mt('Co-authors in domain with active [_1] roles',$showfilter);
1.2       raeburn  2911:             } else {
                   2912:                 if ($rolefilter eq 'Any') {
1.5       raeburn  2913:                     $description .= &mt('All users with co-author roles in domain',$showfilter);
1.2       raeburn  2914:                 } else {
                   2915:                     $description .= &mt('All co-authors in domain  with [_1] roles',$rolefilter);
                   2916:                 }
                   2917:             }
                   2918:         } elsif ($env{'form.roletype'} eq 'course') {
                   2919:             my $coursefilter = $env{'form.coursepick'};
                   2920:             if ($coursefilter eq 'category') {
                   2921:                 my $instcode = &instcode_from_coursefilter();
                   2922:                 if ($instcode eq '.') {
                   2923:                     $description .= &mt('All courses in domain').' - ';
                   2924:                 } else {
                   2925:                     $description .= &mt('Courses in domain with institutional code: [_1]',$instcode).' - ';
                   2926:                 }
                   2927:             } elsif ($coursefilter eq 'selected') {
                   2928:                 $description .= &mt('Selected courses in domain').' - ';
                   2929:             } elsif ($coursefilter eq 'all') {
                   2930:                 $description .= &mt('All courses in domain').' - ';
                   2931:             }
                   2932:             if ($statusmode eq 'Expired') {
1.5       raeburn  2933:                 $description .= &mt('users with expired [_1] roles',$showfilter);
1.2       raeburn  2934:             } elsif ($statusmode eq 'Future') {
1.5       raeburn  2935:                 $description .= &mt('users with future [_1] roles',$showfilter);
1.2       raeburn  2936:             } elsif ($statusmode eq 'Active') {
1.5       raeburn  2937:                 $description .= &mt('users with active [_1] roles',$showfilter);
1.2       raeburn  2938:             } else {
                   2939:                 if ($rolefilter eq 'Any') {
                   2940:                     $description .= &mt('all users');
                   2941:                 } else {
                   2942:                     $description .= &mt('users with [_1] roles',$rolefilter);
                   2943:                 }
                   2944:             }
                   2945:         }
                   2946:     }
                   2947:     return $description;
                   2948: }
1.22      raeburn  2949: 
                   2950: sub viewable_section {
                   2951:     my ($permission) = @_;
                   2952:     my $viewablesec;
                   2953:     if (ref($permission) eq 'HASH') {
                   2954:         if (exists($permission->{'view_section'})) {
                   2955:             $viewablesec = $permission->{'view_section'};
                   2956:         } elsif (exists($permission->{'cusr_section'})) {
                   2957:             $viewablesec = $permission->{'cusr_section'};
                   2958:         }
                   2959:     }
                   2960:     return $viewablesec;
                   2961: }
                   2962: 
1.2       raeburn  2963:     
1.1       raeburn  2964: #################################################
                   2965: #################################################
                   2966: sub show_drop_list {
1.30      raeburn  2967:     my ($r,$classlist,$nosort,$permission) = @_;
1.29      raeburn  2968:     my $cid = $env{'request.course.id'};
1.17      raeburn  2969:     my ($cnum,$cdom) = &get_course_identity($cid);
1.1       raeburn  2970:     if (! exists($env{'form.sortby'})) {
                   2971:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   2972:                                                 ['sortby']);
                   2973:     }
                   2974:     my $sortby = $env{'form.sortby'};
                   2975:     if ($sortby !~ /^(username|domain|section|groups|fullname|id|start|end)$/) {
                   2976:         $sortby = 'username';
                   2977:     }
                   2978:     my $action = "drop";
1.17      raeburn  2979:     my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1.1       raeburn  2980:     $r->print(<<END);
                   2981: <input type="hidden" name="sortby" value="$sortby" />
                   2982: <input type="hidden" name="action" value="$action" />
                   2983: <input type="hidden" name="state"  value="done" />
1.17      raeburn  2984: <script type="text/javascript" language="Javascript">
                   2985: $check_uncheck_js
1.1       raeburn  2986: </script>
                   2987: <p>
                   2988: <input type="hidden" name="phase" value="four">
                   2989: END
1.30      raeburn  2990:     my ($indexhash,$keylist) = &make_keylist_array();
                   2991:     my $studentcount = 0;
                   2992:     if (ref($classlist) eq 'HASH') {
                   2993:         foreach my $student (keys(%{$classlist})) {
                   2994:             my $sdata = $classlist->{$student}; 
                   2995:             my $status = $sdata->[$indexhash->{'status'}];
                   2996:             my $section = $sdata->[$indexhash->{'section'}];
                   2997:             if ($status ne 'Active') {
                   2998:                 delete($classlist->{$student});
                   2999:                 next;
                   3000:             }
                   3001:             if ($env{'request.course.sec'} ne '') {
                   3002:                 if ($section ne $env{'request.course.sec'}) {
                   3003:                     delete($classlist->{$student});
                   3004:                     next;
                   3005:                 }
                   3006:             }
                   3007:             $studentcount ++;
                   3008:         }
                   3009:     }
                   3010:     if (!$studentcount) {
                   3011:         $r->print(&mt('There are no students to drop.'));
                   3012:         return;
                   3013:     }
                   3014:     my ($classgroups) = &Apache::loncoursedata::get_group_memberships(
                   3015:                                               $classlist,$keylist,$cdom,$cnum);
                   3016:     my %lt=&Apache::lonlocal::texthash('usrn'   => "username",
                   3017:                                        'dom'    => "domain",
                   3018:                                        'sn'     => "student name",
                   3019:                                        'sec'    => "section",
                   3020:                                        'start'  => "start date",
                   3021:                                        'end'    => "end date",
                   3022:                                        'groups' => "active groups",
                   3023:                                       );
1.1       raeburn  3024:     if ($nosort) {
1.17      raeburn  3025:         $r->print(&Apache::loncommon::start_data_table().
                   3026:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3027:         $r->print(<<END);
                   3028:     <th>&nbsp;</th>
                   3029:     <th>$lt{'usrn'}</th>
                   3030:     <th>$lt{'dom'}</th>
                   3031:     <th>ID</th>
                   3032:     <th>$lt{'sn'}</th>
                   3033:     <th>$lt{'sec'}</th>
                   3034:     <th>$lt{'start'}</th>
                   3035:     <th>$lt{'end'}</th>
                   3036:     <th>$lt{'groups'}</th>
                   3037: END
1.17      raeburn  3038:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3039:     } else  {
1.17      raeburn  3040:         $r->print(&Apache::loncommon::start_data_table().
                   3041:                   &Apache::loncommon::start_data_table_header_row());
1.1       raeburn  3042:         $r->print(<<END);
1.17      raeburn  3043:     <th>&nbsp;</th>
1.1       raeburn  3044:     <th>
1.17      raeburn  3045:        <a href="/adm/createuser?action=$action&sortby=username">$lt{'usrn'}</a>
1.1       raeburn  3046:     </th><th>
1.17      raeburn  3047:        <a href="/adm/createuser?action=$action&sortby=domain">$lt{'dom'}</a>
1.1       raeburn  3048:     </th><th>
1.17      raeburn  3049:        <a href="/adm/createuser?action=$action&sortby=id">ID</a>
1.1       raeburn  3050:     </th><th>
1.17      raeburn  3051:        <a href="/adm/createuser?action=$action&sortby=fullname">$lt{'sn'}</a>
1.1       raeburn  3052:     </th><th>
1.17      raeburn  3053:        <a href="/adm/createuser?action=$action&sortby=section">$lt{'sec'}</a>
1.1       raeburn  3054:     </th><th>
1.17      raeburn  3055:        <a href="/adm/createuser?action=$action&sortby=start">$lt{'start'}</a>
1.1       raeburn  3056:     </th><th>
1.17      raeburn  3057:        <a href="/adm/createuser?action=$action&sortby=end">$lt{'end'}</a>
1.1       raeburn  3058:     </th><th>
1.17      raeburn  3059:        <a href="/adm/createuser?action=$action&sortby=groups">$lt{'groups'}</a>
1.1       raeburn  3060:     </th>
                   3061: END
1.17      raeburn  3062:         $r->print(&Apache::loncommon::end_data_table_header_row());
1.1       raeburn  3063:     }
                   3064:     #
                   3065:     # Sort the students
1.30      raeburn  3066:     my $index  = $indexhash->{$sortby};
                   3067:     my $second = $indexhash->{'username'};
                   3068:     my $third  = $indexhash->{'domain'};
1.1       raeburn  3069:     my @Sorted_Students = sort {
                   3070:         lc($classlist->{$a}->[$index])  cmp lc($classlist->{$b}->[$index])
                   3071:             ||
                   3072:         lc($classlist->{$a}->[$second]) cmp lc($classlist->{$b}->[$second])
                   3073:             ||
                   3074:         lc($classlist->{$a}->[$third]) cmp lc($classlist->{$b}->[$third])
1.30      raeburn  3075:         } (keys(%{$classlist}));
1.1       raeburn  3076:     foreach my $student (@Sorted_Students) {
                   3077:         my $error;
                   3078:         my $sdata = $classlist->{$student};
1.30      raeburn  3079:         my $username = $sdata->[$indexhash->{'username'}];
                   3080:         my $domain   = $sdata->[$indexhash->{'domain'}];
                   3081:         my $section  = $sdata->[$indexhash->{'section'}];
                   3082:         my $name     = $sdata->[$indexhash->{'fullname'}];
                   3083:         my $id       = $sdata->[$indexhash->{'id'}];
                   3084:         my $start    = $sdata->[$indexhash->{'start'}];
                   3085:         my $end      = $sdata->[$indexhash->{'end'}];
1.1       raeburn  3086:         my $groups = $classgroups->{$student};
                   3087:         my $active_groups;
                   3088:         if (ref($groups->{active}) eq 'HASH') {
                   3089:             $active_groups = join(', ',keys(%{$groups->{'active'}}));
                   3090:         }
                   3091:         if (! defined($start) || $start == 0) {
                   3092:             $start = &mt('none');
                   3093:         } else {
                   3094:             $start = &Apache::lonlocal::locallocaltime($start);
                   3095:         }
                   3096:         if (! defined($end) || $end == 0) {
                   3097:             $end = &mt('none');
                   3098:         } else {
                   3099:             $end = &Apache::lonlocal::locallocaltime($end);
                   3100:         }
1.17      raeburn  3101:         my $studentkey = $student.':'.$section;
1.30      raeburn  3102:         my $startitem = '<input type="hidden" name="'.$studentkey.'_start" value="'.$sdata->[$indexhash->{'start'}].'" />';
1.1       raeburn  3103:         #
                   3104:         $r->print(&Apache::loncommon::start_data_table_row());
                   3105:         $r->print(<<"END");
1.29      raeburn  3106:     <td><input type="checkbox" name="droplist" value="$studentkey"></td>
1.1       raeburn  3107:     <td>$username</td>
                   3108:     <td>$domain</td>
                   3109:     <td>$id</td>
                   3110:     <td>$name</td>
                   3111:     <td>$section</td>
1.29      raeburn  3112:     <td>$start $startitem</td>
1.1       raeburn  3113:     <td>$end</td>
                   3114:     <td>$active_groups</td>
                   3115: END
                   3116:         $r->print(&Apache::loncommon::end_data_table_row());
                   3117:     }
                   3118:     $r->print(&Apache::loncommon::end_data_table().'<br />');
                   3119:     %lt=&Apache::lonlocal::texthash(
1.29      raeburn  3120:                        'dp'   => "Drop Students",
1.1       raeburn  3121:                        'ca'   => "check all",
                   3122:                        'ua'   => "uncheck all",
                   3123:                                        );
                   3124:     $r->print(<<"END");
                   3125: </p><p>
                   3126: <input type="button" value="$lt{'ca'}" onclick="javascript:checkAll(document.studentform.droplist)"> &nbsp;
                   3127: <input type="button" value="$lt{'ua'}" onclick="javascript:uncheckAll(document.studentform.droplist)">
                   3128: <p><input type=submit value="$lt{'dp'}"></p>
                   3129: END
                   3130:     return;
                   3131: }
                   3132: 
                   3133: #
                   3134: # Print out the initial form to get the file containing a list of users
                   3135: #
                   3136: sub print_first_users_upload_form {
                   3137:     my ($r,$context) = @_;
                   3138:     my $str;
                   3139:     $str  = '<input type="hidden" name="phase" value="two">';
                   3140:     $str .= '<input type="hidden" name="action" value="upload" />';
                   3141:     $str .= '<input type="hidden"   name="state"  value="got_file" />';
1.2       raeburn  3142:     $str .= "<h3>".&mt('Upload a file containing information about users')."</h3>\n";
1.1       raeburn  3143:     $str .= &Apache::loncommon::upfile_select_html();
                   3144:     $str .= "<p>\n";
                   3145:     $str .= '<input type="submit" name="fileupload" value="'.
1.2       raeburn  3146:         &mt('Upload file of users').'">'."\n";
1.1       raeburn  3147:     $str .= '<label><input type="checkbox" name="noFirstLine" /> '.
                   3148:         &mt('Ignore First Line')."</label></p>\n";
                   3149:     $str .= &Apache::loncommon::help_open_topic("Course_Create_Class_List",
                   3150:                          &mt("How do I create a users list from a spreadsheet")).
                   3151:                              "<br />\n";
                   3152:     $str .= &Apache::loncommon::help_open_topic("Course_Convert_To_CSV",
                   3153:                            &mt("How do I create a CSV file from a spreadsheet")).
                   3154:                                "<br />\n";
                   3155:     $str .= &Apache::loncommon::end_page();
                   3156:     $r->print($str);
                   3157:     return;
                   3158: }
                   3159: 
                   3160: # ================================================= Drop/Add from uploaded file
                   3161: sub upfile_drop_add {
1.21      raeburn  3162:     my ($r,$context,$permission) = @_;
1.1       raeburn  3163:     &Apache::loncommon::load_tmp_file($r);
                   3164:     my @userdata=&Apache::loncommon::upfile_record_sep();
                   3165:     if($env{'form.noFirstLine'}){shift(@userdata);}
                   3166:     my @keyfields = split(/\,/,$env{'form.keyfields'});
                   3167:     my %fields=();
                   3168:     for (my $i=0; $i<=$env{'form.nfields'}; $i++) {
                   3169:         if ($env{'form.upfile_associate'} eq 'reverse') {
                   3170:             if ($env{'form.f'.$i} ne 'none') {
                   3171:                 $fields{$keyfields[$i]}=$env{'form.f'.$i};
                   3172:             }
                   3173:         } else {
                   3174:             $fields{$env{'form.f'.$i}}=$keyfields[$i];
                   3175:         }
                   3176:     }
1.29      raeburn  3177:     if ($env{'form.fullup'} ne 'yes') {
                   3178:         $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n".
                   3179:                   '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
                   3180:     }
1.1       raeburn  3181:     #
                   3182:     # Store the field choices away
                   3183:     foreach my $field (qw/username names
                   3184:                        fname mname lname gen id sec ipwd email role/) {
                   3185:         $env{'form.'.$field.'_choice'}=$fields{$field};
                   3186:     }
                   3187:     &Apache::loncommon::store_course_settings('enrollment_upload',
                   3188:                                               { 'username_choice' => 'scalar',
                   3189:                                                 'names_choice' => 'scalar',
                   3190:                                                 'fname_choice' => 'scalar',
                   3191:                                                 'mname_choice' => 'scalar',
                   3192:                                                 'lname_choice' => 'scalar',
                   3193:                                                 'gen_choice' => 'scalar',
                   3194:                                                 'id_choice' => 'scalar',
                   3195:                                                 'sec_choice' => 'scalar',
                   3196:                                                 'ipwd_choice' => 'scalar',
                   3197:                                                 'email_choice' => 'scalar',
                   3198:                                                 'role_choice'  => 'scalar' });
                   3199:     #
                   3200:     my ($startdate,$enddate) = &get_dates_from_form();
                   3201:     if ($env{'form.makedatesdefault'}) {
1.27      raeburn  3202:         $r->print(&make_dates_default($startdate,$enddate,$context));
1.1       raeburn  3203:     }
                   3204:     # Determine domain and desired host (home server)
                   3205:     my $domain=$env{'request.role.domain'};
                   3206:     my $desiredhost = $env{'form.lcserver'};
                   3207:     if (lc($desiredhost) eq 'default') {
                   3208:         $desiredhost = undef;
                   3209:     } else {
                   3210:         my %home_servers = &Apache::lonnet::get_servers($domain,'library');
                   3211:         if (! exists($home_servers{$desiredhost})) {
                   3212:             $r->print('<span class="LC_error">'.&mt('Error').
                   3213:                       &mt('Invalid home server specified').'</span>');
                   3214:             $r->print(&Apache::loncommon::end_page());
                   3215:             return;
                   3216:         }
                   3217:     }
                   3218:     # Determine authentication mechanism
                   3219:     my $changeauth;
                   3220:     if ($context eq 'domain') {
                   3221:         $changeauth = $env{'form.changeauth'};
                   3222:     }
                   3223:     my $amode  = '';
                   3224:     my $genpwd = '';
                   3225:     if ($env{'form.login'} eq 'krb') {
                   3226:         $amode='krb';
                   3227:         $amode.=$env{'form.krbver'};
                   3228:         $genpwd=$env{'form.krbarg'};
                   3229:     } elsif ($env{'form.login'} eq 'int') {
                   3230:         $amode='internal';
                   3231:         if ((defined($env{'form.intarg'})) && ($env{'form.intarg'})) {
                   3232:             $genpwd=$env{'form.intarg'};
                   3233:         }
                   3234:     } elsif ($env{'form.login'} eq 'loc') {
                   3235:         $amode='localauth';
                   3236:         if ((defined($env{'form.locarg'})) && ($env{'form.locarg'})) {
                   3237:             $genpwd=$env{'form.locarg'};
                   3238:         }
                   3239:     }
                   3240:     if ($amode =~ /^krb/) {
                   3241:         if (! defined($genpwd) || $genpwd eq '') {
                   3242:             $r->print('<span class="Error">'.
                   3243:                       &mt('Unable to enroll users').' '.
                   3244:                       &mt('No Kerberos domain was specified.').'</span></p>');
                   3245:             $amode = ''; # This causes the loop below to be skipped
                   3246:         }
                   3247:     }
                   3248:     my ($cid,$defaultsec,$defaultrole,$setting);
                   3249:     if ($context eq 'domain') {
                   3250:         $setting = $env{'form.roleaction'};
                   3251:         if ($setting eq 'domain') {
                   3252:             $defaultrole = $env{'form.defaultrole'};
                   3253:         } elsif ($setting eq 'course') {
                   3254:             $defaultrole = $env{'form.courserole'};
1.27      raeburn  3255:             $defaultsec = $env{'form.sections'};
1.1       raeburn  3256:         }  
1.13      raeburn  3257:     } elsif ($context eq 'author') {
1.1       raeburn  3258:         $defaultrole = $env{'form.defaultrole'};
1.27      raeburn  3259:     } elsif ($context eq 'course') {
                   3260:         $defaultrole = $env{'form.defaultrole'};
                   3261:         $defaultsec = $env{'form.sections'};
1.1       raeburn  3262:     }
1.27      raeburn  3263:     if ($env{'request.course.id'} ne '') {
                   3264:         $cid = $env{'request.course.id'};
                   3265:     } elsif ($setting eq 'course') {
                   3266:         if (&Apache::lonnet::is_course($env{'form.dcdomain'},$env{'form.dccourse'})) {
                   3267:             $cid = $env{'form.dcdomain'}.'_'.$env{'form.dccourse'};
1.1       raeburn  3268:         }
                   3269:     }
1.27      raeburn  3270:     # Check to see if user information can be changed
                   3271:     my @userinfo = ('firstname','middlename','lastname','generation',
                   3272:                     'permanentemail','id');
                   3273:     my %canmodify;
                   3274:     if (&Apache::lonnet::allowed('mau',$domain)) {
                   3275:         foreach my $field (@userinfo) {
                   3276:             $canmodify{$field} = 1;
                   3277:         }
                   3278:     }
                   3279:     my (%userlist,%modifiable_fields,@poss_roles);
                   3280:     my $secidx = &Apache::loncoursedata::CL_SECTION();
                   3281:     my @courseroles = &roles_by_context('course',1);
                   3282:     if (!&Apache::lonnet::allowed('mau',$domain)) {
                   3283:         if ($context eq 'course' || $context eq 'author') {
                   3284:             @poss_roles =  &curr_role_permissions($context);
                   3285:             my @statuses = ('active','future');
                   3286:             my ($indexhash,$keylist) = &make_keylist_array();
                   3287:             my %info;
                   3288:             foreach my $role (@poss_roles) {
                   3289:                 %{$modifiable_fields{$role}} = &can_modify_userinfo($context,$domain,
                   3290:                                                         \@userinfo,[$role]);
                   3291:             }
                   3292:             if ($context eq 'course') {
                   3293:                 my ($cnum,$cdom) = &get_course_identity();
                   3294:                 my $roster = &Apache::loncoursedata::get_classlist();
                   3295:                 %userlist = %{$roster};
                   3296:                 my %advrolehash = &Apache::lonnet::get_my_roles($cnum,$cdom,undef,
                   3297:                                                          \@statuses,\@poss_roles);
                   3298:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   3299:                                 \%advrolehash,$permission);
                   3300:             } elsif ($context eq 'author') {
                   3301:                 my %cstr_roles = &Apache::lonnet::get_my_roles(undef,undef,undef,
                   3302:                                                   \@statuses,\@poss_roles);
                   3303:                 &gather_userinfo($context,'view',\%userlist,$indexhash,\%info,
                   3304:                              \%cstr_roles,$permission);
                   3305: 
                   3306:             }
                   3307:         }
1.1       raeburn  3308:     }
                   3309:     if ( $domain eq &LONCAPA::clean_domain($domain)
                   3310:         && ($amode ne '')) {
                   3311:         #######################################
                   3312:         ##         Add/Modify Users          ##
                   3313:         #######################################
                   3314:         if ($context eq 'course') {
                   3315:             $r->print('<h3>'.&mt('Enrolling Users')."</h3>\n<p>\n");
1.13      raeburn  3316:         } elsif ($context eq 'author') {
1.1       raeburn  3317:             $r->print('<h3>'.&mt('Updating Co-authors')."</h3>\n<p>\n");
                   3318:         } else {
                   3319:             $r->print('<h3>'.&mt('Adding/Modifying Users')."</h3>\n<p>\n");
                   3320:         }
                   3321:         my %counts = (
                   3322:                        user => 0,
                   3323:                        auth => 0,
                   3324:                        role => 0,
                   3325:                      );
                   3326:         my $flushc=0;
                   3327:         my %student=();
1.27      raeburn  3328:         my (%curr_groups,@sections,@cleansec,@secs,$defaultwarn,$groupwarn);
1.1       raeburn  3329:         my %userchg;
1.27      raeburn  3330:         if ($context eq 'course' || $setting eq 'course') {
                   3331:             if ($context eq 'course') {
                   3332:                 # Get information about course groups
                   3333:                 %curr_groups = &Apache::longroup::coursegroups();
                   3334:             } elsif ($setting eq 'course') {
                   3335:                 if ($cid) {
                   3336:                     %curr_groups =
                   3337:                         &Apache::longroup::coursegroups($env{'form.dcdomain'},
                   3338:                                                         $env{'form.dccourse'});
                   3339:                 }
                   3340:             }
                   3341:             # determine section number
                   3342:             if ($defaultsec =~ /,/) {
                   3343:                 push(@sections,split(/,/,$defaultsec));
                   3344:             } else {
                   3345:                 push(@sections,$defaultsec);
                   3346:             }
                   3347:             # remove non alphanumeric values from section
                   3348:             foreach my $item (@sections) {
                   3349:                 $item =~ s/\W//g;
                   3350:                 if ($item eq "none" || $item eq 'all') {
                   3351:                     $defaultwarn = &mt('Default section name [_1] could not be used as it is a reserved word.',$item);
                   3352:                 } elsif ($item ne ''  && exists($curr_groups{$item})) {
                   3353:                     $groupwarn = &mt('Default section name "[_1]" is the name of a course group. Section names and group names must be distinct.',$item);
                   3354:                 } elsif ($item ne '') {
                   3355:                     push(@cleansec,$item);
                   3356:                 }
                   3357:             }
                   3358:             if ($defaultwarn) {
                   3359:                 $r->print($defaultwarn.'<br />');
                   3360:             }
                   3361:             if ($groupwarn) {
                   3362:                 $r->print($groupwarn.'<br />');
                   3363:             }
1.1       raeburn  3364:         }
1.5       raeburn  3365:         my (%curr_rules,%got_rules,%alerts);
1.27      raeburn  3366:         my %customroles = &my_custom_roles();
                   3367:         my ($custom_ok,@permitted_roles) =
                   3368:             &roles_on_upload($context,%customroles);
1.1       raeburn  3369:         # Get new users list
1.27      raeburn  3370:         foreach my $line (@userdata) {
                   3371:             my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  3372:             # Determine user name
                   3373:             unless (($entries{$fields{'username'}} eq '') ||
                   3374:                     (!defined($entries{$fields{'username'}}))) {
                   3375:                 my ($fname, $mname, $lname,$gen) = ('','','','');
                   3376:                 if (defined($fields{'names'})) {
                   3377:                     ($lname,$fname,$mname)=($entries{$fields{'names'}}=~
                   3378:                                             /([^\,]+)\,\s*(\w+)\s*(.*)$/);
                   3379:                 } else {
                   3380:                     if (defined($fields{'fname'})) {
                   3381:                         $fname=$entries{$fields{'fname'}};
                   3382:                     }
                   3383:                     if (defined($fields{'mname'})) {
                   3384:                         $mname=$entries{$fields{'mname'}};
                   3385:                     }
                   3386:                     if (defined($fields{'lname'})) {
                   3387:                         $lname=$entries{$fields{'lname'}};
                   3388:                     }
                   3389:                     if (defined($fields{'gen'})) {
                   3390:                         $gen=$entries{$fields{'gen'}};
                   3391:                     }
                   3392:                 }
                   3393:                 if ($entries{$fields{'username'}}
                   3394:                     ne &LONCAPA::clean_username($entries{$fields{'username'}})) {
                   3395:                     $r->print('<br />'.
                   3396:       &mt('<b>[_1]</b>: Unacceptable username for user [_2] [_3] [_4] [_5]',
                   3397:           $entries{$fields{'username'}},$fname,$mname,$lname,$gen).
                   3398:                               '</b>');
1.27      raeburn  3399:                     next;
1.1       raeburn  3400:                 } else {
1.5       raeburn  3401:                     my $username = $entries{$fields{'username'}};
1.27      raeburn  3402:                     if (defined($fields{'sec'})) {
                   3403:                         if (defined($entries{$fields{'sec'}})) {
                   3404:                             my $item = $entries{$fields{'sec'}};
                   3405:                             $item =~ s/(\s+$|^\s+)//g;
                   3406:                             if ($item eq "none" || $item eq 'all') {
                   3407:                                 $r->print('<br />'.&mt('<b>[_1]</b>: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a reserved word.',$username,$fname,$mname,$lname,$gen,$item));
                   3408:                                 next;
                   3409:                             } elsif (exists($curr_groups{$item})) {
                   3410:                                 $r->print('<br />'.&mt('<b>[_1]</b>: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]" - this is a course group.',$username,$fname,$mname,$lname,$gen,$item).' '.&mt('Section names and group names must be distinct.'));
                   3411:                                 next;
                   3412:                             } else {
                   3413:                                 push(@secs,$item);
                   3414:                             }
                   3415:                         }
                   3416:                     }
                   3417:                     if ($env{'request.course.sec'} ne '') {
                   3418:                         @secs = ($env{'request.course.sec'});
                   3419:                         if (ref($userlist{$username.':'.$domain}) eq 'ARRAY') {
                   3420:                             my $currsec = $userlist{$username.':'.$domain}[$secidx];
                   3421:                             if ($currsec ne $env{'request.course.sec'}) {
                   3422:                                 $r->print('<br />'.&mt('<b>[_1]</b>: Unable to enroll user [_2] [_3] [_4] [_5] in a section named "[_6]".',$username,$fname,$mname,$lname,$gen,$secs[0]).'<br />');
                   3423:                                 if ($currsec eq '') {
                   3424:                                     $r->print(&mt('This user already has an active/future student role in the course, unaffiliated to any section.'));
                   3425: 
                   3426:                                 } else {
                   3427:                                     $r->print(&mt('This user already has an active/future role in section "[_1]" of the course.',$currsec));
                   3428:                                 }
                   3429:                                 $r->print('<br />'.&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.',$secs[0]).'<br />');
                   3430:                                 next;
1.1       raeburn  3431:                             }
                   3432:                         }
1.27      raeburn  3433:                     } elsif ($context eq 'course' || $setting eq 'course') {
                   3434:                         if (@secs == 0) {
                   3435:                             @secs = @cleansec;
1.1       raeburn  3436:                         }
                   3437:                     }
                   3438:                     # determine id number
                   3439:                     my $id='';
                   3440:                     if (defined($fields{'id'})) {
                   3441:                         if (defined($entries{$fields{'id'}})) {
                   3442:                             $id=$entries{$fields{'id'}};
                   3443:                         }
                   3444:                         $id=~tr/A-Z/a-z/;
                   3445:                     }
                   3446:                     # determine email address
                   3447:                     my $email='';
                   3448:                     if (defined($fields{'email'})) {
                   3449:                         if (defined($entries{$fields{'email'}})) {
                   3450:                             $email=$entries{$fields{'email'}};
                   3451:                             unless ($email=~/^[^\@]+\@[^\@]+$/) { $email=''; }                        }
                   3452:                     }
                   3453:                     # determine user password
                   3454:                     my $password = $genpwd;
                   3455:                     if (defined($fields{'ipwd'})) {
                   3456:                         if ($entries{$fields{'ipwd'}}) {
                   3457:                             $password=$entries{$fields{'ipwd'}};
                   3458:                         }
                   3459:                     }
                   3460:                     # determine user role
                   3461:                     my $role = '';
                   3462:                     if (defined($fields{'role'})) {
                   3463:                         if ($entries{$fields{'role'}}) {
1.27      raeburn  3464:                             if (grep(/^\Q$entries{$fields{'role'}}\E$/,@permitted_roles)) {
1.1       raeburn  3465:                                 $role=$entries{$fields{'role'}};
1.27      raeburn  3466:                                 $role =~ s/(\s+$|^\s+)//g;
                   3467:                             }
                   3468:                             if ($custom_ok) {
                   3469:                                 if ($customroles{$role}) {
                   3470:                                     $role = 'cr_'.$env{'user.domain'}.'_'.$env{'user.name'}.'_'.$entries{$fields{'role'}};
                   3471:                                 }
                   3472:                             }
                   3473:                             if ($role eq '') {
                   3474:                                 my $rolestr = join(', ',@permitted_roles);
1.1       raeburn  3475:                                 $r->print('<br />'.
                   3476:       &mt('<b>[_1]</b>: You do not have permission to add the requested role [_2] for the user.',$entries{$fields{'username'}},$entries{$fields{'role'}}).'<br />'.&mt('Allowable role(s) is/are: [_1].',$rolestr)."\n");
                   3477:                                 next;
                   3478:                             }
                   3479:                         }
                   3480:                     }
                   3481:                     if ($role eq '') {
                   3482:                         $role = $defaultrole;
                   3483:                     }
                   3484:                     # Clean up whitespace
                   3485:                     foreach (\$domain,\$username,\$id,\$fname,\$mname,
1.27      raeburn  3486:                              \$lname,\$gen) {
1.1       raeburn  3487:                         $$_ =~ s/(\s+$|^\s+)//g;
                   3488:                     }
1.5       raeburn  3489:                     # check against rules
                   3490:                     my $checkid = 0;
                   3491:                     my $newuser = 0;
                   3492:                     my (%rulematch,%inst_results,%idinst_results);
                   3493:                     my $uhome=&Apache::lonnet::homeserver($username,$domain);
                   3494:                     if ($uhome eq 'no_host') {
                   3495:                         $checkid = 1;
                   3496:                         $newuser = 1;
                   3497:                         my $checkhash;
                   3498:                         my $checks = { 'username' => 1 };
                   3499:                         $checkhash->{$username.':'.$domain} = { 'newuser' => 1, };
                   3500:                         &Apache::loncommon::user_rule_check($checkhash,$checks,
                   3501:                             \%alerts,\%rulematch,\%inst_results,\%curr_rules,
                   3502:                             \%got_rules);
                   3503:                         if (ref($alerts{'username'}) eq 'HASH') {
                   3504:                             if (ref($alerts{'username'}{$domain}) eq 'HASH') {
                   3505:                                 next if ($alerts{'username'}{$domain}{$username});
                   3506:                             }
                   3507:                         }
1.13      raeburn  3508:                     } else {
1.27      raeburn  3509:                         if ($context eq 'course' || $context eq 'author') {
                   3510:                             if ($role eq '') {
                   3511:                                 my @checkroles;
                   3512:                                 foreach my $role (@poss_roles) {
                   3513:                                     my $endkey;
                   3514:                                     if ($role ne 'st') {
                   3515:                                         $endkey = ':'.$role;
                   3516:                                     }
                   3517:                                     if (exists($userlist{$username.':'.$domain.$endkey})) {
                   3518:                                         if (!grep(/^\Q$role\E$/,@checkroles)) {
                   3519:                                             push(@checkroles,$role);
                   3520:                                         }
                   3521:                                     }
                   3522:                                 }
                   3523:                                 if (@checkroles > 0) {
                   3524:                                     %canmodify = &can_modify_userinfo($context,$domain,\@userinfo,\@checkroles);
                   3525:                                 }
                   3526:                             } elsif (ref($modifiable_fields{$role}) eq 'HASH') {
                   3527:                                 %canmodify = %{$modifiable_fields{$role}};
                   3528:                             }
                   3529:                         }
                   3530:                         my @newinfo = (\$fname,\$mname,\$lname,\$gen,\$email,\$id);
                   3531:                         for (my $i=0; $i<@userinfo; $i++) {
                   3532:                             if (${$newinfo[$i]} ne '') {
                   3533:                                 if (!$canmodify{$userinfo[$i]}) {
                   3534:                                     ${$newinfo[$i]} = '';
                   3535:                                 }
                   3536:                             }
                   3537:                         }
1.5       raeburn  3538:                     }
                   3539:                     if ($id ne '') {
                   3540:                         if (!$newuser) {
                   3541:                             my %idhash = &Apache::lonnet::idrget($domain,($username));
                   3542:                             if ($idhash{$username} ne $id) {
                   3543:                                 $checkid = 1;
                   3544:                             }
                   3545:                         }
                   3546:                         if ($checkid) {
                   3547:                             my $checkhash;
                   3548:                             my $checks = { 'id' => 1 };
                   3549:                             $checkhash->{$username.':'.$domain} = { 'newuser' => $newuser,
                   3550:                                                                     'id'  => $id };
                   3551:                             &Apache::loncommon::user_rule_check($checkhash,$checks,
                   3552:                                 \%alerts,\%rulematch,\%idinst_results,\%curr_rules,
                   3553:                                 \%got_rules);
                   3554:                             if (ref($alerts{'id'}) eq 'HASH') {
                   3555:                                 if (ref($alerts{'id'}{$domain}) eq 'HASH') {
                   3556:                                     next if ($alerts{'id'}{$domain}{$id});
                   3557:                                 }
                   3558:                             }
                   3559:                         }
                   3560:                     }
1.1       raeburn  3561:                     if ($password || $env{'form.login'} eq 'loc') {
1.27      raeburn  3562:                         my $multiple = 0;
                   3563:                         my ($userresult,$authresult,$roleresult,$idresult);
                   3564:                         my (%userres,%authres,%roleres,%idres);
1.1       raeburn  3565:                         if ($role eq 'st') {
1.27      raeburn  3566:                             my $sec;
                   3567:                             if ($cid) {
                   3568:                                 if (@secs > 0) {
                   3569:                                     $sec = $secs[0];
                   3570:                                 }
                   3571:                                 &modifystudent($domain,$username,$cid,$sec,
                   3572:                                                $desiredhost);
                   3573:                                 $roleresult =
                   3574:                                     &Apache::lonnet::modifystudent
                   3575:                                         ($domain,$username,$id,$amode,$password,
                   3576:                                          $fname,$mname,$lname,$gen,$sec,$enddate,
                   3577:                                          $startdate,$env{'form.forceid'},
                   3578:                                          $desiredhost,$email,'manual','',$cid);
                   3579:                                 $userresult = $roleresult;
                   3580:                             }
1.1       raeburn  3581:                         } else {
1.27      raeburn  3582:                             if (($context eq 'course') || 
                   3583:                                 (grep(/^\Q$role\E$/,@courseroles))) {
                   3584:                                 if (!$cid) {
                   3585:                                     next;
                   3586:                                 }
                   3587:                             }
                   3588:                             my $singlesec;
                   3589:                             if ((grep(/^\Q$role\E$/,@courseroles)) && ($role ne 'cc')) {
                   3590:                                 if (@secs > 1) {
                   3591:                                     $multiple = 1;
                   3592:                                     foreach my $sec (@secs) {
                   3593:                                         ($userres{$sec},$authres{$sec},$roleres{$sec},$idres{$sec}) =
                   3594:                                             &modifyuserrole($context,$setting,
                   3595:                                                 $changeauth,$cid,$domain,$username,
                   3596:                                                 $id,$amode,$password,$fname,
                   3597:                                                 $mname,$lname,$gen,$sec,
                   3598:                                                 $env{'form.forceid'},$desiredhost,
                   3599:                                                 $email,$role,$enddate,$startdate,$checkid);
                   3600:                                     }
                   3601:                                 } elsif (@secs > 0) {
                   3602:                                     $singlesec = $secs[0];
                   3603:                                 }
                   3604:                             }
                   3605:                             if (!$multiple) {
1.28      raeburn  3606:                                 ($userresult,$authresult,$roleresult,$idresult) = 
1.27      raeburn  3607:                                     &modifyuserrole($context,$setting,
                   3608:                                         $changeauth,$cid,$domain,$username, 
                   3609:                                         $id,$amode,$password,$fname,
                   3610:                                         $mname,$lname,$gen,$singlesec,
                   3611:                                         $env{'form.forceid'},$desiredhost,
                   3612:                                         $email,$role,$enddate,$startdate,$checkid);
                   3613:                             }
                   3614:                         }
                   3615:                         if ($multiple) {
                   3616:                             foreach my $sec (sort(keys(%userres))) {
                   3617:                                $flushc =
                   3618:                                 &user_change_result($r,$userres{$sec},$authres{$sec},
                   3619:                                                     $roleres{$sec},$idres{$sec},\%counts,$flushc,
                   3620:                                                     $username,\%userchg);
                   3621: 
                   3622:                             }
                   3623:                         } else {
                   3624:                             $flushc = 
                   3625:                                 &user_change_result($r,$userresult,$authresult,
1.28      raeburn  3626:                                                     $roleresult,$idresult,\%counts,$flushc,
1.29      raeburn  3627:                                                     $username,\%userchg);
1.1       raeburn  3628:                         }
                   3629:                     } else {
                   3630:                         if ($context eq 'course') {
                   3631:                             $r->print('<br />'. 
                   3632:       &mt('<b>[_1]</b>: Unable to enroll.  No password specified.',$username)
                   3633:                                      );
1.13      raeburn  3634:                         } elsif ($context eq 'author') {
1.1       raeburn  3635:                             $r->print('<br />'.
                   3636:       &mt('<b>[_1]</b>: Unable to add co-author.  No password specified.',$username)
                   3637:                                      );
                   3638:                         } else {
                   3639:                             $r->print('<br />'.
                   3640:       &mt('<b>[_1]</b>: Unable to add user.  No password specified.',$username)
                   3641:                                      );
                   3642:                         }
                   3643:                     }
                   3644:                 }
                   3645:             }
                   3646:         } # end of foreach (@userdata)
                   3647:         # Flush the course logs so reverse user roles immediately updated
1.5       raeburn  3648:         &Apache::lonnet::flushcourselogs();
1.29      raeburn  3649:         $r->print("</p>\n<p>\n".&mt('Processed [quant,_1,user].',$counts{'user'}).
1.1       raeburn  3650:                   "</p>\n");
                   3651:         if ($counts{'role'} > 0) {
                   3652:             $r->print("<p>\n".
1.29      raeburn  3653:                       &mt('Roles added for [quant,_1,user].',$counts{'role'}).' '.&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.')."</p>\n");
                   3654:         } else {
                   3655:             $r->print('<p>'.&mt('No roles added').'</p>');
1.1       raeburn  3656:         }
                   3657:         if ($counts{'auth'} > 0) {
                   3658:             $r->print("<p>\n".
                   3659:                       &mt('Authentication changed for [_1] existing users.',
                   3660:                           $counts{'auth'})."</p>\n");
                   3661:         }
1.13      raeburn  3662:         $r->print(&print_namespacing_alerts($domain,\%alerts,\%curr_rules));
1.1       raeburn  3663:         #####################################
1.29      raeburn  3664:         # Display list of students to drop  #
1.1       raeburn  3665:         #####################################
                   3666:         if ($env{'form.fullup'} eq 'yes') {
1.29      raeburn  3667:             $r->print('<h3>'.&mt('Students to Drop')."</h3>\n");
1.1       raeburn  3668:             #  Get current classlist
1.30      raeburn  3669:             my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  3670:             if (! defined($classlist)) {
1.29      raeburn  3671:                 $r->print('<form name="studentform" method="post" action="/adm/createuser" />'.
                   3672:                           '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'.
                   3673:                           &mt('There are no students with current/future access to the course.').
                   3674:                           '</form>'."\n");
1.1       raeburn  3675:             } else {
                   3676:                 # Remove the students we just added from the list of students.
1.30      raeburn  3677:                 foreach my $line (@userdata) {
                   3678:                     my %entries=&Apache::loncommon::record_sep($line);
1.1       raeburn  3679:                     unless (($entries{$fields{'username'}} eq '') ||
                   3680:                             (!defined($entries{$fields{'username'}}))) {
                   3681:                         delete($classlist->{$entries{$fields{'username'}}.
                   3682:                                                 ':'.$domain});
                   3683:                     }
                   3684:                 }
                   3685:                 # Print out list of dropped students.
1.30      raeburn  3686:                 &show_drop_list($r,$classlist,'nosort',$permission);
1.1       raeburn  3687:             }
                   3688:         }
                   3689:     } # end of unless
1.29      raeburn  3690:     if ($env{'form.fullup'} ne 'yes') {
                   3691:         $r->print('</form>');
                   3692:     }
1.1       raeburn  3693: }
                   3694: 
1.13      raeburn  3695: sub print_namespacing_alerts {
                   3696:     my ($domain,$alerts,$curr_rules) = @_;
                   3697:     my $output;
                   3698:     if (ref($alerts) eq 'HASH') {
                   3699:         if (keys(%{$alerts}) > 0) {
                   3700:             if (ref($alerts->{'username'}) eq 'HASH') {
                   3701:                 foreach my $dom (sort(keys(%{$alerts->{'username'}}))) {
                   3702:                     my $count;
                   3703:                     if (ref($alerts->{'username'}{$dom}) eq 'HASH') {
                   3704:                         $count = keys(%{$alerts->{'username'}{$dom}});
                   3705:                     }
                   3706:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   3707:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   3708:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   3709:                                         'username',$domdesc,$count,'upload');
                   3710:                     }
                   3711:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   3712:                                    $domdesc,$curr_rules->{$dom}{'username'},
                   3713:                                    'username');
                   3714:                 }
                   3715:             }
                   3716:             if (ref($alerts->{'id'}) eq 'HASH') {
                   3717:                 foreach my $dom (sort(keys(%{$alerts->{'id'}}))) {
                   3718:                     my $count;
                   3719:                     if (ref($alerts->{'id'}{$dom}) eq 'HASH') {
                   3720:                         $count = keys(%{$alerts->{'id'}{$dom}});
                   3721:                     }
                   3722:                     my $domdesc = &Apache::lonnet::domain($domain,'description');
                   3723:                     if (ref($curr_rules->{$dom}) eq 'HASH') {
                   3724:                         $output .= &Apache::loncommon::instrule_disallow_msg(
                   3725:                                               'id',$domdesc,$count,'upload');
                   3726:                     }
                   3727:                     $output .= &Apache::loncommon::user_rule_formats($dom,
                   3728:                                     $domdesc,$curr_rules->{$dom}{'id'},'id');
                   3729:                 }
                   3730:             }
                   3731:         }
                   3732:     }
                   3733: }
                   3734: 
1.1       raeburn  3735: sub user_change_result {
1.29      raeburn  3736:     my ($r,$userresult,$authresult,$roleresult,$idresult,$counts,$flushc,
                   3737:         $username,$userchg) = @_;
1.1       raeburn  3738:     my $okresult = 0;
                   3739:     if ($userresult ne 'ok') {
                   3740:         if ($userresult =~ /^error:(.+)$/) {
                   3741:             my $error = $1;
                   3742:             $r->print('<br />'.
                   3743:                   &mt('<b>[_1]</b>:  Unable to add/modify: [_2]',$username,$error));
                   3744:         }
                   3745:     } else {
                   3746:         $counts->{'user'} ++;
                   3747:         $okresult = 1;
                   3748:     }
                   3749:     if ($authresult ne 'ok') {
                   3750:         if ($authresult =~ /^error:(.+)$/) {
                   3751:             my $error = $1;
                   3752:             $r->print('<br />'.
                   3753:                   &mt('<b>[_1]</b>:  Unable to modify authentication: [_2]',$username,$error));
                   3754:         } 
                   3755:     } else {
                   3756:         $counts->{'auth'} ++;
                   3757:         $okresult = 1;
                   3758:     }
                   3759:     if ($roleresult ne 'ok') {
                   3760:         if ($roleresult =~ /^error:(.+)$/) {
                   3761:             my $error = $1;
                   3762:             $r->print('<br />'.
                   3763:                   &mt('<b>[_1]</b>:  Unable to add role: [_2]',$username,$error));
                   3764:         }
                   3765:     } else {
                   3766:         $counts->{'role'} ++;
                   3767:         $okresult = 1;
                   3768:     }
                   3769:     if ($okresult) {
                   3770:         $flushc++;
                   3771:         $userchg->{$username}=1;
                   3772:         $r->print('. ');
                   3773:         if ($flushc>15) {
                   3774:             $r->rflush;
                   3775:             $flushc=0;
                   3776:         }
                   3777:     }
1.29      raeburn  3778:     if ($idresult) {
                   3779:         $r->print($idresult);
                   3780:     }
1.1       raeburn  3781:     return $flushc;
                   3782: }
                   3783: 
                   3784: # ========================================================= Menu Phase Two Drop
1.17      raeburn  3785: sub print_drop_menu {
                   3786:     my ($r,$context,$permission) = @_;
                   3787:     $r->print('<h3>'.&mt("Drop Students").'</h3>'."\n".
                   3788:               '<form name="studentform" method="post">'."\n");
1.30      raeburn  3789:     my $classlist = &Apache::loncoursedata::get_classlist();
1.1       raeburn  3790:     if (! defined($classlist)) {
                   3791:         $r->print(&mt('There are no students currently enrolled.')."\n");
1.30      raeburn  3792:     } else {
                   3793:         &show_drop_list($r,$classlist,'nosort',$permission);
1.1       raeburn  3794:     }
1.17      raeburn  3795:     $r->print('</form>'. &Apache::loncommon::end_page());
1.1       raeburn  3796:     return;
                   3797: }
                   3798: 
                   3799: # ================================================================== Phase four
                   3800: 
1.11      raeburn  3801: sub update_user_list {
                   3802:     my ($r,$context,$setting,$choice) = @_;
                   3803:     my $now = time;
1.1       raeburn  3804:     my $count=0;
1.11      raeburn  3805:     my @changelist;
1.29      raeburn  3806:     if ($choice eq 'drop') {
                   3807:         @changelist = &Apache::loncommon::get_env_multiple('form.droplist');
                   3808:     } else {
1.11      raeburn  3809:         @changelist = &Apache::loncommon::get_env_multiple('form.actionlist');
                   3810:     }
                   3811:     my %result_text = ( ok    => { 'revoke'   => 'Revoked',
                   3812:                                    'delete'   => 'Deleted',
                   3813:                                    'reenable' => 'Re-enabled',
1.17      raeburn  3814:                                    'activate' => 'Activated',
                   3815:                                    'chgdates' => 'Changed Access Dates for',
                   3816:                                    'chgsec'   => 'Changed section for',
                   3817:                                    'drop'     => 'Dropped',
1.11      raeburn  3818:                                  },
                   3819:                         error => {'revoke'    => 'revoking',
                   3820:                                   'delete'    => 'deleting',
                   3821:                                   'reenable'  => 're-enabling',
                   3822:                                   'activate'  => 'activating',
1.17      raeburn  3823:                                   'chgdates'  => 'changing access dates for',
                   3824:                                   'chgsec'    => 'changing section for',
                   3825:                                   'drop'      => 'dropping',
1.11      raeburn  3826:                                  },
                   3827:                       );
                   3828:     my ($startdate,$enddate);
                   3829:     if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
                   3830:         ($startdate,$enddate) = &get_dates_from_form();
                   3831:     }
                   3832:     foreach my $item (@changelist) {
                   3833:         my ($role,$uname,$udom,$cid,$sec,$scope,$result,$type,$locktype,@sections,
                   3834:             $scopestem);
1.17      raeburn  3835:         if ($choice eq 'drop') {
                   3836:             ($uname,$udom,$sec) = split(/:/,$item,-1);
                   3837:             $role = 'st';
                   3838:             $cid = $env{'request.course.id'};
                   3839:             $scopestem = '/'.$cid;
                   3840:             $scopestem =~s/\_/\//g;
                   3841:             if ($sec eq '') {
                   3842:                 $scope = $scopestem;
                   3843:             } else {
                   3844:                 $scope = $scopestem.'/'.$sec;
                   3845:             }
                   3846:         } elsif ($context eq 'course') {
1.11      raeburn  3847:             ($uname,$udom,$role,$sec,$type,$locktype) = split(/\:/,$item,-1);
                   3848:             $cid = $env{'request.course.id'};
                   3849:             $scopestem = '/'.$cid;
                   3850:             $scopestem =~s/\_/\//g;
                   3851:             if ($sec eq '') {
                   3852:                 $scope = $scopestem;
                   3853:             } else {
                   3854:                 $scope = $scopestem.'/'.$sec;
                   3855:             }
1.13      raeburn  3856:         } elsif ($context eq 'author') {
1.11      raeburn  3857:             ($uname,$udom,$role) = split(/\:/,$item,-1);
                   3858:             $scope = '/'.$env{'user.domain'}.'/'.$env{'user.name'};
                   3859:         } elsif ($context eq 'domain') {
                   3860:             if ($setting eq 'domain') {
                   3861:                 ($role,$uname,$udom) = split(/\:/,$item,-1);
                   3862:                 $scope = '/'.$env{'request.role.domain'}.'/';
1.13      raeburn  3863:             } elsif ($setting eq 'author') { 
1.11      raeburn  3864:                 ($uname,$udom,$role,$scope) = split(/\:/,$item);
                   3865:             } elsif ($setting eq 'course') {
                   3866:                 ($uname,$udom,$role,$cid,$sec,$type,$locktype) = 
                   3867:                     split(/\:/,$item);
                   3868:                 $scope = '/'.$cid;
                   3869:                 $scope =~s/\_/\//g;
                   3870:                 if ($sec ne '') {
                   3871:                     $scope .= '/'.$sec;
                   3872:                 }
                   3873:             }
                   3874:         }
                   3875:         my $plrole = &Apache::lonnet::plaintext($role);
                   3876:         my $start = $env{'form.'.$item.'_start'};
                   3877:         my $end = $env{'form.'.$item.'_end'};
1.17      raeburn  3878:         if ($choice eq 'drop') {
                   3879:             # drop students
                   3880:             $end = $now;
                   3881:             $type = 'manual';
                   3882:             $result =
                   3883:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
                   3884:         } elsif ($choice eq 'revoke') {
                   3885:             # revoke or delete user role
1.11      raeburn  3886:             $end = $now; 
                   3887:             if ($role eq 'st') {
                   3888:                 $result = 
                   3889:                     &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
                   3890:             } else {
                   3891:                 $result = 
                   3892:                     &Apache::lonnet::revokerole($udom,$uname,$scope,$role);
                   3893:             }
                   3894:         } elsif ($choice eq 'delete') {
                   3895:             if ($role eq 'st') {
1.29      raeburn  3896:                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$now,$start,$type,$locktype,$cid);
                   3897:             }
                   3898:             $result =
                   3899:                 &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$now,
                   3900:                                             $start,1);
1.11      raeburn  3901:         } else {
                   3902:             #reenable, activate, change access dates or change section
                   3903:             if ($choice ne 'chgsec') {
                   3904:                 $start = $startdate; 
                   3905:                 $end = $enddate;
                   3906:             }
                   3907:             if ($choice eq 'reenable') {
                   3908:                 if ($role eq 'st') {
                   3909:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
                   3910:                 } else {
                   3911:                     $result = 
                   3912:                         &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
                   3913:                                                     $now);
                   3914:                 }
                   3915:             } elsif ($choice eq 'activate') {
                   3916:                 if ($role eq 'st') {
                   3917:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
                   3918:                 } else {
                   3919:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
                   3920:                                             $now);
                   3921:                 }
                   3922:             } elsif ($choice eq 'chgdates') {
                   3923:                 if ($role eq 'st') {
                   3924:                     $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$sec,$end,$start,$type,$locktype,$cid);
                   3925:                 } else {
                   3926:                     $result = &Apache::lonnet::assignrole($udom,$uname,$scope,$role,$end,
                   3927:                                                 $start);
                   3928:                 }
                   3929:             } elsif ($choice eq 'chgsec') {
                   3930:                 my (@newsecs,$revresult,$nochg,@retained);
                   3931:                 if ($role ne 'cc') {
                   3932:                     @newsecs = split(/,/,$env{'form.newsecs'});
                   3933:                 }
                   3934:                 # remove existing section if not to be retained.   
                   3935:                 if (!$env{'form.retainsec'}) {
                   3936:                     if ($sec eq '') {
                   3937:                         if (@newsecs == 0) {
                   3938:                             $result = &mt('No change in section assignment (none)');
                   3939:                             $nochg = 1;
                   3940:                         }
                   3941:                     } else {
1.28      raeburn  3942:                         if (@newsecs > 0) {
                   3943:                             if (grep(/^\Q$sec\E$/,@newsecs)) {
                   3944:                                 push(@retained,$sec);
                   3945:                             } else {
                   3946:                                 $revresult =
                   3947:                                     &Apache::lonnet::revokerole($udom,$uname,
                   3948:                                                                 $scope,$role);
                   3949:                             }
                   3950:                         } else {
1.11      raeburn  3951:                             $revresult =
1.28      raeburn  3952:                                 &Apache::lonnet::revokerole($udom,$uname,
                   3953:                                                             $scope,$role);
1.11      raeburn  3954:                         }
                   3955:                     }
                   3956:                 } else {
1.28      raeburn  3957:                     if ($sec eq '') {
                   3958:                         $nochg = 1;
                   3959:                     } else { 
                   3960:                         push(@retained,$sec);
                   3961:                     }
1.11      raeburn  3962:                 }
                   3963:                 # add new sections
                   3964:                 if (@newsecs == 0) {
                   3965:                     if (!$nochg) {
1.28      raeburn  3966:                         if ($role eq 'st') {
                   3967:                             $result = 
                   3968:                                 &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,undef,$end,$start,$type,$locktype,$cid);
                   3969:                         } else {
                   3970:                             my $newscope = $scopestem;
                   3971:                             $result = &Apache::lonnet::assignrole($udom,$uname,$newscope,$role,$end,$start);
1.11      raeburn  3972:                         }
                   3973:                     }
                   3974:                 } else {
                   3975:                     foreach my $newsec (@newsecs) { 
                   3976:                         if (!grep(/^\Q$newsec\E$/,@retained)) {
                   3977:                             if ($role eq 'st') {
                   3978:                                 $result = &Apache::lonnet::modify_student_enrollment($udom,$uname,undef,undef,undef,undef,undef,$newsec,$end,$start,$type,$locktype,$cid);
                   3979:                             } else {
                   3980:                                 my $newscope = $scopestem;
                   3981:                                 if ($newsec ne '') {
                   3982:                                    $newscope .= '/'.$newsec;
                   3983:                                 }
                   3984:                                 $result = &Apache::lonnet::assignrole($udom,$uname,
                   3985:                                                         $newscope,$role,$end,$start);
                   3986:                             }
                   3987:                         }
                   3988:                     }
                   3989:                 }
                   3990:             }
                   3991:         }
1.17      raeburn  3992:         my $extent = $scope;
                   3993:         if ($choice eq 'drop' || $context eq 'course') {
                   3994:             my ($cnum,$cdom,$cdesc) = &get_course_identity($cid);
                   3995:             if ($cdesc) {
                   3996:                 $extent = $cdesc;
                   3997:             }
                   3998:         }
1.1       raeburn  3999:         if ($result eq 'ok' || $result eq 'ok:') {
1.11      raeburn  4000:             $r->print(&mt("$result_text{'ok'}{$choice} role of '[_1]' in [_2] for [_3]",
1.17      raeburn  4001:                           $plrole,$extent,$uname.':'.$udom).'<br />');
1.1       raeburn  4002:             $count++;
                   4003:         } else {
                   4004:             $r->print(
1.29      raeburn  4005:                 &mt("Error $result_text{'error'}{$choice} [_1] in [_2] for [_3]: [_4].",
1.17      raeburn  4006:                     $plrole,$extent,$uname.':'.$udom,$result).'<br />');
1.11      raeburn  4007:         }
                   4008:     }
1.32      raeburn  4009:     $r->print('<form name="studentform" method="post" action="/adm/createuser">'."\n");
1.33      raeburn  4010:     if ($choice eq 'drop') {
                   4011:         $r->print('<input type="hidden" name="action" value="listusers" />'."\n".
                   4012:                   '<input type="hidden" name="Status" value="Active" />'."\n".
                   4013:                   '<input type="hidden" name="showrole" value="st" />'."\n");
                   4014:     } else {
                   4015:         foreach my $item ('action','sortby','roletype','showrole','Status','secfilter','grpfilter') {
                   4016:             if ($env{'form.'.$item} ne '') {
                   4017:                 $r->print('<input type="hidden" name="'.$item.'" value="'.$env{'form.'.$item}.
                   4018:                           '" />'."\n");
                   4019:             }
1.32      raeburn  4020:         }
                   4021:     }
1.29      raeburn  4022:     $r->print('<p><b>'.&mt("$result_text{'ok'}{$choice} role(s) for [quant,_1,user,users,no users].",$count).'</b></p>');
1.11      raeburn  4023:     if ($count > 0) {
1.17      raeburn  4024:         if ($choice eq 'revoke' || $choice eq 'drop') {
1.11      raeburn  4025:             $r->print('<p>'.&mt('Re-enabling will re-activate data for the role.</p>'));
                   4026:         }
                   4027:         # Flush the course logs so reverse user roles immediately updated
                   4028:         &Apache::lonnet::flushcourselogs();
                   4029:     }
                   4030:     if ($env{'form.makedatesdefault'}) {
                   4031:         if ($choice eq 'chgdates' || $choice eq 'reenable' || $choice eq 'activate') {
1.29      raeburn  4032:             $r->print(&make_dates_default($startdate,$enddate,$context));
1.1       raeburn  4033:         }
                   4034:     }
1.33      raeburn  4035:     my $linktext = &mt('Display User Lists');
                   4036:     if ($choice eq 'drop') {
                   4037:         $linktext = &mt('Display current class roster');
                   4038:     }
                   4039:     $r->print('<a href="javascript:document.studentform.submit()">'.$linktext.'</a></form>'."\n");
1.1       raeburn  4040: }
                   4041: 
1.8       raeburn  4042: sub classlist_drop {
1.29      raeburn  4043:     my ($scope,$uname,$udom,$now) = @_;
1.8       raeburn  4044:     my ($cdom,$cnum) = ($scope=~m{^/($match_domain)/($match_courseid)});
1.29      raeburn  4045:     if (&Apache::lonnet::is_course($cdom,$cnum)) {
                   4046:         my $user = $uname.':'.$udom;
1.8       raeburn  4047:         if (!&active_student_roles($cnum,$cdom,$uname,$udom)) {
                   4048:             my $result =
                   4049:                 &Apache::lonnet::cput('classlist',
1.29      raeburn  4050:                                       { $user => $now },$cdom,$cnum);
1.8       raeburn  4051:             return &mt('Drop from classlist: [_1]',
                   4052:                        '<b>'.$result.'</b>').'<br />';
                   4053:         }
                   4054:     }
                   4055: }
                   4056: 
                   4057: sub active_student_roles {
                   4058:     my ($cnum,$cdom,$uname,$udom) = @_;
                   4059:     my %roles =
                   4060:         &Apache::lonnet::get_my_roles($uname,$udom,'userroles',
                   4061:                                       ['future','active'],['st']);
                   4062:     return exists($roles{"$cnum:$cdom:st"});
                   4063: }
                   4064: 
1.1       raeburn  4065: sub section_check_js {
1.8       raeburn  4066:     my $groupslist= &get_groupslist();
1.1       raeburn  4067:     return <<"END";
                   4068: function validate(caller) {
1.9       raeburn  4069:     var groups = new Array($groupslist);
1.1       raeburn  4070:     var secname = caller.value;
                   4071:     if ((secname == 'all') || (secname == 'none')) {
                   4072:         alert("'"+secname+"' may not be used as the name for a section, as it is a reserved word.\\nPlease choose a different section name.");
                   4073:         return 'error';
                   4074:     }
                   4075:     if (secname != '') {
                   4076:         for (var k=0; k<groups.length; k++) {
                   4077:             if (secname == groups[k]) {
                   4078:                 alert("'"+secname+"' may not be used as the name for a section, as it is the name of a course group.\\nSection names and group names must be distinct. Please choose a different section name.");
                   4079:                 return 'error';
                   4080:             }
                   4081:         }
                   4082:     }
                   4083:     return 'ok';
                   4084: }
                   4085: END
                   4086: }
                   4087: 
                   4088: sub set_login {
                   4089:     my ($dom,$authformkrb,$authformint,$authformloc) = @_;
                   4090:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4091:     my $response;
                   4092:     my ($authnum,%can_assign) =
                   4093:         &Apache::loncommon::get_assignable_auth($dom);
                   4094:     if ($authnum) {
                   4095:         $response = &Apache::loncommon::start_data_table();
                   4096:         if (($can_assign{'krb4'}) || ($can_assign{'krb5'})) {
                   4097:             $response .= &Apache::loncommon::start_data_table_row().
                   4098:                          '<td>'.$authformkrb.'</td>'.
                   4099:                          &Apache::loncommon::end_data_table_row()."\n";
                   4100:         }
                   4101:         if ($can_assign{'int'}) {
                   4102:             $response .= &Apache::loncommon::start_data_table_row().
                   4103:                          '<td>'.$authformint.'</td>'.
                   4104:                          &Apache::loncommon::end_data_table_row()."\n"
                   4105:         }
                   4106:         if ($can_assign{'loc'}) {
                   4107:             $response .= &Apache::loncommon::start_data_table_row().
                   4108:                          '<td>'.$authformloc.'</td>'.
                   4109:                          &Apache::loncommon::end_data_table_row()."\n";
                   4110:         }
                   4111:         $response .= &Apache::loncommon::end_data_table();
                   4112:     }
                   4113:     return $response;
                   4114: }
                   4115: 
1.8       raeburn  4116: sub course_sections {
                   4117:     my ($sections_count,$role) = @_;
                   4118:     my $output = '';
                   4119:     my @sections = (sort {$a <=> $b} keys %{$sections_count});
1.29      raeburn  4120:     my $numsec = scalar(@sections);
                   4121:     if ($numsec <= 1) {
1.8       raeburn  4122:         $output = '<select name="currsec_'.$role.'" >'."\n".
1.29      raeburn  4123:                   '  <option value="">'.&mt('Select').'</option>'."\n".
                   4124:                   '  <option value="">'.&mt('No section').'</option>'."\n";
                   4125:         if ($numsec == 1) {
                   4126:             $output .=  
1.8       raeburn  4127:                   '  <option value="'.$sections[0].'" >'.$sections[0].'</option>'."\n";
1.29      raeburn  4128:         }
1.8       raeburn  4129:     } else {
                   4130:         $output = '<select name="currsec_'.$role.'" ';
                   4131:         my $multiple = 4;
                   4132:         if (scalar(@sections) < 4) { $multiple = scalar(@sections); }
1.29      raeburn  4133:         if ($role eq 'st') {
                   4134:             $output .= '>'."\n".
                   4135:                        '  <option value="">'.&mt('Select').'</option>'."\n".
                   4136:                        '  <option value="">'.&mt('No section')."</option>\n";
                   4137:         } else {
                   4138:             $output .= 'multiple="multiple" size="'.$multiple.'">'."\n";
                   4139:         }
1.8       raeburn  4140:         foreach my $sec (@sections) {
                   4141:             $output .= '<option value="'.$sec.'">'.$sec."</option>\n";
                   4142:         }
                   4143:     }
                   4144:     $output .= '</select>';
                   4145:     return $output;
                   4146: }
                   4147: 
                   4148: sub get_groupslist {
                   4149:     my $groupslist;
                   4150:     my %curr_groups = &Apache::longroup::coursegroups();
                   4151:     if (%curr_groups) {
                   4152:         $groupslist = join('","',sort(keys(%curr_groups)));
                   4153:         $groupslist = '"'.$groupslist.'"';
                   4154:     }
1.11      raeburn  4155:     return $groupslist; 
1.8       raeburn  4156: }
                   4157: 
                   4158: sub setsections_javascript {
1.37      raeburn  4159:     my ($formname,$groupslist,$mode,$checkauth) = @_;
1.28      raeburn  4160:     my ($checkincluded,$finish,$rolecode,$setsection_js);
                   4161:     if ($mode eq 'upload') {
                   4162:         $checkincluded = 'formname.name == "'.$formname.'"';
                   4163:         $finish = "return 'ok';";
                   4164:         $rolecode = "var role = formname.defaultrole.options[formname.defaultrole.selectedIndex].value;\n";
                   4165:     } elsif ($formname eq 'cu') {
1.8       raeburn  4166:         $checkincluded = 'formname.elements[i-1].checked == true';
1.37      raeburn  4167:         if ($checkauth) {
                   4168:             $finish = "var authcheck = auth_check();\n".
                   4169:                       "   if (authcheck == 'ok') {\n".
                   4170:                       "       formname.submit();\n".
                   4171:                       "   }\n";
                   4172:         } else {
                   4173:             $finish = 'formname.submit()';
                   4174:         }
1.28      raeburn  4175:         $rolecode = "var match = str.split('_');
                   4176:                 var role = match[3];\n";
                   4177:     } elsif ($formname eq 'enrollstudent') {
                   4178:         $checkincluded = 'formname.name == "'.$formname.'"';
1.37      raeburn  4179:         if ($checkauth) {
                   4180:             $finish = "var authcheck = auth_check();\n".
                   4181:                       "   if (authcheck == 'ok') {\n".
                   4182:                       "       formname.submit();\n".
                   4183:                       "   }\n";
                   4184:         } else {
                   4185:             $finish = 'formname.submit()';
                   4186:         }
1.28      raeburn  4187:         $rolecode = "var match = str.split('_');
                   4188:                 var role = match[1];\n";
1.8       raeburn  4189:     } else {
1.28      raeburn  4190:         $checkincluded = 'formname.name == "'.$formname.'"'; 
1.8       raeburn  4191:         $finish = "seccheck = 'ok';";
1.28      raeburn  4192:         $rolecode = "var match = str.split('_');
                   4193:                 var role = match[1];\n";
1.11      raeburn  4194:         $setsection_js = "var seccheck = 'alert';"; 
1.8       raeburn  4195:     }
                   4196:     my %alerts = &Apache::lonlocal::texthash(
                   4197:                     secd => 'Section designations do not apply to Course Coordinator roles.',
                   4198:                     accr => 'A course coordinator role will be added with access to all sections.',
                   4199:                     inea => 'In each course, each user may only have one student role at a time.',
                   4200:                     youh => 'You had selected ',
                   4201:                     secs => 'sections.',
                   4202:                     plmo => 'Please modify your selections so they include no more than one section.',
                   4203:                     mayn => 'may not be used as the name for a section, as it is a reserved word.',
                   4204:                     plch => 'Please choose a different section name.',
                   4205:                     mnot => 'may not be used as a section name, as it is the name of a course group.',
                   4206:                     secn => 'Section names and group names must be distinct. Please choose a different section name.',
1.11      raeburn  4207:                  );                
1.8       raeburn  4208:     $setsection_js .= <<"ENDSECCODE";
                   4209: 
                   4210: function setSections(formname) {
                   4211:     var re1 = /^currsec_/;
                   4212:     var groups = new Array($groupslist);
                   4213:     for (var i=0;i<formname.elements.length;i++) {
                   4214:         var str = formname.elements[i].name;
                   4215:         var checkcurr = str.match(re1);
                   4216:         if (checkcurr != null) {
                   4217:             if ($checkincluded) {
1.28      raeburn  4218:                 $rolecode
1.8       raeburn  4219:                 if (role == 'cc') {
                   4220:                     alert("$alerts{'secd'}\\n$alerts{'accr'}");
                   4221:                 }
                   4222:                 else {
                   4223:                     var sections = '';
                   4224:                     var numsec = 0;
                   4225:                     var sections;
                   4226:                     for (var j=0; j<formname.elements[i].length; j++) {
                   4227:                         if (formname.elements[i].options[j].selected == true ) {
                   4228:                             if (formname.elements[i].options[j].value != "") {
                   4229:                                 if (numsec == 0) {
                   4230:                                     if (formname.elements[i].options[j].value != "") {
                   4231:                                         sections = formname.elements[i].options[j].value;
                   4232:                                         numsec ++;
                   4233:                                     }
                   4234:                                 }
                   4235:                                 else {
                   4236:                                     sections = sections + "," +  formname.elements[i].options[j].value
                   4237:                                     numsec ++;
                   4238:                                 }
                   4239:                             }
                   4240:                         }
                   4241:                     }
                   4242:                     if (numsec > 0) {
                   4243:                         if (formname.elements[i+1].value != "" && formname.elements[i+1].value != null) {
                   4244:                             sections = sections + "," +  formname.elements[i+1].value;
                   4245:                         }
                   4246:                     }
                   4247:                     else {
                   4248:                         sections = formname.elements[i+1].value;
                   4249:                     }
                   4250:                     var newsecs = formname.elements[i+1].value;
                   4251:                     var numsplit;
                   4252:                     if (newsecs != null && newsecs != "") {
                   4253:                         numsplit = newsecs.split(/,/g);
                   4254:                         numsec = numsec + numsplit.length;
                   4255:                     }
                   4256: 
                   4257:                     if ((role == 'st') && (numsec > 1)) {
                   4258:                         alert("$alerts{'inea'} $alerts{'youh'} "+numsec+" $alerts{'secs'}\\n$alerts{'plmo'}")
                   4259:                         return;
                   4260:                     }
                   4261:                     else {
                   4262:                         if (numsplit != null) {
                   4263:                             for (var j=0; j<numsplit.length; j++) {
                   4264:                                 if ((numsplit[j] == 'all') ||
                   4265:                                     (numsplit[j] == 'none')) {
                   4266:                                     alert("'"+numsplit[j]+"' $alerts{'mayn'}\\n$alerts{'plch'}");
                   4267:                                     return;
                   4268:                                 }
                   4269:                                 for (var k=0; k<groups.length; k++) {
                   4270:                                     if (numsplit[j] == groups[k]) {
                   4271:                                         alert("'"+numsplit[j]+"' $alerts{'mnot'}\\n$alerts{'secn'}");
                   4272:                                         return;
                   4273:                                     }
                   4274:                                 }
                   4275:                             }
                   4276:                         }
                   4277:                         formname.elements[i+2].value = sections;
                   4278:                     }
                   4279:                 }
                   4280:             }
                   4281:         }
                   4282:     }
                   4283:     $finish
                   4284: }
                   4285: ENDSECCODE
1.11      raeburn  4286:     return $setsection_js; 
1.8       raeburn  4287: }
                   4288: 
1.15      raeburn  4289: sub can_create_user {
                   4290:     my ($dom,$context,$usertype) = @_;
                   4291:     my %domconf = &Apache::lonnet::get_dom('configuration',['usercreation'],$dom);
                   4292:     my $cancreate = 1;
1.28      raeburn  4293:     if (&Apache::lonnet::allowed('mau',$dom)) {
                   4294:         return $cancreate;
                   4295:     }
1.15      raeburn  4296:     if (ref($domconf{'usercreation'}) eq 'HASH') {
                   4297:         if (ref($domconf{'usercreation'}{'cancreate'}) eq 'HASH') {
                   4298:             if ($context eq 'course' || $context eq 'author') {
                   4299:                 my $creation = $domconf{'usercreation'}{'cancreate'}{$context};
                   4300:                 if ($creation eq 'none') {
                   4301:                     $cancreate = 0;
                   4302:                 } elsif ($creation ne 'any') {
                   4303:                     if (defined($usertype)) {
                   4304:                         if ($creation ne $usertype) {
                   4305:                             $cancreate = 0;
                   4306:                         }
                   4307:                     }
                   4308:                 }
                   4309:             }
                   4310:         }
                   4311:     }
                   4312:     return $cancreate;
                   4313: }
                   4314: 
1.20      raeburn  4315: sub can_modify_userinfo {
                   4316:     my ($context,$dom,$fields,$userroles) = @_;
                   4317:     my %domconfig =
                   4318:        &Apache::lonnet::get_dom('configuration',['usermodification'],
                   4319:                                 $dom);
                   4320:     my %canmodify;
                   4321:     if (ref($fields) eq 'ARRAY') {
                   4322:         foreach my $field (@{$fields}) {
                   4323:             $canmodify{$field}  = 0;
                   4324:             if (&Apache::lonnet::allowed('mau',$dom)) {
                   4325:                 $canmodify{$field} = 1;
                   4326:             } else {
                   4327:                 if (ref($domconfig{'usermodification'}) eq 'HASH') {
                   4328:                     if (ref($domconfig{'usermodification'}{$context}) eq 'HASH') {
                   4329:                         if (ref($userroles) eq 'ARRAY') {
                   4330:                             foreach my $role (@{$userroles}) {
                   4331:                                 my $testrole;
                   4332:                                 if ($role =~ /^cr\//) {
                   4333:                                     $testrole = 'cr';
                   4334:                                 } else {
                   4335:                                     $testrole = $role;
                   4336:                                 }
                   4337:                                 if (ref($domconfig{'usermodification'}{$context}{$testrole}) eq 'HASH') {
                   4338:                                     if ($domconfig{'usermodification'}{$context}{$testrole}{$field}) {
                   4339:                                         $canmodify{$field} = 1;
                   4340:                                         last;
                   4341:                                     }
                   4342:                                 }
                   4343:                             }
                   4344:                         } else {
                   4345:                             foreach my $key (keys(%{$domconfig{'usermodification'}{$context}})) {
                   4346:                                 if (ref($domconfig{'usermodification'}{$context}{$key}) eq 'HASH') {
                   4347:                                     if ($domconfig{'usermodification'}{$context}{$key}{$field}) {
                   4348:                                         $canmodify{$field} = 1;
                   4349:                                         last;
                   4350:                                     }
                   4351:                                 }
                   4352:                             }
                   4353:                         }
                   4354:                     }
                   4355:                 } elsif ($context eq 'course') {
                   4356:                     if (ref($userroles) eq 'ARRAY') {
                   4357:                         if (grep(/^st$/,@{$userroles})) {
                   4358:                             $canmodify{$field} = 1;
                   4359:                         }
                   4360:                     } else {
                   4361:                         $canmodify{$field} = 1;
                   4362:                     }
                   4363:                 }
                   4364:             }
                   4365:         }
                   4366:     }
                   4367:     return %canmodify;
                   4368: }
                   4369: 
1.18      raeburn  4370: sub check_usertype {
                   4371:     my ($dom,$uname,$rules) = @_;
                   4372:     my $usertype;
                   4373:     if (ref($rules) eq 'HASH') {
                   4374:         my @user_rules = keys(%{$rules});
                   4375:         if (@user_rules > 0) {
                   4376:             my %rule_check = &Apache::lonnet::inst_rulecheck($dom,$uname,undef,'username',\@user_rules);
                   4377:             if (keys(%rule_check) > 0) {
                   4378:                 $usertype = 'unofficial';
                   4379:                 foreach my $item (keys(%rule_check)) {
                   4380:                     if ($rule_check{$item}) {
                   4381:                         $usertype = 'official';
                   4382:                         last;
                   4383:                     }
                   4384:                 }
                   4385:             }
                   4386:         }
                   4387:     }
                   4388:     return $usertype;
                   4389: }
                   4390: 
1.17      raeburn  4391: sub roles_by_context {
                   4392:     my ($context,$custom) = @_;
                   4393:     my @allroles;
                   4394:     if ($context eq 'course') {
                   4395:         @allroles = ('st','ad','ta','ep','in','cc');
                   4396:         if ($custom) {
                   4397:             push(@allroles,'cr');
                   4398:         }
                   4399:     } elsif ($context eq 'author') {
                   4400:         @allroles = ('ca','aa');
                   4401:     } elsif ($context eq 'domain') {
                   4402:         @allroles = ('li','dg','sc','au','dc');
                   4403:     }
                   4404:     return @allroles;
                   4405: }
                   4406: 
1.16      raeburn  4407: sub get_permission {
1.17      raeburn  4408:     my ($context,$roles) = @_;
1.16      raeburn  4409:     my %permission;
                   4410:     if ($context eq 'course') {
1.17      raeburn  4411:         my $custom = 1;
                   4412:         my @allroles = &roles_by_context($context,$custom);
                   4413:         foreach my $role (@allroles) {
                   4414:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.course.id'})) {
                   4415:                 $permission{'cusr'} = 1;
                   4416:                 last;
                   4417:             }
1.16      raeburn  4418:         }
                   4419:         if (&Apache::lonnet::allowed('ccr',$env{'request.course.id'})) {
                   4420:             $permission{'custom'} = 1;
                   4421:         }
                   4422:         if (&Apache::lonnet::allowed('vcl',$env{'request.course.id'})) {
                   4423:             $permission{'view'} = 1;
                   4424:         }
                   4425:         if (!$permission{'view'}) {
                   4426:             my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   4427:             $permission{'view'} =  &Apache::lonnet::allowed('vcl',$scope);
                   4428:             if ($permission{'view'}) {
                   4429:                 $permission{'view_section'} = $env{'request.course.sec'};
                   4430:             }
                   4431:         }
1.17      raeburn  4432:         if (!$permission{'cusr'}) {
                   4433:             if ($env{'request.course.sec'} ne '') {
                   4434:                 my $scope = $env{'request.course.id'}.'/'.$env{'request.course.sec'};
                   4435:                 $permission{'cusr'} = (&Apache::lonnet::allowed('cst',$scope));
                   4436:                 if ($permission{'cusr'}) {
                   4437:                     $permission{'cusr_section'} = $env{'request.course.sec'};
                   4438:                 }
                   4439:             }
                   4440:         }
1.16      raeburn  4441:         if (&Apache::lonnet::allowed('mdg',$env{'request.course.id'})) {
                   4442:             $permission{'grp_manage'} = 1;
                   4443:         }
                   4444:     } elsif ($context eq 'author') {
                   4445:         $permission{'cusr'} = &authorpriv($env{'user.name'},$env{'request.role.domain'});
                   4446:         $permission{'view'} = $permission{'cusr'};
                   4447:     } else {
1.17      raeburn  4448:         my @allroles = &roles_by_context($context);
                   4449:         foreach my $role (@allroles) {
1.28      raeburn  4450:             if (&Apache::lonnet::allowed('c'.$role,$env{'request.role.domain'})) {
                   4451:                 $permission{'cusr'} = 1;
1.17      raeburn  4452:                 last;
                   4453:             }
                   4454:         }
                   4455:         if (!$permission{'cusr'}) {
                   4456:             if (&Apache::lonnet::allowed('mau',$env{'request.role.domain'})) {
                   4457:                 $permission{'cusr'} = 1;
                   4458:             }
1.16      raeburn  4459:         }
                   4460:         if (&Apache::lonnet::allowed('ccr',$env{'request.role.domain'})) {
                   4461:             $permission{'custom'} = 1;
                   4462:         }
                   4463:         $permission{'view'} = $permission{'cusr'};
                   4464:     }
                   4465:     my $allowed = 0;
                   4466:     foreach my $perm (values(%permission)) {
                   4467:         if ($perm) { $allowed=1; last; }
                   4468:     }
                   4469:     return (\%permission,$allowed);
                   4470: }
                   4471: 
                   4472: # ==================================================== Figure out author access
                   4473: 
                   4474: sub authorpriv {
                   4475:     my ($auname,$audom)=@_;
                   4476:     unless ((&Apache::lonnet::allowed('cca',$audom.'/'.$auname))
                   4477:          || (&Apache::lonnet::allowed('caa',$audom.'/'.$auname))) { return ''; }    return 1;
                   4478: }
                   4479: 
1.27      raeburn  4480: sub roles_on_upload {
                   4481:     my ($context,%customroles) = @_;
                   4482:     my (@possible_roles,@permitted_roles);
                   4483:     if ($context eq 'domain') {
                   4484:         @possible_roles = &curr_role_permissions($context,undef,1);
                   4485:         push(@possible_roles,&curr_role_permissions($context,'course',1));
                   4486:     } else {
                   4487:         @possible_roles = &curr_role_permissions($context,undef,1);
                   4488:     }
                   4489:     my $custom_ok = 0;
                   4490:     foreach my $role (@possible_roles) {
                   4491:         if ($role eq 'cr') {
                   4492:             $custom_ok = 1;
                   4493:             push(@permitted_roles,keys(%customroles));
                   4494:         } else {
                   4495:             push(@permitted_roles,$role);
                   4496:         }
                   4497:     }
                   4498:     return ($custom_ok,@permitted_roles);
                   4499: }
                   4500: 
1.17      raeburn  4501: sub get_course_identity {
                   4502:     my ($cid) = @_;
                   4503:     my ($cnum,$cdom,$cdesc);
                   4504:     if ($cid eq '') {
                   4505:         $cid = $env{'request.course.id'}
                   4506:     }
                   4507:     if ($cid ne '') {
                   4508:         $cnum = $env{'course.'.$cid.'.num'};
                   4509:         $cdom = $env{'course.'.$cid.'.domain'};
                   4510:         $cdesc = $env{'course.'.$cid.'.description'};
                   4511:         if ($cnum eq '' || $cdom eq '') {
                   4512:             my %coursehash =
                   4513:                 &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
                   4514:             $cdom = $coursehash{'domain'};
                   4515:             $cnum = $coursehash{'num'};
                   4516:             $cdesc = $coursehash{'description'};
                   4517:         }
                   4518:     }
                   4519:     return ($cnum,$cdom,$cdesc);
                   4520: }
                   4521: 
1.19      raeburn  4522: sub dc_setcourse_js {
1.37      raeburn  4523:     my ($formname,$mode,$context) = @_;
                   4524:     my ($dc_setcourse_code,$authen_check);
1.19      raeburn  4525:     my $cctext = &Apache::lonnet::plaintext('cc');
                   4526:     my %alerts = &sectioncheck_alerts();
                   4527:     my $role = 'role';
                   4528:     if ($mode eq 'upload') {
                   4529:         $role = 'courserole';
1.37      raeburn  4530:     } else {
                   4531:         $authen_check = &verify_authen($formname,$context);
1.19      raeburn  4532:     }
                   4533:     $dc_setcourse_code = (<<"SCRIPTTOP");
1.37      raeburn  4534: $authen_check
                   4535: 
1.19      raeburn  4536: function setCourse() {
                   4537:     var course = document.$formname.dccourse.value;
                   4538:     if (course != "") {
                   4539:         if (document.$formname.dcdomain.value != document.$formname.origdom.value) {
                   4540:             alert("$alerts{'curd'}");
                   4541:             return;
                   4542:         }
                   4543:         var userrole = document.$formname.$role.options[document.$formname.$role.selectedIndex].value
                   4544:         var section="";
                   4545:         var numsections = 0;
                   4546:         var newsecs = new Array();
                   4547:         for (var i=0; i<document.$formname.currsec.length; i++) {
                   4548:             if (document.$formname.currsec.options[i].selected == true ) {
                   4549:                 if (document.$formname.currsec.options[i].value != "" && document.$formname.currsec.options[i].value != null) {
                   4550:                     if (numsections == 0) {
                   4551:                         section = document.$formname.currsec.options[i].value
                   4552:                         numsections = 1;
                   4553:                     }
                   4554:                     else {
                   4555:                         section = section + "," +  document.$formname.currsec.options[i].value
                   4556:                         numsections ++;
                   4557:                     }
                   4558:                 }
                   4559:             }
                   4560:         }
                   4561:         if (document.$formname.newsec.value != "" && document.$formname.newsec.value != null) {
                   4562:             if (numsections == 0) {
                   4563:                 section = document.$formname.newsec.value
                   4564:             }
                   4565:             else {
                   4566:                 section = section + "," +  document.$formname.newsec.value
                   4567:             }
                   4568:             newsecs = document.$formname.newsec.value.split(/,/g);
                   4569:             numsections = numsections + newsecs.length;
                   4570:         }
                   4571:         if ((userrole == 'st') && (numsections > 1)) {
                   4572:             alert("$alerts{'inea'}. $alerts{'youh'} "+numsections+" $alerts{'sect'}.\\n$alerts{'plsm'}.")
                   4573:             return;
                   4574:         }
                   4575:         for (var j=0; j<newsecs.length; j++) {
                   4576:             if ((newsecs[j] == 'all') || (newsecs[j] == 'none')) {
                   4577:                 alert("'"+newsecs[j]+"' $alerts{'mayn'}.\\n$alerts{'plsc'}.");
                   4578:                 return;
                   4579:             }
                   4580:             if (document.$formname.groups.value != '') {
                   4581:                 var groups = document.$formname.groups.value.split(/,/g);
                   4582:                 for (var k=0; k<groups.length; k++) {
                   4583:                     if (newsecs[j] == groups[k]) {
                   4584:                         alert("'"+newsecs[j]+"' $alerts{'mayt'}.\\n$alerts{'secn'}. $alerts{'plsc'}.");
                   4585:                         return;
                   4586:                     }
                   4587:                 }
                   4588:             }
                   4589:         }
                   4590:         if ((userrole == 'cc') && (numsections > 0)) {
                   4591:             alert("$alerts{'secd'} $cctext $alerts{'role'}.\\n$alerts{'accr'}.");
                   4592:             section = "";
                   4593:         }
                   4594: SCRIPTTOP
                   4595:     if ($mode ne 'upload') {
                   4596:         $dc_setcourse_code .= (<<"ENDSCRIPT");
                   4597:         var coursename = "_$env{'request.role.domain'}"+"_"+course+"_"+userrole
                   4598:         var numcourse = getIndex(document.$formname.dccourse);
                   4599:         if (numcourse == "-1") {
                   4600:             alert("$alerts{'thwa'}");
                   4601:             return;
                   4602:         }
                   4603:         else {
                   4604:             document.$formname.elements[numcourse].name = "act"+coursename;
                   4605:             var numnewsec = getIndex(document.$formname.newsec);
                   4606:             if (numnewsec != "-1") {
                   4607:                 document.$formname.elements[numnewsec].name = "sec"+coursename;
                   4608:                 document.$formname.elements[numnewsec].value = section;
                   4609:             }
                   4610:             var numstart = getIndex(document.$formname.start);
                   4611:             if (numstart != "-1") {
                   4612:                 document.$formname.elements[numstart].name = "start"+coursename;
                   4613:             }
                   4614:             var numend = getIndex(document.$formname.end);
                   4615:             if (numend != "-1") {
                   4616:                 document.$formname.elements[numend].name = "end"+coursename
                   4617:             }
                   4618:         }
                   4619:     }
1.37      raeburn  4620:     var authcheck = auth_check();
                   4621:     if (authcheck == 'ok') {
                   4622:         document.$formname.submit();
                   4623:     }
1.19      raeburn  4624: }
                   4625: ENDSCRIPT
                   4626:     } else {
                   4627:         $dc_setcourse_code .=  "
                   4628:         document.$formname.sections.value = section;
                   4629:     }
                   4630:     return 'ok';
                   4631: }
                   4632: ";
                   4633:     }
                   4634:     $dc_setcourse_code .= (<<"ENDSCRIPT");
                   4635: 
                   4636:     function getIndex(caller) {
                   4637:         for (var i=0;i<document.$formname.elements.length;i++) {
                   4638:             if (document.$formname.elements[i] == caller) {
                   4639:                 return i;
                   4640:             }
                   4641:         }
                   4642:         return -1;
                   4643:     }
                   4644: ENDSCRIPT
1.37      raeburn  4645:     return $dc_setcourse_code;
                   4646: }
                   4647: 
                   4648: sub verify_authen {
                   4649:     my ($formname,$context) = @_;
                   4650:     my %alerts = &authcheck_alerts();
                   4651:     my $finish = "return 'ok';";
                   4652:     if ($context eq 'author') {
                   4653:         $finish = "document.$formname.submit();";
                   4654:     }
                   4655:     my $outcome = <<"ENDSCRIPT";
                   4656: 
                   4657: function auth_check() {
                   4658:     var logintype;
                   4659:     if (document.$formname.login.length) {
                   4660:         if (document.$formname.login.length > 0) {
                   4661:             var loginpicked = 0;
                   4662:             for (var i=0; i<document.$formname.login.length; i++) {
                   4663:                 if (document.$formname.login[i].checked == true) {
                   4664:                     loginpicked = 1;
                   4665:                     logintype = document.$formname.login[i].value;
                   4666:                 }
                   4667:             }
                   4668:             if (loginpicked == 0) {
                   4669:                 alert("$alerts{'authen'}");
                   4670:                 return;
                   4671:             }
                   4672:         }
                   4673:     } else {
                   4674:         logintype = document.$formname.login.value;
                   4675:     }
                   4676:     if (logintype == 'nochange') {
                   4677:         return 'ok';
                   4678:     }
                   4679:     var argpicked = document.$formname.elements[logintype+'arg'].value;
                   4680:     if ((argpicked == null) || (argpicked == '') || (typeof argpicked == 'undefined')) {
                   4681:         var alertmsg = '';
                   4682:         switch (logintype) {
                   4683:             case 'krb':
                   4684:                 alertmsg = '$alerts{'krb'}';
                   4685:                 break;
                   4686:             case 'int':
                   4687:                 alertmsg = '$alerts{'ipass'}';
                   4688:             case 'fsys':
                   4689:                 alertmsg = '$alerts{'ipass'}';
                   4690:                 break;
                   4691:             case 'loc':
                   4692:                 alertmsg = '';
                   4693:                 break;
                   4694:             default:
                   4695:                 alertmsg = '';
                   4696:         }
                   4697:         if (alertmsg != '') {
                   4698:             alert(alertmsg);
                   4699:             return;
                   4700:         }
                   4701:     }
                   4702:     $finish
                   4703: }
                   4704: ENDSCRIPT
1.19      raeburn  4705: }
                   4706: 
                   4707: sub sectioncheck_alerts {
                   4708:     my %alerts = &Apache::lonlocal::texthash(
                   4709:                     curd => 'You must select a course in the current domain',
                   4710:                     inea => 'In each course, each user may only have one student role at a time',
                   4711:                     youh => 'You had selected',
                   4712:                     sect => 'sections',
                   4713:                     plsm => 'Please modify your selections so they include no more than one section',
                   4714:                     mayn => 'may not be used as the name for a section, as it is a reserved word',
                   4715:                     plsc => 'Please choose a different section name',
                   4716:                     mayt => 'may not be used as the name for a section, as it is the name of a course group',
                   4717:                     secn => 'Section names and group names must be distinct',
                   4718:                     secd => 'Section designations do not apply to ',
                   4719:                     role => 'roles',
                   4720:                     accr => 'role will be added with access to all sections',
                   4721:                     thwa => 'There was a problem with your course selection'
                   4722:                  );
                   4723:     return %alerts;
                   4724: }
1.17      raeburn  4725: 
1.37      raeburn  4726: sub authcheck_alerts {
                   4727:     my %alerts = 
                   4728:         &Apache::lonlocal::texthash(
                   4729:                     authen => 'You must choose an authentication type.',
                   4730:                     krb    => 'You need to specify the Kerberos domain.',
                   4731:                     ipass  => 'You need to specify the initial password.',
                   4732:         );
                   4733:     return %alerts;
                   4734: }
                   4735: 
1.1       raeburn  4736: 1;
                   4737: 

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