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

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

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