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

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

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